org.el 813 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  74. (when (fboundp 'defvaralias)
  75. (unless (boundp 'calendar-view-holidays-initially-flag)
  76. (defvaralias 'calendar-view-holidays-initially-flag
  77. 'view-calendar-holidays-initially))
  78. (unless (boundp 'calendar-view-diary-initially-flag)
  79. (defvaralias 'calendar-view-diary-initially-flag
  80. 'view-diary-entries-initially))
  81. (unless (boundp 'diary-fancy-buffer)
  82. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  83. (require 'outline) (require 'noutline)
  84. ;; Other stuff we need.
  85. (require 'time-date)
  86. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  87. (require 'easymenu)
  88. (require 'overlay)
  89. (require 'org-macs)
  90. (require 'org-entities)
  91. (require 'org-compat)
  92. (require 'org-faces)
  93. (require 'org-list)
  94. (require 'org-pcomplete)
  95. (require 'org-src)
  96. (require 'org-footnote)
  97. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  98. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  99. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  100. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  101. (declare-function org-at-clock-log-p "org-clock" ())
  102. (declare-function org-clock-timestamps-up "org-clock" ())
  103. (declare-function org-clock-timestamps-down "org-clock" ())
  104. ;; babel
  105. (require 'ob)
  106. (require 'ob-table)
  107. (require 'ob-lob)
  108. (require 'ob-ref)
  109. (require 'ob-tangle)
  110. (require 'ob-comint)
  111. (require 'ob-keys)
  112. ;; load languages based on value of `org-babel-load-languages'
  113. (defvar org-babel-load-languages)
  114. ;;;###autoload
  115. (defun org-babel-do-load-languages (sym value)
  116. "Load the languages defined in `org-babel-load-languages'."
  117. (set-default sym value)
  118. (mapc (lambda (pair)
  119. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  120. (if active
  121. (progn
  122. (require (intern (concat "ob-" lang))))
  123. (progn
  124. (funcall 'fmakunbound
  125. (intern (concat "org-babel-execute:" lang)))
  126. (funcall 'fmakunbound
  127. (intern (concat "org-babel-expand-body:" lang)))))))
  128. org-babel-load-languages))
  129. (defcustom org-babel-load-languages '((emacs-lisp . t))
  130. "Languages which can be evaluated in Org-mode buffers.
  131. This list can be used to load support for any of the languages
  132. below, note that each language will depend on a different set of
  133. system executables and/or Emacs modes. When a language is
  134. \"loaded\", then code blocks in that language can be evaluated
  135. with `org-babel-execute-src-block' bound by default to C-c
  136. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  137. be set to remove code block evaluation from the C-c C-c
  138. keybinding. By default only Emacs Lisp (which has no
  139. requirements) is loaded."
  140. :group 'org-babel
  141. :set 'org-babel-do-load-languages
  142. :version "24.1"
  143. :type '(alist :tag "Babel Languages"
  144. :key-type
  145. (choice
  146. (const :tag "Awk" awk)
  147. (const :tag "C" C)
  148. (const :tag "R" R)
  149. (const :tag "Asymptote" asymptote)
  150. (const :tag "Calc" calc)
  151. (const :tag "Clojure" clojure)
  152. (const :tag "CSS" css)
  153. (const :tag "Ditaa" ditaa)
  154. (const :tag "Dot" dot)
  155. (const :tag "Emacs Lisp" emacs-lisp)
  156. (const :tag "Fortran" fortran)
  157. (const :tag "Gnuplot" gnuplot)
  158. (const :tag "Haskell" haskell)
  159. (const :tag "IO" io)
  160. (const :tag "Java" java)
  161. (const :tag "Javascript" js)
  162. (const :tag "LaTeX" latex)
  163. (const :tag "Ledger" ledger)
  164. (const :tag "Lilypond" lilypond)
  165. (const :tag "Maxima" maxima)
  166. (const :tag "Matlab" matlab)
  167. (const :tag "Mscgen" mscgen)
  168. (const :tag "Ocaml" ocaml)
  169. (const :tag "Octave" octave)
  170. (const :tag "Org" org)
  171. (const :tag "Perl" perl)
  172. (const :tag "Pico Lisp" picolisp)
  173. (const :tag "PlantUML" plantuml)
  174. (const :tag "Python" python)
  175. (const :tag "Ruby" ruby)
  176. (const :tag "Sass" sass)
  177. (const :tag "Scala" scala)
  178. (const :tag "Scheme" scheme)
  179. (const :tag "Screen" screen)
  180. (const :tag "Shell Script" sh)
  181. (const :tag "Shen" shen)
  182. (const :tag "Sql" sql)
  183. (const :tag "Sqlite" sqlite))
  184. :value-type (boolean :tag "Activate" :value t)))
  185. ;;;; Customization variables
  186. (defcustom org-clone-delete-id nil
  187. "Remove ID property of clones of a subtree.
  188. When non-nil, clones of a subtree don't inherit the ID property.
  189. Otherwise they inherit the ID property with a new unique
  190. identifier."
  191. :type 'boolean
  192. :version "24.1"
  193. :group 'org-id)
  194. ;;; Version
  195. (defvaralias 'org-version 'org-release)
  196. ;;;###autoload
  197. (defun org-version (&optional here)
  198. "Show the org-mode version in the echo area.
  199. With prefix arg HERE, insert it at point."
  200. (interactive "P")
  201. (let* ((origin default-directory)
  202. (version (if (boundp 'org-release) org-release "N/A"))
  203. (git-version (if (boundp 'org-git-version) org-git-version "N/A"))
  204. (org-install (ignore-errors (find-library-name "org-install"))))
  205. (setq version (format "Org-mode version %s (%s @ %s)"
  206. version
  207. git-version
  208. (if org-install org-install "org-install.el can not be found!")))
  209. (if here (insert version))
  210. (message version)))
  211. ;;; Compatibility constants
  212. ;;; The custom variables
  213. (defgroup org nil
  214. "Outline-based notes management and organizer."
  215. :tag "Org"
  216. :group 'outlines
  217. :group 'calendar)
  218. (defcustom org-mode-hook nil
  219. "Mode hook for Org-mode, run after the mode was turned on."
  220. :group 'org
  221. :type 'hook)
  222. (defcustom org-load-hook nil
  223. "Hook that is run after org.el has been loaded."
  224. :group 'org
  225. :type 'hook)
  226. (defcustom org-log-buffer-setup-hook nil
  227. "Hook that is run after an Org log buffer is created."
  228. :group 'org
  229. :version "24.1"
  230. :type 'hook)
  231. (defvar org-modules) ; defined below
  232. (defvar org-modules-loaded nil
  233. "Have the modules been loaded already?")
  234. (defun org-load-modules-maybe (&optional force)
  235. "Load all extensions listed in `org-modules'."
  236. (when (or force (not org-modules-loaded))
  237. (mapc (lambda (ext)
  238. (condition-case nil (require ext)
  239. (error (message "Problems while trying to load feature `%s'" ext))))
  240. org-modules)
  241. (setq org-modules-loaded t)))
  242. (defun org-set-modules (var value)
  243. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  244. (set var value)
  245. (when (featurep 'org)
  246. (org-load-modules-maybe 'force)))
  247. (when (org-bound-and-true-p org-modules)
  248. (let ((a (member 'org-infojs org-modules)))
  249. (and a (setcar a 'org-jsinfo))))
  250. (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)
  251. "Modules that should always be loaded together with org.el.
  252. If a description starts with <C>, the file is not part of Emacs
  253. and loading it will require that you have downloaded and properly installed
  254. the org-mode distribution.
  255. You can also use this system to load external packages (i.e. neither Org
  256. core modules, nor modules from the CONTRIB directory). Just add symbols
  257. to the end of the list. If the package is called org-xyz.el, then you need
  258. to add the symbol `xyz', and the package must have a call to
  259. (provide 'org-xyz)"
  260. :group 'org
  261. :set 'org-set-modules
  262. :type
  263. '(set :greedy t
  264. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  265. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  266. (const :tag " crypt: Encryption of subtrees" org-crypt)
  267. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  268. (const :tag " docview: Links to doc-view buffers" org-docview)
  269. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  270. (const :tag " id: Global IDs for identifying entries" org-id)
  271. (const :tag " info: Links to Info nodes" org-info)
  272. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  273. (const :tag " habit: Track your consistency with habits" org-habit)
  274. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  275. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  276. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  277. (const :tag " mew Links to Mew folders/messages" org-mew)
  278. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  279. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  280. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  281. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  282. (const :tag " vm: Links to VM folders/messages" org-vm)
  283. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  284. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  285. (const :tag " mouse: Additional mouse support" org-mouse)
  286. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  287. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  288. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  289. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  290. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  291. (const :tag "C collector: Collect properties into tables" org-collector)
  292. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  293. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  294. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  295. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  296. (const :tag "C eval: Include command output as text" org-eval)
  297. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  298. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  299. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  300. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  301. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  302. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  303. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  304. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  305. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  306. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  307. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  308. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  309. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  310. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  311. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  312. (const :tag "C registry: A registry for Org-mode links" org-registry)
  313. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  314. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  315. (const :tag "C secretary: Team management with org-mode" org-secretary)
  316. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  317. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  318. (const :tag "C track: Keep up with Org-mode development" org-track)
  319. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  320. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  321. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  322. (defcustom org-support-shift-select nil
  323. "Non-nil means make shift-cursor commands select text when possible.
  324. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  325. start selecting a region, or enlarge regions started in this way.
  326. In Org-mode, in special contexts, these same keys are used for
  327. other purposes, important enough to compete with shift selection.
  328. Org tries to balance these needs by supporting `shift-select-mode'
  329. outside these special contexts, under control of this variable.
  330. The default of this variable is nil, to avoid confusing behavior. Shifted
  331. cursor keys will then execute Org commands in the following contexts:
  332. - on a headline, changing TODO state (left/right) and priority (up/down)
  333. - on a time stamp, changing the time
  334. - in a plain list item, changing the bullet type
  335. - in a property definition line, switching between allowed values
  336. - in the BEGIN line of a clock table (changing the time block).
  337. Outside these contexts, the commands will throw an error.
  338. When this variable is t and the cursor is not in a special
  339. context, Org-mode will support shift-selection for making and
  340. enlarging regions. To make this more effective, the bullet
  341. cycling will no longer happen anywhere in an item line, but only
  342. if the cursor is exactly on the bullet.
  343. If you set this variable to the symbol `always', then the keys
  344. will not be special in headlines, property lines, and item lines,
  345. to make shift selection work there as well. If this is what you
  346. want, you can use the following alternative commands: `C-c C-t'
  347. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  348. can be used to switch TODO sets, `C-c -' to cycle item bullet
  349. types, and properties can be edited by hand or in column view.
  350. However, when the cursor is on a timestamp, shift-cursor commands
  351. will still edit the time stamp - this is just too good to give up.
  352. XEmacs user should have this variable set to nil, because
  353. `shift-select-mode' is in Emacs 23 or later only."
  354. :group 'org
  355. :type '(choice
  356. (const :tag "Never" nil)
  357. (const :tag "When outside special context" t)
  358. (const :tag "Everywhere except timestamps" always)))
  359. (defcustom org-loop-over-headlines-in-active-region nil
  360. "Shall some commands act upon headlines in the active region?
  361. When set to `t', some commands will be performed in all headlines
  362. within the active region.
  363. When set to `start-level', some commands will be performed in all
  364. headlines within the active region, provided that these headlines
  365. are of the same level than the first one.
  366. When set to a string, those commands will be performed on the
  367. matching headlines within the active region. Such string must be
  368. a tags/property/todo match as it is used in the agenda tags view.
  369. The list of commands is: `org-schedule', `org-deadline',
  370. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  371. `org-archive-to-archive-sibling'. The archiving commands skip
  372. already archived entries."
  373. :type '(choice (const :tag "Don't loop" nil)
  374. (const :tag "All headlines in active region" t)
  375. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  376. (string :tag "Tags/Property/Todo matcher"))
  377. :version "24.1"
  378. :group 'org-todo
  379. :group 'org-archive)
  380. (defgroup org-startup nil
  381. "Options concerning startup of Org-mode."
  382. :tag "Org Startup"
  383. :group 'org)
  384. (defcustom org-startup-folded t
  385. "Non-nil means entering Org-mode will switch to OVERVIEW.
  386. This can also be configured on a per-file basis by adding one of
  387. the following lines anywhere in the buffer:
  388. #+STARTUP: fold (or `overview', this is equivalent)
  389. #+STARTUP: nofold (or `showall', this is equivalent)
  390. #+STARTUP: content
  391. #+STARTUP: showeverything"
  392. :group 'org-startup
  393. :type '(choice
  394. (const :tag "nofold: show all" nil)
  395. (const :tag "fold: overview" t)
  396. (const :tag "content: all headlines" content)
  397. (const :tag "show everything, even drawers" showeverything)))
  398. (defcustom org-startup-truncated t
  399. "Non-nil means entering Org-mode will set `truncate-lines'.
  400. This is useful since some lines containing links can be very long and
  401. uninteresting. Also tables look terrible when wrapped."
  402. :group 'org-startup
  403. :type 'boolean)
  404. (defcustom org-startup-indented nil
  405. "Non-nil means turn on `org-indent-mode' on startup.
  406. This can also be configured on a per-file basis by adding one of
  407. the following lines anywhere in the buffer:
  408. #+STARTUP: indent
  409. #+STARTUP: noindent"
  410. :group 'org-structure
  411. :type '(choice
  412. (const :tag "Not" nil)
  413. (const :tag "Globally (slow on startup in large files)" t)))
  414. (defcustom org-use-sub-superscripts t
  415. "Non-nil means interpret \"_\" and \"^\" for export.
  416. When this option is turned on, you can use TeX-like syntax for sub- and
  417. superscripts. Several characters after \"_\" or \"^\" will be
  418. considered as a single item - so grouping with {} is normally not
  419. needed. For example, the following things will be parsed as single
  420. sub- or superscripts.
  421. 10^24 or 10^tau several digits will be considered 1 item.
  422. 10^-12 or 10^-tau a leading sign with digits or a word
  423. x^2-y^3 will be read as x^2 - y^3, because items are
  424. terminated by almost any nonword/nondigit char.
  425. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  426. Still, ambiguity is possible - so when in doubt use {} to enclose the
  427. sub/superscript. If you set this variable to the symbol `{}',
  428. the braces are *required* in order to trigger interpretations as
  429. sub/superscript. This can be helpful in documents that need \"_\"
  430. frequently in plain text.
  431. Not all export backends support this, but HTML does.
  432. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  433. :group 'org-startup
  434. :group 'org-export-translation
  435. :version "24.1"
  436. :type '(choice
  437. (const :tag "Always interpret" t)
  438. (const :tag "Only with braces" {})
  439. (const :tag "Never interpret" nil)))
  440. (if (fboundp 'defvaralias)
  441. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  442. (defcustom org-startup-with-beamer-mode nil
  443. "Non-nil means turn on `org-beamer-mode' on startup.
  444. This can also be configured on a per-file basis by adding one of
  445. the following lines anywhere in the buffer:
  446. #+STARTUP: beamer"
  447. :group 'org-startup
  448. :version "24.1"
  449. :type 'boolean)
  450. (defcustom org-startup-align-all-tables nil
  451. "Non-nil means align all tables when visiting a file.
  452. This is useful when the column width in tables is forced with <N> cookies
  453. in table fields. Such tables will look correct only after the first re-align.
  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: align
  457. #+STARTUP: noalign"
  458. :group 'org-startup
  459. :type 'boolean)
  460. (defcustom org-startup-with-inline-images nil
  461. "Non-nil means show inline images when loading a new Org file.
  462. This can also be configured on a per-file basis by adding one of
  463. the following lines anywhere in the buffer:
  464. #+STARTUP: inlineimages
  465. #+STARTUP: noinlineimages"
  466. :group 'org-startup
  467. :version "24.1"
  468. :type 'boolean)
  469. (defcustom org-insert-mode-line-in-empty-file nil
  470. "Non-nil means insert the first line setting Org-mode in empty files.
  471. When the function `org-mode' is called interactively in an empty file, this
  472. normally means that the file name does not automatically trigger Org-mode.
  473. To ensure that the file will always be in Org-mode in the future, a
  474. line enforcing Org-mode will be inserted into the buffer, if this option
  475. has been set."
  476. :group 'org-startup
  477. :type 'boolean)
  478. (defcustom org-replace-disputed-keys nil
  479. "Non-nil means use alternative key bindings for some keys.
  480. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  481. These keys are also used by other packages like shift-selection-mode'
  482. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  483. If you want to use Org-mode together with one of these other modes,
  484. or more generally if you would like to move some Org-mode commands to
  485. other keys, set this variable and configure the keys with the variable
  486. `org-disputed-keys'.
  487. This option is only relevant at load-time of Org-mode, and must be set
  488. *before* org.el is loaded. Changing it requires a restart of Emacs to
  489. become effective."
  490. :group 'org-startup
  491. :type 'boolean)
  492. (defcustom org-use-extra-keys nil
  493. "Non-nil means use extra key sequence definitions for certain commands.
  494. This happens automatically if you run XEmacs or if `window-system'
  495. is nil. This variable lets you do the same manually. You must
  496. set it before loading org.
  497. Example: on Carbon Emacs 22 running graphically, with an external
  498. keyboard on a Powerbook, the default way of setting M-left might
  499. not work for either Alt or ESC. Setting this variable will make
  500. it work for ESC."
  501. :group 'org-startup
  502. :type 'boolean)
  503. (if (fboundp 'defvaralias)
  504. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  505. (defcustom org-disputed-keys
  506. '(([(shift up)] . [(meta p)])
  507. ([(shift down)] . [(meta n)])
  508. ([(shift left)] . [(meta -)])
  509. ([(shift right)] . [(meta +)])
  510. ([(control shift right)] . [(meta shift +)])
  511. ([(control shift left)] . [(meta shift -)]))
  512. "Keys for which Org-mode and other modes compete.
  513. This is an alist, cars are the default keys, second element specifies
  514. the alternative to use when `org-replace-disputed-keys' is t.
  515. Keys can be specified in any syntax supported by `define-key'.
  516. The value of this option takes effect only at Org-mode's startup,
  517. therefore you'll have to restart Emacs to apply it after changing."
  518. :group 'org-startup
  519. :type 'alist)
  520. (defun org-key (key)
  521. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  522. Or return the original if not disputed.
  523. Also apply the translations defined in `org-xemacs-key-equivalents'."
  524. (when org-replace-disputed-keys
  525. (let* ((nkey (key-description key))
  526. (x (org-find-if (lambda (x)
  527. (equal (key-description (car x)) nkey))
  528. org-disputed-keys)))
  529. (setq key (if x (cdr x) key))))
  530. (when (featurep 'xemacs)
  531. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  532. key)
  533. (defun org-find-if (predicate seq)
  534. (catch 'exit
  535. (while seq
  536. (if (funcall predicate (car seq))
  537. (throw 'exit (car seq))
  538. (pop seq)))))
  539. (defun org-defkey (keymap key def)
  540. "Define a key, possibly translated, as returned by `org-key'."
  541. (define-key keymap (org-key key) def))
  542. (defcustom org-ellipsis nil
  543. "The ellipsis to use in the Org-mode outline.
  544. When nil, just use the standard three dots. When a string, use that instead,
  545. When a face, use the standard 3 dots, but with the specified face.
  546. The change affects only Org-mode (which will then use its own display table).
  547. Changing this requires executing `M-x org-mode' in a buffer to become
  548. effective."
  549. :group 'org-startup
  550. :type '(choice (const :tag "Default" nil)
  551. (face :tag "Face" :value org-warning)
  552. (string :tag "String" :value "...#")))
  553. (defvar org-display-table nil
  554. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  555. (defgroup org-keywords nil
  556. "Keywords in Org-mode."
  557. :tag "Org Keywords"
  558. :group 'org)
  559. (defcustom org-deadline-string "DEADLINE:"
  560. "String to mark deadline entries.
  561. A deadline is this string, followed by a time stamp. Should be a word,
  562. terminated by a colon. You can insert a schedule keyword and
  563. a timestamp with \\[org-deadline].
  564. Changes become only effective after restarting Emacs."
  565. :group 'org-keywords
  566. :type 'string)
  567. (defcustom org-scheduled-string "SCHEDULED:"
  568. "String to mark scheduled TODO entries.
  569. A schedule is this string, followed by a time stamp. Should be a word,
  570. terminated by a colon. You can insert a schedule keyword and
  571. a timestamp with \\[org-schedule].
  572. Changes become only effective after restarting Emacs."
  573. :group 'org-keywords
  574. :type 'string)
  575. (defcustom org-closed-string "CLOSED:"
  576. "String used as the prefix for timestamps logging closing a TODO entry."
  577. :group 'org-keywords
  578. :type 'string)
  579. (defcustom org-clock-string "CLOCK:"
  580. "String used as prefix for timestamps clocking work hours on an item."
  581. :group 'org-keywords
  582. :type 'string)
  583. (defcustom org-comment-string "COMMENT"
  584. "Entries starting with this keyword will never be exported.
  585. An entry can be toggled between COMMENT and normal with
  586. \\[org-toggle-comment].
  587. Changes become only effective after restarting Emacs."
  588. :group 'org-keywords
  589. :type 'string)
  590. (defcustom org-quote-string "QUOTE"
  591. "Entries starting with this keyword will be exported in fixed-width font.
  592. Quoting applies only to the text in the entry following the headline, and does
  593. not extend beyond the next headline, even if that is lower level.
  594. An entry can be toggled between QUOTE and normal with
  595. \\[org-toggle-fixed-width-section]."
  596. :group 'org-keywords
  597. :type 'string)
  598. (defconst org-repeat-re
  599. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  600. "Regular expression for specifying repeated events.
  601. After a match, group 1 contains the repeat expression.")
  602. (defgroup org-structure nil
  603. "Options concerning the general structure of Org-mode files."
  604. :tag "Org Structure"
  605. :group 'org)
  606. (defgroup org-reveal-location nil
  607. "Options about how to make context of a location visible."
  608. :tag "Org Reveal Location"
  609. :group 'org-structure)
  610. (defconst org-context-choice
  611. '(choice
  612. (const :tag "Always" t)
  613. (const :tag "Never" nil)
  614. (repeat :greedy t :tag "Individual contexts"
  615. (cons
  616. (choice :tag "Context"
  617. (const agenda)
  618. (const org-goto)
  619. (const occur-tree)
  620. (const tags-tree)
  621. (const link-search)
  622. (const mark-goto)
  623. (const bookmark-jump)
  624. (const isearch)
  625. (const default))
  626. (boolean))))
  627. "Contexts for the reveal options.")
  628. (defcustom org-show-hierarchy-above '((default . t))
  629. "Non-nil means show full hierarchy when revealing a location.
  630. Org-mode often shows locations in an org-mode file which might have
  631. been invisible before. When this is set, the hierarchy of headings
  632. above the exposed location is shown.
  633. Turning this off for example for sparse trees makes them very compact.
  634. Instead of t, this can also be an alist specifying this option for different
  635. contexts. Valid contexts are
  636. agenda when exposing an entry from the agenda
  637. org-goto when using the command `org-goto' on key C-c C-j
  638. occur-tree when using the command `org-occur' on key C-c /
  639. tags-tree when constructing a sparse tree based on tags matches
  640. link-search when exposing search matches associated with a link
  641. mark-goto when exposing the jump goal of a mark
  642. bookmark-jump when exposing a bookmark location
  643. isearch when exiting from an incremental search
  644. default default for all contexts not set explicitly"
  645. :group 'org-reveal-location
  646. :type org-context-choice)
  647. (defcustom org-show-following-heading '((default . nil))
  648. "Non-nil means show following heading when revealing a location.
  649. Org-mode often shows locations in an org-mode file which might have
  650. been invisible before. When this is set, the heading following the
  651. match is shown.
  652. Turning this off for example for sparse trees makes them very compact,
  653. but makes it harder to edit the location of the match. In such a case,
  654. use the command \\[org-reveal] to show more context.
  655. Instead of t, this can also be an alist specifying this option for different
  656. contexts. See `org-show-hierarchy-above' for valid contexts."
  657. :group 'org-reveal-location
  658. :type org-context-choice)
  659. (defcustom org-show-siblings '((default . nil) (isearch t))
  660. "Non-nil means show all sibling heading when revealing a location.
  661. Org-mode often shows locations in an org-mode file which might have
  662. been invisible before. When this is set, the sibling of the current entry
  663. heading are all made visible. If `org-show-hierarchy-above' is t,
  664. the same happens on each level of the hierarchy above the current entry.
  665. By default this is on for the isearch context, off for all other contexts.
  666. Turning this off for example for sparse trees makes them very compact,
  667. but makes it harder to edit the location of the match. In such a case,
  668. use the command \\[org-reveal] to show more context.
  669. Instead of t, this can also be an alist specifying this option for different
  670. contexts. See `org-show-hierarchy-above' for valid contexts."
  671. :group 'org-reveal-location
  672. :type org-context-choice)
  673. (defcustom org-show-entry-below '((default . nil))
  674. "Non-nil means show the entry below a headline when revealing a location.
  675. Org-mode often shows locations in an org-mode file which might have
  676. been invisible before. When this is set, the text below the headline that is
  677. exposed is also shown.
  678. By default this is off for all contexts.
  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-indirect-buffer-display 'other-window
  684. "How should indirect tree buffers be displayed?
  685. This applies to indirect buffers created with the commands
  686. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  687. Valid values are:
  688. current-window Display in the current window
  689. other-window Just display in another window.
  690. dedicated-frame Create one new frame, and re-use it each time.
  691. new-frame Make a new frame each time. Note that in this case
  692. previously-made indirect buffers are kept, and you need to
  693. kill these buffers yourself."
  694. :group 'org-structure
  695. :group 'org-agenda-windows
  696. :type '(choice
  697. (const :tag "In current window" current-window)
  698. (const :tag "In current frame, other window" other-window)
  699. (const :tag "Each time a new frame" new-frame)
  700. (const :tag "One dedicated frame" dedicated-frame)))
  701. (defcustom org-use-speed-commands nil
  702. "Non-nil means activate single letter commands at beginning of a headline.
  703. This may also be a function to test for appropriate locations where speed
  704. commands should be active."
  705. :group 'org-structure
  706. :type '(choice
  707. (const :tag "Never" nil)
  708. (const :tag "At beginning of headline stars" t)
  709. (function)))
  710. (defcustom org-speed-commands-user nil
  711. "Alist of additional speed commands.
  712. This list will be checked before `org-speed-commands-default'
  713. when the variable `org-use-speed-commands' is non-nil
  714. and when the cursor is at the beginning of a headline.
  715. The car if each entry is a string with a single letter, which must
  716. be assigned to `self-insert-command' in the global map.
  717. The cdr is either a command to be called interactively, a function
  718. to be called, or a form to be evaluated.
  719. An entry that is just a list with a single string will be interpreted
  720. as a descriptive headline that will be added when listing the speed
  721. commands in the Help buffer using the `?' speed command."
  722. :group 'org-structure
  723. :type '(repeat :value ("k" . ignore)
  724. (choice :value ("k" . ignore)
  725. (list :tag "Descriptive Headline" (string :tag "Headline"))
  726. (cons :tag "Letter and Command"
  727. (string :tag "Command letter")
  728. (choice
  729. (function)
  730. (sexp))))))
  731. (defgroup org-cycle nil
  732. "Options concerning visibility cycling in Org-mode."
  733. :tag "Org Cycle"
  734. :group 'org-structure)
  735. (defcustom org-cycle-skip-children-state-if-no-children t
  736. "Non-nil means skip CHILDREN state in entries that don't have any."
  737. :group 'org-cycle
  738. :type 'boolean)
  739. (defcustom org-cycle-max-level nil
  740. "Maximum level which should still be subject to visibility cycling.
  741. Levels higher than this will, for cycling, be treated as text, not a headline.
  742. When `org-odd-levels-only' is set, a value of N in this variable actually
  743. means 2N-1 stars as the limiting headline.
  744. When nil, cycle all levels.
  745. Note that the limiting level of cycling is also influenced by
  746. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  747. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  748. than its value."
  749. :group 'org-cycle
  750. :type '(choice
  751. (const :tag "No limit" nil)
  752. (integer :tag "Maximum level")))
  753. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  754. "Names of drawers. Drawers are not opened by cycling on the headline above.
  755. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  756. this:
  757. :DRAWERNAME:
  758. .....
  759. :END:
  760. The drawer \"PROPERTIES\" is special for capturing properties through
  761. the property API.
  762. Drawers can be defined on the per-file basis with a line like:
  763. #+DRAWERS: HIDDEN STATE PROPERTIES"
  764. :group 'org-structure
  765. :group 'org-cycle
  766. :type '(repeat (string :tag "Drawer Name")))
  767. (defcustom org-hide-block-startup nil
  768. "Non-nil means entering Org-mode will fold all blocks.
  769. This can also be set in on a per-file basis with
  770. #+STARTUP: hideblocks
  771. #+STARTUP: showblocks"
  772. :group 'org-startup
  773. :group 'org-cycle
  774. :type 'boolean)
  775. (defcustom org-cycle-global-at-bob nil
  776. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  777. This makes it possible to do global cycling without having to use S-TAB or
  778. \\[universal-argument] TAB. For this special case to work, the first line \
  779. of the buffer
  780. must not be a headline - it may be empty or some other text. When used in
  781. this way, `org-cycle-hook' is disables temporarily, to make sure the
  782. cursor stays at the beginning of the buffer.
  783. When this option is nil, don't do anything special at the beginning
  784. of the buffer."
  785. :group 'org-cycle
  786. :type 'boolean)
  787. (defcustom org-cycle-level-after-item/entry-creation t
  788. "Non-nil means cycle entry level or item indentation in new empty entries.
  789. When the cursor is at the end of an empty headline, i.e with only stars
  790. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  791. and then all possible ancestor states, before returning to the original state.
  792. This makes data entry extremely fast: M-RET to create a new headline,
  793. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  794. When the cursor is at the end of an empty plain list item, one TAB will
  795. make it a subitem, two or more tabs will back up to make this an item
  796. higher up in the item hierarchy."
  797. :group 'org-cycle
  798. :type 'boolean)
  799. (defcustom org-cycle-emulate-tab t
  800. "Where should `org-cycle' emulate TAB.
  801. nil Never
  802. white Only in completely white lines
  803. whitestart Only at the beginning of lines, before the first non-white char
  804. t Everywhere except in headlines
  805. exc-hl-bol Everywhere except at the start of a headline
  806. If TAB is used in a place where it does not emulate TAB, the current subtree
  807. visibility is cycled."
  808. :group 'org-cycle
  809. :type '(choice (const :tag "Never" nil)
  810. (const :tag "Only in completely white lines" white)
  811. (const :tag "Before first char in a line" whitestart)
  812. (const :tag "Everywhere except in headlines" t)
  813. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  814. ))
  815. (defcustom org-cycle-separator-lines 2
  816. "Number of empty lines needed to keep an empty line between collapsed trees.
  817. If you leave an empty line between the end of a subtree and the following
  818. headline, this empty line is hidden when the subtree is folded.
  819. Org-mode will leave (exactly) one empty line visible if the number of
  820. empty lines is equal or larger to the number given in this variable.
  821. So the default 2 means at least 2 empty lines after the end of a subtree
  822. are needed to produce free space between a collapsed subtree and the
  823. following headline.
  824. If the number is negative, and the number of empty lines is at least -N,
  825. all empty lines are shown.
  826. Special case: when 0, never leave empty lines in collapsed view."
  827. :group 'org-cycle
  828. :type 'integer)
  829. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  830. (defcustom org-pre-cycle-hook nil
  831. "Hook that is run before visibility cycling is happening.
  832. The function(s) in this hook must accept a single argument which indicates
  833. the new state that will be set right after running this hook. The
  834. argument is a symbol. Before a global state change, it can have the values
  835. `overview', `content', or `all'. Before a local state change, it can have
  836. the values `folded', `children', or `subtree'."
  837. :group 'org-cycle
  838. :type 'hook)
  839. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  840. org-cycle-hide-drawers
  841. org-cycle-show-empty-lines
  842. org-optimize-window-after-visibility-change)
  843. "Hook that is run after `org-cycle' has changed the buffer visibility.
  844. The function(s) in this hook must accept a single argument which indicates
  845. the new state that was set by the most recent `org-cycle' command. The
  846. argument is a symbol. After a global state change, it can have the values
  847. `overview', `content', or `all'. After a local state change, it can have
  848. the values `folded', `children', or `subtree'."
  849. :group 'org-cycle
  850. :type 'hook)
  851. (defgroup org-edit-structure nil
  852. "Options concerning structure editing in Org-mode."
  853. :tag "Org Edit Structure"
  854. :group 'org-structure)
  855. (defcustom org-odd-levels-only nil
  856. "Non-nil means skip even levels and only use odd levels for the outline.
  857. This has the effect that two stars are being added/taken away in
  858. promotion/demotion commands. It also influences how levels are
  859. handled by the exporters.
  860. Changing it requires restart of `font-lock-mode' to become effective
  861. for fontification also in regions already fontified.
  862. You may also set this on a per-file basis by adding one of the following
  863. lines to the buffer:
  864. #+STARTUP: odd
  865. #+STARTUP: oddeven"
  866. :group 'org-edit-structure
  867. :group 'org-appearance
  868. :type 'boolean)
  869. (defcustom org-adapt-indentation t
  870. "Non-nil means adapt indentation to outline node level.
  871. When this variable is set, Org assumes that you write outlines by
  872. indenting text in each node to align with the headline (after the stars).
  873. The following issues are influenced by this variable:
  874. - When this is set and the *entire* text in an entry is indented, the
  875. indentation is increased by one space in a demotion command, and
  876. decreased by one in a promotion command. If any line in the entry
  877. body starts with text at column 0, indentation is not changed at all.
  878. - Property drawers and planning information is inserted indented when
  879. this variable s set. When nil, they will not be indented.
  880. - TAB indents a line relative to context. The lines below a headline
  881. will be indented when this variable is set.
  882. Note that this is all about true indentation, by adding and removing
  883. space characters. See also `org-indent.el' which does level-dependent
  884. indentation in a virtual way, i.e. at display time in Emacs."
  885. :group 'org-edit-structure
  886. :type 'boolean)
  887. (defcustom org-special-ctrl-a/e nil
  888. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  889. When t, `C-a' will bring back the cursor to the beginning of the
  890. headline text, i.e. after the stars and after a possible TODO
  891. keyword. In an item, this will be the position after bullet and
  892. check-box, if any. When the cursor is already at that position,
  893. another `C-a' will bring it to the beginning of the line.
  894. `C-e' will jump to the end of the headline, ignoring the presence
  895. of tags in the headline. A second `C-e' will then jump to the
  896. true end of the line, after any tags. This also means that, when
  897. this variable is non-nil, `C-e' also will never jump beyond the
  898. end of the heading of a folded section, i.e. not after the
  899. ellipses.
  900. When set to the symbol `reversed', the first `C-a' or `C-e' works
  901. normally, going to the true line boundary first. Only a directly
  902. following, identical keypress will bring the cursor to the
  903. special positions.
  904. This may also be a cons cell where the behavior for `C-a' and
  905. `C-e' is set separately."
  906. :group 'org-edit-structure
  907. :type '(choice
  908. (const :tag "off" nil)
  909. (const :tag "on: after stars/bullet and before tags first" t)
  910. (const :tag "reversed: true line boundary first" reversed)
  911. (cons :tag "Set C-a and C-e separately"
  912. (choice :tag "Special C-a"
  913. (const :tag "off" nil)
  914. (const :tag "on: after stars/bullet first" t)
  915. (const :tag "reversed: before stars/bullet first" reversed))
  916. (choice :tag "Special C-e"
  917. (const :tag "off" nil)
  918. (const :tag "on: before tags first" t)
  919. (const :tag "reversed: after tags first" reversed)))))
  920. (if (fboundp 'defvaralias)
  921. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  922. (defcustom org-special-ctrl-k nil
  923. "Non-nil means `C-k' will behave specially in headlines.
  924. When nil, `C-k' will call the default `kill-line' command.
  925. When t, the following will happen while the cursor is in the headline:
  926. - When the cursor is at the beginning of a headline, kill the entire
  927. line and possible the folded subtree below the line.
  928. - When in the middle of the headline text, kill the headline up to the tags.
  929. - When after the headline text, kill the tags."
  930. :group 'org-edit-structure
  931. :type 'boolean)
  932. (defcustom org-ctrl-k-protect-subtree nil
  933. "Non-nil means, do not delete a hidden subtree with C-k.
  934. When set to the symbol `error', simply throw an error when C-k is
  935. used to kill (part-of) a headline that has hidden text behind it.
  936. Any other non-nil value will result in a query to the user, if it is
  937. OK to kill that hidden subtree. When nil, kill without remorse."
  938. :group 'org-edit-structure
  939. :version "24.1"
  940. :type '(choice
  941. (const :tag "Do not protect hidden subtrees" nil)
  942. (const :tag "Protect hidden subtrees with a security query" t)
  943. (const :tag "Never kill a hidden subtree with C-k" error)))
  944. (defcustom org-catch-invisible-edits nil
  945. "Check if in invisible region before inserting or deleting a character.
  946. Valid values are:
  947. nil Do not check, so just do invisible edits.
  948. error Throw an error and do nothing.
  949. show Make point visible, and do the requested edit.
  950. show-and-error Make point visible, then throw an error and abort the edit.
  951. smart Make point visible, and do insertion/deletion if it is
  952. adjacent to visible text and the change feels predictable.
  953. Never delete a previously invisible character or add in the
  954. middle or right after an invisible region. Basically, this
  955. allows insertion and backward-delete right before ellipses.
  956. FIXME: maybe in this case we should not even show?"
  957. :group 'org-edit-structure
  958. :version "24.1"
  959. :type '(choice
  960. (const :tag "Do not check" nil)
  961. (const :tag "Throw error when trying to edit" error)
  962. (const :tag "Unhide, but do not do the edit" show-and-error)
  963. (const :tag "Show invisible part and do the edit" show)
  964. (const :tag "Be smart and do the right thing" smart)))
  965. (defcustom org-yank-folded-subtrees t
  966. "Non-nil means when yanking subtrees, fold them.
  967. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  968. it starts with a heading and all other headings in it are either children
  969. or siblings, then fold all the subtrees. However, do this only if no
  970. text after the yank would be swallowed into a folded tree by this action."
  971. :group 'org-edit-structure
  972. :type 'boolean)
  973. (defcustom org-yank-adjusted-subtrees nil
  974. "Non-nil means when yanking subtrees, adjust the level.
  975. With this setting, `org-paste-subtree' is used to insert the subtree, see
  976. this function for details."
  977. :group 'org-edit-structure
  978. :type 'boolean)
  979. (defcustom org-M-RET-may-split-line '((default . t))
  980. "Non-nil means M-RET will split the line at the cursor position.
  981. When nil, it will go to the end of the line before making a
  982. new line.
  983. You may also set this option in a different way for different
  984. contexts. Valid contexts are:
  985. headline when creating a new headline
  986. item when creating a new item
  987. table in a table field
  988. default the value to be used for all contexts not explicitly
  989. customized"
  990. :group 'org-structure
  991. :group 'org-table
  992. :type '(choice
  993. (const :tag "Always" t)
  994. (const :tag "Never" nil)
  995. (repeat :greedy t :tag "Individual contexts"
  996. (cons
  997. (choice :tag "Context"
  998. (const headline)
  999. (const item)
  1000. (const table)
  1001. (const default))
  1002. (boolean)))))
  1003. (defcustom org-insert-heading-respect-content nil
  1004. "Non-nil means insert new headings after the current subtree.
  1005. When nil, the new heading is created directly after the current line.
  1006. The commands \\[org-insert-heading-respect-content] and
  1007. \\[org-insert-todo-heading-respect-content] turn this variable on
  1008. for the duration of the command."
  1009. :group 'org-structure
  1010. :type 'boolean)
  1011. (defcustom org-blank-before-new-entry '((heading . auto)
  1012. (plain-list-item . auto))
  1013. "Should `org-insert-heading' leave a blank line before new heading/item?
  1014. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1015. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1016. which case Org will look at the surrounding headings/items and try to
  1017. make an intelligent decision whether to insert a blank line or not.
  1018. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1019. set, the setting here is ignored and no empty line is inserted, to avoid
  1020. breaking the list structure."
  1021. :group 'org-edit-structure
  1022. :type '(list
  1023. (cons (const heading)
  1024. (choice (const :tag "Never" nil)
  1025. (const :tag "Always" t)
  1026. (const :tag "Auto" auto)))
  1027. (cons (const plain-list-item)
  1028. (choice (const :tag "Never" nil)
  1029. (const :tag "Always" t)
  1030. (const :tag "Auto" auto)))))
  1031. (defcustom org-insert-heading-hook nil
  1032. "Hook being run after inserting a new heading."
  1033. :group 'org-edit-structure
  1034. :type 'hook)
  1035. (defcustom org-enable-fixed-width-editor t
  1036. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1037. This currently only means they are never auto-wrapped.
  1038. When nil, such lines will be treated like ordinary lines.
  1039. See also the QUOTE keyword."
  1040. :group 'org-edit-structure
  1041. :type 'boolean)
  1042. (defcustom org-goto-auto-isearch t
  1043. "Non-nil means typing characters in `org-goto' starts incremental search."
  1044. :group 'org-edit-structure
  1045. :type 'boolean)
  1046. (defgroup org-sparse-trees nil
  1047. "Options concerning sparse trees in Org-mode."
  1048. :tag "Org Sparse Trees"
  1049. :group 'org-structure)
  1050. (defcustom org-highlight-sparse-tree-matches t
  1051. "Non-nil means highlight all matches that define a sparse tree.
  1052. The highlights will automatically disappear the next time the buffer is
  1053. changed by an edit command."
  1054. :group 'org-sparse-trees
  1055. :type 'boolean)
  1056. (defcustom org-remove-highlights-with-change t
  1057. "Non-nil means any change to the buffer will remove temporary highlights.
  1058. Such highlights are created by `org-occur' and `org-clock-display'.
  1059. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1060. The highlights created by `org-preview-latex-fragment' always need
  1061. `C-c C-c' to be removed."
  1062. :group 'org-sparse-trees
  1063. :group 'org-time
  1064. :type 'boolean)
  1065. (defcustom org-occur-hook '(org-first-headline-recenter)
  1066. "Hook that is run after `org-occur' has constructed a sparse tree.
  1067. This can be used to recenter the window to show as much of the structure
  1068. as possible."
  1069. :group 'org-sparse-trees
  1070. :type 'hook)
  1071. (defgroup org-imenu-and-speedbar nil
  1072. "Options concerning imenu and speedbar in Org-mode."
  1073. :tag "Org Imenu and Speedbar"
  1074. :group 'org-structure)
  1075. (defcustom org-imenu-depth 2
  1076. "The maximum level for Imenu access to Org-mode headlines.
  1077. This also applied for speedbar access."
  1078. :group 'org-imenu-and-speedbar
  1079. :type 'integer)
  1080. (defgroup org-table nil
  1081. "Options concerning tables in Org-mode."
  1082. :tag "Org Table"
  1083. :group 'org)
  1084. (defcustom org-enable-table-editor 'optimized
  1085. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1086. When nil, such lines will be treated like ordinary lines.
  1087. When equal to the symbol `optimized', the table editor will be optimized to
  1088. do the following:
  1089. - Automatic overwrite mode in front of whitespace in table fields.
  1090. This makes the structure of the table stay in tact as long as the edited
  1091. field does not exceed the column width.
  1092. - Minimize the number of realigns. Normally, the table is aligned each time
  1093. TAB or RET are pressed to move to another field. With optimization this
  1094. happens only if changes to a field might have changed the column width.
  1095. Optimization requires replacing the functions `self-insert-command',
  1096. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1097. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1098. very good at guessing when a re-align will be necessary, but you can always
  1099. force one with \\[org-ctrl-c-ctrl-c].
  1100. If you would like to use the optimized version in Org-mode, but the
  1101. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1102. This variable can be used to turn on and off the table editor during a session,
  1103. but in order to toggle optimization, a restart is required.
  1104. See also the variable `org-table-auto-blank-field'."
  1105. :group 'org-table
  1106. :type '(choice
  1107. (const :tag "off" nil)
  1108. (const :tag "on" t)
  1109. (const :tag "on, optimized" optimized)))
  1110. (defcustom org-self-insert-cluster-for-undo t
  1111. "Non-nil means cluster self-insert commands for undo when possible.
  1112. If this is set, then, like in the Emacs command loop, 20 consecutive
  1113. characters will be undone together.
  1114. This is configurable, because there is some impact on typing performance."
  1115. :group 'org-table
  1116. :type 'boolean)
  1117. (defcustom org-table-tab-recognizes-table.el t
  1118. "Non-nil means TAB will automatically notice a table.el table.
  1119. When it sees such a table, it moves point into it and - if necessary -
  1120. calls `table-recognize-table'."
  1121. :group 'org-table-editing
  1122. :type 'boolean)
  1123. (defgroup org-link nil
  1124. "Options concerning links in Org-mode."
  1125. :tag "Org Link"
  1126. :group 'org)
  1127. (defvar org-link-abbrev-alist-local nil
  1128. "Buffer-local version of `org-link-abbrev-alist', which see.
  1129. The value of this is taken from the #+LINK lines.")
  1130. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1131. (defcustom org-link-abbrev-alist nil
  1132. "Alist of link abbreviations.
  1133. The car of each element is a string, to be replaced at the start of a link.
  1134. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1135. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1136. [[linkkey:tag][description]]
  1137. The 'linkkey' must be a word word, starting with a letter, followed
  1138. by letters, numbers, '-' or '_'.
  1139. If REPLACE is a string, the tag will simply be appended to create the link.
  1140. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1141. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1142. be inserted at that point (see the function `url-hexify-string').
  1143. REPLACE may also be a function that will be called with the tag as the
  1144. only argument to create the link, which should be returned as a string.
  1145. See the manual for examples."
  1146. :group 'org-link
  1147. :type '(repeat
  1148. (cons
  1149. (string :tag "Protocol")
  1150. (choice
  1151. (string :tag "Format")
  1152. (function)))))
  1153. (defcustom org-descriptive-links t
  1154. "Non-nil means Org will display descriptive links.
  1155. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1156. \"Org Website\", hiding the link itself and just displaying its
  1157. description. When set to `nil', Org will display the full links
  1158. literally.
  1159. You can interactively set the value of this variable by calling
  1160. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1161. :group 'org-link
  1162. :type 'boolean)
  1163. (defcustom org-link-file-path-type 'adaptive
  1164. "How the path name in file links should be stored.
  1165. Valid values are:
  1166. relative Relative to the current directory, i.e. the directory of the file
  1167. into which the link is being inserted.
  1168. absolute Absolute path, if possible with ~ for home directory.
  1169. noabbrev Absolute path, no abbreviation of home directory.
  1170. adaptive Use relative path for files in the current directory and sub-
  1171. directories of it. For other files, use an absolute path."
  1172. :group 'org-link
  1173. :type '(choice
  1174. (const relative)
  1175. (const absolute)
  1176. (const noabbrev)
  1177. (const adaptive)))
  1178. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1179. "Types of links that should be activated in Org-mode files.
  1180. This is a list of symbols, each leading to the activation of a certain link
  1181. type. In principle, it does not hurt to turn on most link types - there may
  1182. be a small gain when turning off unused link types. The types are:
  1183. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1184. angle Links in angular brackets that may contain whitespace like
  1185. <bbdb:Carsten Dominik>.
  1186. plain Plain links in normal text, no whitespace, like http://google.com.
  1187. radio Text that is matched by a radio target, see manual for details.
  1188. tag Tag settings in a headline (link to tag search).
  1189. date Time stamps (link to calendar).
  1190. footnote Footnote labels.
  1191. Changing this variable requires a restart of Emacs to become effective."
  1192. :group 'org-link
  1193. :type '(set :greedy t
  1194. (const :tag "Double bracket links" bracket)
  1195. (const :tag "Angular bracket links" angle)
  1196. (const :tag "Plain text links" plain)
  1197. (const :tag "Radio target matches" radio)
  1198. (const :tag "Tags" tag)
  1199. (const :tag "Timestamps" date)
  1200. (const :tag "Footnotes" footnote)))
  1201. (defcustom org-make-link-description-function nil
  1202. "Function to use to generate link descriptions from links.
  1203. If nil the link location will be used. This function must take
  1204. two parameters; the first is the link and the second the
  1205. description `org-insert-link' has generated, and should return the
  1206. description to use."
  1207. :group 'org-link
  1208. :type 'function)
  1209. (defgroup org-link-store nil
  1210. "Options concerning storing links in Org-mode."
  1211. :tag "Org Store Link"
  1212. :group 'org-link)
  1213. (defcustom org-email-link-description-format "Email %c: %.30s"
  1214. "Format of the description part of a link to an email or usenet message.
  1215. The following %-escapes will be replaced by corresponding information:
  1216. %F full \"From\" field
  1217. %f name, taken from \"From\" field, address if no name
  1218. %T full \"To\" field
  1219. %t first name in \"To\" field, address if no name
  1220. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1221. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1222. %s subject
  1223. %d date
  1224. %m message-id.
  1225. You may use normal field width specification between the % and the letter.
  1226. This is for example useful to limit the length of the subject.
  1227. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1228. :group 'org-link-store
  1229. :type 'string)
  1230. (defcustom org-from-is-user-regexp
  1231. (let (r1 r2)
  1232. (when (and user-mail-address (not (string= user-mail-address "")))
  1233. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1234. (when (and user-full-name (not (string= user-full-name "")))
  1235. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1236. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1237. "Regexp matched against the \"From:\" header of an email or usenet message.
  1238. It should match if the message is from the user him/herself."
  1239. :group 'org-link-store
  1240. :type 'regexp)
  1241. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1242. "Non-nil means storing a link to an Org file will use entry IDs.
  1243. Note that before this variable is even considered, org-id must be loaded,
  1244. so please customize `org-modules' and turn it on.
  1245. The variable can have the following values:
  1246. t Create an ID if needed to make a link to the current entry.
  1247. create-if-interactive
  1248. If `org-store-link' is called directly (interactively, as a user
  1249. command), do create an ID to support the link. But when doing the
  1250. job for remember, only use the ID if it already exists. The
  1251. purpose of this setting is to avoid proliferation of unwanted
  1252. IDs, just because you happen to be in an Org file when you
  1253. call `org-remember' that automatically and preemptively
  1254. creates a link. If you do want to get an ID link in a remember
  1255. template to an entry not having an ID, create it first by
  1256. explicitly creating a link to it, using `C-c C-l' first.
  1257. create-if-interactive-and-no-custom-id
  1258. Like create-if-interactive, but do not create an ID if there is
  1259. a CUSTOM_ID property defined in the entry. This is the default.
  1260. use-existing
  1261. Use existing ID, do not create one.
  1262. nil Never use an ID to make a link, instead link using a text search for
  1263. the headline text."
  1264. :group 'org-link-store
  1265. :type '(choice
  1266. (const :tag "Create ID to make link" t)
  1267. (const :tag "Create if storing link interactively"
  1268. create-if-interactive)
  1269. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1270. create-if-interactive-and-no-custom-id)
  1271. (const :tag "Only use existing" use-existing)
  1272. (const :tag "Do not use ID to create link" nil)))
  1273. (defcustom org-context-in-file-links t
  1274. "Non-nil means file links from `org-store-link' contain context.
  1275. A search string will be added to the file name with :: as separator and
  1276. used to find the context when the link is activated by the command
  1277. `org-open-at-point'. When this option is t, the entire active region
  1278. will be placed in the search string of the file link. If set to a
  1279. positive integer, only the first n lines of context will be stored.
  1280. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1281. negates this setting for the duration of the command."
  1282. :group 'org-link-store
  1283. :type '(choice boolean integer))
  1284. (defcustom org-keep-stored-link-after-insertion nil
  1285. "Non-nil means keep link in list for entire session.
  1286. The command `org-store-link' adds a link pointing to the current
  1287. location to an internal list. These links accumulate during a session.
  1288. The command `org-insert-link' can be used to insert links into any
  1289. Org-mode file (offering completion for all stored links). When this
  1290. option is nil, every link which has been inserted once using \\[org-insert-link]
  1291. will be removed from the list, to make completing the unused links
  1292. more efficient."
  1293. :group 'org-link-store
  1294. :type 'boolean)
  1295. (defgroup org-link-follow nil
  1296. "Options concerning following links in Org-mode."
  1297. :tag "Org Follow Link"
  1298. :group 'org-link)
  1299. (defcustom org-link-translation-function nil
  1300. "Function to translate links with different syntax to Org syntax.
  1301. This can be used to translate links created for example by the Planner
  1302. or emacs-wiki packages to Org syntax.
  1303. The function must accept two parameters, a TYPE containing the link
  1304. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1305. which is everything after the link protocol. It should return a cons
  1306. with possibly modified values of type and path.
  1307. Org contains a function for this, so if you set this variable to
  1308. `org-translate-link-from-planner', you should be able follow many
  1309. links created by planner."
  1310. :group 'org-link-follow
  1311. :type 'function)
  1312. (defcustom org-follow-link-hook nil
  1313. "Hook that is run after a link has been followed."
  1314. :group 'org-link-follow
  1315. :type 'hook)
  1316. (defcustom org-tab-follows-link nil
  1317. "Non-nil means on links TAB will follow the link.
  1318. Needs to be set before org.el is loaded.
  1319. This really should not be used, it does not make sense, and the
  1320. implementation is bad."
  1321. :group 'org-link-follow
  1322. :type 'boolean)
  1323. (defcustom org-return-follows-link nil
  1324. "Non-nil means on links RET will follow the link."
  1325. :group 'org-link-follow
  1326. :type 'boolean)
  1327. (defcustom org-mouse-1-follows-link
  1328. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1329. "Non-nil means mouse-1 on a link will follow the link.
  1330. A longer mouse click will still set point. Does not work on XEmacs.
  1331. Needs to be set before org.el is loaded."
  1332. :group 'org-link-follow
  1333. :type 'boolean)
  1334. (defcustom org-mark-ring-length 4
  1335. "Number of different positions to be recorded in the ring.
  1336. Changing this requires a restart of Emacs to work correctly."
  1337. :group 'org-link-follow
  1338. :type 'integer)
  1339. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1340. "Non-nil means internal links in Org files must exactly match a headline.
  1341. When nil, the link search tries to match a phrase with all words
  1342. in the search text."
  1343. :group 'org-link-follow
  1344. :version "24.1"
  1345. :type '(choice
  1346. (const :tag "Use fuzzy text search" nil)
  1347. (const :tag "Match only exact headline" t)
  1348. (const :tag "Match exact headline or query to create it"
  1349. query-to-create)))
  1350. (defcustom org-link-frame-setup
  1351. '((vm . vm-visit-folder-other-frame)
  1352. (vm-imap . vm-visit-imap-folder-other-frame)
  1353. (gnus . org-gnus-no-new-news)
  1354. (file . find-file-other-window)
  1355. (wl . wl-other-frame))
  1356. "Setup the frame configuration for following links.
  1357. When following a link with Emacs, it may often be useful to display
  1358. this link in another window or frame. This variable can be used to
  1359. set this up for the different types of links.
  1360. For VM, use any of
  1361. `vm-visit-folder'
  1362. `vm-visit-folder-other-window'
  1363. `vm-visit-folder-other-frame'
  1364. For Gnus, use any of
  1365. `gnus'
  1366. `gnus-other-frame'
  1367. `org-gnus-no-new-news'
  1368. For FILE, use any of
  1369. `find-file'
  1370. `find-file-other-window'
  1371. `find-file-other-frame'
  1372. For Wanderlust use any of
  1373. `wl'
  1374. `wl-other-frame'
  1375. For the calendar, use the variable `calendar-setup'.
  1376. For BBDB, it is currently only possible to display the matches in
  1377. another window."
  1378. :group 'org-link-follow
  1379. :type '(list
  1380. (cons (const vm)
  1381. (choice
  1382. (const vm-visit-folder)
  1383. (const vm-visit-folder-other-window)
  1384. (const vm-visit-folder-other-frame)))
  1385. (cons (const gnus)
  1386. (choice
  1387. (const gnus)
  1388. (const gnus-other-frame)
  1389. (const org-gnus-no-new-news)))
  1390. (cons (const file)
  1391. (choice
  1392. (const find-file)
  1393. (const find-file-other-window)
  1394. (const find-file-other-frame)))
  1395. (cons (const wl)
  1396. (choice
  1397. (const wl)
  1398. (const wl-other-frame)))))
  1399. (defcustom org-display-internal-link-with-indirect-buffer nil
  1400. "Non-nil means use indirect buffer to display infile links.
  1401. Activating internal links (from one location in a file to another location
  1402. in the same file) normally just jumps to the location. When the link is
  1403. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1404. is displayed in
  1405. another window. When this option is set, the other window actually displays
  1406. an indirect buffer clone of the current buffer, to avoid any visibility
  1407. changes to the current buffer."
  1408. :group 'org-link-follow
  1409. :type 'boolean)
  1410. (defcustom org-open-non-existing-files nil
  1411. "Non-nil means `org-open-file' will open non-existing files.
  1412. When nil, an error will be generated.
  1413. This variable applies only to external applications because they
  1414. might choke on non-existing files. If the link is to a file that
  1415. will be opened in Emacs, the variable is ignored."
  1416. :group 'org-link-follow
  1417. :type 'boolean)
  1418. (defcustom org-open-directory-means-index-dot-org nil
  1419. "Non-nil means a link to a directory really means to index.org.
  1420. When nil, following a directory link will run dired or open a finder/explorer
  1421. window on that directory."
  1422. :group 'org-link-follow
  1423. :type 'boolean)
  1424. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1425. "Function and arguments to call for following mailto links.
  1426. This is a list with the first element being a Lisp function, and the
  1427. remaining elements being arguments to the function. In string arguments,
  1428. %a will be replaced by the address, and %s will be replaced by the subject
  1429. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1430. :group 'org-link-follow
  1431. :type '(choice
  1432. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1433. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1434. (const :tag "message-mail" (message-mail "%a" "%s"))
  1435. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1436. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1437. "Non-nil means ask for confirmation before executing shell links.
  1438. Shell links can be dangerous: just think about a link
  1439. [[shell:rm -rf ~/*][Google Search]]
  1440. This link would show up in your Org-mode document as \"Google Search\",
  1441. but really it would remove your entire home directory.
  1442. Therefore we advise against setting this variable to nil.
  1443. Just change it to `y-or-n-p' if you want to confirm with a
  1444. single keystroke rather than having to type \"yes\"."
  1445. :group 'org-link-follow
  1446. :type '(choice
  1447. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1448. (const :tag "with y-or-n (faster)" y-or-n-p)
  1449. (const :tag "no confirmation (dangerous)" nil)))
  1450. (put 'org-confirm-shell-link-function
  1451. 'safe-local-variable
  1452. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1453. (defcustom org-confirm-shell-link-not-regexp ""
  1454. "A regexp to skip confirmation for shell links."
  1455. :group 'org-link-follow
  1456. :version "24.1"
  1457. :type 'regexp)
  1458. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1459. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1460. Elisp links can be dangerous: just think about a link
  1461. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1462. This link would show up in your Org-mode document as \"Google Search\",
  1463. but really it would remove your entire home directory.
  1464. Therefore we advise against setting this variable to nil.
  1465. Just change it to `y-or-n-p' if you want to confirm with a
  1466. single keystroke rather than having to type \"yes\"."
  1467. :group 'org-link-follow
  1468. :type '(choice
  1469. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1470. (const :tag "with y-or-n (faster)" y-or-n-p)
  1471. (const :tag "no confirmation (dangerous)" nil)))
  1472. (put 'org-confirm-shell-link-function
  1473. 'safe-local-variable
  1474. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1475. (defcustom org-confirm-elisp-link-not-regexp ""
  1476. "A regexp to skip confirmation for Elisp links."
  1477. :group 'org-link-follow
  1478. :version "24.1"
  1479. :type 'regexp)
  1480. (defconst org-file-apps-defaults-gnu
  1481. '((remote . emacs)
  1482. (system . mailcap)
  1483. (t . mailcap))
  1484. "Default file applications on a UNIX or GNU/Linux system.
  1485. See `org-file-apps'.")
  1486. (defconst org-file-apps-defaults-macosx
  1487. '((remote . emacs)
  1488. (t . "open %s")
  1489. (system . "open %s")
  1490. ("ps.gz" . "gv %s")
  1491. ("eps.gz" . "gv %s")
  1492. ("dvi" . "xdvi %s")
  1493. ("fig" . "xfig %s"))
  1494. "Default file applications on a MacOS X system.
  1495. The system \"open\" is known as a default, but we use X11 applications
  1496. for some files for which the OS does not have a good default.
  1497. See `org-file-apps'.")
  1498. (defconst org-file-apps-defaults-windowsnt
  1499. (list
  1500. '(remote . emacs)
  1501. (cons t
  1502. (list (if (featurep 'xemacs)
  1503. 'mswindows-shell-execute
  1504. 'w32-shell-execute)
  1505. "open" 'file))
  1506. (cons 'system
  1507. (list (if (featurep 'xemacs)
  1508. 'mswindows-shell-execute
  1509. 'w32-shell-execute)
  1510. "open" 'file)))
  1511. "Default file applications on a Windows NT system.
  1512. The system \"open\" is used for most files.
  1513. See `org-file-apps'.")
  1514. (defcustom org-file-apps
  1515. '(
  1516. (auto-mode . emacs)
  1517. ("\\.mm\\'" . default)
  1518. ("\\.x?html?\\'" . default)
  1519. ("\\.pdf\\'" . default)
  1520. )
  1521. "External applications for opening `file:path' items in a document.
  1522. Org-mode uses system defaults for different file types, but
  1523. you can use this variable to set the application for a given file
  1524. extension. The entries in this list are cons cells where the car identifies
  1525. files and the cdr the corresponding command. Possible values for the
  1526. file identifier are
  1527. \"string\" A string as a file identifier can be interpreted in different
  1528. ways, depending on its contents:
  1529. - Alphanumeric characters only:
  1530. Match links with this file extension.
  1531. Example: (\"pdf\" . \"evince %s\")
  1532. to open PDFs with evince.
  1533. - Regular expression: Match links where the
  1534. filename matches the regexp. If you want to
  1535. use groups here, use shy groups.
  1536. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1537. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1538. to open *.html and *.xhtml with firefox.
  1539. - Regular expression which contains (non-shy) groups:
  1540. Match links where the whole link, including \"::\", and
  1541. anything after that, matches the regexp.
  1542. In a custom command string, %1, %2, etc. are replaced with
  1543. the parts of the link that were matched by the groups.
  1544. For backwards compatibility, if a command string is given
  1545. that does not use any of the group matches, this case is
  1546. handled identically to the second one (i.e. match against
  1547. file name only).
  1548. In a custom lisp form, you can access the group matches with
  1549. (match-string n link).
  1550. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1551. to open [[file:document.pdf::5]] with evince at page 5.
  1552. `directory' Matches a directory
  1553. `remote' Matches a remote file, accessible through tramp or efs.
  1554. Remote files most likely should be visited through Emacs
  1555. because external applications cannot handle such paths.
  1556. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1557. so all files Emacs knows how to handle. Using this with
  1558. command `emacs' will open most files in Emacs. Beware that this
  1559. will also open html files inside Emacs, unless you add
  1560. (\"html\" . default) to the list as well.
  1561. t Default for files not matched by any of the other options.
  1562. `system' The system command to open files, like `open' on Windows
  1563. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1564. that will be selected if you call `C-c C-o' with a double
  1565. \\[universal-argument] \\[universal-argument] prefix.
  1566. Possible values for the command are:
  1567. `emacs' The file will be visited by the current Emacs process.
  1568. `default' Use the default application for this file type, which is the
  1569. association for t in the list, most likely in the system-specific
  1570. part.
  1571. This can be used to overrule an unwanted setting in the
  1572. system-specific variable.
  1573. `system' Use the system command for opening files, like \"open\".
  1574. This command is specified by the entry whose car is `system'.
  1575. Most likely, the system-specific version of this variable
  1576. does define this command, but you can overrule/replace it
  1577. here.
  1578. string A command to be executed by a shell; %s will be replaced
  1579. by the path to the file.
  1580. sexp A Lisp form which will be evaluated. The file path will
  1581. be available in the Lisp variable `file'.
  1582. For more examples, see the system specific constants
  1583. `org-file-apps-defaults-macosx'
  1584. `org-file-apps-defaults-windowsnt'
  1585. `org-file-apps-defaults-gnu'."
  1586. :group 'org-link-follow
  1587. :type '(repeat
  1588. (cons (choice :value ""
  1589. (string :tag "Extension")
  1590. (const :tag "System command to open files" system)
  1591. (const :tag "Default for unrecognized files" t)
  1592. (const :tag "Remote file" remote)
  1593. (const :tag "Links to a directory" directory)
  1594. (const :tag "Any files that have Emacs modes"
  1595. auto-mode))
  1596. (choice :value ""
  1597. (const :tag "Visit with Emacs" emacs)
  1598. (const :tag "Use default" default)
  1599. (const :tag "Use the system command" system)
  1600. (string :tag "Command")
  1601. (sexp :tag "Lisp form")))))
  1602. (defgroup org-refile nil
  1603. "Options concerning refiling entries in Org-mode."
  1604. :tag "Org Refile"
  1605. :group 'org)
  1606. (defcustom org-directory "~/org"
  1607. "Directory with org files.
  1608. This is just a default location to look for Org files. There is no need
  1609. at all to put your files into this directory. It is only used in the
  1610. following situations:
  1611. 1. When a remember template specifies a target file that is not an
  1612. absolute path. The path will then be interpreted relative to
  1613. `org-directory'
  1614. 2. When a remember note is filed away in an interactive way (when exiting the
  1615. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1616. with `org-directory' as the default path."
  1617. :group 'org-refile
  1618. :group 'org-remember
  1619. :type 'directory)
  1620. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1621. "Default target for storing notes.
  1622. Used as a fall back file for org-remember.el and org-capture.el, for
  1623. templates that do not specify a target file."
  1624. :group 'org-refile
  1625. :group 'org-remember
  1626. :type '(choice
  1627. (const :tag "Default from remember-data-file" nil)
  1628. file))
  1629. (defcustom org-goto-interface 'outline
  1630. "The default interface to be used for `org-goto'.
  1631. Allowed values are:
  1632. outline The interface shows an outline of the relevant file
  1633. and the correct heading is found by moving through
  1634. the outline or by searching with incremental search.
  1635. outline-path-completion Headlines in the current buffer are offered via
  1636. completion. This is the interface also used by
  1637. the refile command."
  1638. :group 'org-refile
  1639. :type '(choice
  1640. (const :tag "Outline" outline)
  1641. (const :tag "Outline-path-completion" outline-path-completion)))
  1642. (defcustom org-goto-max-level 5
  1643. "Maximum target level when running `org-goto' with refile interface."
  1644. :group 'org-refile
  1645. :type 'integer)
  1646. (defcustom org-reverse-note-order nil
  1647. "Non-nil means store new notes at the beginning of a file or entry.
  1648. When nil, new notes will be filed to the end of a file or entry.
  1649. This can also be a list with cons cells of regular expressions that
  1650. are matched against file names, and values."
  1651. :group 'org-remember
  1652. :group 'org-refile
  1653. :type '(choice
  1654. (const :tag "Reverse always" t)
  1655. (const :tag "Reverse never" nil)
  1656. (repeat :tag "By file name regexp"
  1657. (cons regexp boolean))))
  1658. (defcustom org-log-refile nil
  1659. "Information to record when a task is refiled.
  1660. Possible values are:
  1661. nil Don't add anything
  1662. time Add a time stamp to the task
  1663. note Prompt for a note and add it with template `org-log-note-headings'
  1664. This option can also be set with on a per-file-basis with
  1665. #+STARTUP: nologrefile
  1666. #+STARTUP: logrefile
  1667. #+STARTUP: lognoterefile
  1668. You can have local logging settings for a subtree by setting the LOGGING
  1669. property to one or more of these keywords.
  1670. When bulk-refiling from the agenda, the value `note' is forbidden and
  1671. will temporarily be changed to `time'."
  1672. :group 'org-refile
  1673. :group 'org-progress
  1674. :version "24.1"
  1675. :type '(choice
  1676. (const :tag "No logging" nil)
  1677. (const :tag "Record timestamp" time)
  1678. (const :tag "Record timestamp with note." note)))
  1679. (defcustom org-refile-targets nil
  1680. "Targets for refiling entries with \\[org-refile].
  1681. This is a list of cons cells. Each cell contains:
  1682. - a specification of the files to be considered, either a list of files,
  1683. or a symbol whose function or variable value will be used to retrieve
  1684. a file name or a list of file names. If you use `org-agenda-files' for
  1685. that, all agenda files will be scanned for targets. Nil means consider
  1686. headings in the current buffer.
  1687. - A specification of how to find candidate refile targets. This may be
  1688. any of:
  1689. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1690. This tag has to be present in all target headlines, inheritance will
  1691. not be considered.
  1692. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1693. todo keyword.
  1694. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1695. headlines that are refiling targets.
  1696. - a cons cell (:level . N). Any headline of level N is considered a target.
  1697. Note that, when `org-odd-levels-only' is set, level corresponds to
  1698. order in hierarchy, not to the number of stars.
  1699. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1700. Note that, when `org-odd-levels-only' is set, level corresponds to
  1701. order in hierarchy, not to the number of stars.
  1702. Each element of this list generates a set of possible targets.
  1703. The union of these sets is presented (with completion) to
  1704. the user by `org-refile'.
  1705. You can set the variable `org-refile-target-verify-function' to a function
  1706. to verify each headline found by the simple criteria above.
  1707. When this variable is nil, all top-level headlines in the current buffer
  1708. are used, equivalent to the value `((nil . (:level . 1))'."
  1709. :group 'org-refile
  1710. :type '(repeat
  1711. (cons
  1712. (choice :value org-agenda-files
  1713. (const :tag "All agenda files" org-agenda-files)
  1714. (const :tag "Current buffer" nil)
  1715. (function) (variable) (file))
  1716. (choice :tag "Identify target headline by"
  1717. (cons :tag "Specific tag" (const :value :tag) (string))
  1718. (cons :tag "TODO keyword" (const :value :todo) (string))
  1719. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1720. (cons :tag "Level number" (const :value :level) (integer))
  1721. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1722. (defcustom org-refile-target-verify-function nil
  1723. "Function to verify if the headline at point should be a refile target.
  1724. The function will be called without arguments, with point at the
  1725. beginning of the headline. It should return t and leave point
  1726. where it is if the headline is a valid target for refiling.
  1727. If the target should not be selected, the function must return nil.
  1728. In addition to this, it may move point to a place from where the search
  1729. should be continued. For example, the function may decide that the entire
  1730. subtree of the current entry should be excluded and move point to the end
  1731. of the subtree."
  1732. :group 'org-refile
  1733. :type 'function)
  1734. (defcustom org-refile-use-cache nil
  1735. "Non-nil means cache refile targets to speed up the process.
  1736. The cache for a particular file will be updated automatically when
  1737. the buffer has been killed, or when any of the marker used for flagging
  1738. refile targets no longer points at a live buffer.
  1739. If you have added new entries to a buffer that might themselves be targets,
  1740. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1741. find that easier, `C-u C-u C-u C-c C-w'."
  1742. :group 'org-refile
  1743. :version "24.1"
  1744. :type 'boolean)
  1745. (defcustom org-refile-use-outline-path nil
  1746. "Non-nil means provide refile targets as paths.
  1747. So a level 3 headline will be available as level1/level2/level3.
  1748. When the value is `file', also include the file name (without directory)
  1749. into the path. In this case, you can also stop the completion after
  1750. the file name, to get entries inserted as top level in the file.
  1751. When `full-file-path', include the full file path."
  1752. :group 'org-refile
  1753. :type '(choice
  1754. (const :tag "Not" nil)
  1755. (const :tag "Yes" t)
  1756. (const :tag "Start with file name" file)
  1757. (const :tag "Start with full file path" full-file-path)))
  1758. (defcustom org-outline-path-complete-in-steps t
  1759. "Non-nil means complete the outline path in hierarchical steps.
  1760. When Org-mode uses the refile interface to select an outline path
  1761. \(see variable `org-refile-use-outline-path'), the completion of
  1762. the path can be done is a single go, or if can be done in steps down
  1763. the headline hierarchy. Going in steps is probably the best if you
  1764. do not use a special completion package like `ido' or `icicles'.
  1765. However, when using these packages, going in one step can be very
  1766. fast, while still showing the whole path to the entry."
  1767. :group 'org-refile
  1768. :type 'boolean)
  1769. (defcustom org-refile-allow-creating-parent-nodes nil
  1770. "Non-nil means allow to create new nodes as refile targets.
  1771. New nodes are then created by adding \"/new node name\" to the completion
  1772. of an existing node. When the value of this variable is `confirm',
  1773. new node creation must be confirmed by the user (recommended)
  1774. When nil, the completion must match an existing entry.
  1775. Note that, if the new heading is not seen by the criteria
  1776. listed in `org-refile-targets', multiple instances of the same
  1777. heading would be created by trying again to file under the new
  1778. heading."
  1779. :group 'org-refile
  1780. :type '(choice
  1781. (const :tag "Never" nil)
  1782. (const :tag "Always" t)
  1783. (const :tag "Prompt for confirmation" confirm)))
  1784. (defcustom org-refile-active-region-within-subtree nil
  1785. "Non-nil means also refile active region within a subtree.
  1786. By default `org-refile' doesn't allow refiling regions if they
  1787. don't contain a set of subtrees, but it might be convenient to
  1788. do so sometimes: in that case, the first line of the region is
  1789. converted to a headline before refiling."
  1790. :group 'org-refile
  1791. :version "24.1"
  1792. :type 'boolean)
  1793. (defgroup org-todo nil
  1794. "Options concerning TODO items in Org-mode."
  1795. :tag "Org TODO"
  1796. :group 'org)
  1797. (defgroup org-progress nil
  1798. "Options concerning Progress logging in Org-mode."
  1799. :tag "Org Progress"
  1800. :group 'org-time)
  1801. (defvar org-todo-interpretation-widgets
  1802. '((:tag "Sequence (cycling hits every state)" sequence)
  1803. (:tag "Type (cycling directly to DONE)" type))
  1804. "The available interpretation symbols for customizing `org-todo-keywords'.
  1805. Interested libraries should add to this list.")
  1806. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1807. "List of TODO entry keyword sequences and their interpretation.
  1808. \\<org-mode-map>This is a list of sequences.
  1809. Each sequence starts with a symbol, either `sequence' or `type',
  1810. indicating if the keywords should be interpreted as a sequence of
  1811. action steps, or as different types of TODO items. The first
  1812. keywords are states requiring action - these states will select a headline
  1813. for inclusion into the global TODO list Org-mode produces. If one of
  1814. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1815. signify that no further action is necessary. If \"|\" is not found,
  1816. the last keyword is treated as the only DONE state of the sequence.
  1817. The command \\[org-todo] cycles an entry through these states, and one
  1818. additional state where no keyword is present. For details about this
  1819. cycling, see the manual.
  1820. TODO keywords and interpretation can also be set on a per-file basis with
  1821. the special #+SEQ_TODO and #+TYP_TODO lines.
  1822. Each keyword can optionally specify a character for fast state selection
  1823. \(in combination with the variable `org-use-fast-todo-selection')
  1824. and specifiers for state change logging, using the same syntax
  1825. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1826. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1827. indicates to record a time stamp each time this state is selected.
  1828. Each keyword may also specify if a timestamp or a note should be
  1829. recorded when entering or leaving the state, by adding additional
  1830. characters in the parenthesis after the keyword. This looks like this:
  1831. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1832. record only the time of the state change. With X and Y being either
  1833. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1834. Y when leaving the state if and only if the *target* state does not
  1835. define X. You may omit any of the fast-selection key or X or /Y,
  1836. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1837. For backward compatibility, this variable may also be just a list
  1838. of keywords - in this case the interpretation (sequence or type) will be
  1839. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1840. :group 'org-todo
  1841. :group 'org-keywords
  1842. :type '(choice
  1843. (repeat :tag "Old syntax, just keywords"
  1844. (string :tag "Keyword"))
  1845. (repeat :tag "New syntax"
  1846. (cons
  1847. (choice
  1848. :tag "Interpretation"
  1849. ;;Quick and dirty way to see
  1850. ;;`org-todo-interpretations'. This takes the
  1851. ;;place of item arguments
  1852. :convert-widget
  1853. (lambda (widget)
  1854. (widget-put widget
  1855. :args (mapcar
  1856. #'(lambda (x)
  1857. (widget-convert
  1858. (cons 'const x)))
  1859. org-todo-interpretation-widgets))
  1860. widget))
  1861. (repeat
  1862. (string :tag "Keyword"))))))
  1863. (defvar org-todo-keywords-1 nil
  1864. "All TODO and DONE keywords active in a buffer.")
  1865. (make-variable-buffer-local 'org-todo-keywords-1)
  1866. (defvar org-todo-keywords-for-agenda nil)
  1867. (defvar org-done-keywords-for-agenda nil)
  1868. (defvar org-drawers-for-agenda nil)
  1869. (defvar org-todo-keyword-alist-for-agenda nil)
  1870. (defvar org-tag-alist-for-agenda nil)
  1871. (defvar org-agenda-contributing-files nil)
  1872. (defvar org-not-done-keywords nil)
  1873. (make-variable-buffer-local 'org-not-done-keywords)
  1874. (defvar org-done-keywords nil)
  1875. (make-variable-buffer-local 'org-done-keywords)
  1876. (defvar org-todo-heads nil)
  1877. (make-variable-buffer-local 'org-todo-heads)
  1878. (defvar org-todo-sets nil)
  1879. (make-variable-buffer-local 'org-todo-sets)
  1880. (defvar org-todo-log-states nil)
  1881. (make-variable-buffer-local 'org-todo-log-states)
  1882. (defvar org-todo-kwd-alist nil)
  1883. (make-variable-buffer-local 'org-todo-kwd-alist)
  1884. (defvar org-todo-key-alist nil)
  1885. (make-variable-buffer-local 'org-todo-key-alist)
  1886. (defvar org-todo-key-trigger nil)
  1887. (make-variable-buffer-local 'org-todo-key-trigger)
  1888. (defcustom org-todo-interpretation 'sequence
  1889. "Controls how TODO keywords are interpreted.
  1890. This variable is in principle obsolete and is only used for
  1891. backward compatibility, if the interpretation of todo keywords is
  1892. not given already in `org-todo-keywords'. See that variable for
  1893. more information."
  1894. :group 'org-todo
  1895. :group 'org-keywords
  1896. :type '(choice (const sequence)
  1897. (const type)))
  1898. (defcustom org-use-fast-todo-selection t
  1899. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1900. This variable describes if and under what circumstances the cycling
  1901. mechanism for TODO keywords will be replaced by a single-key, direct
  1902. selection scheme.
  1903. When nil, fast selection is never used.
  1904. When the symbol `prefix', it will be used when `org-todo' is called with
  1905. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1906. in an agenda buffer.
  1907. When t, fast selection is used by default. In this case, the prefix
  1908. argument forces cycling instead.
  1909. In all cases, the special interface is only used if access keys have actually
  1910. been assigned by the user, i.e. if keywords in the configuration are followed
  1911. by a letter in parenthesis, like TODO(t)."
  1912. :group 'org-todo
  1913. :type '(choice
  1914. (const :tag "Never" nil)
  1915. (const :tag "By default" t)
  1916. (const :tag "Only with C-u C-c C-t" prefix)))
  1917. (defcustom org-provide-todo-statistics t
  1918. "Non-nil means update todo statistics after insert and toggle.
  1919. ALL-HEADLINES means update todo statistics by including headlines
  1920. with no TODO keyword as well, counting them as not done.
  1921. A list of TODO keywords means the same, but skip keywords that are
  1922. not in this list.
  1923. When this is set, todo statistics is updated in the parent of the
  1924. current entry each time a todo state is changed."
  1925. :group 'org-todo
  1926. :type '(choice
  1927. (const :tag "Yes, only for TODO entries" t)
  1928. (const :tag "Yes, including all entries" 'all-headlines)
  1929. (repeat :tag "Yes, for TODOs in this list"
  1930. (string :tag "TODO keyword"))
  1931. (other :tag "No TODO statistics" nil)))
  1932. (defcustom org-hierarchical-todo-statistics t
  1933. "Non-nil means TODO statistics covers just direct children.
  1934. When nil, all entries in the subtree are considered.
  1935. This has only an effect if `org-provide-todo-statistics' is set.
  1936. To set this to nil for only a single subtree, use a COOKIE_DATA
  1937. property and include the word \"recursive\" into the value."
  1938. :group 'org-todo
  1939. :type 'boolean)
  1940. (defcustom org-after-todo-state-change-hook nil
  1941. "Hook which is run after the state of a TODO item was changed.
  1942. The new state (a string with a TODO keyword, or nil) is available in the
  1943. Lisp variable `org-state'."
  1944. :group 'org-todo
  1945. :type 'hook)
  1946. (defvar org-blocker-hook nil
  1947. "Hook for functions that are allowed to block a state change.
  1948. Each function gets as its single argument a property list, see
  1949. `org-trigger-hook' for more information about this list.
  1950. If any of the functions in this hook returns nil, the state change
  1951. is blocked.")
  1952. (defvar org-trigger-hook nil
  1953. "Hook for functions that are triggered by a state change.
  1954. Each function gets as its single argument a property list with at least
  1955. the following elements:
  1956. (:type type-of-change :position pos-at-entry-start
  1957. :from old-state :to new-state)
  1958. Depending on the type, more properties may be present.
  1959. This mechanism is currently implemented for:
  1960. TODO state changes
  1961. ------------------
  1962. :type todo-state-change
  1963. :from previous state (keyword as a string), or nil, or a symbol
  1964. 'todo' or 'done', to indicate the general type of state.
  1965. :to new state, like in :from")
  1966. (defcustom org-enforce-todo-dependencies nil
  1967. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1968. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1969. be blocked if any prior sibling is not yet done.
  1970. Finally, if the parent is blocked because of ordered siblings of its own,
  1971. the child will also be blocked."
  1972. :set (lambda (var val)
  1973. (set var val)
  1974. (if val
  1975. (add-hook 'org-blocker-hook
  1976. 'org-block-todo-from-children-or-siblings-or-parent)
  1977. (remove-hook 'org-blocker-hook
  1978. 'org-block-todo-from-children-or-siblings-or-parent)))
  1979. :group 'org-todo
  1980. :type 'boolean)
  1981. (defcustom org-enforce-todo-checkbox-dependencies nil
  1982. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1983. When this is nil, checkboxes have no influence on switching TODO states.
  1984. When non-nil, you first need to check off all check boxes before the TODO
  1985. entry can be switched to DONE.
  1986. This variable needs to be set before org.el is loaded, and you need to
  1987. restart Emacs after a change to make the change effective. The only way
  1988. to change is while Emacs is running is through the customize interface."
  1989. :set (lambda (var val)
  1990. (set var val)
  1991. (if val
  1992. (add-hook 'org-blocker-hook
  1993. 'org-block-todo-from-checkboxes)
  1994. (remove-hook 'org-blocker-hook
  1995. 'org-block-todo-from-checkboxes)))
  1996. :group 'org-todo
  1997. :type 'boolean)
  1998. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1999. "Non-nil means inserting a TODO heading is treated as state change.
  2000. So when the command \\[org-insert-todo-heading] is used, state change
  2001. logging will apply if appropriate. When nil, the new TODO item will
  2002. be inserted directly, and no logging will take place."
  2003. :group 'org-todo
  2004. :type 'boolean)
  2005. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2006. "Non-nil means switching TODO states with S-cursor counts as state change.
  2007. This is the default behavior. However, setting this to nil allows a
  2008. convenient way to select a TODO state and bypass any logging associated
  2009. with that."
  2010. :group 'org-todo
  2011. :type 'boolean)
  2012. (defcustom org-todo-state-tags-triggers nil
  2013. "Tag changes that should be triggered by TODO state changes.
  2014. This is a list. Each entry is
  2015. (state-change (tag . flag) .......)
  2016. State-change can be a string with a state, and empty string to indicate the
  2017. state that has no TODO keyword, or it can be one of the symbols `todo'
  2018. or `done', meaning any not-done or done state, respectively."
  2019. :group 'org-todo
  2020. :group 'org-tags
  2021. :type '(repeat
  2022. (cons (choice :tag "When changing to"
  2023. (const :tag "Not-done state" todo)
  2024. (const :tag "Done state" done)
  2025. (string :tag "State"))
  2026. (repeat
  2027. (cons :tag "Tag action"
  2028. (string :tag "Tag")
  2029. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2030. (defcustom org-log-done nil
  2031. "Information to record when a task moves to the DONE state.
  2032. Possible values are:
  2033. nil Don't add anything, just change the keyword
  2034. time Add a time stamp to the task
  2035. note Prompt for a note and add it with template `org-log-note-headings'
  2036. This option can also be set with on a per-file-basis with
  2037. #+STARTUP: nologdone
  2038. #+STARTUP: logdone
  2039. #+STARTUP: lognotedone
  2040. You can have local logging settings for a subtree by setting the LOGGING
  2041. property to one or more of these keywords."
  2042. :group 'org-todo
  2043. :group 'org-progress
  2044. :type '(choice
  2045. (const :tag "No logging" nil)
  2046. (const :tag "Record CLOSED timestamp" time)
  2047. (const :tag "Record CLOSED timestamp with note." note)))
  2048. ;; Normalize old uses of org-log-done.
  2049. (cond
  2050. ((eq org-log-done t) (setq org-log-done 'time))
  2051. ((and (listp org-log-done) (memq 'done org-log-done))
  2052. (setq org-log-done 'note)))
  2053. (defcustom org-log-reschedule nil
  2054. "Information to record when the scheduling date of a tasks is modified.
  2055. Possible values are:
  2056. nil Don't add anything, just change the date
  2057. time Add a time stamp to the task
  2058. note Prompt for a note and add it with template `org-log-note-headings'
  2059. This option can also be set with on a per-file-basis with
  2060. #+STARTUP: nologreschedule
  2061. #+STARTUP: logreschedule
  2062. #+STARTUP: lognotereschedule"
  2063. :group 'org-todo
  2064. :group 'org-progress
  2065. :type '(choice
  2066. (const :tag "No logging" nil)
  2067. (const :tag "Record timestamp" time)
  2068. (const :tag "Record timestamp with note." note)))
  2069. (defcustom org-log-redeadline nil
  2070. "Information to record when the deadline date of a tasks is modified.
  2071. Possible values are:
  2072. nil Don't add anything, just change the date
  2073. time Add a time stamp to the task
  2074. note Prompt for a note and add it with template `org-log-note-headings'
  2075. This option can also be set with on a per-file-basis with
  2076. #+STARTUP: nologredeadline
  2077. #+STARTUP: logredeadline
  2078. #+STARTUP: lognoteredeadline
  2079. You can have local logging settings for a subtree by setting the LOGGING
  2080. property to one or more of these keywords."
  2081. :group 'org-todo
  2082. :group 'org-progress
  2083. :type '(choice
  2084. (const :tag "No logging" nil)
  2085. (const :tag "Record timestamp" time)
  2086. (const :tag "Record timestamp with note." note)))
  2087. (defcustom org-log-note-clock-out nil
  2088. "Non-nil means record a note when clocking out of an item.
  2089. This can also be configured on a per-file basis by adding one of
  2090. the following lines anywhere in the buffer:
  2091. #+STARTUP: lognoteclock-out
  2092. #+STARTUP: nolognoteclock-out"
  2093. :group 'org-todo
  2094. :group 'org-progress
  2095. :type 'boolean)
  2096. (defcustom org-log-done-with-time t
  2097. "Non-nil means the CLOSED time stamp will contain date and time.
  2098. When nil, only the date will be recorded."
  2099. :group 'org-progress
  2100. :type 'boolean)
  2101. (defcustom org-log-note-headings
  2102. '((done . "CLOSING NOTE %t")
  2103. (state . "State %-12s from %-12S %t")
  2104. (note . "Note taken on %t")
  2105. (reschedule . "Rescheduled from %S on %t")
  2106. (delschedule . "Not scheduled, was %S on %t")
  2107. (redeadline . "New deadline from %S on %t")
  2108. (deldeadline . "Removed deadline, was %S on %t")
  2109. (refile . "Refiled on %t")
  2110. (clock-out . ""))
  2111. "Headings for notes added to entries.
  2112. The value is an alist, with the car being a symbol indicating the note
  2113. context, and the cdr is the heading to be used. The heading may also be the
  2114. empty string.
  2115. %t in the heading will be replaced by a time stamp.
  2116. %T will be an active time stamp instead the default inactive one
  2117. %d will be replaced by a short-format time stamp.
  2118. %D will be replaced by an active short-format time stamp.
  2119. %s will be replaced by the new TODO state, in double quotes.
  2120. %S will be replaced by the old TODO state, in double quotes.
  2121. %u will be replaced by the user name.
  2122. %U will be replaced by the full user name.
  2123. In fact, it is not a good idea to change the `state' entry, because
  2124. agenda log mode depends on the format of these entries."
  2125. :group 'org-todo
  2126. :group 'org-progress
  2127. :type '(list :greedy t
  2128. (cons (const :tag "Heading when closing an item" done) string)
  2129. (cons (const :tag
  2130. "Heading when changing todo state (todo sequence only)"
  2131. state) string)
  2132. (cons (const :tag "Heading when just taking a note" note) string)
  2133. (cons (const :tag "Heading when clocking out" clock-out) string)
  2134. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2135. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2136. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2137. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2138. (cons (const :tag "Heading when refiling" refile) string)))
  2139. (unless (assq 'note org-log-note-headings)
  2140. (push '(note . "%t") org-log-note-headings))
  2141. (defcustom org-log-into-drawer nil
  2142. "Non-nil means insert state change notes and time stamps into a drawer.
  2143. When nil, state changes notes will be inserted after the headline and
  2144. any scheduling and clock lines, but not inside a drawer.
  2145. The value of this variable should be the name of the drawer to use.
  2146. LOGBOOK is proposed as the default drawer for this purpose, you can
  2147. also set this to a string to define the drawer of your choice.
  2148. A value of t is also allowed, representing \"LOGBOOK\".
  2149. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2150. will be ignored.
  2151. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2152. a subtree."
  2153. :group 'org-todo
  2154. :group 'org-progress
  2155. :type '(choice
  2156. (const :tag "Not into a drawer" nil)
  2157. (const :tag "LOGBOOK" t)
  2158. (string :tag "Other")))
  2159. (if (fboundp 'defvaralias)
  2160. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2161. (defun org-log-into-drawer ()
  2162. "Return the value of `org-log-into-drawer', but let properties overrule.
  2163. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2164. used instead of the default value."
  2165. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2166. (cond
  2167. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2168. ((equal p "t") "LOGBOOK")
  2169. (t p))))
  2170. (defcustom org-log-state-notes-insert-after-drawers nil
  2171. "Non-nil means insert state change notes after any drawers in entry.
  2172. Only the drawers that *immediately* follow the headline and the
  2173. deadline/scheduled line are skipped.
  2174. When nil, insert notes right after the heading and perhaps the line
  2175. with deadline/scheduling if present.
  2176. This variable will have no effect if `org-log-into-drawer' is
  2177. set."
  2178. :group 'org-todo
  2179. :group 'org-progress
  2180. :type 'boolean)
  2181. (defcustom org-log-states-order-reversed t
  2182. "Non-nil means the latest state note will be directly after heading.
  2183. When nil, the state change notes will be ordered according to time."
  2184. :group 'org-todo
  2185. :group 'org-progress
  2186. :type 'boolean)
  2187. (defcustom org-todo-repeat-to-state nil
  2188. "The TODO state to which a repeater should return the repeating task.
  2189. By default this is the first task in a TODO sequence, or the previous state
  2190. in a TODO_TYP set. But you can specify another task here.
  2191. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2192. :group 'org-todo
  2193. :version "24.1"
  2194. :type '(choice (const :tag "Head of sequence" nil)
  2195. (string :tag "Specific state")))
  2196. (defcustom org-log-repeat 'time
  2197. "Non-nil means record moving through the DONE state when triggering repeat.
  2198. An auto-repeating task is immediately switched back to TODO when
  2199. marked DONE. If you are not logging state changes (by adding \"@\"
  2200. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2201. record a closing note, there will be no record of the task moving
  2202. through DONE. This variable forces taking a note anyway.
  2203. nil Don't force a record
  2204. time Record a time stamp
  2205. note Record a note
  2206. This option can also be set with on a per-file-basis with
  2207. #+STARTUP: logrepeat
  2208. #+STARTUP: lognoterepeat
  2209. #+STARTUP: nologrepeat
  2210. You can have local logging settings for a subtree by setting the LOGGING
  2211. property to one or more of these keywords."
  2212. :group 'org-todo
  2213. :group 'org-progress
  2214. :type '(choice
  2215. (const :tag "Don't force a record" nil)
  2216. (const :tag "Force recording the DONE state" time)
  2217. (const :tag "Force recording a note with the DONE state" note)))
  2218. (defgroup org-priorities nil
  2219. "Priorities in Org-mode."
  2220. :tag "Org Priorities"
  2221. :group 'org-todo)
  2222. (defcustom org-enable-priority-commands t
  2223. "Non-nil means priority commands are active.
  2224. When nil, these commands will be disabled, so that you never accidentally
  2225. set a priority."
  2226. :group 'org-priorities
  2227. :type 'boolean)
  2228. (defcustom org-highest-priority ?A
  2229. "The highest priority of TODO items. A character like ?A, ?B etc.
  2230. Must have a smaller ASCII number than `org-lowest-priority'."
  2231. :group 'org-priorities
  2232. :type 'character)
  2233. (defcustom org-lowest-priority ?C
  2234. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2235. Must have a larger ASCII number than `org-highest-priority'."
  2236. :group 'org-priorities
  2237. :type 'character)
  2238. (defcustom org-default-priority ?B
  2239. "The default priority of TODO items.
  2240. This is the priority an item gets if no explicit priority is given.
  2241. When starting to cycle on an empty priority the first step in the cycle
  2242. depends on `org-priority-start-cycle-with-default'. The resulting first
  2243. step priority must not exceed the range from `org-highest-priority' to
  2244. `org-lowest-priority' which means that `org-default-priority' has to be
  2245. in this range exclusive or inclusive the range boundaries. Else the
  2246. first step refuses to set the default and the second will fall back
  2247. to (depending on the command used) the highest or lowest priority."
  2248. :group 'org-priorities
  2249. :type 'character)
  2250. (defcustom org-priority-start-cycle-with-default t
  2251. "Non-nil means start with default priority when starting to cycle.
  2252. When this is nil, the first step in the cycle will be (depending on the
  2253. command used) one higher or lower than the default priority.
  2254. See also `org-default-priority'."
  2255. :group 'org-priorities
  2256. :type 'boolean)
  2257. (defcustom org-get-priority-function nil
  2258. "Function to extract the priority from a string.
  2259. The string is normally the headline. If this is nil Org computes the
  2260. priority from the priority cookie like [#A] in the headline. It returns
  2261. an integer, increasing by 1000 for each priority level.
  2262. The user can set a different function here, which should take a string
  2263. as an argument and return the numeric priority."
  2264. :group 'org-priorities
  2265. :version "24.1"
  2266. :type 'function)
  2267. (defgroup org-time nil
  2268. "Options concerning time stamps and deadlines in Org-mode."
  2269. :tag "Org Time"
  2270. :group 'org)
  2271. (defcustom org-insert-labeled-timestamps-at-point nil
  2272. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2273. When nil, these labeled time stamps are forces into the second line of an
  2274. entry, just after the headline. When scheduling from the global TODO list,
  2275. the time stamp will always be forced into the second line."
  2276. :group 'org-time
  2277. :type 'boolean)
  2278. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2279. "Formats for `format-time-string' which are used for time stamps.
  2280. It is not recommended to change this constant.")
  2281. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2282. "Number of minutes to round time stamps to.
  2283. These are two values, the first applies when first creating a time stamp.
  2284. The second applies when changing it with the commands `S-up' and `S-down'.
  2285. When changing the time stamp, this means that it will change in steps
  2286. of N minutes, as given by the second value.
  2287. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2288. numbers should be factors of 60, so for example 5, 10, 15.
  2289. When this is larger than 1, you can still force an exact time stamp by using
  2290. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2291. and by using a prefix arg to `S-up/down' to specify the exact number
  2292. of minutes to shift."
  2293. :group 'org-time
  2294. :get #'(lambda (var) ; Make sure both elements are there
  2295. (if (integerp (default-value var))
  2296. (list (default-value var) 5)
  2297. (default-value var)))
  2298. :type '(list
  2299. (integer :tag "when inserting times")
  2300. (integer :tag "when modifying times")))
  2301. ;; Normalize old customizations of this variable.
  2302. (when (integerp org-time-stamp-rounding-minutes)
  2303. (setq org-time-stamp-rounding-minutes
  2304. (list org-time-stamp-rounding-minutes
  2305. org-time-stamp-rounding-minutes)))
  2306. (defcustom org-display-custom-times nil
  2307. "Non-nil means overlay custom formats over all time stamps.
  2308. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2309. To turn this on on a per-file basis, insert anywhere in the file:
  2310. #+STARTUP: customtime"
  2311. :group 'org-time
  2312. :set 'set-default
  2313. :type 'sexp)
  2314. (make-variable-buffer-local 'org-display-custom-times)
  2315. (defcustom org-time-stamp-custom-formats
  2316. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2317. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2318. These are overlaid over the default ISO format if the variable
  2319. `org-display-custom-times' is set. Time like %H:%M should be at the
  2320. end of the second format. The custom formats are also honored by export
  2321. commands, if custom time display is turned on at the time of export."
  2322. :group 'org-time
  2323. :type 'sexp)
  2324. (defun org-time-stamp-format (&optional long inactive)
  2325. "Get the right format for a time string."
  2326. (let ((f (if long (cdr org-time-stamp-formats)
  2327. (car org-time-stamp-formats))))
  2328. (if inactive
  2329. (concat "[" (substring f 1 -1) "]")
  2330. f)))
  2331. (defcustom org-time-clocksum-format "%d:%02d"
  2332. "The format string used when creating CLOCKSUM lines.
  2333. This is also used when org-mode generates a time duration."
  2334. :group 'org-time
  2335. :type 'string)
  2336. (defcustom org-time-clocksum-use-fractional nil
  2337. "If non-nil, \\[org-clock-display] uses fractional times.
  2338. org-mode generates a time duration."
  2339. :group 'org-time
  2340. :type 'boolean)
  2341. (defcustom org-time-clocksum-fractional-format "%.2f"
  2342. "The format string used when creating CLOCKSUM lines, or when
  2343. org-mode generates a time duration."
  2344. :group 'org-time
  2345. :type 'string)
  2346. (defcustom org-deadline-warning-days 14
  2347. "No. of days before expiration during which a deadline becomes active.
  2348. This variable governs the display in sparse trees and in the agenda.
  2349. When 0 or negative, it means use this number (the absolute value of it)
  2350. even if a deadline has a different individual lead time specified.
  2351. Custom commands can set this variable in the options section."
  2352. :group 'org-time
  2353. :group 'org-agenda-daily/weekly
  2354. :type 'integer)
  2355. (defcustom org-read-date-prefer-future t
  2356. "Non-nil means assume future for incomplete date input from user.
  2357. This affects the following situations:
  2358. 1. The user gives a month but not a year.
  2359. For example, if it is April and you enter \"feb 2\", this will be read
  2360. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2361. 2. The user gives a day, but no month.
  2362. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2363. read this as the third of *next* month. However, if you enter \"17\",
  2364. it will be considered as *this* month.
  2365. If you set this variable to the symbol `time', then also the following
  2366. will work:
  2367. 3. If the user gives a time, but no day. If the time is before now,
  2368. to will be interpreted as tomorrow.
  2369. Currently none of this works for ISO week specifications.
  2370. When this option is nil, the current day, month and year will always be
  2371. used as defaults.
  2372. See also `org-agenda-jump-prefer-future'."
  2373. :group 'org-time
  2374. :type '(choice
  2375. (const :tag "Never" nil)
  2376. (const :tag "Check month and day" t)
  2377. (const :tag "Check month, day, and time" time)))
  2378. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2379. "Should the agenda jump command prefer the future for incomplete dates?
  2380. The default is to do the same as configured in `org-read-date-prefer-future'.
  2381. But you can also set a deviating value here.
  2382. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2383. :group 'org-agenda
  2384. :group 'org-time
  2385. :version "24.1"
  2386. :type '(choice
  2387. (const :tag "Use org-read-date-prefer-future"
  2388. org-read-date-prefer-future)
  2389. (const :tag "Never" nil)
  2390. (const :tag "Always" t)))
  2391. (defcustom org-read-date-force-compatible-dates t
  2392. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2393. Depending on the system Emacs is running on, certain dates cannot
  2394. be represented with the type used internally to represent time.
  2395. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2396. correctly. Some systems allow for earlier dates, some for later,
  2397. some for both. One way to find out it to insert any date into an
  2398. Org buffer, putting the cursor on the year and hitting S-up and
  2399. S-down to test the range.
  2400. When this variable is set to t, the date/time prompt will not let
  2401. you specify dates outside the 1970-2037 range, so it is certain that
  2402. these dates will work in whatever version of Emacs you are
  2403. running, and also that you can move a file from one Emacs implementation
  2404. to another. WHenever Org is forcing the year for you, it will display
  2405. a message and beep.
  2406. When this variable is nil, Org will check if the date is
  2407. representable in the specific Emacs implementation you are using.
  2408. If not, it will force a year, usually the current year, and beep
  2409. to remind you. Currently this setting is not recommended because
  2410. the likelihood that you will open your Org files in an Emacs that
  2411. has limited date range is not negligible.
  2412. A workaround for this problem is to use diary sexp dates for time
  2413. stamps outside of this range."
  2414. :group 'org-time
  2415. :version "24.1"
  2416. :type 'boolean)
  2417. (defcustom org-read-date-display-live t
  2418. "Non-nil means display current interpretation of date prompt live.
  2419. This display will be in an overlay, in the minibuffer."
  2420. :group 'org-time
  2421. :type 'boolean)
  2422. (defcustom org-read-date-popup-calendar t
  2423. "Non-nil means pop up a calendar when prompting for a date.
  2424. In the calendar, the date can be selected with mouse-1. However, the
  2425. minibuffer will also be active, and you can simply enter the date as well.
  2426. When nil, only the minibuffer will be available."
  2427. :group 'org-time
  2428. :type 'boolean)
  2429. (if (fboundp 'defvaralias)
  2430. (defvaralias 'org-popup-calendar-for-date-prompt
  2431. 'org-read-date-popup-calendar))
  2432. (defcustom org-read-date-minibuffer-setup-hook nil
  2433. "Hook to be used to set up keys for the date/time interface.
  2434. Add key definitions to `minibuffer-local-map', which will be a temporary
  2435. copy."
  2436. :group 'org-time
  2437. :type 'hook)
  2438. (defcustom org-extend-today-until 0
  2439. "The hour when your day really ends. Must be an integer.
  2440. This has influence for the following applications:
  2441. - When switching the agenda to \"today\". It it is still earlier than
  2442. the time given here, the day recognized as TODAY is actually yesterday.
  2443. - When a date is read from the user and it is still before the time given
  2444. here, the current date and time will be assumed to be yesterday, 23:59.
  2445. Also, timestamps inserted in remember templates follow this rule.
  2446. IMPORTANT: This is a feature whose implementation is and likely will
  2447. remain incomplete. Really, it is only here because past midnight seems to
  2448. be the favorite working time of John Wiegley :-)"
  2449. :group 'org-time
  2450. :type 'integer)
  2451. (defcustom org-use-effective-time nil
  2452. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2453. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2454. \"effective time\" of any timestamps between midnight and 8am will be
  2455. 23:59 of the previous day."
  2456. :group 'org-time
  2457. :version "24.1"
  2458. :type 'boolean)
  2459. (defcustom org-edit-timestamp-down-means-later nil
  2460. "Non-nil means S-down will increase the time in a time stamp.
  2461. When nil, S-up will increase."
  2462. :group 'org-time
  2463. :type 'boolean)
  2464. (defcustom org-calendar-follow-timestamp-change t
  2465. "Non-nil means make the calendar window follow timestamp changes.
  2466. When a timestamp is modified and the calendar window is visible, it will be
  2467. moved to the new date."
  2468. :group 'org-time
  2469. :type 'boolean)
  2470. (defgroup org-tags nil
  2471. "Options concerning tags in Org-mode."
  2472. :tag "Org Tags"
  2473. :group 'org)
  2474. (defcustom org-tag-alist nil
  2475. "List of tags allowed in Org-mode files.
  2476. When this list is nil, Org-mode will base TAG input on what is already in the
  2477. buffer.
  2478. The value of this variable is an alist, the car of each entry must be a
  2479. keyword as a string, the cdr may be a character that is used to select
  2480. that tag through the fast-tag-selection interface.
  2481. See the manual for details."
  2482. :group 'org-tags
  2483. :type '(repeat
  2484. (choice
  2485. (cons (string :tag "Tag name")
  2486. (character :tag "Access char"))
  2487. (list :tag "Start radio group"
  2488. (const :startgroup)
  2489. (option (string :tag "Group description")))
  2490. (list :tag "End radio group"
  2491. (const :endgroup)
  2492. (option (string :tag "Group description")))
  2493. (const :tag "New line" (:newline)))))
  2494. (defcustom org-tag-persistent-alist nil
  2495. "List of tags that will always appear in all Org-mode files.
  2496. This is in addition to any in buffer settings or customizations
  2497. of `org-tag-alist'.
  2498. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2499. The value of this variable is an alist, the car of each entry must be a
  2500. keyword as a string, the cdr may be a character that is used to select
  2501. that tag through the fast-tag-selection interface.
  2502. See the manual for details.
  2503. To disable these tags on a per-file basis, insert anywhere in the file:
  2504. #+STARTUP: noptag"
  2505. :group 'org-tags
  2506. :type '(repeat
  2507. (choice
  2508. (cons (string :tag "Tag name")
  2509. (character :tag "Access char"))
  2510. (const :tag "Start radio group" (:startgroup))
  2511. (const :tag "End radio group" (:endgroup))
  2512. (const :tag "New line" (:newline)))))
  2513. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2514. "If non-nil, always offer completion for all tags of all agenda files.
  2515. Instead of customizing this variable directly, you might want to
  2516. set it locally for capture buffers, because there no list of
  2517. tags in that file can be created dynamically (there are none).
  2518. (add-hook 'org-capture-mode-hook
  2519. (lambda ()
  2520. (set (make-local-variable
  2521. 'org-complete-tags-always-offer-all-agenda-tags)
  2522. t)))"
  2523. :group 'org-tags
  2524. :version "24.1"
  2525. :type 'boolean)
  2526. (defvar org-file-tags nil
  2527. "List of tags that can be inherited by all entries in the file.
  2528. The tags will be inherited if the variable `org-use-tag-inheritance'
  2529. says they should be.
  2530. This variable is populated from #+FILETAGS lines.")
  2531. (defcustom org-use-fast-tag-selection 'auto
  2532. "Non-nil means use fast tag selection scheme.
  2533. This is a special interface to select and deselect tags with single keys.
  2534. When nil, fast selection is never used.
  2535. When the symbol `auto', fast selection is used if and only if selection
  2536. characters for tags have been configured, either through the variable
  2537. `org-tag-alist' or through a #+TAGS line in the buffer.
  2538. When t, fast selection is always used and selection keys are assigned
  2539. automatically if necessary."
  2540. :group 'org-tags
  2541. :type '(choice
  2542. (const :tag "Always" t)
  2543. (const :tag "Never" nil)
  2544. (const :tag "When selection characters are configured" 'auto)))
  2545. (defcustom org-fast-tag-selection-single-key nil
  2546. "Non-nil means fast tag selection exits after first change.
  2547. When nil, you have to press RET to exit it.
  2548. During fast tag selection, you can toggle this flag with `C-c'.
  2549. This variable can also have the value `expert'. In this case, the window
  2550. displaying the tags menu is not even shown, until you press C-c again."
  2551. :group 'org-tags
  2552. :type '(choice
  2553. (const :tag "No" nil)
  2554. (const :tag "Yes" t)
  2555. (const :tag "Expert" expert)))
  2556. (defvar org-fast-tag-selection-include-todo nil
  2557. "Non-nil means fast tags selection interface will also offer TODO states.
  2558. This is an undocumented feature, you should not rely on it.")
  2559. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2560. "The column to which tags should be indented in a headline.
  2561. If this number is positive, it specifies the column. If it is negative,
  2562. it means that the tags should be flushright to that column. For example,
  2563. -80 works well for a normal 80 character screen.
  2564. When 0, place tags directly after headline text, with only one space in
  2565. between."
  2566. :group 'org-tags
  2567. :type 'integer)
  2568. (defcustom org-auto-align-tags t
  2569. "Non-nil keeps tags aligned when modifying headlines.
  2570. Some operations (i.e. demoting) change the length of a headline and
  2571. therefore shift the tags around. With this option turned on, after
  2572. each such operation the tags are again aligned to `org-tags-column'."
  2573. :group 'org-tags
  2574. :type 'boolean)
  2575. (defcustom org-use-tag-inheritance t
  2576. "Non-nil means tags in levels apply also for sublevels.
  2577. When nil, only the tags directly given in a specific line apply there.
  2578. This may also be a list of tags that should be inherited, or a regexp that
  2579. matches tags that should be inherited. Additional control is possible
  2580. with the variable `org-tags-exclude-from-inheritance' which gives an
  2581. explicit list of tags to be excluded from inheritance., even if the value of
  2582. `org-use-tag-inheritance' would select it for inheritance.
  2583. If this option is t, a match early-on in a tree can lead to a large
  2584. number of matches in the subtree when constructing the agenda or creating
  2585. a sparse tree. If you only want to see the first match in a tree during
  2586. a search, check out the variable `org-tags-match-list-sublevels'."
  2587. :group 'org-tags
  2588. :type '(choice
  2589. (const :tag "Not" nil)
  2590. (const :tag "Always" t)
  2591. (repeat :tag "Specific tags" (string :tag "Tag"))
  2592. (regexp :tag "Tags matched by regexp")))
  2593. (defcustom org-tags-exclude-from-inheritance nil
  2594. "List of tags that should never be inherited.
  2595. This is a way to exclude a few tags from inheritance. For way to do
  2596. the opposite, to actively allow inheritance for selected tags,
  2597. see the variable `org-use-tag-inheritance'."
  2598. :group 'org-tags
  2599. :type '(repeat (string :tag "Tag")))
  2600. (defun org-tag-inherit-p (tag)
  2601. "Check if TAG is one that should be inherited."
  2602. (cond
  2603. ((member tag org-tags-exclude-from-inheritance) nil)
  2604. ((eq org-use-tag-inheritance t) t)
  2605. ((not org-use-tag-inheritance) nil)
  2606. ((stringp org-use-tag-inheritance)
  2607. (string-match org-use-tag-inheritance tag))
  2608. ((listp org-use-tag-inheritance)
  2609. (member tag org-use-tag-inheritance))
  2610. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2611. (defcustom org-tags-match-list-sublevels t
  2612. "Non-nil means list also sublevels of headlines matching a search.
  2613. This variable applies to tags/property searches, and also to stuck
  2614. projects because this search is based on a tags match as well.
  2615. When set to the symbol `indented', sublevels are indented with
  2616. leading dots.
  2617. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2618. the sublevels of a headline matching a tag search often also match
  2619. the same search. Listing all of them can create very long lists.
  2620. Setting this variable to nil causes subtrees of a match to be skipped.
  2621. This variable is semi-obsolete and probably should always be true. It
  2622. is better to limit inheritance to certain tags using the variables
  2623. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2624. :group 'org-tags
  2625. :type '(choice
  2626. (const :tag "No, don't list them" nil)
  2627. (const :tag "Yes, do list them" t)
  2628. (const :tag "List them, indented with leading dots" indented)))
  2629. (defcustom org-tags-sort-function nil
  2630. "When set, tags are sorted using this function as a comparator."
  2631. :group 'org-tags
  2632. :type '(choice
  2633. (const :tag "No sorting" nil)
  2634. (const :tag "Alphabetical" string<)
  2635. (const :tag "Reverse alphabetical" string>)
  2636. (function :tag "Custom function" nil)))
  2637. (defvar org-tags-history nil
  2638. "History of minibuffer reads for tags.")
  2639. (defvar org-last-tags-completion-table nil
  2640. "The last used completion table for tags.")
  2641. (defvar org-after-tags-change-hook nil
  2642. "Hook that is run after the tags in a line have changed.")
  2643. (defgroup org-properties nil
  2644. "Options concerning properties in Org-mode."
  2645. :tag "Org Properties"
  2646. :group 'org)
  2647. (defcustom org-property-format "%-10s %s"
  2648. "How property key/value pairs should be formatted by `indent-line'.
  2649. When `indent-line' hits a property definition, it will format the line
  2650. according to this format, mainly to make sure that the values are
  2651. lined-up with respect to each other."
  2652. :group 'org-properties
  2653. :type 'string)
  2654. (defcustom org-properties-postprocess-alist nil
  2655. "Alist of properties and functions to adjust inserted values.
  2656. Elements of this alist must be of the form
  2657. ([string] [function])
  2658. where [string] must be a property name and [function] must be a
  2659. lambda expression: this lambda expression must take one argument,
  2660. the value to adjust, and return the new value as a string.
  2661. For example, this element will allow the property \"Remaining\"
  2662. to be updated wrt the relation between the \"Effort\" property
  2663. and the clock summary:
  2664. ((\"Remaining\" (lambda(value)
  2665. (let ((clocksum (org-clock-sum-current-item))
  2666. (effort (org-duration-string-to-minutes
  2667. (org-entry-get (point) \"Effort\"))))
  2668. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2669. :group 'org-properties
  2670. :version "24.1"
  2671. :type 'alist)
  2672. (defcustom org-use-property-inheritance nil
  2673. "Non-nil means properties apply also for sublevels.
  2674. This setting is chiefly used during property searches. Turning it on can
  2675. cause significant overhead when doing a search, which is why it is not
  2676. on by default.
  2677. When nil, only the properties directly given in the current entry count.
  2678. When t, every property is inherited. The value may also be a list of
  2679. properties that should have inheritance, or a regular expression matching
  2680. properties that should be inherited.
  2681. However, note that some special properties use inheritance under special
  2682. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2683. and the properties ending in \"_ALL\" when they are used as descriptor
  2684. for valid values of a property.
  2685. Note for programmers:
  2686. When querying an entry with `org-entry-get', you can control if inheritance
  2687. should be used. By default, `org-entry-get' looks only at the local
  2688. properties. You can request inheritance by setting the inherit argument
  2689. to t (to force inheritance) or to `selective' (to respect the setting
  2690. in this variable)."
  2691. :group 'org-properties
  2692. :type '(choice
  2693. (const :tag "Not" nil)
  2694. (const :tag "Always" t)
  2695. (repeat :tag "Specific properties" (string :tag "Property"))
  2696. (regexp :tag "Properties matched by regexp")))
  2697. (defun org-property-inherit-p (property)
  2698. "Check if PROPERTY is one that should be inherited."
  2699. (cond
  2700. ((eq org-use-property-inheritance t) t)
  2701. ((not org-use-property-inheritance) nil)
  2702. ((stringp org-use-property-inheritance)
  2703. (string-match org-use-property-inheritance property))
  2704. ((listp org-use-property-inheritance)
  2705. (member property org-use-property-inheritance))
  2706. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2707. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2708. "The default column format, if no other format has been defined.
  2709. This variable can be set on the per-file basis by inserting a line
  2710. #+COLUMNS: %25ITEM ....."
  2711. :group 'org-properties
  2712. :type 'string)
  2713. (defcustom org-columns-ellipses ".."
  2714. "The ellipses to be used when a field in column view is truncated.
  2715. When this is the empty string, as many characters as possible are shown,
  2716. but then there will be no visual indication that the field has been truncated.
  2717. When this is a string of length N, the last N characters of a truncated
  2718. field are replaced by this string. If the column is narrower than the
  2719. ellipses string, only part of the ellipses string will be shown."
  2720. :group 'org-properties
  2721. :type 'string)
  2722. (defcustom org-columns-modify-value-for-display-function nil
  2723. "Function that modifies values for display in column view.
  2724. For example, it can be used to cut out a certain part from a time stamp.
  2725. The function must take 2 arguments:
  2726. column-title The title of the column (*not* the property name)
  2727. value The value that should be modified.
  2728. The function should return the value that should be displayed,
  2729. or nil if the normal value should be used."
  2730. :group 'org-properties
  2731. :type 'function)
  2732. (defcustom org-effort-property "Effort"
  2733. "The property that is being used to keep track of effort estimates.
  2734. Effort estimates given in this property need to have the format H:MM."
  2735. :group 'org-properties
  2736. :group 'org-progress
  2737. :type '(string :tag "Property"))
  2738. (defconst org-global-properties-fixed
  2739. '(("VISIBILITY_ALL" . "folded children content all")
  2740. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2741. "List of property/value pairs that can be inherited by any entry.
  2742. These are fixed values, for the preset properties. The user variable
  2743. that can be used to add to this list is `org-global-properties'.
  2744. The entries in this list are cons cells where the car is a property
  2745. name and cdr is a string with the value. If the value represents
  2746. multiple items like an \"_ALL\" property, separate the items by
  2747. spaces.")
  2748. (defcustom org-global-properties nil
  2749. "List of property/value pairs that can be inherited by any entry.
  2750. This list will be combined with the constant `org-global-properties-fixed'.
  2751. The entries in this list are cons cells where the car is a property
  2752. name and cdr is a string with the value.
  2753. You can set buffer-local values for the same purpose in the variable
  2754. `org-file-properties' this by adding lines like
  2755. #+PROPERTY: NAME VALUE"
  2756. :group 'org-properties
  2757. :type '(repeat
  2758. (cons (string :tag "Property")
  2759. (string :tag "Value"))))
  2760. (defvar org-file-properties nil
  2761. "List of property/value pairs that can be inherited by any entry.
  2762. Valid for the current buffer.
  2763. This variable is populated from #+PROPERTY lines.")
  2764. (make-variable-buffer-local 'org-file-properties)
  2765. (defgroup org-agenda nil
  2766. "Options concerning agenda views in Org-mode."
  2767. :tag "Org Agenda"
  2768. :group 'org)
  2769. (defvar org-category nil
  2770. "Variable used by org files to set a category for agenda display.
  2771. Such files should use a file variable to set it, for example
  2772. # -*- mode: org; org-category: \"ELisp\"
  2773. or contain a special line
  2774. #+CATEGORY: ELisp
  2775. If the file does not specify a category, then file's base name
  2776. is used instead.")
  2777. (make-variable-buffer-local 'org-category)
  2778. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2779. (defcustom org-agenda-files nil
  2780. "The files to be used for agenda display.
  2781. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2782. \\[org-remove-file]. You can also use customize to edit the list.
  2783. If an entry is a directory, all files in that directory that are matched by
  2784. `org-agenda-file-regexp' will be part of the file list.
  2785. If the value of the variable is not a list but a single file name, then
  2786. the list of agenda files is actually stored and maintained in that file, one
  2787. agenda file per line. In this file paths can be given relative to
  2788. `org-directory'. Tilde expansion and environment variable substitution
  2789. are also made."
  2790. :group 'org-agenda
  2791. :type '(choice
  2792. (repeat :tag "List of files and directories" file)
  2793. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2794. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2795. "Regular expression to match files for `org-agenda-files'.
  2796. If any element in the list in that variable contains a directory instead
  2797. of a normal file, all files in that directory that are matched by this
  2798. regular expression will be included."
  2799. :group 'org-agenda
  2800. :type 'regexp)
  2801. (defcustom org-agenda-text-search-extra-files nil
  2802. "List of extra files to be searched by text search commands.
  2803. These files will be search in addition to the agenda files by the
  2804. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2805. Note that these files will only be searched for text search commands,
  2806. not for the other agenda views like todo lists, tag searches or the weekly
  2807. agenda. This variable is intended to list notes and possibly archive files
  2808. that should also be searched by these two commands.
  2809. In fact, if the first element in the list is the symbol `agenda-archives',
  2810. than all archive files of all agenda files will be added to the search
  2811. scope."
  2812. :group 'org-agenda
  2813. :type '(set :greedy t
  2814. (const :tag "Agenda Archives" agenda-archives)
  2815. (repeat :inline t (file))))
  2816. (if (fboundp 'defvaralias)
  2817. (defvaralias 'org-agenda-multi-occur-extra-files
  2818. 'org-agenda-text-search-extra-files))
  2819. (defcustom org-agenda-skip-unavailable-files nil
  2820. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2821. A nil value means to remove them, after a query, from the list."
  2822. :group 'org-agenda
  2823. :type 'boolean)
  2824. (defcustom org-calendar-to-agenda-key [?c]
  2825. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2826. The command `org-calendar-goto-agenda' will be bound to this key. The
  2827. default is the character `c' because then `c' can be used to switch back and
  2828. forth between agenda and calendar."
  2829. :group 'org-agenda
  2830. :type 'sexp)
  2831. (defcustom org-calendar-agenda-action-key [?k]
  2832. "The key to be installed in `calendar-mode-map' for agenda-action.
  2833. The command `org-agenda-action' will be bound to this key. The
  2834. default is the character `k' because we use the same key in the agenda."
  2835. :group 'org-agenda
  2836. :type 'sexp)
  2837. (defcustom org-calendar-insert-diary-entry-key [?i]
  2838. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2839. This option is irrelevant until `org-agenda-diary-file' has been configured
  2840. to point to an Org-mode file. When that is the case, the command
  2841. `org-agenda-diary-entry' will be bound to the key given here, by default
  2842. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2843. if you want to continue doing this, you need to change this to a different
  2844. key."
  2845. :group 'org-agenda
  2846. :type 'sexp)
  2847. (defcustom org-agenda-diary-file 'diary-file
  2848. "File to which to add new entries with the `i' key in agenda and calendar.
  2849. When this is the symbol `diary-file', the functionality in the Emacs
  2850. calendar will be used to add entries to the `diary-file'. But when this
  2851. points to a file, `org-agenda-diary-entry' will be used instead."
  2852. :group 'org-agenda
  2853. :type '(choice
  2854. (const :tag "The standard Emacs diary file" diary-file)
  2855. (file :tag "Special Org file diary entries")))
  2856. (eval-after-load "calendar"
  2857. '(progn
  2858. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2859. 'org-calendar-goto-agenda)
  2860. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2861. 'org-agenda-action)
  2862. (add-hook 'calendar-mode-hook
  2863. (lambda ()
  2864. (unless (eq org-agenda-diary-file 'diary-file)
  2865. (define-key calendar-mode-map
  2866. org-calendar-insert-diary-entry-key
  2867. 'org-agenda-diary-entry))))))
  2868. (defgroup org-latex nil
  2869. "Options for embedding LaTeX code into Org-mode."
  2870. :tag "Org LaTeX"
  2871. :group 'org)
  2872. (defcustom org-format-latex-options
  2873. '(:foreground default :background default :scale 1.0
  2874. :html-foreground "Black" :html-background "Transparent"
  2875. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2876. "Options for creating images from LaTeX fragments.
  2877. This is a property list with the following properties:
  2878. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2879. `default' means use the foreground of the default face.
  2880. :background the background color, or \"Transparent\".
  2881. `default' means use the background of the default face.
  2882. :scale a scaling factor for the size of the images, to get more pixels
  2883. :html-foreground, :html-background, :html-scale
  2884. the same numbers for HTML export.
  2885. :matchers a list indicating which matchers should be used to
  2886. find LaTeX fragments. Valid members of this list are:
  2887. \"begin\" find environments
  2888. \"$1\" find single characters surrounded by $.$
  2889. \"$\" find math expressions surrounded by $...$
  2890. \"$$\" find math expressions surrounded by $$....$$
  2891. \"\\(\" find math expressions surrounded by \\(...\\)
  2892. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2893. :group 'org-latex
  2894. :type 'plist)
  2895. (defcustom org-format-latex-signal-error t
  2896. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2897. When nil, just push out a message."
  2898. :group 'org-latex
  2899. :version "24.1"
  2900. :type 'boolean)
  2901. (defcustom org-latex-to-mathml-jar-file nil
  2902. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2903. Use this to specify additional executable file say a jar file.
  2904. When using MathToWeb as the converter, specify the full-path to
  2905. your mathtoweb.jar file."
  2906. :group 'org-latex
  2907. :version "24.1"
  2908. :type '(choice
  2909. (const :tag "None" nil)
  2910. (file :tag "JAR file" :must-match t)))
  2911. (defcustom org-latex-to-mathml-convert-command nil
  2912. "Command to convert LaTeX fragments to MathML.
  2913. Replace format-specifiers in the command as noted below and use
  2914. `shell-command' to convert LaTeX to MathML.
  2915. %j: Executable file in fully expanded form as specified by
  2916. `org-latex-to-mathml-jar-file'.
  2917. %I: Input LaTeX file in fully expanded form
  2918. %o: Output MathML file
  2919. This command is used by `org-create-math-formula'.
  2920. When using MathToWeb as the converter, set this to
  2921. \"java -jar %j -unicode -force -df %o %I\"."
  2922. :group 'org-latex
  2923. :version "24.1"
  2924. :type '(choice
  2925. (const :tag "None" nil)
  2926. (string :tag "\nShell command")))
  2927. (defcustom org-latex-create-formula-image-program 'dvipng
  2928. "Program to convert LaTeX fragments with.
  2929. dvipng Process the LaTeX fragments to dvi file, then convert
  2930. dvi files to png files using dvipng.
  2931. This will also include processing of non-math environments.
  2932. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2933. to convert pdf files to png files"
  2934. :group 'org-latex
  2935. :version "24.1"
  2936. :type '(choice
  2937. (const :tag "dvipng" dvipng)
  2938. (const :tag "imagemagick" imagemagick)))
  2939. (defun org-format-latex-mathml-available-p ()
  2940. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2941. (save-match-data
  2942. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2943. org-latex-to-mathml-convert-command)
  2944. (let ((executable (car (split-string
  2945. org-latex-to-mathml-convert-command))))
  2946. (when (executable-find executable)
  2947. (if (string-match
  2948. "%j" org-latex-to-mathml-convert-command)
  2949. (file-readable-p org-latex-to-mathml-jar-file)
  2950. t))))))
  2951. (defcustom org-format-latex-header "\\documentclass{article}
  2952. \\usepackage[usenames]{color}
  2953. \\usepackage{amsmath}
  2954. \\usepackage[mathscr]{eucal}
  2955. \\pagestyle{empty} % do not remove
  2956. \[PACKAGES]
  2957. \[DEFAULT-PACKAGES]
  2958. % The settings below are copied from fullpage.sty
  2959. \\setlength{\\textwidth}{\\paperwidth}
  2960. \\addtolength{\\textwidth}{-3cm}
  2961. \\setlength{\\oddsidemargin}{1.5cm}
  2962. \\addtolength{\\oddsidemargin}{-2.54cm}
  2963. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2964. \\setlength{\\textheight}{\\paperheight}
  2965. \\addtolength{\\textheight}{-\\headheight}
  2966. \\addtolength{\\textheight}{-\\headsep}
  2967. \\addtolength{\\textheight}{-\\footskip}
  2968. \\addtolength{\\textheight}{-3cm}
  2969. \\setlength{\\topmargin}{1.5cm}
  2970. \\addtolength{\\topmargin}{-2.54cm}"
  2971. "The document header used for processing LaTeX fragments.
  2972. It is imperative that this header make sure that no page number
  2973. appears on the page. The package defined in the variables
  2974. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2975. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2976. will be appended."
  2977. :group 'org-latex
  2978. :type 'string)
  2979. (defvar org-format-latex-header-extra nil)
  2980. (defun org-set-packages-alist (var val)
  2981. "Set the packages alist and make sure it has 3 elements per entry."
  2982. (set var (mapcar (lambda (x)
  2983. (if (and (consp x) (= (length x) 2))
  2984. (list (car x) (nth 1 x) t)
  2985. x))
  2986. val)))
  2987. (defun org-get-packages-alist (var)
  2988. "Get the packages alist and make sure it has 3 elements per entry."
  2989. (mapcar (lambda (x)
  2990. (if (and (consp x) (= (length x) 2))
  2991. (list (car x) (nth 1 x) t)
  2992. x))
  2993. (default-value var)))
  2994. ;; The following variables are defined here because is it also used
  2995. ;; when formatting latex fragments. Originally it was part of the
  2996. ;; LaTeX exporter, which is why the name includes "export".
  2997. (defcustom org-export-latex-default-packages-alist
  2998. '(("AUTO" "inputenc" t)
  2999. ("T1" "fontenc" t)
  3000. ("" "fixltx2e" nil)
  3001. ("" "graphicx" t)
  3002. ("" "longtable" nil)
  3003. ("" "float" nil)
  3004. ("" "wrapfig" nil)
  3005. ("" "soul" t)
  3006. ("" "textcomp" t)
  3007. ("" "marvosym" t)
  3008. ("" "wasysym" t)
  3009. ("" "latexsym" t)
  3010. ("" "amssymb" t)
  3011. ("" "hyperref" nil)
  3012. "\\tolerance=1000"
  3013. )
  3014. "Alist of default packages to be inserted in the header.
  3015. Change this only if one of the packages here causes an incompatibility
  3016. with another package you are using.
  3017. The packages in this list are needed by one part or another of Org-mode
  3018. to function properly.
  3019. - inputenc, fontenc: for basic font and character selection
  3020. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3021. for interpreting the entities in `org-entities'. You can skip some of these
  3022. packages if you don't use any of the symbols in it.
  3023. - graphicx: for including images
  3024. - float, wrapfig: for figure placement
  3025. - longtable: for long tables
  3026. - hyperref: for cross references
  3027. Therefore you should not modify this variable unless you know what you
  3028. are doing. The one reason to change it anyway is that you might be loading
  3029. some other package that conflicts with one of the default packages.
  3030. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3031. If SNIPPET-FLAG is t, the package also needs to be included when
  3032. compiling LaTeX snippets into images for inclusion into HTML."
  3033. :group 'org-export-latex
  3034. :set 'org-set-packages-alist
  3035. :get 'org-get-packages-alist
  3036. :version "24.1"
  3037. :type '(repeat
  3038. (choice
  3039. (list :tag "options/package pair"
  3040. (string :tag "options")
  3041. (string :tag "package")
  3042. (boolean :tag "Snippet"))
  3043. (string :tag "A line of LaTeX"))))
  3044. (defcustom org-export-latex-packages-alist nil
  3045. "Alist of packages to be inserted in every LaTeX header.
  3046. These will be inserted after `org-export-latex-default-packages-alist'.
  3047. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3048. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3049. turning LaTeX snippets into images for inclusion into HTML.
  3050. Make sure that you only list packages here which:
  3051. - you want in every file
  3052. - do not conflict with the default packages in
  3053. `org-export-latex-default-packages-alist'
  3054. - do not conflict with the setup in `org-format-latex-header'."
  3055. :group 'org-export-latex
  3056. :set 'org-set-packages-alist
  3057. :get 'org-get-packages-alist
  3058. :type '(repeat
  3059. (choice
  3060. (list :tag "options/package pair"
  3061. (string :tag "options")
  3062. (string :tag "package")
  3063. (boolean :tag "Snippet"))
  3064. (string :tag "A line of LaTeX"))))
  3065. (defgroup org-appearance nil
  3066. "Settings for Org-mode appearance."
  3067. :tag "Org Appearance"
  3068. :group 'org)
  3069. (defcustom org-level-color-stars-only nil
  3070. "Non-nil means fontify only the stars in each headline.
  3071. When nil, the entire headline is fontified.
  3072. Changing it requires restart of `font-lock-mode' to become effective
  3073. also in regions already fontified."
  3074. :group 'org-appearance
  3075. :type 'boolean)
  3076. (defcustom org-hide-leading-stars nil
  3077. "Non-nil means hide the first N-1 stars in a headline.
  3078. This works by using the face `org-hide' for these stars. This
  3079. face is white for a light background, and black for a dark
  3080. background. You may have to customize the face `org-hide' to
  3081. make this work.
  3082. Changing it requires restart of `font-lock-mode' to become effective
  3083. also in regions already fontified.
  3084. You may also set this on a per-file basis by adding one of the following
  3085. lines to the buffer:
  3086. #+STARTUP: hidestars
  3087. #+STARTUP: showstars"
  3088. :group 'org-appearance
  3089. :type 'boolean)
  3090. (defcustom org-hidden-keywords nil
  3091. "List of symbols corresponding to keywords to be hidden the org buffer.
  3092. For example, a value '(title) for this list will make the document's title
  3093. appear in the buffer without the initial #+TITLE: keyword."
  3094. :group 'org-appearance
  3095. :version "24.1"
  3096. :type '(set (const :tag "#+AUTHOR" author)
  3097. (const :tag "#+DATE" date)
  3098. (const :tag "#+EMAIL" email)
  3099. (const :tag "#+TITLE" title)))
  3100. (defcustom org-fontify-done-headline nil
  3101. "Non-nil means change the face of a headline if it is marked DONE.
  3102. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3103. When this is non-nil, the headline after the keyword is set to the
  3104. `org-headline-done' as an additional indication."
  3105. :group 'org-appearance
  3106. :type 'boolean)
  3107. (defcustom org-fontify-emphasized-text t
  3108. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3109. Changing this variable requires a restart of Emacs to take effect."
  3110. :group 'org-appearance
  3111. :type 'boolean)
  3112. (defcustom org-fontify-whole-heading-line nil
  3113. "Non-nil means fontify the whole line for headings.
  3114. This is useful when setting a background color for the
  3115. org-level-* faces."
  3116. :group 'org-appearance
  3117. :type 'boolean)
  3118. (defcustom org-highlight-latex-fragments-and-specials nil
  3119. "Non-nil means fontify what is treated specially by the exporters."
  3120. :group 'org-appearance
  3121. :type 'boolean)
  3122. (defcustom org-hide-emphasis-markers nil
  3123. "Non-nil mean font-lock should hide the emphasis marker characters."
  3124. :group 'org-appearance
  3125. :type 'boolean)
  3126. (defcustom org-pretty-entities nil
  3127. "Non-nil means show entities as UTF8 characters.
  3128. When nil, the \\name form remains in the buffer."
  3129. :group 'org-appearance
  3130. :version "24.1"
  3131. :type 'boolean)
  3132. (defcustom org-pretty-entities-include-sub-superscripts t
  3133. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3134. :group 'org-appearance
  3135. :version "24.1"
  3136. :type 'boolean)
  3137. (defvar org-emph-re nil
  3138. "Regular expression for matching emphasis.
  3139. After a match, the match groups contain these elements:
  3140. 0 The match of the full regular expression, including the characters
  3141. before and after the proper match
  3142. 1 The character before the proper match, or empty at beginning of line
  3143. 2 The proper match, including the leading and trailing markers
  3144. 3 The leading marker like * or /, indicating the type of highlighting
  3145. 4 The text between the emphasis markers, not including the markers
  3146. 5 The character after the match, empty at the end of a line")
  3147. (defvar org-verbatim-re nil
  3148. "Regular expression for matching verbatim text.")
  3149. (defvar org-emphasis-regexp-components) ; defined just below
  3150. (defvar org-emphasis-alist) ; defined just below
  3151. (defun org-set-emph-re (var val)
  3152. "Set variable and compute the emphasis regular expression."
  3153. (set var val)
  3154. (when (and (boundp 'org-emphasis-alist)
  3155. (boundp 'org-emphasis-regexp-components)
  3156. org-emphasis-alist org-emphasis-regexp-components)
  3157. (let* ((e org-emphasis-regexp-components)
  3158. (pre (car e))
  3159. (post (nth 1 e))
  3160. (border (nth 2 e))
  3161. (body (nth 3 e))
  3162. (nl (nth 4 e))
  3163. (body1 (concat body "*?"))
  3164. (markers (mapconcat 'car org-emphasis-alist ""))
  3165. (vmarkers (mapconcat
  3166. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3167. org-emphasis-alist "")))
  3168. ;; make sure special characters appear at the right position in the class
  3169. (if (string-match "\\^" markers)
  3170. (setq markers (concat (replace-match "" t t markers) "^")))
  3171. (if (string-match "-" markers)
  3172. (setq markers (concat (replace-match "" t t markers) "-")))
  3173. (if (string-match "\\^" vmarkers)
  3174. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3175. (if (string-match "-" vmarkers)
  3176. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3177. (if (> nl 0)
  3178. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3179. (int-to-string nl) "\\}")))
  3180. ;; Make the regexp
  3181. (setq org-emph-re
  3182. (concat "\\([" pre "]\\|^\\)"
  3183. "\\("
  3184. "\\([" markers "]\\)"
  3185. "\\("
  3186. "[^" border "]\\|"
  3187. "[^" border "]"
  3188. body1
  3189. "[^" border "]"
  3190. "\\)"
  3191. "\\3\\)"
  3192. "\\([" post "]\\|$\\)"))
  3193. (setq org-verbatim-re
  3194. (concat "\\([" pre "]\\|^\\)"
  3195. "\\("
  3196. "\\([" vmarkers "]\\)"
  3197. "\\("
  3198. "[^" border "]\\|"
  3199. "[^" border "]"
  3200. body1
  3201. "[^" border "]"
  3202. "\\)"
  3203. "\\3\\)"
  3204. "\\([" post "]\\|$\\)")))))
  3205. (defcustom org-emphasis-regexp-components
  3206. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3207. "Components used to build the regular expression for emphasis.
  3208. This is a list with five entries. Terminology: In an emphasis string
  3209. like \" *strong word* \", we call the initial space PREMATCH, the final
  3210. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3211. and \"trong wor\" is the body. The different components in this variable
  3212. specify what is allowed/forbidden in each part:
  3213. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3214. post Chars allowed as postmatch. End of line will be allowed too.
  3215. border The chars *forbidden* as border characters.
  3216. body-regexp A regexp like \".\" to match a body character. Don't use
  3217. non-shy groups here, and don't allow newline here.
  3218. newline The maximum number of newlines allowed in an emphasis exp.
  3219. Use customize to modify this, or restart Emacs after changing it."
  3220. :group 'org-appearance
  3221. :set 'org-set-emph-re
  3222. :type '(list
  3223. (sexp :tag "Allowed chars in pre ")
  3224. (sexp :tag "Allowed chars in post ")
  3225. (sexp :tag "Forbidden chars in border ")
  3226. (sexp :tag "Regexp for body ")
  3227. (integer :tag "number of newlines allowed")
  3228. (option (boolean :tag "Please ignore this button"))))
  3229. (defcustom org-emphasis-alist
  3230. `(("*" bold "<b>" "</b>")
  3231. ("/" italic "<i>" "</i>")
  3232. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3233. ("=" org-code "<code>" "</code>" verbatim)
  3234. ("~" org-verbatim "<code>" "</code>" verbatim)
  3235. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3236. "<del>" "</del>")
  3237. )
  3238. "Special syntax for emphasized text.
  3239. Text starting and ending with a special character will be emphasized, for
  3240. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3241. characters, the face to be used by font-lock for highlighting in Org-mode
  3242. Emacs buffers, and the HTML tags to be used for this.
  3243. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3244. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3245. Use customize to modify this, or restart Emacs after changing it."
  3246. :group 'org-appearance
  3247. :set 'org-set-emph-re
  3248. :type '(repeat
  3249. (list
  3250. (string :tag "Marker character")
  3251. (choice
  3252. (face :tag "Font-lock-face")
  3253. (plist :tag "Face property list"))
  3254. (string :tag "HTML start tag")
  3255. (string :tag "HTML end tag")
  3256. (option (const verbatim)))))
  3257. (defvar org-protecting-blocks
  3258. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3259. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3260. This is needed for font-lock setup.")
  3261. ;;; Miscellaneous options
  3262. (defgroup org-completion nil
  3263. "Completion in Org-mode."
  3264. :tag "Org Completion"
  3265. :group 'org)
  3266. (defcustom org-completion-use-ido nil
  3267. "Non-nil means use ido completion wherever possible.
  3268. Note that `ido-mode' must be active for this variable to be relevant.
  3269. If you decide to turn this variable on, you might well want to turn off
  3270. `org-outline-path-complete-in-steps'.
  3271. See also `org-completion-use-iswitchb'."
  3272. :group 'org-completion
  3273. :type 'boolean)
  3274. (defcustom org-completion-use-iswitchb nil
  3275. "Non-nil means use iswitchb completion wherever possible.
  3276. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3277. If you decide to turn this variable on, you might well want to turn off
  3278. `org-outline-path-complete-in-steps'.
  3279. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3280. :group 'org-completion
  3281. :type 'boolean)
  3282. (defcustom org-completion-fallback-command 'hippie-expand
  3283. "The expansion command called by \\[pcomplete] in normal context.
  3284. Normal means, no org-mode-specific context."
  3285. :group 'org-completion
  3286. :type 'function)
  3287. ;;; Functions and variables from their packages
  3288. ;; Declared here to avoid compiler warnings
  3289. ;; XEmacs only
  3290. (defvar outline-mode-menu-heading)
  3291. (defvar outline-mode-menu-show)
  3292. (defvar outline-mode-menu-hide)
  3293. (defvar zmacs-regions) ; XEmacs regions
  3294. ;; Emacs only
  3295. (defvar mark-active)
  3296. ;; Various packages
  3297. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3298. (declare-function calendar-forward-day "cal-move" (arg))
  3299. (declare-function calendar-goto-date "cal-move" (date))
  3300. (declare-function calendar-goto-today "cal-move" ())
  3301. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3302. (defvar calc-embedded-close-formula)
  3303. (defvar calc-embedded-open-formula)
  3304. (declare-function cdlatex-tab "ext:cdlatex" ())
  3305. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3306. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3307. (defvar font-lock-unfontify-region-function)
  3308. (declare-function iswitchb-read-buffer "iswitchb"
  3309. (prompt &optional default require-match start matches-set))
  3310. (defvar iswitchb-temp-buflist)
  3311. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3312. (defvar org-agenda-tags-todo-honor-ignore-options)
  3313. (declare-function org-agenda-skip "org-agenda" ())
  3314. (declare-function
  3315. org-agenda-format-item "org-agenda"
  3316. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3317. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3318. (declare-function org-agenda-change-all-lines "org-agenda"
  3319. (newhead hdmarker &optional fixface just-this))
  3320. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3321. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3322. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3323. (beg end))
  3324. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3325. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3326. "org-agenda" (&optional end))
  3327. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3328. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3329. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3330. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3331. (declare-function org-indent-mode "org-indent" (&optional arg))
  3332. (declare-function parse-time-string "parse-time" (string))
  3333. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3334. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3335. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3336. (defvar remember-data-file)
  3337. (defvar texmathp-why)
  3338. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3339. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3340. (defvar w3m-current-url)
  3341. (defvar w3m-current-title)
  3342. (defvar org-latex-regexps)
  3343. ;;; Autoload and prepare some org modules
  3344. ;; Some table stuff that needs to be defined here, because it is used
  3345. ;; by the functions setting up org-mode or checking for table context.
  3346. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3347. "Detect an org-type or table-type table.")
  3348. (defconst org-table-line-regexp "^[ \t]*|"
  3349. "Detect an org-type table line.")
  3350. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3351. "Detect an org-type table line.")
  3352. (defconst org-table-hline-regexp "^[ \t]*|-"
  3353. "Detect an org-type table hline.")
  3354. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3355. "Detect a table-type table hline.")
  3356. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3357. "Detect the first line outside a table when searching from within it.
  3358. This works for both table types.")
  3359. ;; Autoload the functions in org-table.el that are needed by functions here.
  3360. (eval-and-compile
  3361. (org-autoload "org-table"
  3362. '(org-table-align org-table-begin org-table-blank-field
  3363. org-table-convert org-table-convert-region org-table-copy-down
  3364. org-table-copy-region org-table-create
  3365. org-table-create-or-convert-from-region
  3366. org-table-create-with-table.el org-table-current-dline
  3367. org-table-cut-region org-table-delete-column org-table-edit-field
  3368. org-table-edit-formulas org-table-end org-table-eval-formula
  3369. org-table-export org-table-field-info
  3370. org-table-get-stored-formulas org-table-goto-column
  3371. org-table-hline-and-move org-table-import org-table-insert-column
  3372. org-table-insert-hline org-table-insert-row org-table-iterate
  3373. org-table-justify-field-maybe org-table-kill-row
  3374. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3375. org-table-move-column org-table-move-column-left
  3376. org-table-move-column-right org-table-move-row
  3377. org-table-move-row-down org-table-move-row-up
  3378. org-table-next-field org-table-next-row org-table-paste-rectangle
  3379. org-table-previous-field org-table-recalculate
  3380. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3381. org-table-toggle-coordinate-overlays
  3382. org-table-toggle-formula-debugger org-table-wrap-region
  3383. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3384. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3385. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3386. (defun org-at-table-p (&optional table-type)
  3387. "Return t if the cursor is inside an org-type table.
  3388. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3389. (if org-enable-table-editor
  3390. (save-excursion
  3391. (beginning-of-line 1)
  3392. (looking-at (if table-type org-table-any-line-regexp
  3393. org-table-line-regexp)))
  3394. nil))
  3395. (defsubst org-table-p () (org-at-table-p))
  3396. (defun org-at-table.el-p ()
  3397. "Return t if and only if we are at a table.el table."
  3398. (and (org-at-table-p 'any)
  3399. (save-excursion
  3400. (goto-char (org-table-begin 'any))
  3401. (looking-at org-table1-hline-regexp))))
  3402. (defun org-table-recognize-table.el ()
  3403. "If there is a table.el table nearby, recognize it and move into it."
  3404. (if org-table-tab-recognizes-table.el
  3405. (if (org-at-table.el-p)
  3406. (progn
  3407. (beginning-of-line 1)
  3408. (if (looking-at org-table-dataline-regexp)
  3409. nil
  3410. (if (looking-at org-table1-hline-regexp)
  3411. (progn
  3412. (beginning-of-line 2)
  3413. (if (looking-at org-table-any-border-regexp)
  3414. (beginning-of-line -1)))))
  3415. (if (re-search-forward "|" (org-table-end t) t)
  3416. (progn
  3417. (require 'table)
  3418. (if (table--at-cell-p (point))
  3419. t
  3420. (message "recognizing table.el table...")
  3421. (table-recognize-table)
  3422. (message "recognizing table.el table...done")))
  3423. (error "This should not happen"))
  3424. t)
  3425. nil)
  3426. nil))
  3427. (defun org-at-table-hline-p ()
  3428. "Return t if the cursor is inside a hline in a table."
  3429. (if org-enable-table-editor
  3430. (save-excursion
  3431. (beginning-of-line 1)
  3432. (looking-at org-table-hline-regexp))
  3433. nil))
  3434. (defvar org-table-clean-did-remove-column nil)
  3435. (defun org-table-map-tables (function &optional quietly)
  3436. "Apply FUNCTION to the start of all tables in the buffer."
  3437. (save-excursion
  3438. (save-restriction
  3439. (widen)
  3440. (goto-char (point-min))
  3441. (while (re-search-forward org-table-any-line-regexp nil t)
  3442. (unless quietly
  3443. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3444. (beginning-of-line 1)
  3445. (when (and (looking-at org-table-line-regexp)
  3446. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3447. (not (org-in-block-p '("src" "example"))))
  3448. (save-excursion (funcall function))
  3449. (or (looking-at org-table-line-regexp)
  3450. (forward-char 1)))
  3451. (re-search-forward org-table-any-border-regexp nil 1))))
  3452. (unless quietly (message "Mapping tables: done")))
  3453. ;; Declare and autoload functions from org-exp.el & Co
  3454. (declare-function org-default-export-plist "org-exp")
  3455. (declare-function org-infile-export-plist "org-exp")
  3456. (declare-function org-get-current-options "org-exp")
  3457. (eval-and-compile
  3458. (org-autoload "org-exp"
  3459. '(org-export org-export-visible
  3460. org-insert-export-options-template
  3461. org-table-clean-before-export))
  3462. (org-autoload "org-ascii"
  3463. '(org-export-as-ascii org-export-ascii-preprocess
  3464. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3465. org-export-region-as-ascii))
  3466. (org-autoload "org-latex"
  3467. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3468. org-replace-region-by-latex org-export-region-as-latex
  3469. org-export-as-latex org-export-as-pdf
  3470. org-export-as-pdf-and-open))
  3471. (org-autoload "org-html"
  3472. '(org-export-as-html-and-open
  3473. org-export-as-html-batch org-export-as-html-to-buffer
  3474. org-replace-region-by-html org-export-region-as-html
  3475. org-export-as-html))
  3476. (org-autoload "org-docbook"
  3477. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3478. org-replace-region-by-docbook org-export-region-as-docbook
  3479. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3480. org-export-as-docbook))
  3481. (org-autoload "org-icalendar"
  3482. '(org-export-icalendar-this-file
  3483. org-export-icalendar-all-agenda-files
  3484. org-export-icalendar-combine-agenda-files))
  3485. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3486. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3487. ;; Declare and autoload functions from org-agenda.el
  3488. (eval-and-compile
  3489. (org-autoload "org-agenda"
  3490. '(org-agenda org-agenda-list org-search-view
  3491. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3492. org-diary org-agenda-to-appt
  3493. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3494. ;; Autoload org-remember
  3495. (eval-and-compile
  3496. (org-autoload "org-remember"
  3497. '(org-remember-insinuate org-remember-annotation
  3498. org-remember-apply-template org-remember org-remember-handler)))
  3499. (eval-and-compile
  3500. (org-autoload "org-capture"
  3501. '(org-capture org-capture-insert-template-here
  3502. org-capture-import-remember-templates)))
  3503. ;; Autoload org-clock.el
  3504. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3505. (beg end))
  3506. (declare-function org-clock-update-mode-line "org-clock" ())
  3507. (declare-function org-resolve-clocks "org-clock"
  3508. (&optional also-non-dangling-p prompt last-valid))
  3509. (defvar org-clock-start-time)
  3510. (defvar org-clock-marker (make-marker)
  3511. "Marker recording the last clock-in.")
  3512. (defvar org-clock-hd-marker (make-marker)
  3513. "Marker recording the last clock-in, but the headline position.")
  3514. (defvar org-clock-heading ""
  3515. "The heading of the current clock entry.")
  3516. (defun org-clock-is-active ()
  3517. "Return non-nil if clock is currently running.
  3518. The return value is actually the clock marker."
  3519. (marker-buffer org-clock-marker))
  3520. (eval-and-compile
  3521. (org-autoload
  3522. "org-clock"
  3523. '(org-clock-in org-clock-out org-clock-cancel
  3524. org-clock-goto org-clock-sum org-clock-display
  3525. org-clock-remove-overlays org-clock-report
  3526. org-clocktable-shift org-dblock-write:clocktable
  3527. org-get-clocktable org-resolve-clocks)))
  3528. (defun org-clock-update-time-maybe ()
  3529. "If this is a CLOCK line, update it and return t.
  3530. Otherwise, return nil."
  3531. (interactive)
  3532. (save-excursion
  3533. (beginning-of-line 1)
  3534. (skip-chars-forward " \t")
  3535. (when (looking-at org-clock-string)
  3536. (let ((re (concat "[ \t]*" org-clock-string
  3537. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3538. "\\([ \t]*=>.*\\)?\\)?"))
  3539. ts te h m s neg)
  3540. (cond
  3541. ((not (looking-at re))
  3542. nil)
  3543. ((not (match-end 2))
  3544. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3545. (> org-clock-marker (point))
  3546. (<= org-clock-marker (point-at-eol)))
  3547. ;; The clock is running here
  3548. (setq org-clock-start-time
  3549. (apply 'encode-time
  3550. (org-parse-time-string (match-string 1))))
  3551. (org-clock-update-mode-line)))
  3552. (t
  3553. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3554. (end-of-line 1)
  3555. (setq ts (match-string 1)
  3556. te (match-string 3))
  3557. (setq s (- (org-float-time
  3558. (apply 'encode-time (org-parse-time-string te)))
  3559. (org-float-time
  3560. (apply 'encode-time (org-parse-time-string ts))))
  3561. neg (< s 0)
  3562. s (abs s)
  3563. h (floor (/ s 3600))
  3564. s (- s (* 3600 h))
  3565. m (floor (/ s 60))
  3566. s (- s (* 60 s)))
  3567. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3568. t))))))
  3569. (defun org-check-running-clock ()
  3570. "Check if the current buffer contains the running clock.
  3571. If yes, offer to stop it and to save the buffer with the changes."
  3572. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3573. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3574. (buffer-name))))
  3575. (org-clock-out)
  3576. (when (y-or-n-p "Save changed buffer?")
  3577. (save-buffer))))
  3578. (defun org-clocktable-try-shift (dir n)
  3579. "Check if this line starts a clock table, if yes, shift the time block."
  3580. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3581. (org-clocktable-shift dir n)))
  3582. ;; Autoload org-timer.el
  3583. (eval-and-compile
  3584. (org-autoload
  3585. "org-timer"
  3586. '(org-timer-start org-timer org-timer-item
  3587. org-timer-change-times-in-region
  3588. org-timer-set-timer
  3589. org-timer-reset-timers
  3590. org-timer-show-remaining-time)))
  3591. ;; Autoload org-feed.el
  3592. (eval-and-compile
  3593. (org-autoload
  3594. "org-feed"
  3595. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3596. ;; Autoload org-indent.el
  3597. ;; Define the variable already here, to make sure we have it.
  3598. (defvar org-indent-mode nil
  3599. "Non-nil if Org-Indent mode is enabled.
  3600. Use the command `org-indent-mode' to change this variable.")
  3601. (eval-and-compile
  3602. (org-autoload
  3603. "org-indent"
  3604. '(org-indent-mode)))
  3605. ;; Autoload org-mobile.el
  3606. (eval-and-compile
  3607. (org-autoload
  3608. "org-mobile"
  3609. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3610. ;; Autoload archiving code
  3611. ;; The stuff that is needed for cycling and tags has to be defined here.
  3612. (defgroup org-archive nil
  3613. "Options concerning archiving in Org-mode."
  3614. :tag "Org Archive"
  3615. :group 'org-structure)
  3616. (defcustom org-archive-location "%s_archive::"
  3617. "The location where subtrees should be archived.
  3618. The value of this variable is a string, consisting of two parts,
  3619. separated by a double-colon. The first part is a filename and
  3620. the second part is a headline.
  3621. When the filename is omitted, archiving happens in the same file.
  3622. %s in the filename will be replaced by the current file
  3623. name (without the directory part). Archiving to a different file
  3624. is useful to keep archived entries from contributing to the
  3625. Org-mode Agenda.
  3626. The archived entries will be filed as subtrees of the specified
  3627. headline. When the headline is omitted, the subtrees are simply
  3628. filed away at the end of the file, as top-level entries. Also in
  3629. the heading you can use %s to represent the file name, this can be
  3630. useful when using the same archive for a number of different files.
  3631. Here are a few examples:
  3632. \"%s_archive::\"
  3633. If the current file is Projects.org, archive in file
  3634. Projects.org_archive, as top-level trees. This is the default.
  3635. \"::* Archived Tasks\"
  3636. Archive in the current file, under the top-level headline
  3637. \"* Archived Tasks\".
  3638. \"~/org/archive.org::\"
  3639. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3640. \"~/org/archive.org::* From %s\"
  3641. Archive in file ~/org/archive.org (absolute path), under headlines
  3642. \"From FILENAME\" where file name is the current file name.
  3643. \"basement::** Finished Tasks\"
  3644. Archive in file ./basement (relative path), as level 3 trees
  3645. below the level 2 heading \"** Finished Tasks\".
  3646. You may set this option on a per-file basis by adding to the buffer a
  3647. line like
  3648. #+ARCHIVE: basement::** Finished Tasks
  3649. You may also define it locally for a subtree by setting an ARCHIVE property
  3650. in the entry. If such a property is found in an entry, or anywhere up
  3651. the hierarchy, it will be used."
  3652. :group 'org-archive
  3653. :type 'string)
  3654. (defcustom org-archive-tag "ARCHIVE"
  3655. "The tag that marks a subtree as archived.
  3656. An archived subtree does not open during visibility cycling, and does
  3657. not contribute to the agenda listings.
  3658. After changing this, font-lock must be restarted in the relevant buffers to
  3659. get the proper fontification."
  3660. :group 'org-archive
  3661. :group 'org-keywords
  3662. :type 'string)
  3663. (defcustom org-agenda-skip-archived-trees t
  3664. "Non-nil means the agenda will skip any items located in archived trees.
  3665. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3666. variable is no longer recommended, you should leave it at the value t.
  3667. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3668. :group 'org-archive
  3669. :group 'org-agenda-skip
  3670. :type 'boolean)
  3671. (defcustom org-columns-skip-archived-trees t
  3672. "Non-nil means ignore archived trees when creating column view."
  3673. :group 'org-archive
  3674. :group 'org-properties
  3675. :type 'boolean)
  3676. (defcustom org-cycle-open-archived-trees nil
  3677. "Non-nil means `org-cycle' will open archived trees.
  3678. An archived tree is a tree marked with the tag ARCHIVE.
  3679. When nil, archived trees will stay folded. You can still open them with
  3680. normal outline commands like `show-all', but not with the cycling commands."
  3681. :group 'org-archive
  3682. :group 'org-cycle
  3683. :type 'boolean)
  3684. (defcustom org-sparse-tree-open-archived-trees nil
  3685. "Non-nil means sparse tree construction shows matches in archived trees.
  3686. When nil, matches in these trees are highlighted, but the trees are kept in
  3687. collapsed state."
  3688. :group 'org-archive
  3689. :group 'org-sparse-trees
  3690. :type 'boolean)
  3691. (defun org-cycle-hide-archived-subtrees (state)
  3692. "Re-hide all archived subtrees after a visibility state change."
  3693. (when (and (not org-cycle-open-archived-trees)
  3694. (not (memq state '(overview folded))))
  3695. (save-excursion
  3696. (let* ((globalp (memq state '(contents all)))
  3697. (beg (if globalp (point-min) (point)))
  3698. (end (if globalp (point-max) (org-end-of-subtree t))))
  3699. (org-hide-archived-subtrees beg end)
  3700. (goto-char beg)
  3701. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3702. (message "%s" (substitute-command-keys
  3703. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3704. (defun org-force-cycle-archived ()
  3705. "Cycle subtree even if it is archived."
  3706. (interactive)
  3707. (setq this-command 'org-cycle)
  3708. (let ((org-cycle-open-archived-trees t))
  3709. (call-interactively 'org-cycle)))
  3710. (defun org-hide-archived-subtrees (beg end)
  3711. "Re-hide all archived subtrees after a visibility state change."
  3712. (save-excursion
  3713. (let* ((re (concat ":" org-archive-tag ":")))
  3714. (goto-char beg)
  3715. (while (re-search-forward re end t)
  3716. (when (org-at-heading-p)
  3717. (org-flag-subtree t)
  3718. (org-end-of-subtree t))))))
  3719. (defun org-flag-subtree (flag)
  3720. (save-excursion
  3721. (org-back-to-heading t)
  3722. (outline-end-of-heading)
  3723. (outline-flag-region (point)
  3724. (progn (org-end-of-subtree t) (point))
  3725. flag)))
  3726. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3727. (eval-and-compile
  3728. (org-autoload "org-archive"
  3729. '(org-add-archive-files org-archive-subtree
  3730. org-archive-to-archive-sibling org-toggle-archive-tag
  3731. org-archive-subtree-default
  3732. org-archive-subtree-default-with-confirmation)))
  3733. ;; Autoload Column View Code
  3734. (declare-function org-columns-number-to-string "org-colview")
  3735. (declare-function org-columns-get-format-and-top-level "org-colview")
  3736. (declare-function org-columns-compute "org-colview")
  3737. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3738. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3739. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3740. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3741. ;; Autoload ID code
  3742. (declare-function org-id-store-link "org-id")
  3743. (declare-function org-id-locations-load "org-id")
  3744. (declare-function org-id-locations-save "org-id")
  3745. (defvar org-id-track-globally)
  3746. (org-autoload "org-id"
  3747. '(org-id-get-create org-id-new org-id-copy org-id-get
  3748. org-id-get-with-outline-path-completion
  3749. org-id-get-with-outline-drilling org-id-store-link
  3750. org-id-goto org-id-find org-id-store-link))
  3751. ;; Autoload Plotting Code
  3752. (org-autoload "org-plot"
  3753. '(org-plot/gnuplot))
  3754. ;;; Variables for pre-computed regular expressions, all buffer local
  3755. (defvar org-drawer-regexp nil
  3756. "Matches first line of a hidden block.")
  3757. (make-variable-buffer-local 'org-drawer-regexp)
  3758. (defvar org-todo-regexp nil
  3759. "Matches any of the TODO state keywords.")
  3760. (make-variable-buffer-local 'org-todo-regexp)
  3761. (defvar org-not-done-regexp nil
  3762. "Matches any of the TODO state keywords except the last one.")
  3763. (make-variable-buffer-local 'org-not-done-regexp)
  3764. (defvar org-not-done-heading-regexp nil
  3765. "Matches a TODO headline that is not done.")
  3766. (make-variable-buffer-local 'org-not-done-regexp)
  3767. (defvar org-todo-line-regexp nil
  3768. "Matches a headline and puts TODO state into group 2 if present.")
  3769. (make-variable-buffer-local 'org-todo-line-regexp)
  3770. (defvar org-complex-heading-regexp nil
  3771. "Matches a headline and puts everything into groups:
  3772. group 1: the stars
  3773. group 2: The todo keyword, maybe
  3774. group 3: Priority cookie
  3775. group 4: True headline
  3776. group 5: Tags")
  3777. (make-variable-buffer-local 'org-complex-heading-regexp)
  3778. (defvar org-complex-heading-regexp-format nil
  3779. "Printf format to make regexp to match an exact headline.
  3780. This regexp will match the headline of any node which has the
  3781. exact headline text that is put into the format, but may have any
  3782. TODO state, priority and tags.")
  3783. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3784. (defvar org-todo-line-tags-regexp nil
  3785. "Matches a headline and puts TODO state into group 2 if present.
  3786. Also put tags into group 4 if tags are present.")
  3787. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3788. (defvar org-ds-keyword-length 12
  3789. "Maximum length of the Deadline and SCHEDULED keywords.")
  3790. (make-variable-buffer-local 'org-ds-keyword-length)
  3791. (defvar org-deadline-regexp nil
  3792. "Matches the DEADLINE keyword.")
  3793. (make-variable-buffer-local 'org-deadline-regexp)
  3794. (defvar org-deadline-time-regexp nil
  3795. "Matches the DEADLINE keyword together with a time stamp.")
  3796. (make-variable-buffer-local 'org-deadline-time-regexp)
  3797. (defvar org-deadline-line-regexp nil
  3798. "Matches the DEADLINE keyword and the rest of the line.")
  3799. (make-variable-buffer-local 'org-deadline-line-regexp)
  3800. (defvar org-scheduled-regexp nil
  3801. "Matches the SCHEDULED keyword.")
  3802. (make-variable-buffer-local 'org-scheduled-regexp)
  3803. (defvar org-scheduled-time-regexp nil
  3804. "Matches the SCHEDULED keyword together with a time stamp.")
  3805. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3806. (defvar org-closed-time-regexp nil
  3807. "Matches the CLOSED keyword together with a time stamp.")
  3808. (make-variable-buffer-local 'org-closed-time-regexp)
  3809. (defvar org-keyword-time-regexp nil
  3810. "Matches any of the 4 keywords, together with the time stamp.")
  3811. (make-variable-buffer-local 'org-keyword-time-regexp)
  3812. (defvar org-keyword-time-not-clock-regexp nil
  3813. "Matches any of the 3 keywords, together with the time stamp.")
  3814. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3815. (defvar org-maybe-keyword-time-regexp nil
  3816. "Matches a timestamp, possibly preceded by a keyword.")
  3817. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3818. (defvar org-planning-or-clock-line-re nil
  3819. "Matches a line with planning or clock info.")
  3820. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3821. (defvar org-all-time-keywords nil
  3822. "List of time keywords.")
  3823. (make-variable-buffer-local 'org-all-time-keywords)
  3824. (defconst org-plain-time-of-day-regexp
  3825. (concat
  3826. "\\(\\<[012]?[0-9]"
  3827. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3828. "\\(--?"
  3829. "\\(\\<[012]?[0-9]"
  3830. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3831. "\\)?")
  3832. "Regular expression to match a plain time or time range.
  3833. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3834. groups carry important information:
  3835. 0 the full match
  3836. 1 the first time, range or not
  3837. 8 the second time, if it is a range.")
  3838. (defconst org-plain-time-extension-regexp
  3839. (concat
  3840. "\\(\\<[012]?[0-9]"
  3841. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3842. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3843. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3844. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3845. groups carry important information:
  3846. 0 the full match
  3847. 7 hours of duration
  3848. 9 minutes of duration")
  3849. (defconst org-stamp-time-of-day-regexp
  3850. (concat
  3851. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3852. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3853. "\\(--?"
  3854. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3855. "Regular expression to match a timestamp time or time range.
  3856. After a match, the following groups carry important information:
  3857. 0 the full match
  3858. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3859. 2 the first time, range or not
  3860. 4 the second time, if it is a range.")
  3861. (defconst org-startup-options
  3862. '(("fold" org-startup-folded t)
  3863. ("overview" org-startup-folded t)
  3864. ("nofold" org-startup-folded nil)
  3865. ("showall" org-startup-folded nil)
  3866. ("showeverything" org-startup-folded showeverything)
  3867. ("content" org-startup-folded content)
  3868. ("indent" org-startup-indented t)
  3869. ("noindent" org-startup-indented nil)
  3870. ("hidestars" org-hide-leading-stars t)
  3871. ("showstars" org-hide-leading-stars nil)
  3872. ("odd" org-odd-levels-only t)
  3873. ("oddeven" org-odd-levels-only nil)
  3874. ("align" org-startup-align-all-tables t)
  3875. ("noalign" org-startup-align-all-tables nil)
  3876. ("inlineimages" org-startup-with-inline-images t)
  3877. ("noinlineimages" org-startup-with-inline-images nil)
  3878. ("customtime" org-display-custom-times t)
  3879. ("logdone" org-log-done time)
  3880. ("lognotedone" org-log-done note)
  3881. ("nologdone" org-log-done nil)
  3882. ("lognoteclock-out" org-log-note-clock-out t)
  3883. ("nolognoteclock-out" org-log-note-clock-out nil)
  3884. ("logrepeat" org-log-repeat state)
  3885. ("lognoterepeat" org-log-repeat note)
  3886. ("nologrepeat" org-log-repeat nil)
  3887. ("logreschedule" org-log-reschedule time)
  3888. ("lognotereschedule" org-log-reschedule note)
  3889. ("nologreschedule" org-log-reschedule nil)
  3890. ("logredeadline" org-log-redeadline time)
  3891. ("lognoteredeadline" org-log-redeadline note)
  3892. ("nologredeadline" org-log-redeadline nil)
  3893. ("logrefile" org-log-refile time)
  3894. ("lognoterefile" org-log-refile note)
  3895. ("nologrefile" org-log-refile nil)
  3896. ("fninline" org-footnote-define-inline t)
  3897. ("nofninline" org-footnote-define-inline nil)
  3898. ("fnlocal" org-footnote-section nil)
  3899. ("fnauto" org-footnote-auto-label t)
  3900. ("fnprompt" org-footnote-auto-label nil)
  3901. ("fnconfirm" org-footnote-auto-label confirm)
  3902. ("fnplain" org-footnote-auto-label plain)
  3903. ("fnadjust" org-footnote-auto-adjust t)
  3904. ("nofnadjust" org-footnote-auto-adjust nil)
  3905. ("constcgs" constants-unit-system cgs)
  3906. ("constSI" constants-unit-system SI)
  3907. ("noptag" org-tag-persistent-alist nil)
  3908. ("hideblocks" org-hide-block-startup t)
  3909. ("nohideblocks" org-hide-block-startup nil)
  3910. ("beamer" org-startup-with-beamer-mode t)
  3911. ("entitiespretty" org-pretty-entities t)
  3912. ("entitiesplain" org-pretty-entities nil))
  3913. "Variable associated with STARTUP options for org-mode.
  3914. Each element is a list of three items: The startup options as written
  3915. in the #+STARTUP line, the corresponding variable, and the value to
  3916. set this variable to if the option is found. An optional forth element PUSH
  3917. means to push this value onto the list in the variable.")
  3918. (defun org-update-property-plist (key val props)
  3919. "Update PROPS with KEY and VAL."
  3920. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3921. (key (if appending (substring key 0 (- (length key) 1)) key))
  3922. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3923. (previous (cdr (assoc key props))))
  3924. (if appending
  3925. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3926. (cons (cons key val) remainder))))
  3927. (defconst org-block-regexp
  3928. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3929. "Regular expression for hiding blocks.")
  3930. (defconst org-heading-keyword-regexp-format
  3931. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3932. "Printf format for a regexp matching an headline with some keyword.
  3933. This regexp will match the headline of any node which has the
  3934. exact keyword that is put into the format. The keyword isn't in
  3935. any group by default, but the stars and the body are.")
  3936. (defconst org-heading-keyword-maybe-regexp-format
  3937. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3938. "Printf format for a regexp matching an headline, possibly with some keyword.
  3939. This regexp can match any headline with the specified keyword, or
  3940. without a keyword. The keyword isn't in any group by default,
  3941. but the stars and the body are.")
  3942. (defun org-set-regexps-and-options ()
  3943. "Precompute regular expressions for current buffer."
  3944. (when (derived-mode-p 'org-mode)
  3945. (org-set-local 'org-todo-kwd-alist nil)
  3946. (org-set-local 'org-todo-key-alist nil)
  3947. (org-set-local 'org-todo-key-trigger nil)
  3948. (org-set-local 'org-todo-keywords-1 nil)
  3949. (org-set-local 'org-done-keywords nil)
  3950. (org-set-local 'org-todo-heads nil)
  3951. (org-set-local 'org-todo-sets nil)
  3952. (org-set-local 'org-todo-log-states nil)
  3953. (org-set-local 'org-file-properties nil)
  3954. (org-set-local 'org-file-tags nil)
  3955. (let ((re (org-make-options-regexp
  3956. '("CATEGORY" "TODO" "COLUMNS"
  3957. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3958. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3959. "OPTIONS")
  3960. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3961. (splitre "[ \t]+")
  3962. (scripts org-use-sub-superscripts)
  3963. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3964. tail sep kws1 prio props ftags drawers beamer-p
  3965. ext-setup-or-nil setup-contents (start 0))
  3966. (save-excursion
  3967. (save-restriction
  3968. (widen)
  3969. (goto-char (point-min))
  3970. (while (or (and ext-setup-or-nil
  3971. (string-match re ext-setup-or-nil start)
  3972. (setq start (match-end 0)))
  3973. (and (setq ext-setup-or-nil nil start 0)
  3974. (re-search-forward re nil t)))
  3975. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3976. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3977. (if (stringp value) (setq value (org-trim value)))
  3978. (cond
  3979. ((equal key "CATEGORY")
  3980. (setq cat value))
  3981. ((member key '("SEQ_TODO" "TODO"))
  3982. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3983. ((equal key "TYP_TODO")
  3984. (push (cons 'type (org-split-string value splitre)) kwds))
  3985. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3986. ;; general TODO-like setup
  3987. (push (cons (intern (downcase (match-string 1 key)))
  3988. (org-split-string value splitre)) kwds))
  3989. ((equal key "TAGS")
  3990. (setq tags (append tags (if tags '("\\n") nil)
  3991. (org-split-string value splitre))))
  3992. ((equal key "COLUMNS")
  3993. (org-set-local 'org-columns-default-format value))
  3994. ((equal key "LINK")
  3995. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3996. (push (cons (match-string 1 value)
  3997. (org-trim (match-string 2 value)))
  3998. links)))
  3999. ((equal key "PRIORITIES")
  4000. (setq prio (org-split-string value " +")))
  4001. ((equal key "PROPERTY")
  4002. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4003. (setq props (org-update-property-plist (match-string 1 value)
  4004. (match-string 2 value)
  4005. props))))
  4006. ((equal key "FILETAGS")
  4007. (when (string-match "\\S-" value)
  4008. (setq ftags
  4009. (append
  4010. ftags
  4011. (apply 'append
  4012. (mapcar (lambda (x) (org-split-string x ":"))
  4013. (org-split-string value)))))))
  4014. ((equal key "DRAWERS")
  4015. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4016. ((equal key "CONSTANTS")
  4017. (setq const (append const (org-split-string value splitre))))
  4018. ((equal key "STARTUP")
  4019. (let ((opts (org-split-string value splitre))
  4020. l var val)
  4021. (while (setq l (pop opts))
  4022. (when (setq l (assoc l org-startup-options))
  4023. (setq var (nth 1 l) val (nth 2 l))
  4024. (if (not (nth 3 l))
  4025. (set (make-local-variable var) val)
  4026. (if (not (listp (symbol-value var)))
  4027. (set (make-local-variable var) nil))
  4028. (set (make-local-variable var) (symbol-value var))
  4029. (add-to-list var val))))))
  4030. ((equal key "ARCHIVE")
  4031. (setq arch value)
  4032. (remove-text-properties 0 (length arch)
  4033. '(face t fontified t) arch))
  4034. ((equal key "LATEX_CLASS")
  4035. (setq beamer-p (equal value "beamer")))
  4036. ((equal key "OPTIONS")
  4037. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4038. (setq scripts (read (match-string 2 value)))))
  4039. ((equal key "SETUPFILE")
  4040. (setq setup-contents (org-file-contents
  4041. (expand-file-name
  4042. (org-remove-double-quotes value))
  4043. 'noerror))
  4044. (if (not ext-setup-or-nil)
  4045. (setq ext-setup-or-nil setup-contents start 0)
  4046. (setq ext-setup-or-nil
  4047. (concat (substring ext-setup-or-nil 0 start)
  4048. "\n" setup-contents "\n"
  4049. (substring ext-setup-or-nil start)))))))
  4050. ;; search for property blocks
  4051. (goto-char (point-min))
  4052. (while (re-search-forward org-block-regexp nil t)
  4053. (when (equal "PROPERTY" (upcase (match-string 1)))
  4054. (setq value (replace-regexp-in-string
  4055. "[\n\r]" " " (match-string 4)))
  4056. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4057. (setq props (org-update-property-plist (match-string 1 value)
  4058. (match-string 2 value)
  4059. props)))))))
  4060. (org-set-local 'org-use-sub-superscripts scripts)
  4061. (when cat
  4062. (org-set-local 'org-category (intern cat))
  4063. (push (cons "CATEGORY" cat) props))
  4064. (when prio
  4065. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4066. (setq prio (mapcar 'string-to-char prio))
  4067. (org-set-local 'org-highest-priority (nth 0 prio))
  4068. (org-set-local 'org-lowest-priority (nth 1 prio))
  4069. (org-set-local 'org-default-priority (nth 2 prio)))
  4070. (and props (org-set-local 'org-file-properties (nreverse props)))
  4071. (and ftags (org-set-local 'org-file-tags
  4072. (mapcar 'org-add-prop-inherited ftags)))
  4073. (and drawers (org-set-local 'org-drawers drawers))
  4074. (and arch (org-set-local 'org-archive-location arch))
  4075. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4076. ;; Process the TODO keywords
  4077. (unless kwds
  4078. ;; Use the global values as if they had been given locally.
  4079. (setq kwds (default-value 'org-todo-keywords))
  4080. (if (stringp (car kwds))
  4081. (setq kwds (list (cons org-todo-interpretation
  4082. (default-value 'org-todo-keywords)))))
  4083. (setq kwds (reverse kwds)))
  4084. (setq kwds (nreverse kwds))
  4085. (let (inter kws kw)
  4086. (while (setq kws (pop kwds))
  4087. (let ((kws (or
  4088. (run-hook-with-args-until-success
  4089. 'org-todo-setup-filter-hook kws)
  4090. kws)))
  4091. (setq inter (pop kws) sep (member "|" kws)
  4092. kws0 (delete "|" (copy-sequence kws))
  4093. kwsa nil
  4094. kws1 (mapcar
  4095. (lambda (x)
  4096. ;; 1 2
  4097. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4098. (progn
  4099. (setq kw (match-string 1 x)
  4100. key (and (match-end 2) (match-string 2 x))
  4101. log (org-extract-log-state-settings x))
  4102. (push (cons kw (and key (string-to-char key))) kwsa)
  4103. (and log (push log org-todo-log-states))
  4104. kw)
  4105. (error "Invalid TODO keyword %s" x)))
  4106. kws0)
  4107. kwsa (if kwsa (append '((:startgroup))
  4108. (nreverse kwsa)
  4109. '((:endgroup))))
  4110. hw (car kws1)
  4111. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4112. tail (list inter hw (car dws) (org-last dws))))
  4113. (add-to-list 'org-todo-heads hw 'append)
  4114. (push kws1 org-todo-sets)
  4115. (setq org-done-keywords (append org-done-keywords dws nil))
  4116. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4117. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4118. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4119. (setq org-todo-sets (nreverse org-todo-sets)
  4120. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4121. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4122. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4123. ;; Process the constants
  4124. (when const
  4125. (let (e cst)
  4126. (while (setq e (pop const))
  4127. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4128. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4129. (setq org-table-formula-constants-local cst)))
  4130. ;; Process the tags.
  4131. (when tags
  4132. (let (e tgs)
  4133. (while (setq e (pop tags))
  4134. (cond
  4135. ((equal e "{") (push '(:startgroup) tgs))
  4136. ((equal e "}") (push '(:endgroup) tgs))
  4137. ((equal e "\\n") (push '(:newline) tgs))
  4138. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4139. (push (cons (match-string 1 e)
  4140. (string-to-char (match-string 2 e)))
  4141. tgs))
  4142. (t (push (list e) tgs))))
  4143. (org-set-local 'org-tag-alist nil)
  4144. (while (setq e (pop tgs))
  4145. (or (and (stringp (car e))
  4146. (assoc (car e) org-tag-alist))
  4147. (push e org-tag-alist)))))
  4148. ;; Compute the regular expressions and other local variables.
  4149. ;; Using `org-outline-regexp-bol' would complicate them much,
  4150. ;; because of the fixed white space at the end of that string.
  4151. (if (not org-done-keywords)
  4152. (setq org-done-keywords (and org-todo-keywords-1
  4153. (list (org-last org-todo-keywords-1)))))
  4154. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4155. (length org-scheduled-string)
  4156. (length org-clock-string)
  4157. (length org-closed-string)))
  4158. org-drawer-regexp
  4159. (concat "^[ \t]*:\\("
  4160. (mapconcat 'regexp-quote org-drawers "\\|")
  4161. "\\):[ \t]*$")
  4162. org-not-done-keywords
  4163. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4164. org-todo-regexp
  4165. (concat "\\("
  4166. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4167. "\\)")
  4168. org-not-done-regexp
  4169. (concat "\\("
  4170. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4171. "\\)")
  4172. org-not-done-heading-regexp
  4173. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4174. org-todo-line-regexp
  4175. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4176. org-complex-heading-regexp
  4177. (concat "^\\(\\*+\\)"
  4178. "\\(?: +" org-todo-regexp "\\)?"
  4179. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4180. "\\(?: +\\(.*?\\)\\)?"
  4181. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4182. "[ \t]*$")
  4183. org-complex-heading-regexp-format
  4184. (concat "^\\(\\*+\\)"
  4185. "\\(?: +" org-todo-regexp "\\)?"
  4186. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4187. "\\(?: +"
  4188. ;; Stats cookies can be stuck to body.
  4189. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4190. "\\(%s\\)"
  4191. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4192. "\\)"
  4193. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4194. "[ \t]*$")
  4195. org-todo-line-tags-regexp
  4196. (concat "^\\(\\*+\\)"
  4197. "\\(?: +" org-todo-regexp "\\)?"
  4198. "\\(?: +\\(.*?\\)\\)?"
  4199. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4200. "[ \t]*$")
  4201. org-deadline-regexp (concat "\\<" org-deadline-string)
  4202. org-deadline-time-regexp
  4203. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4204. org-deadline-line-regexp
  4205. (concat "\\<\\(" org-deadline-string "\\).*")
  4206. org-scheduled-regexp
  4207. (concat "\\<" org-scheduled-string)
  4208. org-scheduled-time-regexp
  4209. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4210. org-closed-time-regexp
  4211. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4212. org-keyword-time-regexp
  4213. (concat "\\<\\(" org-scheduled-string
  4214. "\\|" org-deadline-string
  4215. "\\|" org-closed-string
  4216. "\\|" org-clock-string "\\)"
  4217. " *[[<]\\([^]>]+\\)[]>]")
  4218. org-keyword-time-not-clock-regexp
  4219. (concat "\\<\\(" org-scheduled-string
  4220. "\\|" org-deadline-string
  4221. "\\|" org-closed-string
  4222. "\\)"
  4223. " *[[<]\\([^]>]+\\)[]>]")
  4224. org-maybe-keyword-time-regexp
  4225. (concat "\\(\\<\\(" org-scheduled-string
  4226. "\\|" org-deadline-string
  4227. "\\|" org-closed-string
  4228. "\\|" org-clock-string "\\)\\)?"
  4229. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4230. org-planning-or-clock-line-re
  4231. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4232. "\\|" org-deadline-string
  4233. "\\|" org-closed-string "\\|" org-clock-string
  4234. "\\)\\>\\)")
  4235. org-all-time-keywords
  4236. (mapcar (lambda (w) (substring w 0 -1))
  4237. (list org-scheduled-string org-deadline-string
  4238. org-clock-string org-closed-string))
  4239. )
  4240. (org-compute-latex-and-specials-regexp)
  4241. (org-set-font-lock-defaults))))
  4242. (defun org-file-contents (file &optional noerror)
  4243. "Return the contents of FILE, as a string."
  4244. (if (or (not file)
  4245. (not (file-readable-p file)))
  4246. (if noerror
  4247. (progn
  4248. (message "Cannot read file \"%s\"" file)
  4249. (ding) (sit-for 2)
  4250. "")
  4251. (error "Cannot read file \"%s\"" file))
  4252. (with-temp-buffer
  4253. (insert-file-contents file)
  4254. (buffer-string))))
  4255. (defun org-extract-log-state-settings (x)
  4256. "Extract the log state setting from a TODO keyword string.
  4257. This will extract info from a string like \"WAIT(w@/!)\"."
  4258. (let (kw key log1 log2)
  4259. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4260. (setq kw (match-string 1 x)
  4261. key (and (match-end 2) (match-string 2 x))
  4262. log1 (and (match-end 3) (match-string 3 x))
  4263. log2 (and (match-end 4) (match-string 4 x)))
  4264. (and (or log1 log2)
  4265. (list kw
  4266. (and log1 (if (equal log1 "!") 'time 'note))
  4267. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4268. (defun org-remove-keyword-keys (list)
  4269. "Remove a pair of parenthesis at the end of each string in LIST."
  4270. (mapcar (lambda (x)
  4271. (if (string-match "(.*)$" x)
  4272. (substring x 0 (match-beginning 0))
  4273. x))
  4274. list))
  4275. (defun org-assign-fast-keys (alist)
  4276. "Assign fast keys to a keyword-key alist.
  4277. Respect keys that are already there."
  4278. (let (new e (alt ?0))
  4279. (while (setq e (pop alist))
  4280. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4281. (cdr e)) ;; Key already assigned.
  4282. (push e new)
  4283. (let ((clist (string-to-list (downcase (car e))))
  4284. (used (append new alist)))
  4285. (when (= (car clist) ?@)
  4286. (pop clist))
  4287. (while (and clist (rassoc (car clist) used))
  4288. (pop clist))
  4289. (unless clist
  4290. (while (rassoc alt used)
  4291. (incf alt)))
  4292. (push (cons (car e) (or (car clist) alt)) new))))
  4293. (nreverse new)))
  4294. ;;; Some variables used in various places
  4295. (defvar org-window-configuration nil
  4296. "Used in various places to store a window configuration.")
  4297. (defvar org-selected-window nil
  4298. "Used in various places to store a window configuration.")
  4299. (defvar org-finish-function nil
  4300. "Function to be called when `C-c C-c' is used.
  4301. This is for getting out of special buffers like remember.")
  4302. ;; FIXME: Occasionally check by commenting these, to make sure
  4303. ;; no other functions uses these, forgetting to let-bind them.
  4304. (defvar entry)
  4305. (defvar org-last-state)
  4306. (defvar date)
  4307. ;; Defined somewhere in this file, but used before definition.
  4308. (defvar org-entities) ;; defined in org-entities.el
  4309. (defvar org-struct-menu)
  4310. (defvar org-org-menu)
  4311. (defvar org-tbl-menu)
  4312. ;;;; Define the Org-mode
  4313. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4314. (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"))
  4315. ;; We use a before-change function to check if a table might need
  4316. ;; an update.
  4317. (defvar org-table-may-need-update t
  4318. "Indicates that a table might need an update.
  4319. This variable is set by `org-before-change-function'.
  4320. `org-table-align' sets it back to nil.")
  4321. (defun org-before-change-function (beg end)
  4322. "Every change indicates that a table might need an update."
  4323. (setq org-table-may-need-update t))
  4324. (defvar org-mode-map)
  4325. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4326. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4327. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4328. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4329. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4330. (defvar org-table-buffer-is-an nil)
  4331. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4332. ;; some places -- e.g. see the macro org-with-limited-levels.
  4333. ;;
  4334. ;; In Org buffers, the value of `outline-regexp' is that of
  4335. ;; `org-outline-regexp'. The only function still directly relying on
  4336. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4337. ;; job when `orgstruct-mode' is active.
  4338. (defvar org-outline-regexp "\\*+ "
  4339. "Regexp to match Org headlines.")
  4340. (defconst org-outline-regexp-bol "^\\*+ "
  4341. "Regexp to match Org headlines.
  4342. This is similar to `org-outline-regexp' but additionally makes
  4343. sure that we are at the beginning of the line.")
  4344. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4345. "Matches an headline, putting stars and text into groups.
  4346. Stars are put in group 1 and the trimmed body in group 2.")
  4347. (defvar bidi-paragraph-direction)
  4348. (defvar buffer-face-mode-face)
  4349. ;;;###autoload
  4350. (define-derived-mode org-mode outline-mode "Org"
  4351. "Outline-based notes management and organizer, alias
  4352. \"Carsten's outline-mode for keeping track of everything.\"
  4353. Org-mode develops organizational tasks around a NOTES file which
  4354. contains information about projects as plain text. Org-mode is
  4355. implemented on top of outline-mode, which is ideal to keep the content
  4356. of large files well structured. It supports ToDo items, deadlines and
  4357. time stamps, which magically appear in the diary listing of the Emacs
  4358. calendar. Tables are easily created with a built-in table editor.
  4359. Plain text URL-like links connect to websites, emails (VM), Usenet
  4360. messages (Gnus), BBDB entries, and any files related to the project.
  4361. For printing and sharing of notes, an Org-mode file (or a part of it)
  4362. can be exported as a structured ASCII or HTML file.
  4363. The following commands are available:
  4364. \\{org-mode-map}"
  4365. ;; Get rid of Outline menus, they are not needed
  4366. ;; Need to do this here because define-derived-mode sets up
  4367. ;; the keymap so late. Still, it is a waste to call this each time
  4368. ;; we switch another buffer into org-mode.
  4369. (if (featurep 'xemacs)
  4370. (when (boundp 'outline-mode-menu-heading)
  4371. ;; Assume this is Greg's port, it uses easymenu
  4372. (easy-menu-remove outline-mode-menu-heading)
  4373. (easy-menu-remove outline-mode-menu-show)
  4374. (easy-menu-remove outline-mode-menu-hide))
  4375. (define-key org-mode-map [menu-bar headings] 'undefined)
  4376. (define-key org-mode-map [menu-bar hide] 'undefined)
  4377. (define-key org-mode-map [menu-bar show] 'undefined))
  4378. (org-load-modules-maybe)
  4379. (easy-menu-add org-org-menu)
  4380. (easy-menu-add org-tbl-menu)
  4381. (org-install-agenda-files-menu)
  4382. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4383. (add-to-invisibility-spec '(org-cwidth))
  4384. (add-to-invisibility-spec '(org-hide-block . t))
  4385. (when (featurep 'xemacs)
  4386. (org-set-local 'line-move-ignore-invisible t))
  4387. (org-set-local 'outline-regexp org-outline-regexp)
  4388. (org-set-local 'outline-level 'org-outline-level)
  4389. (setq bidi-paragraph-direction 'left-to-right)
  4390. (when (and org-ellipsis
  4391. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4392. (fboundp 'make-glyph-code))
  4393. (unless org-display-table
  4394. (setq org-display-table (make-display-table)))
  4395. (set-display-table-slot
  4396. org-display-table 4
  4397. (vconcat (mapcar
  4398. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4399. org-ellipsis)))
  4400. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4401. (setq buffer-display-table org-display-table))
  4402. (org-set-regexps-and-options)
  4403. (when (and org-tag-faces (not org-tags-special-faces-re))
  4404. ;; tag faces set outside customize.... force initialization.
  4405. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4406. ;; Calc embedded
  4407. (org-set-local 'calc-embedded-open-mode "# ")
  4408. (modify-syntax-entry ?@ "w")
  4409. (if org-startup-truncated (setq truncate-lines t))
  4410. (org-set-local 'font-lock-unfontify-region-function
  4411. 'org-unfontify-region)
  4412. ;; Activate before-change-function
  4413. (org-set-local 'org-table-may-need-update t)
  4414. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4415. 'local)
  4416. ;; Check for running clock before killing a buffer
  4417. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4418. ;; Paragraphs and auto-filling
  4419. (org-set-autofill-regexps)
  4420. (setq indent-line-function 'org-indent-line-function)
  4421. (org-update-radio-target-regexp)
  4422. ;; Beginning/end of defun
  4423. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4424. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4425. ;; Next error for sparse trees
  4426. (org-set-local 'next-error-function 'org-occur-next-match)
  4427. ;; Make sure dependence stuff works reliably, even for users who set it
  4428. ;; too late :-(
  4429. (if org-enforce-todo-dependencies
  4430. (add-hook 'org-blocker-hook
  4431. 'org-block-todo-from-children-or-siblings-or-parent)
  4432. (remove-hook 'org-blocker-hook
  4433. 'org-block-todo-from-children-or-siblings-or-parent))
  4434. (if org-enforce-todo-checkbox-dependencies
  4435. (add-hook 'org-blocker-hook
  4436. 'org-block-todo-from-checkboxes)
  4437. (remove-hook 'org-blocker-hook
  4438. 'org-block-todo-from-checkboxes))
  4439. ;; Comment characters
  4440. (org-set-local 'comment-start "#")
  4441. (org-set-local 'comment-padding " ")
  4442. ;; Align options lines
  4443. (org-set-local
  4444. 'align-mode-rules-list
  4445. '((org-in-buffer-settings
  4446. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4447. (modes . '(org-mode)))))
  4448. ;; Imenu
  4449. (org-set-local 'imenu-create-index-function
  4450. 'org-imenu-get-tree)
  4451. ;; Make isearch reveal context
  4452. (if (or (featurep 'xemacs)
  4453. (not (boundp 'outline-isearch-open-invisible-function)))
  4454. ;; Emacs 21 and XEmacs make use of the hook
  4455. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4456. ;; Emacs 22 deals with this through a special variable
  4457. (org-set-local 'outline-isearch-open-invisible-function
  4458. (lambda (&rest ignore) (org-show-context 'isearch))))
  4459. ;; Turn on org-beamer-mode?
  4460. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4461. ;; Setup the pcomplete hooks
  4462. (set (make-local-variable 'pcomplete-command-completion-function)
  4463. 'org-pcomplete-initial)
  4464. (set (make-local-variable 'pcomplete-command-name-function)
  4465. 'org-command-at-point)
  4466. (set (make-local-variable 'pcomplete-default-completion-function)
  4467. 'ignore)
  4468. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4469. 'org-parse-arguments)
  4470. (set (make-local-variable 'pcomplete-termination-string) "")
  4471. (when (>= emacs-major-version 23)
  4472. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4473. ;; If empty file that did not turn on org-mode automatically, make it to.
  4474. (if (and org-insert-mode-line-in-empty-file
  4475. (org-called-interactively-p 'any)
  4476. (= (point-min) (point-max)))
  4477. (insert "# -*- mode: org -*-\n\n"))
  4478. (unless org-inhibit-startup
  4479. (when org-startup-align-all-tables
  4480. (let ((bmp (buffer-modified-p)))
  4481. (org-table-map-tables 'org-table-align 'quietly)
  4482. (set-buffer-modified-p bmp)))
  4483. (when org-startup-with-inline-images
  4484. (org-display-inline-images))
  4485. (when org-startup-indented
  4486. (require 'org-indent)
  4487. (org-indent-mode 1))
  4488. (unless org-inhibit-startup-visibility-stuff
  4489. (org-set-startup-visibility))))
  4490. (when (fboundp 'abbrev-table-put)
  4491. (abbrev-table-put org-mode-abbrev-table
  4492. :parents (list text-mode-abbrev-table)))
  4493. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4494. (defun org-current-time ()
  4495. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4496. (if (> (car org-time-stamp-rounding-minutes) 1)
  4497. (let ((r (car org-time-stamp-rounding-minutes))
  4498. (time (decode-time)))
  4499. (apply 'encode-time
  4500. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4501. (nthcdr 2 time))))
  4502. (current-time)))
  4503. (defun org-today ()
  4504. "Return today date, considering `org-extend-today-until'."
  4505. (time-to-days
  4506. (time-subtract (current-time)
  4507. (list 0 (* 3600 org-extend-today-until) 0))))
  4508. ;;;; Font-Lock stuff, including the activators
  4509. (defvar org-mouse-map (make-sparse-keymap))
  4510. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4511. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4512. (when org-mouse-1-follows-link
  4513. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4514. (when org-tab-follows-link
  4515. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4516. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4517. (require 'font-lock)
  4518. (defconst org-non-link-chars "]\t\n\r<>")
  4519. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4520. "shell" "elisp" "doi" "message"))
  4521. (defvar org-link-types-re nil
  4522. "Matches a link that has a url-like prefix like \"http:\"")
  4523. (defvar org-link-re-with-space nil
  4524. "Matches a link with spaces, optional angular brackets around it.")
  4525. (defvar org-link-re-with-space2 nil
  4526. "Matches a link with spaces, optional angular brackets around it.")
  4527. (defvar org-link-re-with-space3 nil
  4528. "Matches a link with spaces, only for internal part in bracket links.")
  4529. (defvar org-angle-link-re nil
  4530. "Matches link with angular brackets, spaces are allowed.")
  4531. (defvar org-plain-link-re nil
  4532. "Matches plain link, without spaces.")
  4533. (defvar org-bracket-link-regexp nil
  4534. "Matches a link in double brackets.")
  4535. (defvar org-bracket-link-analytic-regexp nil
  4536. "Regular expression used to analyze links.
  4537. Here is what the match groups contain after a match:
  4538. 1: http:
  4539. 2: http
  4540. 3: path
  4541. 4: [desc]
  4542. 5: desc")
  4543. (defvar org-bracket-link-analytic-regexp++ nil
  4544. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4545. (defvar org-any-link-re nil
  4546. "Regular expression matching any link.")
  4547. (defcustom org-match-sexp-depth 3
  4548. "Number of stacked braces for sub/superscript matching.
  4549. This has to be set before loading org.el to be effective."
  4550. :group 'org-export-translation ; ??????????????????????????/
  4551. :type 'integer)
  4552. (defun org-create-multibrace-regexp (left right n)
  4553. "Create a regular expression which will match a balanced sexp.
  4554. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4555. as single character strings.
  4556. The regexp returned will match the entire expression including the
  4557. delimiters. It will also define a single group which contains the
  4558. match except for the outermost delimiters. The maximum depth of
  4559. stacked delimiters is N. Escaping delimiters is not possible."
  4560. (let* ((nothing (concat "[^" left right "]*?"))
  4561. (or "\\|")
  4562. (re nothing)
  4563. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4564. (while (> n 1)
  4565. (setq n (1- n)
  4566. re (concat re or next)
  4567. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4568. (concat left "\\(" re "\\)" right)))
  4569. (defvar org-match-substring-regexp
  4570. (concat
  4571. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4572. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4573. "\\|"
  4574. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4575. "\\|"
  4576. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4577. "The regular expression matching a sub- or superscript.")
  4578. (defvar org-match-substring-with-braces-regexp
  4579. (concat
  4580. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4581. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4582. "\\)")
  4583. "The regular expression matching a sub- or superscript, forcing braces.")
  4584. (defun org-make-link-regexps ()
  4585. "Update the link regular expressions.
  4586. This should be called after the variable `org-link-types' has changed."
  4587. (setq org-link-types-re
  4588. (concat
  4589. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4590. org-link-re-with-space
  4591. (concat
  4592. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4593. "\\([^" org-non-link-chars " ]"
  4594. "[^" org-non-link-chars "]*"
  4595. "[^" org-non-link-chars " ]\\)>?")
  4596. org-link-re-with-space2
  4597. (concat
  4598. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4599. "\\([^" org-non-link-chars " ]"
  4600. "[^\t\n\r]*"
  4601. "[^" org-non-link-chars " ]\\)>?")
  4602. org-link-re-with-space3
  4603. (concat
  4604. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4605. "\\([^" org-non-link-chars " ]"
  4606. "[^\t\n\r]*\\)")
  4607. org-angle-link-re
  4608. (concat
  4609. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4610. "\\([^" org-non-link-chars " ]"
  4611. "[^" org-non-link-chars "]*"
  4612. "\\)>")
  4613. org-plain-link-re
  4614. (concat
  4615. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4616. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4617. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4618. org-bracket-link-regexp
  4619. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4620. org-bracket-link-analytic-regexp
  4621. (concat
  4622. "\\[\\["
  4623. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4624. "\\([^]]+\\)"
  4625. "\\]"
  4626. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4627. "\\]")
  4628. org-bracket-link-analytic-regexp++
  4629. (concat
  4630. "\\[\\["
  4631. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4632. "\\([^]]+\\)"
  4633. "\\]"
  4634. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4635. "\\]")
  4636. org-any-link-re
  4637. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4638. org-angle-link-re "\\)\\|\\("
  4639. org-plain-link-re "\\)")))
  4640. (org-make-link-regexps)
  4641. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4642. "Regular expression for fast time stamp matching.")
  4643. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4644. "Regular expression for fast time stamp matching.")
  4645. (defconst org-ts-regexp0
  4646. "\\(\\([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. This one does not require the space after the date, so it can be used
  4649. on a string that terminates immediately after the date.")
  4650. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4651. "Regular expression matching time strings for analysis.")
  4652. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4653. "Regular expression matching time stamps, with groups.")
  4654. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4655. "Regular expression matching time stamps (also [..]), with groups.")
  4656. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4657. "Regular expression matching a time stamp range.")
  4658. (defconst org-tr-regexp-both
  4659. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4660. "Regular expression matching a time stamp range.")
  4661. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4662. org-ts-regexp "\\)?")
  4663. "Regular expression matching a time stamp or time stamp range.")
  4664. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4665. org-ts-regexp-both "\\)?")
  4666. "Regular expression matching a time stamp or time stamp range.
  4667. The time stamps may be either active or inactive.")
  4668. (defvar org-emph-face nil)
  4669. (defun org-do-emphasis-faces (limit)
  4670. "Run through the buffer and add overlays to emphasized strings."
  4671. (let (rtn a)
  4672. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4673. (if (not (= (char-after (match-beginning 3))
  4674. (char-after (match-beginning 4))))
  4675. (progn
  4676. (setq rtn t)
  4677. (setq a (assoc (match-string 3) org-emphasis-alist))
  4678. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4679. 'face
  4680. (nth 1 a))
  4681. (and (nth 4 a)
  4682. (org-remove-flyspell-overlays-in
  4683. (match-beginning 0) (match-end 0)))
  4684. (add-text-properties (match-beginning 2) (match-end 2)
  4685. '(font-lock-multiline t org-emphasis t))
  4686. (when org-hide-emphasis-markers
  4687. (add-text-properties (match-end 4) (match-beginning 5)
  4688. '(invisible org-link))
  4689. (add-text-properties (match-beginning 3) (match-end 3)
  4690. '(invisible org-link)))))
  4691. (backward-char 1))
  4692. rtn))
  4693. (defun org-emphasize (&optional char)
  4694. "Insert or change an emphasis, i.e. a font like bold or italic.
  4695. If there is an active region, change that region to a new emphasis.
  4696. If there is no region, just insert the marker characters and position
  4697. the cursor between them.
  4698. CHAR should be either the marker character, or the first character of the
  4699. HTML tag associated with that emphasis. If CHAR is a space, the means
  4700. to remove the emphasis of the selected region.
  4701. If char is not given (for example in an interactive call) it
  4702. will be prompted for."
  4703. (interactive)
  4704. (let ((eal org-emphasis-alist) e det
  4705. (erc org-emphasis-regexp-components)
  4706. (prompt "")
  4707. (string "") beg end move tag c s)
  4708. (if (org-region-active-p)
  4709. (setq beg (region-beginning) end (region-end)
  4710. string (buffer-substring beg end))
  4711. (setq move t))
  4712. (while (setq e (pop eal))
  4713. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4714. c (aref tag 0))
  4715. (push (cons c (string-to-char (car e))) det)
  4716. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4717. (substring tag 1)))))
  4718. (setq det (nreverse det))
  4719. (unless char
  4720. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4721. (setq char (read-char-exclusive)))
  4722. (setq char (or (cdr (assoc char det)) char))
  4723. (if (equal char ?\ )
  4724. (setq s "" move nil)
  4725. (unless (assoc (char-to-string char) org-emphasis-alist)
  4726. (error "No such emphasis marker: \"%c\"" char))
  4727. (setq s (char-to-string char)))
  4728. (while (and (> (length string) 1)
  4729. (equal (substring string 0 1) (substring string -1))
  4730. (assoc (substring string 0 1) org-emphasis-alist))
  4731. (setq string (substring string 1 -1)))
  4732. (setq string (concat s string s))
  4733. (if beg (delete-region beg end))
  4734. (unless (or (bolp)
  4735. (string-match (concat "[" (nth 0 erc) "\n]")
  4736. (char-to-string (char-before (point)))))
  4737. (insert " "))
  4738. (unless (or (eobp)
  4739. (string-match (concat "[" (nth 1 erc) "\n]")
  4740. (char-to-string (char-after (point)))))
  4741. (insert " ") (backward-char 1))
  4742. (insert string)
  4743. (and move (backward-char 1))))
  4744. (defconst org-nonsticky-props
  4745. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4746. (defsubst org-rear-nonsticky-at (pos)
  4747. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4748. (defun org-activate-plain-links (limit)
  4749. "Run through the buffer and add overlays to links."
  4750. (catch 'exit
  4751. (let (f)
  4752. (if (re-search-forward org-plain-link-re limit t)
  4753. (progn
  4754. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4755. (setq f (get-text-property (match-beginning 0) 'face))
  4756. (if (or (eq f 'org-tag)
  4757. (and (listp f) (memq 'org-tag f)))
  4758. nil
  4759. (add-text-properties (match-beginning 0) (match-end 0)
  4760. (list 'mouse-face 'highlight
  4761. 'face 'org-link
  4762. 'keymap org-mouse-map))
  4763. (org-rear-nonsticky-at (match-end 0)))
  4764. t)))))
  4765. (defun org-activate-code (limit)
  4766. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4767. (progn
  4768. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4769. (remove-text-properties (match-beginning 0) (match-end 0)
  4770. '(display t invisible t intangible t))
  4771. t)))
  4772. (defcustom org-src-fontify-natively nil
  4773. "When non-nil, fontify code in code blocks."
  4774. :type 'boolean
  4775. :version "24.1"
  4776. :group 'org-appearance
  4777. :group 'org-babel)
  4778. (defun org-fontify-meta-lines-and-blocks (limit)
  4779. (condition-case nil
  4780. (org-fontify-meta-lines-and-blocks-1 limit)
  4781. (error (message "org-mode fontification error"))))
  4782. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4783. "Fontify #+ lines and blocks, in the correct ways."
  4784. (let ((case-fold-search t))
  4785. (if (re-search-forward
  4786. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4787. limit t)
  4788. (let ((beg (match-beginning 0))
  4789. (block-start (match-end 0))
  4790. (block-end nil)
  4791. (lang (match-string 7))
  4792. (beg1 (line-beginning-position 2))
  4793. (dc1 (downcase (match-string 2)))
  4794. (dc3 (downcase (match-string 3)))
  4795. end end1 quoting block-type ovl)
  4796. (cond
  4797. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4798. ;; a single line of backend-specific content
  4799. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4800. (remove-text-properties (match-beginning 0) (match-end 0)
  4801. '(display t invisible t intangible t))
  4802. (add-text-properties (match-beginning 1) (match-end 3)
  4803. '(font-lock-fontified t face org-meta-line))
  4804. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4805. '(font-lock-fontified t face org-block))
  4806. ; for backend-specific code
  4807. t)
  4808. ((and (match-end 4) (equal dc3 "begin"))
  4809. ;; Truly a block
  4810. (setq block-type (downcase (match-string 5))
  4811. quoting (member block-type org-protecting-blocks))
  4812. (when (re-search-forward
  4813. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4814. nil t) ;; on purpose, we look further than LIMIT
  4815. (setq end (min (point-max) (match-end 0))
  4816. end1 (min (point-max) (1- (match-beginning 0))))
  4817. (setq block-end (match-beginning 0))
  4818. (when quoting
  4819. (remove-text-properties beg end
  4820. '(display t invisible t intangible t)))
  4821. (add-text-properties
  4822. beg end
  4823. '(font-lock-fontified t font-lock-multiline t))
  4824. (add-text-properties beg beg1 '(face org-meta-line))
  4825. (add-text-properties end1 (min (point-max) (1+ end))
  4826. '(face org-meta-line)) ; for end_src
  4827. (cond
  4828. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4829. (org-src-font-lock-fontify-block lang block-start block-end)
  4830. ;; remove old background overlays
  4831. (mapc (lambda (ov)
  4832. (if (eq (overlay-get ov 'face) 'org-block-background)
  4833. (delete-overlay ov)))
  4834. (overlays-at (/ (+ beg1 block-end) 2)))
  4835. ;; add a background overlay
  4836. (setq ovl (make-overlay beg1 block-end))
  4837. (overlay-put ovl 'face 'org-block-background)
  4838. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4839. (quoting
  4840. (add-text-properties beg1 (min (point-max) (1+ end1))
  4841. '(face org-block))) ; end of source block
  4842. ((not org-fontify-quote-and-verse-blocks))
  4843. ((string= block-type "quote")
  4844. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4845. ((string= block-type "verse")
  4846. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4847. (add-text-properties beg beg1 '(face org-block-begin-line))
  4848. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4849. '(face org-block-end-line))
  4850. t))
  4851. ((member dc1 '("title:" "author:" "email:" "date:"))
  4852. (add-text-properties
  4853. beg (match-end 3)
  4854. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4855. '(font-lock-fontified t invisible t)
  4856. '(font-lock-fontified t face org-document-info-keyword)))
  4857. (add-text-properties
  4858. (match-beginning 6) (match-end 6)
  4859. (if (string-equal dc1 "title:")
  4860. '(font-lock-fontified t face org-document-title)
  4861. '(font-lock-fontified t face org-document-info))))
  4862. ((not (member (char-after beg) '(?\ ?\t)))
  4863. ;; just any other in-buffer setting, but not indented
  4864. (add-text-properties
  4865. beg (match-end 0)
  4866. '(font-lock-fontified t face org-meta-line))
  4867. t)
  4868. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4869. "orgtbl:" "tblfm:" "tblname:" "results:"
  4870. "call:" "header:" "headers:" "name:"))
  4871. (and (match-end 4) (equal dc3 "attr")))
  4872. (add-text-properties
  4873. beg (match-end 0)
  4874. '(font-lock-fontified t face org-meta-line))
  4875. t)
  4876. ((member dc3 '(" " ""))
  4877. (add-text-properties
  4878. beg (match-end 0)
  4879. '(font-lock-fontified t face font-lock-comment-face)))
  4880. (t nil))))))
  4881. (defun org-strip-protective-commas (beg end)
  4882. "Strip protective commas between BEG and END in the current buffer."
  4883. (interactive "r")
  4884. (save-excursion
  4885. (save-match-data
  4886. (goto-char beg)
  4887. (let ((front-line (save-excursion
  4888. (re-search-forward
  4889. "[^[:space:]]" end t)
  4890. (goto-char (match-beginning 0))
  4891. (current-column))))
  4892. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4893. (goto-char (match-beginning 1))
  4894. (when (= (current-column) front-line)
  4895. (replace-match "" nil nil nil 1)))))))
  4896. (defun org-activate-angle-links (limit)
  4897. "Run through the buffer and add overlays to links."
  4898. (if (re-search-forward org-angle-link-re limit t)
  4899. (progn
  4900. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4901. (add-text-properties (match-beginning 0) (match-end 0)
  4902. (list 'mouse-face 'highlight
  4903. 'keymap org-mouse-map))
  4904. (org-rear-nonsticky-at (match-end 0))
  4905. t)))
  4906. (defun org-activate-footnote-links (limit)
  4907. "Run through the buffer and add overlays to footnotes."
  4908. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4909. (when fn
  4910. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4911. (org-remove-flyspell-overlays-in beg end)
  4912. (add-text-properties beg end
  4913. (list 'mouse-face 'highlight
  4914. 'keymap org-mouse-map
  4915. 'help-echo
  4916. (if (= (point-at-bol) beg)
  4917. "Footnote definition"
  4918. "Footnote reference")
  4919. 'font-lock-fontified t
  4920. 'font-lock-multiline t
  4921. 'face 'org-footnote))))))
  4922. (defun org-activate-bracket-links (limit)
  4923. "Run through the buffer and add overlays to bracketed links."
  4924. (if (re-search-forward org-bracket-link-regexp limit t)
  4925. (let* ((help (concat "LINK: "
  4926. (org-match-string-no-properties 1)))
  4927. ;; FIXME: above we should remove the escapes.
  4928. ;; but that requires another match, protecting match data,
  4929. ;; a lot of overhead for font-lock.
  4930. (ip (org-maybe-intangible
  4931. (list 'invisible 'org-link
  4932. 'keymap org-mouse-map 'mouse-face 'highlight
  4933. 'font-lock-multiline t 'help-echo help)))
  4934. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4935. 'font-lock-multiline t 'help-echo help)))
  4936. ;; We need to remove the invisible property here. Table narrowing
  4937. ;; may have made some of this invisible.
  4938. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4939. (remove-text-properties (match-beginning 0) (match-end 0)
  4940. '(invisible nil))
  4941. (if (match-end 3)
  4942. (progn
  4943. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4944. (org-rear-nonsticky-at (match-beginning 3))
  4945. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4946. (org-rear-nonsticky-at (match-end 3))
  4947. (add-text-properties (match-end 3) (match-end 0) ip)
  4948. (org-rear-nonsticky-at (match-end 0)))
  4949. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4950. (org-rear-nonsticky-at (match-beginning 1))
  4951. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4952. (org-rear-nonsticky-at (match-end 1))
  4953. (add-text-properties (match-end 1) (match-end 0) ip)
  4954. (org-rear-nonsticky-at (match-end 0)))
  4955. t)))
  4956. (defun org-activate-dates (limit)
  4957. "Run through the buffer and add overlays to dates."
  4958. (if (re-search-forward org-tsr-regexp-both limit t)
  4959. (progn
  4960. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4961. (add-text-properties (match-beginning 0) (match-end 0)
  4962. (list 'mouse-face 'highlight
  4963. 'keymap org-mouse-map))
  4964. (org-rear-nonsticky-at (match-end 0))
  4965. (when org-display-custom-times
  4966. (if (match-end 3)
  4967. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4968. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4969. t)))
  4970. (defvar org-target-link-regexp nil
  4971. "Regular expression matching radio targets in plain text.")
  4972. (make-variable-buffer-local 'org-target-link-regexp)
  4973. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4974. "Regular expression matching a link target.")
  4975. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4976. "Regular expression matching a radio target.")
  4977. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4978. "Regular expression matching any target.")
  4979. (defun org-activate-target-links (limit)
  4980. "Run through the buffer and add overlays to target matches."
  4981. (when org-target-link-regexp
  4982. (let ((case-fold-search t))
  4983. (if (re-search-forward org-target-link-regexp limit t)
  4984. (progn
  4985. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4986. (add-text-properties (match-beginning 0) (match-end 0)
  4987. (list 'mouse-face 'highlight
  4988. 'keymap org-mouse-map
  4989. 'help-echo "Radio target link"
  4990. 'org-linked-text t))
  4991. (org-rear-nonsticky-at (match-end 0))
  4992. t)))))
  4993. (defun org-update-radio-target-regexp ()
  4994. "Find all radio targets in this file and update the regular expression."
  4995. (interactive)
  4996. (when (memq 'radio org-activate-links)
  4997. (setq org-target-link-regexp
  4998. (org-make-target-link-regexp (org-all-targets 'radio)))
  4999. (org-restart-font-lock)))
  5000. (defun org-hide-wide-columns (limit)
  5001. (let (s e)
  5002. (setq s (text-property-any (point) (or limit (point-max))
  5003. 'org-cwidth t))
  5004. (when s
  5005. (setq e (next-single-property-change s 'org-cwidth))
  5006. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5007. (goto-char e)
  5008. t)))
  5009. (defvar org-latex-and-specials-regexp nil
  5010. "Regular expression for highlighting export special stuff.")
  5011. (defvar org-match-substring-regexp)
  5012. (defvar org-match-substring-with-braces-regexp)
  5013. ;; This should be with the exporter code, but we also use if for font-locking
  5014. (defconst org-export-html-special-string-regexps
  5015. '(("\\\\-" . "&shy;")
  5016. ("---\\([^-]\\)" . "&mdash;\\1")
  5017. ("--\\([^-]\\)" . "&ndash;\\1")
  5018. ("\\.\\.\\." . "&hellip;"))
  5019. "Regular expressions for special string conversion.")
  5020. (defun org-compute-latex-and-specials-regexp ()
  5021. "Compute regular expression for stuff treated specially by exporters."
  5022. (if (not org-highlight-latex-fragments-and-specials)
  5023. (org-set-local 'org-latex-and-specials-regexp nil)
  5024. (require 'org-exp)
  5025. (let*
  5026. ((matchers (plist-get org-format-latex-options :matchers))
  5027. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5028. org-latex-regexps)))
  5029. (org-export-allow-BIND nil)
  5030. (options (org-combine-plists (org-default-export-plist)
  5031. (org-infile-export-plist)))
  5032. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5033. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5034. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5035. (org-export-html-expand (plist-get options :expand-quoted-html))
  5036. (org-export-with-special-strings (plist-get options :special-strings))
  5037. (re-sub
  5038. (cond
  5039. ((equal org-export-with-sub-superscripts '{})
  5040. (list org-match-substring-with-braces-regexp))
  5041. (org-export-with-sub-superscripts
  5042. (list org-match-substring-regexp))
  5043. (t nil)))
  5044. (re-latex
  5045. (if org-export-with-LaTeX-fragments
  5046. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5047. (re-macros
  5048. (if org-export-with-TeX-macros
  5049. (list (concat "\\\\"
  5050. (regexp-opt
  5051. (append
  5052. (delq nil
  5053. (mapcar 'car-safe
  5054. (append org-entities-user
  5055. org-entities)))
  5056. (if (boundp 'org-latex-entities)
  5057. (mapcar (lambda (x)
  5058. (or (car-safe x) x))
  5059. org-latex-entities)
  5060. nil))
  5061. 'words))) ; FIXME
  5062. ))
  5063. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5064. (re-special (if org-export-with-special-strings
  5065. (mapcar (lambda (x) (car x))
  5066. org-export-html-special-string-regexps)))
  5067. (re-rest
  5068. (delq nil
  5069. (list
  5070. (if org-export-html-expand "@<[^>\n]+>")
  5071. ))))
  5072. (org-set-local
  5073. 'org-latex-and-specials-regexp
  5074. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5075. re-rest) "\\|")))))
  5076. (defun org-do-latex-and-special-faces (limit)
  5077. "Run through the buffer and add overlays to links."
  5078. (when org-latex-and-specials-regexp
  5079. (let (rtn d)
  5080. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5081. limit t))
  5082. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5083. 'face))
  5084. '(org-code org-verbatim underline)))
  5085. (progn
  5086. (setq rtn t
  5087. d (cond ((member (char-after (1+ (match-beginning 0)))
  5088. '(?_ ?^)) 1)
  5089. (t 0)))
  5090. (font-lock-prepend-text-property
  5091. (+ d (match-beginning 0)) (match-end 0)
  5092. 'face 'org-latex-and-export-specials)
  5093. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5094. '(font-lock-multiline t)))))
  5095. rtn)))
  5096. (defun org-restart-font-lock ()
  5097. "Restart `font-lock-mode', to force refontification."
  5098. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5099. (font-lock-mode -1)
  5100. (font-lock-mode 1)))
  5101. (defun org-all-targets (&optional radio)
  5102. "Return a list of all targets in this file.
  5103. With optional argument RADIO, only find radio targets."
  5104. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5105. rtn)
  5106. (save-excursion
  5107. (goto-char (point-min))
  5108. (while (re-search-forward re nil t)
  5109. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5110. rtn)))
  5111. (defun org-make-target-link-regexp (targets)
  5112. "Make regular expression matching all strings in TARGETS.
  5113. The regular expression finds the targets also if there is a line break
  5114. between words."
  5115. (and targets
  5116. (concat
  5117. "\\<\\("
  5118. (mapconcat
  5119. (lambda (x)
  5120. (setq x (regexp-quote x))
  5121. (while (string-match " +" x)
  5122. (setq x (replace-match "\\s-+" t t x)))
  5123. x)
  5124. targets
  5125. "\\|")
  5126. "\\)\\>")))
  5127. (defun org-activate-tags (limit)
  5128. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5129. (progn
  5130. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5131. (add-text-properties (match-beginning 1) (match-end 1)
  5132. (list 'mouse-face 'highlight
  5133. 'keymap org-mouse-map))
  5134. (org-rear-nonsticky-at (match-end 1))
  5135. t)))
  5136. (defun org-outline-level ()
  5137. "Compute the outline level of the heading at point.
  5138. This function assumes that the cursor is at the beginning of a line matched
  5139. by `outline-regexp'. Otherwise it returns garbage.
  5140. If this is called at a normal headline, the level is the number of stars.
  5141. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5142. (save-excursion
  5143. (looking-at org-outline-regexp)
  5144. (1- (- (match-end 0) (match-beginning 0)))))
  5145. (defvar org-font-lock-keywords nil)
  5146. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5147. "Regular expression matching a property line.")
  5148. (defvar org-font-lock-hook nil
  5149. "Functions to be called for special font lock stuff.")
  5150. (defvar org-font-lock-set-keywords-hook nil
  5151. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5152. This is called after `org-font-lock-extra-keywords' is defined, but before
  5153. it is installed to be used by font lock. This can be useful if something
  5154. needs to be inserted at a specific position in the font-lock sequence.")
  5155. (defun org-font-lock-hook (limit)
  5156. (run-hook-with-args 'org-font-lock-hook limit))
  5157. (defun org-set-font-lock-defaults ()
  5158. (let* ((em org-fontify-emphasized-text)
  5159. (lk org-activate-links)
  5160. (org-font-lock-extra-keywords
  5161. (list
  5162. ;; Call the hook
  5163. '(org-font-lock-hook)
  5164. ;; Headlines
  5165. `(,(if org-fontify-whole-heading-line
  5166. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5167. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5168. (1 (org-get-level-face 1))
  5169. (2 (org-get-level-face 2))
  5170. (3 (org-get-level-face 3)))
  5171. ;; Table lines
  5172. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5173. (1 'org-table t))
  5174. ;; Table internals
  5175. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5176. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5177. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5178. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5179. ;; Drawers
  5180. (list org-drawer-regexp '(0 'org-special-keyword t))
  5181. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5182. ;; Properties
  5183. (list org-property-re
  5184. '(1 'org-special-keyword t)
  5185. '(3 'org-property-value t))
  5186. ;; Links
  5187. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5188. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5189. (if (memq 'plain lk) '(org-activate-plain-links))
  5190. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5191. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5192. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5193. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5194. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5195. '(org-hide-wide-columns (0 nil append))
  5196. ;; TODO keyword
  5197. (list (format org-heading-keyword-regexp-format
  5198. org-todo-regexp)
  5199. '(2 (org-get-todo-face 2) t))
  5200. ;; DONE
  5201. (if org-fontify-done-headline
  5202. (list (format org-heading-keyword-regexp-format
  5203. (concat
  5204. "\\(?:"
  5205. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5206. "\\)"))
  5207. '(2 'org-headline-done t))
  5208. nil)
  5209. ;; Priorities
  5210. '(org-font-lock-add-priority-faces)
  5211. ;; Tags
  5212. '(org-font-lock-add-tag-faces)
  5213. ;; Special keywords
  5214. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5215. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5216. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5217. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5218. ;; Emphasis
  5219. (if em
  5220. (if (featurep 'xemacs)
  5221. '(org-do-emphasis-faces (0 nil append))
  5222. '(org-do-emphasis-faces)))
  5223. ;; Checkboxes
  5224. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5225. 1 'org-checkbox prepend)
  5226. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5227. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5228. (0 (org-get-checkbox-statistics-face) t)))
  5229. ;; Description list items
  5230. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5231. 1 'bold prepend)
  5232. ;; ARCHIVEd headings
  5233. (list (concat
  5234. org-outline-regexp-bol
  5235. "\\(.*:" org-archive-tag ":.*\\)")
  5236. '(1 'org-archived prepend))
  5237. ;; Specials
  5238. '(org-do-latex-and-special-faces)
  5239. '(org-fontify-entities)
  5240. '(org-raise-scripts)
  5241. ;; Code
  5242. '(org-activate-code (1 'org-code t))
  5243. ;; COMMENT
  5244. (list (format org-heading-keyword-regexp-format
  5245. (concat "\\("
  5246. org-comment-string "\\|" org-quote-string
  5247. "\\)"))
  5248. '(2 'org-special-keyword t))
  5249. '("^#.*" (0 'font-lock-comment-face t))
  5250. ;; Blocks and meta lines
  5251. '(org-fontify-meta-lines-and-blocks)
  5252. )))
  5253. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5254. (run-hooks 'org-font-lock-set-keywords-hook)
  5255. ;; Now set the full font-lock-keywords
  5256. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5257. (org-set-local 'font-lock-defaults
  5258. '(org-font-lock-keywords t nil nil backward-paragraph))
  5259. (kill-local-variable 'font-lock-keywords) nil))
  5260. (defun org-toggle-pretty-entities ()
  5261. "Toggle the composition display of entities as UTF8 characters."
  5262. (interactive)
  5263. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5264. (org-restart-font-lock)
  5265. (if org-pretty-entities
  5266. (message "Entities are displayed as UTF8 characters")
  5267. (save-restriction
  5268. (widen)
  5269. (org-decompose-region (point-min) (point-max))
  5270. (message "Entities are displayed plain"))))
  5271. (defun org-fontify-entities (limit)
  5272. "Find an entity to fontify."
  5273. (let (ee)
  5274. (when org-pretty-entities
  5275. (catch 'match
  5276. (while (re-search-forward
  5277. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5278. limit t)
  5279. (if (and (not (org-in-indented-comment-line))
  5280. (setq ee (org-entity-get (match-string 1)))
  5281. (= (length (nth 6 ee)) 1))
  5282. (progn
  5283. (add-text-properties
  5284. (match-beginning 0) (match-end 1)
  5285. (list 'font-lock-fontified t))
  5286. (compose-region (match-beginning 0) (match-end 1)
  5287. (nth 6 ee) nil)
  5288. (backward-char 1)
  5289. (throw 'match t))))
  5290. nil))))
  5291. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5292. "Fontify string S like in Org-mode."
  5293. (with-temp-buffer
  5294. (insert s)
  5295. (let ((org-odd-levels-only odd-levels))
  5296. (org-mode)
  5297. (font-lock-fontify-buffer)
  5298. (buffer-string))))
  5299. (defvar org-m nil)
  5300. (defvar org-l nil)
  5301. (defvar org-f nil)
  5302. (defun org-get-level-face (n)
  5303. "Get the right face for match N in font-lock matching of headlines."
  5304. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5305. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5306. (if org-cycle-level-faces
  5307. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5308. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5309. (cond
  5310. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5311. ((eq n 2) org-f)
  5312. (t (if org-level-color-stars-only nil org-f))))
  5313. (defun org-get-todo-face (kwd)
  5314. "Get the right face for a TODO keyword KWD.
  5315. If KWD is a number, get the corresponding match group."
  5316. (if (numberp kwd) (setq kwd (match-string kwd)))
  5317. (or (org-face-from-face-or-color
  5318. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5319. (and (member kwd org-done-keywords) 'org-done)
  5320. 'org-todo))
  5321. (defun org-face-from-face-or-color (context inherit face-or-color)
  5322. "Create a face list that inherits INHERIT, but sets the foreground color.
  5323. When FACE-OR-COLOR is not a string, just return it."
  5324. (if (stringp face-or-color)
  5325. (list :inherit inherit
  5326. (cdr (assoc context org-faces-easy-properties))
  5327. face-or-color)
  5328. face-or-color))
  5329. (defun org-font-lock-add-tag-faces (limit)
  5330. "Add the special tag faces."
  5331. (when (and org-tag-faces org-tags-special-faces-re)
  5332. (while (re-search-forward org-tags-special-faces-re limit t)
  5333. (add-text-properties (match-beginning 1) (match-end 1)
  5334. (list 'face (org-get-tag-face 1)
  5335. 'font-lock-fontified t))
  5336. (backward-char 1))))
  5337. (defun org-font-lock-add-priority-faces (limit)
  5338. "Add the special priority faces."
  5339. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5340. (add-text-properties
  5341. (match-beginning 0) (match-end 0)
  5342. (list 'face (or (org-face-from-face-or-color
  5343. 'priority 'org-special-keyword
  5344. (cdr (assoc (char-after (match-beginning 1))
  5345. org-priority-faces)))
  5346. 'org-special-keyword)
  5347. 'font-lock-fontified t))))
  5348. (defun org-get-tag-face (kwd)
  5349. "Get the right face for a TODO keyword KWD.
  5350. If KWD is a number, get the corresponding match group."
  5351. (if (numberp kwd) (setq kwd (match-string kwd)))
  5352. (or (org-face-from-face-or-color
  5353. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5354. 'org-tag))
  5355. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5356. "Remove fontification and activation overlays from links."
  5357. (font-lock-default-unfontify-region beg end)
  5358. (let* ((buffer-undo-list t)
  5359. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5360. (inhibit-modification-hooks t)
  5361. deactivate-mark buffer-file-name buffer-file-truename)
  5362. (org-decompose-region beg end)
  5363. (remove-text-properties beg end
  5364. '(mouse-face t keymap t org-linked-text t
  5365. invisible t intangible t
  5366. org-no-flyspell t org-emphasis t))
  5367. (org-remove-font-lock-display-properties beg end)))
  5368. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5369. ((raise 0.3) (height 0.7))
  5370. ((raise -0.5))
  5371. ((raise 0.5)))
  5372. "Display properties for showing superscripts and subscripts.")
  5373. (defun org-remove-font-lock-display-properties (beg end)
  5374. "Remove specific display properties that have been added by font lock.
  5375. The will remove the raise properties that are used to show superscripts
  5376. and subscripts."
  5377. (let (next prop)
  5378. (while (< beg end)
  5379. (setq next (next-single-property-change beg 'display nil end)
  5380. prop (get-text-property beg 'display))
  5381. (if (member prop org-script-display)
  5382. (put-text-property beg next 'display nil))
  5383. (setq beg next))))
  5384. (defun org-raise-scripts (limit)
  5385. "Add raise properties to sub/superscripts."
  5386. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5387. (if (re-search-forward
  5388. (if (eq org-use-sub-superscripts t)
  5389. org-match-substring-regexp
  5390. org-match-substring-with-braces-regexp)
  5391. limit t)
  5392. (let* ((pos (point)) table-p comment-p
  5393. (mpos (match-beginning 3))
  5394. (emph-p (get-text-property mpos 'org-emphasis))
  5395. (link-p (get-text-property mpos 'mouse-face))
  5396. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5397. (goto-char (point-at-bol))
  5398. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5399. comment-p (org-looking-at-p "[ \t]*#"))
  5400. (goto-char pos)
  5401. ;; FIXME: Should we go back one character here, for a_b^c
  5402. ;; (goto-char (1- pos)) ;????????????????????
  5403. (if (or comment-p emph-p link-p keyw-p)
  5404. t
  5405. (put-text-property (match-beginning 3) (match-end 0)
  5406. 'display
  5407. (if (equal (char-after (match-beginning 2)) ?^)
  5408. (nth (if table-p 3 1) org-script-display)
  5409. (nth (if table-p 2 0) org-script-display)))
  5410. (add-text-properties (match-beginning 2) (match-end 2)
  5411. (list 'invisible t
  5412. 'org-dwidth t 'org-dwidth-n 1))
  5413. (if (and (eq (char-after (match-beginning 3)) ?{)
  5414. (eq (char-before (match-end 3)) ?}))
  5415. (progn
  5416. (add-text-properties
  5417. (match-beginning 3) (1+ (match-beginning 3))
  5418. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5419. (add-text-properties
  5420. (1- (match-end 3)) (match-end 3)
  5421. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5422. t)))))
  5423. ;;;; Visibility cycling, including org-goto and indirect buffer
  5424. ;;; Cycling
  5425. (defvar org-cycle-global-status nil)
  5426. (make-variable-buffer-local 'org-cycle-global-status)
  5427. (defvar org-cycle-subtree-status nil)
  5428. (make-variable-buffer-local 'org-cycle-subtree-status)
  5429. ;;;###autoload
  5430. (defvar org-inlinetask-min-level)
  5431. (defun org-cycle (&optional arg)
  5432. "TAB-action and visibility cycling for Org-mode.
  5433. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5434. is outline visibility cycling, but it also invokes other actions
  5435. in special contexts.
  5436. - When this function is called with a prefix argument, rotate the entire
  5437. buffer through 3 states (global cycling)
  5438. 1. OVERVIEW: Show only top-level headlines.
  5439. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5440. 3. SHOW ALL: Show everything.
  5441. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5442. determined by the variable `org-startup-folded', and by any VISIBILITY
  5443. properties in the buffer.
  5444. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5445. including any drawers.
  5446. - When inside a table, re-align the table and move to the next field.
  5447. - When point is at the beginning of a headline, rotate the subtree started
  5448. by this line through 3 different states (local cycling)
  5449. 1. FOLDED: Only the main headline is shown.
  5450. 2. CHILDREN: The main headline and the direct children are shown.
  5451. From this state, you can move to one of the children
  5452. and zoom in further.
  5453. 3. SUBTREE: Show the entire subtree, including body text.
  5454. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5455. - When point is at the beginning of an empty headline and the variable
  5456. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5457. of the headline by demoting and promoting it to likely levels. This
  5458. speeds up creation document structure by pressing TAB once or several
  5459. times right after creating a new headline.
  5460. - When there is a numeric prefix, go up to a heading with level ARG, do
  5461. a `show-subtree' and return to the previous cursor position. If ARG
  5462. is negative, go up that many levels.
  5463. - When point is not at the beginning of a headline, execute the global
  5464. binding for TAB, which is re-indenting the line. See the option
  5465. `org-cycle-emulate-tab' for details.
  5466. - Special case: if point is at the beginning of the buffer and there is
  5467. no headline in line 1, this function will act as if called with prefix arg
  5468. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5469. But only if also the variable `org-cycle-global-at-bob' is t."
  5470. (interactive "P")
  5471. (org-load-modules-maybe)
  5472. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5473. (and org-cycle-level-after-item/entry-creation
  5474. (or (org-cycle-level)
  5475. (org-cycle-item-indentation))))
  5476. (let* ((limit-level
  5477. (or org-cycle-max-level
  5478. (and (boundp 'org-inlinetask-min-level)
  5479. org-inlinetask-min-level
  5480. (1- org-inlinetask-min-level))))
  5481. (nstars (and limit-level
  5482. (if org-odd-levels-only
  5483. (and limit-level (1- (* limit-level 2)))
  5484. limit-level)))
  5485. (org-outline-regexp
  5486. (if (not (derived-mode-p 'org-mode))
  5487. outline-regexp
  5488. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5489. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5490. (not (looking-at org-outline-regexp))))
  5491. (org-cycle-hook
  5492. (if bob-special
  5493. (delq 'org-optimize-window-after-visibility-change
  5494. (copy-sequence org-cycle-hook))
  5495. org-cycle-hook))
  5496. (pos (point)))
  5497. (if (or bob-special (equal arg '(4)))
  5498. ;; special case: use global cycling
  5499. (setq arg t))
  5500. (cond
  5501. ((equal arg '(16))
  5502. (setq last-command 'dummy)
  5503. (org-set-startup-visibility)
  5504. (message "Startup visibility, plus VISIBILITY properties"))
  5505. ((equal arg '(64))
  5506. (show-all)
  5507. (message "Entire buffer visible, including drawers"))
  5508. ;; Table: enter it or move to the next field.
  5509. ((org-at-table-p 'any)
  5510. (if (org-at-table.el-p)
  5511. (message "Use C-c ' to edit table.el tables")
  5512. (if arg (org-table-edit-field t)
  5513. (org-table-justify-field-maybe)
  5514. (call-interactively 'org-table-next-field))))
  5515. ((run-hook-with-args-until-success
  5516. 'org-tab-after-check-for-table-hook))
  5517. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5518. ((eq arg t) (org-cycle-internal-global))
  5519. ;; Drawers: delegate to `org-flag-drawer'.
  5520. ((and org-drawers org-drawer-regexp
  5521. (save-excursion
  5522. (beginning-of-line 1)
  5523. (looking-at org-drawer-regexp)))
  5524. (org-flag-drawer ; toggle block visibility
  5525. (not (get-char-property (match-end 0) 'invisible))))
  5526. ;; Show-subtree, ARG levels up from here.
  5527. ((integerp arg)
  5528. (save-excursion
  5529. (org-back-to-heading)
  5530. (outline-up-heading (if (< arg 0) (- arg)
  5531. (- (funcall outline-level) arg)))
  5532. (org-show-subtree)))
  5533. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5534. ((and (featurep 'org-inlinetask)
  5535. (org-inlinetask-at-task-p)
  5536. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5537. (org-inlinetask-toggle-visibility))
  5538. ((org-try-cdlatex-tab))
  5539. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5540. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5541. (save-excursion (beginning-of-line 1)
  5542. (looking-at org-outline-regexp)))
  5543. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5544. (org-cycle-internal-local))
  5545. ;; From there: TAB emulation and template completion.
  5546. (buffer-read-only (org-back-to-heading))
  5547. ((run-hook-with-args-until-success
  5548. 'org-tab-after-check-for-cycling-hook))
  5549. ((org-try-structure-completion))
  5550. ((run-hook-with-args-until-success
  5551. 'org-tab-before-tab-emulation-hook))
  5552. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5553. (or (not (bolp))
  5554. (not (looking-at org-outline-regexp))))
  5555. (call-interactively (global-key-binding "\t")))
  5556. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5557. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5558. (or (and (eq org-cycle-emulate-tab 'white)
  5559. (= (match-end 0) (point-at-eol)))
  5560. (and (eq org-cycle-emulate-tab 'whitestart)
  5561. (>= (match-end 0) pos))))
  5562. t
  5563. (eq org-cycle-emulate-tab t))
  5564. (call-interactively (global-key-binding "\t")))
  5565. (t (save-excursion
  5566. (org-back-to-heading)
  5567. (org-cycle)))))))
  5568. (defun org-cycle-internal-global ()
  5569. "Do the global cycling action."
  5570. ;; Hack to avoid display of messages for .org attachments in Gnus
  5571. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5572. (cond
  5573. ((and (eq last-command this-command)
  5574. (eq org-cycle-global-status 'overview))
  5575. ;; We just created the overview - now do table of contents
  5576. ;; This can be slow in very large buffers, so indicate action
  5577. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5578. (unless ga (message "CONTENTS..."))
  5579. (org-content)
  5580. (unless ga (message "CONTENTS...done"))
  5581. (setq org-cycle-global-status 'contents)
  5582. (run-hook-with-args 'org-cycle-hook 'contents))
  5583. ((and (eq last-command this-command)
  5584. (eq org-cycle-global-status 'contents))
  5585. ;; We just showed the table of contents - now show everything
  5586. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5587. (show-all)
  5588. (unless ga (message "SHOW ALL"))
  5589. (setq org-cycle-global-status 'all)
  5590. (run-hook-with-args 'org-cycle-hook 'all))
  5591. (t
  5592. ;; Default action: go to overview
  5593. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5594. (org-overview)
  5595. (unless ga (message "OVERVIEW"))
  5596. (setq org-cycle-global-status 'overview)
  5597. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5598. (defun org-cycle-internal-local ()
  5599. "Do the local cycling action."
  5600. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5601. ;; First, determine end of headline (EOH), end of subtree or item
  5602. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5603. (save-excursion
  5604. (if (org-at-item-p)
  5605. (progn
  5606. (beginning-of-line)
  5607. (setq struct (org-list-struct))
  5608. (setq eoh (point-at-eol))
  5609. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5610. (setq has-children (org-list-has-child-p (point) struct)))
  5611. (org-back-to-heading)
  5612. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5613. (setq eos (save-excursion
  5614. (org-end-of-subtree t)
  5615. (unless (eobp)
  5616. (skip-chars-forward " \t\n"))
  5617. (if (eobp) (point) (1- (point)))))
  5618. (setq has-children
  5619. (or (save-excursion
  5620. (let ((level (funcall outline-level)))
  5621. (outline-next-heading)
  5622. (and (org-at-heading-p t)
  5623. (> (funcall outline-level) level))))
  5624. (save-excursion
  5625. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5626. ;; Determine end invisible part of buffer (EOL)
  5627. (beginning-of-line 2)
  5628. ;; XEmacs doesn't have `next-single-char-property-change'
  5629. (if (featurep 'xemacs)
  5630. (while (and (not (eobp)) ;; this is like `next-line'
  5631. (get-char-property (1- (point)) 'invisible))
  5632. (beginning-of-line 2))
  5633. (while (and (not (eobp)) ;; this is like `next-line'
  5634. (get-char-property (1- (point)) 'invisible))
  5635. (goto-char (next-single-char-property-change (point) 'invisible))
  5636. (and (eolp) (beginning-of-line 2))))
  5637. (setq eol (point)))
  5638. ;; Find out what to do next and set `this-command'
  5639. (cond
  5640. ((= eos eoh)
  5641. ;; Nothing is hidden behind this heading
  5642. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5643. (message "EMPTY ENTRY")
  5644. (setq org-cycle-subtree-status nil)
  5645. (save-excursion
  5646. (goto-char eos)
  5647. (outline-next-heading)
  5648. (if (outline-invisible-p) (org-flag-heading nil))))
  5649. ((and (or (>= eol eos)
  5650. (not (string-match "\\S-" (buffer-substring eol eos))))
  5651. (or has-children
  5652. (not (setq children-skipped
  5653. org-cycle-skip-children-state-if-no-children))))
  5654. ;; Entire subtree is hidden in one line: children view
  5655. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5656. (if (org-at-item-p)
  5657. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5658. (org-show-entry)
  5659. (org-with-limited-levels (show-children))
  5660. ;; FIXME: This slows down the func way too much.
  5661. ;; How keep drawers hidden in subtree anyway?
  5662. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5663. ;; (org-cycle-hide-drawers 'subtree))
  5664. ;; Fold every list in subtree to top-level items.
  5665. (when (eq org-cycle-include-plain-lists 'integrate)
  5666. (save-excursion
  5667. (org-back-to-heading)
  5668. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5669. (beginning-of-line 1)
  5670. (let* ((struct (org-list-struct))
  5671. (prevs (org-list-prevs-alist struct))
  5672. (end (org-list-get-bottom-point struct)))
  5673. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5674. (org-list-get-all-items (point) struct prevs))
  5675. (goto-char end))))))
  5676. (message "CHILDREN")
  5677. (save-excursion
  5678. (goto-char eos)
  5679. (outline-next-heading)
  5680. (if (outline-invisible-p) (org-flag-heading nil)))
  5681. (setq org-cycle-subtree-status 'children)
  5682. (run-hook-with-args 'org-cycle-hook 'children))
  5683. ((or children-skipped
  5684. (and (eq last-command this-command)
  5685. (eq org-cycle-subtree-status 'children)))
  5686. ;; We just showed the children, or no children are there,
  5687. ;; now show everything.
  5688. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5689. (outline-flag-region eoh eos nil)
  5690. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5691. (setq org-cycle-subtree-status 'subtree)
  5692. (run-hook-with-args 'org-cycle-hook 'subtree))
  5693. (t
  5694. ;; Default action: hide the subtree.
  5695. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5696. (outline-flag-region eoh eos t)
  5697. (message "FOLDED")
  5698. (setq org-cycle-subtree-status 'folded)
  5699. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5700. ;;;###autoload
  5701. (defun org-global-cycle (&optional arg)
  5702. "Cycle the global visibility. For details see `org-cycle'.
  5703. With \\[universal-argument] prefix arg, switch to startup visibility.
  5704. With a numeric prefix, show all headlines up to that level."
  5705. (interactive "P")
  5706. (let ((org-cycle-include-plain-lists
  5707. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5708. (cond
  5709. ((integerp arg)
  5710. (show-all)
  5711. (hide-sublevels arg)
  5712. (setq org-cycle-global-status 'contents))
  5713. ((equal arg '(4))
  5714. (org-set-startup-visibility)
  5715. (message "Startup visibility, plus VISIBILITY properties."))
  5716. (t
  5717. (org-cycle '(4))))))
  5718. (defun org-set-startup-visibility ()
  5719. "Set the visibility required by startup options and properties."
  5720. (cond
  5721. ((eq org-startup-folded t)
  5722. (org-cycle '(4)))
  5723. ((eq org-startup-folded 'content)
  5724. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5725. (org-cycle '(4)) (org-cycle '(4)))))
  5726. (unless (eq org-startup-folded 'showeverything)
  5727. (if org-hide-block-startup (org-hide-block-all))
  5728. (org-set-visibility-according-to-property 'no-cleanup)
  5729. (org-cycle-hide-archived-subtrees 'all)
  5730. (org-cycle-hide-drawers 'all)
  5731. (org-cycle-show-empty-lines t)))
  5732. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5733. "Switch subtree visibilities according to :VISIBILITY: property."
  5734. (interactive)
  5735. (let (org-show-entry-below state)
  5736. (save-excursion
  5737. (goto-char (point-min))
  5738. (while (re-search-forward
  5739. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5740. nil t)
  5741. (setq state (match-string 1))
  5742. (save-excursion
  5743. (org-back-to-heading t)
  5744. (hide-subtree)
  5745. (org-reveal)
  5746. (cond
  5747. ((equal state '("fold" "folded"))
  5748. (hide-subtree))
  5749. ((equal state "children")
  5750. (org-show-hidden-entry)
  5751. (show-children))
  5752. ((equal state "content")
  5753. (save-excursion
  5754. (save-restriction
  5755. (org-narrow-to-subtree)
  5756. (org-content))))
  5757. ((member state '("all" "showall"))
  5758. (show-subtree)))))
  5759. (unless no-cleanup
  5760. (org-cycle-hide-archived-subtrees 'all)
  5761. (org-cycle-hide-drawers 'all)
  5762. (org-cycle-show-empty-lines 'all)))))
  5763. ;; This function uses outline-regexp instead of the more fundamental
  5764. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5765. ;; buffers, where outline-regexp is needed.
  5766. (defun org-overview ()
  5767. "Switch to overview mode, showing only top-level headlines.
  5768. Really, this shows all headlines with level equal or greater than the level
  5769. of the first headline in the buffer. This is important, because if the
  5770. first headline is not level one, then (hide-sublevels 1) gives confusing
  5771. results."
  5772. (interactive)
  5773. (let ((level (save-excursion
  5774. (goto-char (point-min))
  5775. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5776. (progn
  5777. (goto-char (match-beginning 0))
  5778. (funcall outline-level))))))
  5779. (and level (hide-sublevels level))))
  5780. (defun org-content (&optional arg)
  5781. "Show all headlines in the buffer, like a table of contents.
  5782. With numerical argument N, show content up to level N."
  5783. (interactive "P")
  5784. (save-excursion
  5785. ;; Visit all headings and show their offspring
  5786. (and (integerp arg) (org-overview))
  5787. (goto-char (point-max))
  5788. (catch 'exit
  5789. (while (and (progn (condition-case nil
  5790. (outline-previous-visible-heading 1)
  5791. (error (goto-char (point-min))))
  5792. t)
  5793. (looking-at org-outline-regexp))
  5794. (if (integerp arg)
  5795. (show-children (1- arg))
  5796. (show-branches))
  5797. (if (bobp) (throw 'exit nil))))))
  5798. (defun org-optimize-window-after-visibility-change (state)
  5799. "Adjust the window after a change in outline visibility.
  5800. This function is the default value of the hook `org-cycle-hook'."
  5801. (when (get-buffer-window (current-buffer))
  5802. (cond
  5803. ((eq state 'content) nil)
  5804. ((eq state 'all) nil)
  5805. ((eq state 'folded) nil)
  5806. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5807. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5808. (defun org-remove-empty-overlays-at (pos)
  5809. "Remove outline overlays that do not contain non-white stuff."
  5810. (mapc
  5811. (lambda (o)
  5812. (and (eq 'outline (overlay-get o 'invisible))
  5813. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5814. (overlay-end o))))
  5815. (delete-overlay o)))
  5816. (overlays-at pos)))
  5817. (defun org-clean-visibility-after-subtree-move ()
  5818. "Fix visibility issues after moving a subtree."
  5819. ;; First, find a reasonable region to look at:
  5820. ;; Start two siblings above, end three below
  5821. (let* ((beg (save-excursion
  5822. (and (org-get-last-sibling)
  5823. (org-get-last-sibling))
  5824. (point)))
  5825. (end (save-excursion
  5826. (and (org-get-next-sibling)
  5827. (org-get-next-sibling)
  5828. (org-get-next-sibling))
  5829. (if (org-at-heading-p)
  5830. (point-at-eol)
  5831. (point))))
  5832. (level (looking-at "\\*+"))
  5833. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5834. (save-excursion
  5835. (save-restriction
  5836. (narrow-to-region beg end)
  5837. (when re
  5838. ;; Properly fold already folded siblings
  5839. (goto-char (point-min))
  5840. (while (re-search-forward re nil t)
  5841. (if (and (not (outline-invisible-p))
  5842. (save-excursion
  5843. (goto-char (point-at-eol)) (outline-invisible-p)))
  5844. (hide-entry))))
  5845. (org-cycle-show-empty-lines 'overview)
  5846. (org-cycle-hide-drawers 'overview)))))
  5847. (defun org-cycle-show-empty-lines (state)
  5848. "Show empty lines above all visible headlines.
  5849. The region to be covered depends on STATE when called through
  5850. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5851. entire buffer covered. Note that an empty line is only shown if there
  5852. are at least `org-cycle-separator-lines' empty lines before the headline."
  5853. (when (not (= org-cycle-separator-lines 0))
  5854. (save-excursion
  5855. (let* ((n (abs org-cycle-separator-lines))
  5856. (re (cond
  5857. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5858. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5859. (t (let ((ns (number-to-string (- n 2))))
  5860. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5861. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5862. beg end b e)
  5863. (cond
  5864. ((memq state '(overview contents t))
  5865. (setq beg (point-min) end (point-max)))
  5866. ((memq state '(children folded))
  5867. (setq beg (point) end (progn (org-end-of-subtree t t)
  5868. (beginning-of-line 2)
  5869. (point)))))
  5870. (when beg
  5871. (goto-char beg)
  5872. (while (re-search-forward re end t)
  5873. (unless (get-char-property (match-end 1) 'invisible)
  5874. (setq e (match-end 1))
  5875. (if (< org-cycle-separator-lines 0)
  5876. (setq b (save-excursion
  5877. (goto-char (match-beginning 0))
  5878. (org-back-over-empty-lines)
  5879. (if (save-excursion
  5880. (goto-char (max (point-min) (1- (point))))
  5881. (org-at-heading-p))
  5882. (1- (point))
  5883. (point))))
  5884. (setq b (match-beginning 1)))
  5885. (outline-flag-region b e nil)))))))
  5886. ;; Never hide empty lines at the end of the file.
  5887. (save-excursion
  5888. (goto-char (point-max))
  5889. (outline-previous-heading)
  5890. (outline-end-of-heading)
  5891. (if (and (looking-at "[ \t\n]+")
  5892. (= (match-end 0) (point-max)))
  5893. (outline-flag-region (point) (match-end 0) nil))))
  5894. (defun org-show-empty-lines-in-parent ()
  5895. "Move to the parent and re-show empty lines before visible headlines."
  5896. (save-excursion
  5897. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5898. (org-cycle-show-empty-lines context))))
  5899. (defun org-files-list ()
  5900. "Return `org-agenda-files' list, plus all open org-mode files.
  5901. This is useful for operations that need to scan all of a user's
  5902. open and agenda-wise Org files."
  5903. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5904. (dolist (buf (buffer-list))
  5905. (with-current-buffer buf
  5906. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5907. (let ((file (expand-file-name (buffer-file-name))))
  5908. (unless (member file files)
  5909. (push file files))))))
  5910. files))
  5911. (defsubst org-entry-beginning-position ()
  5912. "Return the beginning position of the current entry."
  5913. (save-excursion (outline-back-to-heading t) (point)))
  5914. (defsubst org-entry-end-position ()
  5915. "Return the end position of the current entry."
  5916. (save-excursion (outline-next-heading) (point)))
  5917. (defun org-cycle-hide-drawers (state)
  5918. "Re-hide all drawers after a visibility state change."
  5919. (when (and (derived-mode-p 'org-mode)
  5920. (not (memq state '(overview folded contents))))
  5921. (save-excursion
  5922. (let* ((globalp (memq state '(contents all)))
  5923. (beg (if globalp (point-min) (point)))
  5924. (end (if globalp (point-max)
  5925. (if (eq state 'children)
  5926. (save-excursion (outline-next-heading) (point))
  5927. (org-end-of-subtree t)))))
  5928. (goto-char beg)
  5929. (while (re-search-forward org-drawer-regexp end t)
  5930. (org-flag-drawer t))))))
  5931. (defun org-flag-drawer (flag)
  5932. (save-excursion
  5933. (beginning-of-line 1)
  5934. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5935. (let ((b (match-end 0)))
  5936. (if (re-search-forward
  5937. "^[ \t]*:END:"
  5938. (save-excursion (outline-next-heading) (point)) t)
  5939. (outline-flag-region b (point-at-eol) flag)
  5940. (error ":END: line missing at position %s" b))))))
  5941. (defun org-subtree-end-visible-p ()
  5942. "Is the end of the current subtree visible?"
  5943. (pos-visible-in-window-p
  5944. (save-excursion (org-end-of-subtree t) (point))))
  5945. (defun org-first-headline-recenter (&optional N)
  5946. "Move cursor to the first headline and recenter the headline.
  5947. Optional argument N means put the headline into the Nth line of the window."
  5948. (goto-char (point-min))
  5949. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5950. (beginning-of-line)
  5951. (recenter (prefix-numeric-value N))))
  5952. ;;; Saving and restoring visibility
  5953. (defun org-outline-overlay-data (&optional use-markers)
  5954. "Return a list of the locations of all outline overlays.
  5955. These are overlays with the `invisible' property value `outline'.
  5956. The return value is a list of cons cells, with start and stop
  5957. positions for each overlay.
  5958. If USE-MARKERS is set, return the positions as markers."
  5959. (let (beg end)
  5960. (save-excursion
  5961. (save-restriction
  5962. (widen)
  5963. (delq nil
  5964. (mapcar (lambda (o)
  5965. (when (eq (overlay-get o 'invisible) 'outline)
  5966. (setq beg (overlay-start o)
  5967. end (overlay-end o))
  5968. (and beg end (> end beg)
  5969. (if use-markers
  5970. (cons (move-marker (make-marker) beg)
  5971. (move-marker (make-marker) end))
  5972. (cons beg end)))))
  5973. (overlays-in (point-min) (point-max))))))))
  5974. (defun org-set-outline-overlay-data (data)
  5975. "Create visibility overlays for all positions in DATA.
  5976. DATA should have been made by `org-outline-overlay-data'."
  5977. (let (o)
  5978. (save-excursion
  5979. (save-restriction
  5980. (widen)
  5981. (show-all)
  5982. (mapc (lambda (c)
  5983. (outline-flag-region (car c) (cdr c) t))
  5984. data)))))
  5985. ;;; Folding of blocks
  5986. (defvar org-hide-block-overlays nil
  5987. "Overlays hiding blocks.")
  5988. (make-variable-buffer-local 'org-hide-block-overlays)
  5989. (defun org-block-map (function &optional start end)
  5990. "Call FUNCTION at the head of all source blocks in the current buffer.
  5991. Optional arguments START and END can be used to limit the range."
  5992. (let ((start (or start (point-min)))
  5993. (end (or end (point-max))))
  5994. (save-excursion
  5995. (goto-char start)
  5996. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5997. (save-excursion
  5998. (save-match-data
  5999. (goto-char (match-beginning 0))
  6000. (funcall function)))))))
  6001. (defun org-hide-block-toggle-all ()
  6002. "Toggle the visibility of all blocks in the current buffer."
  6003. (org-block-map #'org-hide-block-toggle))
  6004. (defun org-hide-block-all ()
  6005. "Fold all blocks in the current buffer."
  6006. (interactive)
  6007. (org-show-block-all)
  6008. (org-block-map #'org-hide-block-toggle-maybe))
  6009. (defun org-show-block-all ()
  6010. "Unfold all blocks in the current buffer."
  6011. (interactive)
  6012. (mapc 'delete-overlay org-hide-block-overlays)
  6013. (setq org-hide-block-overlays nil))
  6014. (defun org-hide-block-toggle-maybe ()
  6015. "Toggle visibility of block at point."
  6016. (interactive)
  6017. (let ((case-fold-search t))
  6018. (if (save-excursion
  6019. (beginning-of-line 1)
  6020. (looking-at org-block-regexp))
  6021. (progn (org-hide-block-toggle)
  6022. t) ;; to signal that we took action
  6023. nil))) ;; to signal that we did not
  6024. (defun org-hide-block-toggle (&optional force)
  6025. "Toggle the visibility of the current block."
  6026. (interactive)
  6027. (save-excursion
  6028. (beginning-of-line)
  6029. (if (re-search-forward org-block-regexp nil t)
  6030. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6031. (end (match-end 0)) ;; end of entire body
  6032. ov)
  6033. (if (memq t (mapcar (lambda (overlay)
  6034. (eq (overlay-get overlay 'invisible)
  6035. 'org-hide-block))
  6036. (overlays-at start)))
  6037. (if (or (not force) (eq force 'off))
  6038. (mapc (lambda (ov)
  6039. (when (member ov org-hide-block-overlays)
  6040. (setq org-hide-block-overlays
  6041. (delq ov org-hide-block-overlays)))
  6042. (when (eq (overlay-get ov 'invisible)
  6043. 'org-hide-block)
  6044. (delete-overlay ov)))
  6045. (overlays-at start)))
  6046. (setq ov (make-overlay start end))
  6047. (overlay-put ov 'invisible 'org-hide-block)
  6048. ;; make the block accessible to isearch
  6049. (overlay-put
  6050. ov 'isearch-open-invisible
  6051. (lambda (ov)
  6052. (when (member ov org-hide-block-overlays)
  6053. (setq org-hide-block-overlays
  6054. (delq ov org-hide-block-overlays)))
  6055. (when (eq (overlay-get ov 'invisible)
  6056. 'org-hide-block)
  6057. (delete-overlay ov))))
  6058. (push ov org-hide-block-overlays)))
  6059. (error "Not looking at a source block"))))
  6060. ;; org-tab-after-check-for-cycling-hook
  6061. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6062. ;; Remove overlays when changing major mode
  6063. (add-hook 'org-mode-hook
  6064. (lambda () (org-add-hook 'change-major-mode-hook
  6065. 'org-show-block-all 'append 'local)))
  6066. ;;; Org-goto
  6067. (defvar org-goto-window-configuration nil)
  6068. (defvar org-goto-marker nil)
  6069. (defvar org-goto-map
  6070. (let ((map (make-sparse-keymap)))
  6071. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6072. (while (setq cmd (pop cmds))
  6073. (substitute-key-definition cmd cmd map global-map)))
  6074. (suppress-keymap map)
  6075. (org-defkey map "\C-m" 'org-goto-ret)
  6076. (org-defkey map [(return)] 'org-goto-ret)
  6077. (org-defkey map [(left)] 'org-goto-left)
  6078. (org-defkey map [(right)] 'org-goto-right)
  6079. (org-defkey map [(control ?g)] 'org-goto-quit)
  6080. (org-defkey map "\C-i" 'org-cycle)
  6081. (org-defkey map [(tab)] 'org-cycle)
  6082. (org-defkey map [(down)] 'outline-next-visible-heading)
  6083. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6084. (if org-goto-auto-isearch
  6085. (if (fboundp 'define-key-after)
  6086. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6087. nil)
  6088. (org-defkey map "q" 'org-goto-quit)
  6089. (org-defkey map "n" 'outline-next-visible-heading)
  6090. (org-defkey map "p" 'outline-previous-visible-heading)
  6091. (org-defkey map "f" 'outline-forward-same-level)
  6092. (org-defkey map "b" 'outline-backward-same-level)
  6093. (org-defkey map "u" 'outline-up-heading))
  6094. (org-defkey map "/" 'org-occur)
  6095. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6096. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6097. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6098. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6099. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6100. map))
  6101. (defconst org-goto-help
  6102. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6103. RET=jump to location [Q]uit and return to previous location
  6104. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6105. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6106. ;; FIXME: Docstring does not mention both interfaces
  6107. (defun org-goto (&optional alternative-interface)
  6108. "Look up a different location in the current file, keeping current visibility.
  6109. When you want look-up or go to a different location in a document, the
  6110. fastest way is often to fold the entire buffer and then dive into the tree.
  6111. This method has the disadvantage, that the previous location will be folded,
  6112. which may not be what you want.
  6113. This command works around this by showing a copy of the current buffer
  6114. in an indirect buffer, in overview mode. You can dive into the tree in
  6115. that copy, use org-occur and incremental search to find a location.
  6116. When pressing RET or `Q', the command returns to the original buffer in
  6117. which the visibility is still unchanged. After RET it will also jump to
  6118. the location selected in the indirect buffer and expose the headline
  6119. hierarchy above."
  6120. (interactive "P")
  6121. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6122. (org-refile-use-outline-path t)
  6123. (org-refile-target-verify-function nil)
  6124. (interface
  6125. (if (not alternative-interface)
  6126. org-goto-interface
  6127. (if (eq org-goto-interface 'outline)
  6128. 'outline-path-completion
  6129. 'outline)))
  6130. (org-goto-start-pos (point))
  6131. (selected-point
  6132. (if (eq interface 'outline)
  6133. (car (org-get-location (current-buffer) org-goto-help))
  6134. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6135. (org-refile-check-position pa)
  6136. (nth 3 pa)))))
  6137. (if selected-point
  6138. (progn
  6139. (org-mark-ring-push org-goto-start-pos)
  6140. (goto-char selected-point)
  6141. (if (or (outline-invisible-p) (org-invisible-p2))
  6142. (org-show-context 'org-goto)))
  6143. (message "Quit"))))
  6144. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6145. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6146. (defvar org-goto-local-auto-isearch-map) ; defined below
  6147. (defun org-get-location (buf help)
  6148. "Let the user select a location in the Org-mode buffer BUF.
  6149. This function uses a recursive edit. It returns the selected position
  6150. or nil."
  6151. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6152. (isearch-hide-immediately nil)
  6153. (isearch-search-fun-function
  6154. (lambda () 'org-goto-local-search-headings))
  6155. (org-goto-selected-point org-goto-exit-command)
  6156. (pop-up-frames nil)
  6157. (special-display-buffer-names nil)
  6158. (special-display-regexps nil)
  6159. (special-display-function nil))
  6160. (save-excursion
  6161. (save-window-excursion
  6162. (delete-other-windows)
  6163. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6164. (org-pop-to-buffer-same-window
  6165. (condition-case nil
  6166. (make-indirect-buffer (current-buffer) "*org-goto*")
  6167. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6168. (with-output-to-temp-buffer "*Help*"
  6169. (princ help))
  6170. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6171. (setq buffer-read-only nil)
  6172. (let ((org-startup-truncated t)
  6173. (org-startup-folded nil)
  6174. (org-startup-align-all-tables nil))
  6175. (org-mode)
  6176. (org-overview))
  6177. (setq buffer-read-only t)
  6178. (if (and (boundp 'org-goto-start-pos)
  6179. (integer-or-marker-p org-goto-start-pos))
  6180. (let ((org-show-hierarchy-above t)
  6181. (org-show-siblings t)
  6182. (org-show-following-heading t))
  6183. (goto-char org-goto-start-pos)
  6184. (and (outline-invisible-p) (org-show-context)))
  6185. (goto-char (point-min)))
  6186. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6187. (message "Select location and press RET")
  6188. (use-local-map org-goto-map)
  6189. (recursive-edit)
  6190. ))
  6191. (kill-buffer "*org-goto*")
  6192. (cons org-goto-selected-point org-goto-exit-command)))
  6193. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6194. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6195. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6196. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6197. (defun org-goto-local-search-headings (string bound noerror)
  6198. "Search and make sure that any matches are in headlines."
  6199. (catch 'return
  6200. (while (if isearch-forward
  6201. (search-forward string bound noerror)
  6202. (search-backward string bound noerror))
  6203. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6204. (and (member :headline context)
  6205. (not (member :tags context))))
  6206. (throw 'return (point))))))
  6207. (defun org-goto-local-auto-isearch ()
  6208. "Start isearch."
  6209. (interactive)
  6210. (goto-char (point-min))
  6211. (let ((keys (this-command-keys)))
  6212. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6213. (isearch-mode t)
  6214. (isearch-process-search-char (string-to-char keys)))))
  6215. (defun org-goto-ret (&optional arg)
  6216. "Finish `org-goto' by going to the new location."
  6217. (interactive "P")
  6218. (setq org-goto-selected-point (point)
  6219. org-goto-exit-command 'return)
  6220. (throw 'exit nil))
  6221. (defun org-goto-left ()
  6222. "Finish `org-goto' by going to the new location."
  6223. (interactive)
  6224. (if (org-at-heading-p)
  6225. (progn
  6226. (beginning-of-line 1)
  6227. (setq org-goto-selected-point (point)
  6228. org-goto-exit-command 'left)
  6229. (throw 'exit nil))
  6230. (error "Not on a heading")))
  6231. (defun org-goto-right ()
  6232. "Finish `org-goto' by going to the new location."
  6233. (interactive)
  6234. (if (org-at-heading-p)
  6235. (progn
  6236. (setq org-goto-selected-point (point)
  6237. org-goto-exit-command 'right)
  6238. (throw 'exit nil))
  6239. (error "Not on a heading")))
  6240. (defun org-goto-quit ()
  6241. "Finish `org-goto' without cursor motion."
  6242. (interactive)
  6243. (setq org-goto-selected-point nil)
  6244. (setq org-goto-exit-command 'quit)
  6245. (throw 'exit nil))
  6246. ;;; Indirect buffer display of subtrees
  6247. (defvar org-indirect-dedicated-frame nil
  6248. "This is the frame being used for indirect tree display.")
  6249. (defvar org-last-indirect-buffer nil)
  6250. (defun org-tree-to-indirect-buffer (&optional arg)
  6251. "Create indirect buffer and narrow it to current subtree.
  6252. With numerical prefix ARG, go up to this level and then take that tree.
  6253. If ARG is negative, go up that many levels.
  6254. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6255. indirect buffer previously made with this command, to avoid proliferation of
  6256. indirect buffers. However, when you call the command with a \
  6257. \\[universal-argument] prefix, or
  6258. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6259. is kept so that you can work with several indirect buffers at the same time.
  6260. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6261. \\[universal-argument] prefix also
  6262. requests that a new frame be made for the new buffer, so that the dedicated
  6263. frame is not changed."
  6264. (interactive "P")
  6265. (let ((cbuf (current-buffer))
  6266. (cwin (selected-window))
  6267. (pos (point))
  6268. beg end level heading ibuf)
  6269. (save-excursion
  6270. (org-back-to-heading t)
  6271. (when (numberp arg)
  6272. (setq level (org-outline-level))
  6273. (if (< arg 0) (setq arg (+ level arg)))
  6274. (while (> (setq level (org-outline-level)) arg)
  6275. (outline-up-heading 1 t)))
  6276. (setq beg (point)
  6277. heading (org-get-heading))
  6278. (org-end-of-subtree t t)
  6279. (if (org-at-heading-p) (backward-char 1))
  6280. (setq end (point)))
  6281. (if (and (buffer-live-p org-last-indirect-buffer)
  6282. (not (eq org-indirect-buffer-display 'new-frame))
  6283. (not arg))
  6284. (kill-buffer org-last-indirect-buffer))
  6285. (setq ibuf (org-get-indirect-buffer cbuf)
  6286. org-last-indirect-buffer ibuf)
  6287. (cond
  6288. ((or (eq org-indirect-buffer-display 'new-frame)
  6289. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6290. (select-frame (make-frame))
  6291. (delete-other-windows)
  6292. (org-pop-to-buffer-same-window ibuf)
  6293. (org-set-frame-title heading))
  6294. ((eq org-indirect-buffer-display 'dedicated-frame)
  6295. (raise-frame
  6296. (select-frame (or (and org-indirect-dedicated-frame
  6297. (frame-live-p org-indirect-dedicated-frame)
  6298. org-indirect-dedicated-frame)
  6299. (setq org-indirect-dedicated-frame (make-frame)))))
  6300. (delete-other-windows)
  6301. (org-pop-to-buffer-same-window ibuf)
  6302. (org-set-frame-title (concat "Indirect: " heading)))
  6303. ((eq org-indirect-buffer-display 'current-window)
  6304. (org-pop-to-buffer-same-window ibuf))
  6305. ((eq org-indirect-buffer-display 'other-window)
  6306. (pop-to-buffer ibuf))
  6307. (t (error "Invalid value")))
  6308. (if (featurep 'xemacs)
  6309. (save-excursion (org-mode) (turn-on-font-lock)))
  6310. (narrow-to-region beg end)
  6311. (show-all)
  6312. (goto-char pos)
  6313. (run-hook-with-args 'org-cycle-hook 'all)
  6314. (and (window-live-p cwin) (select-window cwin))))
  6315. (defun org-get-indirect-buffer (&optional buffer)
  6316. (setq buffer (or buffer (current-buffer)))
  6317. (let ((n 1) (base (buffer-name buffer)) bname)
  6318. (while (buffer-live-p
  6319. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6320. (setq n (1+ n)))
  6321. (condition-case nil
  6322. (make-indirect-buffer buffer bname 'clone)
  6323. (error (make-indirect-buffer buffer bname)))))
  6324. (defun org-set-frame-title (title)
  6325. "Set the title of the current frame to the string TITLE."
  6326. ;; FIXME: how to name a single frame in XEmacs???
  6327. (unless (featurep 'xemacs)
  6328. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6329. ;;;; Structure editing
  6330. ;;; Inserting headlines
  6331. (defun org-previous-line-empty-p ()
  6332. (save-excursion
  6333. (and (not (bobp))
  6334. (or (beginning-of-line 0) t)
  6335. (save-match-data
  6336. (looking-at "[ \t]*$")))))
  6337. (defun org-insert-heading (&optional force-heading invisible-ok)
  6338. "Insert a new heading or item with same depth at point.
  6339. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6340. If point is at the beginning of a headline, insert a sibling before the
  6341. current headline. If point is not at the beginning, split the line,
  6342. create the new headline with the text in the current line after point
  6343. \(but see also the variable `org-M-RET-may-split-line').
  6344. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6345. This is important for non-interactive uses of the command."
  6346. (interactive "P")
  6347. (if (or (= (buffer-size) 0)
  6348. (and (not (save-excursion
  6349. (and (ignore-errors (org-back-to-heading invisible-ok))
  6350. (org-at-heading-p))))
  6351. (or force-heading (not (org-in-item-p)))))
  6352. (progn
  6353. (insert "\n* ")
  6354. (run-hooks 'org-insert-heading-hook))
  6355. (when (or force-heading (not (org-insert-item)))
  6356. (let* ((empty-line-p nil)
  6357. (level nil)
  6358. (on-heading (org-at-heading-p))
  6359. (head (save-excursion
  6360. (condition-case nil
  6361. (progn
  6362. (org-back-to-heading invisible-ok)
  6363. (when (and (not on-heading)
  6364. (featurep 'org-inlinetask)
  6365. (integerp org-inlinetask-min-level)
  6366. (>= (length (match-string 0))
  6367. org-inlinetask-min-level))
  6368. ;; Find a heading level before the inline task
  6369. (while (and (setq level (org-up-heading-safe))
  6370. (>= level org-inlinetask-min-level)))
  6371. (if (org-at-heading-p)
  6372. (org-back-to-heading invisible-ok)
  6373. (error "This should not happen")))
  6374. (setq empty-line-p (org-previous-line-empty-p))
  6375. (match-string 0))
  6376. (error "*"))))
  6377. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6378. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6379. pos hide-previous previous-pos)
  6380. (cond
  6381. ((and (org-at-heading-p) (bolp)
  6382. (or (bobp)
  6383. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6384. ;; insert before the current line
  6385. (open-line (if blank 2 1)))
  6386. ((and (bolp)
  6387. (not org-insert-heading-respect-content)
  6388. (or (bobp)
  6389. (save-excursion
  6390. (backward-char 1) (not (outline-invisible-p)))))
  6391. ;; insert right here
  6392. nil)
  6393. (t
  6394. ;; somewhere in the line
  6395. (save-excursion
  6396. (setq previous-pos (point-at-bol))
  6397. (end-of-line)
  6398. (setq hide-previous (outline-invisible-p)))
  6399. (and org-insert-heading-respect-content (org-show-subtree))
  6400. (let ((split
  6401. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6402. (save-excursion
  6403. (let ((p (point)))
  6404. (goto-char (point-at-bol))
  6405. (and (looking-at org-complex-heading-regexp)
  6406. (match-beginning 4)
  6407. (> p (match-beginning 4)))))))
  6408. tags pos)
  6409. (cond
  6410. (org-insert-heading-respect-content
  6411. (org-end-of-subtree nil t)
  6412. (when (featurep 'org-inlinetask)
  6413. (while (and (not (eobp))
  6414. (looking-at "\\(\\*+\\)[ \t]+")
  6415. (>= (length (match-string 1))
  6416. org-inlinetask-min-level))
  6417. (org-end-of-subtree nil t)))
  6418. (or (bolp) (newline))
  6419. (or (org-previous-line-empty-p)
  6420. (and blank (newline)))
  6421. (open-line 1))
  6422. ((org-at-heading-p)
  6423. (when hide-previous
  6424. (show-children)
  6425. (org-show-entry))
  6426. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6427. (setq tags (and (match-end 2) (match-string 2)))
  6428. (and (match-end 1)
  6429. (delete-region (match-beginning 1) (match-end 1)))
  6430. (setq pos (point-at-bol))
  6431. (or split (end-of-line 1))
  6432. (delete-horizontal-space)
  6433. (if (string-match "\\`\\*+\\'"
  6434. (buffer-substring (point-at-bol) (point)))
  6435. (insert " "))
  6436. (newline (if blank 2 1))
  6437. (when tags
  6438. (save-excursion
  6439. (goto-char pos)
  6440. (end-of-line 1)
  6441. (insert " " tags)
  6442. (org-set-tags nil 'align))))
  6443. (t
  6444. (or split (end-of-line 1))
  6445. (newline (if blank 2 1)))))))
  6446. (insert head) (just-one-space)
  6447. (setq pos (point))
  6448. (end-of-line 1)
  6449. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6450. (when (and org-insert-heading-respect-content hide-previous)
  6451. (save-excursion
  6452. (goto-char previous-pos)
  6453. (hide-subtree)))
  6454. (run-hooks 'org-insert-heading-hook)))))
  6455. (defun org-get-heading (&optional no-tags no-todo)
  6456. "Return the heading of the current entry, without the stars.
  6457. When NO-TAGS is non-nil, don't include tags.
  6458. When NO-TODO is non-nil, don't include TODO keywords."
  6459. (save-excursion
  6460. (org-back-to-heading t)
  6461. (cond
  6462. ((and no-tags no-todo)
  6463. (looking-at org-complex-heading-regexp)
  6464. (match-string 4))
  6465. (no-tags
  6466. (looking-at (concat org-outline-regexp
  6467. "\\(.*?\\)"
  6468. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6469. (match-string 1))
  6470. (no-todo
  6471. (looking-at org-todo-line-regexp)
  6472. (match-string 3))
  6473. (t (looking-at org-heading-regexp)
  6474. (match-string 2)))))
  6475. (defun org-heading-components ()
  6476. "Return the components of the current heading.
  6477. This is a list with the following elements:
  6478. - the level as an integer
  6479. - the reduced level, different if `org-odd-levels-only' is set.
  6480. - the TODO keyword, or nil
  6481. - the priority character, like ?A, or nil if no priority is given
  6482. - the headline text itself, or the tags string if no headline text
  6483. - the tags string, or nil."
  6484. (save-excursion
  6485. (org-back-to-heading t)
  6486. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6487. (list (length (match-string 1))
  6488. (org-reduced-level (length (match-string 1)))
  6489. (org-match-string-no-properties 2)
  6490. (and (match-end 3) (aref (match-string 3) 2))
  6491. (org-match-string-no-properties 4)
  6492. (org-match-string-no-properties 5)))))
  6493. (defun org-get-entry ()
  6494. "Get the entry text, after heading, entire subtree."
  6495. (save-excursion
  6496. (org-back-to-heading t)
  6497. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6498. (defun org-insert-heading-after-current ()
  6499. "Insert a new heading with same level as current, after current subtree."
  6500. (interactive)
  6501. (org-back-to-heading)
  6502. (org-insert-heading)
  6503. (org-move-subtree-down)
  6504. (end-of-line 1))
  6505. (defun org-insert-heading-respect-content ()
  6506. (interactive)
  6507. (let ((org-insert-heading-respect-content t))
  6508. (org-insert-heading t)))
  6509. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6510. (interactive "P")
  6511. (let ((org-insert-heading-respect-content t))
  6512. (org-insert-todo-heading force-state t)))
  6513. (defun org-insert-todo-heading (arg &optional force-heading)
  6514. "Insert a new heading with the same level and TODO state as current heading.
  6515. If the heading has no TODO state, or if the state is DONE, use the first
  6516. state (TODO by default). Also with prefix arg, force first state."
  6517. (interactive "P")
  6518. (when (or force-heading (not (org-insert-item 'checkbox)))
  6519. (org-insert-heading force-heading)
  6520. (save-excursion
  6521. (org-back-to-heading)
  6522. (outline-previous-heading)
  6523. (looking-at org-todo-line-regexp))
  6524. (let*
  6525. ((new-mark-x
  6526. (if (or arg
  6527. (not (match-beginning 2))
  6528. (member (match-string 2) org-done-keywords))
  6529. (car org-todo-keywords-1)
  6530. (match-string 2)))
  6531. (new-mark
  6532. (or
  6533. (run-hook-with-args-until-success
  6534. 'org-todo-get-default-hook new-mark-x nil)
  6535. new-mark-x)))
  6536. (beginning-of-line 1)
  6537. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6538. (if org-treat-insert-todo-heading-as-state-change
  6539. (org-todo new-mark)
  6540. (insert new-mark " "))))
  6541. (when org-provide-todo-statistics
  6542. (org-update-parent-todo-statistics))))
  6543. (defun org-insert-subheading (arg)
  6544. "Insert a new subheading and demote it.
  6545. Works for outline headings and for plain lists alike."
  6546. (interactive "P")
  6547. (org-insert-heading arg)
  6548. (cond
  6549. ((org-at-heading-p) (org-do-demote))
  6550. ((org-at-item-p) (org-indent-item))))
  6551. (defun org-insert-todo-subheading (arg)
  6552. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6553. Works for outline headings and for plain lists alike."
  6554. (interactive "P")
  6555. (org-insert-todo-heading arg)
  6556. (cond
  6557. ((org-at-heading-p) (org-do-demote))
  6558. ((org-at-item-p) (org-indent-item))))
  6559. ;;; Promotion and Demotion
  6560. (defvar org-after-demote-entry-hook nil
  6561. "Hook run after an entry has been demoted.
  6562. The cursor will be at the beginning of the entry.
  6563. When a subtree is being demoted, the hook will be called for each node.")
  6564. (defvar org-after-promote-entry-hook nil
  6565. "Hook run after an entry has been promoted.
  6566. The cursor will be at the beginning of the entry.
  6567. When a subtree is being promoted, the hook will be called for each node.")
  6568. (defun org-promote-subtree ()
  6569. "Promote the entire subtree.
  6570. See also `org-promote'."
  6571. (interactive)
  6572. (save-excursion
  6573. (org-with-limited-levels (org-map-tree 'org-promote)))
  6574. (org-fix-position-after-promote))
  6575. (defun org-demote-subtree ()
  6576. "Demote the entire subtree. See `org-demote'.
  6577. See also `org-promote'."
  6578. (interactive)
  6579. (save-excursion
  6580. (org-with-limited-levels (org-map-tree 'org-demote)))
  6581. (org-fix-position-after-promote))
  6582. (defun org-do-promote ()
  6583. "Promote the current heading higher up the tree.
  6584. If the region is active in `transient-mark-mode', promote all headings
  6585. in the region."
  6586. (interactive)
  6587. (save-excursion
  6588. (if (org-region-active-p)
  6589. (org-map-region 'org-promote (region-beginning) (region-end))
  6590. (org-promote)))
  6591. (org-fix-position-after-promote))
  6592. (defun org-do-demote ()
  6593. "Demote the current heading lower down the tree.
  6594. If the region is active in `transient-mark-mode', demote all headings
  6595. in the region."
  6596. (interactive)
  6597. (save-excursion
  6598. (if (org-region-active-p)
  6599. (org-map-region 'org-demote (region-beginning) (region-end))
  6600. (org-demote)))
  6601. (org-fix-position-after-promote))
  6602. (defun org-fix-position-after-promote ()
  6603. "Make sure that after pro/demotion cursor position is right."
  6604. (let ((pos (point)))
  6605. (when (save-excursion
  6606. (beginning-of-line 1)
  6607. (looking-at org-todo-line-regexp)
  6608. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6609. (cond ((eobp) (insert " "))
  6610. ((eolp) (insert " "))
  6611. ((equal (char-after) ?\ ) (forward-char 1))))))
  6612. (defun org-current-level ()
  6613. "Return the level of the current entry, or nil if before the first headline.
  6614. The level is the number of stars at the beginning of the headline."
  6615. (save-excursion
  6616. (org-with-limited-levels
  6617. (if (ignore-errors (org-back-to-heading t))
  6618. (funcall outline-level)))))
  6619. (defun org-get-previous-line-level ()
  6620. "Return the outline depth of the last headline before the current line.
  6621. Returns 0 for the first headline in the buffer, and nil if before the
  6622. first headline."
  6623. (let ((current-level (org-current-level))
  6624. (prev-level (when (> (line-number-at-pos) 1)
  6625. (save-excursion
  6626. (beginning-of-line 0)
  6627. (org-current-level)))))
  6628. (cond ((null current-level) nil) ; Before first headline
  6629. ((null prev-level) 0) ; At first headline
  6630. (prev-level))))
  6631. (defun org-reduced-level (l)
  6632. "Compute the effective level of a heading.
  6633. This takes into account the setting of `org-odd-levels-only'."
  6634. (cond
  6635. ((zerop l) 0)
  6636. (org-odd-levels-only (1+ (floor (/ l 2))))
  6637. (t l)))
  6638. (defun org-level-increment ()
  6639. "Return the number of stars that will be added or removed at a
  6640. time to headlines when structure editing, based on the value of
  6641. `org-odd-levels-only'."
  6642. (if org-odd-levels-only 2 1))
  6643. (defun org-get-valid-level (level &optional change)
  6644. "Rectify a level change under the influence of `org-odd-levels-only'
  6645. LEVEL is a current level, CHANGE is by how much the level should be
  6646. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6647. even level numbers will become the next higher odd number."
  6648. (if org-odd-levels-only
  6649. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6650. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6651. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6652. (max 1 (+ level (or change 0)))))
  6653. (if (boundp 'define-obsolete-function-alias)
  6654. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6655. (define-obsolete-function-alias 'org-get-legal-level
  6656. 'org-get-valid-level)
  6657. (define-obsolete-function-alias 'org-get-legal-level
  6658. 'org-get-valid-level "23.1")))
  6659. (defun org-promote ()
  6660. "Promote the current heading higher up the tree.
  6661. If the region is active in `transient-mark-mode', promote all headings
  6662. in the region."
  6663. (org-back-to-heading t)
  6664. (let* ((level (save-match-data (funcall outline-level)))
  6665. (after-change-functions (remove 'flyspell-after-change-function
  6666. after-change-functions))
  6667. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6668. (diff (abs (- level (length up-head) -1))))
  6669. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6670. (replace-match up-head nil t)
  6671. ;; Fixup tag positioning
  6672. (and org-auto-align-tags (org-set-tags nil t))
  6673. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6674. (run-hooks 'org-after-promote-entry-hook)))
  6675. (defun org-demote ()
  6676. "Demote the current heading lower down the tree.
  6677. If the region is active in `transient-mark-mode', demote all headings
  6678. in the region."
  6679. (org-back-to-heading t)
  6680. (let* ((level (save-match-data (funcall outline-level)))
  6681. (after-change-functions (remove 'flyspell-after-change-function
  6682. after-change-functions))
  6683. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6684. (diff (abs (- level (length down-head) -1))))
  6685. (replace-match down-head nil t)
  6686. ;; Fixup tag positioning
  6687. (and org-auto-align-tags (org-set-tags nil t))
  6688. (if org-adapt-indentation (org-fixup-indentation diff))
  6689. (run-hooks 'org-after-demote-entry-hook)))
  6690. (defun org-cycle-level ()
  6691. "Cycle the level of an empty headline through possible states.
  6692. This goes first to child, then to parent, level, then up the hierarchy.
  6693. After top level, it switches back to sibling level."
  6694. (interactive)
  6695. (let ((org-adapt-indentation nil))
  6696. (when (org-point-at-end-of-empty-headline)
  6697. (setq this-command 'org-cycle-level) ; Only needed for caching
  6698. (let ((cur-level (org-current-level))
  6699. (prev-level (org-get-previous-line-level)))
  6700. (cond
  6701. ;; If first headline in file, promote to top-level.
  6702. ((= prev-level 0)
  6703. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6704. do (org-do-promote)))
  6705. ;; If same level as prev, demote one.
  6706. ((= prev-level cur-level)
  6707. (org-do-demote))
  6708. ;; If parent is top-level, promote to top level if not already.
  6709. ((= prev-level 1)
  6710. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6711. do (org-do-promote)))
  6712. ;; If top-level, return to prev-level.
  6713. ((= cur-level 1)
  6714. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6715. do (org-do-demote)))
  6716. ;; If less than prev-level, promote one.
  6717. ((< cur-level prev-level)
  6718. (org-do-promote))
  6719. ;; If deeper than prev-level, promote until higher than
  6720. ;; prev-level.
  6721. ((> cur-level prev-level)
  6722. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6723. do (org-do-promote))))
  6724. t))))
  6725. (defun org-map-tree (fun)
  6726. "Call FUN for every heading underneath the current one."
  6727. (org-back-to-heading)
  6728. (let ((level (funcall outline-level)))
  6729. (save-excursion
  6730. (funcall fun)
  6731. (while (and (progn
  6732. (outline-next-heading)
  6733. (> (funcall outline-level) level))
  6734. (not (eobp)))
  6735. (funcall fun)))))
  6736. (defun org-map-region (fun beg end)
  6737. "Call FUN for every heading between BEG and END."
  6738. (let ((org-ignore-region t))
  6739. (save-excursion
  6740. (setq end (copy-marker end))
  6741. (goto-char beg)
  6742. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6743. (< (point) end))
  6744. (funcall fun))
  6745. (while (and (progn
  6746. (outline-next-heading)
  6747. (< (point) end))
  6748. (not (eobp)))
  6749. (funcall fun)))))
  6750. (defvar org-property-end-re) ; silence byte-compiler
  6751. (defun org-fixup-indentation (diff)
  6752. "Change the indentation in the current entry by DIFF.
  6753. However, if any line in the current entry has no indentation, or if it
  6754. would end up with no indentation after the change, nothing at all is done."
  6755. (save-excursion
  6756. (let ((end (save-excursion (outline-next-heading)
  6757. (point-marker)))
  6758. (prohibit (if (> diff 0)
  6759. "^\\S-"
  6760. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6761. col)
  6762. (unless (save-excursion (end-of-line 1)
  6763. (re-search-forward prohibit end t))
  6764. (while (and (< (point) end)
  6765. (re-search-forward "^[ \t]+" end t))
  6766. (goto-char (match-end 0))
  6767. (setq col (current-column))
  6768. (if (< diff 0) (replace-match ""))
  6769. (org-indent-to-column (+ diff col))))
  6770. (move-marker end nil))))
  6771. (defun org-convert-to-odd-levels ()
  6772. "Convert an org-mode file with all levels allowed to one with odd levels.
  6773. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6774. level 5 etc."
  6775. (interactive)
  6776. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6777. (let ((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 (- (length (match-string 0)) 2))
  6783. (while (>= (setq n (1- n)) 0)
  6784. (org-demote))
  6785. (end-of-line 1))))))
  6786. (defun org-convert-to-oddeven-levels ()
  6787. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6788. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6789. file contains a section with an even level, conversion would
  6790. destroy the structure of the file. An error is signaled in this
  6791. case."
  6792. (interactive)
  6793. (goto-char (point-min))
  6794. ;; First check if there are no even levels
  6795. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6796. (org-show-context t)
  6797. (error "Not all levels are odd in this file. Conversion not possible"))
  6798. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6799. (let ((outline-regexp org-outline-regexp)
  6800. (outline-level 'org-outline-level)
  6801. (org-odd-levels-only nil) n)
  6802. (save-excursion
  6803. (goto-char (point-min))
  6804. (while (re-search-forward "^\\*\\*+ " nil t)
  6805. (setq n (/ (1- (length (match-string 0))) 2))
  6806. (while (>= (setq n (1- n)) 0)
  6807. (org-promote))
  6808. (end-of-line 1))))))
  6809. (defun org-tr-level (n)
  6810. "Make N odd if required."
  6811. (if org-odd-levels-only (1+ (/ n 2)) n))
  6812. ;;; Vertical tree motion, cutting and pasting of subtrees
  6813. (defun org-move-subtree-up (&optional arg)
  6814. "Move the current subtree up past ARG headlines of the same level."
  6815. (interactive "p")
  6816. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6817. (defun org-move-subtree-down (&optional arg)
  6818. "Move the current subtree down past ARG headlines of the same level."
  6819. (interactive "p")
  6820. (setq arg (prefix-numeric-value arg))
  6821. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6822. 'org-get-last-sibling))
  6823. (ins-point (make-marker))
  6824. (cnt (abs arg))
  6825. (col (current-column))
  6826. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6827. ;; Select the tree
  6828. (org-back-to-heading)
  6829. (setq beg0 (point))
  6830. (save-excursion
  6831. (setq ne-beg (org-back-over-empty-lines))
  6832. (setq beg (point)))
  6833. (save-match-data
  6834. (save-excursion (outline-end-of-heading)
  6835. (setq folded (outline-invisible-p)))
  6836. (outline-end-of-subtree))
  6837. (outline-next-heading)
  6838. (setq ne-end (org-back-over-empty-lines))
  6839. (setq end (point))
  6840. (goto-char beg0)
  6841. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6842. ;; include less whitespace
  6843. (save-excursion
  6844. (goto-char beg)
  6845. (forward-line (- ne-beg ne-end))
  6846. (setq beg (point))))
  6847. ;; Find insertion point, with error handling
  6848. (while (> cnt 0)
  6849. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6850. (progn (goto-char beg0)
  6851. (error "Cannot move past superior level or buffer limit")))
  6852. (setq cnt (1- cnt)))
  6853. (if (> arg 0)
  6854. ;; Moving forward - still need to move over subtree
  6855. (progn (org-end-of-subtree t t)
  6856. (save-excursion
  6857. (org-back-over-empty-lines)
  6858. (or (bolp) (newline)))))
  6859. (setq ne-ins (org-back-over-empty-lines))
  6860. (move-marker ins-point (point))
  6861. (setq txt (buffer-substring beg end))
  6862. (org-save-markers-in-region beg end)
  6863. (delete-region beg end)
  6864. (org-remove-empty-overlays-at beg)
  6865. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6866. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6867. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6868. (let ((bbb (point)))
  6869. (insert-before-markers txt)
  6870. (org-reinstall-markers-in-region bbb)
  6871. (move-marker ins-point bbb))
  6872. (or (bolp) (insert "\n"))
  6873. (setq ins-end (point))
  6874. (goto-char ins-point)
  6875. (org-skip-whitespace)
  6876. (when (and (< arg 0)
  6877. (org-first-sibling-p)
  6878. (> ne-ins ne-beg))
  6879. ;; Move whitespace back to beginning
  6880. (save-excursion
  6881. (goto-char ins-end)
  6882. (let ((kill-whole-line t))
  6883. (kill-line (- ne-ins ne-beg)) (point)))
  6884. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6885. (move-marker ins-point nil)
  6886. (if folded
  6887. (hide-subtree)
  6888. (org-show-entry)
  6889. (show-children)
  6890. (org-cycle-hide-drawers 'children))
  6891. (org-clean-visibility-after-subtree-move)
  6892. ;; move back to the initial column we were at
  6893. (move-to-column col)))
  6894. (defvar org-subtree-clip ""
  6895. "Clipboard for cut and paste of subtrees.
  6896. This is actually only a copy of the kill, because we use the normal kill
  6897. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6898. (defvar org-subtree-clip-folded nil
  6899. "Was the last copied subtree folded?
  6900. This is used to fold the tree back after pasting.")
  6901. (defun org-cut-subtree (&optional n)
  6902. "Cut the current subtree into the clipboard.
  6903. With prefix arg N, cut this many sequential subtrees.
  6904. This is a short-hand for marking the subtree and then cutting it."
  6905. (interactive "p")
  6906. (org-copy-subtree n 'cut))
  6907. (defun org-copy-subtree (&optional n cut force-store-markers)
  6908. "Cut the current subtree into the clipboard.
  6909. With prefix arg N, cut this many sequential subtrees.
  6910. This is a short-hand for marking the subtree and then copying it.
  6911. If CUT is non-nil, actually cut the subtree.
  6912. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6913. of some markers in the region, even if CUT is non-nil. This is
  6914. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6915. (interactive "p")
  6916. (let (beg end folded (beg0 (point)))
  6917. (if (org-called-interactively-p 'any)
  6918. (org-back-to-heading nil) ; take what looks like a subtree
  6919. (org-back-to-heading t)) ; take what is really there
  6920. (org-back-over-empty-lines)
  6921. (setq beg (point))
  6922. (skip-chars-forward " \t\r\n")
  6923. (save-match-data
  6924. (save-excursion (outline-end-of-heading)
  6925. (setq folded (outline-invisible-p)))
  6926. (condition-case nil
  6927. (org-forward-same-level (1- n) t)
  6928. (error nil))
  6929. (org-end-of-subtree t t))
  6930. (org-back-over-empty-lines)
  6931. (setq end (point))
  6932. (goto-char beg0)
  6933. (when (> end beg)
  6934. (setq org-subtree-clip-folded folded)
  6935. (when (or cut force-store-markers)
  6936. (org-save-markers-in-region beg end))
  6937. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6938. (setq org-subtree-clip (current-kill 0))
  6939. (message "%s: Subtree(s) with %d characters"
  6940. (if cut "Cut" "Copied")
  6941. (length org-subtree-clip)))))
  6942. (defun org-paste-subtree (&optional level tree for-yank)
  6943. "Paste the clipboard as a subtree, with modification of headline level.
  6944. The entire subtree is promoted or demoted in order to match a new headline
  6945. level.
  6946. If the cursor is at the beginning of a headline, the same level as
  6947. that headline is used to paste the tree
  6948. If not, the new level is derived from the *visible* headings
  6949. before and after the insertion point, and taken to be the inferior headline
  6950. level of the two. So if the previous visible heading is level 3 and the
  6951. next is level 4 (or vice versa), level 4 will be used for insertion.
  6952. This makes sure that the subtree remains an independent subtree and does
  6953. not swallow low level entries.
  6954. You can also force a different level, either by using a numeric prefix
  6955. argument, or by inserting the heading marker by hand. For example, if the
  6956. cursor is after \"*****\", then the tree will be shifted to level 5.
  6957. If optional TREE is given, use this text instead of the kill ring.
  6958. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6959. move back over whitespace before inserting, and move point to the end of
  6960. the inserted text when done."
  6961. (interactive "P")
  6962. (setq tree (or tree (and kill-ring (current-kill 0))))
  6963. (unless (org-kill-is-subtree-p tree)
  6964. (error "%s"
  6965. (substitute-command-keys
  6966. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6967. (org-with-limited-levels
  6968. (let* ((visp (not (outline-invisible-p)))
  6969. (txt tree)
  6970. (^re_ "\\(\\*+\\)[ \t]*")
  6971. (old-level (if (string-match org-outline-regexp-bol txt)
  6972. (- (match-end 0) (match-beginning 0) 1)
  6973. -1))
  6974. (force-level (cond (level (prefix-numeric-value level))
  6975. ((and (looking-at "[ \t]*$")
  6976. (string-match
  6977. "^\\*+$" (buffer-substring
  6978. (point-at-bol) (point))))
  6979. (- (match-end 1) (match-beginning 1)))
  6980. ((and (bolp)
  6981. (looking-at org-outline-regexp))
  6982. (- (match-end 0) (point) 1))
  6983. (t nil)))
  6984. (previous-level (save-excursion
  6985. (condition-case nil
  6986. (progn
  6987. (outline-previous-visible-heading 1)
  6988. (if (looking-at ^re_)
  6989. (- (match-end 0) (match-beginning 0) 1)
  6990. 1))
  6991. (error 1))))
  6992. (next-level (save-excursion
  6993. (condition-case nil
  6994. (progn
  6995. (or (looking-at org-outline-regexp)
  6996. (outline-next-visible-heading 1))
  6997. (if (looking-at ^re_)
  6998. (- (match-end 0) (match-beginning 0) 1)
  6999. 1))
  7000. (error 1))))
  7001. (new-level (or force-level (max previous-level next-level)))
  7002. (shift (if (or (= old-level -1)
  7003. (= new-level -1)
  7004. (= old-level new-level))
  7005. 0
  7006. (- new-level old-level)))
  7007. (delta (if (> shift 0) -1 1))
  7008. (func (if (> shift 0) 'org-demote 'org-promote))
  7009. (org-odd-levels-only nil)
  7010. beg end newend)
  7011. ;; Remove the forced level indicator
  7012. (if force-level
  7013. (delete-region (point-at-bol) (point)))
  7014. ;; Paste
  7015. (beginning-of-line (if (bolp) 1 2))
  7016. (unless for-yank (org-back-over-empty-lines))
  7017. (setq beg (point))
  7018. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7019. (insert-before-markers txt)
  7020. (unless (string-match "\n\\'" txt) (insert "\n"))
  7021. (setq newend (point))
  7022. (org-reinstall-markers-in-region beg)
  7023. (setq end (point))
  7024. (goto-char beg)
  7025. (skip-chars-forward " \t\n\r")
  7026. (setq beg (point))
  7027. (if (and (outline-invisible-p) visp)
  7028. (save-excursion (outline-show-heading)))
  7029. ;; Shift if necessary
  7030. (unless (= shift 0)
  7031. (save-restriction
  7032. (narrow-to-region beg end)
  7033. (while (not (= shift 0))
  7034. (org-map-region func (point-min) (point-max))
  7035. (setq shift (+ delta shift)))
  7036. (goto-char (point-min))
  7037. (setq newend (point-max))))
  7038. (when (or (org-called-interactively-p 'interactive) for-yank)
  7039. (message "Clipboard pasted as level %d subtree" new-level))
  7040. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7041. kill-ring
  7042. (eq org-subtree-clip (current-kill 0))
  7043. org-subtree-clip-folded)
  7044. ;; The tree was folded before it was killed/copied
  7045. (hide-subtree))
  7046. (and for-yank (goto-char newend)))))
  7047. (defun org-kill-is-subtree-p (&optional txt)
  7048. "Check if the current kill is an outline subtree, or a set of trees.
  7049. Returns nil if kill does not start with a headline, or if the first
  7050. headline level is not the largest headline level in the tree.
  7051. So this will actually accept several entries of equal levels as well,
  7052. which is OK for `org-paste-subtree'.
  7053. If optional TXT is given, check this string instead of the current kill."
  7054. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7055. (re (org-get-limited-outline-regexp))
  7056. (^re (concat "^" re))
  7057. (start-level (and kill
  7058. (string-match
  7059. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7060. kill)
  7061. (- (match-end 2) (match-beginning 2) 1)))
  7062. (start (1+ (or (match-beginning 2) -1))))
  7063. (if (not start-level)
  7064. (progn
  7065. nil) ;; does not even start with a heading
  7066. (catch 'exit
  7067. (while (setq start (string-match ^re kill (1+ start)))
  7068. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7069. (throw 'exit nil)))
  7070. t))))
  7071. (defvar org-markers-to-move nil
  7072. "Markers that should be moved with a cut-and-paste operation.
  7073. Those markers are stored together with their positions relative to
  7074. the start of the region.")
  7075. (defun org-save-markers-in-region (beg end)
  7076. "Check markers in region.
  7077. If these markers are between BEG and END, record their position relative
  7078. to BEG, so that after moving the block of text, we can put the markers back
  7079. into place.
  7080. This function gets called just before an entry or tree gets cut from the
  7081. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7082. called immediately, to move the markers with the entries."
  7083. (setq org-markers-to-move nil)
  7084. (when (featurep 'org-clock)
  7085. (org-clock-save-markers-for-cut-and-paste beg end))
  7086. (when (featurep 'org-agenda)
  7087. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7088. (defun org-check-and-save-marker (marker beg end)
  7089. "Check if MARKER is between BEG and END.
  7090. If yes, remember the marker and the distance to BEG."
  7091. (when (and (marker-buffer marker)
  7092. (equal (marker-buffer marker) (current-buffer)))
  7093. (if (and (>= marker beg) (< marker end))
  7094. (push (cons marker (- marker beg)) org-markers-to-move))))
  7095. (defun org-reinstall-markers-in-region (beg)
  7096. "Move all remembered markers to their position relative to BEG."
  7097. (mapc (lambda (x)
  7098. (move-marker (car x) (+ beg (cdr x))))
  7099. org-markers-to-move)
  7100. (setq org-markers-to-move nil))
  7101. (defun org-narrow-to-subtree ()
  7102. "Narrow buffer to the current subtree."
  7103. (interactive)
  7104. (save-excursion
  7105. (save-match-data
  7106. (org-with-limited-levels
  7107. (narrow-to-region
  7108. (progn (org-back-to-heading t) (point))
  7109. (progn (org-end-of-subtree t t)
  7110. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7111. (point)))))))
  7112. (defun org-narrow-to-block ()
  7113. "Narrow buffer to the current block."
  7114. (interactive)
  7115. (let* ((case-fold-search t)
  7116. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7117. "^[ \t]*#\\+end_.*")))
  7118. (if blockp
  7119. (narrow-to-region (car blockp) (cdr blockp))
  7120. (error "Not in a block"))))
  7121. (eval-when-compile
  7122. (defvar org-property-drawer-re))
  7123. (defvar org-property-start-re) ;; defined below
  7124. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7125. "Clone the task (subtree) at point N times.
  7126. The clones will be inserted as siblings.
  7127. In interactive use, the user will be prompted for the number of
  7128. clones to be produced, and for a time SHIFT, which may be a
  7129. repeater as used in time stamps, for example `+3d'.
  7130. When a valid repeater is given and the entry contains any time
  7131. stamps, the clones will become a sequence in time, with time
  7132. stamps in the subtree shifted for each clone produced. If SHIFT
  7133. is nil or the empty string, time stamps will be left alone. The
  7134. ID property of the original subtree is removed.
  7135. If the original subtree did contain time stamps with a repeater,
  7136. the following will happen:
  7137. - the repeater will be removed in each clone
  7138. - an additional clone will be produced, with the current, unshifted
  7139. date(s) in the entry.
  7140. - the original entry will be placed *after* all the clones, with
  7141. repeater intact.
  7142. - the start days in the repeater in the original entry will be shifted
  7143. to past the last clone.
  7144. In this way you can spell out a number of instances of a repeating task,
  7145. and still retain the repeater to cover future instances of the task."
  7146. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7147. (let (beg end template task idprop
  7148. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7149. (drawer-re org-drawer-regexp))
  7150. (if (not (and (integerp n) (> n 0)))
  7151. (error "Invalid number of replications %s" n))
  7152. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7153. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7154. shift)))
  7155. (error "Invalid shift specification %s" shift))
  7156. (when doshift
  7157. (setq shift-n (string-to-number (match-string 1 shift))
  7158. shift-what (cdr (assoc (match-string 2 shift)
  7159. '(("d" . day) ("w" . week)
  7160. ("m" . month) ("y" . year))))))
  7161. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7162. (setq nmin 1 nmax n)
  7163. (org-back-to-heading t)
  7164. (setq beg (point))
  7165. (setq idprop (org-entry-get nil "ID"))
  7166. (org-end-of-subtree t t)
  7167. (or (bolp) (insert "\n"))
  7168. (setq end (point))
  7169. (setq template (buffer-substring beg end))
  7170. (when (and doshift
  7171. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7172. (delete-region beg end)
  7173. (setq end beg)
  7174. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7175. (goto-char end)
  7176. (loop for n from nmin to nmax do
  7177. ;; prepare clone
  7178. (with-temp-buffer
  7179. (insert template)
  7180. (org-mode)
  7181. (goto-char (point-min))
  7182. (org-show-subtree)
  7183. (and idprop (if org-clone-delete-id
  7184. (org-entry-delete nil "ID")
  7185. (org-id-get-create t)))
  7186. (unless (= n 0)
  7187. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7188. (kill-whole-line))
  7189. (goto-char (point-min))
  7190. (while (re-search-forward drawer-re nil t)
  7191. (mapc (lambda (d)
  7192. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7193. (goto-char (point-min))
  7194. (when doshift
  7195. (while (re-search-forward org-ts-regexp-both nil t)
  7196. (org-timestamp-change (* n shift-n) shift-what))
  7197. (unless (= n n-no-remove)
  7198. (goto-char (point-min))
  7199. (while (re-search-forward org-ts-regexp nil t)
  7200. (save-excursion
  7201. (goto-char (match-beginning 0))
  7202. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7203. (delete-region (match-beginning 1) (match-end 1)))))))
  7204. (setq task (buffer-string)))
  7205. (insert task))
  7206. (goto-char beg)))
  7207. ;;; Outline Sorting
  7208. (defun org-sort (with-case)
  7209. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7210. Optional argument WITH-CASE means sort case-sensitively."
  7211. (interactive "P")
  7212. (cond
  7213. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7214. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7215. (t
  7216. (org-call-with-arg 'org-sort-entries with-case))))
  7217. (defun org-sort-remove-invisible (s)
  7218. (remove-text-properties 0 (length s) org-rm-props s)
  7219. (while (string-match org-bracket-link-regexp s)
  7220. (setq s (replace-match (if (match-end 2)
  7221. (match-string 3 s)
  7222. (match-string 1 s)) t t s)))
  7223. s)
  7224. (defvar org-priority-regexp) ; defined later in the file
  7225. (defvar org-after-sorting-entries-or-items-hook nil
  7226. "Hook that is run after a bunch of entries or items have been sorted.
  7227. When children are sorted, the cursor is in the parent line when this
  7228. hook gets called. When a region or a plain list is sorted, the cursor
  7229. will be in the first entry of the sorted region/list.")
  7230. (defun org-sort-entries
  7231. (&optional with-case sorting-type getkey-func compare-func property)
  7232. "Sort entries on a certain level of an outline tree.
  7233. If there is an active region, the entries in the region are sorted.
  7234. Else, if the cursor is before the first entry, sort the top-level items.
  7235. Else, the children of the entry at point are sorted.
  7236. Sorting can be alphabetically, numerically, by date/time as given by
  7237. a time stamp, by a property or by priority.
  7238. The command prompts for the sorting type unless it has been given to the
  7239. function through the SORTING-TYPE argument, which needs to be a character,
  7240. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7241. precise meaning of each character:
  7242. n Numerically, by converting the beginning of the entry/item to a number.
  7243. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7244. t By date/time, either the first active time stamp in the entry, or, if
  7245. none exist, by the first inactive one.
  7246. s By the scheduled date/time.
  7247. d By deadline date/time.
  7248. c By creation time, which is assumed to be the first inactive time stamp
  7249. at the beginning of a line.
  7250. p By priority according to the cookie.
  7251. r By the value of a property.
  7252. Capital letters will reverse the sort order.
  7253. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7254. called with point at the beginning of the record. It must return either
  7255. a string or a number that should serve as the sorting key for that record.
  7256. Comparing entries ignores case by default. However, with an optional argument
  7257. WITH-CASE, the sorting considers case as well."
  7258. (interactive "P")
  7259. (let ((case-func (if with-case 'identity 'downcase))
  7260. start beg end stars re re2
  7261. txt what tmp)
  7262. ;; Find beginning and end of region to sort
  7263. (cond
  7264. ((org-region-active-p)
  7265. ;; we will sort the region
  7266. (setq end (region-end)
  7267. what "region")
  7268. (goto-char (region-beginning))
  7269. (if (not (org-at-heading-p)) (outline-next-heading))
  7270. (setq start (point)))
  7271. ((or (org-at-heading-p)
  7272. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7273. ;; we will sort the children of the current headline
  7274. (org-back-to-heading)
  7275. (setq start (point)
  7276. end (progn (org-end-of-subtree t t)
  7277. (or (bolp) (insert "\n"))
  7278. (org-back-over-empty-lines)
  7279. (point))
  7280. what "children")
  7281. (goto-char start)
  7282. (show-subtree)
  7283. (outline-next-heading))
  7284. (t
  7285. ;; we will sort the top-level entries in this file
  7286. (goto-char (point-min))
  7287. (or (org-at-heading-p) (outline-next-heading))
  7288. (setq start (point))
  7289. (goto-char (point-max))
  7290. (beginning-of-line 1)
  7291. (when (looking-at ".*?\\S-")
  7292. ;; File ends in a non-white line
  7293. (end-of-line 1)
  7294. (insert "\n"))
  7295. (setq end (point-max))
  7296. (setq what "top-level")
  7297. (goto-char start)
  7298. (show-all)))
  7299. (setq beg (point))
  7300. (if (>= beg end) (error "Nothing to sort"))
  7301. (looking-at "\\(\\*+\\)")
  7302. (setq stars (match-string 1)
  7303. re (concat "^" (regexp-quote stars) " +")
  7304. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7305. txt (buffer-substring beg end))
  7306. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7307. (if (and (not (equal stars "*")) (string-match re2 txt))
  7308. (error "Region to sort contains a level above the first entry"))
  7309. (unless sorting-type
  7310. (message
  7311. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7312. [t]ime [s]cheduled [d]eadline [c]reated
  7313. A/N/T/S/D/C/P/O/F means reversed:"
  7314. what)
  7315. (setq sorting-type (read-char-exclusive))
  7316. (and (= (downcase sorting-type) ?f)
  7317. (setq getkey-func
  7318. (org-icompleting-read "Sort using function: "
  7319. obarray 'fboundp t nil nil))
  7320. (setq getkey-func (intern getkey-func)))
  7321. (and (= (downcase sorting-type) ?r)
  7322. (setq property
  7323. (org-icompleting-read "Property: "
  7324. (mapcar 'list (org-buffer-property-keys t))
  7325. nil t))))
  7326. (message "Sorting entries...")
  7327. (save-restriction
  7328. (narrow-to-region start end)
  7329. (let ((dcst (downcase sorting-type))
  7330. (case-fold-search nil)
  7331. (now (current-time)))
  7332. (sort-subr
  7333. (/= dcst sorting-type)
  7334. ;; This function moves to the beginning character of the "record" to
  7335. ;; be sorted.
  7336. (lambda nil
  7337. (if (re-search-forward re nil t)
  7338. (goto-char (match-beginning 0))
  7339. (goto-char (point-max))))
  7340. ;; This function moves to the last character of the "record" being
  7341. ;; sorted.
  7342. (lambda nil
  7343. (save-match-data
  7344. (condition-case nil
  7345. (outline-forward-same-level 1)
  7346. (error
  7347. (goto-char (point-max))))))
  7348. ;; This function returns the value that gets sorted against.
  7349. (lambda nil
  7350. (cond
  7351. ((= dcst ?n)
  7352. (if (looking-at org-complex-heading-regexp)
  7353. (string-to-number (match-string 4))
  7354. nil))
  7355. ((= dcst ?a)
  7356. (if (looking-at org-complex-heading-regexp)
  7357. (funcall case-func (match-string 4))
  7358. nil))
  7359. ((= dcst ?t)
  7360. (let ((end (save-excursion (outline-next-heading) (point))))
  7361. (if (or (re-search-forward org-ts-regexp end t)
  7362. (re-search-forward org-ts-regexp-both end t))
  7363. (org-time-string-to-seconds (match-string 0))
  7364. (org-float-time now))))
  7365. ((= dcst ?c)
  7366. (let ((end (save-excursion (outline-next-heading) (point))))
  7367. (if (re-search-forward
  7368. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7369. end t)
  7370. (org-time-string-to-seconds (match-string 0))
  7371. (org-float-time now))))
  7372. ((= dcst ?s)
  7373. (let ((end (save-excursion (outline-next-heading) (point))))
  7374. (if (re-search-forward org-scheduled-time-regexp end t)
  7375. (org-time-string-to-seconds (match-string 1))
  7376. (org-float-time now))))
  7377. ((= dcst ?d)
  7378. (let ((end (save-excursion (outline-next-heading) (point))))
  7379. (if (re-search-forward org-deadline-time-regexp end t)
  7380. (org-time-string-to-seconds (match-string 1))
  7381. (org-float-time now))))
  7382. ((= dcst ?p)
  7383. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7384. (string-to-char (match-string 2))
  7385. org-default-priority))
  7386. ((= dcst ?r)
  7387. (or (org-entry-get nil property) ""))
  7388. ((= dcst ?o)
  7389. (if (looking-at org-complex-heading-regexp)
  7390. (- 9999 (length (member (match-string 2)
  7391. org-todo-keywords-1)))))
  7392. ((= dcst ?f)
  7393. (if getkey-func
  7394. (progn
  7395. (setq tmp (funcall getkey-func))
  7396. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7397. tmp)
  7398. (error "Invalid key function `%s'" getkey-func)))
  7399. (t (error "Invalid sorting type `%c'" sorting-type))))
  7400. nil
  7401. (cond
  7402. ((= dcst ?a) 'string<)
  7403. ((= dcst ?f) compare-func)
  7404. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7405. (t nil)))))
  7406. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7407. (message "Sorting entries...done")))
  7408. (defun org-do-sort (table what &optional with-case sorting-type)
  7409. "Sort TABLE of WHAT according to SORTING-TYPE.
  7410. The user will be prompted for the SORTING-TYPE if the call to this
  7411. function does not specify it. WHAT is only for the prompt, to indicate
  7412. what is being sorted. The sorting key will be extracted from
  7413. the car of the elements of the table.
  7414. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7415. (unless sorting-type
  7416. (message
  7417. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7418. what)
  7419. (setq sorting-type (read-char-exclusive)))
  7420. (let ((dcst (downcase sorting-type))
  7421. extractfun comparefun)
  7422. ;; Define the appropriate functions
  7423. (cond
  7424. ((= dcst ?n)
  7425. (setq extractfun 'string-to-number
  7426. comparefun (if (= dcst sorting-type) '< '>)))
  7427. ((= dcst ?a)
  7428. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7429. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7430. comparefun (if (= dcst sorting-type)
  7431. 'string<
  7432. (lambda (a b) (and (not (string< a b))
  7433. (not (string= a b)))))))
  7434. ((= dcst ?t)
  7435. (setq extractfun
  7436. (lambda (x)
  7437. (if (or (string-match org-ts-regexp x)
  7438. (string-match org-ts-regexp-both x))
  7439. (org-float-time
  7440. (org-time-string-to-time (match-string 0 x)))
  7441. 0))
  7442. comparefun (if (= dcst sorting-type) '< '>)))
  7443. (t (error "Invalid sorting type `%c'" sorting-type)))
  7444. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7445. table)
  7446. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7447. ;;; The orgstruct minor mode
  7448. ;; Define a minor mode which can be used in other modes in order to
  7449. ;; integrate the org-mode structure editing commands.
  7450. ;; This is really a hack, because the org-mode structure commands use
  7451. ;; keys which normally belong to the major mode. Here is how it
  7452. ;; works: The minor mode defines all the keys necessary to operate the
  7453. ;; structure commands, but wraps the commands into a function which
  7454. ;; tests if the cursor is currently at a headline or a plain list
  7455. ;; item. If that is the case, the structure command is used,
  7456. ;; temporarily setting many Org-mode variables like regular
  7457. ;; expressions for filling etc. However, when any of those keys is
  7458. ;; used at a different location, function uses `key-binding' to look
  7459. ;; up if the key has an associated command in another currently active
  7460. ;; keymap (minor modes, major mode, global), and executes that
  7461. ;; command. There might be problems if any of the keys is otherwise
  7462. ;; used as a prefix key.
  7463. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7464. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7465. ;; addresses this by checking explicitly for both bindings.
  7466. (defvar orgstruct-mode-map (make-sparse-keymap)
  7467. "Keymap for the minor `orgstruct-mode'.")
  7468. (defvar org-local-vars nil
  7469. "List of local variables, for use by `orgstruct-mode'.")
  7470. ;;;###autoload
  7471. (define-minor-mode orgstruct-mode
  7472. "Toggle the minor mode `orgstruct-mode'.
  7473. This mode is for using Org-mode structure commands in other
  7474. modes. The following keys behave as if Org-mode were active, if
  7475. the cursor is on a headline, or on a plain list item (both as
  7476. defined by Org-mode).
  7477. M-up Move entry/item up
  7478. M-down Move entry/item down
  7479. M-left Promote
  7480. M-right Demote
  7481. M-S-up Move entry/item up
  7482. M-S-down Move entry/item down
  7483. M-S-left Promote subtree
  7484. M-S-right Demote subtree
  7485. M-q Fill paragraph and items like in Org-mode
  7486. C-c ^ Sort entries
  7487. C-c - Cycle list bullet
  7488. TAB Cycle item visibility
  7489. M-RET Insert new heading/item
  7490. S-M-RET Insert new TODO heading / Checkbox item
  7491. C-c C-c Set tags / toggle checkbox"
  7492. nil " OrgStruct" nil
  7493. (org-load-modules-maybe)
  7494. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7495. ;;;###autoload
  7496. (defun turn-on-orgstruct ()
  7497. "Unconditionally turn on `orgstruct-mode'."
  7498. (orgstruct-mode 1))
  7499. (defun orgstruct++-mode (&optional arg)
  7500. "Toggle `orgstruct-mode', the enhanced version of it.
  7501. In addition to setting orgstruct-mode, this also exports all indentation
  7502. and autofilling variables from org-mode into the buffer. It will also
  7503. recognize item context in multiline items.
  7504. Note that turning off orgstruct-mode will *not* remove the
  7505. indentation/paragraph settings. This can only be done by refreshing the
  7506. major mode, for example with \\[normal-mode]."
  7507. (interactive "P")
  7508. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7509. (if (< arg 1)
  7510. (orgstruct-mode -1)
  7511. (orgstruct-mode 1)
  7512. (let (var val)
  7513. (mapc
  7514. (lambda (x)
  7515. (when (string-match
  7516. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7517. (symbol-name (car x)))
  7518. (setq var (car x) val (nth 1 x))
  7519. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7520. org-local-vars)
  7521. (org-set-local 'orgstruct-is-++ t))))
  7522. (defvar orgstruct-is-++ nil
  7523. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7524. (make-variable-buffer-local 'orgstruct-is-++)
  7525. ;;;###autoload
  7526. (defun turn-on-orgstruct++ ()
  7527. "Unconditionally turn on `orgstruct++-mode'."
  7528. (orgstruct++-mode 1))
  7529. (defun orgstruct-error ()
  7530. "Error when there is no default binding for a structure key."
  7531. (interactive)
  7532. (error "This key has no function outside structure elements"))
  7533. (defun orgstruct-setup ()
  7534. "Setup orgstruct keymaps."
  7535. (let ((nfunc 0)
  7536. (bindings
  7537. (list
  7538. '([(meta up)] org-metaup)
  7539. '([(meta down)] org-metadown)
  7540. '([(meta left)] org-metaleft)
  7541. '([(meta right)] org-metaright)
  7542. '([(meta shift up)] org-shiftmetaup)
  7543. '([(meta shift down)] org-shiftmetadown)
  7544. '([(meta shift left)] org-shiftmetaleft)
  7545. '([(meta shift right)] org-shiftmetaright)
  7546. '([?\e (up)] org-metaup)
  7547. '([?\e (down)] org-metadown)
  7548. '([?\e (left)] org-metaleft)
  7549. '([?\e (right)] org-metaright)
  7550. '([?\e (shift up)] org-shiftmetaup)
  7551. '([?\e (shift down)] org-shiftmetadown)
  7552. '([?\e (shift left)] org-shiftmetaleft)
  7553. '([?\e (shift right)] org-shiftmetaright)
  7554. '([(shift up)] org-shiftup)
  7555. '([(shift down)] org-shiftdown)
  7556. '([(shift left)] org-shiftleft)
  7557. '([(shift right)] org-shiftright)
  7558. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7559. '("\M-q" fill-paragraph)
  7560. '("\C-c^" org-sort)
  7561. '("\C-c-" org-cycle-list-bullet)))
  7562. elt key fun cmd)
  7563. (while (setq elt (pop bindings))
  7564. (setq nfunc (1+ nfunc))
  7565. (setq key (org-key (car elt))
  7566. fun (nth 1 elt)
  7567. cmd (orgstruct-make-binding fun nfunc key))
  7568. (org-defkey orgstruct-mode-map key cmd))
  7569. ;; Special treatment needed for TAB and RET
  7570. (org-defkey orgstruct-mode-map [(tab)]
  7571. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7572. (org-defkey orgstruct-mode-map "\C-i"
  7573. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7574. (org-defkey orgstruct-mode-map "\M-\C-m"
  7575. (orgstruct-make-binding 'org-insert-heading 105
  7576. "\M-\C-m" [(meta return)]))
  7577. (org-defkey orgstruct-mode-map [(meta return)]
  7578. (orgstruct-make-binding 'org-insert-heading 106
  7579. [(meta return)] "\M-\C-m"))
  7580. (org-defkey orgstruct-mode-map [(shift meta return)]
  7581. (orgstruct-make-binding 'org-insert-todo-heading 107
  7582. [(meta return)] "\M-\C-m"))
  7583. (org-defkey orgstruct-mode-map "\e\C-m"
  7584. (orgstruct-make-binding 'org-insert-heading 108
  7585. "\e\C-m" [?\e (return)]))
  7586. (org-defkey orgstruct-mode-map [?\e (return)]
  7587. (orgstruct-make-binding 'org-insert-heading 109
  7588. [?\e (return)] "\e\C-m"))
  7589. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7590. (orgstruct-make-binding 'org-insert-todo-heading 110
  7591. [?\e (return)] "\e\C-m"))
  7592. (unless org-local-vars
  7593. (setq org-local-vars (org-get-local-variables)))
  7594. t))
  7595. (defun orgstruct-make-binding (fun n &rest keys)
  7596. "Create a function for binding in the structure minor mode.
  7597. FUN is the command to call inside a table. N is used to create a unique
  7598. command name. KEYS are keys that should be checked in for a command
  7599. to execute outside of tables."
  7600. (eval
  7601. (list 'defun
  7602. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7603. '(arg)
  7604. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7605. "Outside of structure, run the binding of `"
  7606. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7607. "'.")
  7608. '(interactive "p")
  7609. (list 'if
  7610. `(org-context-p 'headline 'item
  7611. (and orgstruct-is-++
  7612. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7613. 'item-body))
  7614. (list 'org-run-like-in-org-mode (list 'quote fun))
  7615. (list 'let '(orgstruct-mode)
  7616. (list 'call-interactively
  7617. (append '(or)
  7618. (mapcar (lambda (k)
  7619. (list 'key-binding k))
  7620. keys)
  7621. '('orgstruct-error))))))))
  7622. (defun org-context-p (&rest contexts)
  7623. "Check if local context is any of CONTEXTS.
  7624. Possible values in the list of contexts are `table', `headline', and `item'."
  7625. (let ((pos (point)))
  7626. (goto-char (point-at-bol))
  7627. (prog1 (or (and (memq 'table contexts)
  7628. (looking-at "[ \t]*|"))
  7629. (and (memq 'headline contexts)
  7630. (looking-at org-outline-regexp))
  7631. (and (memq 'item contexts)
  7632. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7633. (and (memq 'item-body contexts)
  7634. (org-in-item-p)))
  7635. (goto-char pos))))
  7636. (defun org-get-local-variables ()
  7637. "Return a list of all local variables in an org-mode buffer."
  7638. (let (varlist)
  7639. (with-current-buffer (get-buffer-create "*Org tmp*")
  7640. (erase-buffer)
  7641. (org-mode)
  7642. (setq varlist (buffer-local-variables)))
  7643. (kill-buffer "*Org tmp*")
  7644. (delq nil
  7645. (mapcar
  7646. (lambda (x)
  7647. (setq x
  7648. (if (symbolp x)
  7649. (list x)
  7650. (list (car x) (list 'quote (cdr x)))))
  7651. (if (string-match
  7652. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7653. (symbol-name (car x)))
  7654. x nil))
  7655. varlist))))
  7656. (defun org-clone-local-variables (from-buffer &optional regexp)
  7657. "Clone local variables from FROM-BUFFER.
  7658. Optional argument REGEXP selects variables to clone."
  7659. (mapc
  7660. (lambda (pair)
  7661. (and (symbolp (car pair))
  7662. (or (null regexp)
  7663. (string-match regexp (symbol-name (car pair))))
  7664. (set (make-local-variable (car pair))
  7665. (cdr pair))))
  7666. (buffer-local-variables from-buffer)))
  7667. ;;;###autoload
  7668. (defun org-run-like-in-org-mode (cmd)
  7669. "Run a command, pretending that the current buffer is in Org-mode.
  7670. This will temporarily bind local variables that are typically bound in
  7671. Org-mode to the values they have in Org-mode, and then interactively
  7672. call CMD."
  7673. (org-load-modules-maybe)
  7674. (unless org-local-vars
  7675. (setq org-local-vars (org-get-local-variables)))
  7676. (eval (list 'let org-local-vars
  7677. (list 'call-interactively (list 'quote cmd)))))
  7678. ;;;; Archiving
  7679. (defun org-get-category (&optional pos force-refresh)
  7680. "Get the category applying to position POS."
  7681. (save-match-data
  7682. (if force-refresh (org-refresh-category-properties))
  7683. (let ((pos (or pos (point))))
  7684. (or (get-text-property pos 'org-category)
  7685. (progn (org-refresh-category-properties)
  7686. (get-text-property pos 'org-category))))))
  7687. (defun org-refresh-category-properties ()
  7688. "Refresh category text properties in the buffer."
  7689. (let ((def-cat (cond
  7690. ((null org-category)
  7691. (if buffer-file-name
  7692. (file-name-sans-extension
  7693. (file-name-nondirectory buffer-file-name))
  7694. "???"))
  7695. ((symbolp org-category) (symbol-name org-category))
  7696. (t org-category)))
  7697. beg end cat pos optionp)
  7698. (org-unmodified
  7699. (save-excursion
  7700. (save-restriction
  7701. (widen)
  7702. (goto-char (point-min))
  7703. (put-text-property (point) (point-max) 'org-category def-cat)
  7704. (while (re-search-forward
  7705. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7706. (setq pos (match-end 0)
  7707. optionp (equal (char-after (match-beginning 0)) ?#)
  7708. cat (org-trim (match-string 2)))
  7709. (if optionp
  7710. (setq beg (point-at-bol) end (point-max))
  7711. (org-back-to-heading t)
  7712. (setq beg (point) end (org-end-of-subtree t t)))
  7713. (put-text-property beg end 'org-category cat)
  7714. (put-text-property beg end 'org-category-position beg)
  7715. (goto-char pos)))))))
  7716. ;;;; Link Stuff
  7717. ;;; Link abbreviations
  7718. (defun org-link-expand-abbrev (link)
  7719. "Apply replacements as defined in `org-link-abbrev-alist."
  7720. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7721. (let* ((key (match-string 1 link))
  7722. (as (or (assoc key org-link-abbrev-alist-local)
  7723. (assoc key org-link-abbrev-alist)))
  7724. (tag (and (match-end 2) (match-string 3 link)))
  7725. rpl)
  7726. (if (not as)
  7727. link
  7728. (setq rpl (cdr as))
  7729. (cond
  7730. ((symbolp rpl) (funcall rpl tag))
  7731. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7732. ((string-match "%h" rpl)
  7733. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7734. (t (concat rpl tag)))))
  7735. link))
  7736. ;;; Storing and inserting links
  7737. (defvar org-insert-link-history nil
  7738. "Minibuffer history for links inserted with `org-insert-link'.")
  7739. (defvar org-stored-links nil
  7740. "Contains the links stored with `org-store-link'.")
  7741. (defvar org-store-link-plist nil
  7742. "Plist with info about the most recently link created with `org-store-link'.")
  7743. (defvar org-link-protocols nil
  7744. "Link protocols added to Org-mode using `org-add-link-type'.")
  7745. (defvar org-store-link-functions nil
  7746. "List of functions that are called to create and store a link.
  7747. Each function will be called in turn until one returns a non-nil
  7748. value. Each function should check if it is responsible for creating
  7749. this link (for example by looking at the major mode).
  7750. If not, it must exit and return nil.
  7751. If yes, it should return a non-nil value after a calling
  7752. `org-store-link-props' with a list of properties and values.
  7753. Special properties are:
  7754. :type The link prefix, like \"http\". This must be given.
  7755. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7756. This is obligatory as well.
  7757. :description Optional default description for the second pair
  7758. of brackets in an Org-mode link. The user can still change
  7759. this when inserting this link into an Org-mode buffer.
  7760. In addition to these, any additional properties can be specified
  7761. and then used in remember templates.")
  7762. (defun org-add-link-type (type &optional follow export)
  7763. "Add TYPE to the list of `org-link-types'.
  7764. Re-compute all regular expressions depending on `org-link-types'
  7765. FOLLOW and EXPORT are two functions.
  7766. FOLLOW should take the link path as the single argument and do whatever
  7767. is necessary to follow the link, for example find a file or display
  7768. a mail message.
  7769. EXPORT should format the link path for export to one of the export formats.
  7770. It should be a function accepting three arguments:
  7771. path the path of the link, the text after the prefix (like \"http:\")
  7772. desc the description of the link, if any, or a description added by
  7773. org-export-normalize-links if there is none
  7774. format the export format, a symbol like `html' or `latex' or `ascii'..
  7775. The function may use the FORMAT information to return different values
  7776. depending on the format. The return value will be put literally into
  7777. the exported file. If the return value is nil, this means Org should
  7778. do what it normally does with links which do not have EXPORT defined.
  7779. Org-mode has a built-in default for exporting links. If you are happy with
  7780. this default, there is no need to define an export function for the link
  7781. type. For a simple example of an export function, see `org-bbdb.el'."
  7782. (add-to-list 'org-link-types type t)
  7783. (org-make-link-regexps)
  7784. (if (assoc type org-link-protocols)
  7785. (setcdr (assoc type org-link-protocols) (list follow export))
  7786. (push (list type follow export) org-link-protocols)))
  7787. (defvar org-agenda-buffer-name)
  7788. ;;;###autoload
  7789. (defun org-store-link (arg)
  7790. "\\<org-mode-map>Store an org-link to the current location.
  7791. This link is added to `org-stored-links' and can later be inserted
  7792. into an org-buffer with \\[org-insert-link].
  7793. For some link types, a prefix arg is interpreted:
  7794. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7795. For file links, arg negates `org-context-in-file-links'."
  7796. (interactive "P")
  7797. (org-load-modules-maybe)
  7798. (setq org-store-link-plist nil) ; reset
  7799. (org-with-limited-levels
  7800. (let (link cpltxt desc description search txt custom-id agenda-link)
  7801. (cond
  7802. ((run-hook-with-args-until-success 'org-store-link-functions)
  7803. (setq link (plist-get org-store-link-plist :link)
  7804. desc (or (plist-get org-store-link-plist :description) link)))
  7805. ((org-src-edit-buffer-p)
  7806. (let (label gc)
  7807. (while (or (not label)
  7808. (save-excursion
  7809. (save-restriction
  7810. (widen)
  7811. (goto-char (point-min))
  7812. (re-search-forward
  7813. (regexp-quote (format org-coderef-label-format label))
  7814. nil t))))
  7815. (when label (message "Label exists already") (sit-for 2))
  7816. (setq label (read-string "Code line label: " label)))
  7817. (end-of-line 1)
  7818. (setq link (format org-coderef-label-format label))
  7819. (setq gc (- 79 (length link)))
  7820. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7821. (insert link)
  7822. (setq link (concat "(" label ")") desc nil)))
  7823. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7824. ;; We are in the agenda, link to referenced location
  7825. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7826. (get-text-property (point) 'org-marker))))
  7827. (when m
  7828. (org-with-point-at m
  7829. (setq agenda-link
  7830. (if (org-called-interactively-p 'any)
  7831. (call-interactively 'org-store-link)
  7832. (org-store-link nil)))))))
  7833. ((eq major-mode 'calendar-mode)
  7834. (let ((cd (calendar-cursor-to-date)))
  7835. (setq link
  7836. (format-time-string
  7837. (car org-time-stamp-formats)
  7838. (apply 'encode-time
  7839. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7840. nil nil nil))))
  7841. (org-store-link-props :type "calendar" :date cd)))
  7842. ((eq major-mode 'w3-mode)
  7843. (setq cpltxt (if (and (buffer-name)
  7844. (not (string-match "Untitled" (buffer-name))))
  7845. (buffer-name)
  7846. (url-view-url t))
  7847. link (org-make-link (url-view-url t)))
  7848. (org-store-link-props :type "w3" :url (url-view-url t)))
  7849. ((eq major-mode 'w3m-mode)
  7850. (setq cpltxt (or w3m-current-title w3m-current-url)
  7851. link (org-make-link w3m-current-url))
  7852. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7853. ((setq search (run-hook-with-args-until-success
  7854. 'org-create-file-search-functions))
  7855. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7856. "::" search))
  7857. (setq cpltxt (or description link)))
  7858. ((eq major-mode 'image-mode)
  7859. (setq cpltxt (concat "file:"
  7860. (abbreviate-file-name buffer-file-name))
  7861. link (org-make-link cpltxt))
  7862. (org-store-link-props :type "image" :file buffer-file-name))
  7863. ((eq major-mode 'dired-mode)
  7864. ;; link to the file in the current line
  7865. (let ((file (dired-get-filename nil t)))
  7866. (setq file (if file
  7867. (abbreviate-file-name
  7868. (expand-file-name (dired-get-filename nil t)))
  7869. ;; otherwise, no file so use current directory.
  7870. default-directory))
  7871. (setq cpltxt (concat "file:" file)
  7872. link (org-make-link cpltxt))))
  7873. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7874. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7875. (cond
  7876. ((org-in-regexp "<<\\(.*?\\)>>")
  7877. (setq cpltxt
  7878. (concat "file:"
  7879. (abbreviate-file-name
  7880. (buffer-file-name (buffer-base-buffer)))
  7881. "::" (match-string 1))
  7882. link (org-make-link cpltxt)))
  7883. ((and (featurep 'org-id)
  7884. (or (eq org-link-to-org-use-id t)
  7885. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7886. (org-called-interactively-p 'any))
  7887. (and (eq org-link-to-org-use-id
  7888. 'create-if-interactive-and-no-custom-id)
  7889. (org-called-interactively-p 'any)
  7890. (not custom-id))
  7891. (and org-link-to-org-use-id
  7892. (org-entry-get nil "ID"))))
  7893. ;; We can make a link using the ID.
  7894. (setq link (condition-case nil
  7895. (prog1 (org-id-store-link)
  7896. (setq desc (plist-get org-store-link-plist
  7897. :description)))
  7898. (error
  7899. ;; probably before first headline, link to file only
  7900. (concat "file:"
  7901. (abbreviate-file-name
  7902. (buffer-file-name (buffer-base-buffer))))))))
  7903. (t
  7904. ;; Just link to current headline
  7905. (setq cpltxt (concat "file:"
  7906. (abbreviate-file-name
  7907. (buffer-file-name (buffer-base-buffer)))))
  7908. ;; Add a context search string
  7909. (when (org-xor org-context-in-file-links arg)
  7910. (setq txt (cond
  7911. ((org-at-heading-p) nil)
  7912. ((org-region-active-p)
  7913. (buffer-substring (region-beginning) (region-end)))
  7914. (t nil)))
  7915. (when (or (null txt) (string-match "\\S-" txt))
  7916. (setq cpltxt
  7917. (concat cpltxt "::"
  7918. (condition-case nil
  7919. (org-make-org-heading-search-string txt)
  7920. (error "")))
  7921. desc (or (nth 4 (ignore-errors
  7922. (org-heading-components))) "NONE"))))
  7923. (if (string-match "::\\'" cpltxt)
  7924. (setq cpltxt (substring cpltxt 0 -2)))
  7925. (setq link (org-make-link cpltxt)))))
  7926. ((buffer-file-name (buffer-base-buffer))
  7927. ;; Just link to this file here.
  7928. (setq cpltxt (concat "file:"
  7929. (abbreviate-file-name
  7930. (buffer-file-name (buffer-base-buffer)))))
  7931. ;; Add a context string
  7932. (when (org-xor org-context-in-file-links arg)
  7933. (setq txt (if (org-region-active-p)
  7934. (buffer-substring (region-beginning) (region-end))
  7935. (buffer-substring (point-at-bol) (point-at-eol))))
  7936. ;; Only use search option if there is some text.
  7937. (when (string-match "\\S-" txt)
  7938. (setq cpltxt
  7939. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7940. desc "NONE")))
  7941. (setq link (org-make-link cpltxt)))
  7942. ((org-called-interactively-p 'interactive)
  7943. (error "Cannot link to a buffer which is not visiting a file"))
  7944. (t (setq link nil)))
  7945. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7946. (setq link (or link cpltxt)
  7947. desc (or desc cpltxt))
  7948. (if (equal desc "NONE") (setq desc nil))
  7949. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7950. (progn
  7951. (setq org-stored-links
  7952. (cons (list link desc) org-stored-links))
  7953. (message "Stored: %s" (or desc link))
  7954. (when custom-id
  7955. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7956. "::#" custom-id))
  7957. (setq org-stored-links
  7958. (cons (list link desc) org-stored-links))))
  7959. (or agenda-link (and link (org-make-link-string link desc)))))))
  7960. (defun org-store-link-props (&rest plist)
  7961. "Store link properties, extract names and addresses."
  7962. (let (x adr)
  7963. (when (setq x (plist-get plist :from))
  7964. (setq adr (mail-extract-address-components x))
  7965. (setq plist (plist-put plist :fromname (car adr)))
  7966. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7967. (when (setq x (plist-get plist :to))
  7968. (setq adr (mail-extract-address-components x))
  7969. (setq plist (plist-put plist :toname (car adr)))
  7970. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7971. (let ((from (plist-get plist :from))
  7972. (to (plist-get plist :to)))
  7973. (when (and from to org-from-is-user-regexp)
  7974. (setq plist
  7975. (plist-put plist :fromto
  7976. (if (string-match org-from-is-user-regexp from)
  7977. (concat "to %t")
  7978. (concat "from %f"))))))
  7979. (setq org-store-link-plist plist))
  7980. (defun org-add-link-props (&rest plist)
  7981. "Add these properties to the link property list."
  7982. (let (key value)
  7983. (while plist
  7984. (setq key (pop plist) value (pop plist))
  7985. (setq org-store-link-plist
  7986. (plist-put org-store-link-plist key value)))))
  7987. (defun org-email-link-description (&optional fmt)
  7988. "Return the description part of an email link.
  7989. This takes information from `org-store-link-plist' and formats it
  7990. according to FMT (default from `org-email-link-description-format')."
  7991. (setq fmt (or fmt org-email-link-description-format))
  7992. (let* ((p org-store-link-plist)
  7993. (to (plist-get p :toaddress))
  7994. (from (plist-get p :fromaddress))
  7995. (table
  7996. (list
  7997. (cons "%c" (plist-get p :fromto))
  7998. (cons "%F" (plist-get p :from))
  7999. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8000. (cons "%T" (plist-get p :to))
  8001. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8002. (cons "%s" (plist-get p :subject))
  8003. (cons "%d" (plist-get p :date))
  8004. (cons "%m" (plist-get p :message-id)))))
  8005. (when (string-match "%c" fmt)
  8006. ;; Check if the user wrote this message
  8007. (if (and org-from-is-user-regexp from to
  8008. (save-match-data (string-match org-from-is-user-regexp from)))
  8009. (setq fmt (replace-match "to %t" t t fmt))
  8010. (setq fmt (replace-match "from %f" t t fmt))))
  8011. (org-replace-escapes fmt table)))
  8012. (defun org-make-org-heading-search-string (&optional string heading)
  8013. "Make search string for STRING or current headline."
  8014. (interactive)
  8015. (let ((s (or string (org-get-heading)))
  8016. (lines org-context-in-file-links))
  8017. (unless (and string (not heading))
  8018. ;; We are using a headline, clean up garbage in there.
  8019. (if (string-match org-todo-regexp s)
  8020. (setq s (replace-match "" t t s)))
  8021. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8022. (setq s (replace-match "" t t s)))
  8023. (setq s (org-trim s))
  8024. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8025. org-comment-string "\\)") s)
  8026. (setq s (replace-match "" t t s)))
  8027. (while (string-match org-ts-regexp s)
  8028. (setq s (replace-match "" t t s))))
  8029. (or string (setq s (concat "*" s))) ; Add * for headlines
  8030. (when (and string (integerp lines) (> lines 0))
  8031. (let ((slines (org-split-string s "\n")))
  8032. (when (< lines (length slines))
  8033. (setq s (mapconcat
  8034. 'identity
  8035. (reverse (nthcdr (- (length slines) lines)
  8036. (reverse slines))) "\n")))))
  8037. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8038. (defun org-make-link (&rest strings)
  8039. "Concatenate STRINGS."
  8040. (apply 'concat strings))
  8041. (defun org-make-link-string (link &optional description)
  8042. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8043. (unless (string-match "\\S-" link)
  8044. (error "Empty link"))
  8045. (when (and description
  8046. (stringp description)
  8047. (not (string-match "\\S-" description)))
  8048. (setq description nil))
  8049. (when (stringp description)
  8050. ;; Remove brackets from the description, they are fatal.
  8051. (while (string-match "\\[" description)
  8052. (setq description (replace-match "{" t t description)))
  8053. (while (string-match "\\]" description)
  8054. (setq description (replace-match "}" t t description))))
  8055. (when (equal link description)
  8056. ;; No description needed, it is identical
  8057. (setq description nil))
  8058. (when (and (not description)
  8059. (not (string-match (org-image-file-name-regexp) link))
  8060. (not (equal link (org-link-escape link))))
  8061. (setq description (org-extract-attributes link)))
  8062. (setq link
  8063. (cond ((string-match (org-image-file-name-regexp) link) link)
  8064. ((string-match org-link-types-re link)
  8065. (concat (match-string 1 link)
  8066. (org-link-escape (substring link (match-end 1)))))
  8067. (t (org-link-escape link))))
  8068. (concat "[[" link "]"
  8069. (if description (concat "[" description "]") "")
  8070. "]"))
  8071. (defconst org-link-escape-chars
  8072. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8073. "List of characters that should be escaped in link.
  8074. This is the list that is used for internal purposes.")
  8075. (defvar org-url-encoding-use-url-hexify nil)
  8076. (defconst org-link-escape-chars-browser
  8077. '(?\ )
  8078. "List of escapes for characters that are problematic in links.
  8079. This is the list that is used before handing over to the browser.")
  8080. (defun org-link-escape (text &optional table merge)
  8081. "Return percent escaped representation of TEXT.
  8082. TEXT is a string with the text to escape.
  8083. Optional argument TABLE is a list with characters that should be
  8084. escaped. When nil, `org-link-escape-chars' is used.
  8085. If optional argument MERGE is set, merge TABLE into
  8086. `org-link-escape-chars'."
  8087. (if (and org-url-encoding-use-url-hexify (not table))
  8088. (url-hexify-string text)
  8089. (cond
  8090. ((and table merge)
  8091. (mapc (lambda (defchr)
  8092. (unless (member defchr table)
  8093. (setq table (cons defchr table)))) org-link-escape-chars))
  8094. ((null table)
  8095. (setq table org-link-escape-chars)))
  8096. (mapconcat
  8097. (lambda (char)
  8098. (if (or (member char table)
  8099. (< char 32) (= char 37) (> char 126))
  8100. (mapconcat (lambda (sequence-element)
  8101. (format "%%%.2X" sequence-element))
  8102. (or (encode-coding-char char 'utf-8)
  8103. (error "Unable to percent escape character: %s"
  8104. (char-to-string char))) "")
  8105. (char-to-string char))) text "")))
  8106. (defun org-link-unescape (str)
  8107. "Unhex hexified Unicode strings as returned from the JavaScript function
  8108. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8109. (unless (and (null str) (string= "" str))
  8110. (let ((pos 0) (case-fold-search t) unhexed)
  8111. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8112. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8113. (setq str (replace-match unhexed t t str))
  8114. (setq pos (+ pos (length unhexed))))))
  8115. str)
  8116. (defun org-link-unescape-compound (hex)
  8117. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8118. Note: this function also decodes single byte encodings like
  8119. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8120. (save-match-data
  8121. (let* ((bytes (cdr (split-string hex "%")))
  8122. (ret "")
  8123. (eat 0)
  8124. (sum 0))
  8125. (while bytes
  8126. (let* ((val (string-to-number (pop bytes) 16))
  8127. (shift-xor
  8128. (if (= 0 eat)
  8129. (cond
  8130. ((>= val 252) (cons 6 252))
  8131. ((>= val 248) (cons 5 248))
  8132. ((>= val 240) (cons 4 240))
  8133. ((>= val 224) (cons 3 224))
  8134. ((>= val 192) (cons 2 192))
  8135. (t (cons 0 0)))
  8136. (cons 6 128))))
  8137. (if (>= val 192) (setq eat (car shift-xor)))
  8138. (setq val (logxor val (cdr shift-xor)))
  8139. (setq sum (+ (lsh sum (car shift-xor)) val))
  8140. (if (> eat 0) (setq eat (- eat 1)))
  8141. (cond
  8142. ((= 0 eat) ;multi byte
  8143. (setq ret (concat ret (org-char-to-string sum)))
  8144. (setq sum 0))
  8145. ((not bytes) ; single byte(s)
  8146. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8147. )) ;; end (while bytes
  8148. ret )))
  8149. (defun org-link-unescape-single-byte-sequence (hex)
  8150. "Unhexify hex-encoded single byte character sequences."
  8151. (mapconcat (lambda (byte)
  8152. (char-to-string (string-to-number byte 16)))
  8153. (cdr (split-string hex "%")) ""))
  8154. (defun org-xor (a b)
  8155. "Exclusive or."
  8156. (if a (not b) b))
  8157. (defun org-fixup-message-id-for-http (s)
  8158. "Replace special characters in a message id, so it can be used in an http query."
  8159. (when (string-match "%" s)
  8160. (setq s (mapconcat (lambda (c)
  8161. (if (eq c ?%)
  8162. "%25"
  8163. (char-to-string c)))
  8164. s "")))
  8165. (while (string-match "<" s)
  8166. (setq s (replace-match "%3C" t t s)))
  8167. (while (string-match ">" s)
  8168. (setq s (replace-match "%3E" t t s)))
  8169. (while (string-match "@" s)
  8170. (setq s (replace-match "%40" t t s)))
  8171. s)
  8172. (defun org-link-prettify (link)
  8173. "Return a human-readable representation of LINK.
  8174. The car of LINK must be a raw link the cdr of LINK must be either
  8175. a link description or nil."
  8176. (let ((desc (or (cadr link) "<no description>")))
  8177. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8178. "[[" (car link) "]]")))
  8179. ;;;###autoload
  8180. (defun org-insert-link-global ()
  8181. "Insert a link like Org-mode does.
  8182. This command can be called in any mode to insert a link in Org-mode syntax."
  8183. (interactive)
  8184. (org-load-modules-maybe)
  8185. (org-run-like-in-org-mode 'org-insert-link))
  8186. (defun org-insert-link (&optional complete-file link-location default-description)
  8187. "Insert a link. At the prompt, enter the link.
  8188. Completion can be used to insert any of the link protocol prefixes like
  8189. http or ftp in use.
  8190. The history can be used to select a link previously stored with
  8191. `org-store-link'. When the empty string is entered (i.e. if you just
  8192. press RET at the prompt), the link defaults to the most recently
  8193. stored link. As SPC triggers completion in the minibuffer, you need to
  8194. use M-SPC or C-q SPC to force the insertion of a space character.
  8195. You will also be prompted for a description, and if one is given, it will
  8196. be displayed in the buffer instead of the link.
  8197. If there is already a link at point, this command will allow you to edit link
  8198. and description parts.
  8199. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8200. be selected using completion. The path to the file will be relative to the
  8201. current directory if the file is in the current directory or a subdirectory.
  8202. Otherwise, the link will be the absolute path as completed in the minibuffer
  8203. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8204. option `org-link-file-path-type'.
  8205. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8206. the current directory or below.
  8207. With three \\[universal-argument] prefixes, negate the meaning of
  8208. `org-keep-stored-link-after-insertion'.
  8209. If `org-make-link-description-function' is non-nil, this function will be
  8210. called with the link target, and the result will be the default
  8211. link description.
  8212. If the LINK-LOCATION parameter is non-nil, this value will be
  8213. used as the link location instead of reading one interactively.
  8214. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8215. be used as the default description."
  8216. (interactive "P")
  8217. (let* ((wcf (current-window-configuration))
  8218. (region (if (org-region-active-p)
  8219. (buffer-substring (region-beginning) (region-end))))
  8220. (remove (and region (list (region-beginning) (region-end))))
  8221. (desc region)
  8222. tmphist ; byte-compile incorrectly complains about this
  8223. (link link-location)
  8224. entry file all-prefixes)
  8225. (cond
  8226. (link-location) ; specified by arg, just use it.
  8227. ((org-in-regexp org-bracket-link-regexp 1)
  8228. ;; We do have a link at point, and we are going to edit it.
  8229. (setq remove (list (match-beginning 0) (match-end 0)))
  8230. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8231. (setq link (read-string "Link: "
  8232. (org-link-unescape
  8233. (org-match-string-no-properties 1)))))
  8234. ((or (org-in-regexp org-angle-link-re)
  8235. (org-in-regexp org-plain-link-re))
  8236. ;; Convert to bracket link
  8237. (setq remove (list (match-beginning 0) (match-end 0))
  8238. link (read-string "Link: "
  8239. (org-remove-angle-brackets (match-string 0)))))
  8240. ((member complete-file '((4) (16)))
  8241. ;; Completing read for file names.
  8242. (setq link (org-file-complete-link complete-file)))
  8243. (t
  8244. ;; Read link, with completion for stored links.
  8245. (with-output-to-temp-buffer "*Org Links*"
  8246. (princ "Insert a link.
  8247. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8248. (when org-stored-links
  8249. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8250. (princ (mapconcat 'org-link-prettify
  8251. (reverse org-stored-links) "\n"))))
  8252. (let ((cw (selected-window)))
  8253. (select-window (get-buffer-window "*Org Links*" 'visible))
  8254. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8255. (unless (pos-visible-in-window-p (point-max))
  8256. (org-fit-window-to-buffer))
  8257. (and (window-live-p cw) (select-window cw)))
  8258. ;; Fake a link history, containing the stored links.
  8259. (setq tmphist (append (mapcar 'org-link-prettify org-stored-links)
  8260. org-insert-link-history))
  8261. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8262. (mapcar 'car org-link-abbrev-alist)
  8263. org-link-types))
  8264. (unwind-protect
  8265. (progn
  8266. (setq link
  8267. (let ((org-completion-use-ido nil)
  8268. (org-completion-use-iswitchb nil))
  8269. (org-completing-read
  8270. "Link: "
  8271. (append
  8272. (mapcar (lambda (x) (list (concat x ":")))
  8273. all-prefixes)
  8274. (mapcar 'car org-stored-links))
  8275. nil nil nil
  8276. 'tmphist
  8277. (car (car org-stored-links)))))
  8278. (if (not (string-match "\\S-" link))
  8279. (error "No link selected"))
  8280. (if (or (member link all-prefixes)
  8281. (and (equal ":" (substring link -1))
  8282. (member (substring link 0 -1) all-prefixes)
  8283. (setq link (substring link 0 -1))))
  8284. (setq link (org-link-try-special-completion link))))
  8285. (set-window-configuration wcf)
  8286. (kill-buffer "*Org Links*"))
  8287. (setq entry (assoc link org-stored-links))
  8288. (or entry (push link org-insert-link-history))
  8289. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8290. (not org-keep-stored-link-after-insertion))
  8291. (setq org-stored-links (delq (assoc link org-stored-links)
  8292. org-stored-links)))
  8293. (setq desc (or desc (nth 1 entry)))))
  8294. (if (string-match org-plain-link-re link)
  8295. ;; URL-like link, normalize the use of angular brackets.
  8296. (setq link (org-make-link (org-remove-angle-brackets link))))
  8297. ;; Check if we are linking to the current file with a search option
  8298. ;; If yes, simplify the link by using only the search option.
  8299. (when (and buffer-file-name
  8300. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8301. (let* ((path (match-string 1 link))
  8302. (case-fold-search nil)
  8303. (search (match-string 2 link)))
  8304. (save-match-data
  8305. (if (equal (file-truename buffer-file-name) (file-truename path))
  8306. ;; We are linking to this same file, with a search option
  8307. (setq link search)))))
  8308. ;; Check if we can/should use a relative path. If yes, simplify the link
  8309. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8310. (let* ((type (match-string 1 link))
  8311. (path (match-string 2 link))
  8312. (origpath path)
  8313. (case-fold-search nil))
  8314. (cond
  8315. ((or (eq org-link-file-path-type 'absolute)
  8316. (equal complete-file '(16)))
  8317. (setq path (abbreviate-file-name (expand-file-name path))))
  8318. ((eq org-link-file-path-type 'noabbrev)
  8319. (setq path (expand-file-name path)))
  8320. ((eq org-link-file-path-type 'relative)
  8321. (setq path (file-relative-name path)))
  8322. (t
  8323. (save-match-data
  8324. (if (string-match (concat "^" (regexp-quote
  8325. (expand-file-name
  8326. (file-name-as-directory
  8327. default-directory))))
  8328. (expand-file-name path))
  8329. ;; We are linking a file with relative path name.
  8330. (setq path (substring (expand-file-name path)
  8331. (match-end 0)))
  8332. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8333. (setq link (concat type path))
  8334. (if (equal desc origpath)
  8335. (setq desc path))))
  8336. (if org-make-link-description-function
  8337. (setq desc (funcall org-make-link-description-function link desc))
  8338. (if default-description (setq desc default-description)))
  8339. (setq desc (read-string "Description: " desc))
  8340. (unless (string-match "\\S-" desc) (setq desc nil))
  8341. (if remove (apply 'delete-region remove))
  8342. (insert (org-make-link-string link desc))))
  8343. (defun org-link-try-special-completion (type)
  8344. "If there is completion support for link type TYPE, offer it."
  8345. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8346. (if (functionp fun)
  8347. (funcall fun)
  8348. (read-string "Link (no completion support): " (concat type ":")))))
  8349. (defun org-file-complete-link (&optional arg)
  8350. "Create a file link using completion."
  8351. (let (file link)
  8352. (setq file (read-file-name "File: "))
  8353. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8354. (pwd1 (file-name-as-directory (abbreviate-file-name
  8355. (expand-file-name ".")))))
  8356. (cond
  8357. ((equal arg '(16))
  8358. (setq link (org-make-link
  8359. "file:"
  8360. (abbreviate-file-name (expand-file-name file)))))
  8361. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8362. (setq link (org-make-link "file:" (match-string 1 file))))
  8363. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8364. (expand-file-name file))
  8365. (setq link (org-make-link
  8366. "file:" (match-string 1 (expand-file-name file)))))
  8367. (t (setq link (org-make-link "file:" file)))))
  8368. link))
  8369. (defun org-completing-read (&rest args)
  8370. "Completing-read with SPACE being a normal character."
  8371. (let ((enable-recursive-minibuffers t)
  8372. (minibuffer-local-completion-map
  8373. (copy-keymap minibuffer-local-completion-map)))
  8374. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8375. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8376. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8377. (apply 'org-icompleting-read args)))
  8378. (defun org-completing-read-no-i (&rest args)
  8379. (let (org-completion-use-ido org-completion-use-iswitchb)
  8380. (apply 'org-completing-read args)))
  8381. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8382. "Use iswitch as a completing-read replacement to choose from choices.
  8383. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8384. from."
  8385. (let* ((iswitchb-use-virtual-buffers nil)
  8386. (iswitchb-make-buflist-hook
  8387. (lambda ()
  8388. (setq iswitchb-temp-buflist choices))))
  8389. (iswitchb-read-buffer prompt)))
  8390. (defun org-icompleting-read (&rest args)
  8391. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8392. (org-without-partial-completion
  8393. (if (and org-completion-use-ido
  8394. (fboundp 'ido-completing-read)
  8395. (boundp 'ido-mode) ido-mode
  8396. (listp (second args)))
  8397. (let ((ido-enter-matching-directory nil))
  8398. (apply 'ido-completing-read (concat (car args))
  8399. (if (consp (car (nth 1 args)))
  8400. (mapcar 'car (nth 1 args))
  8401. (nth 1 args))
  8402. (cddr args)))
  8403. (if (and org-completion-use-iswitchb
  8404. (boundp 'iswitchb-mode) iswitchb-mode
  8405. (listp (second args)))
  8406. (apply 'org-iswitchb-completing-read (concat (car args))
  8407. (if (consp (car (nth 1 args)))
  8408. (mapcar 'car (nth 1 args))
  8409. (nth 1 args))
  8410. (cddr args))
  8411. (apply 'completing-read args)))))
  8412. (defun org-extract-attributes (s)
  8413. "Extract the attributes cookie from a string and set as text property."
  8414. (let (a attr (start 0) key value)
  8415. (save-match-data
  8416. (when (string-match "{{\\([^}]+\\)}}$" s)
  8417. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8418. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8419. (setq key (match-string 1 a) value (match-string 2 a)
  8420. start (match-end 0)
  8421. attr (plist-put attr (intern key) value))))
  8422. (org-add-props s nil 'org-attr attr))
  8423. s))
  8424. (defun org-extract-attributes-from-string (tag)
  8425. (let (key value attr)
  8426. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8427. (setq key (match-string 1 tag) value (match-string 2 tag)
  8428. tag (replace-match "" t t tag)
  8429. attr (plist-put attr (intern key) value)))
  8430. (cons tag attr)))
  8431. (defun org-attributes-to-string (plist)
  8432. "Format a property list into an HTML attribute list."
  8433. (let ((s "") key value)
  8434. (while plist
  8435. (setq key (pop plist) value (pop plist))
  8436. (and value
  8437. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8438. s))
  8439. ;;; Opening/following a link
  8440. (defvar org-link-search-failed nil)
  8441. (defvar org-open-link-functions nil
  8442. "Hook for functions finding a plain text link.
  8443. These functions must take a single argument, the link content.
  8444. They will be called for links that look like [[link text][description]]
  8445. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8446. like a filename (e.g. \"./blue.png\").
  8447. These functions will be called *before* Org attempts to resolve the
  8448. link by doing text searches in the current buffer - so if you want a
  8449. link \"[[target]]\" to still find \"<<target>>\", your function should
  8450. handle this as a special case.
  8451. When the function does handle the link, it must return a non-nil value.
  8452. If it decides that it is not responsible for this link, it must return
  8453. nil to indicate that that Org-mode can continue with other options
  8454. like exact and fuzzy text search.")
  8455. (defun org-next-link ()
  8456. "Move forward to the next link.
  8457. If the link is in hidden text, expose it."
  8458. (interactive)
  8459. (when (and org-link-search-failed (eq this-command last-command))
  8460. (goto-char (point-min))
  8461. (message "Link search wrapped back to beginning of buffer"))
  8462. (setq org-link-search-failed nil)
  8463. (let* ((pos (point))
  8464. (ct (org-context))
  8465. (a (assoc :link ct)))
  8466. (if a (goto-char (nth 2 a)))
  8467. (if (re-search-forward org-any-link-re nil t)
  8468. (progn
  8469. (goto-char (match-beginning 0))
  8470. (if (outline-invisible-p) (org-show-context)))
  8471. (goto-char pos)
  8472. (setq org-link-search-failed t)
  8473. (error "No further link found"))))
  8474. (defun org-previous-link ()
  8475. "Move backward to the previous link.
  8476. If the link is in hidden text, expose it."
  8477. (interactive)
  8478. (when (and org-link-search-failed (eq this-command last-command))
  8479. (goto-char (point-max))
  8480. (message "Link search wrapped back to end of buffer"))
  8481. (setq org-link-search-failed nil)
  8482. (let* ((pos (point))
  8483. (ct (org-context))
  8484. (a (assoc :link ct)))
  8485. (if a (goto-char (nth 1 a)))
  8486. (if (re-search-backward org-any-link-re nil t)
  8487. (progn
  8488. (goto-char (match-beginning 0))
  8489. (if (outline-invisible-p) (org-show-context)))
  8490. (goto-char pos)
  8491. (setq org-link-search-failed t)
  8492. (error "No further link found"))))
  8493. (defun org-translate-link (s)
  8494. "Translate a link string if a translation function has been defined."
  8495. (if (and org-link-translation-function
  8496. (fboundp org-link-translation-function)
  8497. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8498. (progn
  8499. (setq s (funcall org-link-translation-function
  8500. (match-string 1) (match-string 2)))
  8501. (concat (car s) ":" (cdr s)))
  8502. s))
  8503. (defun org-translate-link-from-planner (type path)
  8504. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8505. This is still an experimental function, your mileage may vary."
  8506. (cond
  8507. ((member type '("http" "https" "news" "ftp"))
  8508. ;; standard Internet links are the same.
  8509. nil)
  8510. ((and (equal type "irc") (string-match "^//" path))
  8511. ;; Planner has two / at the beginning of an irc link, we have 1.
  8512. ;; We should have zero, actually....
  8513. (setq path (substring path 1)))
  8514. ((and (equal type "lisp") (string-match "^/" path))
  8515. ;; Planner has a slash, we do not.
  8516. (setq type "elisp" path (substring path 1)))
  8517. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8518. ;; A typical message link. Planner has the id after the final slash,
  8519. ;; we separate it with a hash mark
  8520. (setq path (concat (match-string 1 path) "#"
  8521. (org-remove-angle-brackets (match-string 2 path)))))
  8522. )
  8523. (cons type path))
  8524. (defun org-find-file-at-mouse (ev)
  8525. "Open file link or URL at mouse."
  8526. (interactive "e")
  8527. (mouse-set-point ev)
  8528. (org-open-at-point 'in-emacs))
  8529. (defun org-open-at-mouse (ev)
  8530. "Open file link or URL at mouse.
  8531. See the docstring of `org-open-file' for details."
  8532. (interactive "e")
  8533. (mouse-set-point ev)
  8534. (if (eq major-mode 'org-agenda-mode)
  8535. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8536. (org-open-at-point))
  8537. (defvar org-window-config-before-follow-link nil
  8538. "The window configuration before following a link.
  8539. This is saved in case the need arises to restore it.")
  8540. (defvar org-open-link-marker (make-marker)
  8541. "Marker pointing to the location where `org-open-at-point; was called.")
  8542. ;;;###autoload
  8543. (defun org-open-at-point-global ()
  8544. "Follow a link like Org-mode does.
  8545. This command can be called in any mode to follow a link that has
  8546. Org-mode syntax."
  8547. (interactive)
  8548. (org-run-like-in-org-mode 'org-open-at-point))
  8549. ;;;###autoload
  8550. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8551. "Open a link in the string S, as if it was in Org-mode."
  8552. (interactive "sLink: \nP")
  8553. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8554. (with-temp-buffer
  8555. (let ((org-inhibit-startup (not reference-buffer)))
  8556. (org-mode)
  8557. (insert s)
  8558. (goto-char (point-min))
  8559. (when reference-buffer
  8560. (setq org-link-abbrev-alist-local
  8561. (with-current-buffer reference-buffer
  8562. org-link-abbrev-alist-local)))
  8563. (org-open-at-point arg reference-buffer)))))
  8564. (defvar org-open-at-point-functions nil
  8565. "Hook that is run when following a link at point.
  8566. Functions in this hook must return t if they identify and follow
  8567. a link at point. If they don't find anything interesting at point,
  8568. they must return nil.")
  8569. (defun org-open-at-point (&optional arg reference-buffer)
  8570. "Open link at or after point.
  8571. If there is no link at point, this function will search forward up to
  8572. the end of the current line.
  8573. Normally, files will be opened by an appropriate application. If the
  8574. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8575. With a double prefix argument, try to open outside of Emacs, in the
  8576. application the system uses for this file type."
  8577. (interactive "P")
  8578. ;; if in a code block, then open the block's results
  8579. (unless (call-interactively #'org-babel-open-src-block-result)
  8580. (org-load-modules-maybe)
  8581. (move-marker org-open-link-marker (point))
  8582. (setq org-window-config-before-follow-link (current-window-configuration))
  8583. (org-remove-occur-highlights nil nil t)
  8584. (cond
  8585. ((and (org-at-heading-p)
  8586. (not (org-in-regexp
  8587. (concat org-plain-link-re "\\|"
  8588. org-bracket-link-regexp "\\|"
  8589. org-angle-link-re "\\|"
  8590. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8591. (not (get-text-property (point) 'org-linked-text)))
  8592. (or (org-offer-links-in-entry arg)
  8593. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8594. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8595. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8596. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8597. (not (org-in-regexp org-bracket-link-regexp)))
  8598. (org-footnote-action))
  8599. (t
  8600. (let (type path link line search (pos (point)))
  8601. (catch 'match
  8602. (save-excursion
  8603. (skip-chars-forward "^]\n\r")
  8604. (when (org-in-regexp org-bracket-link-regexp 1)
  8605. (setq link (org-extract-attributes
  8606. (org-link-unescape (org-match-string-no-properties 1))))
  8607. (while (string-match " *\n *" link)
  8608. (setq link (replace-match " " t t link)))
  8609. (setq link (org-link-expand-abbrev link))
  8610. (cond
  8611. ((or (file-name-absolute-p link)
  8612. (string-match "^\\.\\.?/" link))
  8613. (setq type "file" path link))
  8614. ((string-match org-link-re-with-space3 link)
  8615. (setq type (match-string 1 link) path (match-string 2 link)))
  8616. (t (setq type "thisfile" path link)))
  8617. (throw 'match t)))
  8618. (when (get-text-property (point) 'org-linked-text)
  8619. (setq type "thisfile"
  8620. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8621. (1+ (point)) (point))
  8622. path (buffer-substring
  8623. (or (previous-single-property-change pos 'org-linked-text)
  8624. (point-min))
  8625. (or (next-single-property-change pos 'org-linked-text)
  8626. (point-max))))
  8627. (throw 'match t))
  8628. (save-excursion
  8629. (when (or (org-in-regexp org-angle-link-re)
  8630. (org-in-regexp org-plain-link-re))
  8631. (setq type (match-string 1)
  8632. path (org-link-unescape (match-string 2)))
  8633. (throw 'match t)))
  8634. (save-excursion
  8635. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8636. (setq type "tags"
  8637. path (match-string 1))
  8638. (while (string-match ":" path)
  8639. (setq path (replace-match "+" t t path)))
  8640. (throw 'match t)))
  8641. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8642. (setq type "tree-match"
  8643. path (match-string 1))
  8644. (throw 'match t)))
  8645. (unless path
  8646. (error "No link found"))
  8647. ;; switch back to reference buffer
  8648. ;; needed when if called in a temporary buffer through
  8649. ;; org-open-link-from-string
  8650. (with-current-buffer (or reference-buffer (current-buffer))
  8651. ;; Remove any trailing spaces in path
  8652. (if (string-match " +\\'" path)
  8653. (setq path (replace-match "" t t path)))
  8654. (if (and org-link-translation-function
  8655. (fboundp org-link-translation-function))
  8656. ;; Check if we need to translate the link
  8657. (let ((tmp (funcall org-link-translation-function type path)))
  8658. (setq type (car tmp) path (cdr tmp))))
  8659. (cond
  8660. ((assoc type org-link-protocols)
  8661. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8662. ((equal type "mailto")
  8663. (let ((cmd (car org-link-mailto-program))
  8664. (args (cdr org-link-mailto-program)) args1
  8665. (address path) (subject "") a)
  8666. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8667. (setq address (match-string 1 path)
  8668. subject (org-link-escape (match-string 2 path))))
  8669. (while args
  8670. (cond
  8671. ((not (stringp (car args))) (push (pop args) args1))
  8672. (t (setq a (pop args))
  8673. (if (string-match "%a" a)
  8674. (setq a (replace-match address t t a)))
  8675. (if (string-match "%s" a)
  8676. (setq a (replace-match subject t t a)))
  8677. (push a args1))))
  8678. (apply cmd (nreverse args1))))
  8679. ((member type '("http" "https" "ftp" "news"))
  8680. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8681. (org-link-escape
  8682. path org-link-escape-chars-browser)
  8683. path))))
  8684. ((string= type "doi")
  8685. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8686. (org-link-escape
  8687. path org-link-escape-chars-browser)
  8688. path))))
  8689. ((member type '("message"))
  8690. (browse-url (concat type ":" path)))
  8691. ((string= type "tags")
  8692. (org-tags-view arg path))
  8693. ((string= type "tree-match")
  8694. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8695. ((string= type "file")
  8696. (if (string-match "::\\([0-9]+\\)\\'" path)
  8697. (setq line (string-to-number (match-string 1 path))
  8698. path (substring path 0 (match-beginning 0)))
  8699. (if (string-match "::\\(.+\\)\\'" path)
  8700. (setq search (match-string 1 path)
  8701. path (substring path 0 (match-beginning 0)))))
  8702. (if (string-match "[*?{]" (file-name-nondirectory path))
  8703. (dired path)
  8704. (org-open-file path arg line search)))
  8705. ((string= type "shell")
  8706. (let ((cmd path))
  8707. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8708. (string-match org-confirm-shell-link-not-regexp cmd))
  8709. (not org-confirm-shell-link-function)
  8710. (funcall org-confirm-shell-link-function
  8711. (format "Execute \"%s\" in shell? "
  8712. (org-add-props cmd nil
  8713. 'face 'org-warning))))
  8714. (progn
  8715. (message "Executing %s" cmd)
  8716. (shell-command cmd))
  8717. (error "Abort"))))
  8718. ((string= type "elisp")
  8719. (let ((cmd path))
  8720. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8721. (string-match org-confirm-elisp-link-not-regexp cmd))
  8722. (not org-confirm-elisp-link-function)
  8723. (funcall org-confirm-elisp-link-function
  8724. (format "Execute \"%s\" as elisp? "
  8725. (org-add-props cmd nil
  8726. 'face 'org-warning))))
  8727. (message "%s => %s" cmd
  8728. (if (equal (string-to-char cmd) ?\()
  8729. (eval (read cmd))
  8730. (call-interactively (read cmd))))
  8731. (error "Abort"))))
  8732. ((and (string= type "thisfile")
  8733. (run-hook-with-args-until-success
  8734. 'org-open-link-functions path)))
  8735. ((string= type "thisfile")
  8736. (if arg
  8737. (switch-to-buffer-other-window
  8738. (org-get-buffer-for-internal-link (current-buffer)))
  8739. (org-mark-ring-push))
  8740. (let ((cmd `(org-link-search
  8741. ,path
  8742. ,(cond ((equal arg '(4)) ''occur)
  8743. ((equal arg '(16)) ''org-occur)
  8744. (t nil))
  8745. ,pos)))
  8746. (condition-case nil (let ((org-link-search-inhibit-query t))
  8747. (eval cmd))
  8748. (error (progn (widen) (eval cmd))))))
  8749. (t
  8750. (browse-url-at-point)))))))
  8751. (move-marker org-open-link-marker nil)
  8752. (run-hook-with-args 'org-follow-link-hook)))
  8753. (defun org-offer-links-in-entry (&optional nth zero)
  8754. "Offer links in the current entry and follow the selected link.
  8755. If there is only one link, follow it immediately as well.
  8756. If NTH is an integer, immediately pick the NTH link found.
  8757. If ZERO is a string, check also this string for a link, and if
  8758. there is one, offer it as link number zero."
  8759. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8760. "\\(" org-angle-link-re "\\)\\|"
  8761. "\\(" org-plain-link-re "\\)"))
  8762. (cnt ?0)
  8763. (in-emacs (if (integerp nth) nil nth))
  8764. have-zero end links link c)
  8765. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8766. (push (match-string 0 zero) links)
  8767. (setq cnt (1- cnt) have-zero t))
  8768. (save-excursion
  8769. (org-back-to-heading t)
  8770. (setq end (save-excursion (outline-next-heading) (point)))
  8771. (while (re-search-forward re end t)
  8772. (push (match-string 0) links))
  8773. (setq links (org-uniquify (reverse links))))
  8774. (cond
  8775. ((null links)
  8776. (message "No links"))
  8777. ((equal (length links) 1)
  8778. (setq link (list (car links))))
  8779. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8780. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8781. (t ; we have to select a link
  8782. (save-excursion
  8783. (save-window-excursion
  8784. (delete-other-windows)
  8785. (with-output-to-temp-buffer "*Select Link*"
  8786. (mapc (lambda (l)
  8787. (if (not (string-match org-bracket-link-regexp l))
  8788. (princ (format "[%c] %s\n" (incf cnt)
  8789. (org-remove-angle-brackets l)))
  8790. (if (match-end 3)
  8791. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8792. (match-string 3 l) (match-string 1 l)))
  8793. (princ (format "[%c] %s\n" (incf cnt)
  8794. (match-string 1 l))))))
  8795. links))
  8796. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8797. (message "Select link to open, RET to open all:")
  8798. (setq c (read-char-exclusive))
  8799. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8800. (when (equal c ?q) (error "Abort"))
  8801. (if (equal c ?\C-m)
  8802. (setq link links)
  8803. (setq nth (- c ?0))
  8804. (if have-zero (setq nth (1+ nth)))
  8805. (unless (and (integerp nth) (>= (length links) nth))
  8806. (error "Invalid link selection"))
  8807. (setq link (list (nth (1- nth) links))))))
  8808. (if link
  8809. (let ((buf (current-buffer)))
  8810. (dolist (l link)
  8811. (org-open-link-from-string l in-emacs buf))
  8812. t)
  8813. nil)))
  8814. ;; Add special file links that specify the way of opening
  8815. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8816. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8817. (defun org-open-file-with-system (path)
  8818. "Open file at PATH using the system way of opening it."
  8819. (org-open-file path 'system))
  8820. (defun org-open-file-with-emacs (path)
  8821. "Open file at PATH in Emacs."
  8822. (org-open-file path 'emacs))
  8823. (defun org-remove-file-link-modifiers ()
  8824. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8825. (goto-char (point-min))
  8826. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8827. (org-if-unprotected
  8828. (replace-match "file:" t t))))
  8829. (eval-after-load "org-exp"
  8830. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8831. 'org-remove-file-link-modifiers))
  8832. ;;;; Time estimates
  8833. (defun org-get-effort (&optional pom)
  8834. "Get the effort estimate for the current entry."
  8835. (org-entry-get pom org-effort-property))
  8836. ;;; File search
  8837. (defvar org-create-file-search-functions nil
  8838. "List of functions to construct the right search string for a file link.
  8839. These functions are called in turn with point at the location to
  8840. which the link should point.
  8841. A function in the hook should first test if it would like to
  8842. handle this file type, for example by checking the `major-mode'
  8843. or the file extension. If it decides not to handle this file, it
  8844. should just return nil to give other functions a chance. If it
  8845. does handle the file, it must return the search string to be used
  8846. when following the link. The search string will be part of the
  8847. file link, given after a double colon, and `org-open-at-point'
  8848. will automatically search for it. If special measures must be
  8849. taken to make the search successful, another function should be
  8850. added to the companion hook `org-execute-file-search-functions',
  8851. which see.
  8852. A function in this hook may also use `setq' to set the variable
  8853. `description' to provide a suggestion for the descriptive text to
  8854. be used for this link when it gets inserted into an Org-mode
  8855. buffer with \\[org-insert-link].")
  8856. (defvar org-execute-file-search-functions nil
  8857. "List of functions to execute a file search triggered by a link.
  8858. Functions added to this hook must accept a single argument, the
  8859. search string that was part of the file link, the part after the
  8860. double colon. The function must first check if it would like to
  8861. handle this search, for example by checking the `major-mode' or
  8862. the file extension. If it decides not to handle this search, it
  8863. should just return nil to give other functions a chance. If it
  8864. does handle the search, it must return a non-nil value to keep
  8865. other functions from trying.
  8866. Each function can access the current prefix argument through the
  8867. variable `current-prefix-argument'. Note that a single prefix is
  8868. used to force opening a link in Emacs, so it may be good to only
  8869. use a numeric or double prefix to guide the search function.
  8870. In case this is needed, a function in this hook can also restore
  8871. the window configuration before `org-open-at-point' was called using:
  8872. (set-window-configuration org-window-config-before-follow-link)")
  8873. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8874. (defun org-link-search (s &optional type avoid-pos stealth)
  8875. "Search for a link search option.
  8876. If S is surrounded by forward slashes, it is interpreted as a
  8877. regular expression. In org-mode files, this will create an `org-occur'
  8878. sparse tree. In ordinary files, `occur' will be used to list matches.
  8879. If the current buffer is in `dired-mode', grep will be used to search
  8880. in all files. If AVOID-POS is given, ignore matches near that position.
  8881. When optional argument STEALTH is non-nil, do not modify
  8882. visibility around point, thus ignoring
  8883. `org-show-hierarchy-above', `org-show-following-heading' and
  8884. `org-show-siblings' variables."
  8885. (let ((case-fold-search t)
  8886. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8887. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8888. (append '(("") (" ") ("\t") ("\n"))
  8889. org-emphasis-alist)
  8890. "\\|") "\\)"))
  8891. (pos (point))
  8892. (pre nil) (post nil)
  8893. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8894. (cond
  8895. ;; First check if there are any special search functions
  8896. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8897. ;; Now try the builtin stuff
  8898. ((and (equal (string-to-char s0) ?#)
  8899. (> (length s0) 1)
  8900. (save-excursion
  8901. (goto-char (point-min))
  8902. (and
  8903. (re-search-forward
  8904. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8905. (setq type 'dedicated
  8906. pos (match-beginning 0))))
  8907. ;; There is an exact target for this
  8908. (goto-char pos)
  8909. (org-back-to-heading t)))
  8910. ((save-excursion
  8911. (goto-char (point-min))
  8912. (and
  8913. (re-search-forward
  8914. (concat "<<" (regexp-quote s0) ">>") nil t)
  8915. (setq type 'dedicated
  8916. pos (match-beginning 0))))
  8917. ;; There is an exact target for this
  8918. (goto-char pos))
  8919. ((save-excursion
  8920. (goto-char (point-min))
  8921. (and
  8922. (re-search-forward
  8923. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8924. (setq type 'dedicated pos (match-beginning 0))))
  8925. ;; Found an invisible target.
  8926. (goto-char pos))
  8927. ((save-excursion
  8928. (goto-char (point-min))
  8929. (and
  8930. (re-search-forward
  8931. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  8932. (setq type 'dedicated pos (match-beginning 0))))
  8933. ;; Found an element with a matching #+name affiliated keyword.
  8934. (goto-char pos))
  8935. ((and (string-match "^(\\(.*\\))$" s0)
  8936. (save-excursion
  8937. (goto-char (point-min))
  8938. (and
  8939. (re-search-forward
  8940. (concat "[^[]" (regexp-quote
  8941. (format org-coderef-label-format
  8942. (match-string 1 s0))))
  8943. nil t)
  8944. (setq type 'dedicated
  8945. pos (1+ (match-beginning 0))))))
  8946. ;; There is a coderef target for this
  8947. (goto-char pos))
  8948. ((string-match "^/\\(.*\\)/$" s)
  8949. ;; A regular expression
  8950. (cond
  8951. ((derived-mode-p 'org-mode)
  8952. (org-occur (match-string 1 s)))
  8953. ;;((eq major-mode 'dired-mode)
  8954. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8955. (t (org-do-occur (match-string 1 s)))))
  8956. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  8957. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8958. (goto-char (point-min))
  8959. (cond
  8960. ((let (case-fold-search)
  8961. (re-search-forward (format org-complex-heading-regexp-format
  8962. (regexp-quote s))
  8963. nil t))
  8964. ;; OK, found a match
  8965. (setq type 'dedicated)
  8966. (goto-char (match-beginning 0)))
  8967. ((and (not org-link-search-inhibit-query)
  8968. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8969. (y-or-n-p "No match - create this as a new heading? "))
  8970. (goto-char (point-max))
  8971. (or (bolp) (newline))
  8972. (insert "* " s "\n")
  8973. (beginning-of-line 0))
  8974. (t
  8975. (goto-char pos)
  8976. (error "No match"))))
  8977. (t
  8978. ;; A normal search string
  8979. (when (equal (string-to-char s) ?*)
  8980. ;; Anchor on headlines, post may include tags.
  8981. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8982. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8983. s (substring s 1)))
  8984. (remove-text-properties
  8985. 0 (length s)
  8986. '(face nil mouse-face nil keymap nil fontified nil) s)
  8987. ;; Make a series of regular expressions to find a match
  8988. (setq words (org-split-string s "[ \n\r\t]+")
  8989. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8990. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8991. "\\)" markers)
  8992. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8993. re2a (concat "[ \t\r\n]" re2a_)
  8994. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8995. re4 (concat "[^a-zA-Z_]" re4_)
  8996. re1 (concat pre re2 post)
  8997. re3 (concat pre (if pre re4_ re4) post)
  8998. re5 (concat pre ".*" re4)
  8999. re2 (concat pre re2)
  9000. re2a (concat pre (if pre re2a_ re2a))
  9001. re4 (concat pre (if pre re4_ re4))
  9002. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9003. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9004. re5 "\\)"
  9005. ))
  9006. (cond
  9007. ((eq type 'org-occur) (org-occur reall))
  9008. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9009. (t (goto-char (point-min))
  9010. (setq type 'fuzzy)
  9011. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9012. (org-search-not-self 1 re1 nil t)
  9013. (org-search-not-self 1 re2 nil t)
  9014. (org-search-not-self 1 re2a nil t)
  9015. (org-search-not-self 1 re3 nil t)
  9016. (org-search-not-self 1 re4 nil t)
  9017. (org-search-not-self 1 re5 nil t)
  9018. )
  9019. (goto-char (match-beginning 1))
  9020. (goto-char pos)
  9021. (error "No match"))))))
  9022. (and (derived-mode-p 'org-mode)
  9023. (not stealth)
  9024. (org-show-context 'link-search))
  9025. type))
  9026. (defun org-search-not-self (group &rest args)
  9027. "Execute `re-search-forward', but only accept matches that do not
  9028. enclose the position of `org-open-link-marker'."
  9029. (let ((m org-open-link-marker))
  9030. (catch 'exit
  9031. (while (apply 're-search-forward args)
  9032. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9033. (goto-char (match-end group))
  9034. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9035. (> (match-beginning 0) (marker-position m))
  9036. (< (match-end 0) (marker-position m)))
  9037. (save-match-data
  9038. (or (not (org-in-regexp
  9039. org-bracket-link-analytic-regexp 1))
  9040. (not (match-end 4)) ; no description
  9041. (and (<= (match-beginning 4) (point))
  9042. (>= (match-end 4) (point))))))
  9043. (throw 'exit (point))))))))
  9044. (defun org-get-buffer-for-internal-link (buffer)
  9045. "Return a buffer to be used for displaying the link target of internal links."
  9046. (cond
  9047. ((not org-display-internal-link-with-indirect-buffer)
  9048. buffer)
  9049. ((string-match "(Clone)$" (buffer-name buffer))
  9050. (message "Buffer is already a clone, not making another one")
  9051. ;; we also do not modify visibility in this case
  9052. buffer)
  9053. (t ; make a new indirect buffer for displaying the link
  9054. (let* ((bn (buffer-name buffer))
  9055. (ibn (concat bn "(Clone)"))
  9056. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9057. (with-current-buffer ib (org-overview))
  9058. ib))))
  9059. (defun org-do-occur (regexp &optional cleanup)
  9060. "Call the Emacs command `occur'.
  9061. If CLEANUP is non-nil, remove the printout of the regular expression
  9062. in the *Occur* buffer. This is useful if the regex is long and not useful
  9063. to read."
  9064. (occur regexp)
  9065. (when cleanup
  9066. (let ((cwin (selected-window)) win beg end)
  9067. (when (setq win (get-buffer-window "*Occur*"))
  9068. (select-window win))
  9069. (goto-char (point-min))
  9070. (when (re-search-forward "match[a-z]+" nil t)
  9071. (setq beg (match-end 0))
  9072. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9073. (setq end (1- (match-beginning 0)))))
  9074. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9075. (goto-char (point-min))
  9076. (select-window cwin))))
  9077. ;;; The mark ring for links jumps
  9078. (defvar org-mark-ring nil
  9079. "Mark ring for positions before jumps in Org-mode.")
  9080. (defvar org-mark-ring-last-goto nil
  9081. "Last position in the mark ring used to go back.")
  9082. ;; Fill and close the ring
  9083. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9084. (loop for i from 1 to org-mark-ring-length do
  9085. (push (make-marker) org-mark-ring))
  9086. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9087. org-mark-ring)
  9088. (defun org-mark-ring-push (&optional pos buffer)
  9089. "Put the current position or POS into the mark ring and rotate it."
  9090. (interactive)
  9091. (setq pos (or pos (point)))
  9092. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9093. (move-marker (car org-mark-ring)
  9094. (or pos (point))
  9095. (or buffer (current-buffer)))
  9096. (message "%s"
  9097. (substitute-command-keys
  9098. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9099. (defun org-mark-ring-goto (&optional n)
  9100. "Jump to the previous position in the mark ring.
  9101. With prefix arg N, jump back that many stored positions. When
  9102. called several times in succession, walk through the entire ring.
  9103. Org-mode commands jumping to a different position in the current file,
  9104. or to another Org-mode file, automatically push the old position
  9105. onto the ring."
  9106. (interactive "p")
  9107. (let (p m)
  9108. (if (eq last-command this-command)
  9109. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9110. (setq p org-mark-ring))
  9111. (setq org-mark-ring-last-goto p)
  9112. (setq m (car p))
  9113. (org-pop-to-buffer-same-window (marker-buffer m))
  9114. (goto-char m)
  9115. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9116. (defun org-remove-angle-brackets (s)
  9117. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9118. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9119. s)
  9120. (defun org-add-angle-brackets (s)
  9121. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9122. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9123. s)
  9124. (defun org-remove-double-quotes (s)
  9125. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9126. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9127. s)
  9128. ;;; Following specific links
  9129. (defun org-follow-timestamp-link ()
  9130. (cond
  9131. ((org-at-date-range-p t)
  9132. (let ((org-agenda-start-on-weekday)
  9133. (t1 (match-string 1))
  9134. (t2 (match-string 2)))
  9135. (setq t1 (time-to-days (org-time-string-to-time t1))
  9136. t2 (time-to-days (org-time-string-to-time t2)))
  9137. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9138. ((org-at-timestamp-p t)
  9139. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9140. (substring (match-string 1) 0 10)))
  9141. 1))
  9142. (t (error "This should not happen"))))
  9143. ;;; Following file links
  9144. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9145. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9146. (declare-function mailcap-mime-info
  9147. "mailcap" (string &optional request no-decode))
  9148. (defvar org-wait nil)
  9149. (defun org-open-file (path &optional in-emacs line search)
  9150. "Open the file at PATH.
  9151. First, this expands any special file name abbreviations. Then the
  9152. configuration variable `org-file-apps' is checked if it contains an
  9153. entry for this file type, and if yes, the corresponding command is launched.
  9154. If no application is found, Emacs simply visits the file.
  9155. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9156. With a double \\[universal-argument] \\[universal-argument] \
  9157. prefix arg, Org tries to avoid opening in Emacs
  9158. and to use an external application to visit the file.
  9159. Optional LINE specifies a line to go to, optional SEARCH a string
  9160. to search for. If LINE or SEARCH is given, the file will be
  9161. opened in Emacs, unless an entry from org-file-apps that makes
  9162. use of groups in a regexp matches.
  9163. If you want to change the way frames are used when following a
  9164. link, please customize `org-link-frame-setup'.
  9165. If the file does not exist, an error is thrown."
  9166. (let* ((file (if (equal path "")
  9167. buffer-file-name
  9168. (substitute-in-file-name (expand-file-name path))))
  9169. (file-apps (append org-file-apps (org-default-apps)))
  9170. (apps (org-remove-if
  9171. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9172. (apps-dlink (org-remove-if-not
  9173. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9174. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9175. (dirp (if remp nil (file-directory-p file)))
  9176. (file (if (and dirp org-open-directory-means-index-dot-org)
  9177. (concat (file-name-as-directory file) "index.org")
  9178. file))
  9179. (a-m-a-p (assq 'auto-mode apps))
  9180. (dfile (downcase file))
  9181. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9182. (link (cond ((and (eq line nil)
  9183. (eq search nil))
  9184. file)
  9185. (line
  9186. (concat file "::" (number-to-string line)))
  9187. (search
  9188. (concat file "::" search))))
  9189. (dlink (downcase link))
  9190. (old-buffer (current-buffer))
  9191. (old-pos (point))
  9192. (old-mode major-mode)
  9193. ext cmd link-match-data)
  9194. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9195. (setq ext (match-string 1 dfile))
  9196. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9197. (setq ext (match-string 1 dfile))))
  9198. (cond
  9199. ((member in-emacs '((16) system))
  9200. (setq cmd (cdr (assoc 'system apps))))
  9201. (in-emacs (setq cmd 'emacs))
  9202. (t
  9203. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9204. (and dirp (cdr (assoc 'directory apps)))
  9205. ; first, try matching against apps-dlink
  9206. ; if we get a match here, store the match data for later
  9207. (let ((match (assoc-default dlink apps-dlink
  9208. 'string-match)))
  9209. (if match
  9210. (progn (setq link-match-data (match-data))
  9211. match)
  9212. (progn (setq in-emacs (or in-emacs line search))
  9213. nil))) ; if we have no match in apps-dlink,
  9214. ; always open the file in emacs if line or search
  9215. ; is given (for backwards compatibility)
  9216. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9217. 'string-match)
  9218. (cdr (assoc ext apps))
  9219. (cdr (assoc t apps))))))
  9220. (when (eq cmd 'system)
  9221. (setq cmd (cdr (assoc 'system apps))))
  9222. (when (eq cmd 'default)
  9223. (setq cmd (cdr (assoc t apps))))
  9224. (when (eq cmd 'mailcap)
  9225. (require 'mailcap)
  9226. (mailcap-parse-mailcaps)
  9227. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9228. (command (mailcap-mime-info mime-type)))
  9229. (if (stringp command)
  9230. (setq cmd command)
  9231. (setq cmd 'emacs))))
  9232. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9233. (not (file-exists-p file))
  9234. (not org-open-non-existing-files))
  9235. (error "No such file: %s" file))
  9236. (cond
  9237. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9238. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9239. (while (string-match "['\"]%s['\"]" cmd)
  9240. (setq cmd (replace-match "%s" t t cmd)))
  9241. (while (string-match "%s" cmd)
  9242. (setq cmd (replace-match
  9243. (save-match-data
  9244. (shell-quote-argument
  9245. (convert-standard-filename file)))
  9246. t t cmd)))
  9247. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9248. (save-match-data
  9249. (let ((match-index 1)
  9250. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9251. (set-match-data link-match-data)
  9252. (while (<= match-index number-of-groups)
  9253. (let ((regex (concat "%" (number-to-string match-index)))
  9254. (replace-with (match-string match-index dlink)))
  9255. (while (string-match regex cmd)
  9256. (setq cmd (replace-match replace-with t t cmd))))
  9257. (setq match-index (+ match-index 1)))))
  9258. (save-window-excursion
  9259. (start-process-shell-command cmd nil cmd)
  9260. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9261. ))
  9262. ((or (stringp cmd)
  9263. (eq cmd 'emacs))
  9264. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9265. (widen)
  9266. (if line (org-goto-line line)
  9267. (if search (org-link-search search))))
  9268. ((consp cmd)
  9269. (let ((file (convert-standard-filename file)))
  9270. (save-match-data
  9271. (set-match-data link-match-data)
  9272. (eval cmd))))
  9273. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9274. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9275. (or (not (equal old-buffer (current-buffer)))
  9276. (not (equal old-pos (point))))
  9277. (org-mark-ring-push old-pos old-buffer))))
  9278. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9279. "This function returns non-nil if `entry' uses a regular
  9280. expression which should be matched against the whole link by
  9281. org-open-file.
  9282. It assumes that is the case when the entry uses a regular
  9283. expression which has at least one grouping construct and the
  9284. action is either a lisp form or a command string containing
  9285. '%1', i.e. using at least one subexpression match as a
  9286. parameter."
  9287. (let ((selector (car entry))
  9288. (action (cdr entry)))
  9289. (if (stringp selector)
  9290. (and (> (regexp-opt-depth selector) 0)
  9291. (or (and (stringp action)
  9292. (string-match "%[0-9]" action))
  9293. (consp action)))
  9294. nil)))
  9295. (defun org-default-apps ()
  9296. "Return the default applications for this operating system."
  9297. (cond
  9298. ((eq system-type 'darwin)
  9299. org-file-apps-defaults-macosx)
  9300. ((eq system-type 'windows-nt)
  9301. org-file-apps-defaults-windowsnt)
  9302. (t org-file-apps-defaults-gnu)))
  9303. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9304. "Convert extensions to regular expressions in the cars of LIST.
  9305. Also, weed out any non-string entries, because the return value is used
  9306. only for regexp matching.
  9307. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9308. point to the symbol `emacs', indicating that the file should
  9309. be opened in Emacs."
  9310. (append
  9311. (delq nil
  9312. (mapcar (lambda (x)
  9313. (if (not (stringp (car x)))
  9314. nil
  9315. (if (string-match "\\W" (car x))
  9316. x
  9317. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9318. list))
  9319. (if add-auto-mode
  9320. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9321. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9322. (defun org-file-remote-p (file)
  9323. "Test whether FILE specifies a location on a remote system.
  9324. Return non-nil if the location is indeed remote.
  9325. For example, the filename \"/user@host:/foo\" specifies a location
  9326. on the system \"/user@host:\"."
  9327. (cond ((fboundp 'file-remote-p)
  9328. (file-remote-p file))
  9329. ((fboundp 'tramp-handle-file-remote-p)
  9330. (tramp-handle-file-remote-p file))
  9331. ((and (boundp 'ange-ftp-name-format)
  9332. (string-match (car ange-ftp-name-format) file))
  9333. t)
  9334. (t nil)))
  9335. ;;;; Refiling
  9336. (defun org-get-org-file ()
  9337. "Read a filename, with default directory `org-directory'."
  9338. (let ((default (or org-default-notes-file remember-data-file)))
  9339. (read-file-name (format "File name [%s]: " default)
  9340. (file-name-as-directory org-directory)
  9341. default)))
  9342. (defun org-notes-order-reversed-p ()
  9343. "Check if the current file should receive notes in reversed order."
  9344. (cond
  9345. ((not org-reverse-note-order) nil)
  9346. ((eq t org-reverse-note-order) t)
  9347. ((not (listp org-reverse-note-order)) nil)
  9348. (t (catch 'exit
  9349. (let ((all org-reverse-note-order)
  9350. entry)
  9351. (while (setq entry (pop all))
  9352. (if (string-match (car entry) buffer-file-name)
  9353. (throw 'exit (cdr entry))))
  9354. nil)))))
  9355. (defvar org-refile-target-table nil
  9356. "The list of refile targets, created by `org-refile'.")
  9357. (defvar org-agenda-new-buffers nil
  9358. "Buffers created to visit agenda files.")
  9359. (defvar org-refile-cache nil
  9360. "Cache for refile targets.")
  9361. (defvar org-refile-markers nil
  9362. "All the markers used for caching refile locations.")
  9363. (defun org-refile-marker (pos)
  9364. "Get a new refile marker, but only if caching is in use."
  9365. (if (not org-refile-use-cache)
  9366. pos
  9367. (let ((m (make-marker)))
  9368. (move-marker m pos)
  9369. (push m org-refile-markers)
  9370. m)))
  9371. (defun org-refile-cache-clear ()
  9372. "Clear the refile cache and disable all the markers."
  9373. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9374. (setq org-refile-markers nil)
  9375. (setq org-refile-cache nil)
  9376. (message "Refile cache has been cleared"))
  9377. (defun org-refile-cache-check-set (set)
  9378. "Check if all the markers in the cache still have live buffers."
  9379. (let (marker)
  9380. (catch 'exit
  9381. (while (and set (setq marker (nth 3 (pop set))))
  9382. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9383. (when (and marker (null (marker-buffer marker)))
  9384. (message "not found") (sit-for 3)
  9385. (throw 'exit nil)))
  9386. t)))
  9387. (defun org-refile-cache-put (set &rest identifiers)
  9388. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9389. (let* ((key (sha1 (prin1-to-string identifiers)))
  9390. (entry (assoc key org-refile-cache)))
  9391. (if entry
  9392. (setcdr entry set)
  9393. (push (cons key set) org-refile-cache))))
  9394. (defun org-refile-cache-get (&rest identifiers)
  9395. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9396. (cond
  9397. ((not org-refile-cache) nil)
  9398. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9399. (t
  9400. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9401. org-refile-cache))))
  9402. (and set (org-refile-cache-check-set set) set)))))
  9403. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9404. "Produce a table with refile targets."
  9405. (let ((case-fold-search nil)
  9406. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9407. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9408. targets tgs txt re files f desc descre fast-path-p level pos0)
  9409. (message "Getting targets...")
  9410. (with-current-buffer (or default-buffer (current-buffer))
  9411. (while (setq entry (pop entries))
  9412. (setq files (car entry) desc (cdr entry))
  9413. (setq fast-path-p nil)
  9414. (cond
  9415. ((null files) (setq files (list (current-buffer))))
  9416. ((eq files 'org-agenda-files)
  9417. (setq files (org-agenda-files 'unrestricted)))
  9418. ((and (symbolp files) (fboundp files))
  9419. (setq files (funcall files)))
  9420. ((and (symbolp files) (boundp files))
  9421. (setq files (symbol-value files))))
  9422. (if (stringp files) (setq files (list files)))
  9423. (cond
  9424. ((eq (car desc) :tag)
  9425. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9426. ((eq (car desc) :todo)
  9427. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9428. ((eq (car desc) :regexp)
  9429. (setq descre (cdr desc)))
  9430. ((eq (car desc) :level)
  9431. (setq descre (concat "^\\*\\{" (number-to-string
  9432. (if org-odd-levels-only
  9433. (1- (* 2 (cdr desc)))
  9434. (cdr desc)))
  9435. "\\}[ \t]")))
  9436. ((eq (car desc) :maxlevel)
  9437. (setq fast-path-p t)
  9438. (setq descre (concat "^\\*\\{1," (number-to-string
  9439. (if org-odd-levels-only
  9440. (1- (* 2 (cdr desc)))
  9441. (cdr desc)))
  9442. "\\}[ \t]")))
  9443. (t (error "Bad refiling target description %s" desc)))
  9444. (while (setq f (pop files))
  9445. (with-current-buffer
  9446. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9447. (or
  9448. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9449. (progn
  9450. (if (bufferp f) (setq f (buffer-file-name
  9451. (buffer-base-buffer f))))
  9452. (setq f (and f (expand-file-name f)))
  9453. (if (eq org-refile-use-outline-path 'file)
  9454. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9455. (save-excursion
  9456. (save-restriction
  9457. (widen)
  9458. (goto-char (point-min))
  9459. (while (re-search-forward descre nil t)
  9460. (goto-char (setq pos0 (point-at-bol)))
  9461. (catch 'next
  9462. (when org-refile-target-verify-function
  9463. (save-match-data
  9464. (or (funcall org-refile-target-verify-function)
  9465. (throw 'next t))))
  9466. (when (and (looking-at org-complex-heading-regexp)
  9467. (not (member (match-string 4) excluded-entries))
  9468. (match-string 4))
  9469. (setq level (org-reduced-level
  9470. (- (match-end 1) (match-beginning 1)))
  9471. txt (org-link-display-format (match-string 4))
  9472. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9473. re (format org-complex-heading-regexp-format
  9474. (regexp-quote (match-string 4))))
  9475. (when org-refile-use-outline-path
  9476. (setq txt (mapconcat
  9477. 'org-protect-slash
  9478. (append
  9479. (if (eq org-refile-use-outline-path
  9480. 'file)
  9481. (list (file-name-nondirectory
  9482. (buffer-file-name
  9483. (buffer-base-buffer))))
  9484. (if (eq org-refile-use-outline-path
  9485. 'full-file-path)
  9486. (list (buffer-file-name
  9487. (buffer-base-buffer)))))
  9488. (org-get-outline-path fast-path-p
  9489. level txt)
  9490. (list txt))
  9491. "/")))
  9492. (push (list txt f re (org-refile-marker (point)))
  9493. tgs)))
  9494. (when (= (point) pos0)
  9495. ;; verification function has not moved point
  9496. (goto-char (point-at-eol))))))))
  9497. (when org-refile-use-cache
  9498. (org-refile-cache-put tgs (buffer-file-name) descre))
  9499. (setq targets (append tgs targets))
  9500. ))))
  9501. (message "Getting targets...done")
  9502. (nreverse targets)))
  9503. (defun org-protect-slash (s)
  9504. (while (string-match "/" s)
  9505. (setq s (replace-match "\\" t t s)))
  9506. s)
  9507. (defvar org-olpa (make-vector 20 nil))
  9508. (defun org-get-outline-path (&optional fastp level heading)
  9509. "Return the outline path to the current entry, as a list.
  9510. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9511. routine which makes outline path derivations for an entire file,
  9512. avoiding backtracing. Refile target collection makes use of that."
  9513. (if fastp
  9514. (progn
  9515. (if (> level 19)
  9516. (error "Outline path failure, more than 19 levels"))
  9517. (loop for i from level upto 19 do
  9518. (aset org-olpa i nil))
  9519. (prog1
  9520. (delq nil (append org-olpa nil))
  9521. (aset org-olpa level heading)))
  9522. (let (rtn case-fold-search)
  9523. (save-excursion
  9524. (save-restriction
  9525. (widen)
  9526. (while (org-up-heading-safe)
  9527. (when (looking-at org-complex-heading-regexp)
  9528. (push (org-match-string-no-properties 4) rtn)))
  9529. rtn)))))
  9530. (defun org-format-outline-path (path &optional width prefix)
  9531. "Format the outline path PATH for display.
  9532. Width is the maximum number of characters that is available.
  9533. Prefix is a prefix to be included in the returned string,
  9534. such as the file name."
  9535. (setq width (or width 79))
  9536. (if prefix (setq width (- width (length prefix))))
  9537. (if (not path)
  9538. (or prefix "")
  9539. (let* ((nsteps (length path))
  9540. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9541. (maxwidth (if (<= total-width width)
  9542. 10000 ;; everything fits
  9543. ;; we need to shorten the level headings
  9544. (/ (- width nsteps) nsteps)))
  9545. (org-odd-levels-only nil)
  9546. (n 0)
  9547. (total (1+ (length prefix))))
  9548. (setq maxwidth (max maxwidth 10))
  9549. (concat prefix
  9550. (mapconcat
  9551. (lambda (h)
  9552. (setq n (1+ n))
  9553. (if (and (= n nsteps) (< maxwidth 10000))
  9554. (setq maxwidth (- total-width total)))
  9555. (if (< (length h) maxwidth)
  9556. (progn (setq total (+ total (length h) 1)) h)
  9557. (setq h (substring h 0 (- maxwidth 2))
  9558. total (+ total maxwidth 1))
  9559. (if (string-match "[ \t]+\\'" h)
  9560. (setq h (substring h 0 (match-beginning 0))))
  9561. (setq h (concat h "..")))
  9562. (org-add-props h nil 'face
  9563. (nth (% (1- n) org-n-level-faces)
  9564. org-level-faces))
  9565. h)
  9566. path "/")))))
  9567. (defun org-display-outline-path (&optional file current)
  9568. "Display the current outline path in the echo area."
  9569. (interactive "P")
  9570. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9571. (case-fold-search nil)
  9572. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9573. (if current (setq path (append path
  9574. (save-excursion
  9575. (org-back-to-heading t)
  9576. (if (looking-at org-complex-heading-regexp)
  9577. (list (match-string 4)))))))
  9578. (message "%s"
  9579. (org-format-outline-path
  9580. path
  9581. (1- (frame-width))
  9582. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9583. (defvar org-refile-history nil
  9584. "History for refiling operations.")
  9585. (defvar org-after-refile-insert-hook nil
  9586. "Hook run after `org-refile' has inserted its stuff at the new location.
  9587. Note that this is still *before* the stuff will be removed from
  9588. the *old* location.")
  9589. (defvar org-capture-last-stored-marker)
  9590. (defun org-refile (&optional goto default-buffer rfloc)
  9591. "Move the entry or entries at point to another heading.
  9592. The list of target headings is compiled using the information in
  9593. `org-refile-targets', which see.
  9594. At the target location, the entry is filed as a subitem of the target
  9595. heading. Depending on `org-reverse-note-order', the new subitem will
  9596. either be the first or the last subitem.
  9597. If there is an active region, all entries in that region will be moved.
  9598. However, the region must fulfill the requirement that the first heading
  9599. is the first one sets the top-level of the moved text - at most siblings
  9600. below it are allowed.
  9601. With prefix arg GOTO, the command will only visit the target location
  9602. and not actually move anything.
  9603. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9604. go to the location where the last refiling operation has put the subtree.
  9605. With a prefix argument of `2', refile to the running clock.
  9606. RFLOC can be a refile location obtained in a different way.
  9607. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9608. If you are using target caching (see `org-refile-use-cache'),
  9609. You have to clear the target cache in order to find new targets.
  9610. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9611. prefix argument (`C-u C-u C-u C-c C-w')."
  9612. (interactive "P")
  9613. (if (member goto '(0 (64)))
  9614. (org-refile-cache-clear)
  9615. (let* ((cbuf (current-buffer))
  9616. (regionp (org-region-active-p))
  9617. (region-start (and regionp (region-beginning)))
  9618. (region-end (and regionp (region-end)))
  9619. (region-length (and regionp (- region-end region-start)))
  9620. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9621. pos it nbuf file re level reversed)
  9622. (setq last-command nil)
  9623. (when regionp
  9624. (goto-char region-start)
  9625. (or (bolp) (goto-char (point-at-bol)))
  9626. (setq region-start (point))
  9627. (unless (or (org-kill-is-subtree-p
  9628. (buffer-substring region-start region-end))
  9629. (prog1 org-refile-active-region-within-subtree
  9630. (org-toggle-heading)))
  9631. (error "The region is not a (sequence of) subtree(s)")))
  9632. (if (equal goto '(16))
  9633. (org-refile-goto-last-stored)
  9634. (when (or
  9635. (and (equal goto 2)
  9636. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9637. (prog1
  9638. (setq it (list (or org-clock-heading "running clock")
  9639. (buffer-file-name
  9640. (marker-buffer org-clock-hd-marker))
  9641. ""
  9642. (marker-position org-clock-hd-marker)))
  9643. (setq goto nil)))
  9644. (setq it (or rfloc
  9645. (let (heading-text)
  9646. (save-excursion
  9647. (unless goto
  9648. (org-back-to-heading t)
  9649. (setq heading-text
  9650. (nth 4 (org-heading-components))))
  9651. (org-refile-get-location
  9652. (cond (goto "Goto")
  9653. (regionp "Refile region to")
  9654. (t (concat "Refile subtree \""
  9655. heading-text "\" to")))
  9656. default-buffer
  9657. (and (not (equal '(4) goto))
  9658. org-refile-allow-creating-parent-nodes)
  9659. goto))))))
  9660. (setq file (nth 1 it)
  9661. re (nth 2 it)
  9662. pos (nth 3 it))
  9663. (if (and (not goto)
  9664. pos
  9665. (equal (buffer-file-name) file)
  9666. (if regionp
  9667. (and (>= pos region-start)
  9668. (<= pos region-end))
  9669. (and (>= pos (point))
  9670. (< pos (save-excursion
  9671. (org-end-of-subtree t t))))))
  9672. (error "Cannot refile to position inside the tree or region"))
  9673. (setq nbuf (or (find-buffer-visiting file)
  9674. (find-file-noselect file)))
  9675. (if goto
  9676. (progn
  9677. (org-pop-to-buffer-same-window nbuf)
  9678. (goto-char pos)
  9679. (org-show-context 'org-goto))
  9680. (if regionp
  9681. (progn
  9682. (org-kill-new (buffer-substring region-start region-end))
  9683. (org-save-markers-in-region region-start region-end))
  9684. (org-copy-subtree 1 nil t))
  9685. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9686. (find-file-noselect file)))
  9687. (setq reversed (org-notes-order-reversed-p))
  9688. (save-excursion
  9689. (save-restriction
  9690. (widen)
  9691. (if pos
  9692. (progn
  9693. (goto-char pos)
  9694. (looking-at org-outline-regexp)
  9695. (setq level (org-get-valid-level (funcall outline-level) 1))
  9696. (goto-char
  9697. (if reversed
  9698. (or (outline-next-heading) (point-max))
  9699. (or (save-excursion (org-get-next-sibling))
  9700. (org-end-of-subtree t t)
  9701. (point-max)))))
  9702. (setq level 1)
  9703. (if (not reversed)
  9704. (goto-char (point-max))
  9705. (goto-char (point-min))
  9706. (or (outline-next-heading) (goto-char (point-max)))))
  9707. (if (not (bolp)) (newline))
  9708. (org-paste-subtree level)
  9709. (when org-log-refile
  9710. (org-add-log-setup 'refile nil nil 'findpos
  9711. org-log-refile)
  9712. (unless (eq org-log-refile 'note)
  9713. (save-excursion (org-add-log-note))))
  9714. (and org-auto-align-tags (org-set-tags nil t))
  9715. (bookmark-set "org-refile-last-stored")
  9716. ;; If we are refiling for capture, make sure that the
  9717. ;; last-capture pointers point here
  9718. (when (org-bound-and-true-p org-refile-for-capture)
  9719. (bookmark-set "org-capture-last-stored-marker")
  9720. (move-marker org-capture-last-stored-marker (point)))
  9721. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9722. (run-hooks 'org-after-refile-insert-hook))))
  9723. (if regionp
  9724. (delete-region (point) (+ (point) region-length))
  9725. (org-cut-subtree))
  9726. (when (featurep 'org-inlinetask)
  9727. (org-inlinetask-remove-END-maybe))
  9728. (setq org-markers-to-move nil)
  9729. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9730. (defun org-refile-goto-last-stored ()
  9731. "Go to the location where the last refile was stored."
  9732. (interactive)
  9733. (bookmark-jump "org-refile-last-stored")
  9734. (message "This is the location of the last refile"))
  9735. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9736. no-exclude)
  9737. "Prompt the user for a refile location, using PROMPT.
  9738. PROMPT should not be suffixed with a colon and a space, because
  9739. this function appends the default value from
  9740. `org-refile-history' automatically, if that is not empty.
  9741. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9742. this is used for the GOTO interface."
  9743. (let ((org-refile-targets org-refile-targets)
  9744. (org-refile-use-outline-path org-refile-use-outline-path)
  9745. excluded-entries)
  9746. (when (and (derived-mode-p 'org-mode)
  9747. (not org-refile-use-cache)
  9748. (not no-exclude))
  9749. (org-map-tree
  9750. (lambda()
  9751. (setq excluded-entries
  9752. (append excluded-entries (list (org-get-heading t t)))))))
  9753. (setq org-refile-target-table
  9754. (org-refile-get-targets default-buffer excluded-entries)))
  9755. (unless org-refile-target-table
  9756. (error "No refile targets"))
  9757. (let* ((prompt (concat prompt
  9758. (and (car org-refile-history)
  9759. (concat " (default " (car org-refile-history) ")"))
  9760. ": "))
  9761. (cbuf (current-buffer))
  9762. (partial-completion-mode nil)
  9763. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9764. (cfunc (if (and org-refile-use-outline-path
  9765. org-outline-path-complete-in-steps)
  9766. 'org-olpath-completing-read
  9767. 'org-icompleting-read))
  9768. (extra (if org-refile-use-outline-path "/" ""))
  9769. (filename (and cfn (expand-file-name cfn)))
  9770. (tbl (mapcar
  9771. (lambda (x)
  9772. (if (and (not (member org-refile-use-outline-path
  9773. '(file full-file-path)))
  9774. (not (equal filename (nth 1 x))))
  9775. (cons (concat (car x) extra " ("
  9776. (file-name-nondirectory (nth 1 x)) ")")
  9777. (cdr x))
  9778. (cons (concat (car x) extra) (cdr x))))
  9779. org-refile-target-table))
  9780. (completion-ignore-case t)
  9781. pa answ parent-target child parent old-hist)
  9782. (setq old-hist org-refile-history)
  9783. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9784. nil 'org-refile-history (car org-refile-history)))
  9785. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9786. (org-refile-check-position pa)
  9787. (if pa
  9788. (progn
  9789. (when (or (not org-refile-history)
  9790. (not (eq old-hist org-refile-history))
  9791. (not (equal (car pa) (car org-refile-history))))
  9792. (setq org-refile-history
  9793. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9794. org-refile-history
  9795. (cdr org-refile-history))))
  9796. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9797. (pop org-refile-history)))
  9798. pa)
  9799. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9800. (progn
  9801. (setq parent (match-string 1 answ)
  9802. child (match-string 2 answ))
  9803. (setq parent-target (or (assoc parent tbl)
  9804. (assoc (concat parent "/") tbl)))
  9805. (when (and parent-target
  9806. (or (eq new-nodes t)
  9807. (and (eq new-nodes 'confirm)
  9808. (y-or-n-p (format "Create new node \"%s\"? "
  9809. child)))))
  9810. (org-refile-new-child parent-target child)))
  9811. (error "Invalid target location")))))
  9812. (declare-function org-string-nw-p "org-macs.el" (s))
  9813. (defun org-refile-check-position (refile-pointer)
  9814. "Check if the refile pointer matches the readline to which it points."
  9815. (let* ((file (nth 1 refile-pointer))
  9816. (re (nth 2 refile-pointer))
  9817. (pos (nth 3 refile-pointer))
  9818. buffer)
  9819. (when (org-string-nw-p re)
  9820. (setq buffer (if (markerp pos)
  9821. (marker-buffer pos)
  9822. (or (find-buffer-visiting file)
  9823. (find-file-noselect file))))
  9824. (with-current-buffer buffer
  9825. (save-excursion
  9826. (save-restriction
  9827. (widen)
  9828. (goto-char pos)
  9829. (beginning-of-line 1)
  9830. (unless (org-looking-at-p re)
  9831. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9832. (defun org-refile-new-child (parent-target child)
  9833. "Use refile target PARENT-TARGET to add new CHILD below it."
  9834. (unless parent-target
  9835. (error "Cannot find parent for new node"))
  9836. (let ((file (nth 1 parent-target))
  9837. (pos (nth 3 parent-target))
  9838. level)
  9839. (with-current-buffer (or (find-buffer-visiting file)
  9840. (find-file-noselect file))
  9841. (save-excursion
  9842. (save-restriction
  9843. (widen)
  9844. (if pos
  9845. (goto-char pos)
  9846. (goto-char (point-max))
  9847. (if (not (bolp)) (newline)))
  9848. (when (looking-at org-outline-regexp)
  9849. (setq level (funcall outline-level))
  9850. (org-end-of-subtree t t))
  9851. (org-back-over-empty-lines)
  9852. (insert "\n" (make-string
  9853. (if pos (org-get-valid-level level 1) 1) ?*)
  9854. " " child "\n")
  9855. (beginning-of-line 0)
  9856. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9857. (defun org-olpath-completing-read (prompt collection &rest args)
  9858. "Read an outline path like a file name."
  9859. (let ((thetable collection)
  9860. (org-completion-use-ido nil) ; does not work with ido.
  9861. (org-completion-use-iswitchb nil)) ; or iswitchb
  9862. (apply
  9863. 'org-icompleting-read prompt
  9864. (lambda (string predicate &optional flag)
  9865. (let (rtn r f (l (length string)))
  9866. (cond
  9867. ((eq flag nil)
  9868. ;; try completion
  9869. (try-completion string thetable))
  9870. ((eq flag t)
  9871. ;; all-completions
  9872. (setq rtn (all-completions string thetable predicate))
  9873. (mapcar
  9874. (lambda (x)
  9875. (setq r (substring x l))
  9876. (if (string-match " ([^)]*)$" x)
  9877. (setq f (match-string 0 x))
  9878. (setq f ""))
  9879. (if (string-match "/" r)
  9880. (concat string (substring r 0 (match-end 0)) f)
  9881. x))
  9882. rtn))
  9883. ((eq flag 'lambda)
  9884. ;; exact match?
  9885. (assoc string thetable)))
  9886. ))
  9887. args)))
  9888. ;;;; Dynamic blocks
  9889. (defun org-find-dblock (name)
  9890. "Find the first dynamic block with name NAME in the buffer.
  9891. If not found, stay at current position and return nil."
  9892. (let (pos)
  9893. (save-excursion
  9894. (goto-char (point-min))
  9895. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9896. nil t)
  9897. (match-beginning 0))))
  9898. (if pos (goto-char pos))
  9899. pos))
  9900. (defconst org-dblock-start-re
  9901. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9902. "Matches the start line of a dynamic block, with parameters.")
  9903. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9904. "Matches the end of a dynamic block.")
  9905. (defun org-create-dblock (plist)
  9906. "Create a dynamic block section, with parameters taken from PLIST.
  9907. PLIST must contain a :name entry which is used as name of the block."
  9908. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9909. (end-of-line 1)
  9910. (newline))
  9911. (let ((col (current-column))
  9912. (name (plist-get plist :name)))
  9913. (insert "#+BEGIN: " name)
  9914. (while plist
  9915. (if (eq (car plist) :name)
  9916. (setq plist (cddr plist))
  9917. (insert " " (prin1-to-string (pop plist)))))
  9918. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9919. (beginning-of-line -2)))
  9920. (defun org-prepare-dblock ()
  9921. "Prepare dynamic block for refresh.
  9922. This empties the block, puts the cursor at the insert position and returns
  9923. the property list including an extra property :name with the block name."
  9924. (unless (looking-at org-dblock-start-re)
  9925. (error "Not at a dynamic block"))
  9926. (let* ((begdel (1+ (match-end 0)))
  9927. (name (org-no-properties (match-string 1)))
  9928. (params (append (list :name name)
  9929. (read (concat "(" (match-string 3) ")")))))
  9930. (save-excursion
  9931. (beginning-of-line 1)
  9932. (skip-chars-forward " \t")
  9933. (setq params (plist-put params :indentation-column (current-column))))
  9934. (unless (re-search-forward org-dblock-end-re nil t)
  9935. (error "Dynamic block not terminated"))
  9936. (setq params
  9937. (append params
  9938. (list :content (buffer-substring
  9939. begdel (match-beginning 0)))))
  9940. (delete-region begdel (match-beginning 0))
  9941. (goto-char begdel)
  9942. (open-line 1)
  9943. params))
  9944. (defun org-map-dblocks (&optional command)
  9945. "Apply COMMAND to all dynamic blocks in the current buffer.
  9946. If COMMAND is not given, use `org-update-dblock'."
  9947. (let ((cmd (or command 'org-update-dblock)))
  9948. (save-excursion
  9949. (goto-char (point-min))
  9950. (while (re-search-forward org-dblock-start-re nil t)
  9951. (goto-char (match-beginning 0))
  9952. (save-excursion
  9953. (condition-case nil
  9954. (funcall cmd)
  9955. (error (message "Error during update of dynamic block"))))
  9956. (unless (re-search-forward org-dblock-end-re nil t)
  9957. (error "Dynamic block not terminated"))))))
  9958. (defun org-dblock-update (&optional arg)
  9959. "User command for updating dynamic blocks.
  9960. Update the dynamic block at point. With prefix ARG, update all dynamic
  9961. blocks in the buffer."
  9962. (interactive "P")
  9963. (if arg
  9964. (org-update-all-dblocks)
  9965. (or (looking-at org-dblock-start-re)
  9966. (org-beginning-of-dblock))
  9967. (org-update-dblock)))
  9968. (defun org-update-dblock ()
  9969. "Update the dynamic block at point.
  9970. This means to empty the block, parse for parameters and then call
  9971. the correct writing function."
  9972. (interactive)
  9973. (save-window-excursion
  9974. (let* ((pos (point))
  9975. (line (org-current-line))
  9976. (params (org-prepare-dblock))
  9977. (name (plist-get params :name))
  9978. (indent (plist-get params :indentation-column))
  9979. (cmd (intern (concat "org-dblock-write:" name))))
  9980. (message "Updating dynamic block `%s' at line %d..." name line)
  9981. (funcall cmd params)
  9982. (message "Updating dynamic block `%s' at line %d...done" name line)
  9983. (goto-char pos)
  9984. (when (and indent (> indent 0))
  9985. (setq indent (make-string indent ?\ ))
  9986. (save-excursion
  9987. (org-beginning-of-dblock)
  9988. (forward-line 1)
  9989. (while (not (looking-at org-dblock-end-re))
  9990. (insert indent)
  9991. (beginning-of-line 2))
  9992. (when (looking-at org-dblock-end-re)
  9993. (and (looking-at "[ \t]+")
  9994. (replace-match ""))
  9995. (insert indent)))))))
  9996. (defun org-beginning-of-dblock ()
  9997. "Find the beginning of the dynamic block at point.
  9998. Error if there is no such block at point."
  9999. (let ((pos (point))
  10000. beg)
  10001. (end-of-line 1)
  10002. (if (and (re-search-backward org-dblock-start-re nil t)
  10003. (setq beg (match-beginning 0))
  10004. (re-search-forward org-dblock-end-re nil t)
  10005. (> (match-end 0) pos))
  10006. (goto-char beg)
  10007. (goto-char pos)
  10008. (error "Not in a dynamic block"))))
  10009. ;;;###autoload
  10010. (defun org-update-all-dblocks ()
  10011. "Update all dynamic blocks in the buffer.
  10012. This function can be used in a hook."
  10013. (interactive)
  10014. (when (derived-mode-p 'org-mode)
  10015. (org-map-dblocks 'org-update-dblock)))
  10016. ;;;; Completion
  10017. (defconst org-additional-option-like-keywords
  10018. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10019. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10020. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10021. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10022. "BEGIN:" "END:"
  10023. "ORGTBL" "TBLFM:" "TBLNAME:"
  10024. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10025. "BEGIN_QUOTE" "END_QUOTE"
  10026. "BEGIN_VERSE" "END_VERSE"
  10027. "BEGIN_CENTER" "END_CENTER"
  10028. "BEGIN_SRC" "END_SRC"
  10029. "BEGIN_RESULT" "END_RESULT"
  10030. "NAME:" "RESULTS:"
  10031. "HEADER:" "HEADERS:"
  10032. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10033. "CAPTION:" "LABEL:"
  10034. "SETUPFILE:"
  10035. "INCLUDE:"
  10036. "BIND:"
  10037. "MACRO:"))
  10038. (defcustom org-structure-template-alist
  10039. '(
  10040. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10041. "<src lang=\"?\">\n\n</src>")
  10042. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10043. "<example>\n?\n</example>")
  10044. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10045. "<quote>\n?\n</quote>")
  10046. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10047. "<verse>\n?\n</verse>")
  10048. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10049. "<center>\n?\n</center>")
  10050. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10051. "<literal style=\"latex\">\n?\n</literal>")
  10052. ("L" "#+LaTeX: "
  10053. "<literal style=\"latex\">?</literal>")
  10054. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10055. "<literal style=\"html\">\n?\n</literal>")
  10056. ("H" "#+HTML: "
  10057. "<literal style=\"html\">?</literal>")
  10058. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10059. ("A" "#+ASCII: ")
  10060. ("i" "#+INDEX: ?"
  10061. "#+INDEX: ?")
  10062. ("I" "#+INCLUDE %file ?"
  10063. "<include file=%file markup=\"?\">")
  10064. )
  10065. "Structure completion elements.
  10066. This is a list of abbreviation keys and values. The value gets inserted
  10067. if you type `<' followed by the key and then press the completion key,
  10068. usually `M-TAB'. %file will be replaced by a file name after prompting
  10069. for the file using completion. The cursor will be placed at the position
  10070. of the `?` in the template.
  10071. There are two templates for each key, the first uses the original Org syntax,
  10072. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10073. the default when the /org-mtags.el/ module has been loaded. See also the
  10074. variable `org-mtags-prefer-muse-templates'."
  10075. :group 'org-completion
  10076. :type '(repeat
  10077. (string :tag "Key")
  10078. (string :tag "Template")
  10079. (string :tag "Muse Template")))
  10080. (defun org-try-structure-completion ()
  10081. "Try to complete a structure template before point.
  10082. This looks for strings like \"<e\" on an otherwise empty line and
  10083. expands them."
  10084. (let ((l (buffer-substring (point-at-bol) (point)))
  10085. a)
  10086. (when (and (looking-at "[ \t]*$")
  10087. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10088. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10089. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10090. (match-beginning 1)) a)
  10091. t)))
  10092. (defun org-complete-expand-structure-template (start cell)
  10093. "Expand a structure template."
  10094. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10095. (rpl (nth (if musep 2 1) cell))
  10096. (ind ""))
  10097. (delete-region start (point))
  10098. (when (string-match "\\`#\\+" rpl)
  10099. (cond
  10100. ((bolp))
  10101. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10102. (setq ind (buffer-substring (point-at-bol) (point))))
  10103. (t (newline))))
  10104. (setq start (point))
  10105. (if (string-match "%file" rpl)
  10106. (setq rpl (replace-match
  10107. (concat
  10108. "\""
  10109. (save-match-data
  10110. (abbreviate-file-name (read-file-name "Include file: ")))
  10111. "\"")
  10112. t t rpl)))
  10113. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10114. (concat "\n" ind)))
  10115. (insert rpl)
  10116. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10117. ;;;; TODO, DEADLINE, Comments
  10118. (defun org-toggle-comment ()
  10119. "Change the COMMENT state of an entry."
  10120. (interactive)
  10121. (save-excursion
  10122. (org-back-to-heading)
  10123. (let (case-fold-search)
  10124. (cond
  10125. ((looking-at (format org-heading-keyword-regexp-format
  10126. org-comment-string))
  10127. (goto-char (match-end 1))
  10128. (looking-at (concat " +" org-comment-string))
  10129. (replace-match "" t t)
  10130. (when (eolp) (insert " ")))
  10131. ((looking-at org-outline-regexp)
  10132. (goto-char (match-end 0))
  10133. (insert org-comment-string " "))))))
  10134. (defvar org-last-todo-state-is-todo nil
  10135. "This is non-nil when the last TODO state change led to a TODO state.
  10136. If the last change removed the TODO tag or switched to DONE, then
  10137. this is nil.")
  10138. (defvar org-setting-tags nil) ; dynamically skipped
  10139. (defvar org-todo-setup-filter-hook nil
  10140. "Hook for functions that pre-filter todo specs.
  10141. Each function takes a todo spec and returns either nil or the spec
  10142. transformed into canonical form." )
  10143. (defvar org-todo-get-default-hook nil
  10144. "Hook for functions that get a default item for todo.
  10145. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10146. nil or a string to be used for the todo mark." )
  10147. (defvar org-agenda-headline-snapshot-before-repeat)
  10148. (defun org-current-effective-time ()
  10149. "Return current time adjusted for `org-extend-today-until' variable"
  10150. (let* ((ct (org-current-time))
  10151. (dct (decode-time ct))
  10152. (ct1
  10153. (if (and org-use-effective-time
  10154. (< (nth 2 dct) org-extend-today-until))
  10155. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10156. ct)))
  10157. ct1))
  10158. (defun org-todo-yesterday (&optional arg)
  10159. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10160. (interactive "P")
  10161. (if (eq major-mode 'org-agenda-mode)
  10162. (apply 'org-agenda-todo-yesterday arg)
  10163. (let* ((hour (third (decode-time
  10164. (org-current-time))))
  10165. (org-extend-today-until (1+ hour)))
  10166. (org-todo arg))))
  10167. (defun org-todo (&optional arg)
  10168. "Change the TODO state of an item.
  10169. The state of an item is given by a keyword at the start of the heading,
  10170. like
  10171. *** TODO Write paper
  10172. *** DONE Call mom
  10173. The different keywords are specified in the variable `org-todo-keywords'.
  10174. By default the available states are \"TODO\" and \"DONE\".
  10175. So for this example: when the item starts with TODO, it is changed to DONE.
  10176. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10177. DONE are present, add TODO at the beginning of the heading.
  10178. With \\[universal-argument] prefix arg, use completion to determine the new \
  10179. state.
  10180. With numeric prefix arg, switch to that state.
  10181. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10182. keywords (nextset).
  10183. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10184. With a numeric prefix arg of 0, inhibit note taking for the change.
  10185. For calling through lisp, arg is also interpreted in the following way:
  10186. 'none -> empty state
  10187. \"\"(empty string) -> switch to empty state
  10188. 'done -> switch to DONE
  10189. 'nextset -> switch to the next set of keywords
  10190. 'previousset -> switch to the previous set of keywords
  10191. \"WAITING\" -> switch to the specified keyword, but only if it
  10192. really is a member of `org-todo-keywords'."
  10193. (interactive "P")
  10194. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10195. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10196. 'region-start-level 'region))
  10197. org-loop-over-headlines-in-active-region)
  10198. (org-map-entries
  10199. `(org-todo ,arg)
  10200. org-loop-over-headlines-in-active-region
  10201. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10202. (if (equal arg '(16)) (setq arg 'nextset))
  10203. (let ((org-blocker-hook org-blocker-hook)
  10204. (case-fold-search nil))
  10205. (when (equal arg '(64))
  10206. (setq arg nil org-blocker-hook nil))
  10207. (when (and org-blocker-hook
  10208. (or org-inhibit-blocking
  10209. (org-entry-get nil "NOBLOCKING")))
  10210. (setq org-blocker-hook nil))
  10211. (save-excursion
  10212. (catch 'exit
  10213. (org-back-to-heading t)
  10214. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10215. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10216. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10217. (let* ((match-data (match-data))
  10218. (startpos (point-at-bol))
  10219. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10220. (org-log-done org-log-done)
  10221. (org-log-repeat org-log-repeat)
  10222. (org-todo-log-states org-todo-log-states)
  10223. (org-inhibit-logging
  10224. (if (equal arg 0)
  10225. (progn (setq arg nil) 'note) org-inhibit-logging))
  10226. (this (match-string 1))
  10227. (hl-pos (match-beginning 0))
  10228. (head (org-get-todo-sequence-head this))
  10229. (ass (assoc head org-todo-kwd-alist))
  10230. (interpret (nth 1 ass))
  10231. (done-word (nth 3 ass))
  10232. (final-done-word (nth 4 ass))
  10233. (org-last-state (or this ""))
  10234. (completion-ignore-case t)
  10235. (member (member this org-todo-keywords-1))
  10236. (tail (cdr member))
  10237. (org-state (cond
  10238. ((and org-todo-key-trigger
  10239. (or (and (equal arg '(4))
  10240. (eq org-use-fast-todo-selection 'prefix))
  10241. (and (not arg) org-use-fast-todo-selection
  10242. (not (eq org-use-fast-todo-selection
  10243. 'prefix)))))
  10244. ;; Use fast selection
  10245. (org-fast-todo-selection))
  10246. ((and (equal arg '(4))
  10247. (or (not org-use-fast-todo-selection)
  10248. (not org-todo-key-trigger)))
  10249. ;; Read a state with completion
  10250. (org-icompleting-read
  10251. "State: " (mapcar (lambda(x) (list x))
  10252. org-todo-keywords-1)
  10253. nil t))
  10254. ((eq arg 'right)
  10255. (if this
  10256. (if tail (car tail) nil)
  10257. (car org-todo-keywords-1)))
  10258. ((eq arg 'left)
  10259. (if (equal member org-todo-keywords-1)
  10260. nil
  10261. (if this
  10262. (nth (- (length org-todo-keywords-1)
  10263. (length tail) 2)
  10264. org-todo-keywords-1)
  10265. (org-last org-todo-keywords-1))))
  10266. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10267. (setq arg nil))) ; hack to fall back to cycling
  10268. (arg
  10269. ;; user or caller requests a specific state
  10270. (cond
  10271. ((equal arg "") nil)
  10272. ((eq arg 'none) nil)
  10273. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10274. ((eq arg 'nextset)
  10275. (or (car (cdr (member head org-todo-heads)))
  10276. (car org-todo-heads)))
  10277. ((eq arg 'previousset)
  10278. (let ((org-todo-heads (reverse org-todo-heads)))
  10279. (or (car (cdr (member head org-todo-heads)))
  10280. (car org-todo-heads))))
  10281. ((car (member arg org-todo-keywords-1)))
  10282. ((stringp arg)
  10283. (error "State `%s' not valid in this file" arg))
  10284. ((nth (1- (prefix-numeric-value arg))
  10285. org-todo-keywords-1))))
  10286. ((null member) (or head (car org-todo-keywords-1)))
  10287. ((equal this final-done-word) nil) ;; -> make empty
  10288. ((null tail) nil) ;; -> first entry
  10289. ((memq interpret '(type priority))
  10290. (if (eq this-command last-command)
  10291. (car tail)
  10292. (if (> (length tail) 0)
  10293. (or done-word (car org-done-keywords))
  10294. nil)))
  10295. (t
  10296. (car tail))))
  10297. (org-state (or
  10298. (run-hook-with-args-until-success
  10299. 'org-todo-get-default-hook org-state org-last-state)
  10300. org-state))
  10301. (next (if org-state (concat " " org-state " ") " "))
  10302. (change-plist (list :type 'todo-state-change :from this :to org-state
  10303. :position startpos))
  10304. dolog now-done-p)
  10305. (when org-blocker-hook
  10306. (setq org-last-todo-state-is-todo
  10307. (not (member this org-done-keywords)))
  10308. (unless (save-excursion
  10309. (save-match-data
  10310. (org-with-wide-buffer
  10311. (run-hook-with-args-until-failure
  10312. 'org-blocker-hook change-plist))))
  10313. (if (org-called-interactively-p 'interactive)
  10314. (error "TODO state change from %s to %s blocked" this org-state)
  10315. ;; fail silently
  10316. (message "TODO state change from %s to %s blocked" this org-state)
  10317. (throw 'exit nil))))
  10318. (store-match-data match-data)
  10319. (replace-match next t t)
  10320. (unless (pos-visible-in-window-p hl-pos)
  10321. (message "TODO state changed to %s" (org-trim next)))
  10322. (unless head
  10323. (setq head (org-get-todo-sequence-head org-state)
  10324. ass (assoc head org-todo-kwd-alist)
  10325. interpret (nth 1 ass)
  10326. done-word (nth 3 ass)
  10327. final-done-word (nth 4 ass)))
  10328. (when (memq arg '(nextset previousset))
  10329. (message "Keyword-Set %d/%d: %s"
  10330. (- (length org-todo-sets) -1
  10331. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10332. (length org-todo-sets)
  10333. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10334. (setq org-last-todo-state-is-todo
  10335. (not (member org-state org-done-keywords)))
  10336. (setq now-done-p (and (member org-state org-done-keywords)
  10337. (not (member this org-done-keywords))))
  10338. (and logging (org-local-logging logging))
  10339. (when (and (or org-todo-log-states org-log-done)
  10340. (not (eq org-inhibit-logging t))
  10341. (not (memq arg '(nextset previousset))))
  10342. ;; we need to look at recording a time and note
  10343. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10344. (nth 2 (assoc this org-todo-log-states))))
  10345. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10346. (setq dolog 'time))
  10347. (when (and org-state
  10348. (member org-state org-not-done-keywords)
  10349. (not (member this org-not-done-keywords)))
  10350. ;; This is now a todo state and was not one before
  10351. ;; If there was a CLOSED time stamp, get rid of it.
  10352. (org-add-planning-info nil nil 'closed))
  10353. (when (and now-done-p org-log-done)
  10354. ;; It is now done, and it was not done before
  10355. (org-add-planning-info 'closed (org-current-effective-time))
  10356. (if (and (not dolog) (eq 'note org-log-done))
  10357. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10358. (when (and org-state dolog)
  10359. ;; This is a non-nil state, and we need to log it
  10360. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10361. ;; Fixup tag positioning
  10362. (org-todo-trigger-tag-changes org-state)
  10363. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10364. (when org-provide-todo-statistics
  10365. (org-update-parent-todo-statistics))
  10366. (run-hooks 'org-after-todo-state-change-hook)
  10367. (if (and arg (not (member org-state org-done-keywords)))
  10368. (setq head (org-get-todo-sequence-head org-state)))
  10369. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10370. ;; Do we need to trigger a repeat?
  10371. (when now-done-p
  10372. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10373. ;; This is for the agenda, take a snapshot of the headline.
  10374. (save-match-data
  10375. (setq org-agenda-headline-snapshot-before-repeat
  10376. (org-get-heading))))
  10377. (org-auto-repeat-maybe org-state))
  10378. ;; Fixup cursor location if close to the keyword
  10379. (if (and (outline-on-heading-p)
  10380. (not (bolp))
  10381. (save-excursion (beginning-of-line 1)
  10382. (looking-at org-todo-line-regexp))
  10383. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10384. (progn
  10385. (goto-char (or (match-end 2) (match-end 1)))
  10386. (and (looking-at " ") (just-one-space))))
  10387. (when org-trigger-hook
  10388. (save-excursion
  10389. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10390. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10391. "Block turning an entry into a TODO, using the hierarchy.
  10392. This checks whether the current task should be blocked from state
  10393. changes. Such blocking occurs when:
  10394. 1. The task has children which are not all in a completed state.
  10395. 2. A task has a parent with the property :ORDERED:, and there
  10396. are siblings prior to the current task with incomplete
  10397. status.
  10398. 3. The parent of the task is blocked because it has siblings that should
  10399. be done first, or is child of a block grandparent TODO entry."
  10400. (if (not org-enforce-todo-dependencies)
  10401. t ; if locally turned off don't block
  10402. (catch 'dont-block
  10403. ;; If this is not a todo state change, or if this entry is already DONE,
  10404. ;; do not block
  10405. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10406. (member (plist-get change-plist :from)
  10407. (cons 'done org-done-keywords))
  10408. (member (plist-get change-plist :to)
  10409. (cons 'todo org-not-done-keywords))
  10410. (not (plist-get change-plist :to)))
  10411. (throw 'dont-block t))
  10412. ;; If this task has children, and any are undone, it's blocked
  10413. (save-excursion
  10414. (org-back-to-heading t)
  10415. (let ((this-level (funcall outline-level)))
  10416. (outline-next-heading)
  10417. (let ((child-level (funcall outline-level)))
  10418. (while (and (not (eobp))
  10419. (> child-level this-level))
  10420. ;; this todo has children, check whether they are all
  10421. ;; completed
  10422. (if (and (not (org-entry-is-done-p))
  10423. (org-entry-is-todo-p))
  10424. (throw 'dont-block nil))
  10425. (outline-next-heading)
  10426. (setq child-level (funcall outline-level))))))
  10427. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10428. ;; any previous siblings are undone, it's blocked
  10429. (save-excursion
  10430. (org-back-to-heading t)
  10431. (let* ((pos (point))
  10432. (parent-pos (and (org-up-heading-safe) (point))))
  10433. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10434. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10435. (forward-line 1)
  10436. (re-search-forward org-not-done-heading-regexp pos t))
  10437. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10438. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10439. (while t
  10440. (goto-char parent-pos)
  10441. (if (not (looking-at org-not-done-heading-regexp))
  10442. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10443. (setq pos (point))
  10444. (setq parent-pos (and (org-up-heading-safe) (point)))
  10445. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10446. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10447. (forward-line 1)
  10448. (re-search-forward org-not-done-heading-regexp pos t))
  10449. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10450. (defcustom org-track-ordered-property-with-tag nil
  10451. "Should the ORDERED property also be shown as a tag?
  10452. The ORDERED property decides if an entry should require subtasks to be
  10453. completed in sequence. Since a property is not very visible, setting
  10454. this option means that toggling the ORDERED property with the command
  10455. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10456. not relevant for the behavior, but it makes things more visible.
  10457. Note that toggling the tag with tags commands will not change the property
  10458. and therefore not influence behavior!
  10459. This can be t, meaning the tag ORDERED should be used, It can also be a
  10460. string to select a different tag for this task."
  10461. :group 'org-todo
  10462. :type '(choice
  10463. (const :tag "No tracking" nil)
  10464. (const :tag "Track with ORDERED tag" t)
  10465. (string :tag "Use other tag")))
  10466. (defun org-toggle-ordered-property ()
  10467. "Toggle the ORDERED property of the current entry.
  10468. For better visibility, you can track the value of this property with a tag.
  10469. See variable `org-track-ordered-property-with-tag'."
  10470. (interactive)
  10471. (let* ((t1 org-track-ordered-property-with-tag)
  10472. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10473. (save-excursion
  10474. (org-back-to-heading)
  10475. (if (org-entry-get nil "ORDERED")
  10476. (progn
  10477. (org-delete-property "ORDERED")
  10478. (and tag (org-toggle-tag tag 'off))
  10479. (message "Subtasks can be completed in arbitrary order"))
  10480. (org-entry-put nil "ORDERED" "t")
  10481. (and tag (org-toggle-tag tag 'on))
  10482. (message "Subtasks must be completed in sequence")))))
  10483. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10484. (defun org-block-todo-from-checkboxes (change-plist)
  10485. "Block turning an entry into a TODO, using checkboxes.
  10486. This checks whether the current task should be blocked from state
  10487. changes because there are unchecked boxes in this entry."
  10488. (if (not org-enforce-todo-checkbox-dependencies)
  10489. t ; if locally turned off don't block
  10490. (catch 'dont-block
  10491. ;; If this is not a todo state change, or if this entry is already DONE,
  10492. ;; do not block
  10493. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10494. (member (plist-get change-plist :from)
  10495. (cons 'done org-done-keywords))
  10496. (member (plist-get change-plist :to)
  10497. (cons 'todo org-not-done-keywords))
  10498. (not (plist-get change-plist :to)))
  10499. (throw 'dont-block t))
  10500. ;; If this task has checkboxes that are not checked, it's blocked
  10501. (save-excursion
  10502. (org-back-to-heading t)
  10503. (let ((beg (point)) end)
  10504. (outline-next-heading)
  10505. (setq end (point))
  10506. (goto-char beg)
  10507. (if (org-list-search-forward
  10508. (concat (org-item-beginning-re)
  10509. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10510. "\\[[- ]\\]")
  10511. end t)
  10512. (progn
  10513. (if (boundp 'org-blocked-by-checkboxes)
  10514. (setq org-blocked-by-checkboxes t))
  10515. (throw 'dont-block nil)))))
  10516. t))) ; do not block
  10517. (defun org-entry-blocked-p ()
  10518. "Is the current entry blocked?"
  10519. (if (org-entry-get nil "NOBLOCKING")
  10520. nil ;; Never block this entry
  10521. (not
  10522. (run-hook-with-args-until-failure
  10523. 'org-blocker-hook
  10524. (list :type 'todo-state-change
  10525. :position (point)
  10526. :from 'todo
  10527. :to 'done)))))
  10528. (defun org-update-statistics-cookies (all)
  10529. "Update the statistics cookie, either from TODO or from checkboxes.
  10530. This should be called with the cursor in a line with a statistics cookie."
  10531. (interactive "P")
  10532. (if all
  10533. (progn
  10534. (org-update-checkbox-count 'all)
  10535. (org-map-entries 'org-update-parent-todo-statistics))
  10536. (if (not (org-at-heading-p))
  10537. (org-update-checkbox-count)
  10538. (let ((pos (move-marker (make-marker) (point)))
  10539. end l1 l2)
  10540. (ignore-errors (org-back-to-heading t))
  10541. (if (not (org-at-heading-p))
  10542. (org-update-checkbox-count)
  10543. (setq l1 (org-outline-level))
  10544. (setq end (save-excursion
  10545. (outline-next-heading)
  10546. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10547. (point)))
  10548. (if (and (save-excursion
  10549. (re-search-forward
  10550. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10551. (not (save-excursion (re-search-forward
  10552. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10553. (org-update-checkbox-count)
  10554. (if (and l2 (> l2 l1))
  10555. (progn
  10556. (goto-char end)
  10557. (org-update-parent-todo-statistics))
  10558. (goto-char pos)
  10559. (beginning-of-line 1)
  10560. (while (re-search-forward
  10561. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10562. (point-at-eol) t)
  10563. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10564. (goto-char pos)
  10565. (move-marker pos nil)))))
  10566. (defvar org-entry-property-inherited-from) ;; defined below
  10567. (defun org-update-parent-todo-statistics ()
  10568. "Update any statistics cookie in the parent of the current headline.
  10569. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10570. the entire subtree and this will travel up the hierarchy and update
  10571. statistics everywhere."
  10572. (let* ((prop (save-excursion (org-up-heading-safe)
  10573. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10574. (recursive (or (not org-hierarchical-todo-statistics)
  10575. (and prop (string-match "\\<recursive\\>" prop))))
  10576. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10577. 0))
  10578. (first t)
  10579. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10580. level ltoggle l1 new ndel
  10581. (cnt-all 0) (cnt-done 0) is-percent kwd
  10582. checkbox-beg ov ovs ove cookie-present)
  10583. (catch 'exit
  10584. (save-excursion
  10585. (beginning-of-line 1)
  10586. (setq ltoggle (funcall outline-level))
  10587. ;; Three situations are to consider:
  10588. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10589. ;; to the top-level ancestor on the headline;
  10590. ;; 2. If parent has "recursive" property, repeat up to the
  10591. ;; headline setting that property, taking inheritance into
  10592. ;; account;
  10593. ;; 3. Else, move up to direct parent and proceed only once.
  10594. (while (and (setq level (org-up-heading-safe))
  10595. (or recursive first)
  10596. (>= (point) lim))
  10597. (setq first nil cookie-present nil)
  10598. (unless (and level
  10599. (not (string-match
  10600. "\\<checkbox\\>"
  10601. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10602. "")))))
  10603. (throw 'exit nil))
  10604. (while (re-search-forward box-re (point-at-eol) t)
  10605. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10606. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10607. (save-match-data
  10608. (unless (outline-next-heading) (throw 'exit nil))
  10609. (while (and (looking-at org-complex-heading-regexp)
  10610. (> (setq l1 (length (match-string 1))) level))
  10611. (setq kwd (and (or recursive (= l1 ltoggle))
  10612. (match-string 2)))
  10613. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10614. (and (listp org-provide-todo-statistics)
  10615. (or (member kwd org-provide-todo-statistics)
  10616. (member kwd org-done-keywords))))
  10617. (setq cnt-all (1+ cnt-all))
  10618. (if (eq org-provide-todo-statistics t)
  10619. (and kwd (setq cnt-all (1+ cnt-all)))))
  10620. (and (member kwd org-done-keywords)
  10621. (setq cnt-done (1+ cnt-done)))
  10622. (outline-next-heading)))
  10623. (setq new
  10624. (if is-percent
  10625. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10626. (format "[%d/%d]" cnt-done cnt-all))
  10627. ndel (- (match-end 0) checkbox-beg))
  10628. ;; handle overlays when updating cookie from column view
  10629. (when (setq ov (car (overlays-at checkbox-beg)))
  10630. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10631. (delete-overlay ov))
  10632. (goto-char checkbox-beg)
  10633. (insert new)
  10634. (delete-region (point) (+ (point) ndel))
  10635. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10636. (when ov (move-overlay ov ovs ove)))
  10637. (when cookie-present
  10638. (run-hook-with-args 'org-after-todo-statistics-hook
  10639. cnt-done (- cnt-all cnt-done))))))
  10640. (run-hooks 'org-todo-statistics-hook)))
  10641. (defvar org-after-todo-statistics-hook nil
  10642. "Hook that is called after a TODO statistics cookie has been updated.
  10643. Each function is called with two arguments: the number of not-done entries
  10644. and the number of done entries.
  10645. For example, the following function, when added to this hook, will switch
  10646. an entry to DONE when all children are done, and back to TODO when new
  10647. entries are set to a TODO status. Note that this hook is only called
  10648. when there is a statistics cookie in the headline!
  10649. (defun org-summary-todo (n-done n-not-done)
  10650. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10651. (let (org-log-done org-log-states) ; turn off logging
  10652. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10653. ")
  10654. (defvar org-todo-statistics-hook nil
  10655. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10656. This hook runs even if there is no statistics cookie present, in which case
  10657. `org-after-todo-statistics-hook' would not run.")
  10658. (defun org-todo-trigger-tag-changes (state)
  10659. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10660. (let ((l org-todo-state-tags-triggers)
  10661. changes)
  10662. (when (or (not state) (equal state ""))
  10663. (setq changes (append changes (cdr (assoc "" l)))))
  10664. (when (and (stringp state) (> (length state) 0))
  10665. (setq changes (append changes (cdr (assoc state l)))))
  10666. (when (member state org-not-done-keywords)
  10667. (setq changes (append changes (cdr (assoc 'todo l)))))
  10668. (when (member state org-done-keywords)
  10669. (setq changes (append changes (cdr (assoc 'done l)))))
  10670. (dolist (c changes)
  10671. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10672. (defun org-local-logging (value)
  10673. "Get logging settings from a property VALUE."
  10674. (let* (words w a)
  10675. ;; directly set the variables, they are already local.
  10676. (setq org-log-done nil
  10677. org-log-repeat nil
  10678. org-todo-log-states nil)
  10679. (setq words (org-split-string value))
  10680. (while (setq w (pop words))
  10681. (cond
  10682. ((setq a (assoc w org-startup-options))
  10683. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10684. (set (nth 1 a) (nth 2 a))))
  10685. ((setq a (org-extract-log-state-settings w))
  10686. (and (member (car a) org-todo-keywords-1)
  10687. (push a org-todo-log-states)))))))
  10688. (defun org-get-todo-sequence-head (kwd)
  10689. "Return the head of the TODO sequence to which KWD belongs.
  10690. If KWD is not set, check if there is a text property remembering the
  10691. right sequence."
  10692. (let (p)
  10693. (cond
  10694. ((not kwd)
  10695. (or (get-text-property (point-at-bol) 'org-todo-head)
  10696. (progn
  10697. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10698. nil (point-at-eol)))
  10699. (get-text-property p 'org-todo-head))))
  10700. ((not (member kwd org-todo-keywords-1))
  10701. (car org-todo-keywords-1))
  10702. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10703. (defun org-fast-todo-selection ()
  10704. "Fast TODO keyword selection with single keys.
  10705. Returns the new TODO keyword, or nil if no state change should occur."
  10706. (let* ((fulltable org-todo-key-alist)
  10707. (done-keywords org-done-keywords) ;; needed for the faces.
  10708. (maxlen (apply 'max (mapcar
  10709. (lambda (x)
  10710. (if (stringp (car x)) (string-width (car x)) 0))
  10711. fulltable)))
  10712. (expert nil)
  10713. (fwidth (+ maxlen 3 1 3))
  10714. (ncol (/ (- (window-width) 4) fwidth))
  10715. tg cnt e c tbl
  10716. groups ingroup)
  10717. (save-excursion
  10718. (save-window-excursion
  10719. (if expert
  10720. (set-buffer (get-buffer-create " *Org todo*"))
  10721. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10722. (erase-buffer)
  10723. (org-set-local 'org-done-keywords done-keywords)
  10724. (setq tbl fulltable cnt 0)
  10725. (while (setq e (pop tbl))
  10726. (cond
  10727. ((equal e '(:startgroup))
  10728. (push '() groups) (setq ingroup t)
  10729. (when (not (= cnt 0))
  10730. (setq cnt 0)
  10731. (insert "\n"))
  10732. (insert "{ "))
  10733. ((equal e '(:endgroup))
  10734. (setq ingroup nil cnt 0)
  10735. (insert "}\n"))
  10736. ((equal e '(:newline))
  10737. (when (not (= cnt 0))
  10738. (setq cnt 0)
  10739. (insert "\n")
  10740. (setq e (car tbl))
  10741. (while (equal (car tbl) '(:newline))
  10742. (insert "\n")
  10743. (setq tbl (cdr tbl)))))
  10744. (t
  10745. (setq tg (car e) c (cdr e))
  10746. (if ingroup (push tg (car groups)))
  10747. (setq tg (org-add-props tg nil 'face
  10748. (org-get-todo-face tg)))
  10749. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10750. (insert "[" c "] " tg (make-string
  10751. (- fwidth 4 (length tg)) ?\ ))
  10752. (when (= (setq cnt (1+ cnt)) ncol)
  10753. (insert "\n")
  10754. (if ingroup (insert " "))
  10755. (setq cnt 0)))))
  10756. (insert "\n")
  10757. (goto-char (point-min))
  10758. (if (not expert) (org-fit-window-to-buffer))
  10759. (message "[a-z..]:Set [SPC]:clear")
  10760. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10761. (cond
  10762. ((or (= c ?\C-g)
  10763. (and (= c ?q) (not (rassoc c fulltable))))
  10764. (setq quit-flag t))
  10765. ((= c ?\ ) nil)
  10766. ((setq e (rassoc c fulltable) tg (car e))
  10767. tg)
  10768. (t (setq quit-flag t)))))))
  10769. (defun org-entry-is-todo-p ()
  10770. (member (org-get-todo-state) org-not-done-keywords))
  10771. (defun org-entry-is-done-p ()
  10772. (member (org-get-todo-state) org-done-keywords))
  10773. (defun org-get-todo-state ()
  10774. (save-excursion
  10775. (org-back-to-heading t)
  10776. (and (looking-at org-todo-line-regexp)
  10777. (match-end 2)
  10778. (match-string 2))))
  10779. (defun org-at-date-range-p (&optional inactive-ok)
  10780. "Is the cursor inside a date range?"
  10781. (interactive)
  10782. (save-excursion
  10783. (catch 'exit
  10784. (let ((pos (point)))
  10785. (skip-chars-backward "^[<\r\n")
  10786. (skip-chars-backward "<[")
  10787. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10788. (>= (match-end 0) pos)
  10789. (throw 'exit t))
  10790. (skip-chars-backward "^<[\r\n")
  10791. (skip-chars-backward "<[")
  10792. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10793. (>= (match-end 0) pos)
  10794. (throw 'exit t)))
  10795. nil)))
  10796. (defun org-get-repeat (&optional tagline)
  10797. "Check if there is a deadline/schedule with repeater in this entry."
  10798. (save-match-data
  10799. (save-excursion
  10800. (org-back-to-heading t)
  10801. (and (re-search-forward (if tagline
  10802. (concat tagline "\\s-*" org-repeat-re)
  10803. org-repeat-re)
  10804. (org-entry-end-position) t)
  10805. (match-string-no-properties 1)))))
  10806. (defvar org-last-changed-timestamp)
  10807. (defvar org-last-inserted-timestamp)
  10808. (defvar org-log-post-message)
  10809. (defvar org-log-note-purpose)
  10810. (defvar org-log-note-how)
  10811. (defvar org-log-note-extra)
  10812. (defun org-auto-repeat-maybe (done-word)
  10813. "Check if the current headline contains a repeated deadline/schedule.
  10814. If yes, set TODO state back to what it was and change the base date
  10815. of repeating deadline/scheduled time stamps to new date.
  10816. This function is run automatically after each state change to a DONE state."
  10817. ;; last-state is dynamically scoped into this function
  10818. (let* ((repeat (org-get-repeat))
  10819. (aa (assoc org-last-state org-todo-kwd-alist))
  10820. (interpret (nth 1 aa))
  10821. (head (nth 2 aa))
  10822. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  10823. (msg "Entry repeats: ")
  10824. (org-log-done nil)
  10825. (org-todo-log-states nil)
  10826. re type n what ts time to-state)
  10827. (when repeat
  10828. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10829. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10830. org-todo-repeat-to-state))
  10831. (unless (and to-state (member to-state org-todo-keywords-1))
  10832. (setq to-state (if (eq interpret 'type) org-last-state head)))
  10833. (org-todo to-state)
  10834. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10835. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10836. (org-time-stamp-format t t))))
  10837. (when org-log-repeat
  10838. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10839. (memq 'org-add-log-note post-command-hook))
  10840. ;; OK, we are already setup for some record
  10841. (if (eq org-log-repeat 'note)
  10842. ;; make sure we take a note, not only a time stamp
  10843. (setq org-log-note-how 'note))
  10844. ;; Set up for taking a record
  10845. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10846. org-last-state
  10847. 'findpos org-log-repeat)))
  10848. (org-back-to-heading t)
  10849. (org-add-planning-info nil nil 'closed)
  10850. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10851. org-deadline-time-regexp "\\)\\|\\("
  10852. org-ts-regexp "\\)"))
  10853. (while (re-search-forward
  10854. re (save-excursion (outline-next-heading) (point)) t)
  10855. (setq type (if (match-end 1) org-scheduled-string
  10856. (if (match-end 3) org-deadline-string "Plain:"))
  10857. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10858. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  10859. (setq n (string-to-number (match-string 2 ts))
  10860. what (match-string 3 ts))
  10861. (if (equal what "w") (setq n (* n 7) what "d"))
  10862. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  10863. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  10864. ;; Preparation, see if we need to modify the start date for the change
  10865. (when (match-end 1)
  10866. (setq time (save-match-data (org-time-string-to-time ts)))
  10867. (cond
  10868. ((equal (match-string 1 ts) ".")
  10869. ;; Shift starting date to today
  10870. (org-timestamp-change
  10871. (- (org-today) (time-to-days time))
  10872. 'day))
  10873. ((equal (match-string 1 ts) "+")
  10874. (let ((nshiftmax 10) (nshift 0))
  10875. (while (or (= nshift 0)
  10876. (<= (time-to-days time)
  10877. (time-to-days (current-time))))
  10878. (when (= (incf nshift) nshiftmax)
  10879. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10880. (error "Abort")))
  10881. (org-timestamp-change n (cdr (assoc what whata)))
  10882. (org-at-timestamp-p t)
  10883. (setq ts (match-string 1))
  10884. (setq time (save-match-data (org-time-string-to-time ts)))))
  10885. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10886. ;; rematch, so that we have everything in place for the real shift
  10887. (org-at-timestamp-p t)
  10888. (setq ts (match-string 1))
  10889. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  10890. (org-timestamp-change n (cdr (assoc what whata)))
  10891. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10892. (setq org-log-post-message msg)
  10893. (message "%s" msg))))
  10894. (defun org-show-todo-tree (arg)
  10895. "Make a compact tree which shows all headlines marked with TODO.
  10896. The tree will show the lines where the regexp matches, and all higher
  10897. headlines above the match.
  10898. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10899. With a numeric prefix N, construct a sparse tree for the Nth element
  10900. of `org-todo-keywords-1'."
  10901. (interactive "P")
  10902. (let ((case-fold-search nil)
  10903. (kwd-re
  10904. (cond ((null arg) org-not-done-regexp)
  10905. ((equal arg '(4))
  10906. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10907. (mapcar 'list org-todo-keywords-1))))
  10908. (concat "\\("
  10909. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10910. "\\)\\>")))
  10911. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10912. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10913. org-todo-keywords-1)))
  10914. (t (error "Invalid prefix argument: %s" arg)))))
  10915. (message "%d TODO entries found"
  10916. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10917. (defun org-deadline (&optional remove time)
  10918. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10919. With argument REMOVE, remove any deadline from the item.
  10920. With argument TIME, set the deadline at the corresponding date. TIME
  10921. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10922. (interactive "P")
  10923. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10924. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10925. 'region-start-level 'region))
  10926. org-loop-over-headlines-in-active-region)
  10927. (org-map-entries
  10928. `(org-deadline ',remove ,time)
  10929. org-loop-over-headlines-in-active-region
  10930. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10931. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10932. (repeater (and old-date
  10933. (string-match
  10934. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  10935. old-date)
  10936. (match-string 1 old-date))))
  10937. (if remove
  10938. (progn
  10939. (when (and old-date org-log-redeadline)
  10940. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10941. org-log-redeadline))
  10942. (org-remove-timestamp-with-keyword org-deadline-string)
  10943. (message "Item no longer has a deadline."))
  10944. (org-add-planning-info 'deadline time 'closed)
  10945. (when (and old-date org-log-redeadline
  10946. (not (equal old-date
  10947. (substring org-last-inserted-timestamp 1 -1))))
  10948. (org-add-log-setup 'redeadline nil old-date 'findpos
  10949. org-log-redeadline))
  10950. (when repeater
  10951. (save-excursion
  10952. (org-back-to-heading t)
  10953. (when (re-search-forward (concat org-deadline-string " "
  10954. org-last-inserted-timestamp)
  10955. (save-excursion
  10956. (outline-next-heading) (point)) t)
  10957. (goto-char (1- (match-end 0)))
  10958. (insert " " repeater)
  10959. (setq org-last-inserted-timestamp
  10960. (concat (substring org-last-inserted-timestamp 0 -1)
  10961. " " repeater
  10962. (substring org-last-inserted-timestamp -1))))))
  10963. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10964. (defun org-schedule (&optional remove time)
  10965. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10966. With argument REMOVE, remove any scheduling date from the item.
  10967. With argument TIME, scheduled at the corresponding date. TIME can
  10968. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10969. (interactive "P")
  10970. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10971. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10972. 'region-start-level 'region))
  10973. org-loop-over-headlines-in-active-region)
  10974. (org-map-entries
  10975. `(org-schedule ',remove ,time)
  10976. org-loop-over-headlines-in-active-region
  10977. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10978. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10979. (repeater (and old-date
  10980. (string-match
  10981. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  10982. old-date)
  10983. (match-string 1 old-date))))
  10984. (if remove
  10985. (progn
  10986. (when (and old-date org-log-reschedule)
  10987. (org-add-log-setup 'delschedule nil old-date 'findpos
  10988. org-log-reschedule))
  10989. (org-remove-timestamp-with-keyword org-scheduled-string)
  10990. (message "Item is no longer scheduled."))
  10991. (org-add-planning-info 'scheduled time 'closed)
  10992. (when (and old-date org-log-reschedule
  10993. (not (equal old-date
  10994. (substring org-last-inserted-timestamp 1 -1))))
  10995. (org-add-log-setup 'reschedule nil old-date 'findpos
  10996. org-log-reschedule))
  10997. (when repeater
  10998. (save-excursion
  10999. (org-back-to-heading t)
  11000. (when (re-search-forward (concat org-scheduled-string " "
  11001. org-last-inserted-timestamp)
  11002. (save-excursion
  11003. (outline-next-heading) (point)) t)
  11004. (goto-char (1- (match-end 0)))
  11005. (insert " " repeater)
  11006. (setq org-last-inserted-timestamp
  11007. (concat (substring org-last-inserted-timestamp 0 -1)
  11008. " " repeater
  11009. (substring org-last-inserted-timestamp -1))))))
  11010. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11011. (defun org-get-scheduled-time (pom &optional inherit)
  11012. "Get the scheduled time as a time tuple, of a format suitable
  11013. for calling org-schedule with, or if there is no scheduling,
  11014. returns nil."
  11015. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11016. (when time
  11017. (apply 'encode-time (org-parse-time-string time)))))
  11018. (defun org-get-deadline-time (pom &optional inherit)
  11019. "Get the deadline as a time tuple, of a format suitable for
  11020. calling org-deadline with, or if there is no scheduling, returns
  11021. nil."
  11022. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11023. (when time
  11024. (apply 'encode-time (org-parse-time-string time)))))
  11025. (defun org-remove-timestamp-with-keyword (keyword)
  11026. "Remove all time stamps with KEYWORD in the current entry."
  11027. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11028. beg)
  11029. (save-excursion
  11030. (org-back-to-heading t)
  11031. (setq beg (point))
  11032. (outline-next-heading)
  11033. (while (re-search-backward re beg t)
  11034. (replace-match "")
  11035. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11036. (equal (char-before) ?\ ))
  11037. (backward-delete-char 1)
  11038. (if (string-match "^[ \t]*$" (buffer-substring
  11039. (point-at-bol) (point-at-eol)))
  11040. (delete-region (point-at-bol)
  11041. (min (point-max) (1+ (point-at-eol))))))))))
  11042. (defun org-add-planning-info (what &optional time &rest remove)
  11043. "Insert new timestamp with keyword in the line directly after the headline.
  11044. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11045. If non is given, the user is prompted for a date.
  11046. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11047. be removed."
  11048. (interactive)
  11049. (let (org-time-was-given org-end-time-was-given ts
  11050. end default-time default-input)
  11051. (catch 'exit
  11052. (when (and (memq what '(scheduled deadline))
  11053. (or (not time)
  11054. (and (stringp time)
  11055. (string-match "^[-+]+[0-9]" time))))
  11056. ;; Try to get a default date/time from existing timestamp
  11057. (save-excursion
  11058. (org-back-to-heading t)
  11059. (setq end (save-excursion (outline-next-heading) (point)))
  11060. (when (re-search-forward (if (eq what 'scheduled)
  11061. org-scheduled-time-regexp
  11062. org-deadline-time-regexp)
  11063. end t)
  11064. (setq ts (match-string 1)
  11065. default-time
  11066. (apply 'encode-time (org-parse-time-string ts))
  11067. default-input (and ts (org-get-compact-tod ts))))))
  11068. (when what
  11069. (setq time
  11070. (if (stringp time)
  11071. ;; This is a string (relative or absolute), set proper date
  11072. (apply 'encode-time
  11073. (org-read-date-analyze
  11074. time default-time (decode-time default-time)))
  11075. ;; If necessary, get the time from the user
  11076. (or time (org-read-date nil 'to-time nil nil
  11077. default-time default-input)))))
  11078. (when (and org-insert-labeled-timestamps-at-point
  11079. (member what '(scheduled deadline)))
  11080. (insert
  11081. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11082. (org-insert-time-stamp time org-time-was-given
  11083. nil nil nil (list org-end-time-was-given))
  11084. (setq what nil))
  11085. (save-excursion
  11086. (save-restriction
  11087. (let (col list elt ts buffer-invisibility-spec)
  11088. (org-back-to-heading t)
  11089. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11090. (goto-char (match-end 1))
  11091. (setq col (current-column))
  11092. (goto-char (match-end 0))
  11093. (if (eobp) (insert "\n") (forward-char 1))
  11094. (when (and (not what)
  11095. (not (looking-at
  11096. (concat "[ \t]*"
  11097. org-keyword-time-not-clock-regexp))))
  11098. ;; Nothing to add, nothing to remove...... :-)
  11099. (throw 'exit nil))
  11100. (if (and (not (looking-at org-outline-regexp))
  11101. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11102. "[^\r\n]*"))
  11103. (not (equal (match-string 1) org-clock-string)))
  11104. (narrow-to-region (match-beginning 0) (match-end 0))
  11105. (insert-before-markers "\n")
  11106. (backward-char 1)
  11107. (narrow-to-region (point) (point))
  11108. (and org-adapt-indentation (org-indent-to-column col)))
  11109. ;; Check if we have to remove something.
  11110. (setq list (cons what remove))
  11111. (while list
  11112. (setq elt (pop list))
  11113. (when (or (and (eq elt 'scheduled)
  11114. (re-search-forward org-scheduled-time-regexp nil t))
  11115. (and (eq elt 'deadline)
  11116. (re-search-forward org-deadline-time-regexp nil t))
  11117. (and (eq elt 'closed)
  11118. (re-search-forward org-closed-time-regexp nil t)))
  11119. (replace-match "")
  11120. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11121. (and (looking-at "[ \t]+") (replace-match ""))
  11122. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11123. (when what
  11124. (insert
  11125. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11126. (cond ((eq what 'scheduled) org-scheduled-string)
  11127. ((eq what 'deadline) org-deadline-string)
  11128. ((eq what 'closed) org-closed-string))
  11129. " ")
  11130. (setq ts (org-insert-time-stamp
  11131. time
  11132. (or org-time-was-given
  11133. (and (eq what 'closed) org-log-done-with-time))
  11134. (eq what 'closed)
  11135. nil nil (list org-end-time-was-given)))
  11136. (insert
  11137. (if (not (or (bolp) (eq (char-before) ?\ )
  11138. (memq (char-after) '(32 10))
  11139. (eobp))) " " ""))
  11140. (end-of-line 1))
  11141. (goto-char (point-min))
  11142. (widen)
  11143. (if (and (looking-at "[ \t]*\n")
  11144. (equal (char-before) ?\n))
  11145. (delete-region (1- (point)) (point-at-eol)))
  11146. ts))))))
  11147. (defvar org-log-note-marker (make-marker))
  11148. (defvar org-log-note-purpose nil)
  11149. (defvar org-log-note-state nil)
  11150. (defvar org-log-note-previous-state nil)
  11151. (defvar org-log-note-how nil)
  11152. (defvar org-log-note-extra nil)
  11153. (defvar org-log-note-window-configuration nil)
  11154. (defvar org-log-note-return-to (make-marker))
  11155. (defvar org-log-note-effective-time nil
  11156. "Remembered current time so that dynamically scoped
  11157. `org-extend-today-until' affects tha timestamps in state change
  11158. log")
  11159. (defvar org-log-post-message nil
  11160. "Message to be displayed after a log note has been stored.
  11161. The auto-repeater uses this.")
  11162. (defun org-add-note ()
  11163. "Add a note to the current entry.
  11164. This is done in the same way as adding a state change note."
  11165. (interactive)
  11166. (org-add-log-setup 'note nil nil 'findpos nil))
  11167. (defvar org-property-end-re)
  11168. (defun org-add-log-setup (&optional purpose state prev-state
  11169. findpos how extra)
  11170. "Set up the post command hook to take a note.
  11171. If this is about to TODO state change, the new state is expected in STATE.
  11172. When FINDPOS is non-nil, find the correct position for the note in
  11173. the current entry. If not, assume that it can be inserted at point.
  11174. HOW is an indicator what kind of note should be created.
  11175. EXTRA is additional text that will be inserted into the notes buffer."
  11176. (let* ((org-log-into-drawer (org-log-into-drawer))
  11177. (drawer (cond ((stringp org-log-into-drawer)
  11178. org-log-into-drawer)
  11179. (org-log-into-drawer "LOGBOOK")
  11180. (t nil))))
  11181. (save-restriction
  11182. (save-excursion
  11183. (when findpos
  11184. (org-back-to-heading t)
  11185. (narrow-to-region (point) (save-excursion
  11186. (outline-next-heading) (point)))
  11187. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11188. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11189. "[^\r\n]*\\)?"))
  11190. (goto-char (match-end 0))
  11191. (cond
  11192. (drawer
  11193. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11194. nil t)
  11195. (progn
  11196. (goto-char (match-end 0))
  11197. (or org-log-states-order-reversed
  11198. (and (re-search-forward org-property-end-re nil t)
  11199. (goto-char (1- (match-beginning 0))))))
  11200. (insert "\n:" drawer ":\n:END:")
  11201. (beginning-of-line 0)
  11202. (org-indent-line-function)
  11203. (beginning-of-line 2)
  11204. (org-indent-line-function)
  11205. (end-of-line 0)))
  11206. ((and org-log-state-notes-insert-after-drawers
  11207. (save-excursion
  11208. (forward-line) (looking-at org-drawer-regexp)))
  11209. (forward-line)
  11210. (while (looking-at org-drawer-regexp)
  11211. (goto-char (match-end 0))
  11212. (re-search-forward org-property-end-re (point-max) t)
  11213. (forward-line))
  11214. (forward-line -1)))
  11215. (unless org-log-states-order-reversed
  11216. (and (= (char-after) ?\n) (forward-char 1))
  11217. (org-skip-over-state-notes)
  11218. (skip-chars-backward " \t\n\r")))
  11219. (move-marker org-log-note-marker (point))
  11220. (setq org-log-note-purpose purpose
  11221. org-log-note-state state
  11222. org-log-note-previous-state prev-state
  11223. org-log-note-how how
  11224. org-log-note-extra extra
  11225. org-log-note-effective-time (org-current-effective-time))
  11226. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11227. (defun org-skip-over-state-notes ()
  11228. "Skip past the list of State notes in an entry."
  11229. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11230. (when (ignore-errors (goto-char (org-in-item-p)))
  11231. (let* ((struct (org-list-struct))
  11232. (prevs (org-list-prevs-alist struct)))
  11233. (while (looking-at "[ \t]*- State")
  11234. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11235. (org-list-get-item-end (point) struct)))))))
  11236. (defun org-add-log-note (&optional purpose)
  11237. "Pop up a window for taking a note, and add this note later at point."
  11238. (remove-hook 'post-command-hook 'org-add-log-note)
  11239. (setq org-log-note-window-configuration (current-window-configuration))
  11240. (delete-other-windows)
  11241. (move-marker org-log-note-return-to (point))
  11242. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11243. (goto-char org-log-note-marker)
  11244. (org-switch-to-buffer-other-window "*Org Note*")
  11245. (erase-buffer)
  11246. (if (memq org-log-note-how '(time state))
  11247. (let (current-prefix-arg) (org-store-log-note))
  11248. (let ((org-inhibit-startup t)) (org-mode))
  11249. (insert (format "# Insert note for %s.
  11250. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11251. (cond
  11252. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11253. ((eq org-log-note-purpose 'done) "closed todo item")
  11254. ((eq org-log-note-purpose 'state)
  11255. (format "state change from \"%s\" to \"%s\""
  11256. (or org-log-note-previous-state "")
  11257. (or org-log-note-state "")))
  11258. ((eq org-log-note-purpose 'reschedule)
  11259. "rescheduling")
  11260. ((eq org-log-note-purpose 'delschedule)
  11261. "no longer scheduled")
  11262. ((eq org-log-note-purpose 'redeadline)
  11263. "changing deadline")
  11264. ((eq org-log-note-purpose 'deldeadline)
  11265. "removing deadline")
  11266. ((eq org-log-note-purpose 'refile)
  11267. "refiling")
  11268. ((eq org-log-note-purpose 'note)
  11269. "this entry")
  11270. (t (error "This should not happen")))))
  11271. (if org-log-note-extra (insert org-log-note-extra))
  11272. (org-set-local 'org-finish-function 'org-store-log-note)
  11273. (run-hooks 'org-log-buffer-setup-hook)))
  11274. (defvar org-note-abort nil) ; dynamically scoped
  11275. (defun org-store-log-note ()
  11276. "Finish taking a log note, and insert it to where it belongs."
  11277. (let ((txt (buffer-string))
  11278. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11279. lines ind bul)
  11280. (kill-buffer (current-buffer))
  11281. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11282. (setq txt (replace-match "" t t txt)))
  11283. (if (string-match "\\s-+\\'" txt)
  11284. (setq txt (replace-match "" t t txt)))
  11285. (setq lines (org-split-string txt "\n"))
  11286. (when (and note (string-match "\\S-" note))
  11287. (setq note
  11288. (org-replace-escapes
  11289. note
  11290. (list (cons "%u" (user-login-name))
  11291. (cons "%U" user-full-name)
  11292. (cons "%t" (format-time-string
  11293. (org-time-stamp-format 'long 'inactive)
  11294. org-log-note-effective-time))
  11295. (cons "%T" (format-time-string
  11296. (org-time-stamp-format 'long nil)
  11297. org-log-note-effective-time))
  11298. (cons "%d" (format-time-string
  11299. (org-time-stamp-format nil 'inactive)
  11300. org-log-note-effective-time))
  11301. (cons "%D" (format-time-string
  11302. (org-time-stamp-format nil nil)
  11303. org-log-note-effective-time))
  11304. (cons "%s" (if org-log-note-state
  11305. (concat "\"" org-log-note-state "\"")
  11306. ""))
  11307. (cons "%S" (if org-log-note-previous-state
  11308. (concat "\"" org-log-note-previous-state "\"")
  11309. "\"\"")))))
  11310. (if lines (setq note (concat note " \\\\")))
  11311. (push note lines))
  11312. (when (or current-prefix-arg org-note-abort)
  11313. (when org-log-into-drawer
  11314. (org-remove-empty-drawer-at
  11315. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11316. org-log-note-marker))
  11317. (setq lines nil))
  11318. (when lines
  11319. (with-current-buffer (marker-buffer org-log-note-marker)
  11320. (save-excursion
  11321. (goto-char org-log-note-marker)
  11322. (move-marker org-log-note-marker nil)
  11323. (end-of-line 1)
  11324. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11325. (setq ind (save-excursion
  11326. (if (ignore-errors (goto-char (org-in-item-p)))
  11327. (let ((struct (org-list-struct)))
  11328. (org-list-get-ind
  11329. (org-list-get-top-point struct) struct))
  11330. (skip-chars-backward " \r\t\n")
  11331. (cond
  11332. ((and (org-at-heading-p)
  11333. org-adapt-indentation)
  11334. (1+ (org-current-level)))
  11335. ((org-at-heading-p) 0)
  11336. (t (org-get-indentation))))))
  11337. (setq bul (org-list-bullet-string "-"))
  11338. (org-indent-line-to ind)
  11339. (insert bul (pop lines))
  11340. (let ((ind-body (+ (length bul) ind)))
  11341. (while lines
  11342. (insert "\n")
  11343. (org-indent-line-to ind-body)
  11344. (insert (pop lines))))
  11345. (message "Note stored")
  11346. (org-back-to-heading t)
  11347. (org-cycle-hide-drawers 'children)))))
  11348. (set-window-configuration org-log-note-window-configuration)
  11349. (with-current-buffer (marker-buffer org-log-note-return-to)
  11350. (goto-char org-log-note-return-to))
  11351. (move-marker org-log-note-return-to nil)
  11352. (and org-log-post-message (message "%s" org-log-post-message)))
  11353. (defun org-remove-empty-drawer-at (drawer pos)
  11354. "Remove an empty drawer DRAWER at position POS.
  11355. POS may also be a marker."
  11356. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11357. (save-excursion
  11358. (save-restriction
  11359. (widen)
  11360. (goto-char pos)
  11361. (if (org-in-regexp
  11362. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11363. (replace-match ""))))))
  11364. (defun org-sparse-tree (&optional arg)
  11365. "Create a sparse tree, prompt for the details.
  11366. This command can create sparse trees. You first need to select the type
  11367. of match used to create the tree:
  11368. t Show all TODO entries.
  11369. T Show entries with a specific TODO keyword.
  11370. m Show entries selected by a tags/property match.
  11371. p Enter a property name and its value (both with completion on existing
  11372. names/values) and show entries with that property.
  11373. r Show entries matching a regular expression (`/' can be used as well)
  11374. d Show deadlines due within `org-deadline-warning-days'.
  11375. b Show deadlines and scheduled items before a date.
  11376. a Show deadlines and scheduled items after a date."
  11377. (interactive "P")
  11378. (let (ans kwd value)
  11379. (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")
  11380. (setq ans (read-char-exclusive))
  11381. (cond
  11382. ((equal ans ?d)
  11383. (call-interactively 'org-check-deadlines))
  11384. ((equal ans ?b)
  11385. (call-interactively 'org-check-before-date))
  11386. ((equal ans ?a)
  11387. (call-interactively 'org-check-after-date))
  11388. ((equal ans ?D)
  11389. (call-interactively 'org-check-dates-range))
  11390. ((equal ans ?t)
  11391. (org-show-todo-tree nil))
  11392. ((equal ans ?T)
  11393. (org-show-todo-tree '(4)))
  11394. ((member ans '(?T ?m))
  11395. (call-interactively 'org-match-sparse-tree))
  11396. ((member ans '(?p ?P))
  11397. (setq kwd (org-icompleting-read "Property: "
  11398. (mapcar 'list (org-buffer-property-keys))))
  11399. (setq value (org-icompleting-read "Value: "
  11400. (mapcar 'list (org-property-values kwd))))
  11401. (unless (string-match "\\`{.*}\\'" value)
  11402. (setq value (concat "\"" value "\"")))
  11403. (org-match-sparse-tree arg (concat kwd "=" value)))
  11404. ((member ans '(?r ?R ?/))
  11405. (call-interactively 'org-occur))
  11406. (t (error "No such sparse tree command \"%c\"" ans)))))
  11407. (defvar org-occur-highlights nil
  11408. "List of overlays used for occur matches.")
  11409. (make-variable-buffer-local 'org-occur-highlights)
  11410. (defvar org-occur-parameters nil
  11411. "Parameters of the active org-occur calls.
  11412. This is a list, each call to org-occur pushes as cons cell,
  11413. containing the regular expression and the callback, onto the list.
  11414. The list can contain several entries if `org-occur' has been called
  11415. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11416. will only contain one set of parameters. When the highlights are
  11417. removed (for example with `C-c C-c', or with the next edit (depending
  11418. on `org-remove-highlights-with-change'), this variable is emptied
  11419. as well.")
  11420. (make-variable-buffer-local 'org-occur-parameters)
  11421. (defun org-occur (regexp &optional keep-previous callback)
  11422. "Make a compact tree which shows all matches of REGEXP.
  11423. The tree will show the lines where the regexp matches, and all higher
  11424. headlines above the match. It will also show the heading after the match,
  11425. to make sure editing the matching entry is easy.
  11426. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11427. call to `org-occur' will be kept, to allow stacking of calls to this
  11428. command.
  11429. If CALLBACK is non-nil, it is a function which is called to confirm
  11430. that the match should indeed be shown."
  11431. (interactive "sRegexp: \nP")
  11432. (when (equal regexp "")
  11433. (error "Regexp cannot be empty"))
  11434. (unless keep-previous
  11435. (org-remove-occur-highlights nil nil t))
  11436. (push (cons regexp callback) org-occur-parameters)
  11437. (let ((cnt 0))
  11438. (save-excursion
  11439. (goto-char (point-min))
  11440. (if (or (not keep-previous) ; do not want to keep
  11441. (not org-occur-highlights)) ; no previous matches
  11442. ;; hide everything
  11443. (org-overview))
  11444. (while (re-search-forward regexp nil t)
  11445. (when (or (not callback)
  11446. (save-match-data (funcall callback)))
  11447. (setq cnt (1+ cnt))
  11448. (when org-highlight-sparse-tree-matches
  11449. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11450. (org-show-context 'occur-tree))))
  11451. (when org-remove-highlights-with-change
  11452. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11453. nil 'local))
  11454. (unless org-sparse-tree-open-archived-trees
  11455. (org-hide-archived-subtrees (point-min) (point-max)))
  11456. (run-hooks 'org-occur-hook)
  11457. (if (org-called-interactively-p 'interactive)
  11458. (message "%d match(es) for regexp %s" cnt regexp))
  11459. cnt))
  11460. (defun org-occur-next-match (&optional n reset)
  11461. "Function for `next-error-function' to find sparse tree matches.
  11462. N is the number of matches to move, when negative move backwards.
  11463. RESET is entirely ignored - this function always goes back to the
  11464. starting point when no match is found."
  11465. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11466. (search-func (if (< n 0)
  11467. 'previous-single-char-property-change
  11468. 'next-single-char-property-change))
  11469. (n (abs n))
  11470. (pos (point))
  11471. p1)
  11472. (catch 'exit
  11473. (while (setq p1 (funcall search-func (point) 'org-type))
  11474. (when (equal p1 limit)
  11475. (goto-char pos)
  11476. (error "No more matches"))
  11477. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11478. (setq n (1- n))
  11479. (when (= n 0)
  11480. (goto-char p1)
  11481. (throw 'exit (point))))
  11482. (goto-char p1))
  11483. (goto-char p1)
  11484. (error "No more matches"))))
  11485. (defun org-show-context (&optional key)
  11486. "Make sure point and context are visible.
  11487. How much context is shown depends upon the variables
  11488. `org-show-hierarchy-above', `org-show-following-heading',
  11489. `org-show-entry-below' and `org-show-siblings'."
  11490. (let ((heading-p (org-at-heading-p t))
  11491. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11492. (following-p (org-get-alist-option org-show-following-heading key))
  11493. (entry-p (org-get-alist-option org-show-entry-below key))
  11494. (siblings-p (org-get-alist-option org-show-siblings key)))
  11495. (catch 'exit
  11496. ;; Show heading or entry text
  11497. (if (and heading-p (not entry-p))
  11498. (org-flag-heading nil) ; only show the heading
  11499. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11500. (org-show-hidden-entry))) ; show entire entry
  11501. (when following-p
  11502. ;; Show next sibling, or heading below text
  11503. (save-excursion
  11504. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11505. (org-flag-heading nil))))
  11506. (when siblings-p (org-show-siblings))
  11507. (when hierarchy-p
  11508. ;; show all higher headings, possibly with siblings
  11509. (save-excursion
  11510. (while (and (condition-case nil
  11511. (progn (org-up-heading-all 1) t)
  11512. (error nil))
  11513. (not (bobp)))
  11514. (org-flag-heading nil)
  11515. (when siblings-p (org-show-siblings))))))))
  11516. (defvar org-reveal-start-hook nil
  11517. "Hook run before revealing a location.")
  11518. (defun org-reveal (&optional siblings)
  11519. "Show current entry, hierarchy above it, and the following headline.
  11520. This can be used to show a consistent set of context around locations
  11521. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11522. not t for the search context.
  11523. With optional argument SIBLINGS, on each level of the hierarchy all
  11524. siblings are shown. This repairs the tree structure to what it would
  11525. look like when opened with hierarchical calls to `org-cycle'.
  11526. With double optional argument \\[universal-argument] \\[universal-argument], \
  11527. go to the parent and show the
  11528. entire tree."
  11529. (interactive "P")
  11530. (run-hooks 'org-reveal-start-hook)
  11531. (let ((org-show-hierarchy-above t)
  11532. (org-show-following-heading t)
  11533. (org-show-siblings (if siblings t org-show-siblings)))
  11534. (org-show-context nil))
  11535. (when (equal siblings '(16))
  11536. (save-excursion
  11537. (when (org-up-heading-safe)
  11538. (org-show-subtree)
  11539. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11540. (defun org-highlight-new-match (beg end)
  11541. "Highlight from BEG to END and mark the highlight is an occur headline."
  11542. (let ((ov (make-overlay beg end)))
  11543. (overlay-put ov 'face 'secondary-selection)
  11544. (overlay-put ov 'org-type 'org-occur)
  11545. (push ov org-occur-highlights)))
  11546. (defun org-remove-occur-highlights (&optional beg end noremove)
  11547. "Remove the occur highlights from the buffer.
  11548. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11549. from the `before-change-functions' in the current buffer."
  11550. (interactive)
  11551. (unless org-inhibit-highlight-removal
  11552. (mapc 'delete-overlay org-occur-highlights)
  11553. (setq org-occur-highlights nil)
  11554. (setq org-occur-parameters nil)
  11555. (unless noremove
  11556. (remove-hook 'before-change-functions
  11557. 'org-remove-occur-highlights 'local))))
  11558. ;;;; Priorities
  11559. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11560. "Regular expression matching the priority indicator.")
  11561. (defvar org-remove-priority-next-time nil)
  11562. (defun org-priority-up ()
  11563. "Increase the priority of the current item."
  11564. (interactive)
  11565. (org-priority 'up))
  11566. (defun org-priority-down ()
  11567. "Decrease the priority of the current item."
  11568. (interactive)
  11569. (org-priority 'down))
  11570. (defun org-priority (&optional action)
  11571. "Change the priority of an item by ARG.
  11572. ACTION can be `set', `up', `down', or a character."
  11573. (interactive)
  11574. (unless org-enable-priority-commands
  11575. (error "Priority commands are disabled"))
  11576. (setq action (or action 'set))
  11577. (let (current new news have remove)
  11578. (save-excursion
  11579. (org-back-to-heading t)
  11580. (if (looking-at org-priority-regexp)
  11581. (setq current (string-to-char (match-string 2))
  11582. have t))
  11583. (cond
  11584. ((eq action 'remove)
  11585. (setq remove t new ?\ ))
  11586. ((or (eq action 'set)
  11587. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11588. (if (not (eq action 'set))
  11589. (setq new action)
  11590. (message "Priority %c-%c, SPC to remove: "
  11591. org-highest-priority org-lowest-priority)
  11592. (save-match-data
  11593. (setq new (read-char-exclusive))))
  11594. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11595. (= (upcase org-lowest-priority) org-lowest-priority))
  11596. (setq new (upcase new)))
  11597. (cond ((equal new ?\ ) (setq remove t))
  11598. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11599. (error "Priority must be between `%c' and `%c'"
  11600. org-highest-priority org-lowest-priority))))
  11601. ((eq action 'up)
  11602. (setq new (if have
  11603. (1- current) ; normal cycling
  11604. ;; last priority was empty
  11605. (if (eq last-command this-command)
  11606. org-lowest-priority ; wrap around empty to lowest
  11607. ;; default
  11608. (if org-priority-start-cycle-with-default
  11609. org-default-priority
  11610. (1- org-default-priority))))))
  11611. ((eq action 'down)
  11612. (setq new (if have
  11613. (1+ current) ; normal cycling
  11614. ;; last priority was empty
  11615. (if (eq last-command this-command)
  11616. org-highest-priority ; wrap around empty to highest
  11617. ;; default
  11618. (if org-priority-start-cycle-with-default
  11619. org-default-priority
  11620. (1+ org-default-priority))))))
  11621. (t (error "Invalid action")))
  11622. (if (or (< (upcase new) org-highest-priority)
  11623. (> (upcase new) org-lowest-priority))
  11624. (if (and (memq action '(up down))
  11625. (not have) (not (eq last-command this-command)))
  11626. ;; `new' is from default priority
  11627. (error
  11628. "The default can not be set, see `org-default-priority' why")
  11629. ;; normal cycling: `new' is beyond highest/lowest priority
  11630. ;; and is wrapped around to the empty priority
  11631. (setq remove t)))
  11632. (setq news (format "%c" new))
  11633. (if have
  11634. (if remove
  11635. (replace-match "" t t nil 1)
  11636. (replace-match news t t nil 2))
  11637. (if remove
  11638. (error "No priority cookie found in line")
  11639. (let ((case-fold-search nil))
  11640. (looking-at org-todo-line-regexp))
  11641. (if (match-end 2)
  11642. (progn
  11643. (goto-char (match-end 2))
  11644. (insert " [#" news "]"))
  11645. (goto-char (match-beginning 3))
  11646. (insert "[#" news "] "))))
  11647. (org-preserve-lc (org-set-tags nil 'align)))
  11648. (if remove
  11649. (message "Priority removed")
  11650. (message "Priority of current item set to %s" news))))
  11651. (defun org-get-priority (s)
  11652. "Find priority cookie and return priority."
  11653. (if (functionp org-get-priority-function)
  11654. (funcall org-get-priority-function)
  11655. (save-match-data
  11656. (if (not (string-match org-priority-regexp s))
  11657. (* 1000 (- org-lowest-priority org-default-priority))
  11658. (* 1000 (- org-lowest-priority
  11659. (string-to-char (match-string 2 s))))))))
  11660. ;;;; Tags
  11661. (defvar org-agenda-archives-mode)
  11662. (defvar org-map-continue-from nil
  11663. "Position from where mapping should continue.
  11664. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11665. (defvar org-scanner-tags nil
  11666. "The current tag list while the tags scanner is running.")
  11667. (defvar org-trust-scanner-tags nil
  11668. "Should `org-get-tags-at' use the tags for the scanner.
  11669. This is for internal dynamical scoping only.
  11670. When this is non-nil, the function `org-get-tags-at' will return the value
  11671. of `org-scanner-tags' instead of building the list by itself. This
  11672. can lead to large speed-ups when the tags scanner is used in a file with
  11673. many entries, and when the list of tags is retrieved, for example to
  11674. obtain a list of properties. Building the tags list for each entry in such
  11675. a file becomes an N^2 operation - but with this variable set, it scales
  11676. as N.")
  11677. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11678. "Scan headline tags with inheritance and produce output ACTION.
  11679. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11680. or `agenda' to produce an entry list for an agenda view. It can also be
  11681. a Lisp form or a function that should be called at each matched headline, in
  11682. this case the return value is a list of all return values from these calls.
  11683. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11684. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11685. only lines with a not-done TODO keyword are included in the output.
  11686. This should be the same variable that was scoped into
  11687. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11688. START-LEVEL can be a string with asterisks, reducing the scope to
  11689. headlines matching this string."
  11690. (require 'org-agenda)
  11691. (let* ((re (concat "^"
  11692. (if start-level
  11693. ;; Get the correct level to match
  11694. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11695. org-outline-regexp)
  11696. " *\\(\\<\\("
  11697. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11698. (org-re
  11699. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11700. (props (list 'face 'default
  11701. 'done-face 'org-agenda-done
  11702. 'undone-face 'default
  11703. 'mouse-face 'highlight
  11704. 'org-not-done-regexp org-not-done-regexp
  11705. 'org-todo-regexp org-todo-regexp
  11706. 'org-complex-heading-regexp org-complex-heading-regexp
  11707. 'help-echo
  11708. (format "mouse-2 or RET jump to org file %s"
  11709. (abbreviate-file-name
  11710. (or (buffer-file-name (buffer-base-buffer))
  11711. (buffer-name (buffer-base-buffer)))))))
  11712. (case-fold-search nil)
  11713. (org-map-continue-from nil)
  11714. lspos tags tags-list
  11715. (tags-alist (list (cons 0 org-file-tags)))
  11716. (llast 0) rtn rtn1 level category i txt
  11717. todo marker entry priority)
  11718. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11719. (setq action (list 'lambda nil action)))
  11720. (save-excursion
  11721. (goto-char (point-min))
  11722. (when (eq action 'sparse-tree)
  11723. (org-overview)
  11724. (org-remove-occur-highlights))
  11725. (while (re-search-forward re nil t)
  11726. (setq org-map-continue-from nil)
  11727. (catch :skip
  11728. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11729. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11730. (goto-char (setq lspos (match-beginning 0)))
  11731. (setq level (org-reduced-level (funcall outline-level))
  11732. category (org-get-category))
  11733. (setq i llast llast level)
  11734. ;; remove tag lists from same and sublevels
  11735. (while (>= i level)
  11736. (when (setq entry (assoc i tags-alist))
  11737. (setq tags-alist (delete entry tags-alist)))
  11738. (setq i (1- i)))
  11739. ;; add the next tags
  11740. (when tags
  11741. (setq tags (org-split-string tags ":")
  11742. tags-alist
  11743. (cons (cons level tags) tags-alist)))
  11744. ;; compile tags for current headline
  11745. (setq tags-list
  11746. (if org-use-tag-inheritance
  11747. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11748. tags)
  11749. org-scanner-tags tags-list)
  11750. (when org-use-tag-inheritance
  11751. (setcdr (car tags-alist)
  11752. (mapcar (lambda (x)
  11753. (setq x (copy-sequence x))
  11754. (org-add-prop-inherited x))
  11755. (cdar tags-alist))))
  11756. (when (and tags org-use-tag-inheritance
  11757. (or (not (eq t org-use-tag-inheritance))
  11758. org-tags-exclude-from-inheritance))
  11759. ;; selective inheritance, remove uninherited ones
  11760. (setcdr (car tags-alist)
  11761. (org-remove-uninherited-tags (cdar tags-alist))))
  11762. (when (and
  11763. ;; eval matcher only when the todo condition is OK
  11764. (and (or (not todo-only) (member todo org-not-done-keywords))
  11765. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11766. (eval matcher)))
  11767. ;; Call the skipper, but return t if it does not skip,
  11768. ;; so that the `and' form continues evaluating
  11769. (progn
  11770. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11771. t)
  11772. ;; Check if timestamps are deselecting this entry
  11773. (or (not todo-only)
  11774. (and (member todo org-not-done-keywords)
  11775. (or (not org-agenda-tags-todo-honor-ignore-options)
  11776. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11777. ;; Extra check for the archive tag
  11778. ;; FIXME: Does the skipper already do this????
  11779. (or
  11780. (not (member org-archive-tag tags-list))
  11781. ;; we have an archive tag, should we use this anyway?
  11782. (or (not org-agenda-skip-archived-trees)
  11783. (and (eq action 'agenda) org-agenda-archives-mode))))
  11784. ;; select this headline
  11785. (cond
  11786. ((eq action 'sparse-tree)
  11787. (and org-highlight-sparse-tree-matches
  11788. (org-get-heading) (match-end 0)
  11789. (org-highlight-new-match
  11790. (match-beginning 1) (match-end 1)))
  11791. (org-show-context 'tags-tree))
  11792. ((eq action 'agenda)
  11793. (setq txt (org-agenda-format-item
  11794. ""
  11795. (concat
  11796. (if (eq org-tags-match-list-sublevels 'indented)
  11797. (make-string (1- level) ?.) "")
  11798. (org-get-heading))
  11799. category
  11800. tags-list)
  11801. priority (org-get-priority txt))
  11802. (goto-char lspos)
  11803. (setq marker (org-agenda-new-marker))
  11804. (org-add-props txt props
  11805. 'org-marker marker 'org-hd-marker marker 'org-category category
  11806. 'todo-state todo
  11807. 'priority priority 'type "tagsmatch")
  11808. (push txt rtn))
  11809. ((functionp action)
  11810. (setq org-map-continue-from nil)
  11811. (save-excursion
  11812. (setq rtn1 (funcall action))
  11813. (push rtn1 rtn)))
  11814. (t (error "Invalid action")))
  11815. ;; if we are to skip sublevels, jump to end of subtree
  11816. (unless org-tags-match-list-sublevels
  11817. (org-end-of-subtree t)
  11818. (backward-char 1))))
  11819. ;; Get the correct position from where to continue
  11820. (if org-map-continue-from
  11821. (goto-char org-map-continue-from)
  11822. (and (= (point) lspos) (end-of-line 1)))))
  11823. (when (and (eq action 'sparse-tree)
  11824. (not org-sparse-tree-open-archived-trees))
  11825. (org-hide-archived-subtrees (point-min) (point-max)))
  11826. (nreverse rtn)))
  11827. (defun org-remove-uninherited-tags (tags)
  11828. "Remove all tags that are not inherited from the list TAGS."
  11829. (cond
  11830. ((eq org-use-tag-inheritance t)
  11831. (if org-tags-exclude-from-inheritance
  11832. (org-delete-all org-tags-exclude-from-inheritance tags)
  11833. tags))
  11834. ((not org-use-tag-inheritance) nil)
  11835. ((stringp org-use-tag-inheritance)
  11836. (delq nil (mapcar
  11837. (lambda (x)
  11838. (if (and (string-match org-use-tag-inheritance x)
  11839. (not (member x org-tags-exclude-from-inheritance)))
  11840. x nil))
  11841. tags)))
  11842. ((listp org-use-tag-inheritance)
  11843. (delq nil (mapcar
  11844. (lambda (x)
  11845. (if (member x org-use-tag-inheritance) x nil))
  11846. tags)))))
  11847. (defun org-match-sparse-tree (&optional todo-only match)
  11848. "Create a sparse tree according to tags string MATCH.
  11849. MATCH can contain positive and negative selection of tags, like
  11850. \"+WORK+URGENT-WITHBOSS\".
  11851. If optional argument TODO-ONLY is non-nil, only select lines that are
  11852. also TODO lines."
  11853. (interactive "P")
  11854. (org-prepare-agenda-buffers (list (current-buffer)))
  11855. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11856. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11857. (defvar org-cached-props nil)
  11858. (defun org-cached-entry-get (pom property)
  11859. (if (or (eq t org-use-property-inheritance)
  11860. (and (stringp org-use-property-inheritance)
  11861. (string-match org-use-property-inheritance property))
  11862. (and (listp org-use-property-inheritance)
  11863. (member property org-use-property-inheritance)))
  11864. ;; Caching is not possible, check it directly
  11865. (org-entry-get pom property 'inherit)
  11866. ;; Get all properties, so that we can do complicated checks easily
  11867. (cdr (assoc property (or org-cached-props
  11868. (setq org-cached-props
  11869. (org-entry-properties pom)))))))
  11870. (defun org-global-tags-completion-table (&optional files)
  11871. "Return the list of all tags in all agenda buffer/files.
  11872. Optional FILES argument is a list of files to which can be used
  11873. instead of the agenda files."
  11874. (save-excursion
  11875. (org-uniquify
  11876. (delq nil
  11877. (apply 'append
  11878. (mapcar
  11879. (lambda (file)
  11880. (set-buffer (find-file-noselect file))
  11881. (append (org-get-buffer-tags)
  11882. (mapcar (lambda (x) (if (stringp (car-safe x))
  11883. (list (car-safe x)) nil))
  11884. org-tag-alist)))
  11885. (if (and files (car files))
  11886. files
  11887. (org-agenda-files))))))))
  11888. (defun org-make-tags-matcher (match)
  11889. "Create the TAGS/TODO matcher form for the selection string MATCH.
  11890. The variable `todo-only' is scoped dynamically into this function; it will be
  11891. set to t if the matcher restricts matching to TODO entries,
  11892. otherwise will not be touched.
  11893. Returns a cons of the selection string MATCH and the constructed
  11894. lisp form implementing the matcher. The matcher is to be
  11895. evaluated at an Org entry, with point on the headline,
  11896. and returns t if the entry matches the
  11897. selection string MATCH. The returned lisp form references
  11898. two variables with information about the entry, which must be
  11899. bound around the form's evaluation: todo, the TODO keyword at the
  11900. entry (or nil of none); and tags-list, the list of all tags at the
  11901. entry including inherited ones. Additionally, the category
  11902. of the entry (if any) must be specified as the text property
  11903. 'org-category on the headline.
  11904. See also `org-scan-tags'.
  11905. "
  11906. (declare (special todo-only))
  11907. (unless (boundp 'todo-only)
  11908. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  11909. (unless match
  11910. ;; Get a new match request, with completion
  11911. (let ((org-last-tags-completion-table
  11912. (org-global-tags-completion-table)))
  11913. (setq match (org-completing-read-no-i
  11914. "Match: " 'org-tags-completion-function nil nil nil
  11915. 'org-tags-history))))
  11916. ;; Parse the string and create a lisp form
  11917. (let ((match0 match)
  11918. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11919. minus tag mm
  11920. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11921. orterms term orlist re-p str-p level-p level-op time-p
  11922. prop-p pn pv po gv rest)
  11923. (if (string-match "/+" match)
  11924. ;; match contains also a todo-matching request
  11925. (progn
  11926. (setq tagsmatch (substring match 0 (match-beginning 0))
  11927. todomatch (substring match (match-end 0)))
  11928. (if (string-match "^!" todomatch)
  11929. (setq todo-only t todomatch (substring todomatch 1)))
  11930. (if (string-match "^\\s-*$" todomatch)
  11931. (setq todomatch nil)))
  11932. ;; only matching tags
  11933. (setq tagsmatch match todomatch nil))
  11934. ;; Make the tags matcher
  11935. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11936. (setq tagsmatcher t)
  11937. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11938. (while (setq term (pop orterms))
  11939. (while (and (equal (substring term -1) "\\") orterms)
  11940. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11941. (while (string-match re term)
  11942. (setq rest (substring term (match-end 0))
  11943. minus (and (match-end 1)
  11944. (equal (match-string 1 term) "-"))
  11945. tag (save-match-data (replace-regexp-in-string
  11946. "\\\\-" "-"
  11947. (match-string 2 term)))
  11948. re-p (equal (string-to-char tag) ?{)
  11949. level-p (match-end 4)
  11950. prop-p (match-end 5)
  11951. mm (cond
  11952. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11953. (level-p
  11954. (setq level-op (org-op-to-function (match-string 3 term)))
  11955. `(,level-op level ,(string-to-number
  11956. (match-string 4 term))))
  11957. (prop-p
  11958. (setq pn (match-string 5 term)
  11959. po (match-string 6 term)
  11960. pv (match-string 7 term)
  11961. re-p (equal (string-to-char pv) ?{)
  11962. str-p (equal (string-to-char pv) ?\")
  11963. time-p (save-match-data
  11964. (string-match "^\"[[<].*[]>]\"$" pv))
  11965. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11966. (if time-p (setq pv (org-matcher-time pv)))
  11967. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11968. (cond
  11969. ((equal pn "CATEGORY")
  11970. (setq gv '(get-text-property (point) 'org-category)))
  11971. ((equal pn "TODO")
  11972. (setq gv 'todo))
  11973. (t
  11974. (setq gv `(org-cached-entry-get nil ,pn))))
  11975. (if re-p
  11976. (if (eq po 'org<>)
  11977. `(not (string-match ,pv (or ,gv "")))
  11978. `(string-match ,pv (or ,gv "")))
  11979. (if str-p
  11980. `(,po (or ,gv "") ,pv)
  11981. `(,po (string-to-number (or ,gv ""))
  11982. ,(string-to-number pv) ))))
  11983. (t `(member ,tag tags-list)))
  11984. mm (if minus (list 'not mm) mm)
  11985. term rest)
  11986. (push mm tagsmatcher))
  11987. (push (if (> (length tagsmatcher) 1)
  11988. (cons 'and tagsmatcher)
  11989. (car tagsmatcher))
  11990. orlist)
  11991. (setq tagsmatcher nil))
  11992. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11993. (setq tagsmatcher
  11994. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11995. ;; Make the todo matcher
  11996. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11997. (setq todomatcher t)
  11998. (setq orterms (org-split-string todomatch "|") orlist nil)
  11999. (while (setq term (pop orterms))
  12000. (while (string-match re term)
  12001. (setq minus (and (match-end 1)
  12002. (equal (match-string 1 term) "-"))
  12003. kwd (match-string 2 term)
  12004. re-p (equal (string-to-char kwd) ?{)
  12005. term (substring term (match-end 0))
  12006. mm (if re-p
  12007. `(string-match ,(substring kwd 1 -1) todo)
  12008. (list 'equal 'todo kwd))
  12009. mm (if minus (list 'not mm) mm))
  12010. (push mm todomatcher))
  12011. (push (if (> (length todomatcher) 1)
  12012. (cons 'and todomatcher)
  12013. (car todomatcher))
  12014. orlist)
  12015. (setq todomatcher nil))
  12016. (setq todomatcher (if (> (length orlist) 1)
  12017. (cons 'or orlist) (car orlist))))
  12018. ;; Return the string and lisp forms of the matcher
  12019. (setq matcher (if todomatcher
  12020. (list 'and tagsmatcher todomatcher)
  12021. tagsmatcher))
  12022. (when todo-only
  12023. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12024. matcher)))
  12025. (cons match0 matcher)))
  12026. (defun org-op-to-function (op &optional stringp)
  12027. "Turn an operator into the appropriate function."
  12028. (setq op
  12029. (cond
  12030. ((equal op "<" ) '(< string< org-time<))
  12031. ((equal op ">" ) '(> org-string> org-time>))
  12032. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12033. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12034. ((member op '("=" "==")) '(= string= org-time=))
  12035. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12036. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12037. (defun org<> (a b) (not (= a b)))
  12038. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12039. (defun org-string>= (a b) (not (string< a b)))
  12040. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12041. (defun org-string<> (a b) (not (string= a b)))
  12042. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12043. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12044. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12045. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12046. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12047. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12048. (defun org-2ft (s)
  12049. "Convert S to a floating point time.
  12050. If S is already a number, just return it. If it is a string, parse
  12051. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12052. (cond
  12053. ((numberp s) s)
  12054. ((stringp s)
  12055. (condition-case nil
  12056. (float-time (apply 'encode-time (org-parse-time-string s)))
  12057. (error 0.)))
  12058. (t 0.)))
  12059. (defun org-time-today ()
  12060. "Time in seconds today at 0:00.
  12061. Returns the float number of seconds since the beginning of the
  12062. epoch to the beginning of today (00:00)."
  12063. (float-time (apply 'encode-time
  12064. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12065. (defun org-matcher-time (s)
  12066. "Interpret a time comparison value."
  12067. (save-match-data
  12068. (cond
  12069. ((string= s "<now>") (float-time))
  12070. ((string= s "<today>") (org-time-today))
  12071. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12072. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12073. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12074. (+ (org-time-today)
  12075. (* (string-to-number (match-string 1 s))
  12076. (cdr (assoc (match-string 2 s)
  12077. '(("d" . 86400.0) ("w" . 604800.0)
  12078. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12079. (t (org-2ft s)))))
  12080. (defun org-match-any-p (re list)
  12081. "Does re match any element of list?"
  12082. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12083. (delq nil list))
  12084. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12085. (defvar org-tags-overlay (make-overlay 1 1))
  12086. (org-detach-overlay org-tags-overlay)
  12087. (defun org-get-local-tags-at (&optional pos)
  12088. "Get a list of tags defined in the current headline."
  12089. (org-get-tags-at pos 'local))
  12090. (defun org-get-local-tags ()
  12091. "Get a list of tags defined in the current headline."
  12092. (org-get-tags-at nil 'local))
  12093. (defun org-get-tags-at (&optional pos local)
  12094. "Get a list of all headline tags applicable at POS.
  12095. POS defaults to point. If tags are inherited, the list contains
  12096. the targets in the same sequence as the headlines appear, i.e.
  12097. the tags of the current headline come last.
  12098. When LOCAL is non-nil, only return tags from the current headline,
  12099. ignore inherited ones."
  12100. (interactive)
  12101. (if (and org-trust-scanner-tags
  12102. (or (not pos) (equal pos (point)))
  12103. (not local))
  12104. org-scanner-tags
  12105. (let (tags ltags lastpos parent)
  12106. (save-excursion
  12107. (save-restriction
  12108. (widen)
  12109. (goto-char (or pos (point)))
  12110. (save-match-data
  12111. (catch 'done
  12112. (condition-case nil
  12113. (progn
  12114. (org-back-to-heading t)
  12115. (while (not (equal lastpos (point)))
  12116. (setq lastpos (point))
  12117. (when (looking-at
  12118. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12119. (setq ltags (org-split-string
  12120. (org-match-string-no-properties 1) ":"))
  12121. (when parent
  12122. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12123. (setq tags (append
  12124. (if parent
  12125. (org-remove-uninherited-tags ltags)
  12126. ltags)
  12127. tags)))
  12128. (or org-use-tag-inheritance (throw 'done t))
  12129. (if local (throw 'done t))
  12130. (or (org-up-heading-safe) (error nil))
  12131. (setq parent t)))
  12132. (error nil)))))
  12133. (if local
  12134. tags
  12135. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12136. (defun org-add-prop-inherited (s)
  12137. (add-text-properties 0 (length s) '(inherited t) s)
  12138. s)
  12139. (defun org-toggle-tag (tag &optional onoff)
  12140. "Toggle the tag TAG for the current line.
  12141. If ONOFF is `on' or `off', don't toggle but set to this state."
  12142. (let (res current)
  12143. (save-excursion
  12144. (org-back-to-heading t)
  12145. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12146. (point-at-eol) t)
  12147. (progn
  12148. (setq current (match-string 1))
  12149. (replace-match ""))
  12150. (setq current ""))
  12151. (setq current (nreverse (org-split-string current ":")))
  12152. (cond
  12153. ((eq onoff 'on)
  12154. (setq res t)
  12155. (or (member tag current) (push tag current)))
  12156. ((eq onoff 'off)
  12157. (or (not (member tag current)) (setq current (delete tag current))))
  12158. (t (if (member tag current)
  12159. (setq current (delete tag current))
  12160. (setq res t)
  12161. (push tag current))))
  12162. (end-of-line 1)
  12163. (if current
  12164. (progn
  12165. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12166. (org-set-tags nil t))
  12167. (delete-horizontal-space))
  12168. (run-hooks 'org-after-tags-change-hook))
  12169. res))
  12170. (defun org-align-tags-here (to-col)
  12171. ;; Assumes that this is a headline
  12172. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12173. (beginning-of-line 1)
  12174. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12175. (< pos (match-beginning 2)))
  12176. (progn
  12177. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12178. (goto-char (match-beginning 1))
  12179. (insert " ")
  12180. (delete-region (point) (1+ (match-beginning 2)))
  12181. (setq ncol (max (current-column)
  12182. (1+ col)
  12183. (if (> to-col 0)
  12184. to-col
  12185. (- (abs to-col) tags-l))))
  12186. (setq p (point))
  12187. (insert (make-string (- ncol (current-column)) ?\ ))
  12188. (setq ncol (current-column))
  12189. (when indent-tabs-mode (tabify p (point-at-eol)))
  12190. (org-move-to-column (min ncol col) t))
  12191. (goto-char pos))))
  12192. (defun org-set-tags-command (&optional arg just-align)
  12193. "Call the set-tags command for the current entry."
  12194. (interactive "P")
  12195. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12196. (org-set-tags arg just-align)
  12197. (save-excursion
  12198. (org-back-to-heading t)
  12199. (org-set-tags arg just-align))))
  12200. (defun org-set-tags-to (data)
  12201. "Set the tags of the current entry to DATA, replacing the current tags.
  12202. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12203. If DATA is nil or the empty string, any tags will be removed."
  12204. (interactive "sTags: ")
  12205. (setq data
  12206. (cond
  12207. ((eq data nil) "")
  12208. ((equal data "") "")
  12209. ((stringp data)
  12210. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12211. ":"))
  12212. ((listp data)
  12213. (concat ":" (mapconcat 'identity data ":") ":"))
  12214. (t nil)))
  12215. (when data
  12216. (save-excursion
  12217. (org-back-to-heading t)
  12218. (when (looking-at org-complex-heading-regexp)
  12219. (if (match-end 5)
  12220. (progn
  12221. (goto-char (match-beginning 5))
  12222. (insert data)
  12223. (delete-region (point) (point-at-eol))
  12224. (org-set-tags nil 'align))
  12225. (goto-char (point-at-eol))
  12226. (insert " " data)
  12227. (org-set-tags nil 'align)))
  12228. (beginning-of-line 1)
  12229. (if (looking-at ".*?\\([ \t]+\\)$")
  12230. (delete-region (match-beginning 1) (match-end 1))))))
  12231. (defun org-align-all-tags ()
  12232. "Align the tags i all headings."
  12233. (interactive)
  12234. (save-excursion
  12235. (or (ignore-errors (org-back-to-heading t))
  12236. (outline-next-heading))
  12237. (if (org-at-heading-p)
  12238. (org-set-tags t)
  12239. (message "No headings"))))
  12240. (defvar org-indent-indentation-per-level)
  12241. (defun org-set-tags (&optional arg just-align)
  12242. "Set the tags for the current headline.
  12243. With prefix ARG, realign all tags in headings in the current buffer."
  12244. (interactive "P")
  12245. (let* ((re org-outline-regexp-bol)
  12246. (current (unless arg (org-get-tags-string)))
  12247. (col (current-column))
  12248. (org-setting-tags t)
  12249. table current-tags inherited-tags ; computed below when needed
  12250. tags p0 c0 c1 rpl di tc level)
  12251. (if arg
  12252. (save-excursion
  12253. (goto-char (point-min))
  12254. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12255. (while (re-search-forward re nil t)
  12256. (org-set-tags nil t)
  12257. (end-of-line 1)))
  12258. (message "All tags realigned to column %d" org-tags-column))
  12259. (if just-align
  12260. (setq tags current)
  12261. ;; Get a new set of tags from the user
  12262. (save-excursion
  12263. (setq table (append org-tag-persistent-alist
  12264. (or org-tag-alist (org-get-buffer-tags))
  12265. (and
  12266. org-complete-tags-always-offer-all-agenda-tags
  12267. (org-global-tags-completion-table
  12268. (org-agenda-files))))
  12269. org-last-tags-completion-table table
  12270. current-tags (org-split-string current ":")
  12271. inherited-tags (nreverse
  12272. (nthcdr (length current-tags)
  12273. (nreverse (org-get-tags-at))))
  12274. tags
  12275. (if (or (eq t org-use-fast-tag-selection)
  12276. (and org-use-fast-tag-selection
  12277. (delq nil (mapcar 'cdr table))))
  12278. (org-fast-tag-selection
  12279. current-tags inherited-tags table
  12280. (if org-fast-tag-selection-include-todo
  12281. org-todo-key-alist))
  12282. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12283. (org-trim
  12284. (org-icompleting-read "Tags: "
  12285. 'org-tags-completion-function
  12286. nil nil current 'org-tags-history))))))
  12287. (while (string-match "[-+&]+" tags)
  12288. ;; No boolean logic, just a list
  12289. (setq tags (replace-match ":" t t tags))))
  12290. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12291. (if org-tags-sort-function
  12292. (setq tags (mapconcat 'identity
  12293. (sort (org-split-string
  12294. tags (org-re "[^[:alnum:]_@#%]+"))
  12295. org-tags-sort-function) ":")))
  12296. (if (string-match "\\`[\t ]*\\'" tags)
  12297. (setq tags "")
  12298. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12299. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12300. ;; Insert new tags at the correct column
  12301. (beginning-of-line 1)
  12302. (setq level (or (and (looking-at org-outline-regexp)
  12303. (- (match-end 0) (point) 1))
  12304. 1))
  12305. (cond
  12306. ((and (equal current "") (equal tags "")))
  12307. ((re-search-forward
  12308. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12309. (point-at-eol) t)
  12310. (if (equal tags "")
  12311. (setq rpl "")
  12312. (goto-char (match-beginning 0))
  12313. (setq c0 (current-column)
  12314. ;; compute offset for the case of org-indent-mode active
  12315. di (if org-indent-mode
  12316. (* (1- org-indent-indentation-per-level) (1- level))
  12317. 0)
  12318. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12319. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12320. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12321. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12322. (replace-match rpl t t)
  12323. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12324. tags)
  12325. (t (error "Tags alignment failed")))
  12326. (org-move-to-column col)
  12327. (unless just-align
  12328. (run-hooks 'org-after-tags-change-hook)))))
  12329. (defun org-change-tag-in-region (beg end tag off)
  12330. "Add or remove TAG for each entry in the region.
  12331. This works in the agenda, and also in an org-mode buffer."
  12332. (interactive
  12333. (list (region-beginning) (region-end)
  12334. (let ((org-last-tags-completion-table
  12335. (if (derived-mode-p 'org-mode)
  12336. (org-get-buffer-tags)
  12337. (org-global-tags-completion-table))))
  12338. (org-icompleting-read
  12339. "Tag: " 'org-tags-completion-function nil nil nil
  12340. 'org-tags-history))
  12341. (progn
  12342. (message "[s]et or [r]emove? ")
  12343. (equal (read-char-exclusive) ?r))))
  12344. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12345. (let ((agendap (equal major-mode 'org-agenda-mode))
  12346. l1 l2 m buf pos newhead (cnt 0))
  12347. (goto-char end)
  12348. (setq l2 (1- (org-current-line)))
  12349. (goto-char beg)
  12350. (setq l1 (org-current-line))
  12351. (loop for l from l1 to l2 do
  12352. (org-goto-line l)
  12353. (setq m (get-text-property (point) 'org-hd-marker))
  12354. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12355. (and agendap m))
  12356. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12357. pos (if agendap m (point)))
  12358. (with-current-buffer buf
  12359. (save-excursion
  12360. (save-restriction
  12361. (goto-char pos)
  12362. (setq cnt (1+ cnt))
  12363. (org-toggle-tag tag (if off 'off 'on))
  12364. (setq newhead (org-get-heading)))))
  12365. (and agendap (org-agenda-change-all-lines newhead m))))
  12366. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12367. (defun org-tags-completion-function (string predicate &optional flag)
  12368. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12369. (confirm (lambda (x) (stringp (car x)))))
  12370. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12371. (setq s1 (match-string 1 string)
  12372. s2 (match-string 2 string))
  12373. (setq s1 "" s2 string))
  12374. (cond
  12375. ((eq flag nil)
  12376. ;; try completion
  12377. (setq rtn (try-completion s2 ctable confirm))
  12378. (if (stringp rtn)
  12379. (setq rtn
  12380. (concat s1 s2 (substring rtn (length s2))
  12381. (if (and org-add-colon-after-tag-completion
  12382. (assoc rtn ctable))
  12383. ":" ""))))
  12384. rtn)
  12385. ((eq flag t)
  12386. ;; all-completions
  12387. (all-completions s2 ctable confirm)
  12388. )
  12389. ((eq flag 'lambda)
  12390. ;; exact match?
  12391. (assoc s2 ctable)))
  12392. ))
  12393. (defun org-fast-tag-insert (kwd tags face &optional end)
  12394. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12395. (insert (format "%-12s" (concat kwd ":"))
  12396. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12397. (or end "")))
  12398. (defun org-fast-tag-show-exit (flag)
  12399. (save-excursion
  12400. (org-goto-line 3)
  12401. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12402. (replace-match ""))
  12403. (when flag
  12404. (end-of-line 1)
  12405. (org-move-to-column (- (window-width) 19) t)
  12406. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12407. (defun org-set-current-tags-overlay (current prefix)
  12408. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12409. (if (featurep 'xemacs)
  12410. (org-overlay-display org-tags-overlay (concat prefix s)
  12411. 'secondary-selection)
  12412. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12413. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12414. (defvar org-last-tag-selection-key nil)
  12415. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12416. "Fast tag selection with single keys.
  12417. CURRENT is the current list of tags in the headline, INHERITED is the
  12418. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12419. possibly with grouping information. TODO-TABLE is a similar table with
  12420. TODO keywords, should these have keys assigned to them.
  12421. If the keys are nil, a-z are automatically assigned.
  12422. Returns the new tags string, or nil to not change the current settings."
  12423. (let* ((fulltable (append table todo-table))
  12424. (maxlen (apply 'max (mapcar
  12425. (lambda (x)
  12426. (if (stringp (car x)) (string-width (car x)) 0))
  12427. fulltable)))
  12428. (buf (current-buffer))
  12429. (expert (eq org-fast-tag-selection-single-key 'expert))
  12430. (buffer-tags nil)
  12431. (fwidth (+ maxlen 3 1 3))
  12432. (ncol (/ (- (window-width) 4) fwidth))
  12433. (i-face 'org-done)
  12434. (c-face 'org-todo)
  12435. tg cnt e c char c1 c2 ntable tbl rtn
  12436. ov-start ov-end ov-prefix
  12437. (exit-after-next org-fast-tag-selection-single-key)
  12438. (done-keywords org-done-keywords)
  12439. groups ingroup)
  12440. (save-excursion
  12441. (beginning-of-line 1)
  12442. (if (looking-at
  12443. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12444. (setq ov-start (match-beginning 1)
  12445. ov-end (match-end 1)
  12446. ov-prefix "")
  12447. (setq ov-start (1- (point-at-eol))
  12448. ov-end (1+ ov-start))
  12449. (skip-chars-forward "^\n\r")
  12450. (setq ov-prefix
  12451. (concat
  12452. (buffer-substring (1- (point)) (point))
  12453. (if (> (current-column) org-tags-column)
  12454. " "
  12455. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12456. (move-overlay org-tags-overlay ov-start ov-end)
  12457. (save-window-excursion
  12458. (if expert
  12459. (set-buffer (get-buffer-create " *Org tags*"))
  12460. (delete-other-windows)
  12461. (split-window-vertically)
  12462. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12463. (erase-buffer)
  12464. (org-set-local 'org-done-keywords done-keywords)
  12465. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12466. (org-fast-tag-insert "Current" current c-face "\n\n")
  12467. (org-fast-tag-show-exit exit-after-next)
  12468. (org-set-current-tags-overlay current ov-prefix)
  12469. (setq tbl fulltable char ?a cnt 0)
  12470. (while (setq e (pop tbl))
  12471. (cond
  12472. ((equal (car e) :startgroup)
  12473. (push '() groups) (setq ingroup t)
  12474. (when (not (= cnt 0))
  12475. (setq cnt 0)
  12476. (insert "\n"))
  12477. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12478. ((equal (car e) :endgroup)
  12479. (setq ingroup nil cnt 0)
  12480. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12481. ((equal e '(:newline))
  12482. (when (not (= cnt 0))
  12483. (setq cnt 0)
  12484. (insert "\n")
  12485. (setq e (car tbl))
  12486. (while (equal (car tbl) '(:newline))
  12487. (insert "\n")
  12488. (setq tbl (cdr tbl)))))
  12489. (t
  12490. (setq tg (copy-sequence (car e)) c2 nil)
  12491. (if (cdr e)
  12492. (setq c (cdr e))
  12493. ;; automatically assign a character.
  12494. (setq c1 (string-to-char
  12495. (downcase (substring
  12496. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12497. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12498. (while (or (rassoc char ntable) (rassoc char table))
  12499. (setq char (1+ char)))
  12500. (setq c2 c1))
  12501. (setq c (or c2 char)))
  12502. (if ingroup (push tg (car groups)))
  12503. (setq tg (org-add-props tg nil 'face
  12504. (cond
  12505. ((not (assoc tg table))
  12506. (org-get-todo-face tg))
  12507. ((member tg current) c-face)
  12508. ((member tg inherited) i-face)
  12509. (t nil))))
  12510. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12511. (insert "[" c "] " tg (make-string
  12512. (- fwidth 4 (length tg)) ?\ ))
  12513. (push (cons tg c) ntable)
  12514. (when (= (setq cnt (1+ cnt)) ncol)
  12515. (insert "\n")
  12516. (if ingroup (insert " "))
  12517. (setq cnt 0)))))
  12518. (setq ntable (nreverse ntable))
  12519. (insert "\n")
  12520. (goto-char (point-min))
  12521. (if (not expert) (org-fit-window-to-buffer))
  12522. (setq rtn
  12523. (catch 'exit
  12524. (while t
  12525. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12526. (if (not groups) "no " "")
  12527. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12528. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12529. (setq org-last-tag-selection-key c)
  12530. (cond
  12531. ((= c ?\r) (throw 'exit t))
  12532. ((= c ?!)
  12533. (setq groups (not groups))
  12534. (goto-char (point-min))
  12535. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12536. ((= c ?\C-c)
  12537. (if (not expert)
  12538. (org-fast-tag-show-exit
  12539. (setq exit-after-next (not exit-after-next)))
  12540. (setq expert nil)
  12541. (delete-other-windows)
  12542. (set-window-buffer (split-window-vertically) " *Org tags*")
  12543. (org-switch-to-buffer-other-window " *Org tags*")
  12544. (org-fit-window-to-buffer)))
  12545. ((or (= c ?\C-g)
  12546. (and (= c ?q) (not (rassoc c ntable))))
  12547. (org-detach-overlay org-tags-overlay)
  12548. (setq quit-flag t))
  12549. ((= c ?\ )
  12550. (setq current nil)
  12551. (if exit-after-next (setq exit-after-next 'now)))
  12552. ((= c ?\t)
  12553. (condition-case nil
  12554. (setq tg (org-icompleting-read
  12555. "Tag: "
  12556. (or buffer-tags
  12557. (with-current-buffer buf
  12558. (org-get-buffer-tags)))))
  12559. (quit (setq tg "")))
  12560. (when (string-match "\\S-" tg)
  12561. (add-to-list 'buffer-tags (list tg))
  12562. (if (member tg current)
  12563. (setq current (delete tg current))
  12564. (push tg current)))
  12565. (if exit-after-next (setq exit-after-next 'now)))
  12566. ((setq e (rassoc c todo-table) tg (car e))
  12567. (with-current-buffer buf
  12568. (save-excursion (org-todo tg)))
  12569. (if exit-after-next (setq exit-after-next 'now)))
  12570. ((setq e (rassoc c ntable) tg (car e))
  12571. (if (member tg current)
  12572. (setq current (delete tg current))
  12573. (loop for g in groups do
  12574. (if (member tg g)
  12575. (mapc (lambda (x)
  12576. (setq current (delete x current)))
  12577. g)))
  12578. (push tg current))
  12579. (if exit-after-next (setq exit-after-next 'now))))
  12580. ;; Create a sorted list
  12581. (setq current
  12582. (sort current
  12583. (lambda (a b)
  12584. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12585. (if (eq exit-after-next 'now) (throw 'exit t))
  12586. (goto-char (point-min))
  12587. (beginning-of-line 2)
  12588. (delete-region (point) (point-at-eol))
  12589. (org-fast-tag-insert "Current" current c-face)
  12590. (org-set-current-tags-overlay current ov-prefix)
  12591. (while (re-search-forward
  12592. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12593. (setq tg (match-string 1))
  12594. (add-text-properties
  12595. (match-beginning 1) (match-end 1)
  12596. (list 'face
  12597. (cond
  12598. ((member tg current) c-face)
  12599. ((member tg inherited) i-face)
  12600. (t (get-text-property (match-beginning 1) 'face))))))
  12601. (goto-char (point-min)))))
  12602. (org-detach-overlay org-tags-overlay)
  12603. (if rtn
  12604. (mapconcat 'identity current ":")
  12605. nil))))
  12606. (defun org-get-tags-string ()
  12607. "Get the TAGS string in the current headline."
  12608. (unless (org-at-heading-p t)
  12609. (error "Not on a heading"))
  12610. (save-excursion
  12611. (beginning-of-line 1)
  12612. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12613. (org-match-string-no-properties 1)
  12614. "")))
  12615. (defun org-get-tags ()
  12616. "Get the list of tags specified in the current headline."
  12617. (org-split-string (org-get-tags-string) ":"))
  12618. (defun org-get-buffer-tags ()
  12619. "Get a table of all tags used in the buffer, for completion."
  12620. (let (tags)
  12621. (save-excursion
  12622. (goto-char (point-min))
  12623. (while (re-search-forward
  12624. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12625. (when (equal (char-after (point-at-bol 0)) ?*)
  12626. (mapc (lambda (x) (add-to-list 'tags x))
  12627. (org-split-string (org-match-string-no-properties 1) ":")))))
  12628. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12629. (mapcar 'list tags)))
  12630. ;;;; The mapping API
  12631. ;;;###autoload
  12632. (defun org-map-entries (func &optional match scope &rest skip)
  12633. "Call FUNC at each headline selected by MATCH in SCOPE.
  12634. FUNC is a function or a lisp form. The function will be called without
  12635. arguments, with the cursor positioned at the beginning of the headline.
  12636. The return values of all calls to the function will be collected and
  12637. returned as a list.
  12638. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12639. does not need to preserve point. After evaluation, the cursor will be
  12640. moved to the end of the line (presumably of the headline of the
  12641. processed entry) and search continues from there. Under some
  12642. circumstances, this may not produce the wanted results. For example,
  12643. if you have removed (e.g. archived) the current (sub)tree it could
  12644. mean that the next entry will be skipped entirely. In such cases, you
  12645. can specify the position from where search should continue by making
  12646. FUNC set the variable `org-map-continue-from' to the desired buffer
  12647. position.
  12648. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12649. Only headlines that are matched by this query will be considered during
  12650. the iteration. When MATCH is nil or t, all headlines will be
  12651. visited by the iteration.
  12652. SCOPE determines the scope of this command. It can be any of:
  12653. nil The current buffer, respecting the restriction if any
  12654. tree The subtree started with the entry at point
  12655. region The entries within the active region, if any
  12656. region-start-level
  12657. The entries within the active region, but only those at
  12658. the same level than the first one.
  12659. file The current buffer, without restriction
  12660. file-with-archives
  12661. The current buffer, and any archives associated with it
  12662. agenda All agenda files
  12663. agenda-with-archives
  12664. All agenda files with any archive files associated with them
  12665. \(file1 file2 ...)
  12666. If this is a list, all files in the list will be scanned
  12667. The remaining args are treated as settings for the skipping facilities of
  12668. the scanner. The following items can be given here:
  12669. archive skip trees with the archive tag.
  12670. comment skip trees with the COMMENT keyword
  12671. function or Emacs Lisp form:
  12672. will be used as value for `org-agenda-skip-function', so whenever
  12673. the function returns t, FUNC will not be called for that
  12674. entry and search will continue from the point where the
  12675. function leaves it.
  12676. If your function needs to retrieve the tags including inherited tags
  12677. at the *current* entry, you can use the value of the variable
  12678. `org-scanner-tags' which will be much faster than getting the value
  12679. with `org-get-tags-at'. If your function gets properties with
  12680. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12681. to t around the call to `org-entry-properties' to get the same speedup.
  12682. Note that if your function moves around to retrieve tags and properties at
  12683. a *different* entry, you cannot use these techniques."
  12684. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12685. (not (org-region-active-p)))
  12686. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12687. (org-agenda-skip-archived-trees (memq 'archive skip))
  12688. (org-agenda-skip-comment-trees (memq 'comment skip))
  12689. (org-agenda-skip-function
  12690. (car (org-delete-all '(comment archive) skip)))
  12691. (org-tags-match-list-sublevels t)
  12692. (start-level (eq scope 'region-start-level))
  12693. matcher file res
  12694. org-todo-keywords-for-agenda
  12695. org-done-keywords-for-agenda
  12696. org-todo-keyword-alist-for-agenda
  12697. org-drawers-for-agenda
  12698. org-tag-alist-for-agenda
  12699. todo-only)
  12700. (cond
  12701. ((eq match t) (setq matcher t))
  12702. ((eq match nil) (setq matcher t))
  12703. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12704. (save-excursion
  12705. (save-restriction
  12706. (cond ((eq scope 'tree)
  12707. (org-back-to-heading t)
  12708. (org-narrow-to-subtree)
  12709. (setq scope nil))
  12710. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12711. (org-region-active-p))
  12712. ;; If needed, set start-level to a string like "2"
  12713. (when start-level
  12714. (save-excursion
  12715. (goto-char (region-beginning))
  12716. (unless (org-at-heading-p) (outline-next-heading))
  12717. (setq start-level (org-current-level))))
  12718. (narrow-to-region (region-beginning)
  12719. (save-excursion
  12720. (goto-char (region-end))
  12721. (unless (and (bolp) (org-at-heading-p))
  12722. (outline-next-heading))
  12723. (point)))
  12724. (setq scope nil)))
  12725. (if (not scope)
  12726. (progn
  12727. (org-prepare-agenda-buffers
  12728. (list (buffer-file-name (current-buffer))))
  12729. (setq res (org-scan-tags func matcher todo-only start-level)))
  12730. ;; Get the right scope
  12731. (cond
  12732. ((and scope (listp scope) (symbolp (car scope)))
  12733. (setq scope (eval scope)))
  12734. ((eq scope 'agenda)
  12735. (setq scope (org-agenda-files t)))
  12736. ((eq scope 'agenda-with-archives)
  12737. (setq scope (org-agenda-files t))
  12738. (setq scope (org-add-archive-files scope)))
  12739. ((eq scope 'file)
  12740. (setq scope (list (buffer-file-name))))
  12741. ((eq scope 'file-with-archives)
  12742. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12743. (org-prepare-agenda-buffers scope)
  12744. (while (setq file (pop scope))
  12745. (with-current-buffer (org-find-base-buffer-visiting file)
  12746. (save-excursion
  12747. (save-restriction
  12748. (widen)
  12749. (goto-char (point-min))
  12750. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12751. res)))
  12752. ;;;; Properties
  12753. ;;; Setting and retrieving properties
  12754. (defconst org-special-properties
  12755. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12756. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12757. "The special properties valid in Org-mode.
  12758. These are properties that are not defined in the property drawer,
  12759. but in some other way.")
  12760. (defconst org-default-properties
  12761. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12762. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12763. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12764. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12765. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12766. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12767. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12768. "Some properties that are used by Org-mode for various purposes.
  12769. Being in this list makes sure that they are offered for completion.")
  12770. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12771. "Regular expression matching the first line of a property drawer.")
  12772. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12773. "Regular expression matching the last line of a property drawer.")
  12774. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12775. "Regular expression matching the first line of a property drawer.")
  12776. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12777. "Regular expression matching the first line of a property drawer.")
  12778. (defconst org-property-drawer-re
  12779. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12780. org-property-end-re "\\)\n?")
  12781. "Matches an entire property drawer.")
  12782. (defconst org-clock-drawer-re
  12783. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12784. org-property-end-re "\\)\n?")
  12785. "Matches an entire clock drawer.")
  12786. (defsubst org-re-property (property)
  12787. "Return a regexp matching PROPERTY.
  12788. Match group 1 will be set to the value "
  12789. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12790. (defun org-property-action ()
  12791. "Do an action on properties."
  12792. (interactive)
  12793. (let (c)
  12794. (org-at-property-p)
  12795. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12796. (setq c (read-char-exclusive))
  12797. (cond
  12798. ((equal c ?s)
  12799. (call-interactively 'org-set-property))
  12800. ((equal c ?d)
  12801. (call-interactively 'org-delete-property))
  12802. ((equal c ?D)
  12803. (call-interactively 'org-delete-property-globally))
  12804. ((equal c ?c)
  12805. (call-interactively 'org-compute-property-at-point))
  12806. (t (error "No such property action %c" c)))))
  12807. (defun org-set-effort (&optional value)
  12808. "Set the effort property of the current entry.
  12809. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12810. allowed value."
  12811. (interactive "P")
  12812. (if (equal value 0) (setq value 10))
  12813. (let* ((completion-ignore-case t)
  12814. (prop org-effort-property)
  12815. (cur (org-entry-get nil prop))
  12816. (allowed (org-property-get-allowed-values nil prop 'table))
  12817. (existing (mapcar 'list (org-property-values prop)))
  12818. rpl
  12819. (val (cond
  12820. ((stringp value) value)
  12821. ((and allowed (integerp value))
  12822. (or (car (nth (1- value) allowed))
  12823. (car (org-last allowed))))
  12824. (allowed
  12825. (message "Select 1-9,0, [RET%s]: %s"
  12826. (if cur (concat "=" cur) "")
  12827. (mapconcat 'car allowed " "))
  12828. (setq rpl (read-char-exclusive))
  12829. (if (equal rpl ?\r)
  12830. cur
  12831. (setq rpl (- rpl ?0))
  12832. (if (equal rpl 0) (setq rpl 10))
  12833. (if (and (> rpl 0) (<= rpl (length allowed)))
  12834. (car (nth (1- rpl) allowed))
  12835. (org-completing-read "Effort: " allowed nil))))
  12836. (t
  12837. (let (org-completion-use-ido org-completion-use-iswitchb)
  12838. (org-completing-read
  12839. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12840. (concat "[" cur "]") "")
  12841. ": ")
  12842. existing nil nil "" nil cur))))))
  12843. (unless (equal (org-entry-get nil prop) val)
  12844. (org-entry-put nil prop val))
  12845. (message "%s is now %s" prop val)))
  12846. (defun org-at-property-p ()
  12847. "Is cursor inside a property drawer?"
  12848. (save-excursion
  12849. (beginning-of-line 1)
  12850. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12851. (save-match-data ;; Used by calling procedures
  12852. (let ((p (point))
  12853. (range (unless (org-before-first-heading-p)
  12854. (org-get-property-block))))
  12855. (and range (<= (car range) p) (< p (cdr range))))))))
  12856. (defun org-get-property-block (&optional beg end force)
  12857. "Return the (beg . end) range of the body of the property drawer.
  12858. BEG and END can be beginning and end of subtree, if not given
  12859. they will be found.
  12860. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12861. (catch 'exit
  12862. (save-excursion
  12863. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12864. (end (or end (progn (outline-next-heading) (point)))))
  12865. (goto-char beg)
  12866. (if (re-search-forward org-property-start-re end t)
  12867. (setq beg (1+ (match-end 0)))
  12868. (if force
  12869. (save-excursion
  12870. (org-insert-property-drawer)
  12871. (setq end (progn (outline-next-heading) (point))))
  12872. (throw 'exit nil))
  12873. (goto-char beg)
  12874. (if (re-search-forward org-property-start-re end t)
  12875. (setq beg (1+ (match-end 0)))))
  12876. (if (re-search-forward org-property-end-re end t)
  12877. (setq end (match-beginning 0))
  12878. (or force (throw 'exit nil))
  12879. (goto-char beg)
  12880. (setq end beg)
  12881. (org-indent-line-function)
  12882. (insert ":END:\n"))
  12883. (cons beg end)))))
  12884. (defun org-entry-properties (&optional pom which specific)
  12885. "Get all properties of the entry at point-or-marker POM.
  12886. This includes the TODO keyword, the tags, time strings for deadline,
  12887. scheduled, and clocking, and any additional properties defined in the
  12888. entry. The return value is an alist, keys may occur multiple times
  12889. if the property key was used several times.
  12890. POM may also be nil, in which case the current entry is used.
  12891. If WHICH is nil or `all', get all properties. If WHICH is
  12892. `special' or `standard', only get that subclass. If WHICH
  12893. is a string only get exactly this property. SPECIFIC can be a string, the
  12894. specific property we are interested in. Specifying it can speed
  12895. things up because then unnecessary parsing is avoided."
  12896. (setq which (or which 'all))
  12897. (org-with-point-at pom
  12898. (let ((clockstr (substring org-clock-string 0 -1))
  12899. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12900. (case-fold-search nil)
  12901. beg end range props sum-props key key1 value string clocksum)
  12902. (save-excursion
  12903. (when (condition-case nil
  12904. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  12905. (error nil))
  12906. (setq beg (point))
  12907. (setq sum-props (get-text-property (point) 'org-summaries))
  12908. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12909. (outline-next-heading)
  12910. (setq end (point))
  12911. (when (memq which '(all special))
  12912. ;; Get the special properties, like TODO and tags
  12913. (goto-char beg)
  12914. (when (and (or (not specific) (string= specific "TODO"))
  12915. (looking-at org-todo-line-regexp) (match-end 2))
  12916. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12917. (when (and (or (not specific) (string= specific "PRIORITY"))
  12918. (looking-at org-priority-regexp))
  12919. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12920. (when (or (not specific) (string= specific "FILE"))
  12921. (push (cons "FILE" buffer-file-name) props))
  12922. (when (and (or (not specific) (string= specific "TAGS"))
  12923. (setq value (org-get-tags-string))
  12924. (string-match "\\S-" value))
  12925. (push (cons "TAGS" value) props))
  12926. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12927. (setq value (org-get-tags-at)))
  12928. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12929. ":"))
  12930. props))
  12931. (when (or (not specific) (string= specific "BLOCKED"))
  12932. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12933. (when (or (not specific)
  12934. (member specific
  12935. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12936. "TIMESTAMP" "TIMESTAMP_IA")))
  12937. (catch 'match
  12938. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12939. (setq key (if (match-end 1)
  12940. (substring (org-match-string-no-properties 1)
  12941. 0 -1))
  12942. string (if (equal key clockstr)
  12943. (org-no-properties
  12944. (org-trim
  12945. (buffer-substring
  12946. (match-beginning 3) (goto-char
  12947. (point-at-eol)))))
  12948. (substring (org-match-string-no-properties 3)
  12949. 1 -1)))
  12950. ;; Get the correct property name from the key. This is
  12951. ;; necessary if the user has configured time keywords.
  12952. (setq key1 (concat key ":"))
  12953. (cond
  12954. ((not key)
  12955. (setq key
  12956. (if (= (char-after (match-beginning 3)) ?\[)
  12957. "TIMESTAMP_IA" "TIMESTAMP")))
  12958. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12959. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12960. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12961. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12962. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12963. (progn
  12964. (push (cons key string) props)
  12965. ;; no need to search further if match is found
  12966. (throw 'match t))
  12967. (when (or (equal key "CLOCK") (not (assoc key props)))
  12968. (push (cons key string) props))))))
  12969. )
  12970. (when (memq which '(all standard))
  12971. ;; Get the standard properties, like :PROP: ...
  12972. (setq range (org-get-property-block beg end))
  12973. (when range
  12974. (goto-char (car range))
  12975. (while (re-search-forward
  12976. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12977. (cdr range) t)
  12978. (setq key (org-match-string-no-properties 1)
  12979. value (org-trim (or (org-match-string-no-properties 2) "")))
  12980. (unless (member key excluded)
  12981. (push (cons key (or value "")) props)))))
  12982. (if clocksum
  12983. (push (cons "CLOCKSUM"
  12984. (org-columns-number-to-string (/ (float clocksum) 60.)
  12985. 'add_times))
  12986. props))
  12987. (unless (assoc "CATEGORY" props)
  12988. (push (cons "CATEGORY" (org-get-category)) props))
  12989. (append sum-props (nreverse props)))))))
  12990. (defun org-entry-get (pom property &optional inherit literal-nil)
  12991. "Get value of PROPERTY for entry at point-or-marker POM.
  12992. If INHERIT is non-nil and the entry does not have the property,
  12993. then also check higher levels of the hierarchy.
  12994. If INHERIT is the symbol `selective', use inheritance only if the setting
  12995. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12996. If the property is present but empty, the return value is the empty string.
  12997. If the property is not present at all, nil is returned.
  12998. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12999. do not interpret it as the list atom nil. This is used for inheritance
  13000. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13001. (org-with-point-at pom
  13002. (if (and inherit (if (eq inherit 'selective)
  13003. (org-property-inherit-p property)
  13004. t))
  13005. (org-entry-get-with-inheritance property literal-nil)
  13006. (if (member property org-special-properties)
  13007. ;; We need a special property. Use `org-entry-properties' to
  13008. ;; retrieve it, but specify the wanted property
  13009. (cdr (assoc property (org-entry-properties nil 'special property)))
  13010. (let ((range (unless (org-before-first-heading-p)
  13011. (org-get-property-block)))
  13012. (props (list (or (assoc property org-file-properties)
  13013. (assoc property org-global-properties)
  13014. (assoc property org-global-properties-fixed))))
  13015. val)
  13016. (flet ((ap (key)
  13017. (when (re-search-forward
  13018. (org-re-property key) (cdr range) t)
  13019. (setq props
  13020. (org-update-property-plist
  13021. key
  13022. (if (match-end 1)
  13023. (org-match-string-no-properties 1) "")
  13024. props)))))
  13025. (when (and range (goto-char (car range)))
  13026. (ap property)
  13027. (goto-char (car range))
  13028. (while (ap (concat property "+")))
  13029. (setq val (cdr (assoc property props)))
  13030. (when val (if literal-nil val (org-not-nil val))))))))))
  13031. (defun org-property-or-variable-value (var &optional inherit)
  13032. "Check if there is a property fixing the value of VAR.
  13033. If yes, return this value. If not, return the current value of the variable."
  13034. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13035. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13036. (read prop)
  13037. (symbol-value var))))
  13038. (defun org-entry-delete (pom property)
  13039. "Delete the property PROPERTY from entry at point-or-marker POM."
  13040. (org-with-point-at pom
  13041. (if (member property org-special-properties)
  13042. nil ; cannot delete these properties.
  13043. (let ((range (org-get-property-block)))
  13044. (if (and range
  13045. (goto-char (car range))
  13046. (re-search-forward
  13047. (org-re-property property)
  13048. (cdr range) t))
  13049. (progn
  13050. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13051. t)
  13052. nil)))))
  13053. ;; Multi-values properties are properties that contain multiple values
  13054. ;; These values are assumed to be single words, separated by whitespace.
  13055. (defun org-entry-add-to-multivalued-property (pom property value)
  13056. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13057. (let* ((old (org-entry-get pom property))
  13058. (values (and old (org-split-string old "[ \t]"))))
  13059. (setq value (org-entry-protect-space value))
  13060. (unless (member value values)
  13061. (setq values (cons value values))
  13062. (org-entry-put pom property
  13063. (mapconcat 'identity values " ")))))
  13064. (defun org-entry-remove-from-multivalued-property (pom property value)
  13065. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13066. (let* ((old (org-entry-get pom property))
  13067. (values (and old (org-split-string old "[ \t]"))))
  13068. (setq value (org-entry-protect-space value))
  13069. (when (member value values)
  13070. (setq values (delete value values))
  13071. (org-entry-put pom property
  13072. (mapconcat 'identity values " ")))))
  13073. (defun org-entry-member-in-multivalued-property (pom property value)
  13074. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13075. (let* ((old (org-entry-get pom property))
  13076. (values (and old (org-split-string old "[ \t]"))))
  13077. (setq value (org-entry-protect-space value))
  13078. (member value values)))
  13079. (defun org-entry-get-multivalued-property (pom property)
  13080. "Return a list of values in a multivalued property."
  13081. (let* ((value (org-entry-get pom property))
  13082. (values (and value (org-split-string value "[ \t]"))))
  13083. (mapcar 'org-entry-restore-space values)))
  13084. (defun org-entry-put-multivalued-property (pom property &rest values)
  13085. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13086. VALUES should be a list of strings. Spaces will be protected."
  13087. (org-entry-put pom property
  13088. (mapconcat 'org-entry-protect-space values " "))
  13089. (let* ((value (org-entry-get pom property))
  13090. (values (and value (org-split-string value "[ \t]"))))
  13091. (mapcar 'org-entry-restore-space values)))
  13092. (defun org-entry-protect-space (s)
  13093. "Protect spaces and newline in string S."
  13094. (while (string-match " " s)
  13095. (setq s (replace-match "%20" t t s)))
  13096. (while (string-match "\n" s)
  13097. (setq s (replace-match "%0A" t t s)))
  13098. s)
  13099. (defun org-entry-restore-space (s)
  13100. "Restore spaces and newline in string S."
  13101. (while (string-match "%20" s)
  13102. (setq s (replace-match " " t t s)))
  13103. (while (string-match "%0A" s)
  13104. (setq s (replace-match "\n" t t s)))
  13105. s)
  13106. (defvar org-entry-property-inherited-from (make-marker)
  13107. "Marker pointing to the entry from where a property was inherited.
  13108. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13109. location of the entry where the inheritance search matched. If there was
  13110. no match, the marker will point nowhere.
  13111. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13112. is set.")
  13113. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13114. "Get entry property, and search higher levels if not present.
  13115. The search will stop at the first ancestor which has the property defined.
  13116. If the value found is \"nil\", return nil to show that the property
  13117. should be considered as undefined (this is the meaning of nil here).
  13118. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13119. (move-marker org-entry-property-inherited-from nil)
  13120. (let (tmp)
  13121. (unless (org-before-first-heading-p)
  13122. (save-excursion
  13123. (save-restriction
  13124. (widen)
  13125. (catch 'ex
  13126. (while t
  13127. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13128. (org-back-to-heading t)
  13129. (move-marker org-entry-property-inherited-from (point))
  13130. (throw 'ex tmp))
  13131. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13132. (setq tmp (or tmp
  13133. (cdr (assoc property org-file-properties))
  13134. (cdr (assoc property org-global-properties))
  13135. (cdr (assoc property org-global-properties-fixed))))
  13136. (if literal-nil tmp (org-not-nil tmp))))
  13137. (defvar org-property-changed-functions nil
  13138. "Hook called when the value of a property has changed.
  13139. Each hook function should accept two arguments, the name of the property
  13140. and the new value.")
  13141. (defun org-entry-put (pom property value)
  13142. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13143. (org-with-point-at pom
  13144. (org-back-to-heading t)
  13145. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13146. range)
  13147. (cond
  13148. ((equal property "TODO")
  13149. (when (and (stringp value) (string-match "\\S-" value)
  13150. (not (member value org-todo-keywords-1)))
  13151. (error "\"%s\" is not a valid TODO state" value))
  13152. (if (or (not value)
  13153. (not (string-match "\\S-" value)))
  13154. (setq value 'none))
  13155. (org-todo value)
  13156. (org-set-tags nil 'align))
  13157. ((equal property "PRIORITY")
  13158. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13159. (string-to-char value) ?\ ))
  13160. (org-set-tags nil 'align))
  13161. ((equal property "SCHEDULED")
  13162. (if (re-search-forward org-scheduled-time-regexp end t)
  13163. (cond
  13164. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13165. ((eq value 'later) (org-timestamp-change 1 'day))
  13166. (t (call-interactively 'org-schedule)))
  13167. (call-interactively 'org-schedule)))
  13168. ((equal property "DEADLINE")
  13169. (if (re-search-forward org-deadline-time-regexp end t)
  13170. (cond
  13171. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13172. ((eq value 'later) (org-timestamp-change 1 'day))
  13173. (t (call-interactively 'org-deadline)))
  13174. (call-interactively 'org-deadline)))
  13175. ((member property org-special-properties)
  13176. (error "The %s property can not yet be set with `org-entry-put'"
  13177. property))
  13178. (t ; a non-special property
  13179. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13180. (setq range (org-get-property-block beg end 'force))
  13181. (goto-char (car range))
  13182. (if (re-search-forward
  13183. (org-re-property property) (cdr range) t)
  13184. (progn
  13185. (delete-region (match-beginning 0) (match-end 0))
  13186. (goto-char (match-beginning 0)))
  13187. (goto-char (cdr range))
  13188. (insert "\n")
  13189. (backward-char 1)
  13190. (org-indent-line-function))
  13191. (insert ":" property ":")
  13192. (and value (insert " " value))
  13193. (org-indent-line-function)))))
  13194. (run-hook-with-args 'org-property-changed-functions property value)))
  13195. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13196. "Get all property keys in the current buffer.
  13197. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13198. like tags and TODO state.
  13199. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13200. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13201. and others.
  13202. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13203. formats in the current buffer."
  13204. (let (rtn range cfmt s p)
  13205. (save-excursion
  13206. (save-restriction
  13207. (widen)
  13208. (goto-char (point-min))
  13209. (while (re-search-forward org-property-start-re nil t)
  13210. (setq range (org-get-property-block))
  13211. (goto-char (car range))
  13212. (while (re-search-forward
  13213. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13214. (cdr range) t)
  13215. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13216. (outline-next-heading))))
  13217. (when include-specials
  13218. (setq rtn (append org-special-properties rtn)))
  13219. (when include-defaults
  13220. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13221. (add-to-list 'rtn org-effort-property))
  13222. (when include-columns
  13223. (save-excursion
  13224. (save-restriction
  13225. (widen)
  13226. (goto-char (point-min))
  13227. (while (re-search-forward
  13228. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13229. nil t)
  13230. (setq cfmt (match-string 2) s 0)
  13231. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13232. cfmt s)
  13233. (setq s (match-end 0)
  13234. p (match-string 1 cfmt))
  13235. (unless (or (equal p "ITEM")
  13236. (member p org-special-properties))
  13237. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13238. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13239. (defun org-property-values (key)
  13240. "Return a list of all values of property KEY in the current buffer."
  13241. (save-excursion
  13242. (save-restriction
  13243. (widen)
  13244. (goto-char (point-min))
  13245. (let ((re (org-re-property key))
  13246. values)
  13247. (while (re-search-forward re nil t)
  13248. (add-to-list 'values (org-trim (match-string 1))))
  13249. (delete "" values)))))
  13250. (defun org-insert-property-drawer ()
  13251. "Insert a property drawer into the current entry."
  13252. (org-back-to-heading t)
  13253. (looking-at org-outline-regexp)
  13254. (let ((indent (if org-adapt-indentation
  13255. (- (match-end 0) (match-beginning 0))
  13256. 0))
  13257. (beg (point))
  13258. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13259. end hiddenp)
  13260. (outline-next-heading)
  13261. (setq end (point))
  13262. (goto-char beg)
  13263. (while (re-search-forward re end t))
  13264. (setq hiddenp (outline-invisible-p))
  13265. (end-of-line 1)
  13266. (and (equal (char-after) ?\n) (forward-char 1))
  13267. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13268. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13269. ;; just skip this line
  13270. (beginning-of-line 2)
  13271. ;; Drawer start, find the end
  13272. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13273. (beginning-of-line 1)))
  13274. (org-skip-over-state-notes)
  13275. (skip-chars-backward " \t\n\r")
  13276. (if (eq (char-before) ?*) (forward-char 1))
  13277. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13278. (beginning-of-line 0)
  13279. (org-indent-to-column indent)
  13280. (beginning-of-line 2)
  13281. (org-indent-to-column indent)
  13282. (beginning-of-line 0)
  13283. (if hiddenp
  13284. (save-excursion
  13285. (org-back-to-heading t)
  13286. (hide-entry))
  13287. (org-flag-drawer t))))
  13288. (defun org-insert-drawer (&optional arg drawer)
  13289. "Insert a drawer at point.
  13290. Optional argument DRAWER, when non-nil, is a string representing
  13291. drawer's name. Otherwise, the user is prompted for a name.
  13292. If a region is active, insert the drawer around that region
  13293. instead.
  13294. Point is left between drawer's boundaries."
  13295. (interactive "P")
  13296. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13297. "LOGBOOK"))
  13298. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13299. ;; internally by Org. They are meant to be inserted
  13300. ;; automatically.
  13301. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13302. ;; Remove system drawers from list. Note: For some reason,
  13303. ;; `org-completing-read' ignores the predicate while
  13304. ;; `completing-read' handles it fine.
  13305. (drawer (if arg "PROPERTIES"
  13306. (or drawer
  13307. (completing-read
  13308. "Drawer: " org-drawers
  13309. (lambda (d) (not (member d system-drawers))))))))
  13310. (cond
  13311. ;; With C-u, fall back on `org-insert-property-drawer'
  13312. (arg (org-insert-property-drawer))
  13313. ;; With an active region, insert a drawer at point.
  13314. ((not (org-region-active-p))
  13315. (progn
  13316. (unless (bolp) (insert "\n"))
  13317. (insert (format ":%s:\n\n:END:\n" drawer))
  13318. (forward-line -2)))
  13319. ;; Otherwise, insert the drawer at point
  13320. (t
  13321. (let ((rbeg (region-beginning))
  13322. (rend (copy-marker (region-end))))
  13323. (unwind-protect
  13324. (progn
  13325. (goto-char rbeg)
  13326. (beginning-of-line)
  13327. (when (save-excursion
  13328. (re-search-forward org-outline-regexp-bol rend t))
  13329. (error "Drawers cannot contain headlines"))
  13330. ;; Position point at the beginning of the first
  13331. ;; non-blank line in region. Insert drawer's opening
  13332. ;; there, then indent it.
  13333. (org-skip-whitespace)
  13334. (beginning-of-line)
  13335. (insert ":" drawer ":\n")
  13336. (forward-line -1)
  13337. (indent-for-tab-command)
  13338. ;; Move point to the beginning of the first blank line
  13339. ;; after the last non-blank line in region. Insert
  13340. ;; drawer's closing, then indent it.
  13341. (goto-char rend)
  13342. (skip-chars-backward " \r\t\n")
  13343. (insert "\n:END:")
  13344. (indent-for-tab-command)
  13345. (unless (eolp) (insert "\n")))
  13346. ;; Clear marker, whatever the outcome of insertion is.
  13347. (set-marker rend nil)))))))
  13348. (defvar org-property-set-functions-alist nil
  13349. "Property set function alist.
  13350. Each entry should have the following format:
  13351. (PROPERTY . READ-FUNCTION)
  13352. The read function will be called with the same argument as
  13353. `org-completing-read'.")
  13354. (defun org-set-property-function (property)
  13355. "Get the function that should be used to set PROPERTY.
  13356. This is computed according to `org-property-set-functions-alist'."
  13357. (or (cdr (assoc property org-property-set-functions-alist))
  13358. 'org-completing-read))
  13359. (defun org-read-property-value (property)
  13360. "Read PROPERTY value from user."
  13361. (let* ((completion-ignore-case t)
  13362. (allowed (org-property-get-allowed-values nil property 'table))
  13363. (cur (org-entry-get nil property))
  13364. (prompt (concat property " value"
  13365. (if (and cur (string-match "\\S-" cur))
  13366. (concat " [" cur "]") "") ": "))
  13367. (set-function (org-set-property-function property))
  13368. (val (if allowed
  13369. (funcall set-function prompt allowed nil
  13370. (not (get-text-property 0 'org-unrestricted
  13371. (caar allowed))))
  13372. (let (org-completion-use-ido org-completion-use-iswitchb)
  13373. (funcall set-function prompt
  13374. (mapcar 'list (org-property-values property))
  13375. nil nil "" nil cur)))))
  13376. (if (equal val "")
  13377. cur
  13378. val)))
  13379. (defvar org-last-set-property nil)
  13380. (defun org-read-property-name ()
  13381. "Read a property name."
  13382. (let* ((completion-ignore-case t)
  13383. (keys (org-buffer-property-keys nil t t))
  13384. (default-prop (or (save-excursion
  13385. (save-match-data
  13386. (beginning-of-line)
  13387. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13388. (null (string= (match-string 1) "END"))
  13389. (match-string 1))))
  13390. org-last-set-property))
  13391. (property (org-icompleting-read
  13392. (concat "Property"
  13393. (if default-prop (concat " [" default-prop "]") "")
  13394. ": ")
  13395. (mapcar 'list keys)
  13396. nil nil nil nil
  13397. default-prop
  13398. )))
  13399. (if (member property keys)
  13400. property
  13401. (or (cdr (assoc (downcase property)
  13402. (mapcar (lambda (x) (cons (downcase x) x))
  13403. keys)))
  13404. property))))
  13405. (defun org-set-property (property value)
  13406. "In the current entry, set PROPERTY to VALUE.
  13407. When called interactively, this will prompt for a property name, offering
  13408. completion on existing and default properties. And then it will prompt
  13409. for a value, offering completion either on allowed values (via an inherited
  13410. xxx_ALL property) or on existing values in other instances of this property
  13411. in the current file."
  13412. (interactive (list nil nil))
  13413. (let* ((property (or property (org-read-property-name)))
  13414. (value (or value (org-read-property-value property)))
  13415. (fn (assoc property org-properties-postprocess-alist)))
  13416. (setq org-last-set-property property)
  13417. ;; Possibly postprocess the inserted value:
  13418. (when fn (setq value (funcall (cadr fn) value)))
  13419. (unless (equal (org-entry-get nil property) value)
  13420. (org-entry-put nil property value))))
  13421. (defun org-delete-property (property)
  13422. "In the current entry, delete PROPERTY."
  13423. (interactive
  13424. (let* ((completion-ignore-case t)
  13425. (prop (org-icompleting-read "Property: "
  13426. (org-entry-properties nil 'standard))))
  13427. (list prop)))
  13428. (message "Property %s %s" property
  13429. (if (org-entry-delete nil property)
  13430. "deleted"
  13431. "was not present in the entry")))
  13432. (defun org-delete-property-globally (property)
  13433. "Remove PROPERTY globally, from all entries."
  13434. (interactive
  13435. (let* ((completion-ignore-case t)
  13436. (prop (org-icompleting-read
  13437. "Globally remove property: "
  13438. (mapcar 'list (org-buffer-property-keys)))))
  13439. (list prop)))
  13440. (save-excursion
  13441. (save-restriction
  13442. (widen)
  13443. (goto-char (point-min))
  13444. (let ((cnt 0))
  13445. (while (re-search-forward
  13446. (org-re-property property)
  13447. nil t)
  13448. (setq cnt (1+ cnt))
  13449. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13450. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13451. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13452. (defun org-compute-property-at-point ()
  13453. "Compute the property at point.
  13454. This looks for an enclosing column format, extracts the operator and
  13455. then applies it to the property in the column format's scope."
  13456. (interactive)
  13457. (unless (org-at-property-p)
  13458. (error "Not at a property"))
  13459. (let ((prop (org-match-string-no-properties 2)))
  13460. (org-columns-get-format-and-top-level)
  13461. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13462. (error "No operator defined for property %s" prop))
  13463. (org-columns-compute prop)))
  13464. (defvar org-property-allowed-value-functions nil
  13465. "Hook for functions supplying allowed values for a specific property.
  13466. The functions must take a single argument, the name of the property, and
  13467. return a flat list of allowed values. If \":ETC\" is one of
  13468. the values, this means that these values are intended as defaults for
  13469. completion, but that other values should be allowed too.
  13470. The functions must return nil if they are not responsible for this
  13471. property.")
  13472. (defun org-property-get-allowed-values (pom property &optional table)
  13473. "Get allowed values for the property PROPERTY.
  13474. When TABLE is non-nil, return an alist that can directly be used for
  13475. completion."
  13476. (let (vals)
  13477. (cond
  13478. ((equal property "TODO")
  13479. (setq vals (org-with-point-at pom
  13480. (append org-todo-keywords-1 '("")))))
  13481. ((equal property "PRIORITY")
  13482. (let ((n org-lowest-priority))
  13483. (while (>= n org-highest-priority)
  13484. (push (char-to-string n) vals)
  13485. (setq n (1- n)))))
  13486. ((member property org-special-properties))
  13487. ((setq vals (run-hook-with-args-until-success
  13488. 'org-property-allowed-value-functions property)))
  13489. (t
  13490. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13491. (when (and vals (string-match "\\S-" vals))
  13492. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13493. (setq vals (mapcar (lambda (x)
  13494. (cond ((stringp x) x)
  13495. ((numberp x) (number-to-string x))
  13496. ((symbolp x) (symbol-name x))
  13497. (t "???")))
  13498. vals)))))
  13499. (when (member ":ETC" vals)
  13500. (setq vals (remove ":ETC" vals))
  13501. (org-add-props (car vals) '(org-unrestricted t)))
  13502. (if table (mapcar 'list vals) vals)))
  13503. (defun org-property-previous-allowed-value (&optional previous)
  13504. "Switch to the next allowed value for this property."
  13505. (interactive)
  13506. (org-property-next-allowed-value t))
  13507. (defun org-property-next-allowed-value (&optional previous)
  13508. "Switch to the next allowed value for this property."
  13509. (interactive)
  13510. (unless (org-at-property-p)
  13511. (error "Not at a property"))
  13512. (let* ((key (match-string 2))
  13513. (value (match-string 3))
  13514. (allowed (or (org-property-get-allowed-values (point) key)
  13515. (and (member value '("[ ]" "[-]" "[X]"))
  13516. '("[ ]" "[X]"))))
  13517. nval)
  13518. (unless allowed
  13519. (error "Allowed values for this property have not been defined"))
  13520. (if previous (setq allowed (reverse allowed)))
  13521. (if (member value allowed)
  13522. (setq nval (car (cdr (member value allowed)))))
  13523. (setq nval (or nval (car allowed)))
  13524. (if (equal nval value)
  13525. (error "Only one allowed value for this property"))
  13526. (org-at-property-p)
  13527. (replace-match (concat " :" key ": " nval) t t)
  13528. (org-indent-line-function)
  13529. (beginning-of-line 1)
  13530. (skip-chars-forward " \t")
  13531. (run-hook-with-args 'org-property-changed-functions key nval)))
  13532. (defun org-find-olp (path &optional this-buffer)
  13533. "Return a marker pointing to the entry at outline path OLP.
  13534. If anything goes wrong, throw an error.
  13535. You can wrap this call to catch the error like this:
  13536. (condition-case msg
  13537. (org-mobile-locate-entry (match-string 4))
  13538. (error (nth 1 msg)))
  13539. The return value will then be either a string with the error message,
  13540. or a marker if everything is OK.
  13541. If THIS-BUFFER is set, the outline path does not contain a file,
  13542. only headings."
  13543. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13544. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13545. (level 1)
  13546. (lmin 1)
  13547. (lmax 1)
  13548. limit re end found pos heading cnt flevel)
  13549. (unless buffer (error "File not found :%s" file))
  13550. (with-current-buffer buffer
  13551. (save-excursion
  13552. (save-restriction
  13553. (widen)
  13554. (setq limit (point-max))
  13555. (goto-char (point-min))
  13556. (while (setq heading (pop path))
  13557. (setq re (format org-complex-heading-regexp-format
  13558. (regexp-quote heading)))
  13559. (setq cnt 0 pos (point))
  13560. (while (re-search-forward re end t)
  13561. (setq level (- (match-end 1) (match-beginning 1)))
  13562. (if (and (>= level lmin) (<= level lmax))
  13563. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13564. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13565. lmax heading))
  13566. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13567. lmax heading))
  13568. (goto-char found)
  13569. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13570. (setq end (save-excursion (org-end-of-subtree t t))))
  13571. (when (org-at-heading-p)
  13572. (move-marker (make-marker) (point))))))))
  13573. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13574. "Find node HEADING in BUFFER.
  13575. Return a marker to the heading if it was found, or nil if not.
  13576. If POS-ONLY is set, return just the position instead of a marker.
  13577. The heading text must match exact, but it may have a TODO keyword,
  13578. a priority cookie and tags in the standard locations."
  13579. (with-current-buffer (or buffer (current-buffer))
  13580. (save-excursion
  13581. (save-restriction
  13582. (widen)
  13583. (goto-char (point-min))
  13584. (let (case-fold-search)
  13585. (if (re-search-forward
  13586. (format org-complex-heading-regexp-format
  13587. (regexp-quote heading)) nil t)
  13588. (if pos-only
  13589. (match-beginning 0)
  13590. (move-marker (make-marker) (match-beginning 0)))))))))
  13591. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13592. "Find Org node headline HEADING in all .org files in directory DIR.
  13593. When the target headline is found, return a marker to this location."
  13594. (let ((files (directory-files (or dir default-directory)
  13595. nil "\\`[^.#].*\\.org\\'"))
  13596. file visiting m buffer)
  13597. (catch 'found
  13598. (while (setq file (pop files))
  13599. (message "trying %s" file)
  13600. (setq visiting (org-find-base-buffer-visiting file))
  13601. (setq buffer (or visiting (find-file-noselect file)))
  13602. (setq m (org-find-exact-headline-in-buffer
  13603. heading buffer))
  13604. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13605. (and m (throw 'found m))))))
  13606. (defun org-find-entry-with-id (ident)
  13607. "Locate the entry that contains the ID property with exact value IDENT.
  13608. IDENT can be a string, a symbol or a number, this function will search for
  13609. the string representation of it.
  13610. Return the position where this entry starts, or nil if there is no such entry."
  13611. (interactive "sID: ")
  13612. (let ((id (cond
  13613. ((stringp ident) ident)
  13614. ((symbol-name ident) (symbol-name ident))
  13615. ((numberp ident) (number-to-string ident))
  13616. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13617. (case-fold-search nil))
  13618. (save-excursion
  13619. (save-restriction
  13620. (widen)
  13621. (goto-char (point-min))
  13622. (when (re-search-forward
  13623. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13624. nil t)
  13625. (org-back-to-heading t)
  13626. (point))))))
  13627. ;;;; Timestamps
  13628. (defvar org-last-changed-timestamp nil)
  13629. (defvar org-last-inserted-timestamp nil
  13630. "The last time stamp inserted with `org-insert-time-stamp'.")
  13631. (defvar org-time-was-given) ; dynamically scoped parameter
  13632. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13633. (defvar org-ts-what) ; dynamically scoped parameter
  13634. (defun org-time-stamp (arg &optional inactive)
  13635. "Prompt for a date/time and insert a time stamp.
  13636. If the user specifies a time like HH:MM, or if this command is called
  13637. with a prefix argument, the time stamp will contain date and time.
  13638. Otherwise, only the date will be included. All parts of a date not
  13639. specified by the user will be filled in from the current date/time.
  13640. So if you press just return without typing anything, the time stamp
  13641. will represent the current date/time. If there is already a timestamp
  13642. at the cursor, it will be modified."
  13643. (interactive "P")
  13644. (let* ((ts nil)
  13645. (default-time
  13646. ;; Default time is either today, or, when entering a range,
  13647. ;; the range start.
  13648. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13649. (save-excursion
  13650. (re-search-backward
  13651. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13652. (- (point) 20) t)))
  13653. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13654. (current-time)))
  13655. (default-input (and ts (org-get-compact-tod ts)))
  13656. (repeater (save-excursion
  13657. (save-match-data
  13658. (beginning-of-line)
  13659. (when (re-search-forward
  13660. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13661. (save-excursion (progn (end-of-line) (point))) t)
  13662. (match-string 0)))))
  13663. org-time-was-given org-end-time-was-given time)
  13664. (cond
  13665. ((and (org-at-timestamp-p t)
  13666. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13667. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13668. (insert "--")
  13669. (setq time (let ((this-command this-command))
  13670. (org-read-date arg 'totime nil nil
  13671. default-time default-input inactive)))
  13672. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13673. ((org-at-timestamp-p t)
  13674. (setq time (let ((this-command this-command))
  13675. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13676. (when (org-at-timestamp-p t) ; just to get the match data
  13677. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13678. (replace-match "")
  13679. (setq org-last-changed-timestamp
  13680. (org-insert-time-stamp
  13681. time (or org-time-was-given arg)
  13682. inactive nil nil (list org-end-time-was-given)))
  13683. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13684. (setq org-last-changed-timestamp
  13685. (concat (substring org-last-inserted-timestamp 0 -1)
  13686. " " repeater ">"))))
  13687. (message "Timestamp updated"))
  13688. (t
  13689. (setq time (let ((this-command this-command))
  13690. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13691. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13692. nil nil (list org-end-time-was-given))))))
  13693. ;; FIXME: can we use this for something else, like computing time differences?
  13694. (defun org-get-compact-tod (s)
  13695. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13696. (let* ((t1 (match-string 1 s))
  13697. (h1 (string-to-number (match-string 2 s)))
  13698. (m1 (string-to-number (match-string 3 s)))
  13699. (t2 (and (match-end 4) (match-string 5 s)))
  13700. (h2 (and t2 (string-to-number (match-string 6 s))))
  13701. (m2 (and t2 (string-to-number (match-string 7 s))))
  13702. dh dm)
  13703. (if (not t2)
  13704. t1
  13705. (setq dh (- h2 h1) dm (- m2 m1))
  13706. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13707. (concat t1 "+" (number-to-string dh)
  13708. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13709. (defun org-time-stamp-inactive (&optional arg)
  13710. "Insert an inactive time stamp.
  13711. An inactive time stamp is enclosed in square brackets instead of angle
  13712. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13713. does not link to the calendar and cannot be changed with the S-cursor keys.
  13714. So these are more for recording a certain time/date."
  13715. (interactive "P")
  13716. (org-time-stamp arg 'inactive))
  13717. (defvar org-date-ovl (make-overlay 1 1))
  13718. (overlay-put org-date-ovl 'face 'org-date-selected)
  13719. (org-detach-overlay org-date-ovl)
  13720. (defvar org-ans1) ; dynamically scoped parameter
  13721. (defvar org-ans2) ; dynamically scoped parameter
  13722. (defvar org-plain-time-of-day-regexp) ; defined below
  13723. (defvar org-overriding-default-time nil) ; dynamically scoped
  13724. (defvar org-read-date-overlay nil)
  13725. (defvar org-dcst nil) ; dynamically scoped
  13726. (defvar org-read-date-history nil)
  13727. (defvar org-read-date-final-answer nil)
  13728. (defvar org-read-date-analyze-futurep nil)
  13729. (defvar org-read-date-analyze-forced-year nil)
  13730. (defvar org-read-date-inactive)
  13731. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13732. default-time default-input inactive)
  13733. "Read a date, possibly a time, and make things smooth for the user.
  13734. The prompt will suggest to enter an ISO date, but you can also enter anything
  13735. which will at least partially be understood by `parse-time-string'.
  13736. Unrecognized parts of the date will default to the current day, month, year,
  13737. hour and minute. If this command is called to replace a timestamp at point,
  13738. or to enter the second timestamp of a range, the default time is taken
  13739. from the existing stamp. Furthermore, the command prefers the future,
  13740. so if you are giving a date where the year is not given, and the day-month
  13741. combination is already past in the current year, it will assume you
  13742. mean next year. For details, see the manual. A few examples:
  13743. 3-2-5 --> 2003-02-05
  13744. feb 15 --> currentyear-02-15
  13745. 2/15 --> currentyear-02-15
  13746. sep 12 9 --> 2009-09-12
  13747. 12:45 --> today 12:45
  13748. 22 sept 0:34 --> currentyear-09-22 0:34
  13749. 12 --> currentyear-currentmonth-12
  13750. Fri --> nearest Friday (today or later)
  13751. etc.
  13752. Furthermore you can specify a relative date by giving, as the *first* thing
  13753. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  13754. change in days weeks, months, years.
  13755. With a single plus or minus, the date is relative to today. With a double
  13756. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13757. +4d --> four days from today
  13758. +4 --> same as above
  13759. +2w --> two weeks from today
  13760. ++5 --> five days from default date
  13761. The function understands only English month and weekday abbreviations.
  13762. While prompting, a calendar is popped up - you can also select the
  13763. date with the mouse (button 1). The calendar shows a period of three
  13764. months. To scroll it to other months, use the keys `>' and `<'.
  13765. If you don't like the calendar, turn it off with
  13766. \(setq org-read-date-popup-calendar nil)
  13767. With optional argument TO-TIME, the date will immediately be converted
  13768. to an internal time.
  13769. With an optional argument WITH-TIME, the prompt will suggest to also
  13770. insert a time. Note that when WITH-TIME is not set, you can still
  13771. enter a time, and this function will inform the calling routine about
  13772. this change. The calling routine may then choose to change the format
  13773. used to insert the time stamp into the buffer to include the time.
  13774. With optional argument FROM-STRING, read from this string instead from
  13775. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13776. the time/date that is used for everything that is not specified by the
  13777. user."
  13778. (require 'parse-time)
  13779. (let* ((org-time-stamp-rounding-minutes
  13780. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13781. (org-dcst org-display-custom-times)
  13782. (ct (org-current-time))
  13783. (org-def (or org-overriding-default-time default-time ct))
  13784. (org-defdecode (decode-time org-def))
  13785. (dummy (progn
  13786. (when (< (nth 2 org-defdecode) org-extend-today-until)
  13787. (setcar (nthcdr 2 org-defdecode) -1)
  13788. (setcar (nthcdr 1 org-defdecode) 59)
  13789. (setq org-def (apply 'encode-time org-defdecode)
  13790. org-defdecode (decode-time org-def)))))
  13791. (calendar-frame-setup nil)
  13792. (calendar-setup nil)
  13793. (calendar-move-hook nil)
  13794. (calendar-view-diary-initially-flag nil)
  13795. (calendar-view-holidays-initially-flag nil)
  13796. (timestr (format-time-string
  13797. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  13798. (prompt (concat (if prompt (concat prompt " ") "")
  13799. (format "Date+time [%s]: " timestr)))
  13800. ans (org-ans0 "") org-ans1 org-ans2 final)
  13801. (cond
  13802. (from-string (setq ans from-string))
  13803. (org-read-date-popup-calendar
  13804. (save-excursion
  13805. (save-window-excursion
  13806. (calendar)
  13807. (unwind-protect
  13808. (progn
  13809. (calendar-forward-day (- (time-to-days org-def)
  13810. (calendar-absolute-from-gregorian
  13811. (calendar-current-date))))
  13812. (org-eval-in-calendar nil t)
  13813. (let* ((old-map (current-local-map))
  13814. (map (copy-keymap calendar-mode-map))
  13815. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13816. (org-defkey map (kbd "RET") 'org-calendar-select)
  13817. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13818. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13819. (org-defkey minibuffer-local-map [(meta shift left)]
  13820. (lambda () (interactive)
  13821. (org-eval-in-calendar '(calendar-backward-month 1))))
  13822. (org-defkey minibuffer-local-map [(meta shift right)]
  13823. (lambda () (interactive)
  13824. (org-eval-in-calendar '(calendar-forward-month 1))))
  13825. (org-defkey minibuffer-local-map [(meta shift up)]
  13826. (lambda () (interactive)
  13827. (org-eval-in-calendar '(calendar-backward-year 1))))
  13828. (org-defkey minibuffer-local-map [(meta shift down)]
  13829. (lambda () (interactive)
  13830. (org-eval-in-calendar '(calendar-forward-year 1))))
  13831. (org-defkey minibuffer-local-map [?\e (shift left)]
  13832. (lambda () (interactive)
  13833. (org-eval-in-calendar '(calendar-backward-month 1))))
  13834. (org-defkey minibuffer-local-map [?\e (shift right)]
  13835. (lambda () (interactive)
  13836. (org-eval-in-calendar '(calendar-forward-month 1))))
  13837. (org-defkey minibuffer-local-map [?\e (shift up)]
  13838. (lambda () (interactive)
  13839. (org-eval-in-calendar '(calendar-backward-year 1))))
  13840. (org-defkey minibuffer-local-map [?\e (shift down)]
  13841. (lambda () (interactive)
  13842. (org-eval-in-calendar '(calendar-forward-year 1))))
  13843. (org-defkey minibuffer-local-map [(shift up)]
  13844. (lambda () (interactive)
  13845. (org-eval-in-calendar '(calendar-backward-week 1))))
  13846. (org-defkey minibuffer-local-map [(shift down)]
  13847. (lambda () (interactive)
  13848. (org-eval-in-calendar '(calendar-forward-week 1))))
  13849. (org-defkey minibuffer-local-map [(shift left)]
  13850. (lambda () (interactive)
  13851. (org-eval-in-calendar '(calendar-backward-day 1))))
  13852. (org-defkey minibuffer-local-map [(shift right)]
  13853. (lambda () (interactive)
  13854. (org-eval-in-calendar '(calendar-forward-day 1))))
  13855. (org-defkey minibuffer-local-map ">"
  13856. (lambda () (interactive)
  13857. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13858. (org-defkey minibuffer-local-map "<"
  13859. (lambda () (interactive)
  13860. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13861. (org-defkey minibuffer-local-map "\C-v"
  13862. (lambda () (interactive)
  13863. (org-eval-in-calendar
  13864. '(calendar-scroll-left-three-months 1))))
  13865. (org-defkey minibuffer-local-map "\M-v"
  13866. (lambda () (interactive)
  13867. (org-eval-in-calendar
  13868. '(calendar-scroll-right-three-months 1))))
  13869. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13870. (unwind-protect
  13871. (progn
  13872. (use-local-map map)
  13873. (setq org-read-date-inactive inactive)
  13874. (add-hook 'post-command-hook 'org-read-date-display)
  13875. (setq org-ans0 (read-string prompt default-input
  13876. 'org-read-date-history nil))
  13877. ;; org-ans0: from prompt
  13878. ;; org-ans1: from mouse click
  13879. ;; org-ans2: from calendar motion
  13880. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13881. (remove-hook 'post-command-hook 'org-read-date-display)
  13882. (use-local-map old-map)
  13883. (when org-read-date-overlay
  13884. (delete-overlay org-read-date-overlay)
  13885. (setq org-read-date-overlay nil)))))
  13886. (bury-buffer "*Calendar*")))))
  13887. (t ; Naked prompt only
  13888. (unwind-protect
  13889. (setq ans (read-string prompt default-input
  13890. 'org-read-date-history timestr))
  13891. (when org-read-date-overlay
  13892. (delete-overlay org-read-date-overlay)
  13893. (setq org-read-date-overlay nil)))))
  13894. (setq final (org-read-date-analyze ans org-def org-defdecode))
  13895. (when org-read-date-analyze-forced-year
  13896. (message "Year was forced into %s"
  13897. (if org-read-date-force-compatible-dates
  13898. "compatible range (1970-2037)"
  13899. "range representable on this machine"))
  13900. (ding))
  13901. ;; One round trip to get rid of 34th of August and stuff like that....
  13902. (setq final (decode-time (apply 'encode-time final)))
  13903. (setq org-read-date-final-answer ans)
  13904. (if to-time
  13905. (apply 'encode-time final)
  13906. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13907. (format "%04d-%02d-%02d %02d:%02d"
  13908. (nth 5 final) (nth 4 final) (nth 3 final)
  13909. (nth 2 final) (nth 1 final))
  13910. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13911. (defvar org-def)
  13912. (defvar org-defdecode)
  13913. (defvar org-with-time)
  13914. (defun org-read-date-display ()
  13915. "Display the current date prompt interpretation in the minibuffer."
  13916. (when org-read-date-display-live
  13917. (when org-read-date-overlay
  13918. (delete-overlay org-read-date-overlay))
  13919. (when (minibufferp (current-buffer))
  13920. (save-excursion
  13921. (end-of-line 1)
  13922. (while (not (equal (buffer-substring
  13923. (max (point-min) (- (point) 4)) (point))
  13924. " "))
  13925. (insert " ")))
  13926. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13927. " " (or org-ans1 org-ans2)))
  13928. (org-end-time-was-given nil)
  13929. (f (org-read-date-analyze ans org-def org-defdecode))
  13930. (fmts (if org-dcst
  13931. org-time-stamp-custom-formats
  13932. org-time-stamp-formats))
  13933. (fmt (if (or org-with-time
  13934. (and (boundp 'org-time-was-given) org-time-was-given))
  13935. (cdr fmts)
  13936. (car fmts)))
  13937. (txt (format-time-string fmt (apply 'encode-time f)))
  13938. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13939. (txt (concat "=> " txt)))
  13940. (when (and org-end-time-was-given
  13941. (string-match org-plain-time-of-day-regexp txt))
  13942. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13943. org-end-time-was-given
  13944. (substring txt (match-end 0)))))
  13945. (when org-read-date-analyze-futurep
  13946. (setq txt (concat txt " (=>F)")))
  13947. (setq org-read-date-overlay
  13948. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13949. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  13950. (defun org-read-date-analyze (ans org-def org-defdecode)
  13951. "Analyze the combined answer of the date prompt."
  13952. ;; FIXME: cleanup and comment
  13953. (let ((nowdecode (decode-time (current-time)))
  13954. delta deltan deltaw deltadef year month day
  13955. hour minute second wday pm h2 m2 tl wday1
  13956. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13957. (setq org-read-date-analyze-futurep nil
  13958. org-read-date-analyze-forced-year nil)
  13959. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13960. (setq ans "+0"))
  13961. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  13962. (setq ans (replace-match "" t t ans)
  13963. deltan (car delta)
  13964. deltaw (nth 1 delta)
  13965. deltadef (nth 2 delta)))
  13966. ;; Check if there is an iso week date in there
  13967. ;; If yes, store the info and postpone interpreting it until the rest
  13968. ;; of the parsing is done
  13969. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13970. (setq iso-year (if (match-end 1)
  13971. (org-small-year-to-year
  13972. (string-to-number (match-string 1 ans))))
  13973. iso-weekday (if (match-end 3)
  13974. (string-to-number (match-string 3 ans)))
  13975. iso-week (string-to-number (match-string 2 ans)))
  13976. (setq ans (replace-match "" t t ans)))
  13977. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13978. (when (string-match
  13979. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13980. (setq year (if (match-end 2)
  13981. (string-to-number (match-string 2 ans))
  13982. (progn (setq kill-year t)
  13983. (string-to-number (format-time-string "%Y"))))
  13984. month (string-to-number (match-string 3 ans))
  13985. day (string-to-number (match-string 4 ans)))
  13986. (if (< year 100) (setq year (+ 2000 year)))
  13987. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13988. t nil ans)))
  13989. ;; Help matching dotted european dates
  13990. (when (string-match
  13991. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13992. (setq year (if (match-end 3)
  13993. (string-to-number (match-string 3 ans))
  13994. (progn (setq kill-year t)
  13995. (string-to-number (format-time-string "%Y"))))
  13996. day (string-to-number (match-string 1 ans))
  13997. month (string-to-number (match-string 2 ans))
  13998. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13999. t nil ans)))
  14000. ;; Help matching american dates, like 5/30 or 5/30/7
  14001. (when (string-match
  14002. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14003. (setq year (if (match-end 4)
  14004. (string-to-number (match-string 4 ans))
  14005. (progn (setq kill-year t)
  14006. (string-to-number (format-time-string "%Y"))))
  14007. month (string-to-number (match-string 1 ans))
  14008. day (string-to-number (match-string 2 ans)))
  14009. (if (< year 100) (setq year (+ 2000 year)))
  14010. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14011. t nil ans)))
  14012. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14013. ;; If there is a time with am/pm, and *no* time without it, we convert
  14014. ;; so that matching will be successful.
  14015. (loop for i from 1 to 2 do ; twice, for end time as well
  14016. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14017. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14018. (setq hour (string-to-number (match-string 1 ans))
  14019. minute (if (match-end 3)
  14020. (string-to-number (match-string 3 ans))
  14021. 0)
  14022. pm (equal ?p
  14023. (string-to-char (downcase (match-string 4 ans)))))
  14024. (if (and (= hour 12) (not pm))
  14025. (setq hour 0)
  14026. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14027. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14028. t t ans))))
  14029. ;; Check if a time range is given as a duration
  14030. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14031. (setq hour (string-to-number (match-string 1 ans))
  14032. h2 (+ hour (string-to-number (match-string 3 ans)))
  14033. minute (string-to-number (match-string 2 ans))
  14034. m2 (+ minute (if (match-end 5) (string-to-number
  14035. (match-string 5 ans))0)))
  14036. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14037. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14038. t t ans)))
  14039. ;; Check if there is a time range
  14040. (when (boundp 'org-end-time-was-given)
  14041. (setq org-time-was-given nil)
  14042. (when (and (string-match org-plain-time-of-day-regexp ans)
  14043. (match-end 8))
  14044. (setq org-end-time-was-given (match-string 8 ans))
  14045. (setq ans (concat (substring ans 0 (match-beginning 7))
  14046. (substring ans (match-end 7))))))
  14047. (setq tl (parse-time-string ans)
  14048. day (or (nth 3 tl) (nth 3 org-defdecode))
  14049. month (or (nth 4 tl)
  14050. (if (and org-read-date-prefer-future
  14051. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14052. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14053. (nth 4 org-defdecode)))
  14054. year (or (and (not kill-year) (nth 5 tl))
  14055. (if (and org-read-date-prefer-future
  14056. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14057. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14058. (nth 5 org-defdecode)))
  14059. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14060. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14061. second (or (nth 0 tl) 0)
  14062. wday (nth 6 tl))
  14063. (when (and (eq org-read-date-prefer-future 'time)
  14064. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14065. (equal day (nth 3 nowdecode))
  14066. (equal month (nth 4 nowdecode))
  14067. (equal year (nth 5 nowdecode))
  14068. (nth 2 tl)
  14069. (or (< (nth 2 tl) (nth 2 nowdecode))
  14070. (and (= (nth 2 tl) (nth 2 nowdecode))
  14071. (nth 1 tl)
  14072. (< (nth 1 tl) (nth 1 nowdecode)))))
  14073. (setq day (1+ day)
  14074. futurep t))
  14075. ;; Special date definitions below
  14076. (cond
  14077. (iso-week
  14078. ;; There was an iso week
  14079. (require 'cal-iso)
  14080. (setq futurep nil)
  14081. (setq year (or iso-year year)
  14082. day (or iso-weekday wday 1)
  14083. wday nil ; to make sure that the trigger below does not match
  14084. iso-date (calendar-gregorian-from-absolute
  14085. (calendar-absolute-from-iso
  14086. (list iso-week day year))))
  14087. ; FIXME: Should we also push ISO weeks into the future?
  14088. ; (when (and org-read-date-prefer-future
  14089. ; (not iso-year)
  14090. ; (< (calendar-absolute-from-gregorian iso-date)
  14091. ; (time-to-days (current-time))))
  14092. ; (setq year (1+ year)
  14093. ; iso-date (calendar-gregorian-from-absolute
  14094. ; (calendar-absolute-from-iso
  14095. ; (list iso-week day year)))))
  14096. (setq month (car iso-date)
  14097. year (nth 2 iso-date)
  14098. day (nth 1 iso-date)))
  14099. (deltan
  14100. (setq futurep nil)
  14101. (unless deltadef
  14102. (let ((now (decode-time (current-time))))
  14103. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14104. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14105. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14106. ((equal deltaw "m") (setq month (+ month deltan)))
  14107. ((equal deltaw "y") (setq year (+ year deltan)))))
  14108. ((and wday (not (nth 3 tl)))
  14109. (setq futurep nil)
  14110. ;; Weekday was given, but no day, so pick that day in the week
  14111. ;; on or after the derived date.
  14112. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14113. (unless (equal wday wday1)
  14114. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14115. (if (and (boundp 'org-time-was-given)
  14116. (nth 2 tl))
  14117. (setq org-time-was-given t))
  14118. (if (< year 100) (setq year (+ 2000 year)))
  14119. ;; Check of the date is representable
  14120. (if org-read-date-force-compatible-dates
  14121. (progn
  14122. (if (< year 1970)
  14123. (setq year 1970 org-read-date-analyze-forced-year t))
  14124. (if (> year 2037)
  14125. (setq year 2037 org-read-date-analyze-forced-year t)))
  14126. (condition-case nil
  14127. (ignore (encode-time second minute hour day month year))
  14128. (error
  14129. (setq year (nth 5 org-defdecode))
  14130. (setq org-read-date-analyze-forced-year t))))
  14131. (setq org-read-date-analyze-futurep futurep)
  14132. (list second minute hour day month year)))
  14133. (defvar parse-time-weekdays)
  14134. (defun org-read-date-get-relative (s today default)
  14135. "Check string S for special relative date string.
  14136. TODAY and DEFAULT are internal times, for today and for a default.
  14137. Return shift list (N what def-flag)
  14138. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14139. N is the number of WHATs to shift.
  14140. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14141. the DEFAULT date rather than TODAY."
  14142. (require 'parse-time)
  14143. (when (and
  14144. (string-match
  14145. (concat
  14146. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14147. "\\([0-9]+\\)?"
  14148. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14149. "\\([ \t]\\|$\\)") s)
  14150. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14151. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14152. (string-to-char (substring (match-string 1 s) -1))
  14153. ?+))
  14154. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14155. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14156. (what (if (match-end 3) (match-string 3 s) "d"))
  14157. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14158. (date (if rel default today))
  14159. (wday (nth 6 (decode-time date)))
  14160. delta)
  14161. (if wday1
  14162. (progn
  14163. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14164. (if (= dir ?-) (setq delta (- delta 7)))
  14165. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14166. (list delta "d" rel))
  14167. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14168. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14169. "Turn a user-specified date into the internal representation.
  14170. The internal representation needed by the calendar is (month day year).
  14171. This is a wrapper to handle the brain-dead convention in calendar that
  14172. user function argument order change dependent on argument order."
  14173. (if (boundp 'calendar-date-style)
  14174. (cond
  14175. ((eq calendar-date-style 'american)
  14176. (list arg1 arg2 arg3))
  14177. ((eq calendar-date-style 'european)
  14178. (list arg2 arg1 arg3))
  14179. ((eq calendar-date-style 'iso)
  14180. (list arg2 arg3 arg1)))
  14181. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14182. (if (org-bound-and-true-p european-calendar-style)
  14183. (list arg2 arg1 arg3)
  14184. (list arg1 arg2 arg3)))))
  14185. (defun org-eval-in-calendar (form &optional keepdate)
  14186. "Eval FORM in the calendar window and return to current window.
  14187. Also, store the cursor date in variable org-ans2."
  14188. (let ((sf (selected-frame))
  14189. (sw (selected-window)))
  14190. (select-window (get-buffer-window "*Calendar*" t))
  14191. (eval form)
  14192. (when (and (not keepdate) (calendar-cursor-to-date))
  14193. (let* ((date (calendar-cursor-to-date))
  14194. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14195. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14196. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14197. (select-window sw)
  14198. (org-select-frame-set-input-focus sf)))
  14199. (defun org-calendar-select ()
  14200. "Return to `org-read-date' with the date currently selected.
  14201. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14202. (interactive)
  14203. (when (calendar-cursor-to-date)
  14204. (let* ((date (calendar-cursor-to-date))
  14205. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14206. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14207. (if (active-minibuffer-window) (exit-minibuffer))))
  14208. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14209. "Insert a date stamp for the date given by the internal TIME.
  14210. WITH-HM means use the stamp format that includes the time of the day.
  14211. INACTIVE means use square brackets instead of angular ones, so that the
  14212. stamp will not contribute to the agenda.
  14213. PRE and POST are optional strings to be inserted before and after the
  14214. stamp.
  14215. The command returns the inserted time stamp."
  14216. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14217. stamp)
  14218. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14219. (insert-before-markers (or pre ""))
  14220. (when (listp extra)
  14221. (setq extra (car extra))
  14222. (if (and (stringp extra)
  14223. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14224. (setq extra (format "-%02d:%02d"
  14225. (string-to-number (match-string 1 extra))
  14226. (string-to-number (match-string 2 extra))))
  14227. (setq extra nil)))
  14228. (when extra
  14229. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14230. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14231. (insert-before-markers (or post ""))
  14232. (setq org-last-inserted-timestamp stamp)))
  14233. (defun org-toggle-time-stamp-overlays ()
  14234. "Toggle the use of custom time stamp formats."
  14235. (interactive)
  14236. (setq org-display-custom-times (not org-display-custom-times))
  14237. (unless org-display-custom-times
  14238. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14239. (while (setq p (next-single-property-change p 'display))
  14240. (if (and (get-text-property p 'display)
  14241. (eq (get-text-property p 'face) 'org-date))
  14242. (remove-text-properties
  14243. p (setq p (next-single-property-change p 'display))
  14244. '(display t))))
  14245. (set-buffer-modified-p bmp)))
  14246. (if (featurep 'xemacs)
  14247. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14248. (org-restart-font-lock)
  14249. (setq org-table-may-need-update t)
  14250. (if org-display-custom-times
  14251. (message "Time stamps are overlaid with custom format")
  14252. (message "Time stamp overlays removed")))
  14253. (defun org-display-custom-time (beg end)
  14254. "Overlay modified time stamp format over timestamp between BEG and END."
  14255. (let* ((ts (buffer-substring beg end))
  14256. t1 w1 with-hm tf time str w2 (off 0))
  14257. (save-match-data
  14258. (setq t1 (org-parse-time-string ts t))
  14259. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14260. (setq off (- (match-end 0) (match-beginning 0)))))
  14261. (setq end (- end off))
  14262. (setq w1 (- end beg)
  14263. with-hm (and (nth 1 t1) (nth 2 t1))
  14264. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14265. time (org-fix-decoded-time t1)
  14266. str (org-add-props
  14267. (format-time-string
  14268. (substring tf 1 -1) (apply 'encode-time time))
  14269. nil 'mouse-face 'highlight)
  14270. w2 (length str))
  14271. (if (not (= w2 w1))
  14272. (add-text-properties (1+ beg) (+ 2 beg)
  14273. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14274. (if (featurep 'xemacs)
  14275. (progn
  14276. (put-text-property beg end 'invisible t)
  14277. (put-text-property beg end 'end-glyph (make-glyph str)))
  14278. (put-text-property beg end 'display str))))
  14279. (defun org-translate-time (string)
  14280. "Translate all timestamps in STRING to custom format.
  14281. But do this only if the variable `org-display-custom-times' is set."
  14282. (when org-display-custom-times
  14283. (save-match-data
  14284. (let* ((start 0)
  14285. (re org-ts-regexp-both)
  14286. t1 with-hm inactive tf time str beg end)
  14287. (while (setq start (string-match re string start))
  14288. (setq beg (match-beginning 0)
  14289. end (match-end 0)
  14290. t1 (save-match-data
  14291. (org-parse-time-string (substring string beg end) t))
  14292. with-hm (and (nth 1 t1) (nth 2 t1))
  14293. inactive (equal (substring string beg (1+ beg)) "[")
  14294. tf (funcall (if with-hm 'cdr 'car)
  14295. org-time-stamp-custom-formats)
  14296. time (org-fix-decoded-time t1)
  14297. str (format-time-string
  14298. (concat
  14299. (if inactive "[" "<") (substring tf 1 -1)
  14300. (if inactive "]" ">"))
  14301. (apply 'encode-time time))
  14302. string (replace-match str t t string)
  14303. start (+ start (length str)))))))
  14304. string)
  14305. (defun org-fix-decoded-time (time)
  14306. "Set 0 instead of nil for the first 6 elements of time.
  14307. Don't touch the rest."
  14308. (let ((n 0))
  14309. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14310. (defun org-days-to-time (timestamp-string)
  14311. "Difference between TIMESTAMP-STRING and now in days."
  14312. (- (time-to-days (org-time-string-to-time timestamp-string))
  14313. (time-to-days (current-time))))
  14314. (defun org-deadline-close (timestamp-string &optional ndays)
  14315. "Is the time in TIMESTAMP-STRING close to the current date?"
  14316. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14317. (and (< (org-days-to-time timestamp-string) ndays)
  14318. (not (org-entry-is-done-p))))
  14319. (defun org-get-wdays (ts)
  14320. "Get the deadline lead time appropriate for timestring TS."
  14321. (cond
  14322. ((<= org-deadline-warning-days 0)
  14323. ;; 0 or negative, enforce this value no matter what
  14324. (- org-deadline-warning-days))
  14325. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14326. ;; lead time is specified.
  14327. (floor (* (string-to-number (match-string 1 ts))
  14328. (cdr (assoc (match-string 2 ts)
  14329. '(("d" . 1) ("w" . 7)
  14330. ("m" . 30.4) ("y" . 365.25)))))))
  14331. ;; go for the default.
  14332. (t org-deadline-warning-days)))
  14333. (defun org-calendar-select-mouse (ev)
  14334. "Return to `org-read-date' with the date currently selected.
  14335. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14336. (interactive "e")
  14337. (mouse-set-point ev)
  14338. (when (calendar-cursor-to-date)
  14339. (let* ((date (calendar-cursor-to-date))
  14340. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14341. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14342. (if (active-minibuffer-window) (exit-minibuffer))))
  14343. (defun org-check-deadlines (ndays)
  14344. "Check if there are any deadlines due or past due.
  14345. A deadline is considered due if it happens within `org-deadline-warning-days'
  14346. days from today's date. If the deadline appears in an entry marked DONE,
  14347. it is not shown. The prefix arg NDAYS can be used to test that many
  14348. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14349. (interactive "P")
  14350. (let* ((org-warn-days
  14351. (cond
  14352. ((equal ndays '(4)) 100000)
  14353. (ndays (prefix-numeric-value ndays))
  14354. (t (abs org-deadline-warning-days))))
  14355. (case-fold-search nil)
  14356. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14357. (callback
  14358. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14359. (message "%d deadlines past-due or due within %d days"
  14360. (org-occur regexp nil callback)
  14361. org-warn-days)))
  14362. (defun org-check-before-date (date)
  14363. "Check if there are deadlines or scheduled entries before DATE."
  14364. (interactive (list (org-read-date)))
  14365. (let ((case-fold-search nil)
  14366. (regexp (concat "\\<\\(" org-deadline-string
  14367. "\\|" org-scheduled-string
  14368. "\\) *<\\([^>]+\\)>"))
  14369. (callback
  14370. (lambda () (time-less-p
  14371. (org-time-string-to-time (match-string 2))
  14372. (org-time-string-to-time date)))))
  14373. (message "%d entries before %s"
  14374. (org-occur regexp nil callback) date)))
  14375. (defun org-check-after-date (date)
  14376. "Check if there are deadlines or scheduled entries after DATE."
  14377. (interactive (list (org-read-date)))
  14378. (let ((case-fold-search nil)
  14379. (regexp (concat "\\<\\(" org-deadline-string
  14380. "\\|" org-scheduled-string
  14381. "\\) *<\\([^>]+\\)>"))
  14382. (callback
  14383. (lambda () (not
  14384. (time-less-p
  14385. (org-time-string-to-time (match-string 2))
  14386. (org-time-string-to-time date))))))
  14387. (message "%d entries after %s"
  14388. (org-occur regexp nil callback) date)))
  14389. (defun org-check-dates-range (start-date end-date)
  14390. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14391. (interactive (list (org-read-date nil nil nil "Range starts")
  14392. (org-read-date nil nil nil "Range end")))
  14393. (let ((case-fold-search nil)
  14394. (regexp (concat "\\<\\(" org-deadline-string
  14395. "\\|" org-scheduled-string
  14396. "\\) *<\\([^>]+\\)>"))
  14397. (callback
  14398. (lambda ()
  14399. (let ((match (match-string 2)))
  14400. (and
  14401. (not (time-less-p
  14402. (org-time-string-to-time match)
  14403. (org-time-string-to-time start-date)))
  14404. (time-less-p
  14405. (org-time-string-to-time match)
  14406. (org-time-string-to-time end-date)))))))
  14407. (message "%d entries between %s and %s"
  14408. (org-occur regexp nil callback) start-date end-date)))
  14409. (defun org-evaluate-time-range (&optional to-buffer)
  14410. "Evaluate a time range by computing the difference between start and end.
  14411. Normally the result is just printed in the echo area, but with prefix arg
  14412. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14413. If the time range is actually in a table, the result is inserted into the
  14414. next column.
  14415. For time difference computation, a year is assumed to be exactly 365
  14416. days in order to avoid rounding problems."
  14417. (interactive "P")
  14418. (or
  14419. (org-clock-update-time-maybe)
  14420. (save-excursion
  14421. (unless (org-at-date-range-p t)
  14422. (goto-char (point-at-bol))
  14423. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14424. (if (not (org-at-date-range-p t))
  14425. (error "Not at a time-stamp range, and none found in current line")))
  14426. (let* ((ts1 (match-string 1))
  14427. (ts2 (match-string 2))
  14428. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14429. (match-end (match-end 0))
  14430. (time1 (org-time-string-to-time ts1))
  14431. (time2 (org-time-string-to-time ts2))
  14432. (t1 (org-float-time time1))
  14433. (t2 (org-float-time time2))
  14434. (diff (abs (- t2 t1)))
  14435. (negative (< (- t2 t1) 0))
  14436. ;; (ys (floor (* 365 24 60 60)))
  14437. (ds (* 24 60 60))
  14438. (hs (* 60 60))
  14439. (fy "%dy %dd %02d:%02d")
  14440. (fy1 "%dy %dd")
  14441. (fd "%dd %02d:%02d")
  14442. (fd1 "%dd")
  14443. (fh "%02d:%02d")
  14444. y d h m align)
  14445. (if havetime
  14446. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14447. y 0
  14448. d (floor (/ diff ds)) diff (mod diff ds)
  14449. h (floor (/ diff hs)) diff (mod diff hs)
  14450. m (floor (/ diff 60)))
  14451. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14452. y 0
  14453. d (floor (+ (/ diff ds) 0.5))
  14454. h 0 m 0))
  14455. (if (not to-buffer)
  14456. (message "%s" (org-make-tdiff-string y d h m))
  14457. (if (org-at-table-p)
  14458. (progn
  14459. (goto-char match-end)
  14460. (setq align t)
  14461. (and (looking-at " *|") (goto-char (match-end 0))))
  14462. (goto-char match-end))
  14463. (if (looking-at
  14464. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14465. (replace-match ""))
  14466. (if negative (insert " -"))
  14467. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14468. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14469. (insert " " (format fh h m))))
  14470. (if align (org-table-align))
  14471. (message "Time difference inserted")))))
  14472. (defun org-make-tdiff-string (y d h m)
  14473. (let ((fmt "")
  14474. (l nil))
  14475. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14476. l (push y l)))
  14477. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14478. l (push d l)))
  14479. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14480. l (push h l)))
  14481. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14482. l (push m l)))
  14483. (apply 'format fmt (nreverse l))))
  14484. (defun org-time-string-to-time (s &optional buffer pos)
  14485. (condition-case errdata
  14486. (apply 'encode-time (org-parse-time-string s))
  14487. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14488. s (if (not (and buffer pos))
  14489. ""
  14490. (format " at %d in buffer `%s'" pos buffer))
  14491. (cdr errdata)))))
  14492. (defun org-time-string-to-seconds (s)
  14493. (org-float-time (org-time-string-to-time s)))
  14494. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14495. "Convert a time stamp to an absolute day number.
  14496. If there is a specifier for a cyclic time stamp, get the closest date to
  14497. DAYNR.
  14498. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14499. The variable date is bound by the calendar when this is called."
  14500. (cond
  14501. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14502. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14503. daynr
  14504. (+ daynr 1000)))
  14505. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14506. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14507. (time-to-days (current-time))) (match-string 0 s)
  14508. prefer show-all))
  14509. (t (time-to-days
  14510. (condition-case errdata
  14511. (apply 'encode-time (org-parse-time-string s))
  14512. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14513. s (if (not (and buffer pos))
  14514. ""
  14515. (format " at %d in buffer `%s'" pos buffer))
  14516. (cdr errdata))))))))
  14517. (defun org-days-to-iso-week (days)
  14518. "Return the iso week number."
  14519. (require 'cal-iso)
  14520. (car (calendar-iso-from-absolute days)))
  14521. (defun org-small-year-to-year (year)
  14522. "Convert 2-digit years into 4-digit years.
  14523. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14524. The year 2000 cannot be abbreviated. Any year larger than 99
  14525. is returned unchanged."
  14526. (if (< year 38)
  14527. (setq year (+ 2000 year))
  14528. (if (< year 100)
  14529. (setq year (+ 1900 year))))
  14530. year)
  14531. (defun org-time-from-absolute (d)
  14532. "Return the time corresponding to date D.
  14533. D may be an absolute day number, or a calendar-type list (month day year)."
  14534. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14535. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14536. (defun org-calendar-holiday ()
  14537. "List of holidays, for Diary display in Org-mode."
  14538. (require 'holidays)
  14539. (let ((hl (funcall
  14540. (if (fboundp 'calendar-check-holidays)
  14541. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14542. (if hl (mapconcat 'identity hl "; "))))
  14543. (defun org-diary-sexp-entry (sexp entry date)
  14544. "Process a SEXP diary ENTRY for DATE."
  14545. (require 'diary-lib)
  14546. (let ((result (if calendar-debug-sexp
  14547. (let ((stack-trace-on-error t))
  14548. (eval (car (read-from-string sexp))))
  14549. (condition-case nil
  14550. (eval (car (read-from-string sexp)))
  14551. (error
  14552. (beep)
  14553. (message "Bad sexp at line %d in %s: %s"
  14554. (org-current-line)
  14555. (buffer-file-name) sexp)
  14556. (sleep-for 2))))))
  14557. (cond ((stringp result) (split-string result "; "))
  14558. ((and (consp result)
  14559. (not (consp (cdr result)))
  14560. (stringp (cdr result))) (cdr result))
  14561. ((and (consp result)
  14562. (stringp (car result))) result)
  14563. (result entry)
  14564. (t nil))))
  14565. (defun org-diary-to-ical-string (frombuf)
  14566. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14567. This uses the icalendar.el library."
  14568. (let* ((tmpdir (if (featurep 'xemacs)
  14569. (temp-directory)
  14570. temporary-file-directory))
  14571. (tmpfile (make-temp-name
  14572. (expand-file-name "orgics" tmpdir)))
  14573. buf rtn b e)
  14574. (with-current-buffer frombuf
  14575. (icalendar-export-region (point-min) (point-max) tmpfile)
  14576. (setq buf (find-buffer-visiting tmpfile))
  14577. (set-buffer buf)
  14578. (goto-char (point-min))
  14579. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14580. (setq b (match-beginning 0)))
  14581. (goto-char (point-max))
  14582. (if (re-search-backward "^END:VEVENT" nil t)
  14583. (setq e (match-end 0)))
  14584. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14585. (kill-buffer buf)
  14586. (delete-file tmpfile)
  14587. rtn))
  14588. (defun org-closest-date (start current change prefer show-all)
  14589. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14590. When PREFER is `past', return a date that is either CURRENT or past.
  14591. When PREFER is `future', return a date that is either CURRENT or future.
  14592. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14593. ;; Make the proper lists from the dates
  14594. (catch 'exit
  14595. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14596. dn dw sday cday n1 n2 n0
  14597. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14598. (setq start (org-date-to-gregorian start)
  14599. current (org-date-to-gregorian
  14600. (if show-all
  14601. current
  14602. (time-to-days (current-time))))
  14603. sday (calendar-absolute-from-gregorian start)
  14604. cday (calendar-absolute-from-gregorian current))
  14605. (if (<= cday sday) (throw 'exit sday))
  14606. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14607. (setq dn (string-to-number (match-string 1 change))
  14608. dw (cdr (assoc (match-string 2 change) a1)))
  14609. (error "Invalid change specifier: %s" change))
  14610. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14611. (cond
  14612. ((eq dw 'day)
  14613. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14614. n2 (+ n1 dn)))
  14615. ((eq dw 'year)
  14616. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14617. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14618. (setq date1 (list m d y1)
  14619. n1 (calendar-absolute-from-gregorian date1)
  14620. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14621. n2 (calendar-absolute-from-gregorian date2)))
  14622. ((eq dw 'month)
  14623. ;; approx number of month between the two dates
  14624. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14625. ;; How often does dn fit in there?
  14626. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14627. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14628. m (+ m nm)
  14629. ny (floor (/ m 12))
  14630. y (+ y ny)
  14631. m (- m (* ny 12)))
  14632. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14633. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14634. (setq m2 (+ m dn) y2 y)
  14635. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14636. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14637. (while (<= n2 cday)
  14638. (setq n1 n2 m m2 y y2)
  14639. (setq m2 (+ m dn) y2 y)
  14640. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14641. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14642. ;; Make sure n1 is the earlier date
  14643. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14644. (if show-all
  14645. (cond
  14646. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14647. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14648. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14649. (cond
  14650. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14651. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14652. (t (if (= cday n1) n1 n2)))))))
  14653. (defun org-date-to-gregorian (date)
  14654. "Turn any specification of DATE into a Gregorian date for the calendar."
  14655. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14656. ((and (listp date) (= (length date) 3)) date)
  14657. ((stringp date)
  14658. (setq date (org-parse-time-string date))
  14659. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14660. ((listp date)
  14661. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14662. (defun org-parse-time-string (s &optional nodefault)
  14663. "Parse the standard Org-mode time string.
  14664. This should be a lot faster than the normal `parse-time-string'.
  14665. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14666. hour and minute fields will be nil if not given."
  14667. (if (string-match org-ts-regexp0 s)
  14668. (list 0
  14669. (if (or (match-beginning 8) (not nodefault))
  14670. (string-to-number (or (match-string 8 s) "0")))
  14671. (if (or (match-beginning 7) (not nodefault))
  14672. (string-to-number (or (match-string 7 s) "0")))
  14673. (string-to-number (match-string 4 s))
  14674. (string-to-number (match-string 3 s))
  14675. (string-to-number (match-string 2 s))
  14676. nil nil nil)
  14677. (error "Not a standard Org-mode time string: %s" s)))
  14678. (defun org-timestamp-up (&optional arg)
  14679. "Increase the date item at the cursor by one.
  14680. If the cursor is on the year, change the year. If it is on the month,
  14681. the day or the time, change that.
  14682. With prefix ARG, change by that many units."
  14683. (interactive "p")
  14684. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14685. (defun org-timestamp-down (&optional arg)
  14686. "Decrease the date item at the cursor by one.
  14687. If the cursor is on the year, change the year. If it is on the month,
  14688. the day or the time, change that.
  14689. With prefix ARG, change by that many units."
  14690. (interactive "p")
  14691. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14692. (defun org-timestamp-up-day (&optional arg)
  14693. "Increase the date in the time stamp by one day.
  14694. With prefix ARG, change that many days."
  14695. (interactive "p")
  14696. (if (and (not (org-at-timestamp-p t))
  14697. (org-at-heading-p))
  14698. (org-todo 'up)
  14699. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14700. (defun org-timestamp-down-day (&optional arg)
  14701. "Decrease the date in the time stamp by one day.
  14702. With prefix ARG, change that many days."
  14703. (interactive "p")
  14704. (if (and (not (org-at-timestamp-p t))
  14705. (org-at-heading-p))
  14706. (org-todo 'down)
  14707. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14708. (defun org-at-timestamp-p (&optional inactive-ok)
  14709. "Determine if the cursor is in or at a timestamp."
  14710. (interactive)
  14711. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14712. (pos (point))
  14713. (ans (or (looking-at tsr)
  14714. (save-excursion
  14715. (skip-chars-backward "^[<\n\r\t")
  14716. (if (> (point) (point-min)) (backward-char 1))
  14717. (and (looking-at tsr)
  14718. (> (- (match-end 0) pos) -1))))))
  14719. (and ans
  14720. (boundp 'org-ts-what)
  14721. (setq org-ts-what
  14722. (cond
  14723. ((= pos (match-beginning 0)) 'bracket)
  14724. ;; Point is considered to be "on the bracket" whether
  14725. ;; it's really on it or right after it.
  14726. ((or (= pos (1- (match-end 0)))
  14727. (= pos (match-end 0))) 'bracket)
  14728. ((org-pos-in-match-range pos 2) 'year)
  14729. ((org-pos-in-match-range pos 3) 'month)
  14730. ((org-pos-in-match-range pos 7) 'hour)
  14731. ((org-pos-in-match-range pos 8) 'minute)
  14732. ((or (org-pos-in-match-range pos 4)
  14733. (org-pos-in-match-range pos 5)) 'day)
  14734. ((and (> pos (or (match-end 8) (match-end 5)))
  14735. (< pos (match-end 0)))
  14736. (- pos (or (match-end 8) (match-end 5))))
  14737. (t 'day))))
  14738. ans))
  14739. (defun org-toggle-timestamp-type ()
  14740. "Toggle the type (<active> or [inactive]) of a time stamp."
  14741. (interactive)
  14742. (when (org-at-timestamp-p t)
  14743. (let ((beg (match-beginning 0)) (end (match-end 0))
  14744. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14745. (save-excursion
  14746. (goto-char beg)
  14747. (while (re-search-forward "[][<>]" end t)
  14748. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14749. t t)))
  14750. (message "Timestamp is now %sactive"
  14751. (if (equal (char-after beg) ?<) "" "in")))))
  14752. (defun org-timestamp-change (n &optional what updown)
  14753. "Change the date in the time stamp at point.
  14754. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14755. `year', `minute', `second'. If WHAT is not given, the cursor position
  14756. in the timestamp determines what will be changed."
  14757. (let ((origin (point)) origin-cat
  14758. with-hm inactive
  14759. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14760. org-ts-what
  14761. extra rem
  14762. ts time time0)
  14763. (if (not (org-at-timestamp-p t))
  14764. (error "Not at a timestamp"))
  14765. (if (and (not what) (eq org-ts-what 'bracket))
  14766. (org-toggle-timestamp-type)
  14767. ;; Point isn't on brackets. Remember the part of the time-stamp
  14768. ;; the point was in. Indeed, size of time-stamps may change,
  14769. ;; but point must be kept in the same category nonetheless.
  14770. (setq origin-cat org-ts-what)
  14771. (if (and (not what) (not (eq org-ts-what 'day))
  14772. org-display-custom-times
  14773. (get-text-property (point) 'display)
  14774. (not (get-text-property (1- (point)) 'display)))
  14775. (setq org-ts-what 'day))
  14776. (setq org-ts-what (or what org-ts-what)
  14777. inactive (= (char-after (match-beginning 0)) ?\[)
  14778. ts (match-string 0))
  14779. (replace-match "")
  14780. (if (string-match
  14781. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  14782. ts)
  14783. (setq extra (match-string 1 ts)))
  14784. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14785. (setq with-hm t))
  14786. (setq time0 (org-parse-time-string ts))
  14787. (when (and updown
  14788. (eq org-ts-what 'minute)
  14789. (not current-prefix-arg))
  14790. ;; This looks like s-up and s-down. Change by one rounding step.
  14791. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14792. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14793. (setcar (cdr time0) (+ (nth 1 time0)
  14794. (if (> n 0) (- rem) (- dm rem))))))
  14795. (setq time
  14796. (encode-time (or (car time0) 0)
  14797. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14798. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14799. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14800. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14801. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14802. (nthcdr 6 time0)))
  14803. (when (and (member org-ts-what '(hour minute))
  14804. extra
  14805. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14806. (setq extra (org-modify-ts-extra
  14807. extra
  14808. (if (eq org-ts-what 'hour) 2 5)
  14809. n dm)))
  14810. (when (integerp org-ts-what)
  14811. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14812. (if (eq what 'calendar)
  14813. (let ((cal-date (org-get-date-from-calendar)))
  14814. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14815. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14816. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14817. (setcar time0 (or (car time0) 0))
  14818. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14819. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14820. (setq time (apply 'encode-time time0))))
  14821. ;; Insert the new time-stamp, and ensure point stays in the same
  14822. ;; category as before (i.e. not after the last position in that
  14823. ;; category).
  14824. (let ((pos (point)))
  14825. ;; Stay before inserted string. `save-excursion' is of no use.
  14826. (setq org-last-changed-timestamp
  14827. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14828. (goto-char pos))
  14829. (save-match-data
  14830. (looking-at org-ts-regexp3)
  14831. (goto-char (cond
  14832. ;; `day' category ends before `hour' if any, or at
  14833. ;; the end of the day name.
  14834. ((eq origin-cat 'day)
  14835. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14836. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14837. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14838. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14839. ;; `year' and `month' have both fixed size: point
  14840. ;; couldn't have moved into another part.
  14841. (t origin))))
  14842. ;; Update clock if on a CLOCK line.
  14843. (org-clock-update-time-maybe)
  14844. ;; Try to recenter the calendar window, if any.
  14845. (if (and org-calendar-follow-timestamp-change
  14846. (get-buffer-window "*Calendar*" t)
  14847. (memq org-ts-what '(day month year)))
  14848. (org-recenter-calendar (time-to-days time))))))
  14849. (defun org-modify-ts-extra (s pos n dm)
  14850. "Change the different parts of the lead-time and repeat fields in timestamp."
  14851. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14852. ng h m new rem)
  14853. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14854. (cond
  14855. ((or (org-pos-in-match-range pos 2)
  14856. (org-pos-in-match-range pos 3))
  14857. (setq m (string-to-number (match-string 3 s))
  14858. h (string-to-number (match-string 2 s)))
  14859. (if (org-pos-in-match-range pos 2)
  14860. (setq h (+ h n))
  14861. (setq n (* dm (org-no-warnings (signum n))))
  14862. (when (not (= 0 (setq rem (% m dm))))
  14863. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14864. (setq m (+ m n)))
  14865. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14866. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14867. (setq h (min 24 (max 0 h)))
  14868. (setq ng 1 new (format "-%02d:%02d" h m)))
  14869. ((org-pos-in-match-range pos 6)
  14870. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14871. ((org-pos-in-match-range pos 5)
  14872. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14873. ((org-pos-in-match-range pos 9)
  14874. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14875. ((org-pos-in-match-range pos 8)
  14876. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14877. (when ng
  14878. (setq s (concat
  14879. (substring s 0 (match-beginning ng))
  14880. new
  14881. (substring s (match-end ng))))))
  14882. s))
  14883. (defun org-recenter-calendar (date)
  14884. "If the calendar is visible, recenter it to DATE."
  14885. (let ((cwin (get-buffer-window "*Calendar*" t)))
  14886. (when cwin
  14887. (let ((calendar-move-hook nil))
  14888. (with-selected-window cwin
  14889. (calendar-goto-date (if (listp date) date
  14890. (calendar-gregorian-from-absolute date))))))))
  14891. (defun org-goto-calendar (&optional arg)
  14892. "Go to the Emacs calendar at the current date.
  14893. If there is a time stamp in the current line, go to that date.
  14894. A prefix ARG can be used to force the current date."
  14895. (interactive "P")
  14896. (let ((tsr org-ts-regexp) diff
  14897. (calendar-move-hook nil)
  14898. (calendar-view-holidays-initially-flag nil)
  14899. (calendar-view-diary-initially-flag nil))
  14900. (if (or (org-at-timestamp-p)
  14901. (save-excursion
  14902. (beginning-of-line 1)
  14903. (looking-at (concat ".*" tsr))))
  14904. (let ((d1 (time-to-days (current-time)))
  14905. (d2 (time-to-days
  14906. (org-time-string-to-time (match-string 1)))))
  14907. (setq diff (- d2 d1))))
  14908. (calendar)
  14909. (calendar-goto-today)
  14910. (if (and diff (not arg)) (calendar-forward-day diff))))
  14911. (defun org-get-date-from-calendar ()
  14912. "Return a list (month day year) of date at point in calendar."
  14913. (with-current-buffer "*Calendar*"
  14914. (save-match-data
  14915. (calendar-cursor-to-date))))
  14916. (defun org-date-from-calendar ()
  14917. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14918. If there is already a time stamp at the cursor position, update it."
  14919. (interactive)
  14920. (if (org-at-timestamp-p t)
  14921. (org-timestamp-change 0 'calendar)
  14922. (let ((cal-date (org-get-date-from-calendar)))
  14923. (org-insert-time-stamp
  14924. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14925. (defun org-minutes-to-hh:mm-string (m)
  14926. "Compute H:MM from a number of minutes."
  14927. (let ((h (/ m 60)))
  14928. (setq m (- m (* 60 h)))
  14929. (format org-time-clocksum-format h m)))
  14930. (defun org-hh:mm-string-to-minutes (s)
  14931. "Convert a string H:MM to a number of minutes.
  14932. If the string is just a number, interpret it as minutes.
  14933. In fact, the first hh:mm or number in the string will be taken,
  14934. there can be extra stuff in the string.
  14935. If no number is found, the return value is 0."
  14936. (cond
  14937. ((integerp s) s)
  14938. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14939. (+ (* (string-to-number (match-string 1 s)) 60)
  14940. (string-to-number (match-string 2 s))))
  14941. ((string-match "\\([0-9]+\\)" s)
  14942. (string-to-number (match-string 1 s)))
  14943. (t 0)))
  14944. (defcustom org-effort-durations
  14945. `(("h" . 60)
  14946. ("d" . ,(* 60 8))
  14947. ("w" . ,(* 60 8 5))
  14948. ("m" . ,(* 60 8 5 4))
  14949. ("y" . ,(* 60 8 5 40)))
  14950. "Conversion factor to minutes for an effort modifier.
  14951. Each entry has the form (MODIFIER . MINUTES).
  14952. In an effort string, a number followed by MODIFIER is multiplied
  14953. by the specified number of MINUTES to obtain an effort in
  14954. minutes.
  14955. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14956. effort string \"2hours\" is equivalent to 120 minutes."
  14957. :group 'org-agenda
  14958. :version "24.1"
  14959. :type '(alist :key-type (string :tag "Modifier")
  14960. :value-type (number :tag "Minutes")))
  14961. (defun org-duration-string-to-minutes (s)
  14962. "Convert a duration string S to minutes.
  14963. A bare number is interpreted as minutes, modifiers can be set by
  14964. customizing `org-effort-durations' (which see).
  14965. Entries containing a colon are interpreted as H:MM by
  14966. `org-hh:mm-string-to-minutes'."
  14967. (let ((result 0)
  14968. (re (concat "\\([0-9]+\\) *\\("
  14969. (regexp-opt (mapcar 'car org-effort-durations))
  14970. "\\)")))
  14971. (while (string-match re s)
  14972. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14973. (string-to-number (match-string 1 s))))
  14974. (setq s (replace-match "" nil t s)))
  14975. (incf result (org-hh:mm-string-to-minutes s))
  14976. result))
  14977. ;;;; Files
  14978. (defun org-save-all-org-buffers ()
  14979. "Save all Org-mode buffers without user confirmation."
  14980. (interactive)
  14981. (message "Saving all Org-mode buffers...")
  14982. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  14983. (when (featurep 'org-id) (org-id-locations-save))
  14984. (message "Saving all Org-mode buffers... done"))
  14985. (defun org-revert-all-org-buffers ()
  14986. "Revert all Org-mode buffers.
  14987. Prompt for confirmation when there are unsaved changes.
  14988. Be sure you know what you are doing before letting this function
  14989. overwrite your changes.
  14990. This function is useful in a setup where one tracks org files
  14991. with a version control system, to revert on one machine after pulling
  14992. changes from another. I believe the procedure must be like this:
  14993. 1. M-x org-save-all-org-buffers
  14994. 2. Pull changes from the other machine, resolve conflicts
  14995. 3. M-x org-revert-all-org-buffers"
  14996. (interactive)
  14997. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14998. (error "Abort"))
  14999. (save-excursion
  15000. (save-window-excursion
  15001. (mapc
  15002. (lambda (b)
  15003. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15004. (with-current-buffer b buffer-file-name))
  15005. (org-pop-to-buffer-same-window b)
  15006. (revert-buffer t 'no-confirm)))
  15007. (buffer-list))
  15008. (when (and (featurep 'org-id) org-id-track-globally)
  15009. (org-id-locations-load)))))
  15010. ;;;; Agenda files
  15011. ;;;###autoload
  15012. (defun org-switchb (&optional arg)
  15013. "Switch between Org buffers.
  15014. With one prefix argument, restrict available buffers to files.
  15015. With two prefix arguments, restrict available buffers to agenda files.
  15016. Defaults to `iswitchb' for buffer name completion.
  15017. Set `org-completion-use-ido' to make it use ido instead."
  15018. (interactive "P")
  15019. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15020. ((equal arg '(16)) (org-buffer-list 'agenda))
  15021. (t (org-buffer-list))))
  15022. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15023. (org-completion-use-ido org-completion-use-ido))
  15024. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15025. (setq org-completion-use-iswitchb t))
  15026. (org-pop-to-buffer-same-window
  15027. (org-icompleting-read "Org buffer: "
  15028. (mapcar 'list (mapcar 'buffer-name blist))
  15029. nil t))))
  15030. ;;; Define some older names previously used for this functionality
  15031. ;;;###autoload
  15032. (defalias 'org-ido-switchb 'org-switchb)
  15033. ;;;###autoload
  15034. (defalias 'org-iswitchb 'org-switchb)
  15035. (defun org-buffer-list (&optional predicate exclude-tmp)
  15036. "Return a list of Org buffers.
  15037. PREDICATE can be `export', `files' or `agenda'.
  15038. export restrict the list to Export buffers.
  15039. files restrict the list to buffers visiting Org files.
  15040. agenda restrict the list to buffers visiting agenda files.
  15041. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15042. (let* ((bfn nil)
  15043. (agenda-files (and (eq predicate 'agenda)
  15044. (mapcar 'file-truename (org-agenda-files t))))
  15045. (filter
  15046. (cond
  15047. ((eq predicate 'files)
  15048. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15049. ((eq predicate 'export)
  15050. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15051. ((eq predicate 'agenda)
  15052. (lambda (b)
  15053. (with-current-buffer b
  15054. (and (derived-mode-p 'org-mode)
  15055. (setq bfn (buffer-file-name b))
  15056. (member (file-truename bfn) agenda-files)))))
  15057. (t (lambda (b) (with-current-buffer b
  15058. (or (derived-mode-p 'org-mode)
  15059. (string-match "\*Org .*Export"
  15060. (buffer-name b)))))))))
  15061. (delq nil
  15062. (mapcar
  15063. (lambda(b)
  15064. (if (and (funcall filter b)
  15065. (or (not exclude-tmp)
  15066. (not (string-match "tmp" (buffer-name b)))))
  15067. b
  15068. nil))
  15069. (buffer-list)))))
  15070. (defun org-agenda-files (&optional unrestricted archives)
  15071. "Get the list of agenda files.
  15072. Optional UNRESTRICTED means return the full list even if a restriction
  15073. is currently in place.
  15074. When ARCHIVES is t, include all archive files that are really being
  15075. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15076. `org-agenda-archives-mode' is t."
  15077. (let ((files
  15078. (cond
  15079. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15080. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15081. ((listp org-agenda-files) org-agenda-files)
  15082. (t (error "Invalid value of `org-agenda-files'")))))
  15083. (setq files (apply 'append
  15084. (mapcar (lambda (f)
  15085. (if (file-directory-p f)
  15086. (directory-files
  15087. f t org-agenda-file-regexp)
  15088. (list f)))
  15089. files)))
  15090. (when org-agenda-skip-unavailable-files
  15091. (setq files (delq nil
  15092. (mapcar (function
  15093. (lambda (file)
  15094. (and (file-readable-p file) file)))
  15095. files))))
  15096. (when (or (eq archives t)
  15097. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15098. (setq files (org-add-archive-files files)))
  15099. files))
  15100. (defun org-agenda-file-p (&optional file)
  15101. "Return non-nil, if FILE is an agenda file.
  15102. If FILE is omitted, use the file associated with the current
  15103. buffer."
  15104. (member (or file (buffer-file-name))
  15105. (org-agenda-files t)))
  15106. (defun org-edit-agenda-file-list ()
  15107. "Edit the list of agenda files.
  15108. Depending on setup, this either uses customize to edit the variable
  15109. `org-agenda-files', or it visits the file that is holding the list. In the
  15110. latter case, the buffer is set up in a way that saving it automatically kills
  15111. the buffer and restores the previous window configuration."
  15112. (interactive)
  15113. (if (stringp org-agenda-files)
  15114. (let ((cw (current-window-configuration)))
  15115. (find-file org-agenda-files)
  15116. (org-set-local 'org-window-configuration cw)
  15117. (org-add-hook 'after-save-hook
  15118. (lambda ()
  15119. (set-window-configuration
  15120. (prog1 org-window-configuration
  15121. (kill-buffer (current-buffer))))
  15122. (org-install-agenda-files-menu)
  15123. (message "New agenda file list installed"))
  15124. nil 'local)
  15125. (message "%s" (substitute-command-keys
  15126. "Edit list and finish with \\[save-buffer]")))
  15127. (customize-variable 'org-agenda-files)))
  15128. (defun org-store-new-agenda-file-list (list)
  15129. "Set new value for the agenda file list and save it correctly."
  15130. (if (stringp org-agenda-files)
  15131. (let ((fe (org-read-agenda-file-list t)) b u)
  15132. (while (setq b (find-buffer-visiting org-agenda-files))
  15133. (kill-buffer b))
  15134. (with-temp-file org-agenda-files
  15135. (insert
  15136. (mapconcat
  15137. (lambda (f) ;; Keep un-expanded entries.
  15138. (if (setq u (assoc f fe))
  15139. (cdr u)
  15140. f))
  15141. list "\n")
  15142. "\n")))
  15143. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15144. (org-insert-mode-line-in-empty-file nil))
  15145. (setq org-agenda-files list)
  15146. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15147. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15148. "Read the list of agenda files from a file.
  15149. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15150. filenames, used by `org-store-new-agenda-file-list' to write back
  15151. un-expanded file names."
  15152. (when (file-directory-p org-agenda-files)
  15153. (error "`org-agenda-files' cannot be a single directory"))
  15154. (when (stringp org-agenda-files)
  15155. (with-temp-buffer
  15156. (insert-file-contents org-agenda-files)
  15157. (mapcar
  15158. (lambda (f)
  15159. (let ((e (expand-file-name (substitute-in-file-name f)
  15160. org-directory)))
  15161. (if pair-with-expansion
  15162. (cons e f)
  15163. e)))
  15164. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15165. ;;;###autoload
  15166. (defun org-cycle-agenda-files ()
  15167. "Cycle through the files in `org-agenda-files'.
  15168. If the current buffer visits an agenda file, find the next one in the list.
  15169. If the current buffer does not, find the first agenda file."
  15170. (interactive)
  15171. (let* ((fs (org-agenda-files t))
  15172. (files (append fs (list (car fs))))
  15173. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15174. file)
  15175. (unless files (error "No agenda files"))
  15176. (catch 'exit
  15177. (while (setq file (pop files))
  15178. (if (equal (file-truename file) tcf)
  15179. (when (car files)
  15180. (find-file (car files))
  15181. (throw 'exit t))))
  15182. (find-file (car fs)))
  15183. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15184. (defun org-agenda-file-to-front (&optional to-end)
  15185. "Move/add the current file to the top of the agenda file list.
  15186. If the file is not present in the list, it is added to the front. If it is
  15187. present, it is moved there. With optional argument TO-END, add/move to the
  15188. end of the list."
  15189. (interactive "P")
  15190. (let ((org-agenda-skip-unavailable-files nil)
  15191. (file-alist (mapcar (lambda (x)
  15192. (cons (file-truename x) x))
  15193. (org-agenda-files t)))
  15194. (ctf (file-truename buffer-file-name))
  15195. x had)
  15196. (setq x (assoc ctf file-alist) had x)
  15197. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15198. (if to-end
  15199. (setq file-alist (append (delq x file-alist) (list x)))
  15200. (setq file-alist (cons x (delq x file-alist))))
  15201. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15202. (org-install-agenda-files-menu)
  15203. (message "File %s to %s of agenda file list"
  15204. (if had "moved" "added") (if to-end "end" "front"))))
  15205. (defun org-remove-file (&optional file)
  15206. "Remove current file from the list of files in variable `org-agenda-files'.
  15207. These are the files which are being checked for agenda entries.
  15208. Optional argument FILE means use this file instead of the current."
  15209. (interactive)
  15210. (let* ((org-agenda-skip-unavailable-files nil)
  15211. (file (or file buffer-file-name))
  15212. (true-file (file-truename file))
  15213. (afile (abbreviate-file-name file))
  15214. (files (delq nil (mapcar
  15215. (lambda (x)
  15216. (if (equal true-file
  15217. (file-truename x))
  15218. nil x))
  15219. (org-agenda-files t)))))
  15220. (if (not (= (length files) (length (org-agenda-files t))))
  15221. (progn
  15222. (org-store-new-agenda-file-list files)
  15223. (org-install-agenda-files-menu)
  15224. (message "Removed file: %s" afile))
  15225. (message "File was not in list: %s (not removed)" afile))))
  15226. (defun org-file-menu-entry (file)
  15227. (vector file (list 'find-file file) t))
  15228. (defun org-check-agenda-file (file)
  15229. "Make sure FILE exists. If not, ask user what to do."
  15230. (when (not (file-exists-p file))
  15231. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15232. (abbreviate-file-name file))
  15233. (let ((r (downcase (read-char-exclusive))))
  15234. (cond
  15235. ((equal r ?r)
  15236. (org-remove-file file)
  15237. (throw 'nextfile t))
  15238. (t (error "Abort"))))))
  15239. (defun org-get-agenda-file-buffer (file)
  15240. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15241. it to the list of buffers which might be released later."
  15242. (let ((buf (org-find-base-buffer-visiting file)))
  15243. (if buf
  15244. buf ; just return it
  15245. ;; Make a new buffer and remember it
  15246. (setq buf (find-file-noselect file))
  15247. (if buf (push buf org-agenda-new-buffers))
  15248. buf)))
  15249. (defun org-release-buffers (blist)
  15250. "Release all buffers in list, asking the user for confirmation when needed.
  15251. When a buffer is unmodified, it is just killed. When modified, it is saved
  15252. \(if the user agrees) and then killed."
  15253. (let (buf file)
  15254. (while (setq buf (pop blist))
  15255. (setq file (buffer-file-name buf))
  15256. (when (and (buffer-modified-p buf)
  15257. file
  15258. (y-or-n-p (format "Save file %s? " file)))
  15259. (with-current-buffer buf (save-buffer)))
  15260. (kill-buffer buf))))
  15261. (defun org-prepare-agenda-buffers (files)
  15262. "Create buffers for all agenda files, protect archived trees and comments."
  15263. (interactive)
  15264. (let ((pa '(:org-archived t))
  15265. (pc '(:org-comment t))
  15266. (pall '(:org-archived t :org-comment t))
  15267. (inhibit-read-only t)
  15268. (rea (concat ":" org-archive-tag ":"))
  15269. bmp file re)
  15270. (save-excursion
  15271. (save-restriction
  15272. (while (setq file (pop files))
  15273. (catch 'nextfile
  15274. (if (bufferp file)
  15275. (set-buffer file)
  15276. (org-check-agenda-file file)
  15277. (set-buffer (org-get-agenda-file-buffer file)))
  15278. (widen)
  15279. (setq bmp (buffer-modified-p))
  15280. (org-refresh-category-properties)
  15281. (setq org-todo-keywords-for-agenda
  15282. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15283. (setq org-done-keywords-for-agenda
  15284. (append org-done-keywords-for-agenda org-done-keywords))
  15285. (setq org-todo-keyword-alist-for-agenda
  15286. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15287. (setq org-drawers-for-agenda
  15288. (append org-drawers-for-agenda org-drawers))
  15289. (setq org-tag-alist-for-agenda
  15290. (append org-tag-alist-for-agenda org-tag-alist))
  15291. (save-excursion
  15292. (remove-text-properties (point-min) (point-max) pall)
  15293. (when org-agenda-skip-archived-trees
  15294. (goto-char (point-min))
  15295. (while (re-search-forward rea nil t)
  15296. (if (org-at-heading-p t)
  15297. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15298. (goto-char (point-min))
  15299. (setq re (format org-heading-keyword-regexp-format
  15300. org-comment-string))
  15301. (while (re-search-forward re nil t)
  15302. (add-text-properties
  15303. (match-beginning 0) (org-end-of-subtree t) pc)))
  15304. (set-buffer-modified-p bmp)))))
  15305. (setq org-todo-keywords-for-agenda
  15306. (org-uniquify org-todo-keywords-for-agenda))
  15307. (setq org-todo-keyword-alist-for-agenda
  15308. (org-uniquify org-todo-keyword-alist-for-agenda)
  15309. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15310. ;;;; Embedded LaTeX
  15311. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15312. "Keymap for the minor `org-cdlatex-mode'.")
  15313. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15314. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15315. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15316. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15317. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15318. (defvar org-cdlatex-texmathp-advice-is-done nil
  15319. "Flag remembering if we have applied the advice to texmathp already.")
  15320. (define-minor-mode org-cdlatex-mode
  15321. "Toggle the minor `org-cdlatex-mode'.
  15322. This mode supports entering LaTeX environment and math in LaTeX fragments
  15323. in Org-mode.
  15324. \\{org-cdlatex-mode-map}"
  15325. nil " OCDL" nil
  15326. (when org-cdlatex-mode
  15327. (require 'cdlatex)
  15328. (run-hooks 'cdlatex-mode-hook)
  15329. (cdlatex-compute-tables))
  15330. (unless org-cdlatex-texmathp-advice-is-done
  15331. (setq org-cdlatex-texmathp-advice-is-done t)
  15332. (defadvice texmathp (around org-math-always-on activate)
  15333. "Always return t in org-mode buffers.
  15334. This is because we want to insert math symbols without dollars even outside
  15335. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15336. an embedded LaTeX fragment, let texmathp do its job.
  15337. \\[org-cdlatex-mode-map]"
  15338. (interactive)
  15339. (let (p)
  15340. (cond
  15341. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15342. ((eq this-command 'cdlatex-math-symbol)
  15343. (setq ad-return-value t
  15344. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15345. (t
  15346. (let ((p (org-inside-LaTeX-fragment-p)))
  15347. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15348. (setq ad-return-value t
  15349. texmathp-why '("Org-mode embedded math" . 0))
  15350. (if p ad-do-it)))))))))
  15351. (defun turn-on-org-cdlatex ()
  15352. "Unconditionally turn on `org-cdlatex-mode'."
  15353. (org-cdlatex-mode 1))
  15354. (defun org-inside-LaTeX-fragment-p ()
  15355. "Test if point is inside a LaTeX fragment.
  15356. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15357. sequence appearing also before point.
  15358. Even though the matchers for math are configurable, this function assumes
  15359. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15360. delimiters are skipped when they have been removed by customization.
  15361. The return value is nil, or a cons cell with the delimiter and the
  15362. position of this delimiter.
  15363. This function does a reasonably good job, but can locally be fooled by
  15364. for example currency specifications. For example it will assume being in
  15365. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15366. fragments that are properly closed, but during editing, we have to live
  15367. with the uncertainty caused by missing closing delimiters. This function
  15368. looks only before point, not after."
  15369. (catch 'exit
  15370. (let ((pos (point))
  15371. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15372. (lim (progn
  15373. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15374. (point)))
  15375. dd-on str (start 0) m re)
  15376. (goto-char pos)
  15377. (when dodollar
  15378. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15379. re (nth 1 (assoc "$" org-latex-regexps)))
  15380. (while (string-match re str start)
  15381. (cond
  15382. ((= (match-end 0) (length str))
  15383. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15384. ((= (match-end 0) (- (length str) 5))
  15385. (throw 'exit nil))
  15386. (t (setq start (match-end 0))))))
  15387. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15388. (goto-char pos)
  15389. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15390. (and (match-beginning 2) (throw 'exit nil))
  15391. ;; count $$
  15392. (while (re-search-backward "\\$\\$" lim t)
  15393. (setq dd-on (not dd-on)))
  15394. (goto-char pos)
  15395. (if dd-on (cons "$$" m))))))
  15396. (defun org-inside-latex-macro-p ()
  15397. "Is point inside a LaTeX macro or its arguments?"
  15398. (save-match-data
  15399. (org-in-regexp
  15400. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15401. (defun org-try-cdlatex-tab ()
  15402. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15403. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15404. - inside a LaTeX fragment, or
  15405. - after the first word in a line, where an abbreviation expansion could
  15406. insert a LaTeX environment."
  15407. (when org-cdlatex-mode
  15408. (cond
  15409. ;; Before any word on the line: No expansion possible.
  15410. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15411. ;; Just after first word on the line: Expand it. Make sure it
  15412. ;; cannot happen on headlines, though.
  15413. ((save-excursion
  15414. (skip-chars-backward "a-zA-Z0-9*")
  15415. (skip-chars-backward " \t")
  15416. (and (bolp) (not (org-at-heading-p))))
  15417. (cdlatex-tab) t)
  15418. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15419. (defun org-cdlatex-underscore-caret (&optional arg)
  15420. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15421. Revert to the normal definition outside of these fragments."
  15422. (interactive "P")
  15423. (if (org-inside-LaTeX-fragment-p)
  15424. (call-interactively 'cdlatex-sub-superscript)
  15425. (let (org-cdlatex-mode)
  15426. (call-interactively (key-binding (vector last-input-event))))))
  15427. (defun org-cdlatex-math-modify (&optional arg)
  15428. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15429. Revert to the normal definition outside of these fragments."
  15430. (interactive "P")
  15431. (if (org-inside-LaTeX-fragment-p)
  15432. (call-interactively 'cdlatex-math-modify)
  15433. (let (org-cdlatex-mode)
  15434. (call-interactively (key-binding (vector last-input-event))))))
  15435. (defvar org-latex-fragment-image-overlays nil
  15436. "List of overlays carrying the images of latex fragments.")
  15437. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15438. (defun org-remove-latex-fragment-image-overlays ()
  15439. "Remove all overlays with LaTeX fragment images in current buffer."
  15440. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15441. (setq org-latex-fragment-image-overlays nil))
  15442. (defun org-preview-latex-fragment (&optional subtree)
  15443. "Preview the LaTeX fragment at point, or all locally or globally.
  15444. If the cursor is in a LaTeX fragment, create the image and overlay
  15445. it over the source code. If there is no fragment at point, display
  15446. all fragments in the current text, from one headline to the next. With
  15447. prefix SUBTREE, display all fragments in the current subtree. With a
  15448. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15449. the cursor is before the first headline,
  15450. display all fragments in the buffer.
  15451. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15452. (interactive "P")
  15453. (unless buffer-file-name
  15454. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15455. (org-remove-latex-fragment-image-overlays)
  15456. (save-excursion
  15457. (save-restriction
  15458. (let (beg end at msg)
  15459. (cond
  15460. ((or (equal subtree '(16))
  15461. (not (save-excursion
  15462. (re-search-backward org-outline-regexp-bol nil t))))
  15463. (setq beg (point-min) end (point-max)
  15464. msg "Creating images for buffer...%s"))
  15465. ((equal subtree '(4))
  15466. (org-back-to-heading)
  15467. (setq beg (point) end (org-end-of-subtree t)
  15468. msg "Creating images for subtree...%s"))
  15469. (t
  15470. (if (setq at (org-inside-LaTeX-fragment-p))
  15471. (goto-char (max (point-min) (- (cdr at) 2)))
  15472. (org-back-to-heading))
  15473. (setq beg (point) end (progn (outline-next-heading) (point))
  15474. msg (if at "Creating image...%s"
  15475. "Creating images for entry...%s"))))
  15476. (message msg "")
  15477. (narrow-to-region beg end)
  15478. (goto-char beg)
  15479. (org-format-latex
  15480. (concat "ltxpng/" (file-name-sans-extension
  15481. (file-name-nondirectory
  15482. buffer-file-name)))
  15483. default-directory 'overlays msg at 'forbuffer
  15484. org-latex-create-formula-image-program)
  15485. (message msg "done. Use `C-c C-c' to remove images.")))))
  15486. (defvar org-latex-regexps
  15487. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15488. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15489. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15490. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15491. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15492. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15493. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15494. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15495. "Regular expressions for matching embedded LaTeX.")
  15496. (defvar org-export-have-math nil) ;; dynamic scoping
  15497. (defun org-format-latex (prefix &optional dir overlays msg at
  15498. forbuffer processing-type)
  15499. "Replace LaTeX fragments with links to an image, and produce images.
  15500. Some of the options can be changed using the variable
  15501. `org-format-latex-options'."
  15502. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15503. (let* ((prefixnodir (file-name-nondirectory prefix))
  15504. (absprefix (expand-file-name prefix dir))
  15505. (todir (file-name-directory absprefix))
  15506. (opt org-format-latex-options)
  15507. (matchers (plist-get opt :matchers))
  15508. (re-list org-latex-regexps)
  15509. (org-format-latex-header-extra
  15510. (plist-get (org-infile-export-plist) :latex-header-extra))
  15511. (cnt 0) txt hash link beg end re e checkdir
  15512. executables-checked string
  15513. m n block-type block linkfile movefile ov)
  15514. ;; Check the different regular expressions
  15515. (while (setq e (pop re-list))
  15516. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15517. block (if block-type "\n\n" ""))
  15518. (when (member m matchers)
  15519. (goto-char (point-min))
  15520. (while (re-search-forward re nil t)
  15521. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15522. (not (get-text-property (match-beginning n)
  15523. 'org-protected))
  15524. (or (not overlays)
  15525. (not (eq (get-char-property (match-beginning n)
  15526. 'org-overlay-type)
  15527. 'org-latex-overlay))))
  15528. (setq org-export-have-math t)
  15529. (cond
  15530. ((eq processing-type 'verbatim)
  15531. ;; Leave the text verbatim, just protect it
  15532. (add-text-properties (match-beginning n) (match-end n)
  15533. '(org-protected t)))
  15534. ((eq processing-type 'mathjax)
  15535. ;; Prepare for MathJax processing
  15536. (setq string (match-string n))
  15537. (if (member m '("$" "$1"))
  15538. (save-excursion
  15539. (delete-region (match-beginning n) (match-end n))
  15540. (goto-char (match-beginning n))
  15541. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15542. "\\)")
  15543. '(org-protected t))))
  15544. (add-text-properties (match-beginning n) (match-end n)
  15545. '(org-protected t))))
  15546. ((or (eq processing-type 'dvipng)
  15547. (eq processing-type 'imagemagick))
  15548. ;; Process to an image
  15549. (setq txt (match-string n)
  15550. beg (match-beginning n) end (match-end n)
  15551. cnt (1+ cnt))
  15552. (let (print-length print-level) ; make sure full list is printed
  15553. (setq hash (sha1 (prin1-to-string
  15554. (list org-format-latex-header
  15555. org-format-latex-header-extra
  15556. org-export-latex-default-packages-alist
  15557. org-export-latex-packages-alist
  15558. org-format-latex-options
  15559. forbuffer txt)))
  15560. linkfile (format "%s_%s.png" prefix hash)
  15561. movefile (format "%s_%s.png" absprefix hash)))
  15562. (setq link (concat block "[[file:" linkfile "]]" block))
  15563. (if msg (message msg cnt))
  15564. (goto-char beg)
  15565. (unless checkdir ; make sure the directory exists
  15566. (setq checkdir t)
  15567. (or (file-directory-p todir) (make-directory todir t)))
  15568. (cond
  15569. ((eq processing-type 'dvipng)
  15570. (unless executables-checked
  15571. (org-check-external-command
  15572. "latex" "needed to convert LaTeX fragments to images")
  15573. (org-check-external-command
  15574. "dvipng" "needed to convert LaTeX fragments to images")
  15575. (setq executables-checked t))
  15576. (unless (file-exists-p movefile)
  15577. (org-create-formula-image-with-dvipng
  15578. txt movefile opt forbuffer)))
  15579. ((eq processing-type 'imagemagick)
  15580. (unless executables-checked
  15581. (org-check-external-command
  15582. "converte" "you need to install imagemagick")
  15583. (setq executables-checked t))
  15584. (unless (file-exists-p movefile)
  15585. (org-create-formula-image-with-imagemagick
  15586. txt movefile opt forbuffer))))
  15587. (if overlays
  15588. (progn
  15589. (mapc (lambda (o)
  15590. (if (eq (overlay-get o 'org-overlay-type)
  15591. 'org-latex-overlay)
  15592. (delete-overlay o)))
  15593. (overlays-in beg end))
  15594. (setq ov (make-overlay beg end))
  15595. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15596. (if (featurep 'xemacs)
  15597. (progn
  15598. (overlay-put ov 'invisible t)
  15599. (overlay-put
  15600. ov 'end-glyph
  15601. (make-glyph (vector 'png :file movefile))))
  15602. (overlay-put
  15603. ov 'display
  15604. (list 'image :type 'png :file movefile :ascent 'center)))
  15605. (push ov org-latex-fragment-image-overlays)
  15606. (goto-char end))
  15607. (delete-region beg end)
  15608. (insert (org-add-props link
  15609. (list 'org-latex-src
  15610. (replace-regexp-in-string
  15611. "\"" "" txt)
  15612. 'org-latex-src-embed-type
  15613. (if block-type 'paragraph 'character))))))
  15614. ((eq processing-type 'mathml)
  15615. ;; Process to MathML
  15616. (unless executables-checked
  15617. (unless (save-match-data (org-format-latex-mathml-available-p))
  15618. (error "LaTeX to MathML converter not configured"))
  15619. (setq executables-checked t))
  15620. (setq txt (match-string n)
  15621. beg (match-beginning n) end (match-end n)
  15622. cnt (1+ cnt))
  15623. (if msg (message msg cnt))
  15624. (goto-char beg)
  15625. (delete-region beg end)
  15626. (insert (org-format-latex-as-mathml
  15627. txt block-type prefix dir)))
  15628. (t
  15629. (error "Unknown conversion type %s for latex fragments"
  15630. processing-type)))))))))
  15631. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15632. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15633. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15634. sucessful, return the portion between \"<math...> </math>\"
  15635. elements otherwise return nil. When MATHML-FILE is specified,
  15636. write the results in to that file. When invoked as an
  15637. interactive command, prompt for LATEX-FRAG, with initial value
  15638. set to the current active region and echo the results for user
  15639. inspection."
  15640. (interactive (list (let ((frag (when (region-active-p)
  15641. (buffer-substring-no-properties
  15642. (region-beginning) (region-end)))))
  15643. (read-string "LaTeX Fragment: " frag nil frag))))
  15644. (unless latex-frag (error "Invalid latex-frag"))
  15645. (let* ((tmp-in-file (file-relative-name
  15646. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15647. (ignore (write-region latex-frag nil tmp-in-file))
  15648. (tmp-out-file (file-relative-name
  15649. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15650. (cmd (format-spec
  15651. org-latex-to-mathml-convert-command
  15652. `((?j . ,(shell-quote-argument
  15653. (expand-file-name org-latex-to-mathml-jar-file)))
  15654. (?I . ,(shell-quote-argument tmp-in-file))
  15655. (?o . ,(shell-quote-argument tmp-out-file)))))
  15656. mathml shell-command-output)
  15657. (when (org-called-interactively-p 'any)
  15658. (unless (org-format-latex-mathml-available-p)
  15659. (error "LaTeX to MathML converter not configured")))
  15660. (message "Running %s" cmd)
  15661. (setq shell-command-output (shell-command-to-string cmd))
  15662. (setq mathml
  15663. (when (file-readable-p tmp-out-file)
  15664. (with-current-buffer (find-file-noselect tmp-out-file t)
  15665. (goto-char (point-min))
  15666. (when (re-search-forward
  15667. (concat
  15668. (regexp-quote
  15669. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15670. "\\(.\\|\n\\)*"
  15671. (regexp-quote "</math>")) nil t)
  15672. (prog1 (match-string 0) (kill-buffer))))))
  15673. (cond
  15674. (mathml
  15675. (setq mathml
  15676. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15677. (when mathml-file
  15678. (write-region mathml nil mathml-file))
  15679. (when (org-called-interactively-p 'any)
  15680. (message mathml)))
  15681. ((message "LaTeX to MathML conversion failed")
  15682. (message shell-command-output)))
  15683. (delete-file tmp-in-file)
  15684. (when (file-exists-p tmp-out-file)
  15685. (delete-file tmp-out-file))
  15686. mathml))
  15687. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15688. prefix &optional dir)
  15689. "Use `org-create-math-formula' but check local cache first."
  15690. (let* ((absprefix (expand-file-name prefix dir))
  15691. (print-length nil) (print-level nil)
  15692. (formula-id (concat
  15693. "formula-"
  15694. (sha1
  15695. (prin1-to-string
  15696. (list latex-frag
  15697. org-latex-to-mathml-convert-command)))))
  15698. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15699. (formula-cache-dir (file-name-directory formula-cache)))
  15700. (unless (file-directory-p formula-cache-dir)
  15701. (make-directory formula-cache-dir t))
  15702. (unless (file-exists-p formula-cache)
  15703. (org-create-math-formula latex-frag formula-cache))
  15704. (if (file-exists-p formula-cache)
  15705. ;; Successful conversion. Return the link to MathML file.
  15706. (org-add-props
  15707. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15708. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15709. 'org-latex-src-embed-type (if latex-frag-type
  15710. 'paragraph 'character)))
  15711. ;; Failed conversion. Return the LaTeX fragment verbatim
  15712. (add-text-properties
  15713. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15714. latex-frag)))
  15715. ;; This function borrows from Ganesh Swami's latex2png.el
  15716. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  15717. "This calls dvipng."
  15718. (require 'org-latex)
  15719. (let* ((tmpdir (if (featurep 'xemacs)
  15720. (temp-directory)
  15721. temporary-file-directory))
  15722. (texfilebase (make-temp-name
  15723. (expand-file-name "orgtex" tmpdir)))
  15724. (texfile (concat texfilebase ".tex"))
  15725. (dvifile (concat texfilebase ".dvi"))
  15726. (pngfile (concat texfilebase ".png"))
  15727. (fnh (if (featurep 'xemacs)
  15728. (font-height (face-font 'default))
  15729. (face-attribute 'default :height nil)))
  15730. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15731. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15732. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15733. "Black"))
  15734. (bg (or (plist-get options (if buffer :background :html-background))
  15735. "Transparent")))
  15736. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15737. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15738. (with-temp-file texfile
  15739. (insert (org-splice-latex-header
  15740. org-format-latex-header
  15741. org-export-latex-default-packages-alist
  15742. org-export-latex-packages-alist t
  15743. org-format-latex-header-extra))
  15744. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15745. (require 'org-latex)
  15746. (org-export-latex-fix-inputenc))
  15747. (let ((dir default-directory))
  15748. (condition-case nil
  15749. (progn
  15750. (cd tmpdir)
  15751. (call-process "latex" nil nil nil texfile))
  15752. (error nil))
  15753. (cd dir))
  15754. (if (not (file-exists-p dvifile))
  15755. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15756. (condition-case nil
  15757. (if (featurep 'xemacs)
  15758. (call-process "dvipng" nil nil nil
  15759. "-fg" fg "-bg" bg
  15760. "-T" "tight"
  15761. "-o" pngfile
  15762. dvifile)
  15763. (call-process "dvipng" nil nil nil
  15764. "-fg" fg "-bg" bg
  15765. "-D" dpi
  15766. ;;"-x" scale "-y" scale
  15767. "-T" "tight"
  15768. "-o" pngfile
  15769. dvifile))
  15770. (error nil))
  15771. (if (not (file-exists-p pngfile))
  15772. (if org-format-latex-signal-error
  15773. (error "Failed to create png file from %s" texfile)
  15774. (message "Failed to create png file from %s" texfile)
  15775. nil)
  15776. ;; Use the requested file name and clean up
  15777. (copy-file pngfile tofile 'replace)
  15778. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  15779. (delete-file (concat texfilebase e)))
  15780. pngfile))))
  15781. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  15782. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  15783. "This calls convert, which is included into imagemagick."
  15784. (require 'org-latex)
  15785. (let* ((tmpdir (if (featurep 'xemacs)
  15786. (temp-directory)
  15787. temporary-file-directory))
  15788. (texfilebase (make-temp-name
  15789. (expand-file-name "orgtex" tmpdir)))
  15790. (texfile (concat texfilebase ".tex"))
  15791. (pdffile (concat texfilebase ".pdf"))
  15792. (pngfile (concat texfilebase ".png"))
  15793. (fnh (if (featurep 'xemacs)
  15794. (font-height (face-font 'default))
  15795. (face-attribute 'default :height nil)))
  15796. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15797. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15798. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15799. "black"))
  15800. (bg (or (plist-get options (if buffer :background :html-background))
  15801. "white")))
  15802. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  15803. (setq fg (org-latex-color-format fg)))
  15804. (if (eq bg 'default) (setq bg (org-latex-color :background))
  15805. (setq bg (org-latex-color-format
  15806. (if (string= bg "Transparent")(setq bg "white")))))
  15807. (with-temp-file texfile
  15808. (insert (org-splice-latex-header
  15809. org-format-latex-header
  15810. org-export-latex-default-packages-alist
  15811. org-export-latex-packages-alist t
  15812. org-format-latex-header-extra))
  15813. (insert "\n\\begin{document}\n"
  15814. "\\definecolor{fg}{rgb}{" fg "}\n"
  15815. "\\definecolor{bg}{rgb}{" bg "}\n"
  15816. "\n\\pagecolor{bg}\n"
  15817. "\n{\\color{fg}\n"
  15818. string
  15819. "\n}\n"
  15820. "\n\\end{document}\n" )
  15821. (require 'org-latex)
  15822. (org-export-latex-fix-inputenc))
  15823. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  15824. (condition-case nil
  15825. (progn
  15826. (cd tmpdir)
  15827. (setq cmds org-latex-to-pdf-process)
  15828. (while cmds
  15829. (setq latex-frags-cmds (pop cmds))
  15830. (if (listp latex-frags-cmds)
  15831. (setq cmds nil)
  15832. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  15833. (while latex-frags-cmds
  15834. (setq cmd (pop latex-frags-cmds))
  15835. (while (string-match "%b" cmd)
  15836. (setq cmd (replace-match
  15837. (save-match-data
  15838. (shell-quote-argument texfile))
  15839. t t cmd)))
  15840. (while (string-match "%f" cmd)
  15841. (setq cmd (replace-match
  15842. (save-match-data
  15843. (shell-quote-argument (file-name-nondirectory texfile)))
  15844. t t cmd)))
  15845. (while (string-match "%o" cmd)
  15846. (setq cmd (replace-match
  15847. (save-match-data
  15848. (shell-quote-argument (file-name-directory texfile)))
  15849. t t cmd)))
  15850. (shell-command cmd)))
  15851. (error nil))
  15852. (cd dir))
  15853. (if (not (file-exists-p pdffile))
  15854. (progn (message "Failed to create pdf file from %s" texfile) nil)
  15855. (condition-case nil
  15856. (if (featurep 'xemacs)
  15857. (call-process "convert" nil nil nil
  15858. "-density" "96"
  15859. "-trim"
  15860. "-antialias"
  15861. pdffile
  15862. "-quality" "100"
  15863. ;; "-sharpen" "0x1.0"
  15864. pngfile)
  15865. (call-process "convert" nil nil nil
  15866. "-density" dpi
  15867. "-trim"
  15868. "-antialias"
  15869. pdffile
  15870. "-quality" "100"
  15871. ; "-sharpen" "0x1.0"
  15872. pngfile))
  15873. (error nil))
  15874. (if (not (file-exists-p pngfile))
  15875. (if org-format-latex-signal-error
  15876. (error "Failed to create png file from %s" texfile)
  15877. (message "Failed to create png file from %s" texfile)
  15878. nil)
  15879. ;; Use the requested file name and clean up
  15880. (copy-file pngfile tofile 'replace)
  15881. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  15882. (delete-file (concat texfilebase e)))
  15883. pngfile))))
  15884. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15885. "Fill a LaTeX header template TPL.
  15886. In the template, the following place holders will be recognized:
  15887. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15888. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15889. [PACKAGES] \\usepackage statements for PKG
  15890. [NO-PACKAGES] do not include PKG
  15891. [EXTRA] the string EXTRA
  15892. [NO-EXTRA] do not include EXTRA
  15893. For backward compatibility, if both the positive and the negative place
  15894. holder is missing, the positive one (without the \"NO-\") will be
  15895. assumed to be present at the end of the template.
  15896. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15897. EXTRA is a string.
  15898. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15899. (let (rpl (end ""))
  15900. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15901. (setq rpl (if (or (match-end 1) (not def-pkg))
  15902. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15903. tpl (replace-match rpl t t tpl))
  15904. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15905. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15906. (setq rpl (if (or (match-end 1) (not pkg))
  15907. "" (org-latex-packages-to-string pkg snippets-p t))
  15908. tpl (replace-match rpl t t tpl))
  15909. (if pkg (setq end
  15910. (concat end "\n"
  15911. (org-latex-packages-to-string pkg snippets-p)))))
  15912. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15913. (setq rpl (if (or (match-end 1) (not extra))
  15914. "" (concat extra "\n"))
  15915. tpl (replace-match rpl t t tpl))
  15916. (if (and extra (string-match "\\S-" extra))
  15917. (setq end (concat end "\n" extra))))
  15918. (if (string-match "\\S-" end)
  15919. (concat tpl "\n" end)
  15920. tpl)))
  15921. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15922. "Turn an alist of packages into a string with the \\usepackage macros."
  15923. (setq pkg (mapconcat (lambda(p)
  15924. (cond
  15925. ((stringp p) p)
  15926. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15927. (format "%% Package %s omitted" (cadr p)))
  15928. ((equal "" (car p))
  15929. (format "\\usepackage{%s}" (cadr p)))
  15930. (t
  15931. (format "\\usepackage[%s]{%s}"
  15932. (car p) (cadr p)))))
  15933. pkg
  15934. "\n"))
  15935. (if newline (concat pkg "\n") pkg))
  15936. (defun org-dvipng-color (attr)
  15937. "Return a RGB color specification for dvipng."
  15938. (apply 'format "rgb %s %s %s"
  15939. (mapcar 'org-normalize-color
  15940. (if (featurep 'xemacs)
  15941. (color-rgb-components
  15942. (face-property 'default
  15943. (cond ((eq attr :foreground) 'foreground)
  15944. ((eq attr :background) 'background))))
  15945. (color-values (face-attribute 'default attr nil))))))
  15946. (defun org-latex-color (attr)
  15947. "Return a RGB color for the LaTeX color package."
  15948. (apply 'format "%s,%s,%s"
  15949. (mapcar 'org-normalize-color
  15950. (if (featurep 'xemacs)
  15951. (color-rgb-components
  15952. (face-property 'default
  15953. (cond ((eq attr :foreground) 'foreground)
  15954. ((eq attr :background) 'background))))
  15955. (color-values (face-attribute 'default attr nil))))))
  15956. (defun org-latex-color-format (color-name)
  15957. "Convert COLOR-NAME to a RGB color value."
  15958. (apply 'format "%s,%s,%s"
  15959. (mapcar 'org-normalize-color
  15960. (color-values color-name))))
  15961. (defun org-normalize-color (value)
  15962. "Return string to be used as color value for an RGB component."
  15963. (format "%g" (/ value 65535.0)))
  15964. ;; Image display
  15965. (defvar org-inline-image-overlays nil)
  15966. (make-variable-buffer-local 'org-inline-image-overlays)
  15967. (defun org-toggle-inline-images (&optional include-linked)
  15968. "Toggle the display of inline images.
  15969. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15970. (interactive "P")
  15971. (if org-inline-image-overlays
  15972. (progn
  15973. (org-remove-inline-images)
  15974. (message "Inline image display turned off"))
  15975. (org-display-inline-images include-linked)
  15976. (if org-inline-image-overlays
  15977. (message "%d images displayed inline"
  15978. (length org-inline-image-overlays))
  15979. (message "No images to display inline"))))
  15980. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15981. "Display inline images.
  15982. Normally only links without a description part are inlined, because this
  15983. is how it will work for export. When INCLUDE-LINKED is set, also links
  15984. with a description part will be inlined. This can be nice for a quick
  15985. look at those images, but it does not reflect what exported files will look
  15986. like.
  15987. When REFRESH is set, refresh existing images between BEG and END.
  15988. This will create new image displays only if necessary.
  15989. BEG and END default to the buffer boundaries."
  15990. (interactive "P")
  15991. (unless refresh
  15992. (org-remove-inline-images)
  15993. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15994. (save-excursion
  15995. (save-restriction
  15996. (widen)
  15997. (setq beg (or beg (point-min)) end (or end (point-max)))
  15998. (goto-char beg)
  15999. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16000. (substring (org-image-file-name-regexp) 0 -2)
  16001. "\\)\\]" (if include-linked "" "\\]")))
  16002. old file ov img)
  16003. (while (re-search-forward re end t)
  16004. (setq old (get-char-property-and-overlay (match-beginning 1)
  16005. 'org-image-overlay))
  16006. (setq file (expand-file-name
  16007. (concat (or (match-string 3) "") (match-string 4))))
  16008. (when (file-exists-p file)
  16009. (if (and (car-safe old) refresh)
  16010. (image-refresh (overlay-get (cdr old) 'display))
  16011. (setq img (save-match-data (create-image file)))
  16012. (when img
  16013. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16014. (overlay-put ov 'display img)
  16015. (overlay-put ov 'face 'default)
  16016. (overlay-put ov 'org-image-overlay t)
  16017. (overlay-put ov 'modification-hooks
  16018. (list 'org-display-inline-modification-hook))
  16019. (push ov org-inline-image-overlays)))))))))
  16020. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  16021. "Remove inline-display overlay if a corresponding region is modified."
  16022. (let ((inhibit-modification-hooks t))
  16023. (when (and ov after)
  16024. (delete ov org-inline-image-overlays)
  16025. (delete-overlay ov))))
  16026. (defun org-remove-inline-images ()
  16027. "Remove inline display of images."
  16028. (interactive)
  16029. (mapc 'delete-overlay org-inline-image-overlays)
  16030. (setq org-inline-image-overlays nil))
  16031. ;;;; Key bindings
  16032. ;; Outline functions from `outline-mode-prefix-map'
  16033. ;; that can be remapped in Org:
  16034. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16035. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16036. (define-key org-mode-map [remap outline-forward-same-level]
  16037. 'org-forward-same-level)
  16038. (define-key org-mode-map [remap outline-backward-same-level]
  16039. 'org-backward-same-level)
  16040. (define-key org-mode-map [remap show-branches]
  16041. 'org-kill-note-or-show-branches)
  16042. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16043. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16044. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16045. ;; Outline functions from `outline-mode-prefix-map'
  16046. ;; that can not be remapped in Org:
  16047. ;; - the column "key binding" shows whether the Outline function is still
  16048. ;; available in Org mode on the same key that it has been bound to in
  16049. ;; Outline mode:
  16050. ;; - "overridden": key used for a different functionality in Org mode
  16051. ;; - else: key still bound to the same Outline function in Org mode
  16052. ;; | Outline function | key binding | Org replacement |
  16053. ;; |------------------------------------+-------------+-----------------------|
  16054. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16055. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16056. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16057. ;; | `hide-subtree' | overridden | visibility cycling |
  16058. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16059. ;; | `hide-body' | overridden | no replacement |
  16060. ;; | `show-all' | overridden | no replacement |
  16061. ;; | `hide-entry' | overridden | visibility cycling |
  16062. ;; | `show-entry' | overridden | no replacement |
  16063. ;; | `hide-leaves' | overridden | no replacement |
  16064. ;; | `hide-sublevels' | overridden | no replacement |
  16065. ;; | `hide-other' | overridden | no replacement |
  16066. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  16067. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16068. ;; Make `C-c C-x' a prefix key
  16069. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16070. ;; TAB key with modifiers
  16071. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16072. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16073. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16074. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16075. ;; The following line is necessary under Suse GNU/Linux
  16076. (unless (featurep 'xemacs)
  16077. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16078. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16079. (define-key org-mode-map [backtab] 'org-shifttab)
  16080. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16081. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16082. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16083. ;; Cursor keys with modifiers
  16084. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16085. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16086. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16087. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16088. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16089. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16090. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16091. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16092. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16093. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16094. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16095. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16096. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16097. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16098. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16099. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16100. ;; Babel keys
  16101. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16102. (mapc (lambda (pair)
  16103. (define-key org-babel-map (car pair) (cdr pair)))
  16104. org-babel-key-bindings)
  16105. ;;; Extra keys for tty access.
  16106. ;; We only set them when really needed because otherwise the
  16107. ;; menus don't show the simple keys
  16108. (when (or org-use-extra-keys
  16109. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16110. (not window-system))
  16111. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16112. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16113. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16114. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16115. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16116. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16117. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16118. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16119. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16120. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16121. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16122. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16123. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16124. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16125. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16126. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16127. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16128. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16129. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16130. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16131. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16132. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16133. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16134. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16135. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16136. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16137. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16138. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16139. ;; All the other keys
  16140. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16141. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16142. (if (boundp 'narrow-map)
  16143. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16144. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16145. (if (boundp 'narrow-map)
  16146. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16147. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16148. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  16149. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  16150. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16151. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16152. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16153. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16154. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16155. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16156. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16157. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16158. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16159. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16160. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16161. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16162. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16163. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16164. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16165. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16166. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16167. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16168. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16169. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16170. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16171. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16172. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16173. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16174. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16175. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16176. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16177. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16178. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16179. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16180. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16181. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16182. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16183. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16184. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16185. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16186. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16187. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16188. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16189. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16190. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16191. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16192. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16193. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16194. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16195. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16196. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16197. (org-defkey org-mode-map "\C-m" 'org-return)
  16198. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16199. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16200. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16201. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16202. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16203. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16204. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16205. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16206. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16207. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16208. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16209. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16210. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16211. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16212. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16213. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16214. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16215. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16216. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16217. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16218. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16219. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16220. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16221. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16222. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16223. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16224. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16225. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16226. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16227. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16228. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16229. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  16230. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16231. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16232. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16233. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16234. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16235. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16236. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16237. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16238. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16239. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16240. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16241. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16242. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16243. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16244. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16245. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16246. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16247. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16248. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16249. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16250. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16251. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16252. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16253. (when (featurep 'xemacs)
  16254. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16255. (defconst org-speed-commands-default
  16256. '(
  16257. ("Outline Navigation")
  16258. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16259. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16260. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16261. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16262. ("u" . (org-speed-move-safe 'outline-up-heading))
  16263. ("j" . org-goto)
  16264. ("g" . (org-refile t))
  16265. ("Outline Visibility")
  16266. ("c" . org-cycle)
  16267. ("C" . org-shifttab)
  16268. (" " . org-display-outline-path)
  16269. ("Outline Structure Editing")
  16270. ("U" . org-shiftmetaup)
  16271. ("D" . org-shiftmetadown)
  16272. ("r" . org-metaright)
  16273. ("l" . org-metaleft)
  16274. ("R" . org-shiftmetaright)
  16275. ("L" . org-shiftmetaleft)
  16276. ("i" . (progn (forward-char 1) (call-interactively
  16277. 'org-insert-heading-respect-content)))
  16278. ("^" . org-sort)
  16279. ("w" . org-refile)
  16280. ("a" . org-archive-subtree-default-with-confirmation)
  16281. ("." . org-mark-subtree)
  16282. ("Clock Commands")
  16283. ("I" . org-clock-in)
  16284. ("O" . org-clock-out)
  16285. ("Meta Data Editing")
  16286. ("t" . org-todo)
  16287. ("0" . (org-priority ?\ ))
  16288. ("1" . (org-priority ?A))
  16289. ("2" . (org-priority ?B))
  16290. ("3" . (org-priority ?C))
  16291. (";" . org-set-tags-command)
  16292. ("e" . org-set-effort)
  16293. ("Agenda Views etc")
  16294. ("v" . org-agenda)
  16295. ("/" . org-sparse-tree)
  16296. ("Misc")
  16297. ("o" . org-open-at-point)
  16298. ("?" . org-speed-command-help)
  16299. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16300. (">" . (org-agenda-remove-restriction-lock))
  16301. )
  16302. "The default speed commands.")
  16303. (defun org-print-speed-command (e)
  16304. (if (> (length (car e)) 1)
  16305. (progn
  16306. (princ "\n")
  16307. (princ (car e))
  16308. (princ "\n")
  16309. (princ (make-string (length (car e)) ?-))
  16310. (princ "\n"))
  16311. (princ (car e))
  16312. (princ " ")
  16313. (if (symbolp (cdr e))
  16314. (princ (symbol-name (cdr e)))
  16315. (prin1 (cdr e)))
  16316. (princ "\n")))
  16317. (defun org-speed-command-help ()
  16318. "Show the available speed commands."
  16319. (interactive)
  16320. (if (not org-use-speed-commands)
  16321. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16322. (with-output-to-temp-buffer "*Help*"
  16323. (princ "User-defined Speed commands\n===========================\n")
  16324. (mapc 'org-print-speed-command org-speed-commands-user)
  16325. (princ "\n")
  16326. (princ "Built-in Speed commands\n=======================\n")
  16327. (mapc 'org-print-speed-command org-speed-commands-default))
  16328. (with-current-buffer "*Help*"
  16329. (setq truncate-lines t))))
  16330. (defun org-speed-move-safe (cmd)
  16331. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16332. If not, return to the original position and throw an error."
  16333. (interactive)
  16334. (let ((pos (point)))
  16335. (call-interactively cmd)
  16336. (unless (and (bolp) (org-at-heading-p))
  16337. (goto-char pos)
  16338. (error "Boundary reached while executing %s" cmd))))
  16339. (defvar org-self-insert-command-undo-counter 0)
  16340. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16341. (defvar org-speed-command nil)
  16342. (defun org-speed-command-default-hook (keys)
  16343. "Hook for activating single-letter speed commands.
  16344. `org-speed-commands-default' specifies a minimal command set.
  16345. Use `org-speed-commands-user' for further customization."
  16346. (when (or (and (bolp) (looking-at org-outline-regexp))
  16347. (and (functionp org-use-speed-commands)
  16348. (funcall org-use-speed-commands)))
  16349. (cdr (assoc keys (append org-speed-commands-user
  16350. org-speed-commands-default)))))
  16351. (defun org-babel-speed-command-hook (keys)
  16352. "Hook for activating single-letter code block commands."
  16353. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16354. (cdr (assoc keys org-babel-key-bindings))))
  16355. (defcustom org-speed-command-hook
  16356. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16357. "Hook for activating speed commands at strategic locations.
  16358. Hook functions are called in sequence until a valid handler is
  16359. found.
  16360. Each hook takes a single argument, a user-pressed command key
  16361. which is also a `self-insert-command' from the global map.
  16362. Within the hook, examine the cursor position and the command key
  16363. and return nil or a valid handler as appropriate. Handler could
  16364. be one of an interactive command, a function, or a form.
  16365. Set `org-use-speed-commands' to non-nil value to enable this
  16366. hook. The default setting is `org-speed-command-default-hook'."
  16367. :group 'org-structure
  16368. :version "24.1"
  16369. :type 'hook)
  16370. (defun org-self-insert-command (N)
  16371. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16372. If the cursor is in a table looking at whitespace, the whitespace is
  16373. overwritten, and the table is not marked as requiring realignment."
  16374. (interactive "p")
  16375. (org-check-before-invisible-edit 'insert)
  16376. (cond
  16377. ((and org-use-speed-commands
  16378. (setq org-speed-command
  16379. (run-hook-with-args-until-success
  16380. 'org-speed-command-hook (this-command-keys))))
  16381. (cond
  16382. ((commandp org-speed-command)
  16383. (setq this-command org-speed-command)
  16384. (call-interactively org-speed-command))
  16385. ((functionp org-speed-command)
  16386. (funcall org-speed-command))
  16387. ((and org-speed-command (listp org-speed-command))
  16388. (eval org-speed-command))
  16389. (t (let (org-use-speed-commands)
  16390. (call-interactively 'org-self-insert-command)))))
  16391. ((and
  16392. (org-table-p)
  16393. (progn
  16394. ;; check if we blank the field, and if that triggers align
  16395. (and (featurep 'org-table) org-table-auto-blank-field
  16396. (member last-command
  16397. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16398. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16399. ;; got extra space, this field does not determine column width
  16400. (let (org-table-may-need-update) (org-table-blank-field))
  16401. ;; no extra space, this field may determine column width
  16402. (org-table-blank-field)))
  16403. t)
  16404. (eq N 1)
  16405. (looking-at "[^|\n]* |"))
  16406. (let (org-table-may-need-update)
  16407. (goto-char (1- (match-end 0)))
  16408. (backward-delete-char 1)
  16409. (goto-char (match-beginning 0))
  16410. (self-insert-command N)))
  16411. (t
  16412. (setq org-table-may-need-update t)
  16413. (self-insert-command N)
  16414. (org-fix-tags-on-the-fly)
  16415. (if org-self-insert-cluster-for-undo
  16416. (if (not (eq last-command 'org-self-insert-command))
  16417. (setq org-self-insert-command-undo-counter 1)
  16418. (if (>= org-self-insert-command-undo-counter 20)
  16419. (setq org-self-insert-command-undo-counter 1)
  16420. (and (> org-self-insert-command-undo-counter 0)
  16421. buffer-undo-list (listp buffer-undo-list)
  16422. (not (cadr buffer-undo-list)) ; remove nil entry
  16423. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16424. (setq org-self-insert-command-undo-counter
  16425. (1+ org-self-insert-command-undo-counter))))))))
  16426. (defun org-check-before-invisible-edit (kind)
  16427. "Check is editing if kind KIND would be dangerous with invisible text around.
  16428. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16429. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16430. (if (and org-catch-invisible-edits
  16431. (or (not (boundp 'visible-mode)) (not visible-mode))
  16432. (or (get-char-property (point) 'invisible)
  16433. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16434. ;; OK, we need to take a closer look
  16435. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16436. (invisible-before-point (if (bobp) nil (get-char-property
  16437. (1- (point)) 'invisible)))
  16438. (border-and-ok-direction
  16439. (or
  16440. ;; Check if we are acting predictably before invisible text
  16441. (and invisible-at-point (not invisible-before-point)
  16442. (memq kind '(insert delete-backward)))
  16443. ;; Check if we are acting predictably after invisible text
  16444. ;; This works not well, and I have turned it off. It seems
  16445. ;; better to always show and stop after invisible text.
  16446. ;; (and (not invisible-at-point) invisible-before-point
  16447. ;; (memq kind '(insert delete)))
  16448. )))
  16449. (when (or (memq invisible-at-point '(outline org-hide-block))
  16450. (memq invisible-before-point '(outline org-hide-block)))
  16451. (if (eq org-catch-invisible-edits 'error)
  16452. (error "Editing in invisible areas is prohibited - make visible first"))
  16453. ;; Make the area visible
  16454. (save-excursion
  16455. (if invisible-before-point
  16456. (goto-char (previous-single-char-property-change
  16457. (point) 'invisible)))
  16458. (org-cycle))
  16459. (cond
  16460. ((eq org-catch-invisible-edits 'show)
  16461. ;; That's it, we do the edit after showing
  16462. (message
  16463. "Unfolding invisible region around point before editing")
  16464. (sit-for 1))
  16465. ((and (eq org-catch-invisible-edits 'smart)
  16466. border-and-ok-direction)
  16467. (message "Unfolding invisible region around point before editing"))
  16468. (t
  16469. ;; Don't do the edit, make the user repeat it in full visibility
  16470. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16471. (defun org-fix-tags-on-the-fly ()
  16472. (when (and (equal (char-after (point-at-bol)) ?*)
  16473. (org-at-heading-p))
  16474. (org-align-tags-here org-tags-column)))
  16475. (defun org-delete-backward-char (N)
  16476. "Like `delete-backward-char', insert whitespace at field end in tables.
  16477. When deleting backwards, in tables this function will insert whitespace in
  16478. front of the next \"|\" separator, to keep the table aligned. The table will
  16479. still be marked for re-alignment if the field did fill the entire column,
  16480. because, in this case the deletion might narrow the column."
  16481. (interactive "p")
  16482. (org-check-before-invisible-edit 'delete-backward)
  16483. (if (and (org-table-p)
  16484. (eq N 1)
  16485. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16486. (looking-at ".*?|"))
  16487. (let ((pos (point))
  16488. (noalign (looking-at "[^|\n\r]* |"))
  16489. (c org-table-may-need-update))
  16490. (backward-delete-char N)
  16491. (if (not overwrite-mode)
  16492. (progn
  16493. (skip-chars-forward "^|")
  16494. (insert " ")
  16495. (goto-char (1- pos))))
  16496. ;; noalign: if there were two spaces at the end, this field
  16497. ;; does not determine the width of the column.
  16498. (if noalign (setq org-table-may-need-update c)))
  16499. (backward-delete-char N)
  16500. (org-fix-tags-on-the-fly)))
  16501. (defun org-delete-char (N)
  16502. "Like `delete-char', but insert whitespace at field end in tables.
  16503. When deleting characters, in tables this function will insert whitespace in
  16504. front of the next \"|\" separator, to keep the table aligned. The table will
  16505. still be marked for re-alignment if the field did fill the entire column,
  16506. because, in this case the deletion might narrow the column."
  16507. (interactive "p")
  16508. (org-check-before-invisible-edit 'delete)
  16509. (if (and (org-table-p)
  16510. (not (bolp))
  16511. (not (= (char-after) ?|))
  16512. (eq N 1))
  16513. (if (looking-at ".*?|")
  16514. (let ((pos (point))
  16515. (noalign (looking-at "[^|\n\r]* |"))
  16516. (c org-table-may-need-update))
  16517. (replace-match (concat
  16518. (substring (match-string 0) 1 -1)
  16519. " |"))
  16520. (goto-char pos)
  16521. ;; noalign: if there were two spaces at the end, this field
  16522. ;; does not determine the width of the column.
  16523. (if noalign (setq org-table-may-need-update c)))
  16524. (delete-char N))
  16525. (delete-char N)
  16526. (org-fix-tags-on-the-fly)))
  16527. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16528. (put 'org-self-insert-command 'delete-selection t)
  16529. (put 'orgtbl-self-insert-command 'delete-selection t)
  16530. (put 'org-delete-char 'delete-selection 'supersede)
  16531. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16532. (put 'org-yank 'delete-selection 'yank)
  16533. ;; Make `flyspell-mode' delay after some commands
  16534. (put 'org-self-insert-command 'flyspell-delayed t)
  16535. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16536. (put 'org-delete-char 'flyspell-delayed t)
  16537. (put 'org-delete-backward-char 'flyspell-delayed t)
  16538. ;; Make pabbrev-mode expand after org-mode commands
  16539. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16540. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16541. ;; How to do this: Measure non-white length of current string
  16542. ;; If equal to column width, we should realign.
  16543. (defun org-remap (map &rest commands)
  16544. "In MAP, remap the functions given in COMMANDS.
  16545. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16546. (let (new old)
  16547. (while commands
  16548. (setq old (pop commands) new (pop commands))
  16549. (if (fboundp 'command-remapping)
  16550. (org-defkey map (vector 'remap old) new)
  16551. (substitute-key-definition old new map global-map)))))
  16552. (when (eq org-enable-table-editor 'optimized)
  16553. ;; If the user wants maximum table support, we need to hijack
  16554. ;; some standard editing functions
  16555. (org-remap org-mode-map
  16556. 'self-insert-command 'org-self-insert-command
  16557. 'delete-char 'org-delete-char
  16558. 'delete-backward-char 'org-delete-backward-char)
  16559. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16560. (defvar org-ctrl-c-ctrl-c-hook nil
  16561. "Hook for functions attaching themselves to `C-c C-c'.
  16562. This can be used to add additional functionality to the C-c C-c
  16563. key which executes context-dependent commands. This hook is run
  16564. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16565. run after the last test.
  16566. Each function will be called with no arguments. The function
  16567. must check if the context is appropriate for it to act. If yes,
  16568. it should do its thing and then return a non-nil value. If the
  16569. context is wrong, just do nothing and return nil.")
  16570. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16571. "Hook for functions attaching themselves to `C-c C-c'.
  16572. This can be used to add additional functionality to the C-c C-c
  16573. key which executes context-dependent commands. This hook is run
  16574. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16575. before the first test.
  16576. Each function will be called with no arguments. The function
  16577. must check if the context is appropriate for it to act. If yes,
  16578. it should do its thing and then return a non-nil value. If the
  16579. context is wrong, just do nothing and return nil.")
  16580. (defvar org-tab-first-hook nil
  16581. "Hook for functions to attach themselves to TAB.
  16582. See `org-ctrl-c-ctrl-c-hook' for more information.
  16583. This hook runs as the first action when TAB is pressed, even before
  16584. `org-cycle' messes around with the `outline-regexp' to cater for
  16585. inline tasks and plain list item folding.
  16586. If any function in this hook returns t, any other actions that
  16587. would have been caused by TAB (such as table field motion or visibility
  16588. cycling) will not occur.")
  16589. (defvar org-tab-after-check-for-table-hook nil
  16590. "Hook for functions to attach themselves to TAB.
  16591. See `org-ctrl-c-ctrl-c-hook' for more information.
  16592. This hook runs after it has been established that the cursor is not in a
  16593. table, but before checking if the cursor is in a headline or if global cycling
  16594. should be done.
  16595. If any function in this hook returns t, not other actions like visibility
  16596. cycling will be done.")
  16597. (defvar org-tab-after-check-for-cycling-hook nil
  16598. "Hook for functions to attach themselves to TAB.
  16599. See `org-ctrl-c-ctrl-c-hook' for more information.
  16600. This hook runs after it has been established that not table field motion and
  16601. not visibility should be done because of current context. This is probably
  16602. the place where a package like yasnippets can hook in.")
  16603. (defvar org-tab-before-tab-emulation-hook nil
  16604. "Hook for functions to attach themselves to TAB.
  16605. See `org-ctrl-c-ctrl-c-hook' for more information.
  16606. This hook runs after every other options for TAB have been exhausted, but
  16607. before indentation and \t insertion takes place.")
  16608. (defvar org-metaleft-hook nil
  16609. "Hook for functions attaching themselves to `M-left'.
  16610. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16611. (defvar org-metaright-hook nil
  16612. "Hook for functions attaching themselves to `M-right'.
  16613. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16614. (defvar org-metaup-hook nil
  16615. "Hook for functions attaching themselves to `M-up'.
  16616. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16617. (defvar org-metadown-hook nil
  16618. "Hook for functions attaching themselves to `M-down'.
  16619. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16620. (defvar org-shiftmetaleft-hook nil
  16621. "Hook for functions attaching themselves to `M-S-left'.
  16622. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16623. (defvar org-shiftmetaright-hook nil
  16624. "Hook for functions attaching themselves to `M-S-right'.
  16625. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16626. (defvar org-shiftmetaup-hook nil
  16627. "Hook for functions attaching themselves to `M-S-up'.
  16628. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16629. (defvar org-shiftmetadown-hook nil
  16630. "Hook for functions attaching themselves to `M-S-down'.
  16631. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16632. (defvar org-metareturn-hook nil
  16633. "Hook for functions attaching themselves to `M-RET'.
  16634. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16635. (defvar org-shiftup-hook nil
  16636. "Hook for functions attaching themselves to `S-up'.
  16637. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16638. (defvar org-shiftup-final-hook nil
  16639. "Hook for functions attaching themselves to `S-up'.
  16640. This one runs after all other options except shift-select have been excluded.
  16641. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16642. (defvar org-shiftdown-hook nil
  16643. "Hook for functions attaching themselves to `S-down'.
  16644. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16645. (defvar org-shiftdown-final-hook nil
  16646. "Hook for functions attaching themselves to `S-down'.
  16647. This one runs after all other options except shift-select have been excluded.
  16648. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16649. (defvar org-shiftleft-hook nil
  16650. "Hook for functions attaching themselves to `S-left'.
  16651. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16652. (defvar org-shiftleft-final-hook nil
  16653. "Hook for functions attaching themselves to `S-left'.
  16654. This one runs after all other options except shift-select have been excluded.
  16655. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16656. (defvar org-shiftright-hook nil
  16657. "Hook for functions attaching themselves to `S-right'.
  16658. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16659. (defvar org-shiftright-final-hook nil
  16660. "Hook for functions attaching themselves to `S-right'.
  16661. This one runs after all other options except shift-select have been excluded.
  16662. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16663. (defun org-modifier-cursor-error ()
  16664. "Throw an error, a modified cursor command was applied in wrong context."
  16665. (error "This command is active in special context like tables, headlines or items"))
  16666. (defun org-shiftselect-error ()
  16667. "Throw an error because Shift-Cursor command was applied in wrong context."
  16668. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16669. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16670. (error "This command works only in special context like headlines or timestamps")))
  16671. (defun org-call-for-shift-select (cmd)
  16672. (let ((this-command-keys-shift-translated t))
  16673. (call-interactively cmd)))
  16674. (defun org-shifttab (&optional arg)
  16675. "Global visibility cycling or move to previous table field.
  16676. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16677. on context.
  16678. See the individual commands for more information."
  16679. (interactive "P")
  16680. (cond
  16681. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16682. ((integerp arg)
  16683. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16684. (message "Content view to level: %d" arg)
  16685. (org-content (prefix-numeric-value arg2))
  16686. (setq org-cycle-global-status 'overview)))
  16687. (t (call-interactively 'org-global-cycle))))
  16688. (defun org-shiftmetaleft ()
  16689. "Promote subtree or delete table column.
  16690. Calls `org-promote-subtree', `org-outdent-item-tree', or
  16691. `org-table-delete-column', depending on context. See the
  16692. individual commands for more information."
  16693. (interactive)
  16694. (cond
  16695. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16696. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16697. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16698. ((if (not (org-region-active-p)) (org-at-item-p)
  16699. (save-excursion (goto-char (region-beginning))
  16700. (org-at-item-p)))
  16701. (call-interactively 'org-outdent-item-tree))
  16702. (t (org-modifier-cursor-error))))
  16703. (defun org-shiftmetaright ()
  16704. "Demote subtree or insert table column.
  16705. Calls `org-demote-subtree', `org-indent-item-tree', or
  16706. `org-table-insert-column', depending on context. See the
  16707. individual commands for more information."
  16708. (interactive)
  16709. (cond
  16710. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16711. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16712. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16713. ((if (not (org-region-active-p)) (org-at-item-p)
  16714. (save-excursion (goto-char (region-beginning))
  16715. (org-at-item-p)))
  16716. (call-interactively 'org-indent-item-tree))
  16717. (t (org-modifier-cursor-error))))
  16718. (defun org-shiftmetaup (&optional arg)
  16719. "Move subtree up or kill table row.
  16720. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16721. `org-move-item-up' depending on context. See the individual commands
  16722. for more information."
  16723. (interactive "P")
  16724. (cond
  16725. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16726. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16727. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16728. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16729. (t (org-modifier-cursor-error))))
  16730. (defun org-shiftmetadown (&optional arg)
  16731. "Move subtree down or insert table row.
  16732. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16733. `org-move-item-down', depending on context. See the individual
  16734. commands for more information."
  16735. (interactive "P")
  16736. (cond
  16737. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16738. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16739. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16740. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16741. (t (org-modifier-cursor-error))))
  16742. (defsubst org-hidden-tree-error ()
  16743. (error
  16744. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16745. (defun org-metaleft (&optional arg)
  16746. "Promote heading or move table column to left.
  16747. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16748. With no specific context, calls the Emacs default `backward-word'.
  16749. See the individual commands for more information."
  16750. (interactive "P")
  16751. (cond
  16752. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16753. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16754. ((org-with-limited-levels
  16755. (or (org-at-heading-p)
  16756. (and (org-region-active-p)
  16757. (save-excursion
  16758. (goto-char (region-beginning))
  16759. (org-at-heading-p)))))
  16760. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16761. (call-interactively 'org-do-promote))
  16762. ;; At an inline task.
  16763. ((org-at-heading-p)
  16764. (call-interactively 'org-inlinetask-promote))
  16765. ((or (org-at-item-p)
  16766. (and (org-region-active-p)
  16767. (save-excursion
  16768. (goto-char (region-beginning))
  16769. (org-at-item-p))))
  16770. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16771. (call-interactively 'org-outdent-item))
  16772. (t (call-interactively 'backward-word))))
  16773. (defun org-metaright (&optional arg)
  16774. "Demote a subtree, a list item or move table column to right.
  16775. In front of a drawer or a block keyword, indent it correctly.
  16776. With no specific context, calls the Emacs default `forward-word'.
  16777. See the individual commands for more information."
  16778. (interactive "P")
  16779. (cond
  16780. ((run-hook-with-args-until-success 'org-metaright-hook))
  16781. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16782. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16783. ((org-at-block-p) (call-interactively 'org-indent-block))
  16784. ((org-with-limited-levels
  16785. (or (org-at-heading-p)
  16786. (and (org-region-active-p)
  16787. (save-excursion
  16788. (goto-char (region-beginning))
  16789. (org-at-heading-p)))))
  16790. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16791. (call-interactively 'org-do-demote))
  16792. ;; At an inline task.
  16793. ((org-at-heading-p)
  16794. (call-interactively 'org-inlinetask-demote))
  16795. ((or (org-at-item-p)
  16796. (and (org-region-active-p)
  16797. (save-excursion
  16798. (goto-char (region-beginning))
  16799. (org-at-item-p))))
  16800. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16801. (call-interactively 'org-indent-item))
  16802. (t (call-interactively 'forward-word))))
  16803. (defun org-check-for-hidden (what)
  16804. "Check if there are hidden headlines/items in the current visual line.
  16805. WHAT can be either `headlines' or `items'. If the current line is
  16806. an outline or item heading and it has a folded subtree below it,
  16807. this function returns t, nil otherwise."
  16808. (let ((re (cond
  16809. ((eq what 'headlines) org-outline-regexp-bol)
  16810. ((eq what 'items) (org-item-beginning-re))
  16811. (t (error "This should not happen"))))
  16812. beg end)
  16813. (save-excursion
  16814. (catch 'exit
  16815. (unless (org-region-active-p)
  16816. (setq beg (point-at-bol))
  16817. (beginning-of-line 2)
  16818. (while (and (not (eobp)) ;; this is like `next-line'
  16819. (get-char-property (1- (point)) 'invisible))
  16820. (beginning-of-line 2))
  16821. (setq end (point))
  16822. (goto-char beg)
  16823. (goto-char (point-at-eol))
  16824. (setq end (max end (point)))
  16825. (while (re-search-forward re end t)
  16826. (if (get-char-property (match-beginning 0) 'invisible)
  16827. (throw 'exit t))))
  16828. nil))))
  16829. (defun org-metaup (&optional arg)
  16830. "Move subtree up or move table row up.
  16831. Calls `org-move-subtree-up' or `org-table-move-row' or
  16832. `org-move-item-up', depending on context. See the individual commands
  16833. for more information."
  16834. (interactive "P")
  16835. (cond
  16836. ((run-hook-with-args-until-success 'org-metaup-hook))
  16837. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16838. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16839. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16840. (t (transpose-lines 1) (beginning-of-line -1))))
  16841. (defun org-metadown (&optional arg)
  16842. "Move subtree down or move table row down.
  16843. Calls `org-move-subtree-down' or `org-table-move-row' or
  16844. `org-move-item-down', depending on context. See the individual
  16845. commands for more information."
  16846. (interactive "P")
  16847. (cond
  16848. ((run-hook-with-args-until-success 'org-metadown-hook))
  16849. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16850. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16851. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16852. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16853. (defun org-shiftup (&optional arg)
  16854. "Increase item in timestamp or increase priority of current headline.
  16855. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16856. depending on context. See the individual commands for more information."
  16857. (interactive "P")
  16858. (cond
  16859. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16860. ((and org-support-shift-select (org-region-active-p))
  16861. (org-call-for-shift-select 'previous-line))
  16862. ((org-at-timestamp-p t)
  16863. (call-interactively (if org-edit-timestamp-down-means-later
  16864. 'org-timestamp-down 'org-timestamp-up)))
  16865. ((and (not (eq org-support-shift-select 'always))
  16866. org-enable-priority-commands
  16867. (org-at-heading-p))
  16868. (call-interactively 'org-priority-up))
  16869. ((and (not org-support-shift-select) (org-at-item-p))
  16870. (call-interactively 'org-previous-item))
  16871. ((org-clocktable-try-shift 'up arg))
  16872. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16873. (org-support-shift-select
  16874. (org-call-for-shift-select 'previous-line))
  16875. (t (org-shiftselect-error))))
  16876. (defun org-shiftdown (&optional arg)
  16877. "Decrease item in timestamp or decrease priority of current headline.
  16878. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16879. depending on context. See the individual commands for more information."
  16880. (interactive "P")
  16881. (cond
  16882. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16883. ((and org-support-shift-select (org-region-active-p))
  16884. (org-call-for-shift-select 'next-line))
  16885. ((org-at-timestamp-p t)
  16886. (call-interactively (if org-edit-timestamp-down-means-later
  16887. 'org-timestamp-up 'org-timestamp-down)))
  16888. ((and (not (eq org-support-shift-select 'always))
  16889. org-enable-priority-commands
  16890. (org-at-heading-p))
  16891. (call-interactively 'org-priority-down))
  16892. ((and (not org-support-shift-select) (org-at-item-p))
  16893. (call-interactively 'org-next-item))
  16894. ((org-clocktable-try-shift 'down arg))
  16895. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16896. (org-support-shift-select
  16897. (org-call-for-shift-select 'next-line))
  16898. (t (org-shiftselect-error))))
  16899. (defun org-shiftright (&optional arg)
  16900. "Cycle the thing at point or in the current line, depending on context.
  16901. Depending on context, this does one of the following:
  16902. - switch a timestamp at point one day into the future
  16903. - on a headline, switch to the next TODO keyword.
  16904. - on an item, switch entire list to the next bullet type
  16905. - on a property line, switch to the next allowed value
  16906. - on a clocktable definition line, move time block into the future"
  16907. (interactive "P")
  16908. (cond
  16909. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16910. ((and org-support-shift-select (org-region-active-p))
  16911. (org-call-for-shift-select 'forward-char))
  16912. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16913. ((and (not (eq org-support-shift-select 'always))
  16914. (org-at-heading-p))
  16915. (let ((org-inhibit-logging
  16916. (not org-treat-S-cursor-todo-selection-as-state-change))
  16917. (org-inhibit-blocking
  16918. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16919. (org-call-with-arg 'org-todo 'right)))
  16920. ((or (and org-support-shift-select
  16921. (not (eq org-support-shift-select 'always))
  16922. (org-at-item-bullet-p))
  16923. (and (not org-support-shift-select) (org-at-item-p)))
  16924. (org-call-with-arg 'org-cycle-list-bullet nil))
  16925. ((and (not (eq org-support-shift-select 'always))
  16926. (org-at-property-p))
  16927. (call-interactively 'org-property-next-allowed-value))
  16928. ((org-clocktable-try-shift 'right arg))
  16929. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16930. (org-support-shift-select
  16931. (org-call-for-shift-select 'forward-char))
  16932. (t (org-shiftselect-error))))
  16933. (defun org-shiftleft (&optional arg)
  16934. "Cycle the thing at point or in the current line, depending on context.
  16935. Depending on context, this does one of the following:
  16936. - switch a timestamp at point one day into the past
  16937. - on a headline, switch to the previous TODO keyword.
  16938. - on an item, switch entire list to the previous bullet type
  16939. - on a property line, switch to the previous allowed value
  16940. - on a clocktable definition line, move time block into the past"
  16941. (interactive "P")
  16942. (cond
  16943. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16944. ((and org-support-shift-select (org-region-active-p))
  16945. (org-call-for-shift-select 'backward-char))
  16946. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16947. ((and (not (eq org-support-shift-select 'always))
  16948. (org-at-heading-p))
  16949. (let ((org-inhibit-logging
  16950. (not org-treat-S-cursor-todo-selection-as-state-change))
  16951. (org-inhibit-blocking
  16952. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16953. (org-call-with-arg 'org-todo 'left)))
  16954. ((or (and org-support-shift-select
  16955. (not (eq org-support-shift-select 'always))
  16956. (org-at-item-bullet-p))
  16957. (and (not org-support-shift-select) (org-at-item-p)))
  16958. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16959. ((and (not (eq org-support-shift-select 'always))
  16960. (org-at-property-p))
  16961. (call-interactively 'org-property-previous-allowed-value))
  16962. ((org-clocktable-try-shift 'left arg))
  16963. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16964. (org-support-shift-select
  16965. (org-call-for-shift-select 'backward-char))
  16966. (t (org-shiftselect-error))))
  16967. (defun org-shiftcontrolright ()
  16968. "Switch to next TODO set."
  16969. (interactive)
  16970. (cond
  16971. ((and org-support-shift-select (org-region-active-p))
  16972. (org-call-for-shift-select 'forward-word))
  16973. ((and (not (eq org-support-shift-select 'always))
  16974. (org-at-heading-p))
  16975. (org-call-with-arg 'org-todo 'nextset))
  16976. (org-support-shift-select
  16977. (org-call-for-shift-select 'forward-word))
  16978. (t (org-shiftselect-error))))
  16979. (defun org-shiftcontrolleft ()
  16980. "Switch to previous TODO set."
  16981. (interactive)
  16982. (cond
  16983. ((and org-support-shift-select (org-region-active-p))
  16984. (org-call-for-shift-select 'backward-word))
  16985. ((and (not (eq org-support-shift-select 'always))
  16986. (org-at-heading-p))
  16987. (org-call-with-arg 'org-todo 'previousset))
  16988. (org-support-shift-select
  16989. (org-call-for-shift-select 'backward-word))
  16990. (t (org-shiftselect-error))))
  16991. (defun org-shiftcontrolup ()
  16992. "Change timestamps synchronously up in CLOCK log lines."
  16993. (interactive)
  16994. (cond ((and (not org-support-shift-select)
  16995. (org-at-clock-log-p)
  16996. (org-at-timestamp-p t))
  16997. (org-clock-timestamps-up))
  16998. (t (org-shiftselect-error))))
  16999. (defun org-shiftcontroldown ()
  17000. "Change timestamps synchronously down in CLOCK log lines."
  17001. (interactive)
  17002. (cond ((and (not org-support-shift-select)
  17003. (org-at-clock-log-p)
  17004. (org-at-timestamp-p t))
  17005. (org-clock-timestamps-down))
  17006. (t (org-shiftselect-error))))
  17007. (defun org-ctrl-c-ret ()
  17008. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17009. (interactive)
  17010. (cond
  17011. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17012. (t (call-interactively 'org-insert-heading))))
  17013. (defun org-find-visible ()
  17014. (let ((s (point)))
  17015. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17016. (get-char-property s 'invisible)))
  17017. s))
  17018. (defun org-find-invisible ()
  17019. (let ((s (point)))
  17020. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17021. (not (get-char-property s 'invisible))))
  17022. s))
  17023. (defun org-copy-visible (beg end)
  17024. "Copy the visible parts of the region."
  17025. (interactive "r")
  17026. (let (snippets s)
  17027. (save-excursion
  17028. (save-restriction
  17029. (narrow-to-region beg end)
  17030. (setq s (goto-char (point-min)))
  17031. (while (not (= (point) (point-max)))
  17032. (goto-char (org-find-invisible))
  17033. (push (buffer-substring s (point)) snippets)
  17034. (setq s (goto-char (org-find-visible))))))
  17035. (kill-new (apply 'concat (nreverse snippets)))))
  17036. (defun org-copy-special ()
  17037. "Copy region in table or copy current subtree.
  17038. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17039. See the individual commands for more information."
  17040. (interactive)
  17041. (call-interactively
  17042. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17043. (defun org-cut-special ()
  17044. "Cut region in table or cut current subtree.
  17045. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17046. See the individual commands for more information."
  17047. (interactive)
  17048. (call-interactively
  17049. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17050. (defun org-paste-special (arg)
  17051. "Paste rectangular region into table, or past subtree relative to level.
  17052. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17053. See the individual commands for more information."
  17054. (interactive "P")
  17055. (if (org-at-table-p)
  17056. (org-table-paste-rectangle)
  17057. (org-paste-subtree arg)))
  17058. (defun org-edit-special (&optional arg)
  17059. "Call a special editor for the stuff at point.
  17060. When at a table, call the formula editor with `org-table-edit-formulas'.
  17061. When at the first line of an src example, call `org-edit-src-code'.
  17062. When in an #+include line, visit the include file. Otherwise call
  17063. `ffap' to visit the file at point."
  17064. (interactive)
  17065. ;; possibly prep session before editing source
  17066. (when arg
  17067. (let* ((info (org-babel-get-src-block-info))
  17068. (lang (nth 0 info))
  17069. (params (nth 2 info))
  17070. (session (cdr (assoc :session params))))
  17071. (when (and info session) ;; we are in a source-code block with a session
  17072. (funcall
  17073. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17074. (cond ;; proceed with `org-edit-special'
  17075. ((save-excursion
  17076. (beginning-of-line 1)
  17077. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17078. (find-file (org-trim (match-string 1))))
  17079. ((org-edit-src-code))
  17080. ((org-edit-fixed-width-region))
  17081. ((org-at-table.el-p)
  17082. (org-edit-src-code))
  17083. ((or (org-at-table-p)
  17084. (save-excursion
  17085. (beginning-of-line 1)
  17086. (looking-at "[ \t]*#\\+TBLFM:")))
  17087. (call-interactively 'org-table-edit-formulas))
  17088. (t (call-interactively 'ffap))))
  17089. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17090. (defun org-ctrl-c-ctrl-c (&optional arg)
  17091. "Set tags in headline, or update according to changed information at point.
  17092. This command does many different things, depending on context:
  17093. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17094. this is what we do.
  17095. - If the cursor is on a statistics cookie, update it.
  17096. - If the cursor is in a headline, prompt for tags and insert them
  17097. into the current line, aligned to `org-tags-column'. When called
  17098. with prefix arg, realign all tags in the current buffer.
  17099. - If the cursor is in one of the special #+KEYWORD lines, this
  17100. triggers scanning the buffer for these lines and updating the
  17101. information.
  17102. - If the cursor is inside a table, realign the table. This command
  17103. works even if the automatic table editor has been turned off.
  17104. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17105. the entire table.
  17106. - If the cursor is at a footnote reference or definition, jump to
  17107. the corresponding definition or references, respectively.
  17108. - If the cursor is a the beginning of a dynamic block, update it.
  17109. - If the current buffer is a capture buffer, close note and file it.
  17110. - If the cursor is on a <<<target>>>, update radio targets and
  17111. corresponding links in this buffer.
  17112. - If the cursor is on a numbered item in a plain list, renumber the
  17113. ordered list.
  17114. - If the cursor is on a checkbox, toggle it.
  17115. - If the cursor is on a code block, evaluate it. The variable
  17116. `org-confirm-babel-evaluate' can be used to control prompting
  17117. before code block evaluation, by default every code block
  17118. evaluation requires confirmation. Code block evaluation can be
  17119. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17120. (interactive "P")
  17121. (let ((org-enable-table-editor t))
  17122. (cond
  17123. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17124. org-occur-highlights
  17125. org-latex-fragment-image-overlays)
  17126. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17127. (org-remove-occur-highlights)
  17128. (org-remove-latex-fragment-image-overlays)
  17129. (message "Temporary highlights/overlays removed from current buffer"))
  17130. ((and (local-variable-p 'org-finish-function (current-buffer))
  17131. (fboundp org-finish-function))
  17132. (funcall org-finish-function))
  17133. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17134. ((org-in-regexp org-ts-regexp-both)
  17135. (org-timestamp-change 0 'day))
  17136. ((or (looking-at org-property-start-re)
  17137. (org-at-property-p))
  17138. (call-interactively 'org-property-action))
  17139. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17140. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17141. (or (org-at-heading-p) (org-at-item-p)))
  17142. (call-interactively 'org-update-statistics-cookies))
  17143. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17144. ((org-at-table.el-p)
  17145. (message "Use C-c ' to edit table.el tables"))
  17146. ((org-at-table-p)
  17147. (org-table-maybe-eval-formula)
  17148. (if arg
  17149. (call-interactively 'org-table-recalculate)
  17150. (org-table-maybe-recalculate-line))
  17151. (call-interactively 'org-table-align)
  17152. (orgtbl-send-table 'maybe))
  17153. ((or (org-footnote-at-reference-p)
  17154. (org-footnote-at-definition-p))
  17155. (call-interactively 'org-footnote-action))
  17156. ((org-at-item-checkbox-p)
  17157. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17158. ;; list only if at top item.
  17159. (let* ((cbox (match-string 1))
  17160. (struct (org-list-struct))
  17161. (old-struct (copy-tree struct))
  17162. (parents (org-list-parents-alist struct))
  17163. (orderedp (org-entry-get nil "ORDERED"))
  17164. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17165. block-item)
  17166. ;; Use a light version of `org-toggle-checkbox' to avoid
  17167. ;; computing list structure twice.
  17168. (let ((new-box (cond
  17169. ((equal arg '(16)) "[-]")
  17170. ((equal arg '(4)) nil)
  17171. ((equal "[X]" cbox) "[ ]")
  17172. (t "[X]"))))
  17173. (if (and firstp arg)
  17174. ;; If at first item of sub-list, remove check-box from
  17175. ;; every item at the same level.
  17176. (mapc
  17177. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17178. (org-list-get-all-items
  17179. (point-at-bol) struct (org-list-prevs-alist struct)))
  17180. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17181. ;; Replicate `org-list-write-struct', while grabbing a return
  17182. ;; value from `org-list-struct-fix-box'.
  17183. (org-list-struct-fix-ind struct parents 2)
  17184. (org-list-struct-fix-item-end struct)
  17185. (let ((prevs (org-list-prevs-alist struct)))
  17186. (org-list-struct-fix-bul struct prevs)
  17187. (org-list-struct-fix-ind struct parents)
  17188. (setq block-item
  17189. (org-list-struct-fix-box struct parents prevs orderedp)))
  17190. (org-list-struct-apply-struct struct old-struct)
  17191. (org-update-checkbox-count-maybe)
  17192. (when block-item
  17193. (message
  17194. "Checkboxes were removed due to unchecked box at line %d"
  17195. (org-current-line block-item)))
  17196. (when firstp (org-list-send-list 'maybe))))
  17197. ((org-at-item-p)
  17198. ;; Cursor at an item: repair list. Do checkbox related actions
  17199. ;; only if function was called with an argument. Send list only
  17200. ;; if at top item.
  17201. (let* ((struct (org-list-struct))
  17202. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17203. old-struct)
  17204. (when arg
  17205. (setq old-struct (copy-tree struct))
  17206. (if firstp
  17207. ;; If at first item of sub-list, add check-box to every
  17208. ;; item at the same level.
  17209. (mapc
  17210. (lambda (pos)
  17211. (unless (org-list-get-checkbox pos struct)
  17212. (org-list-set-checkbox pos struct "[ ]")))
  17213. (org-list-get-all-items
  17214. (point-at-bol) struct (org-list-prevs-alist struct)))
  17215. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17216. (org-list-write-struct
  17217. struct (org-list-parents-alist struct) old-struct)
  17218. (when arg (org-update-checkbox-count-maybe))
  17219. (when firstp (org-list-send-list 'maybe))))
  17220. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17221. ;; Dynamic block
  17222. (beginning-of-line 1)
  17223. (save-excursion (org-update-dblock)))
  17224. ((save-excursion
  17225. (beginning-of-line 1)
  17226. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  17227. (cond
  17228. ((equal (match-string 1) "TBLFM")
  17229. ;; Recalculate the table before this line
  17230. (save-excursion
  17231. (beginning-of-line 1)
  17232. (skip-chars-backward " \r\n\t")
  17233. (if (org-at-table-p)
  17234. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17235. (t
  17236. (let ((org-inhibit-startup-visibility-stuff t)
  17237. (org-startup-align-all-tables nil))
  17238. (when (boundp 'org-table-coordinate-overlays)
  17239. (mapc 'delete-overlay org-table-coordinate-overlays)
  17240. (setq org-table-coordinate-overlays nil))
  17241. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17242. (message "Local setup has been refreshed"))))
  17243. ((org-clock-update-time-maybe))
  17244. (t
  17245. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17246. (error "C-c C-c can do nothing useful at this location"))))))
  17247. (defun org-mode-restart ()
  17248. "Restart Org-mode, to scan again for special lines.
  17249. Also updates the keyword regular expressions."
  17250. (interactive)
  17251. (org-mode)
  17252. (message "Org-mode restarted"))
  17253. (defun org-kill-note-or-show-branches ()
  17254. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17255. (interactive)
  17256. (if (not org-finish-function)
  17257. (progn
  17258. (hide-subtree)
  17259. (call-interactively 'show-branches))
  17260. (let ((org-note-abort t))
  17261. (funcall org-finish-function))))
  17262. (defun org-return (&optional indent)
  17263. "Goto next table row or insert a newline.
  17264. Calls `org-table-next-row' or `newline', depending on context.
  17265. See the individual commands for more information."
  17266. (interactive)
  17267. (cond
  17268. ((or (bobp) (org-in-src-block-p))
  17269. (if indent (newline-and-indent) (newline)))
  17270. ((org-at-table-p)
  17271. (org-table-justify-field-maybe)
  17272. (call-interactively 'org-table-next-row))
  17273. ;; when `newline-and-indent' is called within a list, make sure
  17274. ;; text moved stays inside the item.
  17275. ((and (org-in-item-p) indent)
  17276. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17277. (progn
  17278. (save-match-data (newline))
  17279. (org-indent-line-to (length (match-string 0))))
  17280. (let ((ind (org-get-indentation)))
  17281. (newline)
  17282. (if (org-looking-back org-list-end-re)
  17283. (org-indent-line-function)
  17284. (org-indent-line-to ind)))))
  17285. ((and org-return-follows-link
  17286. (let ((tprop (get-text-property (point) 'face)))
  17287. (or (eq tprop 'org-link)
  17288. (and (listp tprop) (memq 'org-link tprop)))))
  17289. (call-interactively 'org-open-at-point))
  17290. ((and (org-at-heading-p)
  17291. (looking-at
  17292. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17293. (org-show-entry)
  17294. (end-of-line 1)
  17295. (newline))
  17296. (t (if indent (newline-and-indent) (newline)))))
  17297. (defun org-return-indent ()
  17298. "Goto next table row or insert a newline and indent.
  17299. Calls `org-table-next-row' or `newline-and-indent', depending on
  17300. context. See the individual commands for more information."
  17301. (interactive)
  17302. (org-return t))
  17303. (defun org-ctrl-c-star ()
  17304. "Compute table, or change heading status of lines.
  17305. Calls `org-table-recalculate' or `org-toggle-heading',
  17306. depending on context."
  17307. (interactive)
  17308. (cond
  17309. ((org-at-table-p)
  17310. (call-interactively 'org-table-recalculate))
  17311. (t
  17312. ;; Convert all lines in region to list items
  17313. (call-interactively 'org-toggle-heading))))
  17314. (defun org-ctrl-c-minus ()
  17315. "Insert separator line in table or modify bullet status of line.
  17316. Also turns a plain line or a region of lines into list items.
  17317. Calls `org-table-insert-hline', `org-toggle-item', or
  17318. `org-cycle-list-bullet', depending on context."
  17319. (interactive)
  17320. (cond
  17321. ((org-at-table-p)
  17322. (call-interactively 'org-table-insert-hline))
  17323. ((org-region-active-p)
  17324. (call-interactively 'org-toggle-item))
  17325. ((org-in-item-p)
  17326. (call-interactively 'org-cycle-list-bullet))
  17327. (t
  17328. (call-interactively 'org-toggle-item))))
  17329. (defun org-toggle-item (arg)
  17330. "Convert headings or normal lines to items, items to normal lines.
  17331. If there is no active region, only the current line is considered.
  17332. If the first non blank line in the region is an headline, convert
  17333. all headlines to items, shifting text accordingly.
  17334. If it is an item, convert all items to normal lines.
  17335. If it is normal text, change region into an item. With a prefix
  17336. argument ARG, change each line in region into an item."
  17337. (interactive "P")
  17338. (let ((shift-text
  17339. (function
  17340. ;; Shift text in current section to IND, from point to END.
  17341. ;; The function leaves point to END line.
  17342. (lambda (ind end)
  17343. (let ((min-i 1000) (end (copy-marker end)))
  17344. ;; First determine the minimum indentation (MIN-I) of
  17345. ;; the text.
  17346. (save-excursion
  17347. (catch 'exit
  17348. (while (< (point) end)
  17349. (let ((i (org-get-indentation)))
  17350. (cond
  17351. ;; Skip blank lines and inline tasks.
  17352. ((looking-at "^[ \t]*$"))
  17353. ((looking-at org-outline-regexp-bol))
  17354. ;; We can't find less than 0 indentation.
  17355. ((zerop i) (throw 'exit (setq min-i 0)))
  17356. ((< i min-i) (setq min-i i))))
  17357. (forward-line))))
  17358. ;; Then indent each line so that a line indented to
  17359. ;; MIN-I becomes indented to IND. Ignore blank lines
  17360. ;; and inline tasks in the process.
  17361. (let ((delta (- ind min-i)))
  17362. (while (< (point) end)
  17363. (unless (or (looking-at "^[ \t]*$")
  17364. (looking-at org-outline-regexp-bol))
  17365. (org-indent-line-to (+ (org-get-indentation) delta)))
  17366. (forward-line)))))))
  17367. (skip-blanks
  17368. (function
  17369. ;; Return beginning of first non-blank line, starting from
  17370. ;; line at POS.
  17371. (lambda (pos)
  17372. (save-excursion
  17373. (goto-char pos)
  17374. (skip-chars-forward " \r\t\n")
  17375. (point-at-bol)))))
  17376. beg end)
  17377. ;; Determine boundaries of changes.
  17378. (if (org-region-active-p)
  17379. (setq beg (funcall skip-blanks (region-beginning))
  17380. end (copy-marker (region-end)))
  17381. (setq beg (funcall skip-blanks (point-at-bol))
  17382. end (copy-marker (point-at-eol))))
  17383. ;; Depending on the starting line, choose an action on the text
  17384. ;; between BEG and END.
  17385. (org-with-limited-levels
  17386. (save-excursion
  17387. (goto-char beg)
  17388. (cond
  17389. ;; Case 1. Start at an item: de-itemize. Note that it only
  17390. ;; happens when a region is active: `org-ctrl-c-minus'
  17391. ;; would call `org-cycle-list-bullet' otherwise.
  17392. ((org-at-item-p)
  17393. (while (< (point) end)
  17394. (when (org-at-item-p)
  17395. (skip-chars-forward " \t")
  17396. (delete-region (point) (match-end 0)))
  17397. (forward-line)))
  17398. ;; Case 2. Start at an heading: convert to items.
  17399. ((org-at-heading-p)
  17400. (let* ((bul (org-list-bullet-string "-"))
  17401. (bul-len (length bul))
  17402. ;; Indentation of the first heading. It should be
  17403. ;; relative to the indentation of its parent, if any.
  17404. (start-ind (save-excursion
  17405. (cond
  17406. ((not org-adapt-indentation) 0)
  17407. ((not (outline-previous-heading)) 0)
  17408. (t (length (match-string 0))))))
  17409. ;; Level of first heading. Further headings will be
  17410. ;; compared to it to determine hierarchy in the list.
  17411. (ref-level (org-reduced-level (org-outline-level))))
  17412. (while (< (point) end)
  17413. (let* ((level (org-reduced-level (org-outline-level)))
  17414. (delta (max 0 (- level ref-level))))
  17415. ;; If current headline is less indented than the first
  17416. ;; one, set it as reference, in order to preserve
  17417. ;; subtrees.
  17418. (when (< level ref-level) (setq ref-level level))
  17419. (replace-match bul t t)
  17420. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17421. ;; Ensure all text down to END (or SECTION-END) belongs
  17422. ;; to the newly created item.
  17423. (let ((section-end (save-excursion
  17424. (or (outline-next-heading) (point)))))
  17425. (forward-line)
  17426. (funcall shift-text
  17427. (+ start-ind (* (1+ delta) bul-len))
  17428. (min end section-end)))))))
  17429. ;; Case 3. Normal line with ARG: turn each non-item line into
  17430. ;; an item.
  17431. (arg
  17432. (while (< (point) end)
  17433. (unless (or (org-at-heading-p) (org-at-item-p))
  17434. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17435. (replace-match
  17436. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17437. (forward-line)))
  17438. ;; Case 4. Normal line without ARG: make the first line of
  17439. ;; region an item, and shift indentation of others
  17440. ;; lines to set them as item's body.
  17441. (t (let* ((bul (org-list-bullet-string "-"))
  17442. (bul-len (length bul))
  17443. (ref-ind (org-get-indentation)))
  17444. (skip-chars-forward " \t")
  17445. (insert bul)
  17446. (forward-line)
  17447. (while (< (point) end)
  17448. ;; Ensure that lines less indented than first one
  17449. ;; still get included in item body.
  17450. (funcall shift-text
  17451. (+ ref-ind bul-len)
  17452. (min end (save-excursion (or (outline-next-heading)
  17453. (point)))))
  17454. (forward-line)))))))))
  17455. (defun org-toggle-heading (&optional nstars)
  17456. "Convert headings to normal text, or items or text to headings.
  17457. If there is no active region, only the current line is considered.
  17458. If the first non blank line is an headline, remove the stars from
  17459. all headlines in the region.
  17460. If it is a plain list item, turn all plain list items into headings.
  17461. If it is a normal line, turn each and every normal line (i.e. not
  17462. an heading or an item) in the region into a heading.
  17463. When converting a line into a heading, the number of stars is chosen
  17464. such that the lines become children of the current entry. However,
  17465. when a prefix argument is given, its value determines the number of
  17466. stars to add."
  17467. (interactive "P")
  17468. (let ((skip-blanks
  17469. (function
  17470. ;; Return beginning of first non-blank line, starting from
  17471. ;; line at POS.
  17472. (lambda (pos)
  17473. (save-excursion
  17474. (goto-char pos)
  17475. (skip-chars-forward " \r\t\n")
  17476. (point-at-bol)))))
  17477. beg end)
  17478. ;; Determine boundaries of changes. If region ends at a bol, do
  17479. ;; not consider the last line to be in the region.
  17480. (if (org-region-active-p)
  17481. (setq beg (funcall skip-blanks (region-beginning))
  17482. end (copy-marker (save-excursion
  17483. (goto-char (region-end))
  17484. (if (bolp) (point) (point-at-eol)))))
  17485. (setq beg (funcall skip-blanks (point-at-bol))
  17486. end (copy-marker (point-at-eol))))
  17487. ;; Ensure inline tasks don't count as headings.
  17488. (org-with-limited-levels
  17489. (save-excursion
  17490. (goto-char beg)
  17491. (cond
  17492. ;; Case 1. Started at an heading: de-star headings.
  17493. ((org-at-heading-p)
  17494. (while (< (point) end)
  17495. (when (org-at-heading-p t)
  17496. (looking-at org-outline-regexp) (replace-match ""))
  17497. (forward-line)))
  17498. ;; Case 2. Started at an item: change items into headlines.
  17499. ;; One star will be added by `org-list-to-subtree'.
  17500. ((org-at-item-p)
  17501. (let* ((stars (make-string
  17502. (if nstars
  17503. ;; subtract the star that will be added again by
  17504. ;; `org-list-to-subtree'
  17505. (1- (prefix-numeric-value current-prefix-arg))
  17506. (or (org-current-level) 0))
  17507. ?*))
  17508. (add-stars
  17509. (cond (nstars "") ; stars from prefix only
  17510. ((equal stars "") "") ; before first heading
  17511. (org-odd-levels-only "*") ; inside heading, odd
  17512. (t "")))) ; inside heading, oddeven
  17513. (while (< (point) end)
  17514. (when (org-at-item-p)
  17515. ;; Pay attention to cases when region ends before list.
  17516. (let* ((struct (org-list-struct))
  17517. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17518. (save-restriction
  17519. (narrow-to-region (point) list-end)
  17520. (insert
  17521. (org-list-to-subtree
  17522. (org-list-parse-list t)
  17523. '(:istart (concat stars add-stars (funcall get-stars depth))
  17524. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17525. (forward-line))))
  17526. ;; Case 3. Started at normal text: make every line an heading,
  17527. ;; skipping headlines and items.
  17528. (t (let* ((stars (make-string
  17529. (if nstars
  17530. (prefix-numeric-value current-prefix-arg)
  17531. (or (org-current-level) 0))
  17532. ?*))
  17533. (add-stars
  17534. (cond (nstars "") ; stars from prefix only
  17535. ((equal stars "") "*") ; before first heading
  17536. (org-odd-levels-only "**") ; inside heading, odd
  17537. (t "*"))) ; inside heading, oddeven
  17538. (rpl (concat stars add-stars " ")))
  17539. (while (< (point) end)
  17540. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17541. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17542. (replace-match (concat rpl (match-string 2))))
  17543. (forward-line)))))))))
  17544. (defun org-meta-return (&optional arg)
  17545. "Insert a new heading or wrap a region in a table.
  17546. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17547. See the individual commands for more information."
  17548. (interactive "P")
  17549. (cond
  17550. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17551. ((or (org-at-drawer-p) (org-at-property-p))
  17552. (newline-and-indent))
  17553. ((org-at-table-p)
  17554. (call-interactively 'org-table-wrap-region))
  17555. (t (call-interactively 'org-insert-heading))))
  17556. ;;; Menu entries
  17557. ;; Define the Org-mode menus
  17558. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17559. '("Tbl"
  17560. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17561. ["Next Field" org-cycle (org-at-table-p)]
  17562. ["Previous Field" org-shifttab (org-at-table-p)]
  17563. ["Next Row" org-return (org-at-table-p)]
  17564. "--"
  17565. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17566. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17567. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17568. "--"
  17569. ("Column"
  17570. ["Move Column Left" org-metaleft (org-at-table-p)]
  17571. ["Move Column Right" org-metaright (org-at-table-p)]
  17572. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17573. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17574. ("Row"
  17575. ["Move Row Up" org-metaup (org-at-table-p)]
  17576. ["Move Row Down" org-metadown (org-at-table-p)]
  17577. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17578. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17579. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17580. "--"
  17581. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17582. ("Rectangle"
  17583. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17584. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17585. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17586. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17587. "--"
  17588. ("Calculate"
  17589. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17590. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17591. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17592. "--"
  17593. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17594. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17595. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17596. "--"
  17597. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17598. "--"
  17599. ["Sum Column/Rectangle" org-table-sum
  17600. (or (org-at-table-p) (org-region-active-p))]
  17601. ["Which Column?" org-table-current-column (org-at-table-p)])
  17602. ["Debug Formulas"
  17603. org-table-toggle-formula-debugger
  17604. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17605. ["Show Col/Row Numbers"
  17606. org-table-toggle-coordinate-overlays
  17607. :style toggle
  17608. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17609. "--"
  17610. ["Create" org-table-create (and (not (org-at-table-p))
  17611. org-enable-table-editor)]
  17612. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17613. ["Import from File" org-table-import (not (org-at-table-p))]
  17614. ["Export to File" org-table-export (org-at-table-p)]
  17615. "--"
  17616. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17617. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17618. '("Org"
  17619. ("Show/Hide"
  17620. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17621. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17622. ["Sparse Tree..." org-sparse-tree t]
  17623. ["Reveal Context" org-reveal t]
  17624. ["Show All" show-all t]
  17625. "--"
  17626. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17627. "--"
  17628. ["New Heading" org-insert-heading t]
  17629. ("Navigate Headings"
  17630. ["Up" outline-up-heading t]
  17631. ["Next" outline-next-visible-heading t]
  17632. ["Previous" outline-previous-visible-heading t]
  17633. ["Next Same Level" outline-forward-same-level t]
  17634. ["Previous Same Level" outline-backward-same-level t]
  17635. "--"
  17636. ["Jump" org-goto t])
  17637. ("Edit Structure"
  17638. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17639. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17640. "--"
  17641. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17642. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17643. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17644. "--"
  17645. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17646. "--"
  17647. ["Copy visible text" org-copy-visible t]
  17648. "--"
  17649. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17650. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17651. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17652. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17653. "--"
  17654. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17655. "--"
  17656. ["Convert to odd levels" org-convert-to-odd-levels t]
  17657. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17658. ("Editing"
  17659. ["Emphasis..." org-emphasize t]
  17660. ["Edit Source Example" org-edit-special t]
  17661. "--"
  17662. ["Footnote new/jump" org-footnote-action t]
  17663. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17664. ("Archive"
  17665. ["Archive (default method)" org-archive-subtree-default t]
  17666. "--"
  17667. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17668. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17669. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17670. )
  17671. "--"
  17672. ("Hyperlinks"
  17673. ["Store Link (Global)" org-store-link t]
  17674. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17675. ["Insert Link" org-insert-link t]
  17676. ["Follow Link" org-open-at-point t]
  17677. "--"
  17678. ["Next link" org-next-link t]
  17679. ["Previous link" org-previous-link t]
  17680. "--"
  17681. ["Descriptive Links"
  17682. org-toggle-link-display
  17683. :style radio
  17684. :selected org-descriptive-links
  17685. ]
  17686. ["Literal Links"
  17687. org-toggle-link-display
  17688. :style radio
  17689. :selected (not org-descriptive-links)])
  17690. "--"
  17691. ("TODO Lists"
  17692. ["TODO/DONE/-" org-todo t]
  17693. ("Select keyword"
  17694. ["Next keyword" org-shiftright (org-at-heading-p)]
  17695. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17696. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17697. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17698. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17699. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17700. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17701. "--"
  17702. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17703. :selected org-enforce-todo-dependencies :style toggle :active t]
  17704. "Settings for tree at point"
  17705. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17706. :selected (org-entry-get nil "ORDERED")
  17707. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17708. ["Do Children parallel" org-toggle-ordered-property :style radio
  17709. :selected (not (org-entry-get nil "ORDERED"))
  17710. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17711. "--"
  17712. ["Set Priority" org-priority t]
  17713. ["Priority Up" org-shiftup t]
  17714. ["Priority Down" org-shiftdown t]
  17715. "--"
  17716. ["Get news from all feeds" org-feed-update-all t]
  17717. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17718. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17719. ("TAGS and Properties"
  17720. ["Set Tags" org-set-tags-command t]
  17721. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17722. "--"
  17723. ["Set property" org-set-property t]
  17724. ["Column view of properties" org-columns t]
  17725. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17726. ("Dates and Scheduling"
  17727. ["Timestamp" org-time-stamp t]
  17728. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17729. ("Change Date"
  17730. ["1 Day Later" org-shiftright t]
  17731. ["1 Day Earlier" org-shiftleft t]
  17732. ["1 ... Later" org-shiftup t]
  17733. ["1 ... Earlier" org-shiftdown t])
  17734. ["Compute Time Range" org-evaluate-time-range t]
  17735. ["Schedule Item" org-schedule t]
  17736. ["Deadline" org-deadline t]
  17737. "--"
  17738. ["Custom time format" org-toggle-time-stamp-overlays
  17739. :style radio :selected org-display-custom-times]
  17740. "--"
  17741. ["Goto Calendar" org-goto-calendar t]
  17742. ["Date from Calendar" org-date-from-calendar t]
  17743. "--"
  17744. ["Start/Restart Timer" org-timer-start t]
  17745. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17746. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17747. ["Insert Timer String" org-timer t]
  17748. ["Insert Timer Item" org-timer-item t])
  17749. ("Logging work"
  17750. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17751. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17752. ["Clock out" org-clock-out t]
  17753. ["Clock cancel" org-clock-cancel t]
  17754. "--"
  17755. ["Mark as default task" org-clock-mark-default-task t]
  17756. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17757. ["Goto running clock" org-clock-goto t]
  17758. "--"
  17759. ["Display times" org-clock-display t]
  17760. ["Create clock table" org-clock-report t]
  17761. "--"
  17762. ["Record DONE time"
  17763. (progn (setq org-log-done (not org-log-done))
  17764. (message "Switching to %s will %s record a timestamp"
  17765. (car org-done-keywords)
  17766. (if org-log-done "automatically" "not")))
  17767. :style toggle :selected org-log-done])
  17768. "--"
  17769. ["Agenda Command..." org-agenda t]
  17770. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17771. ("File List for Agenda")
  17772. ("Special views current file"
  17773. ["TODO Tree" org-show-todo-tree t]
  17774. ["Check Deadlines" org-check-deadlines t]
  17775. ["Timeline" org-timeline t]
  17776. ["Tags/Property tree" org-match-sparse-tree t])
  17777. "--"
  17778. ["Export/Publish..." org-export t]
  17779. ("LaTeX"
  17780. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17781. :selected org-cdlatex-mode]
  17782. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17783. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17784. ["Modify math symbol" org-cdlatex-math-modify
  17785. (org-inside-LaTeX-fragment-p)]
  17786. ["Insert citation" org-reftex-citation t]
  17787. "--"
  17788. ["Template for BEAMER" (progn (require 'org-beamer)
  17789. (org-insert-beamer-options-template)) t])
  17790. "--"
  17791. ("MobileOrg"
  17792. ["Push Files and Views" org-mobile-push t]
  17793. ["Get Captured and Flagged" org-mobile-pull t]
  17794. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17795. "--"
  17796. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17797. "--"
  17798. ("Documentation"
  17799. ["Show Version" org-version t]
  17800. ["Info Documentation" org-info t])
  17801. ("Customize"
  17802. ["Browse Org Group" org-customize t]
  17803. "--"
  17804. ["Expand This Menu" org-create-customize-menu
  17805. (fboundp 'customize-menu-create)])
  17806. ["Send bug report" org-submit-bug-report t]
  17807. "--"
  17808. ("Refresh/Reload"
  17809. ["Refresh setup current buffer" org-mode-restart t]
  17810. ["Reload Org (after update)" org-reload t]
  17811. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17812. ))
  17813. (defun org-info (&optional node)
  17814. "Read documentation for Org-mode in the info system.
  17815. With optional NODE, go directly to that node."
  17816. (interactive)
  17817. (info (format "(org)%s" (or node ""))))
  17818. ;;;###autoload
  17819. (defun org-submit-bug-report ()
  17820. "Submit a bug report on Org-mode via mail.
  17821. Don't hesitate to report any problems or inaccurate documentation.
  17822. If you don't have setup sending mail from (X)Emacs, please copy the
  17823. output buffer into your mail program, as it gives us important
  17824. information about your Org-mode version and configuration."
  17825. (interactive)
  17826. (require 'reporter)
  17827. (org-load-modules-maybe)
  17828. (org-require-autoloaded-modules)
  17829. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17830. (reporter-submit-bug-report
  17831. "emacs-orgmode@gnu.org"
  17832. (org-version)
  17833. (let (list)
  17834. (save-window-excursion
  17835. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17836. (delete-other-windows)
  17837. (erase-buffer)
  17838. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17839. We would like to add your full Org-mode and Outline configuration to the
  17840. bug report. This greatly simplifies the work of the maintainer and
  17841. other experts on the mailing list.
  17842. HOWEVER, some variables you have customized may contain private
  17843. information. The names of customers, colleagues, or friends, might
  17844. appear in the form of file names, tags, todo states, or search strings.
  17845. If you answer yes to the prompt, you might want to check and remove
  17846. such private information before sending the email.")
  17847. (add-text-properties (point-min) (point-max) '(face org-warning))
  17848. (when (yes-or-no-p "Include your Org-mode configuration ")
  17849. (mapatoms
  17850. (lambda (v)
  17851. (and (boundp v)
  17852. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17853. (or (and (symbol-value v)
  17854. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17855. (and
  17856. (get v 'custom-type) (get v 'standard-value)
  17857. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17858. (push v list)))))
  17859. (kill-buffer (get-buffer "*Warn about privacy*"))
  17860. list))
  17861. nil nil
  17862. "Remember to cover the basics, that is, what you expected to happen and
  17863. what in fact did happen. You don't know how to make a good report? See
  17864. http://orgmode.org/manual/Feedback.html#Feedback
  17865. Your bug report will be posted to the Org-mode mailing list.
  17866. ------------------------------------------------------------------------")
  17867. (save-excursion
  17868. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17869. (replace-match "\\1Bug: \\3 [\\2]")))))
  17870. (defun org-install-agenda-files-menu ()
  17871. (let ((bl (buffer-list)))
  17872. (save-excursion
  17873. (while bl
  17874. (set-buffer (pop bl))
  17875. (if (derived-mode-p 'org-mode) (setq bl nil)))
  17876. (when (derived-mode-p 'org-mode)
  17877. (easy-menu-change
  17878. '("Org") "File List for Agenda"
  17879. (append
  17880. (list
  17881. ["Edit File List" (org-edit-agenda-file-list) t]
  17882. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17883. ["Remove Current File from List" org-remove-file t]
  17884. ["Cycle through agenda files" org-cycle-agenda-files t]
  17885. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17886. "--")
  17887. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17888. ;;;; Documentation
  17889. ;;;###autoload
  17890. (defun org-require-autoloaded-modules ()
  17891. (interactive)
  17892. (mapc 'require
  17893. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17894. org-docbook org-exp org-html org-icalendar
  17895. org-id org-latex
  17896. org-publish org-remember org-table
  17897. org-timer org-xoxo)))
  17898. ;;;###autoload
  17899. (defun org-reload (&optional uncompiled)
  17900. "Reload all org lisp files.
  17901. With prefix arg UNCOMPILED, load the uncompiled versions."
  17902. (interactive "P")
  17903. (require 'find-func)
  17904. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17905. (dir-org (file-name-directory (org-find-library-name "org")))
  17906. (dir-org-contrib (ignore-errors
  17907. (file-name-directory
  17908. (org-find-library-name "org-contribdir"))))
  17909. (babel-files
  17910. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17911. (append (list nil "comint" "eval" "exp" "keys"
  17912. "lob" "ref" "table" "tangle")
  17913. (delq nil
  17914. (mapcar
  17915. (lambda (lang)
  17916. (when (cdr lang) (symbol-name (car lang))))
  17917. org-babel-load-languages)))))
  17918. (files
  17919. (append (directory-files dir-org t file-re)
  17920. babel-files
  17921. (and dir-org-contrib
  17922. (directory-files dir-org-contrib t file-re))))
  17923. (remove-re (concat (if (featurep 'xemacs)
  17924. "org-colview" "org-colview-xemacs")
  17925. "\\'")))
  17926. (setq files (mapcar 'file-name-sans-extension files))
  17927. (setq files (mapcar
  17928. (lambda (x) (if (string-match remove-re x) nil x))
  17929. files))
  17930. (setq files (delq nil files))
  17931. (mapc
  17932. (lambda (f)
  17933. (when (featurep (intern (file-name-nondirectory f)))
  17934. (if (and (not uncompiled)
  17935. (file-exists-p (concat f ".elc")))
  17936. (load (concat f ".elc") nil nil t)
  17937. (load (concat f ".el") nil nil t))))
  17938. files))
  17939. (org-version))
  17940. ;;;###autoload
  17941. (defun org-customize ()
  17942. "Call the customize function with org as argument."
  17943. (interactive)
  17944. (org-load-modules-maybe)
  17945. (org-require-autoloaded-modules)
  17946. (customize-browse 'org))
  17947. (defun org-create-customize-menu ()
  17948. "Create a full customization menu for Org-mode, insert it into the menu."
  17949. (interactive)
  17950. (org-load-modules-maybe)
  17951. (org-require-autoloaded-modules)
  17952. (if (fboundp 'customize-menu-create)
  17953. (progn
  17954. (easy-menu-change
  17955. '("Org") "Customize"
  17956. `(["Browse Org group" org-customize t]
  17957. "--"
  17958. ,(customize-menu-create 'org)
  17959. ["Set" Custom-set t]
  17960. ["Save" Custom-save t]
  17961. ["Reset to Current" Custom-reset-current t]
  17962. ["Reset to Saved" Custom-reset-saved t]
  17963. ["Reset to Standard Settings" Custom-reset-standard t]))
  17964. (message "\"Org\"-menu now contains full customization menu"))
  17965. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17966. ;;;; Miscellaneous stuff
  17967. ;;; Generally useful functions
  17968. (defun org-get-at-bol (property)
  17969. "Get text property PROPERTY at beginning of line."
  17970. (get-text-property (point-at-bol) property))
  17971. (defun org-find-text-property-in-string (prop s)
  17972. "Return the first non-nil value of property PROP in string S."
  17973. (or (get-text-property 0 prop s)
  17974. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17975. prop s)))
  17976. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17977. "Display the given MESSAGE as a warning."
  17978. (if (fboundp 'display-warning)
  17979. (display-warning 'org message
  17980. (if (featurep 'xemacs) 'warning :warning))
  17981. (let ((buf (get-buffer-create "*Org warnings*")))
  17982. (with-current-buffer buf
  17983. (goto-char (point-max))
  17984. (insert "Warning (Org): " message)
  17985. (unless (bolp)
  17986. (newline)))
  17987. (display-buffer buf)
  17988. (sit-for 0))))
  17989. (defun org-eval (form)
  17990. "Eval FORM and return result."
  17991. (condition-case error
  17992. (eval form)
  17993. (error (format "%%![Error: %s]" error))))
  17994. (defun org-in-clocktable-p ()
  17995. "Check if the cursor is in a clocktable."
  17996. (let ((pos (point)) start)
  17997. (save-excursion
  17998. (end-of-line 1)
  17999. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18000. (setq start (match-beginning 0))
  18001. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18002. (>= (match-end 0) pos)
  18003. start))))
  18004. (defun org-in-commented-line ()
  18005. "Is point in a line starting with `#'?"
  18006. (equal (char-after (point-at-bol)) ?#))
  18007. (defun org-in-indented-comment-line ()
  18008. "Is point in a line starting with `#' after some white space?"
  18009. (save-excursion
  18010. (save-match-data
  18011. (goto-char (point-at-bol))
  18012. (looking-at "[ \t]*#"))))
  18013. (defun org-in-verbatim-emphasis ()
  18014. (save-match-data
  18015. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18016. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18017. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18018. (if (and marker (marker-buffer marker)
  18019. (buffer-live-p (marker-buffer marker)))
  18020. (progn
  18021. (org-pop-to-buffer-same-window (marker-buffer marker))
  18022. (if (or (> marker (point-max)) (< marker (point-min)))
  18023. (widen))
  18024. (goto-char marker)
  18025. (org-show-context 'org-goto))
  18026. (if bookmark
  18027. (bookmark-jump bookmark)
  18028. (error "Cannot find location"))))
  18029. (defun org-quote-csv-field (s)
  18030. "Quote field for inclusion in CSV material."
  18031. (if (string-match "[\",]" s)
  18032. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18033. s))
  18034. (defun org-force-self-insert (N)
  18035. "Needed to enforce self-insert under remapping."
  18036. (interactive "p")
  18037. (self-insert-command N))
  18038. (defun org-string-width (s)
  18039. "Compute width of string, ignoring invisible characters.
  18040. This ignores character with invisibility property `org-link', and also
  18041. characters with property `org-cwidth', because these will become invisible
  18042. upon the next fontification round."
  18043. (let (b l)
  18044. (when (or (eq t buffer-invisibility-spec)
  18045. (assq 'org-link buffer-invisibility-spec))
  18046. (while (setq b (text-property-any 0 (length s)
  18047. 'invisible 'org-link s))
  18048. (setq s (concat (substring s 0 b)
  18049. (substring s (or (next-single-property-change
  18050. b 'invisible s) (length s)))))))
  18051. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18052. (setq s (concat (substring s 0 b)
  18053. (substring s (or (next-single-property-change
  18054. b 'org-cwidth s) (length s))))))
  18055. (setq l (string-width s) b -1)
  18056. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18057. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18058. l))
  18059. (defun org-shorten-string (s maxlength)
  18060. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18061. If the string is shorter or has length MAXLENGTH, just return the
  18062. original string. If it is longer, the functions finds a space in the
  18063. string, breaks this string off at that locations and adds three dots
  18064. as ellipsis. Including the ellipsis, the string will not be longer
  18065. than MAXLENGTH. If finding a good breaking point in the string does
  18066. not work, the string is just chopped off in the middle of a word
  18067. if necessary."
  18068. (if (<= (length s) maxlength)
  18069. s
  18070. (let* ((n (max (- maxlength 4) 1))
  18071. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18072. (if (string-match re s)
  18073. (concat (match-string 1 s) "...")
  18074. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18075. (defun org-get-indentation (&optional line)
  18076. "Get the indentation of the current line, interpreting tabs.
  18077. When LINE is given, assume it represents a line and compute its indentation."
  18078. (if line
  18079. (if (string-match "^ *" (org-remove-tabs line))
  18080. (match-end 0))
  18081. (save-excursion
  18082. (beginning-of-line 1)
  18083. (skip-chars-forward " \t")
  18084. (current-column))))
  18085. (defun org-get-string-indentation (s)
  18086. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18087. (let ((n -1) (i 0) (w tab-width) c)
  18088. (catch 'exit
  18089. (while (< (setq n (1+ n)) (length s))
  18090. (setq c (aref s n))
  18091. (cond ((= c ?\ ) (setq i (1+ i)))
  18092. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18093. (t (throw 'exit t)))))
  18094. i))
  18095. (defun org-remove-tabs (s &optional width)
  18096. "Replace tabulators in S with spaces.
  18097. Assumes that s is a single line, starting in column 0."
  18098. (setq width (or width tab-width))
  18099. (while (string-match "\t" s)
  18100. (setq s (replace-match
  18101. (make-string
  18102. (- (* width (/ (+ (match-beginning 0) width) width))
  18103. (match-beginning 0)) ?\ )
  18104. t t s)))
  18105. s)
  18106. (defun org-fix-indentation (line ind)
  18107. "Fix indentation in LINE.
  18108. IND is a cons cell with target and minimum indentation.
  18109. If the current indentation in LINE is smaller than the minimum,
  18110. leave it alone. If it is larger than ind, set it to the target."
  18111. (let* ((l (org-remove-tabs line))
  18112. (i (org-get-indentation l))
  18113. (i1 (car ind)) (i2 (cdr ind)))
  18114. (if (>= i i2) (setq l (substring line i2)))
  18115. (if (> i1 0)
  18116. (concat (make-string i1 ?\ ) l)
  18117. l)))
  18118. (defun org-remove-indentation (code &optional n)
  18119. "Remove the maximum common indentation from the lines in CODE.
  18120. N may optionally be the number of spaces to remove."
  18121. (with-temp-buffer
  18122. (insert code)
  18123. (org-do-remove-indentation n)
  18124. (buffer-string)))
  18125. (defun org-do-remove-indentation (&optional n)
  18126. "Remove the maximum common indentation from the buffer."
  18127. (untabify (point-min) (point-max))
  18128. (let ((min 10000) re)
  18129. (if n
  18130. (setq min n)
  18131. (goto-char (point-min))
  18132. (while (re-search-forward "^ *[^ \n]" nil t)
  18133. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18134. (unless (or (= min 0) (= min 10000))
  18135. (setq re (format "^ \\{%d\\}" min))
  18136. (goto-char (point-min))
  18137. (while (re-search-forward re nil t)
  18138. (replace-match "")
  18139. (end-of-line 1))
  18140. min)))
  18141. (defun org-fill-template (template alist)
  18142. "Find each %key of ALIST in TEMPLATE and replace it."
  18143. (let ((case-fold-search nil)
  18144. entry key value)
  18145. (setq alist (sort (copy-sequence alist)
  18146. (lambda (a b) (< (length (car a)) (length (car b))))))
  18147. (while (setq entry (pop alist))
  18148. (setq template
  18149. (replace-regexp-in-string
  18150. (concat "%" (regexp-quote (car entry)))
  18151. (cdr entry) template t t)))
  18152. template))
  18153. (defun org-base-buffer (buffer)
  18154. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18155. (if (not buffer)
  18156. buffer
  18157. (or (buffer-base-buffer buffer)
  18158. buffer)))
  18159. (defun org-trim (s)
  18160. "Remove whitespace at beginning and end of string."
  18161. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18162. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18163. s)
  18164. (defun org-wrap (string &optional width lines)
  18165. "Wrap string to either a number of lines, or a width in characters.
  18166. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18167. that costs. If there is a word longer than WIDTH, the text is actually
  18168. wrapped to the length of that word.
  18169. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18170. many lines, whatever width that takes.
  18171. The return value is a list of lines, without newlines at the end."
  18172. (let* ((words (org-split-string string "[ \t\n]+"))
  18173. (maxword (apply 'max (mapcar 'org-string-width words)))
  18174. w ll)
  18175. (cond (width
  18176. (org-do-wrap words (max maxword width)))
  18177. (lines
  18178. (setq w maxword)
  18179. (setq ll (org-do-wrap words maxword))
  18180. (if (<= (length ll) lines)
  18181. ll
  18182. (setq ll words)
  18183. (while (> (length ll) lines)
  18184. (setq w (1+ w))
  18185. (setq ll (org-do-wrap words w)))
  18186. ll))
  18187. (t (error "Cannot wrap this")))))
  18188. (defun org-do-wrap (words width)
  18189. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18190. (let (lines line)
  18191. (while words
  18192. (setq line (pop words))
  18193. (while (and words (< (+ (length line) (length (car words))) width))
  18194. (setq line (concat line " " (pop words))))
  18195. (setq lines (push line lines)))
  18196. (nreverse lines)))
  18197. (defun org-split-string (string &optional separators)
  18198. "Splits STRING into substrings at SEPARATORS.
  18199. No empty strings are returned if there are matches at the beginning
  18200. and end of string."
  18201. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18202. (start 0)
  18203. notfirst
  18204. (list nil))
  18205. (while (and (string-match rexp string
  18206. (if (and notfirst
  18207. (= start (match-beginning 0))
  18208. (< start (length string)))
  18209. (1+ start) start))
  18210. (< (match-beginning 0) (length string)))
  18211. (setq notfirst t)
  18212. (or (eq (match-beginning 0) 0)
  18213. (and (eq (match-beginning 0) (match-end 0))
  18214. (eq (match-beginning 0) start))
  18215. (setq list
  18216. (cons (substring string start (match-beginning 0))
  18217. list)))
  18218. (setq start (match-end 0)))
  18219. (or (eq start (length string))
  18220. (setq list
  18221. (cons (substring string start)
  18222. list)))
  18223. (nreverse list)))
  18224. (defun org-quote-vert (s)
  18225. "Replace \"|\" with \"\\vert\"."
  18226. (while (string-match "|" s)
  18227. (setq s (replace-match "\\vert" t t s)))
  18228. s)
  18229. (defun org-uuidgen-p (s)
  18230. "Is S an ID created by UUIDGEN?"
  18231. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18232. (defun org-in-src-block-p nil
  18233. "Whether point is in a code source block."
  18234. (let (ov)
  18235. (when (setq ov (overlays-at (point)))
  18236. (memq 'org-block-background
  18237. (overlay-properties
  18238. (car ov))))))
  18239. (defun org-context ()
  18240. "Return a list of contexts of the current cursor position.
  18241. If several contexts apply, all are returned.
  18242. Each context entry is a list with a symbol naming the context, and
  18243. two positions indicating start and end of the context. Possible
  18244. contexts are:
  18245. :headline anywhere in a headline
  18246. :headline-stars on the leading stars in a headline
  18247. :todo-keyword on a TODO keyword (including DONE) in a headline
  18248. :tags on the TAGS in a headline
  18249. :priority on the priority cookie in a headline
  18250. :item on the first line of a plain list item
  18251. :item-bullet on the bullet/number of a plain list item
  18252. :checkbox on the checkbox in a plain list item
  18253. :table in an org-mode table
  18254. :table-special on a special filed in a table
  18255. :table-table in a table.el table
  18256. :clocktable in a clocktable
  18257. :src-block in a source block
  18258. :link on a hyperlink
  18259. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18260. :target on a <<target>>
  18261. :radio-target on a <<<radio-target>>>
  18262. :latex-fragment on a LaTeX fragment
  18263. :latex-preview on a LaTeX fragment with overlaid preview image
  18264. This function expects the position to be visible because it uses font-lock
  18265. faces as a help to recognize the following contexts: :table-special, :link,
  18266. and :keyword."
  18267. (let* ((f (get-text-property (point) 'face))
  18268. (faces (if (listp f) f (list f)))
  18269. (case-fold-search t)
  18270. (p (point)) clist o)
  18271. ;; First the large context
  18272. (cond
  18273. ((org-at-heading-p t)
  18274. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18275. (when (progn
  18276. (beginning-of-line 1)
  18277. (looking-at org-todo-line-tags-regexp))
  18278. (push (org-point-in-group p 1 :headline-stars) clist)
  18279. (push (org-point-in-group p 2 :todo-keyword) clist)
  18280. (push (org-point-in-group p 4 :tags) clist))
  18281. (goto-char p)
  18282. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18283. (if (looking-at "\\[#[A-Z0-9]\\]")
  18284. (push (org-point-in-group p 0 :priority) clist)))
  18285. ((org-at-item-p)
  18286. (push (org-point-in-group p 2 :item-bullet) clist)
  18287. (push (list :item (point-at-bol)
  18288. (save-excursion (org-end-of-item) (point)))
  18289. clist)
  18290. (and (org-at-item-checkbox-p)
  18291. (push (org-point-in-group p 0 :checkbox) clist)))
  18292. ((org-at-table-p)
  18293. (push (list :table (org-table-begin) (org-table-end)) clist)
  18294. (if (memq 'org-formula faces)
  18295. (push (list :table-special
  18296. (previous-single-property-change p 'face)
  18297. (next-single-property-change p 'face)) clist)))
  18298. ((org-at-table-p 'any)
  18299. (push (list :table-table) clist)))
  18300. (goto-char p)
  18301. ;; New the "medium" contexts: clocktables, source blocks
  18302. (cond ((org-in-clocktable-p)
  18303. (push (list :clocktable
  18304. (and (or (looking-at "#\\+BEGIN: clocktable")
  18305. (search-backward "#+BEGIN: clocktable" nil t))
  18306. (match-beginning 0))
  18307. (and (re-search-forward "#\\+END:?" nil t)
  18308. (match-end 0))) clist))
  18309. ((org-in-src-block-p)
  18310. (push (list :src-block
  18311. (and (or (looking-at "#\\+BEGIN_SRC")
  18312. (search-backward "#+BEGIN_SRC" nil t))
  18313. (match-beginning 0))
  18314. (and (search-forward "#+END_SRC" nil t)
  18315. (match-beginning 0))) clist)))
  18316. (goto-char p)
  18317. ;; Now the small context
  18318. (cond
  18319. ((org-at-timestamp-p)
  18320. (push (org-point-in-group p 0 :timestamp) clist))
  18321. ((memq 'org-link faces)
  18322. (push (list :link
  18323. (previous-single-property-change p 'face)
  18324. (next-single-property-change p 'face)) clist))
  18325. ((memq 'org-special-keyword faces)
  18326. (push (list :keyword
  18327. (previous-single-property-change p 'face)
  18328. (next-single-property-change p 'face)) clist))
  18329. ((org-at-target-p)
  18330. (push (org-point-in-group p 0 :target) clist)
  18331. (goto-char (1- (match-beginning 0)))
  18332. (if (looking-at org-radio-target-regexp)
  18333. (push (org-point-in-group p 0 :radio-target) clist))
  18334. (goto-char p))
  18335. ((setq o (car (delq nil
  18336. (mapcar
  18337. (lambda (x)
  18338. (if (memq x org-latex-fragment-image-overlays) x))
  18339. (overlays-at (point))))))
  18340. (push (list :latex-fragment
  18341. (overlay-start o) (overlay-end o)) clist)
  18342. (push (list :latex-preview
  18343. (overlay-start o) (overlay-end o)) clist))
  18344. ((org-inside-LaTeX-fragment-p)
  18345. ;; FIXME: positions wrong.
  18346. (push (list :latex-fragment (point) (point)) clist)))
  18347. (setq clist (nreverse (delq nil clist)))
  18348. clist))
  18349. ;; FIXME: Compare with at-regexp-p Do we need both?
  18350. (defun org-in-regexp (re &optional nlines visually)
  18351. "Check if point is inside a match of regexp.
  18352. Normally only the current line is checked, but you can include NLINES extra
  18353. lines both before and after point into the search.
  18354. If VISUALLY is set, require that the cursor is not after the match but
  18355. really on, so that the block visually is on the match."
  18356. (catch 'exit
  18357. (let ((pos (point))
  18358. (eol (point-at-eol (+ 1 (or nlines 0))))
  18359. (inc (if visually 1 0)))
  18360. (save-excursion
  18361. (beginning-of-line (- 1 (or nlines 0)))
  18362. (while (re-search-forward re eol t)
  18363. (if (and (<= (match-beginning 0) pos)
  18364. (>= (+ inc (match-end 0)) pos))
  18365. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18366. (defun org-at-regexp-p (regexp)
  18367. "Is point inside a match of REGEXP in the current line?"
  18368. (catch 'exit
  18369. (save-excursion
  18370. (let ((pos (point)) (end (point-at-eol)))
  18371. (beginning-of-line 1)
  18372. (while (re-search-forward regexp end t)
  18373. (if (and (<= (match-beginning 0) pos)
  18374. (>= (match-end 0) pos))
  18375. (throw 'exit t)))
  18376. nil))))
  18377. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18378. "Non-nil when point is between matches of START-RE and END-RE.
  18379. Also return a non-nil value when point is on one of the matches.
  18380. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18381. buffer positions. Default values are the positions of headlines
  18382. surrounding the point.
  18383. The functions returns a cons cell whose car (resp. cdr) is the
  18384. position before START-RE (resp. after END-RE)."
  18385. (save-match-data
  18386. (let ((pos (point))
  18387. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18388. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18389. beg end)
  18390. (save-excursion
  18391. ;; Point is on a block when on START-RE or if START-RE can be
  18392. ;; found before it...
  18393. (and (or (org-at-regexp-p start-re)
  18394. (re-search-backward start-re limit-up t))
  18395. (setq beg (match-beginning 0))
  18396. ;; ... and END-RE after it...
  18397. (goto-char (match-end 0))
  18398. (re-search-forward end-re limit-down t)
  18399. (> (setq end (match-end 0)) pos)
  18400. ;; ... without another START-RE in-between.
  18401. (goto-char (match-beginning 0))
  18402. (not (re-search-backward start-re (1+ beg) t))
  18403. ;; Return value.
  18404. (cons beg end))))))
  18405. (defun org-in-block-p (names)
  18406. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18407. NAMES is a list of strings containing names of blocks.
  18408. Return first block name matched, or nil. Beware that in case of
  18409. nested blocks, the returned name may not belong to the closest
  18410. block from point."
  18411. (save-match-data
  18412. (catch 'exit
  18413. (let ((case-fold-search t)
  18414. (lim-up (save-excursion (outline-previous-heading)))
  18415. (lim-down (save-excursion (outline-next-heading))))
  18416. (mapc (lambda (name)
  18417. (let ((n (regexp-quote name)))
  18418. (when (org-between-regexps-p
  18419. (concat "^[ \t]*#\\+begin_" n)
  18420. (concat "^[ \t]*#\\+end_" n)
  18421. lim-up lim-down)
  18422. (throw 'exit n))))
  18423. names))
  18424. nil)))
  18425. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18426. "Call `multi-occur' with buffers for all agenda files."
  18427. (interactive "sOrg-files matching: \np")
  18428. (let* ((files (org-agenda-files))
  18429. (tnames (mapcar 'file-truename files))
  18430. (extra org-agenda-text-search-extra-files)
  18431. f)
  18432. (when (eq (car extra) 'agenda-archives)
  18433. (setq extra (cdr extra))
  18434. (setq files (org-add-archive-files files)))
  18435. (while (setq f (pop extra))
  18436. (unless (member (file-truename f) tnames)
  18437. (add-to-list 'files f 'append)
  18438. (add-to-list 'tnames (file-truename f) 'append)))
  18439. (multi-occur
  18440. (mapcar (lambda (x)
  18441. (with-current-buffer
  18442. (or (get-file-buffer x) (find-file-noselect x))
  18443. (widen)
  18444. (current-buffer)))
  18445. files)
  18446. regexp)))
  18447. (if (boundp 'occur-mode-find-occurrence-hook)
  18448. ;; Emacs 23
  18449. (add-hook 'occur-mode-find-occurrence-hook
  18450. (lambda ()
  18451. (when (derived-mode-p 'org-mode)
  18452. (org-reveal))))
  18453. ;; Emacs 22
  18454. (defadvice occur-mode-goto-occurrence
  18455. (after org-occur-reveal activate)
  18456. (and (derived-mode-p 'org-mode) (org-reveal)))
  18457. (defadvice occur-mode-goto-occurrence-other-window
  18458. (after org-occur-reveal activate)
  18459. (and (derived-mode-p 'org-mode) (org-reveal)))
  18460. (defadvice occur-mode-display-occurrence
  18461. (after org-occur-reveal activate)
  18462. (when (derived-mode-p 'org-mode)
  18463. (let ((pos (occur-mode-find-occurrence)))
  18464. (with-current-buffer (marker-buffer pos)
  18465. (save-excursion
  18466. (goto-char pos)
  18467. (org-reveal)))))))
  18468. (defun org-occur-link-in-agenda-files ()
  18469. "Create a link and search for it in the agendas.
  18470. The link is not stored in `org-stored-links', it is just created
  18471. for the search purpose."
  18472. (interactive)
  18473. (let ((link (condition-case nil
  18474. (org-store-link nil)
  18475. (error "Unable to create a link to here"))))
  18476. (org-occur-in-agenda-files (regexp-quote link))))
  18477. (defun org-uniquify (list)
  18478. "Remove duplicate elements from LIST."
  18479. (let (res)
  18480. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18481. res))
  18482. (defun org-delete-all (elts list)
  18483. "Remove all elements in ELTS from LIST."
  18484. (while elts
  18485. (setq list (delete (pop elts) list)))
  18486. list)
  18487. (defun org-count (cl-item cl-seq)
  18488. "Count the number of occurrences of ITEM in SEQ.
  18489. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18490. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18491. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18492. (while (< cl-start cl-end)
  18493. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18494. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18495. (setq cl-start (1+ cl-start)))
  18496. cl-count))
  18497. (defun org-remove-if (predicate seq)
  18498. "Remove everything from SEQ that fulfills PREDICATE."
  18499. (let (res e)
  18500. (while seq
  18501. (setq e (pop seq))
  18502. (if (not (funcall predicate e)) (push e res)))
  18503. (nreverse res)))
  18504. (defun org-remove-if-not (predicate seq)
  18505. "Remove everything from SEQ that does not fulfill PREDICATE."
  18506. (let (res e)
  18507. (while seq
  18508. (setq e (pop seq))
  18509. (if (funcall predicate e) (push e res)))
  18510. (nreverse res)))
  18511. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18512. "Reduce two-argument FUNCTION across SEQ.
  18513. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18514. \":initial-value\" removed."
  18515. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18516. (cadr (memq :initial-value cl-keys)))
  18517. (cl-seq (pop cl-seq))
  18518. (t (funcall cl-func)))))
  18519. (while cl-seq
  18520. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18521. cl-accum))
  18522. (defun org-back-over-empty-lines ()
  18523. "Move backwards over whitespace, to the beginning of the first empty line.
  18524. Returns the number of empty lines passed."
  18525. (let ((pos (point)))
  18526. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18527. (skip-chars-backward " \t\n\r")
  18528. (unless (eobp)
  18529. (forward-line -1)))
  18530. (beginning-of-line 2)
  18531. (goto-char (min (point) pos))
  18532. (count-lines (point) pos)))
  18533. (defun org-skip-whitespace ()
  18534. (skip-chars-forward " \t\n\r"))
  18535. (defun org-point-in-group (point group &optional context)
  18536. "Check if POINT is in match-group GROUP.
  18537. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18538. match. If the match group does not exist or point is not inside it,
  18539. return nil."
  18540. (and (match-beginning group)
  18541. (>= point (match-beginning group))
  18542. (<= point (match-end group))
  18543. (if context
  18544. (list context (match-beginning group) (match-end group))
  18545. t)))
  18546. (defun org-switch-to-buffer-other-window (&rest args)
  18547. "Switch to buffer in a second window on the current frame.
  18548. In particular, do not allow pop-up frames.
  18549. Returns the newly created buffer."
  18550. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18551. special-display-function)
  18552. (apply 'switch-to-buffer-other-window args)))
  18553. (defun org-combine-plists (&rest plists)
  18554. "Create a single property list from all plists in PLISTS.
  18555. The process starts by copying the first list, and then setting properties
  18556. from the other lists. Settings in the last list are the most significant
  18557. ones and overrule settings in the other lists."
  18558. (let ((rtn (copy-sequence (pop plists)))
  18559. p v ls)
  18560. (while plists
  18561. (setq ls (pop plists))
  18562. (while ls
  18563. (setq p (pop ls) v (pop ls))
  18564. (setq rtn (plist-put rtn p v))))
  18565. rtn))
  18566. (defun org-move-line-down (arg)
  18567. "Move the current line down. With prefix argument, move it past ARG lines."
  18568. (interactive "p")
  18569. (let ((col (current-column))
  18570. beg end pos)
  18571. (beginning-of-line 1) (setq beg (point))
  18572. (beginning-of-line 2) (setq end (point))
  18573. (beginning-of-line (+ 1 arg))
  18574. (setq pos (move-marker (make-marker) (point)))
  18575. (insert (delete-and-extract-region beg end))
  18576. (goto-char pos)
  18577. (org-move-to-column col)))
  18578. (defun org-move-line-up (arg)
  18579. "Move the current line up. With prefix argument, move it past ARG lines."
  18580. (interactive "p")
  18581. (let ((col (current-column))
  18582. beg end pos)
  18583. (beginning-of-line 1) (setq beg (point))
  18584. (beginning-of-line 2) (setq end (point))
  18585. (beginning-of-line (- arg))
  18586. (setq pos (move-marker (make-marker) (point)))
  18587. (insert (delete-and-extract-region beg end))
  18588. (goto-char pos)
  18589. (org-move-to-column col)))
  18590. (defun org-replace-escapes (string table)
  18591. "Replace %-escapes in STRING with values in TABLE.
  18592. TABLE is an association list with keys like \"%a\" and string values.
  18593. The sequences in STRING may contain normal field width and padding information,
  18594. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18595. so values can contain further %-escapes if they are define later in TABLE."
  18596. (let ((tbl (copy-alist table))
  18597. (case-fold-search nil)
  18598. (pchg 0)
  18599. e re rpl)
  18600. (while (setq e (pop tbl))
  18601. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18602. (when (and (cdr e) (string-match re (cdr e)))
  18603. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18604. (safe "SREF"))
  18605. (add-text-properties 0 3 (list 'sref sref) safe)
  18606. (setcdr e (replace-match safe t t (cdr e)))))
  18607. (while (string-match re string)
  18608. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18609. (cdr e)))
  18610. (setq string (replace-match rpl t t string))))
  18611. (while (setq pchg (next-property-change pchg string))
  18612. (let ((sref (get-text-property pchg 'sref string)))
  18613. (when (and sref (string-match "SREF" string pchg))
  18614. (setq string (replace-match sref t t string)))))
  18615. string))
  18616. (defun org-sublist (list start end)
  18617. "Return a section of LIST, from START to END.
  18618. Counting starts at 1."
  18619. (let (rtn (c start))
  18620. (setq list (nthcdr (1- start) list))
  18621. (while (and list (<= c end))
  18622. (push (pop list) rtn)
  18623. (setq c (1+ c)))
  18624. (nreverse rtn)))
  18625. (defun org-find-base-buffer-visiting (file)
  18626. "Like `find-buffer-visiting' but always return the base buffer and
  18627. not an indirect buffer."
  18628. (let ((buf (or (get-file-buffer file)
  18629. (find-buffer-visiting file))))
  18630. (if buf
  18631. (or (buffer-base-buffer buf) buf)
  18632. nil)))
  18633. (defun org-image-file-name-regexp (&optional extensions)
  18634. "Return regexp matching the file names of images.
  18635. If EXTENSIONS is given, only match these."
  18636. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18637. (image-file-name-regexp)
  18638. (let ((image-file-name-extensions
  18639. (or extensions
  18640. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18641. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18642. (concat "\\."
  18643. (regexp-opt (nconc (mapcar 'upcase
  18644. image-file-name-extensions)
  18645. image-file-name-extensions)
  18646. t)
  18647. "\\'"))))
  18648. (defun org-file-image-p (file &optional extensions)
  18649. "Return non-nil if FILE is an image."
  18650. (save-match-data
  18651. (string-match (org-image-file-name-regexp extensions) file)))
  18652. (defun org-get-cursor-date ()
  18653. "Return the date at cursor in as a time.
  18654. This works in the calendar and in the agenda, anywhere else it just
  18655. returns the current time."
  18656. (let (date day defd)
  18657. (cond
  18658. ((eq major-mode 'calendar-mode)
  18659. (setq date (calendar-cursor-to-date)
  18660. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18661. ((eq major-mode 'org-agenda-mode)
  18662. (setq day (get-text-property (point) 'day))
  18663. (if day
  18664. (setq date (calendar-gregorian-from-absolute day)
  18665. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18666. (nth 2 date))))))
  18667. (or defd (current-time))))
  18668. (defvar org-agenda-action-marker (make-marker)
  18669. "Marker pointing to the entry for the next agenda action.")
  18670. (defun org-mark-entry-for-agenda-action ()
  18671. "Mark the current entry as target of an agenda action.
  18672. Agenda actions are actions executed from the agenda with the key `k',
  18673. which make use of the date at the cursor."
  18674. (interactive)
  18675. (move-marker org-agenda-action-marker
  18676. (save-excursion (org-back-to-heading t) (point))
  18677. (current-buffer))
  18678. (message
  18679. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18680. (defun org-mark-subtree ()
  18681. "Mark the current subtree.
  18682. This puts point at the start of the current subtree, and mark at the end.
  18683. If point is in an inline task, mark that task instead."
  18684. (interactive)
  18685. (let ((inline-task-p
  18686. (and (featurep 'org-inlinetask)
  18687. (org-inlinetask-in-task-p)))
  18688. (beg))
  18689. ;; Get beginning of subtree
  18690. (cond
  18691. (inline-task-p (org-inlinetask-goto-beginning))
  18692. ((org-at-heading-p) (beginning-of-line))
  18693. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18694. (setq beg (point))
  18695. ;; Get end of it
  18696. (if inline-task-p
  18697. (org-inlinetask-goto-end)
  18698. (org-end-of-subtree))
  18699. ;; Mark zone
  18700. (push-mark (point) nil t)
  18701. (goto-char beg)))
  18702. ;;; Paragraph filling stuff.
  18703. ;; We want this to be just right, so use the full arsenal.
  18704. (defun org-indent-line-function ()
  18705. "Indent line depending on context."
  18706. (interactive)
  18707. (let* ((pos (point))
  18708. (itemp (org-at-item-p))
  18709. (case-fold-search t)
  18710. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18711. (inline-task-p (and (featurep 'org-inlinetask)
  18712. (org-inlinetask-in-task-p)))
  18713. (inline-re (and inline-task-p
  18714. (org-inlinetask-outline-regexp)))
  18715. column)
  18716. (beginning-of-line 1)
  18717. (cond
  18718. ;; Comments
  18719. ((looking-at "# ") (setq column 0))
  18720. ;; Headings
  18721. ((looking-at org-outline-regexp) (setq column 0))
  18722. ;; Included files
  18723. ((looking-at "#\\+include:") (setq column 0))
  18724. ;; Footnote definition
  18725. ((looking-at org-footnote-definition-re) (setq column 0))
  18726. ;; Literal examples
  18727. ((looking-at "[ \t]*:\\( \\|$\\)")
  18728. (setq column (org-get-indentation))) ; do nothing
  18729. ;; Lists
  18730. ((ignore-errors (goto-char (org-in-item-p)))
  18731. (setq column (if itemp
  18732. (org-get-indentation)
  18733. (org-list-item-body-column (point))))
  18734. (goto-char pos))
  18735. ;; Drawers
  18736. ((and (looking-at "[ \t]*:END:")
  18737. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18738. (save-excursion
  18739. (goto-char (1- (match-beginning 1)))
  18740. (setq column (current-column))))
  18741. ;; Special blocks
  18742. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18743. (save-excursion
  18744. (re-search-backward
  18745. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18746. (setq column (org-get-indentation (match-string 0))))
  18747. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18748. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18749. (save-excursion
  18750. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18751. (setq column
  18752. (cond ((equal (downcase (match-string 1)) "src")
  18753. ;; src blocks: let `org-edit-src-exit' handle them
  18754. (org-get-indentation))
  18755. ((equal (downcase (match-string 1)) "example")
  18756. (max (org-get-indentation)
  18757. (org-get-indentation (match-string 0))))
  18758. (t
  18759. (org-get-indentation (match-string 0))))))
  18760. ;; This line has nothing special, look at the previous relevant
  18761. ;; line to compute indentation
  18762. (t
  18763. (beginning-of-line 0)
  18764. (while (and (not (bobp))
  18765. (not (looking-at org-drawer-regexp))
  18766. ;; When point started in an inline task, do not move
  18767. ;; above task starting line.
  18768. (not (and inline-task-p (looking-at inline-re)))
  18769. ;; Skip drawers, blocks, empty lines, verbatim,
  18770. ;; comments, tables, footnotes definitions, lists,
  18771. ;; inline tasks.
  18772. (or (and (looking-at "[ \t]*:END:")
  18773. (re-search-backward org-drawer-regexp nil t))
  18774. (and (looking-at "[ \t]*#\\+end_")
  18775. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18776. (looking-at "[ \t]*[\n:#|]")
  18777. (looking-at org-footnote-definition-re)
  18778. (and (ignore-errors (goto-char (org-in-item-p)))
  18779. (goto-char
  18780. (org-list-get-top-point (org-list-struct))))
  18781. (and (not inline-task-p)
  18782. (featurep 'org-inlinetask)
  18783. (org-inlinetask-in-task-p)
  18784. (or (org-inlinetask-goto-beginning) t))))
  18785. (beginning-of-line 0))
  18786. (cond
  18787. ;; There was an heading above.
  18788. ((looking-at "\\*+[ \t]+")
  18789. (if (not org-adapt-indentation)
  18790. (setq column 0)
  18791. (goto-char (match-end 0))
  18792. (setq column (current-column))))
  18793. ;; A drawer had started and is unfinished
  18794. ((looking-at org-drawer-regexp)
  18795. (goto-char (1- (match-beginning 1)))
  18796. (setq column (current-column)))
  18797. ;; Else, nothing noticeable found: get indentation and go on.
  18798. (t (setq column (org-get-indentation))))))
  18799. ;; Now apply indentation and move cursor accordingly
  18800. (goto-char pos)
  18801. (if (<= (current-column) (current-indentation))
  18802. (org-indent-line-to column)
  18803. (save-excursion (org-indent-line-to column)))
  18804. ;; Special polishing for properties, see `org-property-format'
  18805. (setq column (current-column))
  18806. (beginning-of-line 1)
  18807. (if (looking-at
  18808. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18809. (replace-match (concat (match-string 1)
  18810. (format org-property-format
  18811. (match-string 2) (match-string 3)))
  18812. t t))
  18813. (org-move-to-column column)))
  18814. (defun org-indent-drawer ()
  18815. "Indent the drawer at point."
  18816. (interactive)
  18817. (let ((p (point))
  18818. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18819. (match-end 0)))
  18820. (folded
  18821. (save-excursion
  18822. (end-of-line)
  18823. (when (overlays-at (point))
  18824. (member 'invisible (overlay-properties
  18825. (car (overlays-at (point)))))))))
  18826. (when folded (org-cycle))
  18827. (indent-for-tab-command)
  18828. (while (and (move-beginning-of-line 2) (< (point) e))
  18829. (indent-for-tab-command))
  18830. (goto-char p)
  18831. (when folded (org-cycle)))
  18832. (message "Drawer at point indented"))
  18833. (defun org-indent-block ()
  18834. "Indent the block at point."
  18835. (interactive)
  18836. (let ((p (point))
  18837. (case-fold-search t)
  18838. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18839. (match-end 0)))
  18840. (folded
  18841. (save-excursion
  18842. (end-of-line)
  18843. (when (overlays-at (point))
  18844. (member 'invisible (overlay-properties
  18845. (car (overlays-at (point)))))))))
  18846. (when folded (org-cycle))
  18847. (indent-for-tab-command)
  18848. (while (and (move-beginning-of-line 2) (< (point) e))
  18849. (indent-for-tab-command))
  18850. (goto-char p)
  18851. (when folded (org-cycle)))
  18852. (message "Block at point indented"))
  18853. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18854. "Variable to store copy of `adaptive-fill-regexp'.
  18855. Since `adaptive-fill-regexp' is set to never match, we need to
  18856. store a backup of its value before entering `org-mode' so that
  18857. the functionality can be provided as a fall-back.")
  18858. (defun org-set-autofill-regexps ()
  18859. (interactive)
  18860. ;; In the paragraph separator we include headlines, because filling
  18861. ;; text in a line directly attached to a headline would otherwise
  18862. ;; fill the headline as well.
  18863. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18864. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18865. ;; The paragraph starter includes hand-formatted lists.
  18866. (org-set-local
  18867. 'paragraph-start
  18868. (concat
  18869. "\f" "\\|"
  18870. "[ ]*$" "\\|"
  18871. org-outline-regexp "\\|"
  18872. "[ \t]*#" "\\|"
  18873. (org-item-re) "\\|"
  18874. "[ \t]*[:|]" "\\|"
  18875. "\\$\\$" "\\|"
  18876. "\\\\\\(begin\\|end\\|[][]\\)"))
  18877. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18878. ;; But only if the user has not turned off tables or fixed-width regions
  18879. (org-set-local
  18880. 'auto-fill-inhibit-regexp
  18881. (concat org-outline-regexp
  18882. "\\|#\\+"
  18883. "\\|[ \t]*" org-keyword-time-regexp
  18884. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18885. (concat
  18886. "\\|[ \t]*["
  18887. (if org-enable-table-editor "|" "")
  18888. (if org-enable-fixed-width-editor ":" "")
  18889. "]"))))
  18890. ;; We use our own fill-paragraph function, to make sure that tables
  18891. ;; and fixed-width regions are not wrapped. That function will pass
  18892. ;; through to `fill-paragraph' when appropriate.
  18893. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18894. ;; Prevent auto-fill from inserting unwanted new items.
  18895. (if (boundp 'fill-nobreak-predicate)
  18896. (org-set-local
  18897. 'fill-nobreak-predicate
  18898. (org-uniquify
  18899. (append fill-nobreak-predicate
  18900. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  18901. ;; Adaptive filling: To get full control, first make sure that
  18902. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18903. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18904. (org-set-local 'org-adaptive-fill-regexp-backup
  18905. adaptive-fill-regexp))
  18906. (org-set-local 'adaptive-fill-regexp "\000")
  18907. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18908. (org-set-local 'adaptive-fill-function
  18909. 'org-adaptive-fill-function)
  18910. (org-set-local
  18911. 'align-mode-rules-list
  18912. '((org-in-buffer-settings
  18913. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18914. (modes . '(org-mode))))))
  18915. (defun org-fill-item-nobreak-p ()
  18916. "Non-nil when a line break at point would insert a new item."
  18917. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18918. (defun org-fill-line-break-nobreak-p ()
  18919. "Non-nil when a line break at point would create an Org line break."
  18920. (save-excursion
  18921. (skip-chars-backward "[ \t]")
  18922. (skip-chars-backward "\\\\")
  18923. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  18924. (defun org-fill-paragraph (&optional justify)
  18925. "Re-align a table, pass through to fill-paragraph if no table."
  18926. (let ((table-p (org-at-table-p))
  18927. (table.el-p (org-at-table.el-p))
  18928. (itemp (org-in-item-p)))
  18929. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18930. (save-excursion (goto-char (point-at-bol))
  18931. (looking-at org-outline-regexp)))
  18932. t) ; skip headlines
  18933. (table.el-p t) ; skip table.el tables
  18934. (table-p (org-table-align) t) ; align Org tables
  18935. (itemp ; align text in items
  18936. (let* ((struct (save-excursion (goto-char itemp)
  18937. (org-list-struct)))
  18938. (parents (org-list-parents-alist struct))
  18939. (children (org-list-get-children itemp struct parents))
  18940. beg end prev next prefix)
  18941. ;; Determine in which part of item point is: before
  18942. ;; first child, after last child, between two
  18943. ;; sub-lists, or simply in item if there's no child.
  18944. (cond
  18945. ((not children)
  18946. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18947. beg itemp
  18948. end (org-list-get-item-end itemp struct)))
  18949. ((< (point) (setq next (car children)))
  18950. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18951. beg itemp
  18952. end next))
  18953. ((> (point) (setq prev (car (last children))))
  18954. (setq beg (org-list-get-item-end prev struct)
  18955. end (org-list-get-item-end itemp struct)
  18956. prefix (save-excursion
  18957. (goto-char beg)
  18958. (skip-chars-forward " \t")
  18959. (make-string (current-column) ?\ ))))
  18960. (t (catch 'exit
  18961. (while (setq next (pop children))
  18962. (if (> (point) next)
  18963. (setq prev next)
  18964. (setq beg (org-list-get-item-end prev struct)
  18965. end next
  18966. prefix (save-excursion
  18967. (goto-char beg)
  18968. (skip-chars-forward " \t")
  18969. (make-string (current-column) ?\ )))
  18970. (throw 'exit nil))))))
  18971. ;; Use `fill-paragraph' with buffer narrowed to item
  18972. ;; without any child, and with our computed PREFIX.
  18973. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18974. (save-restriction
  18975. (narrow-to-region beg end)
  18976. (save-excursion (fill-paragraph justify)))) t))
  18977. ;; Special case where point is not in a list but is on
  18978. ;; a paragraph adjacent to a list: make sure this paragraph
  18979. ;; doesn't get merged with the end of the list by narrowing
  18980. ;; buffer first.
  18981. ((save-excursion (forward-paragraph -1)
  18982. (setq itemp (org-in-item-p)))
  18983. (let ((struct (save-excursion (goto-char itemp)
  18984. (org-list-struct))))
  18985. (save-restriction
  18986. (narrow-to-region (org-list-get-bottom-point struct)
  18987. (save-excursion (forward-paragraph 1)
  18988. (point)))
  18989. (fill-paragraph justify) t)))
  18990. ;; Don't fill schedule/deadline line before a paragraph
  18991. ((save-excursion (forward-paragraph -1)
  18992. (or (looking-at (concat "^[^\n]*" org-scheduled-regexp ".*$"))
  18993. (looking-at (concat "^[^\n]*" org-deadline-regexp ".*$"))))
  18994. (save-restriction
  18995. (narrow-to-region (1+ (match-end 0))
  18996. (save-excursion (forward-paragraph 1) (point)))
  18997. (fill-paragraph justify) t))
  18998. ;; Else simply call `fill-paragraph'.
  18999. (t nil))))
  19000. ;; For reference, this is the default value of adaptive-fill-regexp
  19001. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  19002. (defun org-adaptive-fill-function ()
  19003. "Return a fill prefix for org-mode files."
  19004. (let (itemp)
  19005. (save-excursion
  19006. (cond
  19007. ;; Comment line
  19008. ((looking-at "#[ \t]+")
  19009. (match-string-no-properties 0))
  19010. ;; Plain list item
  19011. ((org-at-item-p)
  19012. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  19013. ;; Point is in a list after `backward-paragraph': original
  19014. ;; point wasn't in the list, or filling would have been taken
  19015. ;; care of by `org-auto-fill-function', but the list and the
  19016. ;; real paragraph are not separated by a blank line. Thus, move
  19017. ;; point after the list to go back to real paragraph and
  19018. ;; determine fill-prefix.
  19019. ((setq itemp (org-in-item-p))
  19020. (goto-char itemp)
  19021. (let* ((struct (org-list-struct))
  19022. (bottom (org-list-get-bottom-point struct)))
  19023. (goto-char bottom)
  19024. (make-string (org-get-indentation) ?\ )))
  19025. ;; Other text
  19026. ((looking-at org-adaptive-fill-regexp-backup)
  19027. (match-string-no-properties 0))))))
  19028. (defun org-auto-fill-function ()
  19029. "Auto-fill function."
  19030. (let (itemp prefix)
  19031. ;; When in a list, compute an appropriate fill-prefix and make
  19032. ;; sure it will be used by `do-auto-fill'.
  19033. (if (setq itemp (org-in-item-p))
  19034. (progn
  19035. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  19036. (flet ((fill-context-prefix (from to &optional flr) prefix))
  19037. (do-auto-fill)))
  19038. ;; Else just use `do-auto-fill'.
  19039. (do-auto-fill))))
  19040. ;;; Other stuff.
  19041. (defun org-toggle-fixed-width-section (arg)
  19042. "Toggle the fixed-width export.
  19043. If there is no active region, the QUOTE keyword at the current headline is
  19044. inserted or removed. When present, it causes the text between this headline
  19045. and the next to be exported as fixed-width text, and unmodified.
  19046. If there is an active region, this command adds or removes a colon as the
  19047. first character of this line. If the first character of a line is a colon,
  19048. this line is also exported in fixed-width font."
  19049. (interactive "P")
  19050. (let* ((cc 0)
  19051. (regionp (org-region-active-p))
  19052. (beg (if regionp (region-beginning) (point)))
  19053. (end (if regionp (region-end)))
  19054. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19055. (case-fold-search nil)
  19056. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19057. off)
  19058. (if regionp
  19059. (save-excursion
  19060. (goto-char beg)
  19061. (setq cc (current-column))
  19062. (beginning-of-line 1)
  19063. (setq off (looking-at re))
  19064. (while (> nlines 0)
  19065. (setq nlines (1- nlines))
  19066. (beginning-of-line 1)
  19067. (cond
  19068. (arg
  19069. (org-move-to-column cc t)
  19070. (insert ": \n")
  19071. (forward-line -1))
  19072. ((and off (looking-at re))
  19073. (replace-match "" t t nil 1))
  19074. ((not off) (org-move-to-column cc t) (insert ": ")))
  19075. (forward-line 1)))
  19076. (save-excursion
  19077. (org-back-to-heading)
  19078. (cond
  19079. ((looking-at (format org-heading-keyword-regexp-format
  19080. org-quote-string))
  19081. (goto-char (match-end 1))
  19082. (looking-at (concat " +" org-quote-string))
  19083. (replace-match "" t t)
  19084. (when (eolp) (insert " ")))
  19085. ((looking-at org-outline-regexp)
  19086. (goto-char (match-end 0))
  19087. (insert org-quote-string " ")))))))
  19088. (defun org-reftex-citation ()
  19089. "Use reftex-citation to insert a citation into the buffer.
  19090. This looks for a line like
  19091. #+BIBLIOGRAPHY: foo plain option:-d
  19092. and derives from it that foo.bib is the bibliography file relevant
  19093. for this document. It then installs the necessary environment for RefTeX
  19094. to work in this buffer and calls `reftex-citation' to insert a citation
  19095. into the buffer.
  19096. Export of such citations to both LaTeX and HTML is handled by the contributed
  19097. package org-exp-bibtex by Taru Karttunen."
  19098. (interactive)
  19099. (let ((reftex-docstruct-symbol 'rds)
  19100. (reftex-cite-format "\\cite{%l}")
  19101. rds bib)
  19102. (save-excursion
  19103. (save-restriction
  19104. (widen)
  19105. (let ((case-fold-search t)
  19106. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19107. (if (not (save-excursion
  19108. (or (re-search-forward re nil t)
  19109. (re-search-backward re nil t))))
  19110. (error "No bibliography defined in file")
  19111. (setq bib (concat (match-string 1) ".bib")
  19112. rds (list (list 'bib bib)))))))
  19113. (call-interactively 'reftex-citation)))
  19114. ;;;; Functions extending outline functionality
  19115. (defun org-beginning-of-line (&optional arg)
  19116. "Go to the beginning of the current line. If that is invisible, continue
  19117. to a visible line beginning. This makes the function of C-a more intuitive.
  19118. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19119. first attempt, and only move to after the tags when the cursor is already
  19120. beyond the end of the headline."
  19121. (interactive "P")
  19122. (let ((pos (point))
  19123. (special (if (consp org-special-ctrl-a/e)
  19124. (car org-special-ctrl-a/e)
  19125. org-special-ctrl-a/e))
  19126. refpos)
  19127. (if (org-bound-and-true-p line-move-visual)
  19128. (beginning-of-visual-line 1)
  19129. (beginning-of-line 1))
  19130. (if (and arg (fboundp 'move-beginning-of-line))
  19131. (call-interactively 'move-beginning-of-line)
  19132. (if (bobp)
  19133. nil
  19134. (backward-char 1)
  19135. (if (org-truely-invisible-p)
  19136. (while (and (not (bobp)) (org-truely-invisible-p))
  19137. (backward-char 1)
  19138. (beginning-of-line 1))
  19139. (forward-char 1))))
  19140. (when special
  19141. (cond
  19142. ((and (looking-at org-complex-heading-regexp)
  19143. (= (char-after (match-end 1)) ?\ ))
  19144. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19145. (point-at-eol)))
  19146. (goto-char
  19147. (if (eq special t)
  19148. (cond ((> pos refpos) refpos)
  19149. ((= pos (point)) refpos)
  19150. (t (point)))
  19151. (cond ((> pos (point)) (point))
  19152. ((not (eq last-command this-command)) (point))
  19153. (t refpos)))))
  19154. ((org-at-item-p)
  19155. ;; Being at an item and not looking at an the item means point
  19156. ;; was previously moved to beginning of a visual line, which
  19157. ;; doesn't contain the item. Therefore, do nothing special,
  19158. ;; just stay here.
  19159. (when (looking-at org-list-full-item-re)
  19160. ;; Set special position at first white space character after
  19161. ;; bullet, and check-box, if any.
  19162. (let ((after-bullet
  19163. (let ((box (match-end 3)))
  19164. (if (not box) (match-end 1)
  19165. (let ((after (char-after box)))
  19166. (if (and after (= after ? )) (1+ box) box))))))
  19167. ;; Special case: Move point to special position when
  19168. ;; currently after it or at beginning of line.
  19169. (if (eq special t)
  19170. (when (or (> pos after-bullet) (= (point) pos))
  19171. (goto-char after-bullet))
  19172. ;; Reversed case: Move point to special position when
  19173. ;; point was already at beginning of line and command is
  19174. ;; repeated.
  19175. (when (and (= (point) pos) (eq last-command this-command))
  19176. (goto-char after-bullet))))))))
  19177. (org-no-warnings
  19178. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19179. (defun org-end-of-line (&optional arg)
  19180. "Go to the end of the line.
  19181. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19182. first attempt, and only move to after the tags when the cursor is already
  19183. beyond the end of the headline."
  19184. (interactive "P")
  19185. (let ((special (if (consp org-special-ctrl-a/e)
  19186. (cdr org-special-ctrl-a/e)
  19187. org-special-ctrl-a/e)))
  19188. (cond
  19189. ((or (not special) arg
  19190. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19191. (call-interactively
  19192. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19193. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19194. (t 'end-of-line))))
  19195. ((org-at-heading-p)
  19196. (let ((pos (point)))
  19197. (beginning-of-line 1)
  19198. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19199. (if (eq special t)
  19200. (if (or (< pos (match-beginning 1))
  19201. (= pos (match-end 0)))
  19202. (goto-char (match-beginning 1))
  19203. (goto-char (match-end 0)))
  19204. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19205. (goto-char (match-end 0))
  19206. (goto-char (match-beginning 1))))
  19207. (call-interactively (if (fboundp 'move-end-of-line)
  19208. 'move-end-of-line
  19209. 'end-of-line)))))
  19210. ((org-at-drawer-p)
  19211. (move-end-of-line 1)
  19212. (when (overlays-at (1- (point))) (backward-char 1)))
  19213. ;; At an item: Move before any hidden text.
  19214. (t (call-interactively
  19215. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19216. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19217. (t 'end-of-line)))))
  19218. (org-no-warnings
  19219. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19220. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19221. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19222. (defun org-backward-sentence (&optional arg)
  19223. "Go to beginning of sentence, or beginning of table field.
  19224. This will call `backward-sentence' or `org-table-beginning-of-field',
  19225. depending on context."
  19226. (interactive "P")
  19227. (cond
  19228. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19229. (t (call-interactively 'backward-sentence))))
  19230. (defun org-forward-sentence (&optional arg)
  19231. "Go to end of sentence, or end of table field.
  19232. This will call `forward-sentence' or `org-table-end-of-field',
  19233. depending on context."
  19234. (interactive "P")
  19235. (cond
  19236. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19237. (t (call-interactively 'forward-sentence))))
  19238. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19239. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19240. (defun org-kill-line (&optional arg)
  19241. "Kill line, to tags or end of line."
  19242. (interactive "P")
  19243. (cond
  19244. ((or (not org-special-ctrl-k)
  19245. (bolp)
  19246. (not (org-at-heading-p)))
  19247. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19248. org-ctrl-k-protect-subtree)
  19249. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19250. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19251. (error "C-k aborted - would kill hidden subtree")))
  19252. (call-interactively
  19253. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19254. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19255. (kill-region (point) (match-beginning 1))
  19256. (org-set-tags nil t))
  19257. (t (kill-region (point) (point-at-eol)))))
  19258. (define-key org-mode-map "\C-k" 'org-kill-line)
  19259. (defun org-yank (&optional arg)
  19260. "Yank. If the kill is a subtree, treat it specially.
  19261. This command will look at the current kill and check if is a single
  19262. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19263. cursor is at the beginning of a line or after the stars of a currently
  19264. empty headline, then the yank is handled specially. How exactly depends
  19265. on the value of the following variables, both set by default.
  19266. org-yank-folded-subtrees
  19267. When set, the subtree(s) will be folded after insertion, but only
  19268. if doing so would now swallow text after the yanked text.
  19269. org-yank-adjusted-subtrees
  19270. When set, the subtree will be promoted or demoted in order to
  19271. fit into the local outline tree structure, which means that the level
  19272. will be adjusted so that it becomes the smaller one of the two
  19273. *visible* surrounding headings.
  19274. Any prefix to this command will cause `yank' to be called directly with
  19275. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19276. will just
  19277. plainly yank the text as it is.
  19278. \[1] The test checks if the first non-white line is a heading
  19279. and if there are no other headings with fewer stars."
  19280. (interactive "P")
  19281. (org-yank-generic 'yank arg))
  19282. (defun org-yank-generic (command arg)
  19283. "Perform some yank-like command.
  19284. This function implements the behavior described in the `org-yank'
  19285. documentation. However, it has been generalized to work for any
  19286. interactive command with similar behavior."
  19287. ;; pretend to be command COMMAND
  19288. (setq this-command command)
  19289. (if arg
  19290. (call-interactively command)
  19291. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19292. (and (org-kill-is-subtree-p)
  19293. (or (bolp)
  19294. (and (looking-at "[ \t]*$")
  19295. (string-match
  19296. "\\`\\*+\\'"
  19297. (buffer-substring (point-at-bol) (point)))))))
  19298. swallowp)
  19299. (cond
  19300. ((and subtreep org-yank-folded-subtrees)
  19301. (let ((beg (point))
  19302. end)
  19303. (if (and subtreep org-yank-adjusted-subtrees)
  19304. (org-paste-subtree nil nil 'for-yank)
  19305. (call-interactively command))
  19306. (setq end (point))
  19307. (goto-char beg)
  19308. (when (and (bolp) subtreep
  19309. (not (setq swallowp
  19310. (org-yank-folding-would-swallow-text beg end))))
  19311. (org-with-limited-levels
  19312. (or (looking-at org-outline-regexp)
  19313. (re-search-forward org-outline-regexp-bol end t))
  19314. (while (and (< (point) end) (looking-at org-outline-regexp))
  19315. (hide-subtree)
  19316. (org-cycle-show-empty-lines 'folded)
  19317. (condition-case nil
  19318. (outline-forward-same-level 1)
  19319. (error (goto-char end))))))
  19320. (when swallowp
  19321. (message
  19322. "Inserted text not folded because that would swallow text"))
  19323. (goto-char end)
  19324. (skip-chars-forward " \t\n\r")
  19325. (beginning-of-line 1)
  19326. (push-mark beg 'nomsg)))
  19327. ((and subtreep org-yank-adjusted-subtrees)
  19328. (let ((beg (point-at-bol)))
  19329. (org-paste-subtree nil nil 'for-yank)
  19330. (push-mark beg 'nomsg)))
  19331. (t
  19332. (call-interactively command))))))
  19333. (defun org-yank-folding-would-swallow-text (beg end)
  19334. "Would hide-subtree at BEG swallow any text after END?"
  19335. (let (level)
  19336. (org-with-limited-levels
  19337. (save-excursion
  19338. (goto-char beg)
  19339. (when (or (looking-at org-outline-regexp)
  19340. (re-search-forward org-outline-regexp-bol end t))
  19341. (setq level (org-outline-level)))
  19342. (goto-char end)
  19343. (skip-chars-forward " \t\r\n\v\f")
  19344. (if (or (eobp)
  19345. (and (bolp) (looking-at org-outline-regexp)
  19346. (<= (org-outline-level) level)))
  19347. nil ; Nothing would be swallowed
  19348. t))))) ; something would swallow
  19349. (define-key org-mode-map "\C-y" 'org-yank)
  19350. (defun org-truely-invisible-p ()
  19351. "Check if point is at a character currently not visible.
  19352. This version does not only check the character property, but also
  19353. `visible-mode'."
  19354. ;; Early versions of noutline don't have `outline-invisible-p'.
  19355. (if (org-bound-and-true-p visible-mode)
  19356. nil
  19357. (outline-invisible-p)))
  19358. (defun org-invisible-p2 ()
  19359. "Check if point is at a character currently not visible."
  19360. (save-excursion
  19361. (if (and (eolp) (not (bobp))) (backward-char 1))
  19362. ;; Early versions of noutline don't have `outline-invisible-p'.
  19363. (outline-invisible-p)))
  19364. (defun org-back-to-heading (&optional invisible-ok)
  19365. "Call `outline-back-to-heading', but provide a better error message."
  19366. (condition-case nil
  19367. (outline-back-to-heading invisible-ok)
  19368. (error (error "Before first headline at position %d in buffer %s"
  19369. (point) (current-buffer)))))
  19370. (defun org-beginning-of-defun ()
  19371. "Go to the beginning of the subtree, i.e. back to the heading."
  19372. (org-back-to-heading))
  19373. (defun org-end-of-defun ()
  19374. "Go to the end of the subtree."
  19375. (org-end-of-subtree nil t))
  19376. (defun org-before-first-heading-p ()
  19377. "Before first heading?"
  19378. (save-excursion
  19379. (end-of-line)
  19380. (null (re-search-backward org-outline-regexp-bol nil t))))
  19381. (defun org-at-heading-p (&optional ignored)
  19382. (outline-on-heading-p t))
  19383. ;; Compatibility alias with Org versions < 7.8.03
  19384. (defalias 'org-on-heading-p 'org-at-heading-p)
  19385. (defun org-at-drawer-p nil
  19386. "Is cursor at a drawer keyword?"
  19387. (save-excursion
  19388. (move-beginning-of-line 1)
  19389. (looking-at org-drawer-regexp)))
  19390. (defun org-at-block-p nil
  19391. "Is cursor at a block keyword?"
  19392. (save-excursion
  19393. (move-beginning-of-line 1)
  19394. (looking-at org-block-regexp)))
  19395. (defun org-point-at-end-of-empty-headline ()
  19396. "If point is at the end of an empty headline, return t, else nil.
  19397. If the heading only contains a TODO keyword, it is still still considered
  19398. empty."
  19399. (and (looking-at "[ \t]*$")
  19400. (when org-todo-line-regexp
  19401. (save-excursion
  19402. (beginning-of-line 1)
  19403. (let ((case-fold-search nil))
  19404. (looking-at org-todo-line-regexp)
  19405. (string= (match-string 3) ""))))))
  19406. (defun org-at-heading-or-item-p ()
  19407. (or (org-at-heading-p) (org-at-item-p)))
  19408. (defun org-at-target-p ()
  19409. (or (org-in-regexp org-radio-target-regexp)
  19410. (org-in-regexp org-target-regexp)))
  19411. ;; Compatibility alias with Org versions < 7.8.03
  19412. (defalias 'org-on-target-p 'org-at-target-p)
  19413. (defun org-up-heading-all (arg)
  19414. "Move to the heading line of which the present line is a subheading.
  19415. This function considers both visible and invisible heading lines.
  19416. With argument, move up ARG levels."
  19417. (if (fboundp 'outline-up-heading-all)
  19418. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19419. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19420. (defun org-up-heading-safe ()
  19421. "Move to the heading line of which the present line is a subheading.
  19422. This version will not throw an error. It will return the level of the
  19423. headline found, or nil if no higher level is found.
  19424. Also, this function will be a lot faster than `outline-up-heading',
  19425. because it relies on stars being the outline starters. This can really
  19426. make a significant difference in outlines with very many siblings."
  19427. (let (start-level re)
  19428. (org-back-to-heading t)
  19429. (setq start-level (funcall outline-level))
  19430. (if (equal start-level 1)
  19431. nil
  19432. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19433. (if (re-search-backward re nil t)
  19434. (funcall outline-level)))))
  19435. (defun org-first-sibling-p ()
  19436. "Is this heading the first child of its parents?"
  19437. (interactive)
  19438. (let ((re org-outline-regexp-bol)
  19439. level l)
  19440. (unless (org-at-heading-p t)
  19441. (error "Not at a heading"))
  19442. (setq level (funcall outline-level))
  19443. (save-excursion
  19444. (if (not (re-search-backward re nil t))
  19445. t
  19446. (setq l (funcall outline-level))
  19447. (< l level)))))
  19448. (defun org-goto-sibling (&optional previous)
  19449. "Goto the next sibling, even if it is invisible.
  19450. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19451. when a sibling was found. When none is found, return nil and don't
  19452. move point."
  19453. (let ((fun (if previous 're-search-backward 're-search-forward))
  19454. (pos (point))
  19455. (re org-outline-regexp-bol)
  19456. level l)
  19457. (when (condition-case nil (org-back-to-heading t) (error nil))
  19458. (setq level (funcall outline-level))
  19459. (catch 'exit
  19460. (or previous (forward-char 1))
  19461. (while (funcall fun re nil t)
  19462. (setq l (funcall outline-level))
  19463. (when (< l level) (goto-char pos) (throw 'exit nil))
  19464. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19465. (goto-char pos)
  19466. nil))))
  19467. (defun org-show-siblings ()
  19468. "Show all siblings of the current headline."
  19469. (save-excursion
  19470. (while (org-goto-sibling) (org-flag-heading nil)))
  19471. (save-excursion
  19472. (while (org-goto-sibling 'previous)
  19473. (org-flag-heading nil))))
  19474. (defun org-goto-first-child ()
  19475. "Goto the first child, even if it is invisible.
  19476. Return t when a child was found. Otherwise don't move point and
  19477. return nil."
  19478. (let (level (pos (point)) (re org-outline-regexp-bol))
  19479. (when (condition-case nil (org-back-to-heading t) (error nil))
  19480. (setq level (outline-level))
  19481. (forward-char 1)
  19482. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19483. (progn (goto-char (match-beginning 0)) t)
  19484. (goto-char pos) nil))))
  19485. (defun org-show-hidden-entry ()
  19486. "Show an entry where even the heading is hidden."
  19487. (save-excursion
  19488. (org-show-entry)))
  19489. (defun org-flag-heading (flag &optional entry)
  19490. "Flag the current heading. FLAG non-nil means make invisible.
  19491. When ENTRY is non-nil, show the entire entry."
  19492. (save-excursion
  19493. (org-back-to-heading t)
  19494. ;; Check if we should show the entire entry
  19495. (if entry
  19496. (progn
  19497. (org-show-entry)
  19498. (save-excursion
  19499. (and (outline-next-heading)
  19500. (org-flag-heading nil))))
  19501. (outline-flag-region (max (point-min) (1- (point)))
  19502. (save-excursion (outline-end-of-heading) (point))
  19503. flag))))
  19504. (defun org-get-next-sibling ()
  19505. "Move to next heading of the same level, and return point.
  19506. If there is no such heading, return nil.
  19507. This is like outline-next-sibling, but invisible headings are ok."
  19508. (let ((level (funcall outline-level)))
  19509. (outline-next-heading)
  19510. (while (and (not (eobp)) (> (funcall outline-level) level))
  19511. (outline-next-heading))
  19512. (if (or (eobp) (< (funcall outline-level) level))
  19513. nil
  19514. (point))))
  19515. (defun org-get-last-sibling ()
  19516. "Move to previous heading of the same level, and return point.
  19517. If there is no such heading, return nil."
  19518. (let ((opoint (point))
  19519. (level (funcall outline-level)))
  19520. (outline-previous-heading)
  19521. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19522. (while (and (> (funcall outline-level) level)
  19523. (not (bobp)))
  19524. (outline-previous-heading))
  19525. (if (< (funcall outline-level) level)
  19526. nil
  19527. (point)))))
  19528. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19529. ;; This contains an exact copy of the original function, but it uses
  19530. ;; `org-back-to-heading', to make it work also in invisible
  19531. ;; trees. And is uses an invisible-OK argument.
  19532. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19533. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19534. ;; with many children and grandchildren etc, this can be much faster
  19535. ;; than the outline version.
  19536. (org-back-to-heading invisible-OK)
  19537. (let ((first t)
  19538. (level (funcall outline-level)))
  19539. (if (and (derived-mode-p 'org-mode) (< level 1000))
  19540. ;; A true heading (not a plain list item), in Org-mode
  19541. ;; This means we can easily find the end by looking
  19542. ;; only for the right number of stars. Using a regexp to do
  19543. ;; this is so much faster than using a Lisp loop.
  19544. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19545. (forward-char 1)
  19546. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19547. ;; something else, do it the slow way
  19548. (while (and (not (eobp))
  19549. (or first (> (funcall outline-level) level)))
  19550. (setq first nil)
  19551. (outline-next-heading)))
  19552. (unless to-heading
  19553. (if (memq (preceding-char) '(?\n ?\^M))
  19554. (progn
  19555. ;; Go to end of line before heading
  19556. (forward-char -1)
  19557. (if (memq (preceding-char) '(?\n ?\^M))
  19558. ;; leave blank line before heading
  19559. (forward-char -1))))))
  19560. (point))
  19561. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19562. "Use Org version in org-mode, for dramatic speed-up."
  19563. (if (derived-mode-p 'org-mode)
  19564. (progn
  19565. (org-end-of-subtree nil t)
  19566. (unless (eobp) (backward-char 1)))
  19567. ad-do-it))
  19568. (defun org-end-of-meta-data-and-drawers ()
  19569. "Jump to the first text after meta data and drawers in the current entry.
  19570. This will move over empty lines, lines with planning time stamps,
  19571. clocking lines, and drawers."
  19572. (org-back-to-heading t)
  19573. (let ((end (save-excursion (outline-next-heading) (point)))
  19574. (re (concat "\\(" org-drawer-regexp "\\)"
  19575. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19576. (forward-line 1)
  19577. (while (re-search-forward re end t)
  19578. (if (not (match-end 1))
  19579. ;; empty or planning line
  19580. (forward-line 1)
  19581. ;; a drawer, find the end
  19582. (re-search-forward "^[ \t]*:END:" end 'move)
  19583. (forward-line 1)))
  19584. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19585. (point)))
  19586. (defun org-forward-same-level (arg &optional invisible-ok)
  19587. "Move forward to the arg'th subheading at same level as this one.
  19588. Stop at the first and last subheadings of a superior heading.
  19589. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19590. it wil also look at invisible ones."
  19591. (interactive "p")
  19592. (org-back-to-heading invisible-ok)
  19593. (org-at-heading-p)
  19594. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19595. (re (format "^\\*\\{1,%d\\} " level))
  19596. l)
  19597. (forward-char 1)
  19598. (while (> arg 0)
  19599. (while (and (re-search-forward re nil 'move)
  19600. (setq l (- (match-end 0) (match-beginning 0) 1))
  19601. (= l level)
  19602. (not invisible-ok)
  19603. (progn (backward-char 1) (outline-invisible-p)))
  19604. (if (< l level) (setq arg 1)))
  19605. (setq arg (1- arg)))
  19606. (beginning-of-line 1)))
  19607. (defun org-backward-same-level (arg &optional invisible-ok)
  19608. "Move backward to the arg'th subheading at same level as this one.
  19609. Stop at the first and last subheadings of a superior heading."
  19610. (interactive "p")
  19611. (org-back-to-heading)
  19612. (org-at-heading-p)
  19613. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19614. (re (format "^\\*\\{1,%d\\} " level))
  19615. l)
  19616. (while (> arg 0)
  19617. (while (and (re-search-backward re nil 'move)
  19618. (setq l (- (match-end 0) (match-beginning 0) 1))
  19619. (= l level)
  19620. (not invisible-ok)
  19621. (outline-invisible-p))
  19622. (if (< l level) (setq arg 1)))
  19623. (setq arg (1- arg)))))
  19624. (defun org-show-subtree ()
  19625. "Show everything after this heading at deeper levels."
  19626. (interactive)
  19627. (outline-flag-region
  19628. (point)
  19629. (save-excursion
  19630. (org-end-of-subtree t t))
  19631. nil))
  19632. (defun org-show-entry ()
  19633. "Show the body directly following this heading.
  19634. Show the heading too, if it is currently invisible."
  19635. (interactive)
  19636. (save-excursion
  19637. (condition-case nil
  19638. (progn
  19639. (org-back-to-heading t)
  19640. (outline-flag-region
  19641. (max (point-min) (1- (point)))
  19642. (save-excursion
  19643. (if (re-search-forward
  19644. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19645. (match-beginning 1)
  19646. (point-max)))
  19647. nil)
  19648. (org-cycle-hide-drawers 'children))
  19649. (error nil))))
  19650. (defun org-make-options-regexp (kwds &optional extra)
  19651. "Make a regular expression for keyword lines."
  19652. (concat
  19653. "^"
  19654. "#?[ \t]*\\+\\("
  19655. (mapconcat 'regexp-quote kwds "\\|")
  19656. (if extra (concat "\\|" extra))
  19657. "\\):[ \t]*"
  19658. "\\(.*\\)"))
  19659. ;; Make isearch reveal the necessary context
  19660. (defun org-isearch-end ()
  19661. "Reveal context after isearch exits."
  19662. (when isearch-success ; only if search was successful
  19663. (if (featurep 'xemacs)
  19664. ;; Under XEmacs, the hook is run in the correct place,
  19665. ;; we directly show the context.
  19666. (org-show-context 'isearch)
  19667. ;; In Emacs the hook runs *before* restoring the overlays.
  19668. ;; So we have to use a one-time post-command-hook to do this.
  19669. ;; (Emacs 22 has a special variable, see function `org-mode')
  19670. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19671. isearch-mode-end-hook-quit)
  19672. ;; Only when the isearch was not quitted.
  19673. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19674. 'append 'local)))))
  19675. (defun org-isearch-post-command ()
  19676. "Remove self from hook, and show context."
  19677. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19678. (org-show-context 'isearch))
  19679. ;;;; Integration with and fixes for other packages
  19680. ;;; Imenu support
  19681. (defvar org-imenu-markers nil
  19682. "All markers currently used by Imenu.")
  19683. (make-variable-buffer-local 'org-imenu-markers)
  19684. (defun org-imenu-new-marker (&optional pos)
  19685. "Return a new marker for use by Imenu, and remember the marker."
  19686. (let ((m (make-marker)))
  19687. (move-marker m (or pos (point)))
  19688. (push m org-imenu-markers)
  19689. m))
  19690. (defun org-imenu-get-tree ()
  19691. "Produce the index for Imenu."
  19692. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19693. (setq org-imenu-markers nil)
  19694. (let* ((n org-imenu-depth)
  19695. (re (concat "^" (org-get-limited-outline-regexp)))
  19696. (subs (make-vector (1+ n) nil))
  19697. (last-level 0)
  19698. m level head)
  19699. (save-excursion
  19700. (save-restriction
  19701. (widen)
  19702. (goto-char (point-max))
  19703. (while (re-search-backward re nil t)
  19704. (setq level (org-reduced-level (funcall outline-level)))
  19705. (when (and (<= level n)
  19706. (looking-at org-complex-heading-regexp))
  19707. (setq head (org-link-display-format
  19708. (org-match-string-no-properties 4))
  19709. m (org-imenu-new-marker))
  19710. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19711. (if (>= level last-level)
  19712. (push (cons head m) (aref subs level))
  19713. (push (cons head (aref subs (1+ level))) (aref subs level))
  19714. (loop for i from (1+ level) to n do (aset subs i nil)))
  19715. (setq last-level level)))))
  19716. (aref subs 1)))
  19717. (eval-after-load "imenu"
  19718. '(progn
  19719. (add-hook 'imenu-after-jump-hook
  19720. (lambda ()
  19721. (if (derived-mode-p 'org-mode)
  19722. (org-show-context 'org-goto))))))
  19723. (defun org-link-display-format (link)
  19724. "Replace a link with either the description, or the link target
  19725. if no description is present"
  19726. (save-match-data
  19727. (if (string-match org-bracket-link-analytic-regexp link)
  19728. (replace-match (if (match-end 5)
  19729. (match-string 5 link)
  19730. (concat (match-string 1 link)
  19731. (match-string 3 link)))
  19732. nil t link)
  19733. link)))
  19734. (defun org-toggle-link-display ()
  19735. "Toggle the literal or descriptive display of links."
  19736. (interactive)
  19737. (if org-descriptive-links
  19738. (progn (org-remove-from-invisibility-spec '(org-link))
  19739. (org-restart-font-lock)
  19740. (setq org-descriptive-links nil))
  19741. (progn (add-to-invisibility-spec '(org-link))
  19742. (org-restart-font-lock)
  19743. (setq org-descriptive-links t))))
  19744. ;; Speedbar support
  19745. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19746. "Overlay marking the agenda restriction line in speedbar.")
  19747. (overlay-put org-speedbar-restriction-lock-overlay
  19748. 'face 'org-agenda-restriction-lock)
  19749. (overlay-put org-speedbar-restriction-lock-overlay
  19750. 'help-echo "Agendas are currently limited to this item.")
  19751. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19752. (defun org-speedbar-set-agenda-restriction ()
  19753. "Restrict future agenda commands to the location at point in speedbar.
  19754. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19755. (interactive)
  19756. (require 'org-agenda)
  19757. (let (p m tp np dir txt)
  19758. (cond
  19759. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19760. 'org-imenu t))
  19761. (setq m (get-text-property p 'org-imenu-marker))
  19762. (with-current-buffer (marker-buffer m)
  19763. (goto-char m)
  19764. (org-agenda-set-restriction-lock 'subtree)))
  19765. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19766. 'speedbar-function 'speedbar-find-file))
  19767. (setq tp (previous-single-property-change
  19768. (1+ p) 'speedbar-function)
  19769. np (next-single-property-change
  19770. tp 'speedbar-function)
  19771. dir (speedbar-line-directory)
  19772. txt (buffer-substring-no-properties (or tp (point-min))
  19773. (or np (point-max))))
  19774. (with-current-buffer (find-file-noselect
  19775. (let ((default-directory dir))
  19776. (expand-file-name txt)))
  19777. (unless (derived-mode-p 'org-mode)
  19778. (error "Cannot restrict to non-Org-mode file"))
  19779. (org-agenda-set-restriction-lock 'file)))
  19780. (t (error "Don't know how to restrict Org-mode's agenda")))
  19781. (move-overlay org-speedbar-restriction-lock-overlay
  19782. (point-at-bol) (point-at-eol))
  19783. (setq current-prefix-arg nil)
  19784. (org-agenda-maybe-redo)))
  19785. (eval-after-load "speedbar"
  19786. '(progn
  19787. (speedbar-add-supported-extension ".org")
  19788. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19789. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19790. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19791. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19792. (add-hook 'speedbar-visiting-tag-hook
  19793. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  19794. ;;; Fixes and Hacks for problems with other packages
  19795. ;; Make flyspell not check words in links, to not mess up our keymap
  19796. (defun org-mode-flyspell-verify ()
  19797. "Don't let flyspell put overlays at active buttons, or on
  19798. {todo,all-time,additional-option-like}-keywords."
  19799. (let ((pos (max (1- (point)) (point-min)))
  19800. (word (thing-at-point 'word)))
  19801. (and (not (get-text-property pos 'keymap))
  19802. (not (get-text-property pos 'org-no-flyspell))
  19803. (not (member word org-todo-keywords-1))
  19804. (not (member word org-all-time-keywords))
  19805. (not (member word org-additional-option-like-keywords)))))
  19806. (defun org-remove-flyspell-overlays-in (beg end)
  19807. "Remove flyspell overlays in region."
  19808. (and (org-bound-and-true-p flyspell-mode)
  19809. (fboundp 'flyspell-delete-region-overlays)
  19810. (flyspell-delete-region-overlays beg end))
  19811. (add-text-properties beg end '(org-no-flyspell t)))
  19812. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19813. (eval-after-load "bookmark"
  19814. '(if (boundp 'bookmark-after-jump-hook)
  19815. ;; We can use the hook
  19816. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19817. ;; Hook not available, use advice
  19818. (defadvice bookmark-jump (after org-make-visible activate)
  19819. "Make the position visible."
  19820. (org-bookmark-jump-unhide))))
  19821. ;; Make sure saveplace shows the location if it was hidden
  19822. (eval-after-load "saveplace"
  19823. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19824. "Make the position visible."
  19825. (org-bookmark-jump-unhide)))
  19826. ;; Make sure ecb shows the location if it was hidden
  19827. (eval-after-load "ecb"
  19828. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19829. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19830. (if (derived-mode-p 'org-mode)
  19831. (org-show-context))))
  19832. (defun org-bookmark-jump-unhide ()
  19833. "Unhide the current position, to show the bookmark location."
  19834. (and (derived-mode-p 'org-mode)
  19835. (or (outline-invisible-p)
  19836. (save-excursion (goto-char (max (point-min) (1- (point))))
  19837. (outline-invisible-p)))
  19838. (org-show-context 'bookmark-jump)))
  19839. ;; Make session.el ignore our circular variable
  19840. (eval-after-load "session"
  19841. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19842. ;;;; Experimental code
  19843. (defun org-closed-in-range ()
  19844. "Sparse tree of items closed in a certain time range.
  19845. Still experimental, may disappear in the future."
  19846. (interactive)
  19847. ;; Get the time interval from the user.
  19848. (let* ((time1 (org-float-time
  19849. (org-read-date nil 'to-time nil "Starting date: ")))
  19850. (time2 (org-float-time
  19851. (org-read-date nil 'to-time nil "End date:")))
  19852. ;; callback function
  19853. (callback (lambda ()
  19854. (let ((time
  19855. (org-float-time
  19856. (apply 'encode-time
  19857. (org-parse-time-string
  19858. (match-string 1))))))
  19859. ;; check if time in interval
  19860. (and (>= time time1) (<= time time2))))))
  19861. ;; make tree, check each match with the callback
  19862. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19863. ;;;; Finish up
  19864. (provide 'org)
  19865. (run-hooks 'org-load-hook)
  19866. ;;; org.el ends here