org.el 801 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.8.03
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  100. (declare-function org-at-clock-log-p "org-clock" ())
  101. (declare-function org-clock-timestamps-up "org-clock" ())
  102. (declare-function org-clock-timestamps-down "org-clock" ())
  103. ;; babel
  104. (require 'ob)
  105. (require 'ob-table)
  106. (require 'ob-lob)
  107. (require 'ob-ref)
  108. (require 'ob-tangle)
  109. (require 'ob-comint)
  110. (require 'ob-keys)
  111. ;; load languages based on value of `org-babel-load-languages'
  112. (defvar org-babel-load-languages)
  113. ;;;###autoload
  114. (defun org-babel-do-load-languages (sym value)
  115. "Load the languages defined in `org-babel-load-languages'."
  116. (set-default sym value)
  117. (mapc (lambda (pair)
  118. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  119. (if active
  120. (progn
  121. (require (intern (concat "ob-" lang))))
  122. (progn
  123. (funcall 'fmakunbound
  124. (intern (concat "org-babel-execute:" lang)))
  125. (funcall 'fmakunbound
  126. (intern (concat "org-babel-expand-body:" lang)))))))
  127. org-babel-load-languages))
  128. (defcustom org-babel-load-languages '((emacs-lisp . t))
  129. "Languages which can be evaluated in Org-mode buffers.
  130. This list can be used to load support for any of the languages
  131. below, note that each language will depend on a different set of
  132. system executables and/or Emacs modes. When a language is
  133. \"loaded\", then code blocks in that language can be evaluated
  134. with `org-babel-execute-src-block' bound by default to C-c
  135. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  136. be set to remove code block evaluation from the C-c C-c
  137. keybinding. By default only Emacs Lisp (which has no
  138. requirements) is loaded."
  139. :group 'org-babel
  140. :set 'org-babel-do-load-languages
  141. :type '(alist :tag "Babel Languages"
  142. :key-type
  143. (choice
  144. (const :tag "Awk" awk)
  145. (const :tag "C" C)
  146. (const :tag "R" R)
  147. (const :tag "Asymptote" asymptote)
  148. (const :tag "Calc" calc)
  149. (const :tag "Clojure" clojure)
  150. (const :tag "CSS" css)
  151. (const :tag "Ditaa" ditaa)
  152. (const :tag "Dot" dot)
  153. (const :tag "Emacs Lisp" emacs-lisp)
  154. (const :tag "Fortran" fortran)
  155. (const :tag "Gnuplot" gnuplot)
  156. (const :tag "Haskell" haskell)
  157. (const :tag "Java" java)
  158. (const :tag "Javascript" js)
  159. (const :tag "LaTeX" latex)
  160. (const :tag "Ledger" ledger)
  161. (const :tag "Lilypond" lilypond)
  162. (const :tag "Maxima" maxima)
  163. (const :tag "Matlab" matlab)
  164. (const :tag "Mscgen" mscgen)
  165. (const :tag "Ocaml" ocaml)
  166. (const :tag "Octave" octave)
  167. (const :tag "Org" org)
  168. (const :tag "Perl" perl)
  169. (const :tag "Pico Lisp" picolisp)
  170. (const :tag "PlantUML" plantuml)
  171. (const :tag "Python" python)
  172. (const :tag "Ruby" ruby)
  173. (const :tag "Sass" sass)
  174. (const :tag "Scheme" scheme)
  175. (const :tag "Screen" screen)
  176. (const :tag "Shell Script" sh)
  177. (const :tag "Shen" shen)
  178. (const :tag "Sql" sql)
  179. (const :tag "Sqlite" sqlite))
  180. :value-type (boolean :tag "Activate" :value t)))
  181. ;;;; Customization variables
  182. (defcustom org-clone-delete-id nil
  183. "Remove ID property of clones of a subtree.
  184. When non-nil, clones of a subtree don't inherit the ID property.
  185. Otherwise they inherit the ID property with a new unique
  186. identifier."
  187. :type 'boolean
  188. :group 'org-id)
  189. ;;; Version
  190. (defconst org-version "7.8.03"
  191. "The version number of the file org.el.")
  192. (defun org-version (&optional here)
  193. "Show the org-mode version in the echo area.
  194. With prefix arg HERE, insert it at point."
  195. (interactive "P")
  196. (let* ((origin default-directory)
  197. (version org-version)
  198. (git-version)
  199. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  200. (when (and (file-exists-p (expand-file-name ".git" dir))
  201. (executable-find "git"))
  202. (unwind-protect
  203. (progn
  204. (cd dir)
  205. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  206. (with-current-buffer "*Shell Command Output*"
  207. (goto-char (point-min))
  208. (setq git-version (buffer-substring (point) (point-at-eol))))
  209. (subst-char-in-string ?- ?. git-version t)
  210. (when (string-match "\\S-"
  211. (shell-command-to-string
  212. "git diff-index --name-only HEAD --"))
  213. (setq git-version (concat git-version ".dirty")))
  214. (setq version (concat version " (" git-version ")"))))
  215. (cd origin)))
  216. (setq version (format "Org-mode version %s" version))
  217. (if here (insert version))
  218. (message version)))
  219. ;;; Compatibility constants
  220. ;;; The custom variables
  221. (defgroup org nil
  222. "Outline-based notes management and organizer."
  223. :tag "Org"
  224. :group 'outlines
  225. :group 'calendar)
  226. (defcustom org-mode-hook nil
  227. "Mode hook for Org-mode, run after the mode was turned on."
  228. :group 'org
  229. :type 'hook)
  230. (defcustom org-load-hook nil
  231. "Hook that is run after org.el has been loaded."
  232. :group 'org
  233. :type 'hook)
  234. (defcustom org-log-buffer-setup-hook nil
  235. "Hook that is run after an Org log buffer is created."
  236. :group 'org
  237. :type 'hook)
  238. (defvar org-modules) ; defined below
  239. (defvar org-modules-loaded nil
  240. "Have the modules been loaded already?")
  241. (defun org-load-modules-maybe (&optional force)
  242. "Load all extensions listed in `org-modules'."
  243. (when (or force (not org-modules-loaded))
  244. (mapc (lambda (ext)
  245. (condition-case nil (require ext)
  246. (error (message "Problems while trying to load feature `%s'" ext))))
  247. org-modules)
  248. (setq org-modules-loaded t)))
  249. (defun org-set-modules (var value)
  250. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  251. (set var value)
  252. (when (featurep 'org)
  253. (org-load-modules-maybe 'force)))
  254. (when (org-bound-and-true-p org-modules)
  255. (let ((a (member 'org-infojs org-modules)))
  256. (and a (setcar a 'org-jsinfo))))
  257. (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)
  258. "Modules that should always be loaded together with org.el.
  259. If a description starts with <C>, the file is not part of Emacs
  260. and loading it will require that you have downloaded and properly installed
  261. the org-mode distribution.
  262. You can also use this system to load external packages (i.e. neither Org
  263. core modules, nor modules from the CONTRIB directory). Just add symbols
  264. to the end of the list. If the package is called org-xyz.el, then you need
  265. to add the symbol `xyz', and the package must have a call to
  266. (provide 'org-xyz)"
  267. :group 'org
  268. :set 'org-set-modules
  269. :type
  270. '(set :greedy t
  271. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  272. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  273. (const :tag " crypt: Encryption of subtrees" org-crypt)
  274. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  275. (const :tag " docview: Links to doc-view buffers" org-docview)
  276. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  277. (const :tag " id: Global IDs for identifying entries" org-id)
  278. (const :tag " info: Links to Info nodes" org-info)
  279. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  280. (const :tag " habit: Track your consistency with habits" org-habit)
  281. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  282. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  283. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  284. (const :tag " mew Links to Mew folders/messages" org-mew)
  285. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  286. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  287. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  288. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  289. (const :tag " vm: Links to VM folders/messages" org-vm)
  290. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  291. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  292. (const :tag " mouse: Additional mouse support" org-mouse)
  293. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  294. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  295. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  296. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  297. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  298. (const :tag "C collector: Collect properties into tables" org-collector)
  299. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  300. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  301. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  302. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  303. (const :tag "C eval: Include command output as text" org-eval)
  304. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  305. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  306. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  307. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  308. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  309. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  310. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  311. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  312. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  313. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  314. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  315. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  316. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  317. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  318. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  319. (const :tag "C registry: A registry for Org-mode links" org-registry)
  320. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  321. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  322. (const :tag "C secretary: Team management with org-mode" org-secretary)
  323. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  324. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  325. (const :tag "C track: Keep up with Org-mode development" org-track)
  326. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  327. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  328. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  329. (defcustom org-support-shift-select nil
  330. "Non-nil means make shift-cursor commands select text when possible.
  331. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  332. start selecting a region, or enlarge regions started in this way.
  333. In Org-mode, in special contexts, these same keys are used for
  334. other purposes, important enough to compete with shift selection.
  335. Org tries to balance these needs by supporting `shift-select-mode'
  336. outside these special contexts, under control of this variable.
  337. The default of this variable is nil, to avoid confusing behavior. Shifted
  338. cursor keys will then execute Org commands in the following contexts:
  339. - on a headline, changing TODO state (left/right) and priority (up/down)
  340. - on a time stamp, changing the time
  341. - in a plain list item, changing the bullet type
  342. - in a property definition line, switching between allowed values
  343. - in the BEGIN line of a clock table (changing the time block).
  344. Outside these contexts, the commands will throw an error.
  345. When this variable is t and the cursor is not in a special
  346. context, Org-mode will support shift-selection for making and
  347. enlarging regions. To make this more effective, the bullet
  348. cycling will no longer happen anywhere in an item line, but only
  349. if the cursor is exactly on the bullet.
  350. If you set this variable to the symbol `always', then the keys
  351. will not be special in headlines, property lines, and item lines,
  352. to make shift selection work there as well. If this is what you
  353. want, you can use the following alternative commands: `C-c C-t'
  354. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  355. can be used to switch TODO sets, `C-c -' to cycle item bullet
  356. types, and properties can be edited by hand or in column view.
  357. However, when the cursor is on a timestamp, shift-cursor commands
  358. will still edit the time stamp - this is just too good to give up.
  359. XEmacs user should have this variable set to nil, because
  360. `shift-select-mode' is in Emacs 23 or later only."
  361. :group 'org
  362. :type '(choice
  363. (const :tag "Never" nil)
  364. (const :tag "When outside special context" t)
  365. (const :tag "Everywhere except timestamps" always)))
  366. (defcustom org-loop-over-headlines-in-active-region nil
  367. "Shall some commands act upon headlines in the active region?
  368. When set to `t', some commands will be performed in all headlines
  369. within the active region.
  370. When set to `start-level', some commands will be performed in all
  371. headlines within the active region, provided that these headlines
  372. are of the same level than the first one.
  373. When set to a string, those commands will be performed on the
  374. matching headlines within the active region. Such string must be
  375. a tags/property/todo match as it is used in the agenda tags view.
  376. The list of commands is: `org-schedule', `org-deadline',
  377. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  378. `org-archive-to-archive-sibling'. The archiving commands skip
  379. already archived entries."
  380. :type '(choice (const :tag "Don't loop" nil)
  381. (const :tag "All headlines in active region" t)
  382. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  383. (string :tag "Tags/Property/Todo matcher"))
  384. :group 'org-todo
  385. :group 'org-archive)
  386. (defgroup org-startup nil
  387. "Options concerning startup of Org-mode."
  388. :tag "Org Startup"
  389. :group 'org)
  390. (defcustom org-startup-folded t
  391. "Non-nil means entering Org-mode will switch to OVERVIEW.
  392. This can also be configured on a per-file basis by adding one of
  393. the following lines anywhere in the buffer:
  394. #+STARTUP: fold (or `overview', this is equivalent)
  395. #+STARTUP: nofold (or `showall', this is equivalent)
  396. #+STARTUP: content
  397. #+STARTUP: showeverything"
  398. :group 'org-startup
  399. :type '(choice
  400. (const :tag "nofold: show all" nil)
  401. (const :tag "fold: overview" t)
  402. (const :tag "content: all headlines" content)
  403. (const :tag "show everything, even drawers" showeverything)))
  404. (defcustom org-startup-truncated t
  405. "Non-nil means entering Org-mode will set `truncate-lines'.
  406. This is useful since some lines containing links can be very long and
  407. uninteresting. Also tables look terrible when wrapped."
  408. :group 'org-startup
  409. :type 'boolean)
  410. (defcustom org-startup-indented nil
  411. "Non-nil means turn on `org-indent-mode' on startup.
  412. This can also be configured on a per-file basis by adding one of
  413. the following lines anywhere in the buffer:
  414. #+STARTUP: indent
  415. #+STARTUP: noindent"
  416. :group 'org-structure
  417. :type '(choice
  418. (const :tag "Not" nil)
  419. (const :tag "Globally (slow on startup in large files)" t)))
  420. (defcustom org-use-sub-superscripts t
  421. "Non-nil means interpret \"_\" and \"^\" for export.
  422. When this option is turned on, you can use TeX-like syntax for sub- and
  423. superscripts. Several characters after \"_\" or \"^\" will be
  424. considered as a single item - so grouping with {} is normally not
  425. needed. For example, the following things will be parsed as single
  426. sub- or superscripts.
  427. 10^24 or 10^tau several digits will be considered 1 item.
  428. 10^-12 or 10^-tau a leading sign with digits or a word
  429. x^2-y^3 will be read as x^2 - y^3, because items are
  430. terminated by almost any nonword/nondigit char.
  431. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  432. Still, ambiguity is possible - so when in doubt use {} to enclose the
  433. sub/superscript. If you set this variable to the symbol `{}',
  434. the braces are *required* in order to trigger interpretations as
  435. sub/superscript. This can be helpful in documents that need \"_\"
  436. frequently in plain text.
  437. Not all export backends support this, but HTML does.
  438. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  439. :group 'org-startup
  440. :group 'org-export-translation
  441. :type '(choice
  442. (const :tag "Always interpret" t)
  443. (const :tag "Only with braces" {})
  444. (const :tag "Never interpret" nil)))
  445. (if (fboundp 'defvaralias)
  446. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  447. (defcustom org-startup-with-beamer-mode nil
  448. "Non-nil means turn on `org-beamer-mode' on startup.
  449. This can also be configured on a per-file basis by adding one of
  450. the following lines anywhere in the buffer:
  451. #+STARTUP: beamer"
  452. :group 'org-startup
  453. :type 'boolean)
  454. (defcustom org-startup-align-all-tables nil
  455. "Non-nil means align all tables when visiting a file.
  456. This is useful when the column width in tables is forced with <N> cookies
  457. in table fields. Such tables will look correct only after the first re-align.
  458. This can also be configured on a per-file basis by adding one of
  459. the following lines anywhere in the buffer:
  460. #+STARTUP: align
  461. #+STARTUP: noalign"
  462. :group 'org-startup
  463. :type 'boolean)
  464. (defcustom org-startup-with-inline-images nil
  465. "Non-nil means show inline images when loading a new Org file.
  466. This can also be configured on a per-file basis by adding one of
  467. the following lines anywhere in the buffer:
  468. #+STARTUP: inlineimages
  469. #+STARTUP: noinlineimages"
  470. :group 'org-startup
  471. :type 'boolean)
  472. (defcustom org-insert-mode-line-in-empty-file nil
  473. "Non-nil means insert the first line setting Org-mode in empty files.
  474. When the function `org-mode' is called interactively in an empty file, this
  475. normally means that the file name does not automatically trigger Org-mode.
  476. To ensure that the file will always be in Org-mode in the future, a
  477. line enforcing Org-mode will be inserted into the buffer, if this option
  478. has been set."
  479. :group 'org-startup
  480. :type 'boolean)
  481. (defcustom org-replace-disputed-keys nil
  482. "Non-nil means use alternative key bindings for some keys.
  483. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  484. These keys are also used by other packages like shift-selection-mode'
  485. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  486. If you want to use Org-mode together with one of these other modes,
  487. or more generally if you would like to move some Org-mode commands to
  488. other keys, set this variable and configure the keys with the variable
  489. `org-disputed-keys'.
  490. This option is only relevant at load-time of Org-mode, and must be set
  491. *before* org.el is loaded. Changing it requires a restart of Emacs to
  492. become effective."
  493. :group 'org-startup
  494. :type 'boolean)
  495. (defcustom org-use-extra-keys nil
  496. "Non-nil means use extra key sequence definitions for certain commands.
  497. This happens automatically if you run XEmacs or if `window-system'
  498. is nil. This variable lets you do the same manually. You must
  499. set it before loading org.
  500. Example: on Carbon Emacs 22 running graphically, with an external
  501. keyboard on a Powerbook, the default way of setting M-left might
  502. not work for either Alt or ESC. Setting this variable will make
  503. it work for ESC."
  504. :group 'org-startup
  505. :type 'boolean)
  506. (if (fboundp 'defvaralias)
  507. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  508. (defcustom org-disputed-keys
  509. '(([(shift up)] . [(meta p)])
  510. ([(shift down)] . [(meta n)])
  511. ([(shift left)] . [(meta -)])
  512. ([(shift right)] . [(meta +)])
  513. ([(control shift right)] . [(meta shift +)])
  514. ([(control shift left)] . [(meta shift -)]))
  515. "Keys for which Org-mode and other modes compete.
  516. This is an alist, cars are the default keys, second element specifies
  517. the alternative to use when `org-replace-disputed-keys' is t.
  518. Keys can be specified in any syntax supported by `define-key'.
  519. The value of this option takes effect only at Org-mode's startup,
  520. therefore you'll have to restart Emacs to apply it after changing."
  521. :group 'org-startup
  522. :type 'alist)
  523. (defun org-key (key)
  524. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  525. Or return the original if not disputed.
  526. Also apply the translations defined in `org-xemacs-key-equivalents'."
  527. (when org-replace-disputed-keys
  528. (let* ((nkey (key-description key))
  529. (x (org-find-if (lambda (x)
  530. (equal (key-description (car x)) nkey))
  531. org-disputed-keys)))
  532. (setq key (if x (cdr x) key))))
  533. (when (featurep 'xemacs)
  534. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  535. key)
  536. (defun org-find-if (predicate seq)
  537. (catch 'exit
  538. (while seq
  539. (if (funcall predicate (car seq))
  540. (throw 'exit (car seq))
  541. (pop seq)))))
  542. (defun org-defkey (keymap key def)
  543. "Define a key, possibly translated, as returned by `org-key'."
  544. (define-key keymap (org-key key) def))
  545. (defcustom org-ellipsis nil
  546. "The ellipsis to use in the Org-mode outline.
  547. When nil, just use the standard three dots. When a string, use that instead,
  548. When a face, use the standard 3 dots, but with the specified face.
  549. The change affects only Org-mode (which will then use its own display table).
  550. Changing this requires executing `M-x org-mode' in a buffer to become
  551. effective."
  552. :group 'org-startup
  553. :type '(choice (const :tag "Default" nil)
  554. (face :tag "Face" :value org-warning)
  555. (string :tag "String" :value "...#")))
  556. (defvar org-display-table nil
  557. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  558. (defgroup org-keywords nil
  559. "Keywords in Org-mode."
  560. :tag "Org Keywords"
  561. :group 'org)
  562. (defcustom org-deadline-string "DEADLINE:"
  563. "String to mark deadline entries.
  564. A deadline is this string, followed by a time stamp. Should be a word,
  565. terminated by a colon. You can insert a schedule keyword and
  566. a timestamp with \\[org-deadline].
  567. Changes become only effective after restarting Emacs."
  568. :group 'org-keywords
  569. :type 'string)
  570. (defcustom org-scheduled-string "SCHEDULED:"
  571. "String to mark scheduled TODO entries.
  572. A schedule is this string, followed by a time stamp. Should be a word,
  573. terminated by a colon. You can insert a schedule keyword and
  574. a timestamp with \\[org-schedule].
  575. Changes become only effective after restarting Emacs."
  576. :group 'org-keywords
  577. :type 'string)
  578. (defcustom org-closed-string "CLOSED:"
  579. "String used as the prefix for timestamps logging closing a TODO entry."
  580. :group 'org-keywords
  581. :type 'string)
  582. (defcustom org-clock-string "CLOCK:"
  583. "String used as prefix for timestamps clocking work hours on an item."
  584. :group 'org-keywords
  585. :type 'string)
  586. (defcustom org-comment-string "COMMENT"
  587. "Entries starting with this keyword will never be exported.
  588. An entry can be toggled between COMMENT and normal with
  589. \\[org-toggle-comment].
  590. Changes become only effective after restarting Emacs."
  591. :group 'org-keywords
  592. :type 'string)
  593. (defcustom org-quote-string "QUOTE"
  594. "Entries starting with this keyword will be exported in fixed-width font.
  595. Quoting applies only to the text in the entry following the headline, and does
  596. not extend beyond the next headline, even if that is lower level.
  597. An entry can be toggled between QUOTE and normal with
  598. \\[org-toggle-fixed-width-section]."
  599. :group 'org-keywords
  600. :type 'string)
  601. (defconst org-repeat-re
  602. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  603. "Regular expression for specifying repeated events.
  604. After a match, group 1 contains the repeat expression.")
  605. (defgroup org-structure nil
  606. "Options concerning the general structure of Org-mode files."
  607. :tag "Org Structure"
  608. :group 'org)
  609. (defgroup org-reveal-location nil
  610. "Options about how to make context of a location visible."
  611. :tag "Org Reveal Location"
  612. :group 'org-structure)
  613. (defconst org-context-choice
  614. '(choice
  615. (const :tag "Always" t)
  616. (const :tag "Never" nil)
  617. (repeat :greedy t :tag "Individual contexts"
  618. (cons
  619. (choice :tag "Context"
  620. (const agenda)
  621. (const org-goto)
  622. (const occur-tree)
  623. (const tags-tree)
  624. (const link-search)
  625. (const mark-goto)
  626. (const bookmark-jump)
  627. (const isearch)
  628. (const default))
  629. (boolean))))
  630. "Contexts for the reveal options.")
  631. (defcustom org-show-hierarchy-above '((default . t))
  632. "Non-nil means show full hierarchy when revealing a location.
  633. Org-mode often shows locations in an org-mode file which might have
  634. been invisible before. When this is set, the hierarchy of headings
  635. above the exposed location is shown.
  636. Turning this off for example for sparse trees makes them very compact.
  637. Instead of t, this can also be an alist specifying this option for different
  638. contexts. Valid contexts are
  639. agenda when exposing an entry from the agenda
  640. org-goto when using the command `org-goto' on key C-c C-j
  641. occur-tree when using the command `org-occur' on key C-c /
  642. tags-tree when constructing a sparse tree based on tags matches
  643. link-search when exposing search matches associated with a link
  644. mark-goto when exposing the jump goal of a mark
  645. bookmark-jump when exposing a bookmark location
  646. isearch when exiting from an incremental search
  647. default default for all contexts not set explicitly"
  648. :group 'org-reveal-location
  649. :type org-context-choice)
  650. (defcustom org-show-following-heading '((default . nil))
  651. "Non-nil means show following heading when revealing a location.
  652. Org-mode often shows locations in an org-mode file which might have
  653. been invisible before. When this is set, the heading following the
  654. match is shown.
  655. Turning this off for example for sparse trees makes them very compact,
  656. but makes it harder to edit the location of the match. In such a case,
  657. use the command \\[org-reveal] to show more context.
  658. Instead of t, this can also be an alist specifying this option for different
  659. contexts. See `org-show-hierarchy-above' for valid contexts."
  660. :group 'org-reveal-location
  661. :type org-context-choice)
  662. (defcustom org-show-siblings '((default . nil) (isearch t))
  663. "Non-nil means show all sibling heading when revealing a location.
  664. Org-mode often shows locations in an org-mode file which might have
  665. been invisible before. When this is set, the sibling of the current entry
  666. heading are all made visible. If `org-show-hierarchy-above' is t,
  667. the same happens on each level of the hierarchy above the current entry.
  668. By default this is on for the isearch context, off for all other contexts.
  669. Turning this off for example for sparse trees makes them very compact,
  670. but makes it harder to edit the location of the match. In such a case,
  671. use the command \\[org-reveal] to show more context.
  672. Instead of t, this can also be an alist specifying this option for different
  673. contexts. See `org-show-hierarchy-above' for valid contexts."
  674. :group 'org-reveal-location
  675. :type org-context-choice)
  676. (defcustom org-show-entry-below '((default . nil))
  677. "Non-nil means show the entry below a headline when revealing a location.
  678. Org-mode often shows locations in an org-mode file which might have
  679. been invisible before. When this is set, the text below the headline that is
  680. exposed is also shown.
  681. By default this is off for all contexts.
  682. Instead of t, this can also be an alist specifying this option for different
  683. contexts. See `org-show-hierarchy-above' for valid contexts."
  684. :group 'org-reveal-location
  685. :type org-context-choice)
  686. (defcustom org-indirect-buffer-display 'other-window
  687. "How should indirect tree buffers be displayed?
  688. This applies to indirect buffers created with the commands
  689. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  690. Valid values are:
  691. current-window Display in the current window
  692. other-window Just display in another window.
  693. dedicated-frame Create one new frame, and re-use it each time.
  694. new-frame Make a new frame each time. Note that in this case
  695. previously-made indirect buffers are kept, and you need to
  696. kill these buffers yourself."
  697. :group 'org-structure
  698. :group 'org-agenda-windows
  699. :type '(choice
  700. (const :tag "In current window" current-window)
  701. (const :tag "In current frame, other window" other-window)
  702. (const :tag "Each time a new frame" new-frame)
  703. (const :tag "One dedicated frame" dedicated-frame)))
  704. (defcustom org-use-speed-commands nil
  705. "Non-nil means activate single letter commands at beginning of a headline.
  706. This may also be a function to test for appropriate locations where speed
  707. commands should be active."
  708. :group 'org-structure
  709. :type '(choice
  710. (const :tag "Never" nil)
  711. (const :tag "At beginning of headline stars" t)
  712. (function)))
  713. (defcustom org-speed-commands-user nil
  714. "Alist of additional speed commands.
  715. This list will be checked before `org-speed-commands-default'
  716. when the variable `org-use-speed-commands' is non-nil
  717. and when the cursor is at the beginning of a headline.
  718. The car if each entry is a string with a single letter, which must
  719. be assigned to `self-insert-command' in the global map.
  720. The cdr is either a command to be called interactively, a function
  721. to be called, or a form to be evaluated.
  722. An entry that is just a list with a single string will be interpreted
  723. as a descriptive headline that will be added when listing the speed
  724. commands in the Help buffer using the `?' speed command."
  725. :group 'org-structure
  726. :type '(repeat :value ("k" . ignore)
  727. (choice :value ("k" . ignore)
  728. (list :tag "Descriptive Headline" (string :tag "Headline"))
  729. (cons :tag "Letter and Command"
  730. (string :tag "Command letter")
  731. (choice
  732. (function)
  733. (sexp))))))
  734. (defgroup org-cycle nil
  735. "Options concerning visibility cycling in Org-mode."
  736. :tag "Org Cycle"
  737. :group 'org-structure)
  738. (defcustom org-cycle-skip-children-state-if-no-children t
  739. "Non-nil means skip CHILDREN state in entries that don't have any."
  740. :group 'org-cycle
  741. :type 'boolean)
  742. (defcustom org-cycle-max-level nil
  743. "Maximum level which should still be subject to visibility cycling.
  744. Levels higher than this will, for cycling, be treated as text, not a headline.
  745. When `org-odd-levels-only' is set, a value of N in this variable actually
  746. means 2N-1 stars as the limiting headline.
  747. When nil, cycle all levels.
  748. Note that the limiting level of cycling is also influenced by
  749. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  750. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  751. than its value."
  752. :group 'org-cycle
  753. :type '(choice
  754. (const :tag "No limit" nil)
  755. (integer :tag "Maximum level")))
  756. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  757. "Names of drawers. Drawers are not opened by cycling on the headline above.
  758. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  759. this:
  760. :DRAWERNAME:
  761. .....
  762. :END:
  763. The drawer \"PROPERTIES\" is special for capturing properties through
  764. the property API.
  765. Drawers can be defined on the per-file basis with a line like:
  766. #+DRAWERS: HIDDEN STATE PROPERTIES"
  767. :group 'org-structure
  768. :group 'org-cycle
  769. :type '(repeat (string :tag "Drawer Name")))
  770. (defcustom org-hide-block-startup nil
  771. "Non-nil means entering Org-mode will fold all blocks.
  772. This can also be set in on a per-file basis with
  773. #+STARTUP: hideblocks
  774. #+STARTUP: showblocks"
  775. :group 'org-startup
  776. :group 'org-cycle
  777. :type 'boolean)
  778. (defcustom org-cycle-global-at-bob nil
  779. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  780. This makes it possible to do global cycling without having to use S-TAB or
  781. \\[universal-argument] TAB. For this special case to work, the first line \
  782. of the buffer
  783. must not be a headline - it may be empty or some other text. When used in
  784. this way, `org-cycle-hook' is disables temporarily, to make sure the
  785. cursor stays at the beginning of the buffer.
  786. When this option is nil, don't do anything special at the beginning
  787. of the buffer."
  788. :group 'org-cycle
  789. :type 'boolean)
  790. (defcustom org-cycle-level-after-item/entry-creation t
  791. "Non-nil means cycle entry level or item indentation in new empty entries.
  792. When the cursor is at the end of an empty headline, i.e with only stars
  793. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  794. and then all possible ancestor states, before returning to the original state.
  795. This makes data entry extremely fast: M-RET to create a new headline,
  796. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  797. When the cursor is at the end of an empty plain list item, one TAB will
  798. make it a subitem, two or more tabs will back up to make this an item
  799. higher up in the item hierarchy."
  800. :group 'org-cycle
  801. :type 'boolean)
  802. (defcustom org-cycle-emulate-tab t
  803. "Where should `org-cycle' emulate TAB.
  804. nil Never
  805. white Only in completely white lines
  806. whitestart Only at the beginning of lines, before the first non-white char
  807. t Everywhere except in headlines
  808. exc-hl-bol Everywhere except at the start of a headline
  809. If TAB is used in a place where it does not emulate TAB, the current subtree
  810. visibility is cycled."
  811. :group 'org-cycle
  812. :type '(choice (const :tag "Never" nil)
  813. (const :tag "Only in completely white lines" white)
  814. (const :tag "Before first char in a line" whitestart)
  815. (const :tag "Everywhere except in headlines" t)
  816. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  817. ))
  818. (defcustom org-cycle-separator-lines 2
  819. "Number of empty lines needed to keep an empty line between collapsed trees.
  820. If you leave an empty line between the end of a subtree and the following
  821. headline, this empty line is hidden when the subtree is folded.
  822. Org-mode will leave (exactly) one empty line visible if the number of
  823. empty lines is equal or larger to the number given in this variable.
  824. So the default 2 means at least 2 empty lines after the end of a subtree
  825. are needed to produce free space between a collapsed subtree and the
  826. following headline.
  827. If the number is negative, and the number of empty lines is at least -N,
  828. all empty lines are shown.
  829. Special case: when 0, never leave empty lines in collapsed view."
  830. :group 'org-cycle
  831. :type 'integer)
  832. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  833. (defcustom org-pre-cycle-hook nil
  834. "Hook that is run before visibility cycling is happening.
  835. The function(s) in this hook must accept a single argument which indicates
  836. the new state that will be set right after running this hook. The
  837. argument is a symbol. Before a global state change, it can have the values
  838. `overview', `content', or `all'. Before a local state change, it can have
  839. the values `folded', `children', or `subtree'."
  840. :group 'org-cycle
  841. :type 'hook)
  842. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  843. org-cycle-hide-drawers
  844. org-cycle-show-empty-lines
  845. org-optimize-window-after-visibility-change)
  846. "Hook that is run after `org-cycle' has changed the buffer visibility.
  847. The function(s) in this hook must accept a single argument which indicates
  848. the new state that was set by the most recent `org-cycle' command. The
  849. argument is a symbol. After a global state change, it can have the values
  850. `overview', `content', or `all'. After a local state change, it can have
  851. the values `folded', `children', or `subtree'."
  852. :group 'org-cycle
  853. :type 'hook)
  854. (defgroup org-edit-structure nil
  855. "Options concerning structure editing in Org-mode."
  856. :tag "Org Edit Structure"
  857. :group 'org-structure)
  858. (defcustom org-odd-levels-only nil
  859. "Non-nil means skip even levels and only use odd levels for the outline.
  860. This has the effect that two stars are being added/taken away in
  861. promotion/demotion commands. It also influences how levels are
  862. handled by the exporters.
  863. Changing it requires restart of `font-lock-mode' to become effective
  864. for fontification also in regions already fontified.
  865. You may also set this on a per-file basis by adding one of the following
  866. lines to the buffer:
  867. #+STARTUP: odd
  868. #+STARTUP: oddeven"
  869. :group 'org-edit-structure
  870. :group 'org-appearance
  871. :type 'boolean)
  872. (defcustom org-adapt-indentation t
  873. "Non-nil means adapt indentation to outline node level.
  874. When this variable is set, Org assumes that you write outlines by
  875. indenting text in each node to align with the headline (after the stars).
  876. The following issues are influenced by this variable:
  877. - When this is set and the *entire* text in an entry is indented, the
  878. indentation is increased by one space in a demotion command, and
  879. decreased by one in a promotion command. If any line in the entry
  880. body starts with text at column 0, indentation is not changed at all.
  881. - Property drawers and planning information is inserted indented when
  882. this variable s set. When nil, they will not be indented.
  883. - TAB indents a line relative to context. The lines below a headline
  884. will be indented when this variable is set.
  885. Note that this is all about true indentation, by adding and removing
  886. space characters. See also `org-indent.el' which does level-dependent
  887. indentation in a virtual way, i.e. at display time in Emacs."
  888. :group 'org-edit-structure
  889. :type 'boolean)
  890. (defcustom org-special-ctrl-a/e nil
  891. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  892. When t, `C-a' will bring back the cursor to the beginning of the
  893. headline text, i.e. after the stars and after a possible TODO
  894. keyword. In an item, this will be the position after bullet and
  895. check-box, if any. When the cursor is already at that position,
  896. another `C-a' will bring it to the beginning of the line.
  897. `C-e' will jump to the end of the headline, ignoring the presence
  898. of tags in the headline. A second `C-e' will then jump to the
  899. true end of the line, after any tags. This also means that, when
  900. this variable is non-nil, `C-e' also will never jump beyond the
  901. end of the heading of a folded section, i.e. not after the
  902. ellipses.
  903. When set to the symbol `reversed', the first `C-a' or `C-e' works
  904. normally, going to the true line boundary first. Only a directly
  905. following, identical keypress will bring the cursor to the
  906. special positions.
  907. This may also be a cons cell where the behavior for `C-a' and
  908. `C-e' is set separately."
  909. :group 'org-edit-structure
  910. :type '(choice
  911. (const :tag "off" nil)
  912. (const :tag "on: after stars/bullet and before tags first" t)
  913. (const :tag "reversed: true line boundary first" reversed)
  914. (cons :tag "Set C-a and C-e separately"
  915. (choice :tag "Special C-a"
  916. (const :tag "off" nil)
  917. (const :tag "on: after stars/bullet first" t)
  918. (const :tag "reversed: before stars/bullet first" reversed))
  919. (choice :tag "Special C-e"
  920. (const :tag "off" nil)
  921. (const :tag "on: before tags first" t)
  922. (const :tag "reversed: after tags first" reversed)))))
  923. (if (fboundp 'defvaralias)
  924. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  925. (defcustom org-special-ctrl-k nil
  926. "Non-nil means `C-k' will behave specially in headlines.
  927. When nil, `C-k' will call the default `kill-line' command.
  928. When t, the following will happen while the cursor is in the headline:
  929. - When the cursor is at the beginning of a headline, kill the entire
  930. line and possible the folded subtree below the line.
  931. - When in the middle of the headline text, kill the headline up to the tags.
  932. - When after the headline text, kill the tags."
  933. :group 'org-edit-structure
  934. :type 'boolean)
  935. (defcustom org-ctrl-k-protect-subtree nil
  936. "Non-nil means, do not delete a hidden subtree with C-k.
  937. When set to the symbol `error', simply throw an error when C-k is
  938. used to kill (part-of) a headline that has hidden text behind it.
  939. Any other non-nil value will result in a query to the user, if it is
  940. OK to kill that hidden subtree. When nil, kill without remorse."
  941. :group 'org-edit-structure
  942. :type '(choice
  943. (const :tag "Do not protect hidden subtrees" nil)
  944. (const :tag "Protect hidden subtrees with a security query" t)
  945. (const :tag "Never kill a hidden subtree with C-k" error)))
  946. (defcustom org-catch-invisible-edits nil
  947. "Check if in invisible region before inserting or deleting a character.
  948. Valid values are:
  949. nil Do not check, so just do invisible edits.
  950. error Throw an error and do nothing.
  951. show Make point visible, and do the requested edit.
  952. show-and-error Make point visible, then throw an error and abort the edit.
  953. smart Make point visible, and do insertion/deletion if it is
  954. adjacent to visible text and the change feels predictable.
  955. Never delete a previously invisible character or add in the
  956. middle or right after an invisible region. Basically, this
  957. allows insertion and backward-delete right before ellipses.
  958. FIXME: maybe in this case we should not even show?"
  959. :group 'org-edit-structure
  960. :type '(choice
  961. (const :tag "Do not check" nil)
  962. (const :tag "Throw error when trying to edit" error)
  963. (const :tag "Unhide, but do not do the edit" show-and-error)
  964. (const :tag "Show invisible part and do the edit" show)
  965. (const :tag "Be smart and do the right thing" smart)))
  966. (defcustom org-yank-folded-subtrees t
  967. "Non-nil means when yanking subtrees, fold them.
  968. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  969. it starts with a heading and all other headings in it are either children
  970. or siblings, then fold all the subtrees. However, do this only if no
  971. text after the yank would be swallowed into a folded tree by this action."
  972. :group 'org-edit-structure
  973. :type 'boolean)
  974. (defcustom org-yank-adjusted-subtrees nil
  975. "Non-nil means when yanking subtrees, adjust the level.
  976. With this setting, `org-paste-subtree' is used to insert the subtree, see
  977. this function for details."
  978. :group 'org-edit-structure
  979. :type 'boolean)
  980. (defcustom org-M-RET-may-split-line '((default . t))
  981. "Non-nil means M-RET will split the line at the cursor position.
  982. When nil, it will go to the end of the line before making a
  983. new line.
  984. You may also set this option in a different way for different
  985. contexts. Valid contexts are:
  986. headline when creating a new headline
  987. item when creating a new item
  988. table in a table field
  989. default the value to be used for all contexts not explicitly
  990. customized"
  991. :group 'org-structure
  992. :group 'org-table
  993. :type '(choice
  994. (const :tag "Always" t)
  995. (const :tag "Never" nil)
  996. (repeat :greedy t :tag "Individual contexts"
  997. (cons
  998. (choice :tag "Context"
  999. (const headline)
  1000. (const item)
  1001. (const table)
  1002. (const default))
  1003. (boolean)))))
  1004. (defcustom org-insert-heading-respect-content nil
  1005. "Non-nil means insert new headings after the current subtree.
  1006. When nil, the new heading is created directly after the current line.
  1007. The commands \\[org-insert-heading-respect-content] and
  1008. \\[org-insert-todo-heading-respect-content] turn this variable on
  1009. for the duration of the command."
  1010. :group 'org-structure
  1011. :type 'boolean)
  1012. (defcustom org-blank-before-new-entry '((heading . auto)
  1013. (plain-list-item . auto))
  1014. "Should `org-insert-heading' leave a blank line before new heading/item?
  1015. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1016. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1017. which case Org will look at the surrounding headings/items and try to
  1018. make an intelligent decision whether to insert a blank line or not.
  1019. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1020. set, the setting here is ignored and no empty line is inserted, to avoid
  1021. breaking the list structure."
  1022. :group 'org-edit-structure
  1023. :type '(list
  1024. (cons (const heading)
  1025. (choice (const :tag "Never" nil)
  1026. (const :tag "Always" t)
  1027. (const :tag "Auto" auto)))
  1028. (cons (const plain-list-item)
  1029. (choice (const :tag "Never" nil)
  1030. (const :tag "Always" t)
  1031. (const :tag "Auto" auto)))))
  1032. (defcustom org-insert-heading-hook nil
  1033. "Hook being run after inserting a new heading."
  1034. :group 'org-edit-structure
  1035. :type 'hook)
  1036. (defcustom org-enable-fixed-width-editor t
  1037. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1038. This currently only means they are never auto-wrapped.
  1039. When nil, such lines will be treated like ordinary lines.
  1040. See also the QUOTE keyword."
  1041. :group 'org-edit-structure
  1042. :type 'boolean)
  1043. (defcustom org-goto-auto-isearch t
  1044. "Non-nil means typing characters in `org-goto' starts incremental search."
  1045. :group 'org-edit-structure
  1046. :type 'boolean)
  1047. (defgroup org-sparse-trees nil
  1048. "Options concerning sparse trees in Org-mode."
  1049. :tag "Org Sparse Trees"
  1050. :group 'org-structure)
  1051. (defcustom org-highlight-sparse-tree-matches t
  1052. "Non-nil means highlight all matches that define a sparse tree.
  1053. The highlights will automatically disappear the next time the buffer is
  1054. changed by an edit command."
  1055. :group 'org-sparse-trees
  1056. :type 'boolean)
  1057. (defcustom org-remove-highlights-with-change t
  1058. "Non-nil means any change to the buffer will remove temporary highlights.
  1059. Such highlights are created by `org-occur' and `org-clock-display'.
  1060. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1061. The highlights created by `org-preview-latex-fragment' always need
  1062. `C-c C-c' to be removed."
  1063. :group 'org-sparse-trees
  1064. :group 'org-time
  1065. :type 'boolean)
  1066. (defcustom org-occur-hook '(org-first-headline-recenter)
  1067. "Hook that is run after `org-occur' has constructed a sparse tree.
  1068. This can be used to recenter the window to show as much of the structure
  1069. as possible."
  1070. :group 'org-sparse-trees
  1071. :type 'hook)
  1072. (defgroup org-imenu-and-speedbar nil
  1073. "Options concerning imenu and speedbar in Org-mode."
  1074. :tag "Org Imenu and Speedbar"
  1075. :group 'org-structure)
  1076. (defcustom org-imenu-depth 2
  1077. "The maximum level for Imenu access to Org-mode headlines.
  1078. This also applied for speedbar access."
  1079. :group 'org-imenu-and-speedbar
  1080. :type 'integer)
  1081. (defgroup org-table nil
  1082. "Options concerning tables in Org-mode."
  1083. :tag "Org Table"
  1084. :group 'org)
  1085. (defcustom org-enable-table-editor 'optimized
  1086. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1087. When nil, such lines will be treated like ordinary lines.
  1088. When equal to the symbol `optimized', the table editor will be optimized to
  1089. do the following:
  1090. - Automatic overwrite mode in front of whitespace in table fields.
  1091. This makes the structure of the table stay in tact as long as the edited
  1092. field does not exceed the column width.
  1093. - Minimize the number of realigns. Normally, the table is aligned each time
  1094. TAB or RET are pressed to move to another field. With optimization this
  1095. happens only if changes to a field might have changed the column width.
  1096. Optimization requires replacing the functions `self-insert-command',
  1097. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1098. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1099. very good at guessing when a re-align will be necessary, but you can always
  1100. force one with \\[org-ctrl-c-ctrl-c].
  1101. If you would like to use the optimized version in Org-mode, but the
  1102. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1103. This variable can be used to turn on and off the table editor during a session,
  1104. but in order to toggle optimization, a restart is required.
  1105. See also the variable `org-table-auto-blank-field'."
  1106. :group 'org-table
  1107. :type '(choice
  1108. (const :tag "off" nil)
  1109. (const :tag "on" t)
  1110. (const :tag "on, optimized" optimized)))
  1111. (defcustom org-self-insert-cluster-for-undo t
  1112. "Non-nil means cluster self-insert commands for undo when possible.
  1113. If this is set, then, like in the Emacs command loop, 20 consecutive
  1114. characters will be undone together.
  1115. This is configurable, because there is some impact on typing performance."
  1116. :group 'org-table
  1117. :type 'boolean)
  1118. (defcustom org-table-tab-recognizes-table.el t
  1119. "Non-nil means TAB will automatically notice a table.el table.
  1120. When it sees such a table, it moves point into it and - if necessary -
  1121. calls `table-recognize-table'."
  1122. :group 'org-table-editing
  1123. :type 'boolean)
  1124. (defgroup org-link nil
  1125. "Options concerning links in Org-mode."
  1126. :tag "Org Link"
  1127. :group 'org)
  1128. (defvar org-link-abbrev-alist-local nil
  1129. "Buffer-local version of `org-link-abbrev-alist', which see.
  1130. The value of this is taken from the #+LINK lines.")
  1131. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1132. (defcustom org-link-abbrev-alist nil
  1133. "Alist of link abbreviations.
  1134. The car of each element is a string, to be replaced at the start of a link.
  1135. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1136. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1137. [[linkkey:tag][description]]
  1138. The 'linkkey' must be a word word, starting with a letter, followed
  1139. by letters, numbers, '-' or '_'.
  1140. If REPLACE is a string, the tag will simply be appended to create the link.
  1141. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1142. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1143. be inserted at that point (see the function `url-hexify-string').
  1144. REPLACE may also be a function that will be called with the tag as the
  1145. only argument to create the link, which should be returned as a string.
  1146. See the manual for examples."
  1147. :group 'org-link
  1148. :type '(repeat
  1149. (cons
  1150. (string :tag "Protocol")
  1151. (choice
  1152. (string :tag "Format")
  1153. (function)))))
  1154. (defcustom org-descriptive-links t
  1155. "Non-nil means Org will display descriptive links.
  1156. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1157. \"Org Website\", hiding the link itself and just displaying its
  1158. description. When set to `nil', Org will display the full links
  1159. literally.
  1160. You can interactively set the value of this variable by calling
  1161. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1162. :group 'org-link
  1163. :type 'boolean)
  1164. (defcustom org-link-file-path-type 'adaptive
  1165. "How the path name in file links should be stored.
  1166. Valid values are:
  1167. relative Relative to the current directory, i.e. the directory of the file
  1168. into which the link is being inserted.
  1169. absolute Absolute path, if possible with ~ for home directory.
  1170. noabbrev Absolute path, no abbreviation of home directory.
  1171. adaptive Use relative path for files in the current directory and sub-
  1172. directories of it. For other files, use an absolute path."
  1173. :group 'org-link
  1174. :type '(choice
  1175. (const relative)
  1176. (const absolute)
  1177. (const noabbrev)
  1178. (const adaptive)))
  1179. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1180. "Types of links that should be activated in Org-mode files.
  1181. This is a list of symbols, each leading to the activation of a certain link
  1182. type. In principle, it does not hurt to turn on most link types - there may
  1183. be a small gain when turning off unused link types. The types are:
  1184. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1185. angle Links in angular brackets that may contain whitespace like
  1186. <bbdb:Carsten Dominik>.
  1187. plain Plain links in normal text, no whitespace, like http://google.com.
  1188. radio Text that is matched by a radio target, see manual for details.
  1189. tag Tag settings in a headline (link to tag search).
  1190. date Time stamps (link to calendar).
  1191. footnote Footnote labels.
  1192. Changing this variable requires a restart of Emacs to become effective."
  1193. :group 'org-link
  1194. :type '(set :greedy t
  1195. (const :tag "Double bracket links" bracket)
  1196. (const :tag "Angular bracket links" angle)
  1197. (const :tag "Plain text links" plain)
  1198. (const :tag "Radio target matches" radio)
  1199. (const :tag "Tags" tag)
  1200. (const :tag "Timestamps" date)
  1201. (const :tag "Footnotes" footnote)))
  1202. (defcustom org-make-link-description-function nil
  1203. "Function to use to generate link descriptions from links.
  1204. If nil the link location will be used. This function must take
  1205. two parameters; the first is the link and the second the
  1206. description `org-insert-link' has generated, and should return the
  1207. description to use."
  1208. :group 'org-link
  1209. :type 'function)
  1210. (defgroup org-link-store nil
  1211. "Options concerning storing links in Org-mode."
  1212. :tag "Org Store Link"
  1213. :group 'org-link)
  1214. (defcustom org-email-link-description-format "Email %c: %.30s"
  1215. "Format of the description part of a link to an email or usenet message.
  1216. The following %-escapes will be replaced by corresponding information:
  1217. %F full \"From\" field
  1218. %f name, taken from \"From\" field, address if no name
  1219. %T full \"To\" field
  1220. %t first name in \"To\" field, address if no name
  1221. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1222. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1223. %s subject
  1224. %d date
  1225. %m message-id.
  1226. You may use normal field width specification between the % and the letter.
  1227. This is for example useful to limit the length of the subject.
  1228. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1229. :group 'org-link-store
  1230. :type 'string)
  1231. (defcustom org-from-is-user-regexp
  1232. (let (r1 r2)
  1233. (when (and user-mail-address (not (string= user-mail-address "")))
  1234. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1235. (when (and user-full-name (not (string= user-full-name "")))
  1236. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1237. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1238. "Regexp matched against the \"From:\" header of an email or usenet message.
  1239. It should match if the message is from the user him/herself."
  1240. :group 'org-link-store
  1241. :type 'regexp)
  1242. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1243. "Non-nil means storing a link to an Org file will use entry IDs.
  1244. Note that before this variable is even considered, org-id must be loaded,
  1245. so please customize `org-modules' and turn it on.
  1246. The variable can have the following values:
  1247. t Create an ID if needed to make a link to the current entry.
  1248. create-if-interactive
  1249. If `org-store-link' is called directly (interactively, as a user
  1250. command), do create an ID to support the link. But when doing the
  1251. job for remember, only use the ID if it already exists. The
  1252. purpose of this setting is to avoid proliferation of unwanted
  1253. IDs, just because you happen to be in an Org file when you
  1254. call `org-remember' that automatically and preemptively
  1255. creates a link. If you do want to get an ID link in a remember
  1256. template to an entry not having an ID, create it first by
  1257. explicitly creating a link to it, using `C-c C-l' first.
  1258. create-if-interactive-and-no-custom-id
  1259. Like create-if-interactive, but do not create an ID if there is
  1260. a CUSTOM_ID property defined in the entry. This is the default.
  1261. use-existing
  1262. Use existing ID, do not create one.
  1263. nil Never use an ID to make a link, instead link using a text search for
  1264. the headline text."
  1265. :group 'org-link-store
  1266. :type '(choice
  1267. (const :tag "Create ID to make link" t)
  1268. (const :tag "Create if storing link interactively"
  1269. create-if-interactive)
  1270. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1271. create-if-interactive-and-no-custom-id)
  1272. (const :tag "Only use existing" use-existing)
  1273. (const :tag "Do not use ID to create link" nil)))
  1274. (defcustom org-context-in-file-links t
  1275. "Non-nil means file links from `org-store-link' contain context.
  1276. A search string will be added to the file name with :: as separator and
  1277. used to find the context when the link is activated by the command
  1278. `org-open-at-point'. When this option is t, the entire active region
  1279. will be placed in the search string of the file link. If set to a
  1280. positive integer, only the first n lines of context will be stored.
  1281. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1282. negates this setting for the duration of the command."
  1283. :group 'org-link-store
  1284. :type '(choice boolean integer))
  1285. (defcustom org-keep-stored-link-after-insertion nil
  1286. "Non-nil means keep link in list for entire session.
  1287. The command `org-store-link' adds a link pointing to the current
  1288. location to an internal list. These links accumulate during a session.
  1289. The command `org-insert-link' can be used to insert links into any
  1290. Org-mode file (offering completion for all stored links). When this
  1291. option is nil, every link which has been inserted once using \\[org-insert-link]
  1292. will be removed from the list, to make completing the unused links
  1293. more efficient."
  1294. :group 'org-link-store
  1295. :type 'boolean)
  1296. (defgroup org-link-follow nil
  1297. "Options concerning following links in Org-mode."
  1298. :tag "Org Follow Link"
  1299. :group 'org-link)
  1300. (defcustom org-link-translation-function nil
  1301. "Function to translate links with different syntax to Org syntax.
  1302. This can be used to translate links created for example by the Planner
  1303. or emacs-wiki packages to Org syntax.
  1304. The function must accept two parameters, a TYPE containing the link
  1305. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1306. which is everything after the link protocol. It should return a cons
  1307. with possibly modified values of type and path.
  1308. Org contains a function for this, so if you set this variable to
  1309. `org-translate-link-from-planner', you should be able follow many
  1310. links created by planner."
  1311. :group 'org-link-follow
  1312. :type 'function)
  1313. (defcustom org-follow-link-hook nil
  1314. "Hook that is run after a link has been followed."
  1315. :group 'org-link-follow
  1316. :type 'hook)
  1317. (defcustom org-tab-follows-link nil
  1318. "Non-nil means on links TAB will follow the link.
  1319. Needs to be set before org.el is loaded.
  1320. This really should not be used, it does not make sense, and the
  1321. implementation is bad."
  1322. :group 'org-link-follow
  1323. :type 'boolean)
  1324. (defcustom org-return-follows-link nil
  1325. "Non-nil means on links RET will follow the link."
  1326. :group 'org-link-follow
  1327. :type 'boolean)
  1328. (defcustom org-mouse-1-follows-link
  1329. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1330. "Non-nil means mouse-1 on a link will follow the link.
  1331. A longer mouse click will still set point. Does not work on XEmacs.
  1332. Needs to be set before org.el is loaded."
  1333. :group 'org-link-follow
  1334. :type 'boolean)
  1335. (defcustom org-mark-ring-length 4
  1336. "Number of different positions to be recorded in the ring.
  1337. Changing this requires a restart of Emacs to work correctly."
  1338. :group 'org-link-follow
  1339. :type 'integer)
  1340. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1341. "Non-nil means internal links in Org files must exactly match a headline.
  1342. When nil, the link search tries to match a phrase with all words
  1343. in the search text."
  1344. :group 'org-link-follow
  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. :type 'regexp)
  1457. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1458. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1459. Elisp links can be dangerous: just think about a link
  1460. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1461. This link would show up in your Org-mode document as \"Google Search\",
  1462. but really it would remove your entire home directory.
  1463. Therefore we advise against setting this variable to nil.
  1464. Just change it to `y-or-n-p' if you want to confirm with a
  1465. single keystroke rather than having to type \"yes\"."
  1466. :group 'org-link-follow
  1467. :type '(choice
  1468. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1469. (const :tag "with y-or-n (faster)" y-or-n-p)
  1470. (const :tag "no confirmation (dangerous)" nil)))
  1471. (put 'org-confirm-shell-link-function
  1472. 'safe-local-variable
  1473. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1474. (defcustom org-confirm-elisp-link-not-regexp ""
  1475. "A regexp to skip confirmation for Elisp links."
  1476. :group 'org-link-follow
  1477. :type 'regexp)
  1478. (defconst org-file-apps-defaults-gnu
  1479. '((remote . emacs)
  1480. (system . mailcap)
  1481. (t . mailcap))
  1482. "Default file applications on a UNIX or GNU/Linux system.
  1483. See `org-file-apps'.")
  1484. (defconst org-file-apps-defaults-macosx
  1485. '((remote . emacs)
  1486. (t . "open %s")
  1487. (system . "open %s")
  1488. ("ps.gz" . "gv %s")
  1489. ("eps.gz" . "gv %s")
  1490. ("dvi" . "xdvi %s")
  1491. ("fig" . "xfig %s"))
  1492. "Default file applications on a MacOS X system.
  1493. The system \"open\" is known as a default, but we use X11 applications
  1494. for some files for which the OS does not have a good default.
  1495. See `org-file-apps'.")
  1496. (defconst org-file-apps-defaults-windowsnt
  1497. (list
  1498. '(remote . emacs)
  1499. (cons t
  1500. (list (if (featurep 'xemacs)
  1501. 'mswindows-shell-execute
  1502. 'w32-shell-execute)
  1503. "open" 'file))
  1504. (cons 'system
  1505. (list (if (featurep 'xemacs)
  1506. 'mswindows-shell-execute
  1507. 'w32-shell-execute)
  1508. "open" 'file)))
  1509. "Default file applications on a Windows NT system.
  1510. The system \"open\" is used for most files.
  1511. See `org-file-apps'.")
  1512. (defcustom org-file-apps
  1513. '(
  1514. (auto-mode . emacs)
  1515. ("\\.mm\\'" . default)
  1516. ("\\.x?html?\\'" . default)
  1517. ("\\.pdf\\'" . default)
  1518. )
  1519. "External applications for opening `file:path' items in a document.
  1520. Org-mode uses system defaults for different file types, but
  1521. you can use this variable to set the application for a given file
  1522. extension. The entries in this list are cons cells where the car identifies
  1523. files and the cdr the corresponding command. Possible values for the
  1524. file identifier are
  1525. \"string\" A string as a file identifier can be interpreted in different
  1526. ways, depending on its contents:
  1527. - Alphanumeric characters only:
  1528. Match links with this file extension.
  1529. Example: (\"pdf\" . \"evince %s\")
  1530. to open PDFs with evince.
  1531. - Regular expression: Match links where the
  1532. filename matches the regexp. If you want to
  1533. use groups here, use shy groups.
  1534. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1535. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1536. to open *.html and *.xhtml with firefox.
  1537. - Regular expression which contains (non-shy) groups:
  1538. Match links where the whole link, including \"::\", and
  1539. anything after that, matches the regexp.
  1540. In a custom command string, %1, %2, etc. are replaced with
  1541. the parts of the link that were matched by the groups.
  1542. For backwards compatibility, if a command string is given
  1543. that does not use any of the group matches, this case is
  1544. handled identically to the second one (i.e. match against
  1545. file name only).
  1546. In a custom lisp form, you can access the group matches with
  1547. (match-string n link).
  1548. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1549. to open [[file:document.pdf::5]] with evince at page 5.
  1550. `directory' Matches a directory
  1551. `remote' Matches a remote file, accessible through tramp or efs.
  1552. Remote files most likely should be visited through Emacs
  1553. because external applications cannot handle such paths.
  1554. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1555. so all files Emacs knows how to handle. Using this with
  1556. command `emacs' will open most files in Emacs. Beware that this
  1557. will also open html files inside Emacs, unless you add
  1558. (\"html\" . default) to the list as well.
  1559. t Default for files not matched by any of the other options.
  1560. `system' The system command to open files, like `open' on Windows
  1561. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1562. that will be selected if you call `C-c C-o' with a double
  1563. \\[universal-argument] \\[universal-argument] prefix.
  1564. Possible values for the command are:
  1565. `emacs' The file will be visited by the current Emacs process.
  1566. `default' Use the default application for this file type, which is the
  1567. association for t in the list, most likely in the system-specific
  1568. part.
  1569. This can be used to overrule an unwanted setting in the
  1570. system-specific variable.
  1571. `system' Use the system command for opening files, like \"open\".
  1572. This command is specified by the entry whose car is `system'.
  1573. Most likely, the system-specific version of this variable
  1574. does define this command, but you can overrule/replace it
  1575. here.
  1576. string A command to be executed by a shell; %s will be replaced
  1577. by the path to the file.
  1578. sexp A Lisp form which will be evaluated. The file path will
  1579. be available in the Lisp variable `file'.
  1580. For more examples, see the system specific constants
  1581. `org-file-apps-defaults-macosx'
  1582. `org-file-apps-defaults-windowsnt'
  1583. `org-file-apps-defaults-gnu'."
  1584. :group 'org-link-follow
  1585. :type '(repeat
  1586. (cons (choice :value ""
  1587. (string :tag "Extension")
  1588. (const :tag "System command to open files" system)
  1589. (const :tag "Default for unrecognized files" t)
  1590. (const :tag "Remote file" remote)
  1591. (const :tag "Links to a directory" directory)
  1592. (const :tag "Any files that have Emacs modes"
  1593. auto-mode))
  1594. (choice :value ""
  1595. (const :tag "Visit with Emacs" emacs)
  1596. (const :tag "Use default" default)
  1597. (const :tag "Use the system command" system)
  1598. (string :tag "Command")
  1599. (sexp :tag "Lisp form")))))
  1600. (defgroup org-refile nil
  1601. "Options concerning refiling entries in Org-mode."
  1602. :tag "Org Refile"
  1603. :group 'org)
  1604. (defcustom org-directory "~/org"
  1605. "Directory with org files.
  1606. This is just a default location to look for Org files. There is no need
  1607. at all to put your files into this directory. It is only used in the
  1608. following situations:
  1609. 1. When a remember template specifies a target file that is not an
  1610. absolute path. The path will then be interpreted relative to
  1611. `org-directory'
  1612. 2. When a remember note is filed away in an interactive way (when exiting the
  1613. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1614. with `org-directory' as the default path."
  1615. :group 'org-refile
  1616. :group 'org-remember
  1617. :type 'directory)
  1618. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1619. "Default target for storing notes.
  1620. Used as a fall back file for org-remember.el and org-capture.el, for
  1621. templates that do not specify a target file."
  1622. :group 'org-refile
  1623. :group 'org-remember
  1624. :type '(choice
  1625. (const :tag "Default from remember-data-file" nil)
  1626. file))
  1627. (defcustom org-goto-interface 'outline
  1628. "The default interface to be used for `org-goto'.
  1629. Allowed values are:
  1630. outline The interface shows an outline of the relevant file
  1631. and the correct heading is found by moving through
  1632. the outline or by searching with incremental search.
  1633. outline-path-completion Headlines in the current buffer are offered via
  1634. completion. This is the interface also used by
  1635. the refile command."
  1636. :group 'org-refile
  1637. :type '(choice
  1638. (const :tag "Outline" outline)
  1639. (const :tag "Outline-path-completion" outline-path-completion)))
  1640. (defcustom org-goto-max-level 5
  1641. "Maximum target level when running `org-goto' with refile interface."
  1642. :group 'org-refile
  1643. :type 'integer)
  1644. (defcustom org-reverse-note-order nil
  1645. "Non-nil means store new notes at the beginning of a file or entry.
  1646. When nil, new notes will be filed to the end of a file or entry.
  1647. This can also be a list with cons cells of regular expressions that
  1648. are matched against file names, and values."
  1649. :group 'org-remember
  1650. :group 'org-refile
  1651. :type '(choice
  1652. (const :tag "Reverse always" t)
  1653. (const :tag "Reverse never" nil)
  1654. (repeat :tag "By file name regexp"
  1655. (cons regexp boolean))))
  1656. (defcustom org-log-refile nil
  1657. "Information to record when a task is refiled.
  1658. Possible values are:
  1659. nil Don't add anything
  1660. time Add a time stamp to the task
  1661. note Prompt for a note and add it with template `org-log-note-headings'
  1662. This option can also be set with on a per-file-basis with
  1663. #+STARTUP: nologrefile
  1664. #+STARTUP: logrefile
  1665. #+STARTUP: lognoterefile
  1666. You can have local logging settings for a subtree by setting the LOGGING
  1667. property to one or more of these keywords.
  1668. When bulk-refiling from the agenda, the value `note' is forbidden and
  1669. will temporarily be changed to `time'."
  1670. :group 'org-refile
  1671. :group 'org-progress
  1672. :type '(choice
  1673. (const :tag "No logging" nil)
  1674. (const :tag "Record timestamp" time)
  1675. (const :tag "Record timestamp with note." note)))
  1676. (defcustom org-refile-targets nil
  1677. "Targets for refiling entries with \\[org-refile].
  1678. This is a list of cons cells. Each cell contains:
  1679. - a specification of the files to be considered, either a list of files,
  1680. or a symbol whose function or variable value will be used to retrieve
  1681. a file name or a list of file names. If you use `org-agenda-files' for
  1682. that, all agenda files will be scanned for targets. Nil means consider
  1683. headings in the current buffer.
  1684. - A specification of how to find candidate refile targets. This may be
  1685. any of:
  1686. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1687. This tag has to be present in all target headlines, inheritance will
  1688. not be considered.
  1689. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1690. todo keyword.
  1691. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1692. headlines that are refiling targets.
  1693. - a cons cell (:level . N). Any headline of level N is considered a target.
  1694. Note that, when `org-odd-levels-only' is set, level corresponds to
  1695. order in hierarchy, not to the number of stars.
  1696. - a cons cell (:maxlevel . N). Any headline with level <= N is 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. Each element of this list generates a set of possible targets.
  1700. The union of these sets is presented (with completion) to
  1701. the user by `org-refile'.
  1702. You can set the variable `org-refile-target-verify-function' to a function
  1703. to verify each headline found by the simple criteria above.
  1704. When this variable is nil, all top-level headlines in the current buffer
  1705. are used, equivalent to the value `((nil . (:level . 1))'."
  1706. :group 'org-refile
  1707. :type '(repeat
  1708. (cons
  1709. (choice :value org-agenda-files
  1710. (const :tag "All agenda files" org-agenda-files)
  1711. (const :tag "Current buffer" nil)
  1712. (function) (variable) (file))
  1713. (choice :tag "Identify target headline by"
  1714. (cons :tag "Specific tag" (const :value :tag) (string))
  1715. (cons :tag "TODO keyword" (const :value :todo) (string))
  1716. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1717. (cons :tag "Level number" (const :value :level) (integer))
  1718. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1719. (defcustom org-refile-target-verify-function nil
  1720. "Function to verify if the headline at point should be a refile target.
  1721. The function will be called without arguments, with point at the
  1722. beginning of the headline. It should return t and leave point
  1723. where it is if the headline is a valid target for refiling.
  1724. If the target should not be selected, the function must return nil.
  1725. In addition to this, it may move point to a place from where the search
  1726. should be continued. For example, the function may decide that the entire
  1727. subtree of the current entry should be excluded and move point to the end
  1728. of the subtree."
  1729. :group 'org-refile
  1730. :type 'function)
  1731. (defcustom org-refile-use-cache nil
  1732. "Non-nil means cache refile targets to speed up the process.
  1733. The cache for a particular file will be updated automatically when
  1734. the buffer has been killed, or when any of the marker used for flagging
  1735. refile targets no longer points at a live buffer.
  1736. If you have added new entries to a buffer that might themselves be targets,
  1737. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1738. find that easier, `C-u C-u C-u C-c C-w'."
  1739. :group 'org-refile
  1740. :type 'boolean)
  1741. (defcustom org-refile-use-outline-path nil
  1742. "Non-nil means provide refile targets as paths.
  1743. So a level 3 headline will be available as level1/level2/level3.
  1744. When the value is `file', also include the file name (without directory)
  1745. into the path. In this case, you can also stop the completion after
  1746. the file name, to get entries inserted as top level in the file.
  1747. When `full-file-path', include the full file path."
  1748. :group 'org-refile
  1749. :type '(choice
  1750. (const :tag "Not" nil)
  1751. (const :tag "Yes" t)
  1752. (const :tag "Start with file name" file)
  1753. (const :tag "Start with full file path" full-file-path)))
  1754. (defcustom org-outline-path-complete-in-steps t
  1755. "Non-nil means complete the outline path in hierarchical steps.
  1756. When Org-mode uses the refile interface to select an outline path
  1757. \(see variable `org-refile-use-outline-path'), the completion of
  1758. the path can be done is a single go, or if can be done in steps down
  1759. the headline hierarchy. Going in steps is probably the best if you
  1760. do not use a special completion package like `ido' or `icicles'.
  1761. However, when using these packages, going in one step can be very
  1762. fast, while still showing the whole path to the entry."
  1763. :group 'org-refile
  1764. :type 'boolean)
  1765. (defcustom org-refile-allow-creating-parent-nodes nil
  1766. "Non-nil means allow to create new nodes as refile targets.
  1767. New nodes are then created by adding \"/new node name\" to the completion
  1768. of an existing node. When the value of this variable is `confirm',
  1769. new node creation must be confirmed by the user (recommended)
  1770. When nil, the completion must match an existing entry.
  1771. Note that, if the new heading is not seen by the criteria
  1772. listed in `org-refile-targets', multiple instances of the same
  1773. heading would be created by trying again to file under the new
  1774. heading."
  1775. :group 'org-refile
  1776. :type '(choice
  1777. (const :tag "Never" nil)
  1778. (const :tag "Always" t)
  1779. (const :tag "Prompt for confirmation" confirm)))
  1780. (defcustom org-refile-active-region-within-subtree nil
  1781. "Non-nil means also refile active region within a subtree.
  1782. By default `org-refile' doesn't allow refiling regions if they
  1783. don't contain a set of subtrees, but it might be convenient to
  1784. do so sometimes: in that case, the first line of the region is
  1785. converted to a headline before refiling."
  1786. :group 'org-refile
  1787. :type 'boolean)
  1788. (defgroup org-todo nil
  1789. "Options concerning TODO items in Org-mode."
  1790. :tag "Org TODO"
  1791. :group 'org)
  1792. (defgroup org-progress nil
  1793. "Options concerning Progress logging in Org-mode."
  1794. :tag "Org Progress"
  1795. :group 'org-time)
  1796. (defvar org-todo-interpretation-widgets
  1797. '((:tag "Sequence (cycling hits every state)" sequence)
  1798. (:tag "Type (cycling directly to DONE)" type))
  1799. "The available interpretation symbols for customizing `org-todo-keywords'.
  1800. Interested libraries should add to this list.")
  1801. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1802. "List of TODO entry keyword sequences and their interpretation.
  1803. \\<org-mode-map>This is a list of sequences.
  1804. Each sequence starts with a symbol, either `sequence' or `type',
  1805. indicating if the keywords should be interpreted as a sequence of
  1806. action steps, or as different types of TODO items. The first
  1807. keywords are states requiring action - these states will select a headline
  1808. for inclusion into the global TODO list Org-mode produces. If one of
  1809. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1810. signify that no further action is necessary. If \"|\" is not found,
  1811. the last keyword is treated as the only DONE state of the sequence.
  1812. The command \\[org-todo] cycles an entry through these states, and one
  1813. additional state where no keyword is present. For details about this
  1814. cycling, see the manual.
  1815. TODO keywords and interpretation can also be set on a per-file basis with
  1816. the special #+SEQ_TODO and #+TYP_TODO lines.
  1817. Each keyword can optionally specify a character for fast state selection
  1818. \(in combination with the variable `org-use-fast-todo-selection')
  1819. and specifiers for state change logging, using the same syntax
  1820. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1821. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1822. indicates to record a time stamp each time this state is selected.
  1823. Each keyword may also specify if a timestamp or a note should be
  1824. recorded when entering or leaving the state, by adding additional
  1825. characters in the parenthesis after the keyword. This looks like this:
  1826. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1827. record only the time of the state change. With X and Y being either
  1828. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1829. Y when leaving the state if and only if the *target* state does not
  1830. define X. You may omit any of the fast-selection key or X or /Y,
  1831. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1832. For backward compatibility, this variable may also be just a list
  1833. of keywords - in this case the interpretation (sequence or type) will be
  1834. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1835. :group 'org-todo
  1836. :group 'org-keywords
  1837. :type '(choice
  1838. (repeat :tag "Old syntax, just keywords"
  1839. (string :tag "Keyword"))
  1840. (repeat :tag "New syntax"
  1841. (cons
  1842. (choice
  1843. :tag "Interpretation"
  1844. ;;Quick and dirty way to see
  1845. ;;`org-todo-interpretations'. This takes the
  1846. ;;place of item arguments
  1847. :convert-widget
  1848. (lambda (widget)
  1849. (widget-put widget
  1850. :args (mapcar
  1851. #'(lambda (x)
  1852. (widget-convert
  1853. (cons 'const x)))
  1854. org-todo-interpretation-widgets))
  1855. widget))
  1856. (repeat
  1857. (string :tag "Keyword"))))))
  1858. (defvar org-todo-keywords-1 nil
  1859. "All TODO and DONE keywords active in a buffer.")
  1860. (make-variable-buffer-local 'org-todo-keywords-1)
  1861. (defvar org-todo-keywords-for-agenda nil)
  1862. (defvar org-done-keywords-for-agenda nil)
  1863. (defvar org-drawers-for-agenda nil)
  1864. (defvar org-todo-keyword-alist-for-agenda nil)
  1865. (defvar org-tag-alist-for-agenda nil)
  1866. (defvar org-agenda-contributing-files nil)
  1867. (defvar org-not-done-keywords nil)
  1868. (make-variable-buffer-local 'org-not-done-keywords)
  1869. (defvar org-done-keywords nil)
  1870. (make-variable-buffer-local 'org-done-keywords)
  1871. (defvar org-todo-heads nil)
  1872. (make-variable-buffer-local 'org-todo-heads)
  1873. (defvar org-todo-sets nil)
  1874. (make-variable-buffer-local 'org-todo-sets)
  1875. (defvar org-todo-log-states nil)
  1876. (make-variable-buffer-local 'org-todo-log-states)
  1877. (defvar org-todo-kwd-alist nil)
  1878. (make-variable-buffer-local 'org-todo-kwd-alist)
  1879. (defvar org-todo-key-alist nil)
  1880. (make-variable-buffer-local 'org-todo-key-alist)
  1881. (defvar org-todo-key-trigger nil)
  1882. (make-variable-buffer-local 'org-todo-key-trigger)
  1883. (defcustom org-todo-interpretation 'sequence
  1884. "Controls how TODO keywords are interpreted.
  1885. This variable is in principle obsolete and is only used for
  1886. backward compatibility, if the interpretation of todo keywords is
  1887. not given already in `org-todo-keywords'. See that variable for
  1888. more information."
  1889. :group 'org-todo
  1890. :group 'org-keywords
  1891. :type '(choice (const sequence)
  1892. (const type)))
  1893. (defcustom org-use-fast-todo-selection t
  1894. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1895. This variable describes if and under what circumstances the cycling
  1896. mechanism for TODO keywords will be replaced by a single-key, direct
  1897. selection scheme.
  1898. When nil, fast selection is never used.
  1899. When the symbol `prefix', it will be used when `org-todo' is called with
  1900. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1901. in an agenda buffer.
  1902. When t, fast selection is used by default. In this case, the prefix
  1903. argument forces cycling instead.
  1904. In all cases, the special interface is only used if access keys have actually
  1905. been assigned by the user, i.e. if keywords in the configuration are followed
  1906. by a letter in parenthesis, like TODO(t)."
  1907. :group 'org-todo
  1908. :type '(choice
  1909. (const :tag "Never" nil)
  1910. (const :tag "By default" t)
  1911. (const :tag "Only with C-u C-c C-t" prefix)))
  1912. (defcustom org-provide-todo-statistics t
  1913. "Non-nil means update todo statistics after insert and toggle.
  1914. ALL-HEADLINES means update todo statistics by including headlines
  1915. with no TODO keyword as well, counting them as not done.
  1916. A list of TODO keywords means the same, but skip keywords that are
  1917. not in this list.
  1918. When this is set, todo statistics is updated in the parent of the
  1919. current entry each time a todo state is changed."
  1920. :group 'org-todo
  1921. :type '(choice
  1922. (const :tag "Yes, only for TODO entries" t)
  1923. (const :tag "Yes, including all entries" 'all-headlines)
  1924. (repeat :tag "Yes, for TODOs in this list"
  1925. (string :tag "TODO keyword"))
  1926. (other :tag "No TODO statistics" nil)))
  1927. (defcustom org-hierarchical-todo-statistics t
  1928. "Non-nil means TODO statistics covers just direct children.
  1929. When nil, all entries in the subtree are considered.
  1930. This has only an effect if `org-provide-todo-statistics' is set.
  1931. To set this to nil for only a single subtree, use a COOKIE_DATA
  1932. property and include the word \"recursive\" into the value."
  1933. :group 'org-todo
  1934. :type 'boolean)
  1935. (defcustom org-after-todo-state-change-hook nil
  1936. "Hook which is run after the state of a TODO item was changed.
  1937. The new state (a string with a TODO keyword, or nil) is available in the
  1938. Lisp variable `state'."
  1939. :group 'org-todo
  1940. :type 'hook)
  1941. (defvar org-blocker-hook nil
  1942. "Hook for functions that are allowed to block a state change.
  1943. Each function gets as its single argument a property list, see
  1944. `org-trigger-hook' for more information about this list.
  1945. If any of the functions in this hook returns nil, the state change
  1946. is blocked.")
  1947. (defvar org-trigger-hook nil
  1948. "Hook for functions that are triggered by a state change.
  1949. Each function gets as its single argument a property list with at least
  1950. the following elements:
  1951. (:type type-of-change :position pos-at-entry-start
  1952. :from old-state :to new-state)
  1953. Depending on the type, more properties may be present.
  1954. This mechanism is currently implemented for:
  1955. TODO state changes
  1956. ------------------
  1957. :type todo-state-change
  1958. :from previous state (keyword as a string), or nil, or a symbol
  1959. 'todo' or 'done', to indicate the general type of state.
  1960. :to new state, like in :from")
  1961. (defcustom org-enforce-todo-dependencies nil
  1962. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1963. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1964. be blocked if any prior sibling is not yet done.
  1965. Finally, if the parent is blocked because of ordered siblings of its own,
  1966. the child will also be blocked.
  1967. This variable needs to be set before org.el is loaded, and you need to
  1968. restart Emacs after a change to make the change effective. The only way
  1969. to change is while Emacs is running is through the customize interface."
  1970. :set (lambda (var val)
  1971. (set var val)
  1972. (if val
  1973. (add-hook 'org-blocker-hook
  1974. 'org-block-todo-from-children-or-siblings-or-parent)
  1975. (remove-hook 'org-blocker-hook
  1976. 'org-block-todo-from-children-or-siblings-or-parent)))
  1977. :group 'org-todo
  1978. :type 'boolean)
  1979. (defcustom org-enforce-todo-checkbox-dependencies nil
  1980. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1981. When this is nil, checkboxes have no influence on switching TODO states.
  1982. When non-nil, you first need to check off all check boxes before the TODO
  1983. entry can be switched to DONE.
  1984. This variable needs to be set before org.el is loaded, and you need to
  1985. restart Emacs after a change to make the change effective. The only way
  1986. to change is while Emacs is running is through the customize interface."
  1987. :set (lambda (var val)
  1988. (set var val)
  1989. (if val
  1990. (add-hook 'org-blocker-hook
  1991. 'org-block-todo-from-checkboxes)
  1992. (remove-hook 'org-blocker-hook
  1993. 'org-block-todo-from-checkboxes)))
  1994. :group 'org-todo
  1995. :type 'boolean)
  1996. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1997. "Non-nil means inserting a TODO heading is treated as state change.
  1998. So when the command \\[org-insert-todo-heading] is used, state change
  1999. logging will apply if appropriate. When nil, the new TODO item will
  2000. be inserted directly, and no logging will take place."
  2001. :group 'org-todo
  2002. :type 'boolean)
  2003. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2004. "Non-nil means switching TODO states with S-cursor counts as state change.
  2005. This is the default behavior. However, setting this to nil allows a
  2006. convenient way to select a TODO state and bypass any logging associated
  2007. with that."
  2008. :group 'org-todo
  2009. :type 'boolean)
  2010. (defcustom org-todo-state-tags-triggers nil
  2011. "Tag changes that should be triggered by TODO state changes.
  2012. This is a list. Each entry is
  2013. (state-change (tag . flag) .......)
  2014. State-change can be a string with a state, and empty string to indicate the
  2015. state that has no TODO keyword, or it can be one of the symbols `todo'
  2016. or `done', meaning any not-done or done state, respectively."
  2017. :group 'org-todo
  2018. :group 'org-tags
  2019. :type '(repeat
  2020. (cons (choice :tag "When changing to"
  2021. (const :tag "Not-done state" todo)
  2022. (const :tag "Done state" done)
  2023. (string :tag "State"))
  2024. (repeat
  2025. (cons :tag "Tag action"
  2026. (string :tag "Tag")
  2027. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2028. (defcustom org-log-done nil
  2029. "Information to record when a task moves to the DONE state.
  2030. Possible values are:
  2031. nil Don't add anything, just change the keyword
  2032. time Add a time stamp to the task
  2033. note Prompt for a note and add it with template `org-log-note-headings'
  2034. This option can also be set with on a per-file-basis with
  2035. #+STARTUP: nologdone
  2036. #+STARTUP: logdone
  2037. #+STARTUP: lognotedone
  2038. You can have local logging settings for a subtree by setting the LOGGING
  2039. property to one or more of these keywords."
  2040. :group 'org-todo
  2041. :group 'org-progress
  2042. :type '(choice
  2043. (const :tag "No logging" nil)
  2044. (const :tag "Record CLOSED timestamp" time)
  2045. (const :tag "Record CLOSED timestamp with note." note)))
  2046. ;; Normalize old uses of org-log-done.
  2047. (cond
  2048. ((eq org-log-done t) (setq org-log-done 'time))
  2049. ((and (listp org-log-done) (memq 'done org-log-done))
  2050. (setq org-log-done 'note)))
  2051. (defcustom org-log-reschedule nil
  2052. "Information to record when the scheduling date of a tasks is modified.
  2053. Possible values are:
  2054. nil Don't add anything, just change the date
  2055. time Add a time stamp to the task
  2056. note Prompt for a note and add it with template `org-log-note-headings'
  2057. This option can also be set with on a per-file-basis with
  2058. #+STARTUP: nologreschedule
  2059. #+STARTUP: logreschedule
  2060. #+STARTUP: lognotereschedule"
  2061. :group 'org-todo
  2062. :group 'org-progress
  2063. :type '(choice
  2064. (const :tag "No logging" nil)
  2065. (const :tag "Record timestamp" time)
  2066. (const :tag "Record timestamp with note." note)))
  2067. (defcustom org-log-redeadline nil
  2068. "Information to record when the deadline date of a tasks is modified.
  2069. Possible values are:
  2070. nil Don't add anything, just change the date
  2071. time Add a time stamp to the task
  2072. note Prompt for a note and add it with template `org-log-note-headings'
  2073. This option can also be set with on a per-file-basis with
  2074. #+STARTUP: nologredeadline
  2075. #+STARTUP: logredeadline
  2076. #+STARTUP: lognoteredeadline
  2077. You can have local logging settings for a subtree by setting the LOGGING
  2078. property to one or more of these keywords."
  2079. :group 'org-todo
  2080. :group 'org-progress
  2081. :type '(choice
  2082. (const :tag "No logging" nil)
  2083. (const :tag "Record timestamp" time)
  2084. (const :tag "Record timestamp with note." note)))
  2085. (defcustom org-log-note-clock-out nil
  2086. "Non-nil means record a note when clocking out of an item.
  2087. This can also be configured on a per-file basis by adding one of
  2088. the following lines anywhere in the buffer:
  2089. #+STARTUP: lognoteclock-out
  2090. #+STARTUP: nolognoteclock-out"
  2091. :group 'org-todo
  2092. :group 'org-progress
  2093. :type 'boolean)
  2094. (defcustom org-log-done-with-time t
  2095. "Non-nil means the CLOSED time stamp will contain date and time.
  2096. When nil, only the date will be recorded."
  2097. :group 'org-progress
  2098. :type 'boolean)
  2099. (defcustom org-log-note-headings
  2100. '((done . "CLOSING NOTE %t")
  2101. (state . "State %-12s from %-12S %t")
  2102. (note . "Note taken on %t")
  2103. (reschedule . "Rescheduled from %S on %t")
  2104. (delschedule . "Not scheduled, was %S on %t")
  2105. (redeadline . "New deadline from %S on %t")
  2106. (deldeadline . "Removed deadline, was %S on %t")
  2107. (refile . "Refiled on %t")
  2108. (clock-out . ""))
  2109. "Headings for notes added to entries.
  2110. The value is an alist, with the car being a symbol indicating the note
  2111. context, and the cdr is the heading to be used. The heading may also be the
  2112. empty string.
  2113. %t in the heading will be replaced by a time stamp.
  2114. %T will be an active time stamp instead the default inactive one
  2115. %d will be replaced by a short-format time stamp.
  2116. %D will be replaced by an active short-format time stamp.
  2117. %s will be replaced by the new TODO state, in double quotes.
  2118. %S will be replaced by the old TODO state, in double quotes.
  2119. %u will be replaced by the user name.
  2120. %U will be replaced by the full user name.
  2121. In fact, it is not a good idea to change the `state' entry, because
  2122. agenda log mode depends on the format of these entries."
  2123. :group 'org-todo
  2124. :group 'org-progress
  2125. :type '(list :greedy t
  2126. (cons (const :tag "Heading when closing an item" done) string)
  2127. (cons (const :tag
  2128. "Heading when changing todo state (todo sequence only)"
  2129. state) string)
  2130. (cons (const :tag "Heading when just taking a note" note) string)
  2131. (cons (const :tag "Heading when clocking out" clock-out) string)
  2132. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2133. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2134. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2135. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2136. (cons (const :tag "Heading when refiling" refile) string)))
  2137. (unless (assq 'note org-log-note-headings)
  2138. (push '(note . "%t") org-log-note-headings))
  2139. (defcustom org-log-into-drawer nil
  2140. "Non-nil means insert state change notes and time stamps into a drawer.
  2141. When nil, state changes notes will be inserted after the headline and
  2142. any scheduling and clock lines, but not inside a drawer.
  2143. The value of this variable should be the name of the drawer to use.
  2144. LOGBOOK is proposed as the default drawer for this purpose, you can
  2145. also set this to a string to define the drawer of your choice.
  2146. A value of t is also allowed, representing \"LOGBOOK\".
  2147. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2148. will be ignored.
  2149. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2150. a subtree."
  2151. :group 'org-todo
  2152. :group 'org-progress
  2153. :type '(choice
  2154. (const :tag "Not into a drawer" nil)
  2155. (const :tag "LOGBOOK" t)
  2156. (string :tag "Other")))
  2157. (if (fboundp 'defvaralias)
  2158. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2159. (defun org-log-into-drawer ()
  2160. "Return the value of `org-log-into-drawer', but let properties overrule.
  2161. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2162. used instead of the default value."
  2163. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2164. (cond
  2165. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2166. ((equal p "t") "LOGBOOK")
  2167. (t p))))
  2168. (defcustom org-log-state-notes-insert-after-drawers nil
  2169. "Non-nil means insert state change notes after any drawers in entry.
  2170. Only the drawers that *immediately* follow the headline and the
  2171. deadline/scheduled line are skipped.
  2172. When nil, insert notes right after the heading and perhaps the line
  2173. with deadline/scheduling if present.
  2174. This variable will have no effect if `org-log-into-drawer' is
  2175. set."
  2176. :group 'org-todo
  2177. :group 'org-progress
  2178. :type 'boolean)
  2179. (defcustom org-log-states-order-reversed t
  2180. "Non-nil means the latest state note will be directly after heading.
  2181. When nil, the state change notes will be ordered according to time."
  2182. :group 'org-todo
  2183. :group 'org-progress
  2184. :type 'boolean)
  2185. (defcustom org-todo-repeat-to-state nil
  2186. "The TODO state to which a repeater should return the repeating task.
  2187. By default this is the first task in a TODO sequence, or the previous state
  2188. in a TODO_TYP set. But you can specify another task here.
  2189. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2190. :group 'org-todo
  2191. :type '(choice (const :tag "Head of sequence" nil)
  2192. (string :tag "Specific state")))
  2193. (defcustom org-log-repeat 'time
  2194. "Non-nil means record moving through the DONE state when triggering repeat.
  2195. An auto-repeating task is immediately switched back to TODO when
  2196. marked DONE. If you are not logging state changes (by adding \"@\"
  2197. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2198. record a closing note, there will be no record of the task moving
  2199. through DONE. This variable forces taking a note anyway.
  2200. nil Don't force a record
  2201. time Record a time stamp
  2202. note Record a note
  2203. This option can also be set with on a per-file-basis with
  2204. #+STARTUP: logrepeat
  2205. #+STARTUP: lognoterepeat
  2206. #+STARTUP: nologrepeat
  2207. You can have local logging settings for a subtree by setting the LOGGING
  2208. property to one or more of these keywords."
  2209. :group 'org-todo
  2210. :group 'org-progress
  2211. :type '(choice
  2212. (const :tag "Don't force a record" nil)
  2213. (const :tag "Force recording the DONE state" time)
  2214. (const :tag "Force recording a note with the DONE state" note)))
  2215. (defgroup org-priorities nil
  2216. "Priorities in Org-mode."
  2217. :tag "Org Priorities"
  2218. :group 'org-todo)
  2219. (defcustom org-enable-priority-commands t
  2220. "Non-nil means priority commands are active.
  2221. When nil, these commands will be disabled, so that you never accidentally
  2222. set a priority."
  2223. :group 'org-priorities
  2224. :type 'boolean)
  2225. (defcustom org-highest-priority ?A
  2226. "The highest priority of TODO items. A character like ?A, ?B etc.
  2227. Must have a smaller ASCII number than `org-lowest-priority'."
  2228. :group 'org-priorities
  2229. :type 'character)
  2230. (defcustom org-lowest-priority ?C
  2231. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2232. Must have a larger ASCII number than `org-highest-priority'."
  2233. :group 'org-priorities
  2234. :type 'character)
  2235. (defcustom org-default-priority ?B
  2236. "The default priority of TODO items.
  2237. This is the priority an item gets if no explicit priority is given.
  2238. When starting to cycle on an empty priority the first step in the cycle
  2239. depends on `org-priority-start-cycle-with-default'. The resulting first
  2240. step priority must not exceed the range from `org-highest-priority' to
  2241. `org-lowest-priority' which means that `org-default-priority' has to be
  2242. in this range exclusive or inclusive the range boundaries. Else the
  2243. first step refuses to set the default and the second will fall back
  2244. to (depending on the command used) the highest or lowest priority."
  2245. :group 'org-priorities
  2246. :type 'character)
  2247. (defcustom org-priority-start-cycle-with-default t
  2248. "Non-nil means start with default priority when starting to cycle.
  2249. When this is nil, the first step in the cycle will be (depending on the
  2250. command used) one higher or lower than the default priority.
  2251. See also `org-default-priority'."
  2252. :group 'org-priorities
  2253. :type 'boolean)
  2254. (defcustom org-get-priority-function nil
  2255. "Function to extract the priority from a string.
  2256. The string is normally the headline. If this is nil Org computes the
  2257. priority from the priority cookie like [#A] in the headline. It returns
  2258. an integer, increasing by 1000 for each priority level.
  2259. The user can set a different function here, which should take a string
  2260. as an argument and return the numeric priority."
  2261. :group 'org-priorities
  2262. :type 'function)
  2263. (defgroup org-time nil
  2264. "Options concerning time stamps and deadlines in Org-mode."
  2265. :tag "Org Time"
  2266. :group 'org)
  2267. (defcustom org-insert-labeled-timestamps-at-point nil
  2268. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2269. When nil, these labeled time stamps are forces into the second line of an
  2270. entry, just after the headline. When scheduling from the global TODO list,
  2271. the time stamp will always be forced into the second line."
  2272. :group 'org-time
  2273. :type 'boolean)
  2274. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2275. "Formats for `format-time-string' which are used for time stamps.
  2276. It is not recommended to change this constant.")
  2277. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2278. "Number of minutes to round time stamps to.
  2279. These are two values, the first applies when first creating a time stamp.
  2280. The second applies when changing it with the commands `S-up' and `S-down'.
  2281. When changing the time stamp, this means that it will change in steps
  2282. of N minutes, as given by the second value.
  2283. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2284. numbers should be factors of 60, so for example 5, 10, 15.
  2285. When this is larger than 1, you can still force an exact time stamp by using
  2286. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2287. and by using a prefix arg to `S-up/down' to specify the exact number
  2288. of minutes to shift."
  2289. :group 'org-time
  2290. :get #'(lambda (var) ; Make sure both elements are there
  2291. (if (integerp (default-value var))
  2292. (list (default-value var) 5)
  2293. (default-value var)))
  2294. :type '(list
  2295. (integer :tag "when inserting times")
  2296. (integer :tag "when modifying times")))
  2297. ;; Normalize old customizations of this variable.
  2298. (when (integerp org-time-stamp-rounding-minutes)
  2299. (setq org-time-stamp-rounding-minutes
  2300. (list org-time-stamp-rounding-minutes
  2301. org-time-stamp-rounding-minutes)))
  2302. (defcustom org-display-custom-times nil
  2303. "Non-nil means overlay custom formats over all time stamps.
  2304. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2305. To turn this on on a per-file basis, insert anywhere in the file:
  2306. #+STARTUP: customtime"
  2307. :group 'org-time
  2308. :set 'set-default
  2309. :type 'sexp)
  2310. (make-variable-buffer-local 'org-display-custom-times)
  2311. (defcustom org-time-stamp-custom-formats
  2312. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2313. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2314. These are overlaid over the default ISO format if the variable
  2315. `org-display-custom-times' is set. Time like %H:%M should be at the
  2316. end of the second format. The custom formats are also honored by export
  2317. commands, if custom time display is turned on at the time of export."
  2318. :group 'org-time
  2319. :type 'sexp)
  2320. (defun org-time-stamp-format (&optional long inactive)
  2321. "Get the right format for a time string."
  2322. (let ((f (if long (cdr org-time-stamp-formats)
  2323. (car org-time-stamp-formats))))
  2324. (if inactive
  2325. (concat "[" (substring f 1 -1) "]")
  2326. f)))
  2327. (defcustom org-time-clocksum-format "%d:%02d"
  2328. "The format string used when creating CLOCKSUM lines.
  2329. This is also used when org-mode generates a time duration."
  2330. :group 'org-time
  2331. :type 'string)
  2332. (defcustom org-time-clocksum-use-fractional nil
  2333. "If non-nil, \\[org-clock-display] uses fractional times.
  2334. org-mode generates a time duration."
  2335. :group 'org-time
  2336. :type 'boolean)
  2337. (defcustom org-time-clocksum-fractional-format "%.2f"
  2338. "The format string used when creating CLOCKSUM lines, or when
  2339. org-mode generates a time duration."
  2340. :group 'org-time
  2341. :type 'string)
  2342. (defcustom org-deadline-warning-days 14
  2343. "No. of days before expiration during which a deadline becomes active.
  2344. This variable governs the display in sparse trees and in the agenda.
  2345. When 0 or negative, it means use this number (the absolute value of it)
  2346. even if a deadline has a different individual lead time specified.
  2347. Custom commands can set this variable in the options section."
  2348. :group 'org-time
  2349. :group 'org-agenda-daily/weekly
  2350. :type 'integer)
  2351. (defcustom org-read-date-prefer-future t
  2352. "Non-nil means assume future for incomplete date input from user.
  2353. This affects the following situations:
  2354. 1. The user gives a month but not a year.
  2355. For example, if it is April and you enter \"feb 2\", this will be read
  2356. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2357. 2. The user gives a day, but no month.
  2358. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2359. read this as the third of *next* month. However, if you enter \"17\",
  2360. it will be considered as *this* month.
  2361. If you set this variable to the symbol `time', then also the following
  2362. will work:
  2363. 3. If the user gives a time, but no day. If the time is before now,
  2364. to will be interpreted as tomorrow.
  2365. Currently none of this works for ISO week specifications.
  2366. When this option is nil, the current day, month and year will always be
  2367. used as defaults.
  2368. See also `org-agenda-jump-prefer-future'."
  2369. :group 'org-time
  2370. :type '(choice
  2371. (const :tag "Never" nil)
  2372. (const :tag "Check month and day" t)
  2373. (const :tag "Check month, day, and time" time)))
  2374. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2375. "Should the agenda jump command prefer the future for incomplete dates?
  2376. The default is to do the same as configured in `org-read-date-prefer-future'.
  2377. But you can also set a deviating value here.
  2378. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2379. :group 'org-agenda
  2380. :group 'org-time
  2381. :type '(choice
  2382. (const :tag "Use org-read-date-prefer-future"
  2383. org-read-date-prefer-future)
  2384. (const :tag "Never" nil)
  2385. (const :tag "Always" t)))
  2386. (defcustom org-read-date-force-compatible-dates t
  2387. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2388. Depending on the system Emacs is running on, certain dates cannot
  2389. be represented with the type used internally to represent time.
  2390. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2391. correctly. Some systems allow for earlier dates, some for later,
  2392. some for both. One way to find out it to insert any date into an
  2393. Org buffer, putting the cursor on the year and hitting S-up and
  2394. S-down to test the range.
  2395. When this variable is set to t, the date/time prompt will not let
  2396. you specify dates outside the 1970-2037 range, so it is certain that
  2397. these dates will work in whatever version of Emacs you are
  2398. running, and also that you can move a file from one Emacs implementation
  2399. to another. WHenever Org is forcing the year for you, it will display
  2400. a message and beep.
  2401. When this variable is nil, Org will check if the date is
  2402. representable in the specific Emacs implementation you are using.
  2403. If not, it will force a year, usually the current year, and beep
  2404. to remind you. Currently this setting is not recommended because
  2405. the likelihood that you will open your Org files in an Emacs that
  2406. has limited date range is not negligible.
  2407. A workaround for this problem is to use diary sexp dates for time
  2408. stamps outside of this range."
  2409. :group 'org-time
  2410. :type 'boolean)
  2411. (defcustom org-read-date-display-live t
  2412. "Non-nil means display current interpretation of date prompt live.
  2413. This display will be in an overlay, in the minibuffer."
  2414. :group 'org-time
  2415. :type 'boolean)
  2416. (defcustom org-read-date-popup-calendar t
  2417. "Non-nil means pop up a calendar when prompting for a date.
  2418. In the calendar, the date can be selected with mouse-1. However, the
  2419. minibuffer will also be active, and you can simply enter the date as well.
  2420. When nil, only the minibuffer will be available."
  2421. :group 'org-time
  2422. :type 'boolean)
  2423. (if (fboundp 'defvaralias)
  2424. (defvaralias 'org-popup-calendar-for-date-prompt
  2425. 'org-read-date-popup-calendar))
  2426. (defcustom org-read-date-minibuffer-setup-hook nil
  2427. "Hook to be used to set up keys for the date/time interface.
  2428. Add key definitions to `minibuffer-local-map', which will be a temporary
  2429. copy."
  2430. :group 'org-time
  2431. :type 'hook)
  2432. (defcustom org-extend-today-until 0
  2433. "The hour when your day really ends. Must be an integer.
  2434. This has influence for the following applications:
  2435. - When switching the agenda to \"today\". It it is still earlier than
  2436. the time given here, the day recognized as TODAY is actually yesterday.
  2437. - When a date is read from the user and it is still before the time given
  2438. here, the current date and time will be assumed to be yesterday, 23:59.
  2439. Also, timestamps inserted in remember templates follow this rule.
  2440. IMPORTANT: This is a feature whose implementation is and likely will
  2441. remain incomplete. Really, it is only here because past midnight seems to
  2442. be the favorite working time of John Wiegley :-)"
  2443. :group 'org-time
  2444. :type 'integer)
  2445. (defcustom org-use-effective-time nil
  2446. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2447. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2448. \"effective time\" of any timestamps between midnight and 8am will be
  2449. 23:59 of the previous day."
  2450. :group 'boolean
  2451. :type 'integer)
  2452. (defcustom org-edit-timestamp-down-means-later nil
  2453. "Non-nil means S-down will increase the time in a time stamp.
  2454. When nil, S-up will increase."
  2455. :group 'org-time
  2456. :type 'boolean)
  2457. (defcustom org-calendar-follow-timestamp-change t
  2458. "Non-nil means make the calendar window follow timestamp changes.
  2459. When a timestamp is modified and the calendar window is visible, it will be
  2460. moved to the new date."
  2461. :group 'org-time
  2462. :type 'boolean)
  2463. (defgroup org-tags nil
  2464. "Options concerning tags in Org-mode."
  2465. :tag "Org Tags"
  2466. :group 'org)
  2467. (defcustom org-tag-alist nil
  2468. "List of tags allowed in Org-mode files.
  2469. When this list is nil, Org-mode will base TAG input on what is already in the
  2470. buffer.
  2471. The value of this variable is an alist, the car of each entry must be a
  2472. keyword as a string, the cdr may be a character that is used to select
  2473. that tag through the fast-tag-selection interface.
  2474. See the manual for details."
  2475. :group 'org-tags
  2476. :type '(repeat
  2477. (choice
  2478. (cons (string :tag "Tag name")
  2479. (character :tag "Access char"))
  2480. (list :tag "Start radio group"
  2481. (const :startgroup)
  2482. (option (string :tag "Group description")))
  2483. (list :tag "End radio group"
  2484. (const :endgroup)
  2485. (option (string :tag "Group description")))
  2486. (const :tag "New line" (:newline)))))
  2487. (defcustom org-tag-persistent-alist nil
  2488. "List of tags that will always appear in all Org-mode files.
  2489. This is in addition to any in buffer settings or customizations
  2490. of `org-tag-alist'.
  2491. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2492. The value of this variable is an alist, the car of each entry must be a
  2493. keyword as a string, the cdr may be a character that is used to select
  2494. that tag through the fast-tag-selection interface.
  2495. See the manual for details.
  2496. To disable these tags on a per-file basis, insert anywhere in the file:
  2497. #+STARTUP: noptag"
  2498. :group 'org-tags
  2499. :type '(repeat
  2500. (choice
  2501. (cons (string :tag "Tag name")
  2502. (character :tag "Access char"))
  2503. (const :tag "Start radio group" (:startgroup))
  2504. (const :tag "End radio group" (:endgroup))
  2505. (const :tag "New line" (:newline)))))
  2506. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2507. "If non-nil, always offer completion for all tags of all agenda files.
  2508. Instead of customizing this variable directly, you might want to
  2509. set it locally for capture buffers, because there no list of
  2510. tags in that file can be created dynamically (there are none).
  2511. (add-hook 'org-capture-mode-hook
  2512. (lambda ()
  2513. (set (make-local-variable
  2514. 'org-complete-tags-always-offer-all-agenda-tags)
  2515. t)))"
  2516. :group 'org-tags
  2517. :type 'boolean)
  2518. (defvar org-file-tags nil
  2519. "List of tags that can be inherited by all entries in the file.
  2520. The tags will be inherited if the variable `org-use-tag-inheritance'
  2521. says they should be.
  2522. This variable is populated from #+FILETAGS lines.")
  2523. (defcustom org-use-fast-tag-selection 'auto
  2524. "Non-nil means use fast tag selection scheme.
  2525. This is a special interface to select and deselect tags with single keys.
  2526. When nil, fast selection is never used.
  2527. When the symbol `auto', fast selection is used if and only if selection
  2528. characters for tags have been configured, either through the variable
  2529. `org-tag-alist' or through a #+TAGS line in the buffer.
  2530. When t, fast selection is always used and selection keys are assigned
  2531. automatically if necessary."
  2532. :group 'org-tags
  2533. :type '(choice
  2534. (const :tag "Always" t)
  2535. (const :tag "Never" nil)
  2536. (const :tag "When selection characters are configured" 'auto)))
  2537. (defcustom org-fast-tag-selection-single-key nil
  2538. "Non-nil means fast tag selection exits after first change.
  2539. When nil, you have to press RET to exit it.
  2540. During fast tag selection, you can toggle this flag with `C-c'.
  2541. This variable can also have the value `expert'. In this case, the window
  2542. displaying the tags menu is not even shown, until you press C-c again."
  2543. :group 'org-tags
  2544. :type '(choice
  2545. (const :tag "No" nil)
  2546. (const :tag "Yes" t)
  2547. (const :tag "Expert" expert)))
  2548. (defvar org-fast-tag-selection-include-todo nil
  2549. "Non-nil means fast tags selection interface will also offer TODO states.
  2550. This is an undocumented feature, you should not rely on it.")
  2551. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2552. "The column to which tags should be indented in a headline.
  2553. If this number is positive, it specifies the column. If it is negative,
  2554. it means that the tags should be flushright to that column. For example,
  2555. -80 works well for a normal 80 character screen.
  2556. When 0, place tags directly after headline text, with only one space in
  2557. between."
  2558. :group 'org-tags
  2559. :type 'integer)
  2560. (defcustom org-auto-align-tags t
  2561. "Non-nil keeps tags aligned when modifying headlines.
  2562. Some operations (i.e. demoting) change the length of a headline and
  2563. therefore shift the tags around. With this option turned on, after
  2564. each such operation the tags are again aligned to `org-tags-column'."
  2565. :group 'org-tags
  2566. :type 'boolean)
  2567. (defcustom org-use-tag-inheritance t
  2568. "Non-nil means tags in levels apply also for sublevels.
  2569. When nil, only the tags directly given in a specific line apply there.
  2570. This may also be a list of tags that should be inherited, or a regexp that
  2571. matches tags that should be inherited. Additional control is possible
  2572. with the variable `org-tags-exclude-from-inheritance' which gives an
  2573. explicit list of tags to be excluded from inheritance., even if the value of
  2574. `org-use-tag-inheritance' would select it for inheritance.
  2575. If this option is t, a match early-on in a tree can lead to a large
  2576. number of matches in the subtree when constructing the agenda or creating
  2577. a sparse tree. If you only want to see the first match in a tree during
  2578. a search, check out the variable `org-tags-match-list-sublevels'."
  2579. :group 'org-tags
  2580. :type '(choice
  2581. (const :tag "Not" nil)
  2582. (const :tag "Always" t)
  2583. (repeat :tag "Specific tags" (string :tag "Tag"))
  2584. (regexp :tag "Tags matched by regexp")))
  2585. (defcustom org-tags-exclude-from-inheritance nil
  2586. "List of tags that should never be inherited.
  2587. This is a way to exclude a few tags from inheritance. For way to do
  2588. the opposite, to actively allow inheritance for selected tags,
  2589. see the variable `org-use-tag-inheritance'."
  2590. :group 'org-tags
  2591. :type '(repeat (string :tag "Tag")))
  2592. (defun org-tag-inherit-p (tag)
  2593. "Check if TAG is one that should be inherited."
  2594. (cond
  2595. ((member tag org-tags-exclude-from-inheritance) nil)
  2596. ((eq org-use-tag-inheritance t) t)
  2597. ((not org-use-tag-inheritance) nil)
  2598. ((stringp org-use-tag-inheritance)
  2599. (string-match org-use-tag-inheritance tag))
  2600. ((listp org-use-tag-inheritance)
  2601. (member tag org-use-tag-inheritance))
  2602. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2603. (defcustom org-tags-match-list-sublevels t
  2604. "Non-nil means list also sublevels of headlines matching a search.
  2605. This variable applies to tags/property searches, and also to stuck
  2606. projects because this search is based on a tags match as well.
  2607. When set to the symbol `indented', sublevels are indented with
  2608. leading dots.
  2609. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2610. the sublevels of a headline matching a tag search often also match
  2611. the same search. Listing all of them can create very long lists.
  2612. Setting this variable to nil causes subtrees of a match to be skipped.
  2613. This variable is semi-obsolete and probably should always be true. It
  2614. is better to limit inheritance to certain tags using the variables
  2615. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2616. :group 'org-tags
  2617. :type '(choice
  2618. (const :tag "No, don't list them" nil)
  2619. (const :tag "Yes, do list them" t)
  2620. (const :tag "List them, indented with leading dots" indented)))
  2621. (defcustom org-tags-sort-function nil
  2622. "When set, tags are sorted using this function as a comparator."
  2623. :group 'org-tags
  2624. :type '(choice
  2625. (const :tag "No sorting" nil)
  2626. (const :tag "Alphabetical" string<)
  2627. (const :tag "Reverse alphabetical" string>)
  2628. (function :tag "Custom function" nil)))
  2629. (defvar org-tags-history nil
  2630. "History of minibuffer reads for tags.")
  2631. (defvar org-last-tags-completion-table nil
  2632. "The last used completion table for tags.")
  2633. (defvar org-after-tags-change-hook nil
  2634. "Hook that is run after the tags in a line have changed.")
  2635. (defgroup org-properties nil
  2636. "Options concerning properties in Org-mode."
  2637. :tag "Org Properties"
  2638. :group 'org)
  2639. (defcustom org-property-format "%-10s %s"
  2640. "How property key/value pairs should be formatted by `indent-line'.
  2641. When `indent-line' hits a property definition, it will format the line
  2642. according to this format, mainly to make sure that the values are
  2643. lined-up with respect to each other."
  2644. :group 'org-properties
  2645. :type 'string)
  2646. (defcustom org-properties-postprocess-alist nil
  2647. "Alist of properties and functions to adjust inserted values.
  2648. Elements of this alist must be of the form
  2649. ([string] [function])
  2650. where [string] must be a property name and [function] must be a
  2651. lambda expression: this lambda expression must take one argument,
  2652. the value to adjust, and return the new value as a string.
  2653. For example, this element will allow the property \"Remaining\"
  2654. to be updated wrt the relation between the \"Effort\" property
  2655. and the clock summary:
  2656. ((\"Remaining\" (lambda(value)
  2657. (let ((clocksum (org-clock-sum-current-item))
  2658. (effort (org-duration-string-to-minutes
  2659. (org-entry-get (point) \"Effort\"))))
  2660. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2661. :group 'org-properties
  2662. :type 'alist)
  2663. (defcustom org-use-property-inheritance nil
  2664. "Non-nil means properties apply also for sublevels.
  2665. This setting is chiefly used during property searches. Turning it on can
  2666. cause significant overhead when doing a search, which is why it is not
  2667. on by default.
  2668. When nil, only the properties directly given in the current entry count.
  2669. When t, every property is inherited. The value may also be a list of
  2670. properties that should have inheritance, or a regular expression matching
  2671. properties that should be inherited.
  2672. However, note that some special properties use inheritance under special
  2673. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2674. and the properties ending in \"_ALL\" when they are used as descriptor
  2675. for valid values of a property.
  2676. Note for programmers:
  2677. When querying an entry with `org-entry-get', you can control if inheritance
  2678. should be used. By default, `org-entry-get' looks only at the local
  2679. properties. You can request inheritance by setting the inherit argument
  2680. to t (to force inheritance) or to `selective' (to respect the setting
  2681. in this variable)."
  2682. :group 'org-properties
  2683. :type '(choice
  2684. (const :tag "Not" nil)
  2685. (const :tag "Always" t)
  2686. (repeat :tag "Specific properties" (string :tag "Property"))
  2687. (regexp :tag "Properties matched by regexp")))
  2688. (defun org-property-inherit-p (property)
  2689. "Check if PROPERTY is one that should be inherited."
  2690. (cond
  2691. ((eq org-use-property-inheritance t) t)
  2692. ((not org-use-property-inheritance) nil)
  2693. ((stringp org-use-property-inheritance)
  2694. (string-match org-use-property-inheritance property))
  2695. ((listp org-use-property-inheritance)
  2696. (member property org-use-property-inheritance))
  2697. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2698. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2699. "The default column format, if no other format has been defined.
  2700. This variable can be set on the per-file basis by inserting a line
  2701. #+COLUMNS: %25ITEM ....."
  2702. :group 'org-properties
  2703. :type 'string)
  2704. (defcustom org-columns-ellipses ".."
  2705. "The ellipses to be used when a field in column view is truncated.
  2706. When this is the empty string, as many characters as possible are shown,
  2707. but then there will be no visual indication that the field has been truncated.
  2708. When this is a string of length N, the last N characters of a truncated
  2709. field are replaced by this string. If the column is narrower than the
  2710. ellipses string, only part of the ellipses string will be shown."
  2711. :group 'org-properties
  2712. :type 'string)
  2713. (defcustom org-columns-modify-value-for-display-function nil
  2714. "Function that modifies values for display in column view.
  2715. For example, it can be used to cut out a certain part from a time stamp.
  2716. The function must take 2 arguments:
  2717. column-title The title of the column (*not* the property name)
  2718. value The value that should be modified.
  2719. The function should return the value that should be displayed,
  2720. or nil if the normal value should be used."
  2721. :group 'org-properties
  2722. :type 'function)
  2723. (defcustom org-effort-property "Effort"
  2724. "The property that is being used to keep track of effort estimates.
  2725. Effort estimates given in this property need to have the format H:MM."
  2726. :group 'org-properties
  2727. :group 'org-progress
  2728. :type '(string :tag "Property"))
  2729. (defconst org-global-properties-fixed
  2730. '(("VISIBILITY_ALL" . "folded children content all")
  2731. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2732. "List of property/value pairs that can be inherited by any entry.
  2733. These are fixed values, for the preset properties. The user variable
  2734. that can be used to add to this list is `org-global-properties'.
  2735. The entries in this list are cons cells where the car is a property
  2736. name and cdr is a string with the value. If the value represents
  2737. multiple items like an \"_ALL\" property, separate the items by
  2738. spaces.")
  2739. (defcustom org-global-properties nil
  2740. "List of property/value pairs that can be inherited by any entry.
  2741. This list will be combined with the constant `org-global-properties-fixed'.
  2742. The entries in this list are cons cells where the car is a property
  2743. name and cdr is a string with the value.
  2744. You can set buffer-local values for the same purpose in the variable
  2745. `org-file-properties' this by adding lines like
  2746. #+PROPERTY: NAME VALUE"
  2747. :group 'org-properties
  2748. :type '(repeat
  2749. (cons (string :tag "Property")
  2750. (string :tag "Value"))))
  2751. (defvar org-file-properties nil
  2752. "List of property/value pairs that can be inherited by any entry.
  2753. Valid for the current buffer.
  2754. This variable is populated from #+PROPERTY lines.")
  2755. (make-variable-buffer-local 'org-file-properties)
  2756. (defgroup org-agenda nil
  2757. "Options concerning agenda views in Org-mode."
  2758. :tag "Org Agenda"
  2759. :group 'org)
  2760. (defvar org-category nil
  2761. "Variable used by org files to set a category for agenda display.
  2762. Such files should use a file variable to set it, for example
  2763. # -*- mode: org; org-category: \"ELisp\"
  2764. or contain a special line
  2765. #+CATEGORY: ELisp
  2766. If the file does not specify a category, then file's base name
  2767. is used instead.")
  2768. (make-variable-buffer-local 'org-category)
  2769. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2770. (defcustom org-agenda-files nil
  2771. "The files to be used for agenda display.
  2772. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2773. \\[org-remove-file]. You can also use customize to edit the list.
  2774. If an entry is a directory, all files in that directory that are matched by
  2775. `org-agenda-file-regexp' will be part of the file list.
  2776. If the value of the variable is not a list but a single file name, then
  2777. the list of agenda files is actually stored and maintained in that file, one
  2778. agenda file per line. In this file paths can be given relative to
  2779. `org-directory'. Tilde expansion and environment variable substitution
  2780. are also made."
  2781. :group 'org-agenda
  2782. :type '(choice
  2783. (repeat :tag "List of files and directories" file)
  2784. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2785. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2786. "Regular expression to match files for `org-agenda-files'.
  2787. If any element in the list in that variable contains a directory instead
  2788. of a normal file, all files in that directory that are matched by this
  2789. regular expression will be included."
  2790. :group 'org-agenda
  2791. :type 'regexp)
  2792. (defcustom org-agenda-text-search-extra-files nil
  2793. "List of extra files to be searched by text search commands.
  2794. These files will be search in addition to the agenda files by the
  2795. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2796. Note that these files will only be searched for text search commands,
  2797. not for the other agenda views like todo lists, tag searches or the weekly
  2798. agenda. This variable is intended to list notes and possibly archive files
  2799. that should also be searched by these two commands.
  2800. In fact, if the first element in the list is the symbol `agenda-archives',
  2801. than all archive files of all agenda files will be added to the search
  2802. scope."
  2803. :group 'org-agenda
  2804. :type '(set :greedy t
  2805. (const :tag "Agenda Archives" agenda-archives)
  2806. (repeat :inline t (file))))
  2807. (if (fboundp 'defvaralias)
  2808. (defvaralias 'org-agenda-multi-occur-extra-files
  2809. 'org-agenda-text-search-extra-files))
  2810. (defcustom org-agenda-skip-unavailable-files nil
  2811. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2812. A nil value means to remove them, after a query, from the list."
  2813. :group 'org-agenda
  2814. :type 'boolean)
  2815. (defcustom org-calendar-to-agenda-key [?c]
  2816. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2817. The command `org-calendar-goto-agenda' will be bound to this key. The
  2818. default is the character `c' because then `c' can be used to switch back and
  2819. forth between agenda and calendar."
  2820. :group 'org-agenda
  2821. :type 'sexp)
  2822. (defcustom org-calendar-agenda-action-key [?k]
  2823. "The key to be installed in `calendar-mode-map' for agenda-action.
  2824. The command `org-agenda-action' will be bound to this key. The
  2825. default is the character `k' because we use the same key in the agenda."
  2826. :group 'org-agenda
  2827. :type 'sexp)
  2828. (defcustom org-calendar-insert-diary-entry-key [?i]
  2829. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2830. This option is irrelevant until `org-agenda-diary-file' has been configured
  2831. to point to an Org-mode file. When that is the case, the command
  2832. `org-agenda-diary-entry' will be bound to the key given here, by default
  2833. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2834. if you want to continue doing this, you need to change this to a different
  2835. key."
  2836. :group 'org-agenda
  2837. :type 'sexp)
  2838. (defcustom org-agenda-diary-file 'diary-file
  2839. "File to which to add new entries with the `i' key in agenda and calendar.
  2840. When this is the symbol `diary-file', the functionality in the Emacs
  2841. calendar will be used to add entries to the `diary-file'. But when this
  2842. points to a file, `org-agenda-diary-entry' will be used instead."
  2843. :group 'org-agenda
  2844. :type '(choice
  2845. (const :tag "The standard Emacs diary file" diary-file)
  2846. (file :tag "Special Org file diary entries")))
  2847. (eval-after-load "calendar"
  2848. '(progn
  2849. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2850. 'org-calendar-goto-agenda)
  2851. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2852. 'org-agenda-action)
  2853. (add-hook 'calendar-mode-hook
  2854. (lambda ()
  2855. (unless (eq org-agenda-diary-file 'diary-file)
  2856. (define-key calendar-mode-map
  2857. org-calendar-insert-diary-entry-key
  2858. 'org-agenda-diary-entry))))))
  2859. (defgroup org-latex nil
  2860. "Options for embedding LaTeX code into Org-mode."
  2861. :tag "Org LaTeX"
  2862. :group 'org)
  2863. (defcustom org-format-latex-options
  2864. '(:foreground default :background default :scale 1.0
  2865. :html-foreground "Black" :html-background "Transparent"
  2866. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2867. "Options for creating images from LaTeX fragments.
  2868. This is a property list with the following properties:
  2869. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2870. `default' means use the foreground of the default face.
  2871. :background the background color, or \"Transparent\".
  2872. `default' means use the background of the default face.
  2873. :scale a scaling factor for the size of the images, to get more pixels
  2874. :html-foreground, :html-background, :html-scale
  2875. the same numbers for HTML export.
  2876. :matchers a list indicating which matchers should be used to
  2877. find LaTeX fragments. Valid members of this list are:
  2878. \"begin\" find environments
  2879. \"$1\" find single characters surrounded by $.$
  2880. \"$\" find math expressions surrounded by $...$
  2881. \"$$\" find math expressions surrounded by $$....$$
  2882. \"\\(\" find math expressions surrounded by \\(...\\)
  2883. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2884. :group 'org-latex
  2885. :type 'plist)
  2886. (defcustom org-format-latex-signal-error t
  2887. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2888. When nil, just push out a message."
  2889. :group 'org-latex
  2890. :type 'boolean)
  2891. (defcustom org-latex-to-mathml-jar-file nil
  2892. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2893. Use this to specify additional executable file say a jar file.
  2894. When using MathToWeb as the converter, specify the full-path to
  2895. your mathtoweb.jar file."
  2896. :group 'org-latex
  2897. :type '(choice
  2898. (const :tag "None" nil)
  2899. (file :tag "JAR file" :must-match t)))
  2900. (defcustom org-latex-to-mathml-convert-command nil
  2901. "Command to convert LaTeX fragments to MathML.
  2902. Replace format-specifiers in the command as noted below and use
  2903. `shell-command' to convert LaTeX to MathML.
  2904. %j: Executable file in fully expanded form as specified by
  2905. `org-latex-to-mathml-jar-file'.
  2906. %I: Input LaTeX file in fully expanded form
  2907. %o: Output MathML file
  2908. This command is used by `org-create-math-formula'.
  2909. When using MathToWeb as the converter, set this to
  2910. \"java -jar %j -unicode -force -df %o %I\"."
  2911. :group 'org-latex
  2912. :type '(choice
  2913. (const :tag "None" nil)
  2914. (string :tag "\nShell command")))
  2915. (defun org-format-latex-mathml-available-p ()
  2916. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2917. (save-match-data
  2918. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2919. org-latex-to-mathml-convert-command)
  2920. (let ((executable (car (split-string
  2921. org-latex-to-mathml-convert-command))))
  2922. (when (executable-find executable)
  2923. (if (string-match
  2924. "%j" org-latex-to-mathml-convert-command)
  2925. (file-readable-p org-latex-to-mathml-jar-file)
  2926. t))))))
  2927. (defcustom org-format-latex-header "\\documentclass{article}
  2928. \\usepackage[usenames]{color}
  2929. \\usepackage{amsmath}
  2930. \\usepackage[mathscr]{eucal}
  2931. \\pagestyle{empty} % do not remove
  2932. \[PACKAGES]
  2933. \[DEFAULT-PACKAGES]
  2934. % The settings below are copied from fullpage.sty
  2935. \\setlength{\\textwidth}{\\paperwidth}
  2936. \\addtolength{\\textwidth}{-3cm}
  2937. \\setlength{\\oddsidemargin}{1.5cm}
  2938. \\addtolength{\\oddsidemargin}{-2.54cm}
  2939. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2940. \\setlength{\\textheight}{\\paperheight}
  2941. \\addtolength{\\textheight}{-\\headheight}
  2942. \\addtolength{\\textheight}{-\\headsep}
  2943. \\addtolength{\\textheight}{-\\footskip}
  2944. \\addtolength{\\textheight}{-3cm}
  2945. \\setlength{\\topmargin}{1.5cm}
  2946. \\addtolength{\\topmargin}{-2.54cm}"
  2947. "The document header used for processing LaTeX fragments.
  2948. It is imperative that this header make sure that no page number
  2949. appears on the page. The package defined in the variables
  2950. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2951. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2952. will be appended."
  2953. :group 'org-latex
  2954. :type 'string)
  2955. (defvar org-format-latex-header-extra nil)
  2956. (defun org-set-packages-alist (var val)
  2957. "Set the packages alist and make sure it has 3 elements per entry."
  2958. (set var (mapcar (lambda (x)
  2959. (if (and (consp x) (= (length x) 2))
  2960. (list (car x) (nth 1 x) t)
  2961. x))
  2962. val)))
  2963. (defun org-get-packages-alist (var)
  2964. "Get the packages alist and make sure it has 3 elements per entry."
  2965. (mapcar (lambda (x)
  2966. (if (and (consp x) (= (length x) 2))
  2967. (list (car x) (nth 1 x) t)
  2968. x))
  2969. (default-value var)))
  2970. ;; The following variables are defined here because is it also used
  2971. ;; when formatting latex fragments. Originally it was part of the
  2972. ;; LaTeX exporter, which is why the name includes "export".
  2973. (defcustom org-export-latex-default-packages-alist
  2974. '(("AUTO" "inputenc" t)
  2975. ("T1" "fontenc" t)
  2976. ("" "fixltx2e" nil)
  2977. ("" "graphicx" t)
  2978. ("" "longtable" nil)
  2979. ("" "float" nil)
  2980. ("" "wrapfig" nil)
  2981. ("" "soul" t)
  2982. ("" "textcomp" t)
  2983. ("" "marvosym" t)
  2984. ("" "wasysym" t)
  2985. ("" "latexsym" t)
  2986. ("" "amssymb" t)
  2987. ("" "hyperref" nil)
  2988. "\\tolerance=1000"
  2989. )
  2990. "Alist of default packages to be inserted in the header.
  2991. Change this only if one of the packages here causes an incompatibility
  2992. with another package you are using.
  2993. The packages in this list are needed by one part or another of Org-mode
  2994. to function properly.
  2995. - inputenc, fontenc: for basic font and character selection
  2996. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2997. for interpreting the entities in `org-entities'. You can skip some of these
  2998. packages if you don't use any of the symbols in it.
  2999. - graphicx: for including images
  3000. - float, wrapfig: for figure placement
  3001. - longtable: for long tables
  3002. - hyperref: for cross references
  3003. Therefore you should not modify this variable unless you know what you
  3004. are doing. The one reason to change it anyway is that you might be loading
  3005. some other package that conflicts with one of the default packages.
  3006. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3007. If SNIPPET-FLAG is t, the package also needs to be included when
  3008. compiling LaTeX snippets into images for inclusion into HTML."
  3009. :group 'org-export-latex
  3010. :set 'org-set-packages-alist
  3011. :get 'org-get-packages-alist
  3012. :type '(repeat
  3013. (choice
  3014. (list :tag "options/package pair"
  3015. (string :tag "options")
  3016. (string :tag "package")
  3017. (boolean :tag "Snippet"))
  3018. (string :tag "A line of LaTeX"))))
  3019. (defcustom org-export-latex-packages-alist nil
  3020. "Alist of packages to be inserted in every LaTeX header.
  3021. These will be inserted after `org-export-latex-default-packages-alist'.
  3022. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3023. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3024. turning LaTeX snippets into images for inclusion into HTML.
  3025. Make sure that you only list packages here which:
  3026. - you want in every file
  3027. - do not conflict with the default packages in
  3028. `org-export-latex-default-packages-alist'
  3029. - do not conflict with the setup in `org-format-latex-header'."
  3030. :group 'org-export-latex
  3031. :set 'org-set-packages-alist
  3032. :get 'org-get-packages-alist
  3033. :type '(repeat
  3034. (choice
  3035. (list :tag "options/package pair"
  3036. (string :tag "options")
  3037. (string :tag "package")
  3038. (boolean :tag "Snippet"))
  3039. (string :tag "A line of LaTeX"))))
  3040. (defgroup org-appearance nil
  3041. "Settings for Org-mode appearance."
  3042. :tag "Org Appearance"
  3043. :group 'org)
  3044. (defcustom org-level-color-stars-only nil
  3045. "Non-nil means fontify only the stars in each headline.
  3046. When nil, the entire headline is fontified.
  3047. Changing it requires restart of `font-lock-mode' to become effective
  3048. also in regions already fontified."
  3049. :group 'org-appearance
  3050. :type 'boolean)
  3051. (defcustom org-hide-leading-stars nil
  3052. "Non-nil means hide the first N-1 stars in a headline.
  3053. This works by using the face `org-hide' for these stars. This
  3054. face is white for a light background, and black for a dark
  3055. background. You may have to customize the face `org-hide' to
  3056. make this work.
  3057. Changing it requires restart of `font-lock-mode' to become effective
  3058. also in regions already fontified.
  3059. You may also set this on a per-file basis by adding one of the following
  3060. lines to the buffer:
  3061. #+STARTUP: hidestars
  3062. #+STARTUP: showstars"
  3063. :group 'org-appearance
  3064. :type 'boolean)
  3065. (defcustom org-hidden-keywords nil
  3066. "List of symbols corresponding to keywords to be hidden the org buffer.
  3067. For example, a value '(title) for this list will make the document's title
  3068. appear in the buffer without the initial #+TITLE: keyword."
  3069. :group 'org-appearance
  3070. :type '(set (const :tag "#+AUTHOR" author)
  3071. (const :tag "#+DATE" date)
  3072. (const :tag "#+EMAIL" email)
  3073. (const :tag "#+TITLE" title)))
  3074. (defcustom org-fontify-done-headline nil
  3075. "Non-nil means change the face of a headline if it is marked DONE.
  3076. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3077. When this is non-nil, the headline after the keyword is set to the
  3078. `org-headline-done' as an additional indication."
  3079. :group 'org-appearance
  3080. :type 'boolean)
  3081. (defcustom org-fontify-emphasized-text t
  3082. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3083. Changing this variable requires a restart of Emacs to take effect."
  3084. :group 'org-appearance
  3085. :type 'boolean)
  3086. (defcustom org-fontify-whole-heading-line nil
  3087. "Non-nil means fontify the whole line for headings.
  3088. This is useful when setting a background color for the
  3089. org-level-* faces."
  3090. :group 'org-appearance
  3091. :type 'boolean)
  3092. (defcustom org-highlight-latex-fragments-and-specials nil
  3093. "Non-nil means fontify what is treated specially by the exporters."
  3094. :group 'org-appearance
  3095. :type 'boolean)
  3096. (defcustom org-hide-emphasis-markers nil
  3097. "Non-nil mean font-lock should hide the emphasis marker characters."
  3098. :group 'org-appearance
  3099. :type 'boolean)
  3100. (defcustom org-pretty-entities nil
  3101. "Non-nil means show entities as UTF8 characters.
  3102. When nil, the \\name form remains in the buffer."
  3103. :group 'org-appearance
  3104. :type 'boolean)
  3105. (defcustom org-pretty-entities-include-sub-superscripts t
  3106. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3107. :group 'org-appearance
  3108. :type 'boolean)
  3109. (defvar org-emph-re nil
  3110. "Regular expression for matching emphasis.
  3111. After a match, the match groups contain these elements:
  3112. 0 The match of the full regular expression, including the characters
  3113. before and after the proper match
  3114. 1 The character before the proper match, or empty at beginning of line
  3115. 2 The proper match, including the leading and trailing markers
  3116. 3 The leading marker like * or /, indicating the type of highlighting
  3117. 4 The text between the emphasis markers, not including the markers
  3118. 5 The character after the match, empty at the end of a line")
  3119. (defvar org-verbatim-re nil
  3120. "Regular expression for matching verbatim text.")
  3121. (defvar org-emphasis-regexp-components) ; defined just below
  3122. (defvar org-emphasis-alist) ; defined just below
  3123. (defun org-set-emph-re (var val)
  3124. "Set variable and compute the emphasis regular expression."
  3125. (set var val)
  3126. (when (and (boundp 'org-emphasis-alist)
  3127. (boundp 'org-emphasis-regexp-components)
  3128. org-emphasis-alist org-emphasis-regexp-components)
  3129. (let* ((e org-emphasis-regexp-components)
  3130. (pre (car e))
  3131. (post (nth 1 e))
  3132. (border (nth 2 e))
  3133. (body (nth 3 e))
  3134. (nl (nth 4 e))
  3135. (body1 (concat body "*?"))
  3136. (markers (mapconcat 'car org-emphasis-alist ""))
  3137. (vmarkers (mapconcat
  3138. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3139. org-emphasis-alist "")))
  3140. ;; make sure special characters appear at the right position in the class
  3141. (if (string-match "\\^" markers)
  3142. (setq markers (concat (replace-match "" t t markers) "^")))
  3143. (if (string-match "-" markers)
  3144. (setq markers (concat (replace-match "" t t markers) "-")))
  3145. (if (string-match "\\^" vmarkers)
  3146. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3147. (if (string-match "-" vmarkers)
  3148. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3149. (if (> nl 0)
  3150. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3151. (int-to-string nl) "\\}")))
  3152. ;; Make the regexp
  3153. (setq org-emph-re
  3154. (concat "\\([" pre "]\\|^\\)"
  3155. "\\("
  3156. "\\([" markers "]\\)"
  3157. "\\("
  3158. "[^" border "]\\|"
  3159. "[^" border "]"
  3160. body1
  3161. "[^" border "]"
  3162. "\\)"
  3163. "\\3\\)"
  3164. "\\([" post "]\\|$\\)"))
  3165. (setq org-verbatim-re
  3166. (concat "\\([" pre "]\\|^\\)"
  3167. "\\("
  3168. "\\([" vmarkers "]\\)"
  3169. "\\("
  3170. "[^" border "]\\|"
  3171. "[^" border "]"
  3172. body1
  3173. "[^" border "]"
  3174. "\\)"
  3175. "\\3\\)"
  3176. "\\([" post "]\\|$\\)")))))
  3177. (defcustom org-emphasis-regexp-components
  3178. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3179. "Components used to build the regular expression for emphasis.
  3180. This is a list with five entries. Terminology: In an emphasis string
  3181. like \" *strong word* \", we call the initial space PREMATCH, the final
  3182. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3183. and \"trong wor\" is the body. The different components in this variable
  3184. specify what is allowed/forbidden in each part:
  3185. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3186. post Chars allowed as postmatch. End of line will be allowed too.
  3187. border The chars *forbidden* as border characters.
  3188. body-regexp A regexp like \".\" to match a body character. Don't use
  3189. non-shy groups here, and don't allow newline here.
  3190. newline The maximum number of newlines allowed in an emphasis exp.
  3191. Use customize to modify this, or restart Emacs after changing it."
  3192. :group 'org-appearance
  3193. :set 'org-set-emph-re
  3194. :type '(list
  3195. (sexp :tag "Allowed chars in pre ")
  3196. (sexp :tag "Allowed chars in post ")
  3197. (sexp :tag "Forbidden chars in border ")
  3198. (sexp :tag "Regexp for body ")
  3199. (integer :tag "number of newlines allowed")
  3200. (option (boolean :tag "Please ignore this button"))))
  3201. (defcustom org-emphasis-alist
  3202. `(("*" bold "<b>" "</b>")
  3203. ("/" italic "<i>" "</i>")
  3204. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3205. ("=" org-code "<code>" "</code>" verbatim)
  3206. ("~" org-verbatim "<code>" "</code>" verbatim)
  3207. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3208. "<del>" "</del>")
  3209. )
  3210. "Special syntax for emphasized text.
  3211. Text starting and ending with a special character will be emphasized, for
  3212. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3213. characters, the face to be used by font-lock for highlighting in Org-mode
  3214. Emacs buffers, and the HTML tags to be used for this.
  3215. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3216. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3217. Use customize to modify this, or restart Emacs after changing it."
  3218. :group 'org-appearance
  3219. :set 'org-set-emph-re
  3220. :type '(repeat
  3221. (list
  3222. (string :tag "Marker character")
  3223. (choice
  3224. (face :tag "Font-lock-face")
  3225. (plist :tag "Face property list"))
  3226. (string :tag "HTML start tag")
  3227. (string :tag "HTML end tag")
  3228. (option (const verbatim)))))
  3229. (defvar org-protecting-blocks
  3230. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3231. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3232. This is needed for font-lock setup.")
  3233. ;;; Miscellaneous options
  3234. (defgroup org-completion nil
  3235. "Completion in Org-mode."
  3236. :tag "Org Completion"
  3237. :group 'org)
  3238. (defcustom org-completion-use-ido nil
  3239. "Non-nil means use ido completion wherever possible.
  3240. Note that `ido-mode' must be active for this variable to be relevant.
  3241. If you decide to turn this variable on, you might well want to turn off
  3242. `org-outline-path-complete-in-steps'.
  3243. See also `org-completion-use-iswitchb'."
  3244. :group 'org-completion
  3245. :type 'boolean)
  3246. (defcustom org-completion-use-iswitchb nil
  3247. "Non-nil means use iswitchb completion wherever possible.
  3248. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3249. If you decide to turn this variable on, you might well want to turn off
  3250. `org-outline-path-complete-in-steps'.
  3251. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3252. :group 'org-completion
  3253. :type 'boolean)
  3254. (defcustom org-completion-fallback-command 'hippie-expand
  3255. "The expansion command called by \\[pcomplete] in normal context.
  3256. Normal means, no org-mode-specific context."
  3257. :group 'org-completion
  3258. :type 'function)
  3259. ;;; Functions and variables from their packages
  3260. ;; Declared here to avoid compiler warnings
  3261. ;; XEmacs only
  3262. (defvar outline-mode-menu-heading)
  3263. (defvar outline-mode-menu-show)
  3264. (defvar outline-mode-menu-hide)
  3265. (defvar zmacs-regions) ; XEmacs regions
  3266. ;; Emacs only
  3267. (defvar mark-active)
  3268. ;; Various packages
  3269. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3270. (declare-function calendar-forward-day "cal-move" (arg))
  3271. (declare-function calendar-goto-date "cal-move" (date))
  3272. (declare-function calendar-goto-today "cal-move" ())
  3273. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3274. (defvar calc-embedded-close-formula)
  3275. (defvar calc-embedded-open-formula)
  3276. (declare-function cdlatex-tab "ext:cdlatex" ())
  3277. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3278. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3279. (defvar font-lock-unfontify-region-function)
  3280. (declare-function iswitchb-read-buffer "iswitchb"
  3281. (prompt &optional default require-match start matches-set))
  3282. (defvar iswitchb-temp-buflist)
  3283. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3284. (defvar org-agenda-tags-todo-honor-ignore-options)
  3285. (declare-function org-agenda-skip "org-agenda" ())
  3286. (declare-function
  3287. org-agenda-format-item "org-agenda"
  3288. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3289. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3290. (declare-function org-agenda-change-all-lines "org-agenda"
  3291. (newhead hdmarker &optional fixface just-this))
  3292. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3293. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3294. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3295. (beg end))
  3296. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3297. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3298. "org-agenda" (&optional end))
  3299. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3300. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3301. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3302. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3303. (declare-function org-indent-mode "org-indent" (&optional arg))
  3304. (declare-function parse-time-string "parse-time" (string))
  3305. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3306. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3307. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3308. (defvar remember-data-file)
  3309. (defvar texmathp-why)
  3310. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3311. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3312. (defvar w3m-current-url)
  3313. (defvar w3m-current-title)
  3314. (defvar org-latex-regexps)
  3315. ;;; Autoload and prepare some org modules
  3316. ;; Some table stuff that needs to be defined here, because it is used
  3317. ;; by the functions setting up org-mode or checking for table context.
  3318. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3319. "Detect an org-type or table-type table.")
  3320. (defconst org-table-line-regexp "^[ \t]*|"
  3321. "Detect an org-type table line.")
  3322. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3323. "Detect an org-type table line.")
  3324. (defconst org-table-hline-regexp "^[ \t]*|-"
  3325. "Detect an org-type table hline.")
  3326. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3327. "Detect a table-type table hline.")
  3328. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3329. "Detect the first line outside a table when searching from within it.
  3330. This works for both table types.")
  3331. ;; Autoload the functions in org-table.el that are needed by functions here.
  3332. (eval-and-compile
  3333. (org-autoload "org-table"
  3334. '(org-table-align org-table-begin org-table-blank-field
  3335. org-table-convert org-table-convert-region org-table-copy-down
  3336. org-table-copy-region org-table-create
  3337. org-table-create-or-convert-from-region
  3338. org-table-create-with-table.el org-table-current-dline
  3339. org-table-cut-region org-table-delete-column org-table-edit-field
  3340. org-table-edit-formulas org-table-end org-table-eval-formula
  3341. org-table-export org-table-field-info
  3342. org-table-get-stored-formulas org-table-goto-column
  3343. org-table-hline-and-move org-table-import org-table-insert-column
  3344. org-table-insert-hline org-table-insert-row org-table-iterate
  3345. org-table-justify-field-maybe org-table-kill-row
  3346. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3347. org-table-move-column org-table-move-column-left
  3348. org-table-move-column-right org-table-move-row
  3349. org-table-move-row-down org-table-move-row-up
  3350. org-table-next-field org-table-next-row org-table-paste-rectangle
  3351. org-table-previous-field org-table-recalculate
  3352. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3353. org-table-toggle-coordinate-overlays
  3354. org-table-toggle-formula-debugger org-table-wrap-region
  3355. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3356. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3357. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3358. (defun org-at-table-p (&optional table-type)
  3359. "Return t if the cursor is inside an org-type table.
  3360. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3361. (if org-enable-table-editor
  3362. (save-excursion
  3363. (beginning-of-line 1)
  3364. (looking-at (if table-type org-table-any-line-regexp
  3365. org-table-line-regexp)))
  3366. nil))
  3367. (defsubst org-table-p () (org-at-table-p))
  3368. (defun org-at-table.el-p ()
  3369. "Return t if and only if we are at a table.el table."
  3370. (and (org-at-table-p 'any)
  3371. (save-excursion
  3372. (goto-char (org-table-begin 'any))
  3373. (looking-at org-table1-hline-regexp))))
  3374. (defun org-table-recognize-table.el ()
  3375. "If there is a table.el table nearby, recognize it and move into it."
  3376. (if org-table-tab-recognizes-table.el
  3377. (if (org-at-table.el-p)
  3378. (progn
  3379. (beginning-of-line 1)
  3380. (if (looking-at org-table-dataline-regexp)
  3381. nil
  3382. (if (looking-at org-table1-hline-regexp)
  3383. (progn
  3384. (beginning-of-line 2)
  3385. (if (looking-at org-table-any-border-regexp)
  3386. (beginning-of-line -1)))))
  3387. (if (re-search-forward "|" (org-table-end t) t)
  3388. (progn
  3389. (require 'table)
  3390. (if (table--at-cell-p (point))
  3391. t
  3392. (message "recognizing table.el table...")
  3393. (table-recognize-table)
  3394. (message "recognizing table.el table...done")))
  3395. (error "This should not happen"))
  3396. t)
  3397. nil)
  3398. nil))
  3399. (defun org-at-table-hline-p ()
  3400. "Return t if the cursor is inside a hline in a table."
  3401. (if org-enable-table-editor
  3402. (save-excursion
  3403. (beginning-of-line 1)
  3404. (looking-at org-table-hline-regexp))
  3405. nil))
  3406. (defvar org-table-clean-did-remove-column nil)
  3407. (defun org-table-map-tables (function &optional quietly)
  3408. "Apply FUNCTION to the start of all tables in the buffer."
  3409. (save-excursion
  3410. (save-restriction
  3411. (widen)
  3412. (goto-char (point-min))
  3413. (while (re-search-forward org-table-any-line-regexp nil t)
  3414. (unless quietly
  3415. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3416. (beginning-of-line 1)
  3417. (when (looking-at org-table-line-regexp)
  3418. (save-excursion (funcall function))
  3419. (or (looking-at org-table-line-regexp)
  3420. (forward-char 1)))
  3421. (re-search-forward org-table-any-border-regexp nil 1))))
  3422. (unless quietly (message "Mapping tables: done")))
  3423. ;; Declare and autoload functions from org-exp.el & Co
  3424. (declare-function org-default-export-plist "org-exp")
  3425. (declare-function org-infile-export-plist "org-exp")
  3426. (declare-function org-get-current-options "org-exp")
  3427. (eval-and-compile
  3428. (org-autoload "org-exp"
  3429. '(org-export org-export-visible
  3430. org-insert-export-options-template
  3431. org-table-clean-before-export))
  3432. (org-autoload "org-ascii"
  3433. '(org-export-as-ascii org-export-ascii-preprocess
  3434. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3435. org-export-region-as-ascii))
  3436. (org-autoload "org-latex"
  3437. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3438. org-replace-region-by-latex org-export-region-as-latex
  3439. org-export-as-latex org-export-as-pdf
  3440. org-export-as-pdf-and-open))
  3441. (org-autoload "org-html"
  3442. '(org-export-as-html-and-open
  3443. org-export-as-html-batch org-export-as-html-to-buffer
  3444. org-replace-region-by-html org-export-region-as-html
  3445. org-export-as-html))
  3446. (org-autoload "org-docbook"
  3447. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3448. org-replace-region-by-docbook org-export-region-as-docbook
  3449. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3450. org-export-as-docbook))
  3451. (org-autoload "org-icalendar"
  3452. '(org-export-icalendar-this-file
  3453. org-export-icalendar-all-agenda-files
  3454. org-export-icalendar-combine-agenda-files))
  3455. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3456. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3457. ;; Declare and autoload functions from org-agenda.el
  3458. (eval-and-compile
  3459. (org-autoload "org-agenda"
  3460. '(org-agenda org-agenda-list org-search-view
  3461. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3462. org-diary org-agenda-to-appt
  3463. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3464. ;; Autoload org-remember
  3465. (eval-and-compile
  3466. (org-autoload "org-remember"
  3467. '(org-remember-insinuate org-remember-annotation
  3468. org-remember-apply-template org-remember org-remember-handler)))
  3469. (eval-and-compile
  3470. (org-autoload "org-capture"
  3471. '(org-capture org-capture-insert-template-here
  3472. org-capture-import-remember-templates)))
  3473. ;; Autoload org-clock.el
  3474. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3475. (beg end))
  3476. (declare-function org-clock-update-mode-line "org-clock" ())
  3477. (declare-function org-resolve-clocks "org-clock"
  3478. (&optional also-non-dangling-p prompt last-valid))
  3479. (defvar org-clock-start-time)
  3480. (defvar org-clock-marker (make-marker)
  3481. "Marker recording the last clock-in.")
  3482. (defvar org-clock-hd-marker (make-marker)
  3483. "Marker recording the last clock-in, but the headline position.")
  3484. (defvar org-clock-heading ""
  3485. "The heading of the current clock entry.")
  3486. (defun org-clock-is-active ()
  3487. "Return non-nil if clock is currently running.
  3488. The return value is actually the clock marker."
  3489. (marker-buffer org-clock-marker))
  3490. (eval-and-compile
  3491. (org-autoload
  3492. "org-clock"
  3493. '(org-clock-in org-clock-out org-clock-cancel
  3494. org-clock-goto org-clock-sum org-clock-display
  3495. org-clock-remove-overlays org-clock-report
  3496. org-clocktable-shift org-dblock-write:clocktable
  3497. org-get-clocktable org-resolve-clocks)))
  3498. (defun org-clock-update-time-maybe ()
  3499. "If this is a CLOCK line, update it and return t.
  3500. Otherwise, return nil."
  3501. (interactive)
  3502. (save-excursion
  3503. (beginning-of-line 1)
  3504. (skip-chars-forward " \t")
  3505. (when (looking-at org-clock-string)
  3506. (let ((re (concat "[ \t]*" org-clock-string
  3507. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3508. "\\([ \t]*=>.*\\)?\\)?"))
  3509. ts te h m s neg)
  3510. (cond
  3511. ((not (looking-at re))
  3512. nil)
  3513. ((not (match-end 2))
  3514. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3515. (> org-clock-marker (point))
  3516. (<= org-clock-marker (point-at-eol)))
  3517. ;; The clock is running here
  3518. (setq org-clock-start-time
  3519. (apply 'encode-time
  3520. (org-parse-time-string (match-string 1))))
  3521. (org-clock-update-mode-line)))
  3522. (t
  3523. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3524. (end-of-line 1)
  3525. (setq ts (match-string 1)
  3526. te (match-string 3))
  3527. (setq s (- (org-float-time
  3528. (apply 'encode-time (org-parse-time-string te)))
  3529. (org-float-time
  3530. (apply 'encode-time (org-parse-time-string ts))))
  3531. neg (< s 0)
  3532. s (abs s)
  3533. h (floor (/ s 3600))
  3534. s (- s (* 3600 h))
  3535. m (floor (/ s 60))
  3536. s (- s (* 60 s)))
  3537. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3538. t))))))
  3539. (defun org-check-running-clock ()
  3540. "Check if the current buffer contains the running clock.
  3541. If yes, offer to stop it and to save the buffer with the changes."
  3542. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3543. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3544. (buffer-name))))
  3545. (org-clock-out)
  3546. (when (y-or-n-p "Save changed buffer?")
  3547. (save-buffer))))
  3548. (defun org-clocktable-try-shift (dir n)
  3549. "Check if this line starts a clock table, if yes, shift the time block."
  3550. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3551. (org-clocktable-shift dir n)))
  3552. ;; Autoload org-timer.el
  3553. (eval-and-compile
  3554. (org-autoload
  3555. "org-timer"
  3556. '(org-timer-start org-timer org-timer-item
  3557. org-timer-change-times-in-region
  3558. org-timer-set-timer
  3559. org-timer-reset-timers
  3560. org-timer-show-remaining-time)))
  3561. ;; Autoload org-feed.el
  3562. (eval-and-compile
  3563. (org-autoload
  3564. "org-feed"
  3565. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3566. ;; Autoload org-indent.el
  3567. ;; Define the variable already here, to make sure we have it.
  3568. (defvar org-indent-mode nil
  3569. "Non-nil if Org-Indent mode is enabled.
  3570. Use the command `org-indent-mode' to change this variable.")
  3571. (eval-and-compile
  3572. (org-autoload
  3573. "org-indent"
  3574. '(org-indent-mode)))
  3575. ;; Autoload org-mobile.el
  3576. (eval-and-compile
  3577. (org-autoload
  3578. "org-mobile"
  3579. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3580. ;; Autoload archiving code
  3581. ;; The stuff that is needed for cycling and tags has to be defined here.
  3582. (defgroup org-archive nil
  3583. "Options concerning archiving in Org-mode."
  3584. :tag "Org Archive"
  3585. :group 'org-structure)
  3586. (defcustom org-archive-location "%s_archive::"
  3587. "The location where subtrees should be archived.
  3588. The value of this variable is a string, consisting of two parts,
  3589. separated by a double-colon. The first part is a filename and
  3590. the second part is a headline.
  3591. When the filename is omitted, archiving happens in the same file.
  3592. %s in the filename will be replaced by the current file
  3593. name (without the directory part). Archiving to a different file
  3594. is useful to keep archived entries from contributing to the
  3595. Org-mode Agenda.
  3596. The archived entries will be filed as subtrees of the specified
  3597. headline. When the headline is omitted, the subtrees are simply
  3598. filed away at the end of the file, as top-level entries. Also in
  3599. the heading you can use %s to represent the file name, this can be
  3600. useful when using the same archive for a number of different files.
  3601. Here are a few examples:
  3602. \"%s_archive::\"
  3603. If the current file is Projects.org, archive in file
  3604. Projects.org_archive, as top-level trees. This is the default.
  3605. \"::* Archived Tasks\"
  3606. Archive in the current file, under the top-level headline
  3607. \"* Archived Tasks\".
  3608. \"~/org/archive.org::\"
  3609. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3610. \"~/org/archive.org::* From %s\"
  3611. Archive in file ~/org/archive.org (absolute path), under headlines
  3612. \"From FILENAME\" where file name is the current file name.
  3613. \"basement::** Finished Tasks\"
  3614. Archive in file ./basement (relative path), as level 3 trees
  3615. below the level 2 heading \"** Finished Tasks\".
  3616. You may set this option on a per-file basis by adding to the buffer a
  3617. line like
  3618. #+ARCHIVE: basement::** Finished Tasks
  3619. You may also define it locally for a subtree by setting an ARCHIVE property
  3620. in the entry. If such a property is found in an entry, or anywhere up
  3621. the hierarchy, it will be used."
  3622. :group 'org-archive
  3623. :type 'string)
  3624. (defcustom org-archive-tag "ARCHIVE"
  3625. "The tag that marks a subtree as archived.
  3626. An archived subtree does not open during visibility cycling, and does
  3627. not contribute to the agenda listings.
  3628. After changing this, font-lock must be restarted in the relevant buffers to
  3629. get the proper fontification."
  3630. :group 'org-archive
  3631. :group 'org-keywords
  3632. :type 'string)
  3633. (defcustom org-agenda-skip-archived-trees t
  3634. "Non-nil means the agenda will skip any items located in archived trees.
  3635. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3636. variable is no longer recommended, you should leave it at the value t.
  3637. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3638. :group 'org-archive
  3639. :group 'org-agenda-skip
  3640. :type 'boolean)
  3641. (defcustom org-columns-skip-archived-trees t
  3642. "Non-nil means ignore archived trees when creating column view."
  3643. :group 'org-archive
  3644. :group 'org-properties
  3645. :type 'boolean)
  3646. (defcustom org-cycle-open-archived-trees nil
  3647. "Non-nil means `org-cycle' will open archived trees.
  3648. An archived tree is a tree marked with the tag ARCHIVE.
  3649. When nil, archived trees will stay folded. You can still open them with
  3650. normal outline commands like `show-all', but not with the cycling commands."
  3651. :group 'org-archive
  3652. :group 'org-cycle
  3653. :type 'boolean)
  3654. (defcustom org-sparse-tree-open-archived-trees nil
  3655. "Non-nil means sparse tree construction shows matches in archived trees.
  3656. When nil, matches in these trees are highlighted, but the trees are kept in
  3657. collapsed state."
  3658. :group 'org-archive
  3659. :group 'org-sparse-trees
  3660. :type 'boolean)
  3661. (defun org-cycle-hide-archived-subtrees (state)
  3662. "Re-hide all archived subtrees after a visibility state change."
  3663. (when (and (not org-cycle-open-archived-trees)
  3664. (not (memq state '(overview folded))))
  3665. (save-excursion
  3666. (let* ((globalp (memq state '(contents all)))
  3667. (beg (if globalp (point-min) (point)))
  3668. (end (if globalp (point-max) (org-end-of-subtree t))))
  3669. (org-hide-archived-subtrees beg end)
  3670. (goto-char beg)
  3671. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3672. (message "%s" (substitute-command-keys
  3673. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3674. (defun org-force-cycle-archived ()
  3675. "Cycle subtree even if it is archived."
  3676. (interactive)
  3677. (setq this-command 'org-cycle)
  3678. (let ((org-cycle-open-archived-trees t))
  3679. (call-interactively 'org-cycle)))
  3680. (defun org-hide-archived-subtrees (beg end)
  3681. "Re-hide all archived subtrees after a visibility state change."
  3682. (save-excursion
  3683. (let* ((re (concat ":" org-archive-tag ":")))
  3684. (goto-char beg)
  3685. (while (re-search-forward re end t)
  3686. (when (org-at-heading-p)
  3687. (org-flag-subtree t)
  3688. (org-end-of-subtree t))))))
  3689. (defun org-flag-subtree (flag)
  3690. (save-excursion
  3691. (org-back-to-heading t)
  3692. (outline-end-of-heading)
  3693. (outline-flag-region (point)
  3694. (progn (org-end-of-subtree t) (point))
  3695. flag)))
  3696. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3697. (eval-and-compile
  3698. (org-autoload "org-archive"
  3699. '(org-add-archive-files org-archive-subtree
  3700. org-archive-to-archive-sibling org-toggle-archive-tag
  3701. org-archive-subtree-default
  3702. org-archive-subtree-default-with-confirmation)))
  3703. ;; Autoload Column View Code
  3704. (declare-function org-columns-number-to-string "org-colview")
  3705. (declare-function org-columns-get-format-and-top-level "org-colview")
  3706. (declare-function org-columns-compute "org-colview")
  3707. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3708. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3709. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3710. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3711. ;; Autoload ID code
  3712. (declare-function org-id-store-link "org-id")
  3713. (declare-function org-id-locations-load "org-id")
  3714. (declare-function org-id-locations-save "org-id")
  3715. (defvar org-id-track-globally)
  3716. (org-autoload "org-id"
  3717. '(org-id-get-create org-id-new org-id-copy org-id-get
  3718. org-id-get-with-outline-path-completion
  3719. org-id-get-with-outline-drilling org-id-store-link
  3720. org-id-goto org-id-find org-id-store-link))
  3721. ;; Autoload Plotting Code
  3722. (org-autoload "org-plot"
  3723. '(org-plot/gnuplot))
  3724. ;;; Variables for pre-computed regular expressions, all buffer local
  3725. (defvar org-drawer-regexp nil
  3726. "Matches first line of a hidden block.")
  3727. (make-variable-buffer-local 'org-drawer-regexp)
  3728. (defvar org-todo-regexp nil
  3729. "Matches any of the TODO state keywords.")
  3730. (make-variable-buffer-local 'org-todo-regexp)
  3731. (defvar org-not-done-regexp nil
  3732. "Matches any of the TODO state keywords except the last one.")
  3733. (make-variable-buffer-local 'org-not-done-regexp)
  3734. (defvar org-not-done-heading-regexp nil
  3735. "Matches a TODO headline that is not done.")
  3736. (make-variable-buffer-local 'org-not-done-regexp)
  3737. (defvar org-todo-line-regexp nil
  3738. "Matches a headline and puts TODO state into group 2 if present.")
  3739. (make-variable-buffer-local 'org-todo-line-regexp)
  3740. (defvar org-complex-heading-regexp nil
  3741. "Matches a headline and puts everything into groups:
  3742. group 1: the stars
  3743. group 2: The todo keyword, maybe
  3744. group 3: Priority cookie
  3745. group 4: True headline
  3746. group 5: Tags")
  3747. (make-variable-buffer-local 'org-complex-heading-regexp)
  3748. (defvar org-complex-heading-regexp-format nil
  3749. "Printf format to make regexp to match an exact headline.
  3750. This regexp will match the headline of any node which has the
  3751. exact headline text that is put into the format, but may have any
  3752. TODO state, priority and tags.")
  3753. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3754. (defvar org-todo-line-tags-regexp nil
  3755. "Matches a headline and puts TODO state into group 2 if present.
  3756. Also put tags into group 4 if tags are present.")
  3757. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3758. (defvar org-ds-keyword-length 12
  3759. "Maximum length of the Deadline and SCHEDULED keywords.")
  3760. (make-variable-buffer-local 'org-ds-keyword-length)
  3761. (defvar org-deadline-regexp nil
  3762. "Matches the DEADLINE keyword.")
  3763. (make-variable-buffer-local 'org-deadline-regexp)
  3764. (defvar org-deadline-time-regexp nil
  3765. "Matches the DEADLINE keyword together with a time stamp.")
  3766. (make-variable-buffer-local 'org-deadline-time-regexp)
  3767. (defvar org-deadline-line-regexp nil
  3768. "Matches the DEADLINE keyword and the rest of the line.")
  3769. (make-variable-buffer-local 'org-deadline-line-regexp)
  3770. (defvar org-scheduled-regexp nil
  3771. "Matches the SCHEDULED keyword.")
  3772. (make-variable-buffer-local 'org-scheduled-regexp)
  3773. (defvar org-scheduled-time-regexp nil
  3774. "Matches the SCHEDULED keyword together with a time stamp.")
  3775. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3776. (defvar org-closed-time-regexp nil
  3777. "Matches the CLOSED keyword together with a time stamp.")
  3778. (make-variable-buffer-local 'org-closed-time-regexp)
  3779. (defvar org-keyword-time-regexp nil
  3780. "Matches any of the 4 keywords, together with the time stamp.")
  3781. (make-variable-buffer-local 'org-keyword-time-regexp)
  3782. (defvar org-keyword-time-not-clock-regexp nil
  3783. "Matches any of the 3 keywords, together with the time stamp.")
  3784. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3785. (defvar org-maybe-keyword-time-regexp nil
  3786. "Matches a timestamp, possibly preceded by a keyword.")
  3787. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3788. (defvar org-planning-or-clock-line-re nil
  3789. "Matches a line with planning or clock info.")
  3790. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3791. (defvar org-all-time-keywords nil
  3792. "List of time keywords.")
  3793. (make-variable-buffer-local 'org-all-time-keywords)
  3794. (defconst org-plain-time-of-day-regexp
  3795. (concat
  3796. "\\(\\<[012]?[0-9]"
  3797. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3798. "\\(--?"
  3799. "\\(\\<[012]?[0-9]"
  3800. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3801. "\\)?")
  3802. "Regular expression to match a plain time or time range.
  3803. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3804. groups carry important information:
  3805. 0 the full match
  3806. 1 the first time, range or not
  3807. 8 the second time, if it is a range.")
  3808. (defconst org-plain-time-extension-regexp
  3809. (concat
  3810. "\\(\\<[012]?[0-9]"
  3811. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3812. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3813. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3814. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3815. groups carry important information:
  3816. 0 the full match
  3817. 7 hours of duration
  3818. 9 minutes of duration")
  3819. (defconst org-stamp-time-of-day-regexp
  3820. (concat
  3821. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3822. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3823. "\\(--?"
  3824. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3825. "Regular expression to match a timestamp time or time range.
  3826. After a match, the following groups carry important information:
  3827. 0 the full match
  3828. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3829. 2 the first time, range or not
  3830. 4 the second time, if it is a range.")
  3831. (defconst org-startup-options
  3832. '(("fold" org-startup-folded t)
  3833. ("overview" org-startup-folded t)
  3834. ("nofold" org-startup-folded nil)
  3835. ("showall" org-startup-folded nil)
  3836. ("showeverything" org-startup-folded showeverything)
  3837. ("content" org-startup-folded content)
  3838. ("indent" org-startup-indented t)
  3839. ("noindent" org-startup-indented nil)
  3840. ("hidestars" org-hide-leading-stars t)
  3841. ("showstars" org-hide-leading-stars nil)
  3842. ("odd" org-odd-levels-only t)
  3843. ("oddeven" org-odd-levels-only nil)
  3844. ("align" org-startup-align-all-tables t)
  3845. ("noalign" org-startup-align-all-tables nil)
  3846. ("inlineimages" org-startup-with-inline-images t)
  3847. ("noinlineimages" org-startup-with-inline-images nil)
  3848. ("customtime" org-display-custom-times t)
  3849. ("logdone" org-log-done time)
  3850. ("lognotedone" org-log-done note)
  3851. ("nologdone" org-log-done nil)
  3852. ("lognoteclock-out" org-log-note-clock-out t)
  3853. ("nolognoteclock-out" org-log-note-clock-out nil)
  3854. ("logrepeat" org-log-repeat state)
  3855. ("lognoterepeat" org-log-repeat note)
  3856. ("nologrepeat" org-log-repeat nil)
  3857. ("logreschedule" org-log-reschedule time)
  3858. ("lognotereschedule" org-log-reschedule note)
  3859. ("nologreschedule" org-log-reschedule nil)
  3860. ("logredeadline" org-log-redeadline time)
  3861. ("lognoteredeadline" org-log-redeadline note)
  3862. ("nologredeadline" org-log-redeadline nil)
  3863. ("logrefile" org-log-refile time)
  3864. ("lognoterefile" org-log-refile note)
  3865. ("nologrefile" org-log-refile nil)
  3866. ("fninline" org-footnote-define-inline t)
  3867. ("nofninline" org-footnote-define-inline nil)
  3868. ("fnlocal" org-footnote-section nil)
  3869. ("fnauto" org-footnote-auto-label t)
  3870. ("fnprompt" org-footnote-auto-label nil)
  3871. ("fnconfirm" org-footnote-auto-label confirm)
  3872. ("fnplain" org-footnote-auto-label plain)
  3873. ("fnadjust" org-footnote-auto-adjust t)
  3874. ("nofnadjust" org-footnote-auto-adjust nil)
  3875. ("constcgs" constants-unit-system cgs)
  3876. ("constSI" constants-unit-system SI)
  3877. ("noptag" org-tag-persistent-alist nil)
  3878. ("hideblocks" org-hide-block-startup t)
  3879. ("nohideblocks" org-hide-block-startup nil)
  3880. ("beamer" org-startup-with-beamer-mode t)
  3881. ("entitiespretty" org-pretty-entities t)
  3882. ("entitiesplain" org-pretty-entities nil))
  3883. "Variable associated with STARTUP options for org-mode.
  3884. Each element is a list of three items: The startup options as written
  3885. in the #+STARTUP line, the corresponding variable, and the value to
  3886. set this variable to if the option is found. An optional forth element PUSH
  3887. means to push this value onto the list in the variable.")
  3888. (defun org-update-property-plist (key val props)
  3889. "Update PROPS with KEY and VAL."
  3890. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3891. (key (if appending (substring key 0 (- (length key) 1)) key))
  3892. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3893. (previous (cdr (assoc key props))))
  3894. (if appending
  3895. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3896. (cons (cons key val) remainder))))
  3897. (defconst org-block-regexp
  3898. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3899. "Regular expression for hiding blocks.")
  3900. (defconst org-heading-keyword-regexp-format
  3901. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3902. "Printf format for a regexp matching an headline with some keyword.
  3903. This regexp will match the headline of any node which has the
  3904. exact keyword that is put into the format. The keyword isn't in
  3905. any group by default, but the stars and the body are.")
  3906. (defconst org-heading-keyword-maybe-regexp-format
  3907. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3908. "Printf format for a regexp matching an headline, possibly with some keyword.
  3909. This regexp can match any headline with the specified keyword, or
  3910. without a keyword. The keyword isn't in any group by default,
  3911. but the stars and the body are.")
  3912. (defun org-set-regexps-and-options ()
  3913. "Precompute regular expressions for current buffer."
  3914. (when (eq major-mode 'org-mode)
  3915. (org-set-local 'org-todo-kwd-alist nil)
  3916. (org-set-local 'org-todo-key-alist nil)
  3917. (org-set-local 'org-todo-key-trigger nil)
  3918. (org-set-local 'org-todo-keywords-1 nil)
  3919. (org-set-local 'org-done-keywords nil)
  3920. (org-set-local 'org-todo-heads nil)
  3921. (org-set-local 'org-todo-sets nil)
  3922. (org-set-local 'org-todo-log-states nil)
  3923. (org-set-local 'org-file-properties nil)
  3924. (org-set-local 'org-file-tags nil)
  3925. (let ((re (org-make-options-regexp
  3926. '("CATEGORY" "TODO" "COLUMNS"
  3927. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3928. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3929. "OPTIONS")
  3930. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3931. (splitre "[ \t]+")
  3932. (scripts org-use-sub-superscripts)
  3933. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3934. tail sep kws1 prio props ftags drawers beamer-p
  3935. ext-setup-or-nil setup-contents (start 0))
  3936. (save-excursion
  3937. (save-restriction
  3938. (widen)
  3939. (goto-char (point-min))
  3940. (while (or (and ext-setup-or-nil
  3941. (string-match re ext-setup-or-nil start)
  3942. (setq start (match-end 0)))
  3943. (and (setq ext-setup-or-nil nil start 0)
  3944. (re-search-forward re nil t)))
  3945. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3946. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3947. (if (stringp value) (setq value (org-trim value)))
  3948. (cond
  3949. ((equal key "CATEGORY")
  3950. (setq cat value))
  3951. ((member key '("SEQ_TODO" "TODO"))
  3952. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3953. ((equal key "TYP_TODO")
  3954. (push (cons 'type (org-split-string value splitre)) kwds))
  3955. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3956. ;; general TODO-like setup
  3957. (push (cons (intern (downcase (match-string 1 key)))
  3958. (org-split-string value splitre)) kwds))
  3959. ((equal key "TAGS")
  3960. (setq tags (append tags (if tags '("\\n") nil)
  3961. (org-split-string value splitre))))
  3962. ((equal key "COLUMNS")
  3963. (org-set-local 'org-columns-default-format value))
  3964. ((equal key "LINK")
  3965. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3966. (push (cons (match-string 1 value)
  3967. (org-trim (match-string 2 value)))
  3968. links)))
  3969. ((equal key "PRIORITIES")
  3970. (setq prio (org-split-string value " +")))
  3971. ((equal key "PROPERTY")
  3972. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3973. (setq props (org-update-property-plist (match-string 1 value)
  3974. (match-string 2 value)
  3975. props))))
  3976. ((equal key "FILETAGS")
  3977. (when (string-match "\\S-" value)
  3978. (setq ftags
  3979. (append
  3980. ftags
  3981. (apply 'append
  3982. (mapcar (lambda (x) (org-split-string x ":"))
  3983. (org-split-string value)))))))
  3984. ((equal key "DRAWERS")
  3985. (setq drawers (org-split-string value splitre)))
  3986. ((equal key "CONSTANTS")
  3987. (setq const (append const (org-split-string value splitre))))
  3988. ((equal key "STARTUP")
  3989. (let ((opts (org-split-string value splitre))
  3990. l var val)
  3991. (while (setq l (pop opts))
  3992. (when (setq l (assoc l org-startup-options))
  3993. (setq var (nth 1 l) val (nth 2 l))
  3994. (if (not (nth 3 l))
  3995. (set (make-local-variable var) val)
  3996. (if (not (listp (symbol-value var)))
  3997. (set (make-local-variable var) nil))
  3998. (set (make-local-variable var) (symbol-value var))
  3999. (add-to-list var val))))))
  4000. ((equal key "ARCHIVE")
  4001. (setq arch value)
  4002. (remove-text-properties 0 (length arch)
  4003. '(face t fontified t) arch))
  4004. ((equal key "LATEX_CLASS")
  4005. (setq beamer-p (equal value "beamer")))
  4006. ((equal key "OPTIONS")
  4007. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4008. (setq scripts (read (match-string 2 value)))))
  4009. ((equal key "SETUPFILE")
  4010. (setq setup-contents (org-file-contents
  4011. (expand-file-name
  4012. (org-remove-double-quotes value))
  4013. 'noerror))
  4014. (if (not ext-setup-or-nil)
  4015. (setq ext-setup-or-nil setup-contents start 0)
  4016. (setq ext-setup-or-nil
  4017. (concat (substring ext-setup-or-nil 0 start)
  4018. "\n" setup-contents "\n"
  4019. (substring ext-setup-or-nil start)))))))
  4020. ;; search for property blocks
  4021. (goto-char (point-min))
  4022. (while (re-search-forward org-block-regexp nil t)
  4023. (when (equal "PROPERTY" (upcase (match-string 1)))
  4024. (setq value (replace-regexp-in-string
  4025. "[\n\r]" " " (match-string 4)))
  4026. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4027. (setq props (org-update-property-plist (match-string 1 value)
  4028. (match-string 2 value)
  4029. props)))))))
  4030. (org-set-local 'org-use-sub-superscripts scripts)
  4031. (when cat
  4032. (org-set-local 'org-category (intern cat))
  4033. (push (cons "CATEGORY" cat) props))
  4034. (when prio
  4035. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4036. (setq prio (mapcar 'string-to-char prio))
  4037. (org-set-local 'org-highest-priority (nth 0 prio))
  4038. (org-set-local 'org-lowest-priority (nth 1 prio))
  4039. (org-set-local 'org-default-priority (nth 2 prio)))
  4040. (and props (org-set-local 'org-file-properties (nreverse props)))
  4041. (and ftags (org-set-local 'org-file-tags
  4042. (mapcar 'org-add-prop-inherited ftags)))
  4043. (and drawers (org-set-local 'org-drawers drawers))
  4044. (and arch (org-set-local 'org-archive-location arch))
  4045. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4046. ;; Process the TODO keywords
  4047. (unless kwds
  4048. ;; Use the global values as if they had been given locally.
  4049. (setq kwds (default-value 'org-todo-keywords))
  4050. (if (stringp (car kwds))
  4051. (setq kwds (list (cons org-todo-interpretation
  4052. (default-value 'org-todo-keywords)))))
  4053. (setq kwds (reverse kwds)))
  4054. (setq kwds (nreverse kwds))
  4055. (let (inter kws kw)
  4056. (while (setq kws (pop kwds))
  4057. (let ((kws (or
  4058. (run-hook-with-args-until-success
  4059. 'org-todo-setup-filter-hook kws)
  4060. kws)))
  4061. (setq inter (pop kws) sep (member "|" kws)
  4062. kws0 (delete "|" (copy-sequence kws))
  4063. kwsa nil
  4064. kws1 (mapcar
  4065. (lambda (x)
  4066. ;; 1 2
  4067. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4068. (progn
  4069. (setq kw (match-string 1 x)
  4070. key (and (match-end 2) (match-string 2 x))
  4071. log (org-extract-log-state-settings x))
  4072. (push (cons kw (and key (string-to-char key))) kwsa)
  4073. (and log (push log org-todo-log-states))
  4074. kw)
  4075. (error "Invalid TODO keyword %s" x)))
  4076. kws0)
  4077. kwsa (if kwsa (append '((:startgroup))
  4078. (nreverse kwsa)
  4079. '((:endgroup))))
  4080. hw (car kws1)
  4081. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4082. tail (list inter hw (car dws) (org-last dws))))
  4083. (add-to-list 'org-todo-heads hw 'append)
  4084. (push kws1 org-todo-sets)
  4085. (setq org-done-keywords (append org-done-keywords dws nil))
  4086. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4087. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4088. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4089. (setq org-todo-sets (nreverse org-todo-sets)
  4090. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4091. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4092. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4093. ;; Process the constants
  4094. (when const
  4095. (let (e cst)
  4096. (while (setq e (pop const))
  4097. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4098. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4099. (setq org-table-formula-constants-local cst)))
  4100. ;; Process the tags.
  4101. (when tags
  4102. (let (e tgs)
  4103. (while (setq e (pop tags))
  4104. (cond
  4105. ((equal e "{") (push '(:startgroup) tgs))
  4106. ((equal e "}") (push '(:endgroup) tgs))
  4107. ((equal e "\\n") (push '(:newline) tgs))
  4108. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4109. (push (cons (match-string 1 e)
  4110. (string-to-char (match-string 2 e)))
  4111. tgs))
  4112. (t (push (list e) tgs))))
  4113. (org-set-local 'org-tag-alist nil)
  4114. (while (setq e (pop tgs))
  4115. (or (and (stringp (car e))
  4116. (assoc (car e) org-tag-alist))
  4117. (push e org-tag-alist)))))
  4118. ;; Compute the regular expressions and other local variables.
  4119. ;; Using `org-outline-regexp-bol' would complicate them much,
  4120. ;; because of the fixed white space at the end of that string.
  4121. (if (not org-done-keywords)
  4122. (setq org-done-keywords (and org-todo-keywords-1
  4123. (list (org-last org-todo-keywords-1)))))
  4124. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4125. (length org-scheduled-string)
  4126. (length org-clock-string)
  4127. (length org-closed-string)))
  4128. org-drawer-regexp
  4129. (concat "^[ \t]*:\\("
  4130. (mapconcat 'regexp-quote org-drawers "\\|")
  4131. "\\):[ \t]*$")
  4132. org-not-done-keywords
  4133. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4134. org-todo-regexp
  4135. (concat "\\("
  4136. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4137. "\\)")
  4138. org-not-done-regexp
  4139. (concat "\\("
  4140. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4141. "\\)")
  4142. org-not-done-heading-regexp
  4143. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4144. org-todo-line-regexp
  4145. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4146. org-complex-heading-regexp
  4147. (concat "^\\(\\*+\\)"
  4148. "\\(?: +" org-todo-regexp "\\)?"
  4149. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4150. "\\(?: +\\(.*?\\)\\)?"
  4151. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4152. "[ \t]*$")
  4153. org-complex-heading-regexp-format
  4154. (concat "^\\(\\*+\\)"
  4155. "\\(?: +" org-todo-regexp "\\)?"
  4156. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4157. "\\(?: +"
  4158. ;; Stats cookies can be stuck to body.
  4159. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4160. "\\(%s\\)"
  4161. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4162. "\\)"
  4163. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4164. "[ \t]*$")
  4165. org-todo-line-tags-regexp
  4166. (concat "^\\(\\*+\\)"
  4167. "\\(?: +" org-todo-regexp "\\)?"
  4168. "\\(?: +\\(.*?\\)\\)?"
  4169. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4170. "[ \t]*$")
  4171. org-deadline-regexp (concat "\\<" org-deadline-string)
  4172. org-deadline-time-regexp
  4173. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4174. org-deadline-line-regexp
  4175. (concat "\\<\\(" org-deadline-string "\\).*")
  4176. org-scheduled-regexp
  4177. (concat "\\<" org-scheduled-string)
  4178. org-scheduled-time-regexp
  4179. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4180. org-closed-time-regexp
  4181. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4182. org-keyword-time-regexp
  4183. (concat "\\<\\(" org-scheduled-string
  4184. "\\|" org-deadline-string
  4185. "\\|" org-closed-string
  4186. "\\|" org-clock-string "\\)"
  4187. " *[[<]\\([^]>]+\\)[]>]")
  4188. org-keyword-time-not-clock-regexp
  4189. (concat "\\<\\(" org-scheduled-string
  4190. "\\|" org-deadline-string
  4191. "\\|" org-closed-string
  4192. "\\)"
  4193. " *[[<]\\([^]>]+\\)[]>]")
  4194. org-maybe-keyword-time-regexp
  4195. (concat "\\(\\<\\(" org-scheduled-string
  4196. "\\|" org-deadline-string
  4197. "\\|" org-closed-string
  4198. "\\|" org-clock-string "\\)\\)?"
  4199. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4200. org-planning-or-clock-line-re
  4201. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4202. "\\|" org-deadline-string
  4203. "\\|" org-closed-string "\\|" org-clock-string
  4204. "\\)\\>\\)")
  4205. org-all-time-keywords
  4206. (mapcar (lambda (w) (substring w 0 -1))
  4207. (list org-scheduled-string org-deadline-string
  4208. org-clock-string org-closed-string))
  4209. )
  4210. (org-compute-latex-and-specials-regexp)
  4211. (org-set-font-lock-defaults))))
  4212. (defun org-file-contents (file &optional noerror)
  4213. "Return the contents of FILE, as a string."
  4214. (if (or (not file)
  4215. (not (file-readable-p file)))
  4216. (if noerror
  4217. (progn
  4218. (message "Cannot read file \"%s\"" file)
  4219. (ding) (sit-for 2)
  4220. "")
  4221. (error "Cannot read file \"%s\"" file))
  4222. (with-temp-buffer
  4223. (insert-file-contents file)
  4224. (buffer-string))))
  4225. (defun org-extract-log-state-settings (x)
  4226. "Extract the log state setting from a TODO keyword string.
  4227. This will extract info from a string like \"WAIT(w@/!)\"."
  4228. (let (kw key log1 log2)
  4229. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4230. (setq kw (match-string 1 x)
  4231. key (and (match-end 2) (match-string 2 x))
  4232. log1 (and (match-end 3) (match-string 3 x))
  4233. log2 (and (match-end 4) (match-string 4 x)))
  4234. (and (or log1 log2)
  4235. (list kw
  4236. (and log1 (if (equal log1 "!") 'time 'note))
  4237. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4238. (defun org-remove-keyword-keys (list)
  4239. "Remove a pair of parenthesis at the end of each string in LIST."
  4240. (mapcar (lambda (x)
  4241. (if (string-match "(.*)$" x)
  4242. (substring x 0 (match-beginning 0))
  4243. x))
  4244. list))
  4245. (defun org-assign-fast-keys (alist)
  4246. "Assign fast keys to a keyword-key alist.
  4247. Respect keys that are already there."
  4248. (let (new e (alt ?0))
  4249. (while (setq e (pop alist))
  4250. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4251. (cdr e)) ;; Key already assigned.
  4252. (push e new)
  4253. (let ((clist (string-to-list (downcase (car e))))
  4254. (used (append new alist)))
  4255. (when (= (car clist) ?@)
  4256. (pop clist))
  4257. (while (and clist (rassoc (car clist) used))
  4258. (pop clist))
  4259. (unless clist
  4260. (while (rassoc alt used)
  4261. (incf alt)))
  4262. (push (cons (car e) (or (car clist) alt)) new))))
  4263. (nreverse new)))
  4264. ;;; Some variables used in various places
  4265. (defvar org-window-configuration nil
  4266. "Used in various places to store a window configuration.")
  4267. (defvar org-selected-window nil
  4268. "Used in various places to store a window configuration.")
  4269. (defvar org-finish-function nil
  4270. "Function to be called when `C-c C-c' is used.
  4271. This is for getting out of special buffers like remember.")
  4272. ;; FIXME: Occasionally check by commenting these, to make sure
  4273. ;; no other functions uses these, forgetting to let-bind them.
  4274. (defvar entry)
  4275. (defvar last-state)
  4276. (defvar date)
  4277. ;; Defined somewhere in this file, but used before definition.
  4278. (defvar org-entities) ;; defined in org-entities.el
  4279. (defvar org-struct-menu)
  4280. (defvar org-org-menu)
  4281. (defvar org-tbl-menu)
  4282. ;;;; Define the Org-mode
  4283. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4284. (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"))
  4285. ;; We use a before-change function to check if a table might need
  4286. ;; an update.
  4287. (defvar org-table-may-need-update t
  4288. "Indicates that a table might need an update.
  4289. This variable is set by `org-before-change-function'.
  4290. `org-table-align' sets it back to nil.")
  4291. (defun org-before-change-function (beg end)
  4292. "Every change indicates that a table might need an update."
  4293. (setq org-table-may-need-update t))
  4294. (defvar org-mode-map)
  4295. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4296. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4297. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4298. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4299. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4300. (defvar org-table-buffer-is-an nil)
  4301. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  4302. ;; some places -- e.g. see the macro org-with-limited-levels.
  4303. ;;
  4304. ;; In Org buffers, the value of `outline-regexp' is that of
  4305. ;; `org-outline-regexp'. The only function still directly relying on
  4306. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  4307. ;; job when `orgstruct-mode' is active.
  4308. (defvar org-outline-regexp "\\*+ "
  4309. "Regexp to match Org headlines.")
  4310. (defconst org-outline-regexp-bol "^\\*+ "
  4311. "Regexp to match Org headlines.
  4312. This is similar to `org-outline-regexp' but additionally makes
  4313. sure that we are at the beginning of the line.")
  4314. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4315. "Matches an headline, putting stars and text into groups.
  4316. Stars are put in group 1 and the trimmed body in group 2.")
  4317. ;;;###autoload
  4318. (define-derived-mode org-mode outline-mode "Org"
  4319. "Outline-based notes management and organizer, alias
  4320. \"Carsten's outline-mode for keeping track of everything.\"
  4321. Org-mode develops organizational tasks around a NOTES file which
  4322. contains information about projects as plain text. Org-mode is
  4323. implemented on top of outline-mode, which is ideal to keep the content
  4324. of large files well structured. It supports ToDo items, deadlines and
  4325. time stamps, which magically appear in the diary listing of the Emacs
  4326. calendar. Tables are easily created with a built-in table editor.
  4327. Plain text URL-like links connect to websites, emails (VM), Usenet
  4328. messages (Gnus), BBDB entries, and any files related to the project.
  4329. For printing and sharing of notes, an Org-mode file (or a part of it)
  4330. can be exported as a structured ASCII or HTML file.
  4331. The following commands are available:
  4332. \\{org-mode-map}"
  4333. ;; Get rid of Outline menus, they are not needed
  4334. ;; Need to do this here because define-derived-mode sets up
  4335. ;; the keymap so late. Still, it is a waste to call this each time
  4336. ;; we switch another buffer into org-mode.
  4337. (if (featurep 'xemacs)
  4338. (when (boundp 'outline-mode-menu-heading)
  4339. ;; Assume this is Greg's port, it uses easymenu
  4340. (easy-menu-remove outline-mode-menu-heading)
  4341. (easy-menu-remove outline-mode-menu-show)
  4342. (easy-menu-remove outline-mode-menu-hide))
  4343. (define-key org-mode-map [menu-bar headings] 'undefined)
  4344. (define-key org-mode-map [menu-bar hide] 'undefined)
  4345. (define-key org-mode-map [menu-bar show] 'undefined))
  4346. (org-load-modules-maybe)
  4347. (easy-menu-add org-org-menu)
  4348. (easy-menu-add org-tbl-menu)
  4349. (org-install-agenda-files-menu)
  4350. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4351. (add-to-invisibility-spec '(org-cwidth))
  4352. (add-to-invisibility-spec '(org-hide-block . t))
  4353. (when (featurep 'xemacs)
  4354. (org-set-local 'line-move-ignore-invisible t))
  4355. (org-set-local 'outline-regexp org-outline-regexp)
  4356. (org-set-local 'outline-level 'org-outline-level)
  4357. (setq bidi-paragraph-direction 'left-to-right)
  4358. (when (and org-ellipsis
  4359. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4360. (fboundp 'make-glyph-code))
  4361. (unless org-display-table
  4362. (setq org-display-table (make-display-table)))
  4363. (set-display-table-slot
  4364. org-display-table 4
  4365. (vconcat (mapcar
  4366. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4367. org-ellipsis)))
  4368. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4369. (setq buffer-display-table org-display-table))
  4370. (org-set-regexps-and-options)
  4371. (when (and org-tag-faces (not org-tags-special-faces-re))
  4372. ;; tag faces set outside customize.... force initialization.
  4373. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4374. ;; Calc embedded
  4375. (org-set-local 'calc-embedded-open-mode "# ")
  4376. (modify-syntax-entry ?@ "w")
  4377. (if org-startup-truncated (setq truncate-lines t))
  4378. (org-set-local 'font-lock-unfontify-region-function
  4379. 'org-unfontify-region)
  4380. ;; Activate before-change-function
  4381. (org-set-local 'org-table-may-need-update t)
  4382. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4383. 'local)
  4384. ;; Check for running clock before killing a buffer
  4385. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4386. ;; Paragraphs and auto-filling
  4387. (org-set-autofill-regexps)
  4388. (setq indent-line-function 'org-indent-line-function)
  4389. (org-update-radio-target-regexp)
  4390. ;; Beginning/end of defun
  4391. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4392. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4393. ;; Next error for sparse trees
  4394. (org-set-local 'next-error-function 'org-occur-next-match)
  4395. ;; Make sure dependence stuff works reliably, even for users who set it
  4396. ;; too late :-(
  4397. (if org-enforce-todo-dependencies
  4398. (add-hook 'org-blocker-hook
  4399. 'org-block-todo-from-children-or-siblings-or-parent)
  4400. (remove-hook 'org-blocker-hook
  4401. 'org-block-todo-from-children-or-siblings-or-parent))
  4402. (if org-enforce-todo-checkbox-dependencies
  4403. (add-hook 'org-blocker-hook
  4404. 'org-block-todo-from-checkboxes)
  4405. (remove-hook 'org-blocker-hook
  4406. 'org-block-todo-from-checkboxes))
  4407. ;; Comment characters
  4408. (org-set-local 'comment-start "#")
  4409. (org-set-local 'comment-padding " ")
  4410. ;; Align options lines
  4411. (org-set-local
  4412. 'align-mode-rules-list
  4413. '((org-in-buffer-settings
  4414. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4415. (modes . '(org-mode)))))
  4416. ;; Imenu
  4417. (org-set-local 'imenu-create-index-function
  4418. 'org-imenu-get-tree)
  4419. ;; Make isearch reveal context
  4420. (if (or (featurep 'xemacs)
  4421. (not (boundp 'outline-isearch-open-invisible-function)))
  4422. ;; Emacs 21 and XEmacs make use of the hook
  4423. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4424. ;; Emacs 22 deals with this through a special variable
  4425. (org-set-local 'outline-isearch-open-invisible-function
  4426. (lambda (&rest ignore) (org-show-context 'isearch))))
  4427. ;; Turn on org-beamer-mode?
  4428. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4429. ;; Setup the pcomplete hooks
  4430. (set (make-local-variable 'pcomplete-command-completion-function)
  4431. 'org-pcomplete-initial)
  4432. (set (make-local-variable 'pcomplete-command-name-function)
  4433. 'org-command-at-point)
  4434. (set (make-local-variable 'pcomplete-default-completion-function)
  4435. 'ignore)
  4436. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4437. 'org-parse-arguments)
  4438. (set (make-local-variable 'pcomplete-termination-string) "")
  4439. (set (make-local-variable 'face-remapping-alist)
  4440. '((default org-default)))
  4441. ;; If empty file that did not turn on org-mode automatically, make it to.
  4442. (if (and org-insert-mode-line-in-empty-file
  4443. (org-called-interactively-p 'any)
  4444. (= (point-min) (point-max)))
  4445. (insert "# -*- mode: org -*-\n\n"))
  4446. (unless org-inhibit-startup
  4447. (when org-startup-align-all-tables
  4448. (let ((bmp (buffer-modified-p)))
  4449. (org-table-map-tables 'org-table-align 'quietly)
  4450. (set-buffer-modified-p bmp)))
  4451. (when org-startup-with-inline-images
  4452. (org-display-inline-images))
  4453. (when org-startup-indented
  4454. (require 'org-indent)
  4455. (org-indent-mode 1))
  4456. (unless org-inhibit-startup-visibility-stuff
  4457. (org-set-startup-visibility))))
  4458. (when (fboundp 'abbrev-table-put)
  4459. (abbrev-table-put org-mode-abbrev-table
  4460. :parents (list text-mode-abbrev-table)))
  4461. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4462. (defun org-current-time ()
  4463. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4464. (if (> (car org-time-stamp-rounding-minutes) 1)
  4465. (let ((r (car org-time-stamp-rounding-minutes))
  4466. (time (decode-time)))
  4467. (apply 'encode-time
  4468. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4469. (nthcdr 2 time))))
  4470. (current-time)))
  4471. (defun org-today ()
  4472. "Return today date, considering `org-extend-today-until'."
  4473. (time-to-days
  4474. (time-subtract (current-time)
  4475. (list 0 (* 3600 org-extend-today-until) 0))))
  4476. ;;;; Font-Lock stuff, including the activators
  4477. (defvar org-mouse-map (make-sparse-keymap))
  4478. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4479. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4480. (when org-mouse-1-follows-link
  4481. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4482. (when org-tab-follows-link
  4483. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4484. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4485. (require 'font-lock)
  4486. (defconst org-non-link-chars "]\t\n\r<>")
  4487. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4488. "shell" "elisp" "doi" "message"))
  4489. (defvar org-link-types-re nil
  4490. "Matches a link that has a url-like prefix like \"http:\"")
  4491. (defvar org-link-re-with-space nil
  4492. "Matches a link with spaces, optional angular brackets around it.")
  4493. (defvar org-link-re-with-space2 nil
  4494. "Matches a link with spaces, optional angular brackets around it.")
  4495. (defvar org-link-re-with-space3 nil
  4496. "Matches a link with spaces, only for internal part in bracket links.")
  4497. (defvar org-angle-link-re nil
  4498. "Matches link with angular brackets, spaces are allowed.")
  4499. (defvar org-plain-link-re nil
  4500. "Matches plain link, without spaces.")
  4501. (defvar org-bracket-link-regexp nil
  4502. "Matches a link in double brackets.")
  4503. (defvar org-bracket-link-analytic-regexp nil
  4504. "Regular expression used to analyze links.
  4505. Here is what the match groups contain after a match:
  4506. 1: http:
  4507. 2: http
  4508. 3: path
  4509. 4: [desc]
  4510. 5: desc")
  4511. (defvar org-bracket-link-analytic-regexp++ nil
  4512. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4513. (defvar org-any-link-re nil
  4514. "Regular expression matching any link.")
  4515. (defcustom org-match-sexp-depth 3
  4516. "Number of stacked braces for sub/superscript matching.
  4517. This has to be set before loading org.el to be effective."
  4518. :group 'org-export-translation ; ??????????????????????????/
  4519. :type 'integer)
  4520. (defun org-create-multibrace-regexp (left right n)
  4521. "Create a regular expression which will match a balanced sexp.
  4522. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4523. as single character strings.
  4524. The regexp returned will match the entire expression including the
  4525. delimiters. It will also define a single group which contains the
  4526. match except for the outermost delimiters. The maximum depth of
  4527. stacked delimiters is N. Escaping delimiters is not possible."
  4528. (let* ((nothing (concat "[^" left right "]*?"))
  4529. (or "\\|")
  4530. (re nothing)
  4531. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4532. (while (> n 1)
  4533. (setq n (1- n)
  4534. re (concat re or next)
  4535. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4536. (concat left "\\(" re "\\)" right)))
  4537. (defvar org-match-substring-regexp
  4538. (concat
  4539. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4540. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4541. "\\|"
  4542. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4543. "\\|"
  4544. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4545. "The regular expression matching a sub- or superscript.")
  4546. (defvar org-match-substring-with-braces-regexp
  4547. (concat
  4548. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4549. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4550. "\\)")
  4551. "The regular expression matching a sub- or superscript, forcing braces.")
  4552. (defun org-make-link-regexps ()
  4553. "Update the link regular expressions.
  4554. This should be called after the variable `org-link-types' has changed."
  4555. (setq org-link-types-re
  4556. (concat
  4557. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4558. org-link-re-with-space
  4559. (concat
  4560. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4561. "\\([^" org-non-link-chars " ]"
  4562. "[^" org-non-link-chars "]*"
  4563. "[^" org-non-link-chars " ]\\)>?")
  4564. org-link-re-with-space2
  4565. (concat
  4566. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4567. "\\([^" org-non-link-chars " ]"
  4568. "[^\t\n\r]*"
  4569. "[^" org-non-link-chars " ]\\)>?")
  4570. org-link-re-with-space3
  4571. (concat
  4572. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4573. "\\([^" org-non-link-chars " ]"
  4574. "[^\t\n\r]*\\)")
  4575. org-angle-link-re
  4576. (concat
  4577. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4578. "\\([^" org-non-link-chars " ]"
  4579. "[^" org-non-link-chars "]*"
  4580. "\\)>")
  4581. org-plain-link-re
  4582. (concat
  4583. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4584. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4585. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4586. org-bracket-link-regexp
  4587. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4588. org-bracket-link-analytic-regexp
  4589. (concat
  4590. "\\[\\["
  4591. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4592. "\\([^]]+\\)"
  4593. "\\]"
  4594. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4595. "\\]")
  4596. org-bracket-link-analytic-regexp++
  4597. (concat
  4598. "\\[\\["
  4599. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4600. "\\([^]]+\\)"
  4601. "\\]"
  4602. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4603. "\\]")
  4604. org-any-link-re
  4605. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4606. org-angle-link-re "\\)\\|\\("
  4607. org-plain-link-re "\\)")))
  4608. (org-make-link-regexps)
  4609. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4610. "Regular expression for fast time stamp matching.")
  4611. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4612. "Regular expression for fast time stamp matching.")
  4613. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4614. "Regular expression matching time strings for analysis.
  4615. This one does not require the space after the date, so it can be used
  4616. on a string that terminates immediately after the date.")
  4617. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4618. "Regular expression matching time strings for analysis.")
  4619. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4620. "Regular expression matching time stamps, with groups.")
  4621. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4622. "Regular expression matching time stamps (also [..]), with groups.")
  4623. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4624. "Regular expression matching a time stamp range.")
  4625. (defconst org-tr-regexp-both
  4626. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4627. "Regular expression matching a time stamp range.")
  4628. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4629. org-ts-regexp "\\)?")
  4630. "Regular expression matching a time stamp or time stamp range.")
  4631. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4632. org-ts-regexp-both "\\)?")
  4633. "Regular expression matching a time stamp or time stamp range.
  4634. The time stamps may be either active or inactive.")
  4635. (defvar org-emph-face nil)
  4636. (defun org-do-emphasis-faces (limit)
  4637. "Run through the buffer and add overlays to emphasized strings."
  4638. (let (rtn a)
  4639. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4640. (if (not (= (char-after (match-beginning 3))
  4641. (char-after (match-beginning 4))))
  4642. (progn
  4643. (setq rtn t)
  4644. (setq a (assoc (match-string 3) org-emphasis-alist))
  4645. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4646. 'face
  4647. (nth 1 a))
  4648. (and (nth 4 a)
  4649. (org-remove-flyspell-overlays-in
  4650. (match-beginning 0) (match-end 0)))
  4651. (add-text-properties (match-beginning 2) (match-end 2)
  4652. '(font-lock-multiline t org-emphasis t))
  4653. (when org-hide-emphasis-markers
  4654. (add-text-properties (match-end 4) (match-beginning 5)
  4655. '(invisible org-link))
  4656. (add-text-properties (match-beginning 3) (match-end 3)
  4657. '(invisible org-link)))))
  4658. (backward-char 1))
  4659. rtn))
  4660. (defun org-emphasize (&optional char)
  4661. "Insert or change an emphasis, i.e. a font like bold or italic.
  4662. If there is an active region, change that region to a new emphasis.
  4663. If there is no region, just insert the marker characters and position
  4664. the cursor between them.
  4665. CHAR should be either the marker character, or the first character of the
  4666. HTML tag associated with that emphasis. If CHAR is a space, the means
  4667. to remove the emphasis of the selected region.
  4668. If char is not given (for example in an interactive call) it
  4669. will be prompted for."
  4670. (interactive)
  4671. (let ((eal org-emphasis-alist) e det
  4672. (erc org-emphasis-regexp-components)
  4673. (prompt "")
  4674. (string "") beg end move tag c s)
  4675. (if (org-region-active-p)
  4676. (setq beg (region-beginning) end (region-end)
  4677. string (buffer-substring beg end))
  4678. (setq move t))
  4679. (while (setq e (pop eal))
  4680. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4681. c (aref tag 0))
  4682. (push (cons c (string-to-char (car e))) det)
  4683. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4684. (substring tag 1)))))
  4685. (setq det (nreverse det))
  4686. (unless char
  4687. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4688. (setq char (read-char-exclusive)))
  4689. (setq char (or (cdr (assoc char det)) char))
  4690. (if (equal char ?\ )
  4691. (setq s "" move nil)
  4692. (unless (assoc (char-to-string char) org-emphasis-alist)
  4693. (error "No such emphasis marker: \"%c\"" char))
  4694. (setq s (char-to-string char)))
  4695. (while (and (> (length string) 1)
  4696. (equal (substring string 0 1) (substring string -1))
  4697. (assoc (substring string 0 1) org-emphasis-alist))
  4698. (setq string (substring string 1 -1)))
  4699. (setq string (concat s string s))
  4700. (if beg (delete-region beg end))
  4701. (unless (or (bolp)
  4702. (string-match (concat "[" (nth 0 erc) "\n]")
  4703. (char-to-string (char-before (point)))))
  4704. (insert " "))
  4705. (unless (or (eobp)
  4706. (string-match (concat "[" (nth 1 erc) "\n]")
  4707. (char-to-string (char-after (point)))))
  4708. (insert " ") (backward-char 1))
  4709. (insert string)
  4710. (and move (backward-char 1))))
  4711. (defconst org-nonsticky-props
  4712. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4713. (defsubst org-rear-nonsticky-at (pos)
  4714. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4715. (defun org-activate-plain-links (limit)
  4716. "Run through the buffer and add overlays to links."
  4717. (catch 'exit
  4718. (let (f)
  4719. (if (re-search-forward org-plain-link-re limit t)
  4720. (progn
  4721. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4722. (setq f (get-text-property (match-beginning 0) 'face))
  4723. (if (or (eq f 'org-tag)
  4724. (and (listp f) (memq 'org-tag f)))
  4725. nil
  4726. (add-text-properties (match-beginning 0) (match-end 0)
  4727. (list 'mouse-face 'highlight
  4728. 'face 'org-link
  4729. 'keymap org-mouse-map))
  4730. (org-rear-nonsticky-at (match-end 0)))
  4731. t)))))
  4732. (defun org-activate-code (limit)
  4733. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4734. (progn
  4735. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4736. (remove-text-properties (match-beginning 0) (match-end 0)
  4737. '(display t invisible t intangible t))
  4738. t)))
  4739. (defcustom org-src-fontify-natively nil
  4740. "When non-nil, fontify code in code blocks."
  4741. :type 'boolean
  4742. :group 'org-appearance
  4743. :group 'org-babel)
  4744. (defun org-fontify-meta-lines-and-blocks (limit)
  4745. (condition-case nil
  4746. (org-fontify-meta-lines-and-blocks-1 limit)
  4747. (error (message "org-mode fontification error"))))
  4748. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4749. "Fontify #+ lines and blocks, in the correct ways."
  4750. (let ((case-fold-search t))
  4751. (if (re-search-forward
  4752. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4753. limit t)
  4754. (let ((beg (match-beginning 0))
  4755. (block-start (match-end 0))
  4756. (block-end nil)
  4757. (lang (match-string 7))
  4758. (beg1 (line-beginning-position 2))
  4759. (dc1 (downcase (match-string 2)))
  4760. (dc3 (downcase (match-string 3)))
  4761. end end1 quoting block-type ovl)
  4762. (cond
  4763. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4764. ;; a single line of backend-specific content
  4765. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4766. (remove-text-properties (match-beginning 0) (match-end 0)
  4767. '(display t invisible t intangible t))
  4768. (add-text-properties (match-beginning 1) (match-end 3)
  4769. '(font-lock-fontified t face org-meta-line))
  4770. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4771. '(font-lock-fontified t face org-block))
  4772. ; for backend-specific code
  4773. t)
  4774. ((and (match-end 4) (equal dc3 "begin"))
  4775. ;; Truly a block
  4776. (setq block-type (downcase (match-string 5))
  4777. quoting (member block-type org-protecting-blocks))
  4778. (when (re-search-forward
  4779. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4780. nil t) ;; on purpose, we look further than LIMIT
  4781. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4782. (setq block-end (match-beginning 0))
  4783. (when quoting
  4784. (remove-text-properties beg end
  4785. '(display t invisible t intangible t)))
  4786. (add-text-properties
  4787. beg end
  4788. '(font-lock-fontified t font-lock-multiline t))
  4789. (add-text-properties beg beg1 '(face org-meta-line))
  4790. (add-text-properties end1 (min (point-max) (1+ end))
  4791. '(face org-meta-line)) ; for end_src
  4792. (cond
  4793. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4794. (org-src-font-lock-fontify-block lang block-start block-end)
  4795. ;; remove old background overlays
  4796. (mapc (lambda (ov)
  4797. (if (eq (overlay-get ov 'face) 'org-block-background)
  4798. (delete-overlay ov)))
  4799. (overlays-at (/ (+ beg1 block-end) 2)))
  4800. ;; add a background overlay
  4801. (setq ovl (make-overlay beg1 block-end))
  4802. (overlay-put ovl 'face 'org-block-background)
  4803. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4804. (quoting
  4805. (add-text-properties beg1 (min (point-max) (1+ end1))
  4806. '(face org-block))) ; end of source block
  4807. ((not org-fontify-quote-and-verse-blocks))
  4808. ((string= block-type "quote")
  4809. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4810. ((string= block-type "verse")
  4811. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4812. (add-text-properties beg beg1 '(face org-block-begin-line))
  4813. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4814. t))
  4815. ((member dc1 '("title:" "author:" "email:" "date:"))
  4816. (add-text-properties
  4817. beg (match-end 3)
  4818. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4819. '(font-lock-fontified t invisible t)
  4820. '(font-lock-fontified t face org-document-info-keyword)))
  4821. (add-text-properties
  4822. (match-beginning 6) (match-end 6)
  4823. (if (string-equal dc1 "title:")
  4824. '(font-lock-fontified t face org-document-title)
  4825. '(font-lock-fontified t face org-document-info))))
  4826. ((not (member (char-after beg) '(?\ ?\t)))
  4827. ;; just any other in-buffer setting, but not indented
  4828. (add-text-properties
  4829. beg (1+ (match-end 0))
  4830. '(font-lock-fontified t face org-meta-line))
  4831. t)
  4832. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4833. "orgtbl:" "tblfm:" "tblname:" "results:"
  4834. "call:" "header:" "headers:" "name:"))
  4835. (and (match-end 4) (equal dc3 "attr")))
  4836. (add-text-properties
  4837. beg (match-end 0)
  4838. '(font-lock-fontified t face org-meta-line))
  4839. t)
  4840. ((member dc3 '(" " ""))
  4841. (add-text-properties
  4842. beg (match-end 0)
  4843. '(font-lock-fontified t face font-lock-comment-face)))
  4844. (t nil))))))
  4845. (defun org-activate-angle-links (limit)
  4846. "Run through the buffer and add overlays to links."
  4847. (if (re-search-forward org-angle-link-re limit t)
  4848. (progn
  4849. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4850. (add-text-properties (match-beginning 0) (match-end 0)
  4851. (list 'mouse-face 'highlight
  4852. 'keymap org-mouse-map))
  4853. (org-rear-nonsticky-at (match-end 0))
  4854. t)))
  4855. (defun org-activate-footnote-links (limit)
  4856. "Run through the buffer and add overlays to footnotes."
  4857. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4858. (when fn
  4859. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4860. (org-remove-flyspell-overlays-in beg end)
  4861. (add-text-properties beg end
  4862. (list 'mouse-face 'highlight
  4863. 'keymap org-mouse-map
  4864. 'help-echo
  4865. (if (= (point-at-bol) beg)
  4866. "Footnote definition"
  4867. "Footnote reference")
  4868. 'font-lock-fontified t
  4869. 'font-lock-multiline t
  4870. 'face 'org-footnote))))))
  4871. (defun org-activate-bracket-links (limit)
  4872. "Run through the buffer and add overlays to bracketed links."
  4873. (if (re-search-forward org-bracket-link-regexp limit t)
  4874. (let* ((help (concat "LINK: "
  4875. (org-match-string-no-properties 1)))
  4876. ;; FIXME: above we should remove the escapes.
  4877. ;; but that requires another match, protecting match data,
  4878. ;; a lot of overhead for font-lock.
  4879. (ip (org-maybe-intangible
  4880. (list 'invisible 'org-link
  4881. 'keymap org-mouse-map 'mouse-face 'highlight
  4882. 'font-lock-multiline t 'help-echo help)))
  4883. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4884. 'font-lock-multiline t 'help-echo help)))
  4885. ;; We need to remove the invisible property here. Table narrowing
  4886. ;; may have made some of this invisible.
  4887. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4888. (remove-text-properties (match-beginning 0) (match-end 0)
  4889. '(invisible nil))
  4890. (if (match-end 3)
  4891. (progn
  4892. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4893. (org-rear-nonsticky-at (match-beginning 3))
  4894. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4895. (org-rear-nonsticky-at (match-end 3))
  4896. (add-text-properties (match-end 3) (match-end 0) ip)
  4897. (org-rear-nonsticky-at (match-end 0)))
  4898. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4899. (org-rear-nonsticky-at (match-beginning 1))
  4900. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4901. (org-rear-nonsticky-at (match-end 1))
  4902. (add-text-properties (match-end 1) (match-end 0) ip)
  4903. (org-rear-nonsticky-at (match-end 0)))
  4904. t)))
  4905. (defun org-activate-dates (limit)
  4906. "Run through the buffer and add overlays to dates."
  4907. (if (re-search-forward org-tsr-regexp-both limit t)
  4908. (progn
  4909. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4910. (add-text-properties (match-beginning 0) (match-end 0)
  4911. (list 'mouse-face 'highlight
  4912. 'keymap org-mouse-map))
  4913. (org-rear-nonsticky-at (match-end 0))
  4914. (when org-display-custom-times
  4915. (if (match-end 3)
  4916. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4917. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4918. t)))
  4919. (defvar org-target-link-regexp nil
  4920. "Regular expression matching radio targets in plain text.")
  4921. (make-variable-buffer-local 'org-target-link-regexp)
  4922. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4923. "Regular expression matching a link target.")
  4924. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4925. "Regular expression matching a radio target.")
  4926. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4927. "Regular expression matching any target.")
  4928. (defun org-activate-target-links (limit)
  4929. "Run through the buffer and add overlays to target matches."
  4930. (when org-target-link-regexp
  4931. (let ((case-fold-search t))
  4932. (if (re-search-forward org-target-link-regexp limit t)
  4933. (progn
  4934. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4935. (add-text-properties (match-beginning 0) (match-end 0)
  4936. (list 'mouse-face 'highlight
  4937. 'keymap org-mouse-map
  4938. 'help-echo "Radio target link"
  4939. 'org-linked-text t))
  4940. (org-rear-nonsticky-at (match-end 0))
  4941. t)))))
  4942. (defun org-update-radio-target-regexp ()
  4943. "Find all radio targets in this file and update the regular expression."
  4944. (interactive)
  4945. (when (memq 'radio org-activate-links)
  4946. (setq org-target-link-regexp
  4947. (org-make-target-link-regexp (org-all-targets 'radio)))
  4948. (org-restart-font-lock)))
  4949. (defun org-hide-wide-columns (limit)
  4950. (let (s e)
  4951. (setq s (text-property-any (point) (or limit (point-max))
  4952. 'org-cwidth t))
  4953. (when s
  4954. (setq e (next-single-property-change s 'org-cwidth))
  4955. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4956. (goto-char e)
  4957. t)))
  4958. (defvar org-latex-and-specials-regexp nil
  4959. "Regular expression for highlighting export special stuff.")
  4960. (defvar org-match-substring-regexp)
  4961. (defvar org-match-substring-with-braces-regexp)
  4962. ;; This should be with the exporter code, but we also use if for font-locking
  4963. (defconst org-export-html-special-string-regexps
  4964. '(("\\\\-" . "&shy;")
  4965. ("---\\([^-]\\)" . "&mdash;\\1")
  4966. ("--\\([^-]\\)" . "&ndash;\\1")
  4967. ("\\.\\.\\." . "&hellip;"))
  4968. "Regular expressions for special string conversion.")
  4969. (defun org-compute-latex-and-specials-regexp ()
  4970. "Compute regular expression for stuff treated specially by exporters."
  4971. (if (not org-highlight-latex-fragments-and-specials)
  4972. (org-set-local 'org-latex-and-specials-regexp nil)
  4973. (require 'org-exp)
  4974. (let*
  4975. ((matchers (plist-get org-format-latex-options :matchers))
  4976. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4977. org-latex-regexps)))
  4978. (org-export-allow-BIND nil)
  4979. (options (org-combine-plists (org-default-export-plist)
  4980. (org-infile-export-plist)))
  4981. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4982. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4983. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4984. (org-export-html-expand (plist-get options :expand-quoted-html))
  4985. (org-export-with-special-strings (plist-get options :special-strings))
  4986. (re-sub
  4987. (cond
  4988. ((equal org-export-with-sub-superscripts '{})
  4989. (list org-match-substring-with-braces-regexp))
  4990. (org-export-with-sub-superscripts
  4991. (list org-match-substring-regexp))
  4992. (t nil)))
  4993. (re-latex
  4994. (if org-export-with-LaTeX-fragments
  4995. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4996. (re-macros
  4997. (if org-export-with-TeX-macros
  4998. (list (concat "\\\\"
  4999. (regexp-opt
  5000. (append
  5001. (delq nil
  5002. (mapcar 'car-safe
  5003. (append org-entities-user
  5004. org-entities)))
  5005. (if (boundp 'org-latex-entities)
  5006. (mapcar (lambda (x)
  5007. (or (car-safe x) x))
  5008. org-latex-entities)
  5009. nil))
  5010. 'words))) ; FIXME
  5011. ))
  5012. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5013. (re-special (if org-export-with-special-strings
  5014. (mapcar (lambda (x) (car x))
  5015. org-export-html-special-string-regexps)))
  5016. (re-rest
  5017. (delq nil
  5018. (list
  5019. (if org-export-html-expand "@<[^>\n]+>")
  5020. ))))
  5021. (org-set-local
  5022. 'org-latex-and-specials-regexp
  5023. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5024. re-rest) "\\|")))))
  5025. (defun org-do-latex-and-special-faces (limit)
  5026. "Run through the buffer and add overlays to links."
  5027. (when org-latex-and-specials-regexp
  5028. (let (rtn d)
  5029. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5030. limit t))
  5031. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5032. 'face))
  5033. '(org-code org-verbatim underline)))
  5034. (progn
  5035. (setq rtn t
  5036. d (cond ((member (char-after (1+ (match-beginning 0)))
  5037. '(?_ ?^)) 1)
  5038. (t 0)))
  5039. (font-lock-prepend-text-property
  5040. (+ d (match-beginning 0)) (match-end 0)
  5041. 'face 'org-latex-and-export-specials)
  5042. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5043. '(font-lock-multiline t)))))
  5044. rtn)))
  5045. (defun org-restart-font-lock ()
  5046. "Restart `font-lock-mode', to force refontification."
  5047. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5048. (font-lock-mode -1)
  5049. (font-lock-mode 1)))
  5050. (defun org-all-targets (&optional radio)
  5051. "Return a list of all targets in this file.
  5052. With optional argument RADIO, only find radio targets."
  5053. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5054. rtn)
  5055. (save-excursion
  5056. (goto-char (point-min))
  5057. (while (re-search-forward re nil t)
  5058. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5059. rtn)))
  5060. (defun org-make-target-link-regexp (targets)
  5061. "Make regular expression matching all strings in TARGETS.
  5062. The regular expression finds the targets also if there is a line break
  5063. between words."
  5064. (and targets
  5065. (concat
  5066. "\\<\\("
  5067. (mapconcat
  5068. (lambda (x)
  5069. (setq x (regexp-quote x))
  5070. (while (string-match " +" x)
  5071. (setq x (replace-match "\\s-+" t t x)))
  5072. x)
  5073. targets
  5074. "\\|")
  5075. "\\)\\>")))
  5076. (defun org-activate-tags (limit)
  5077. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5078. (progn
  5079. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5080. (add-text-properties (match-beginning 1) (match-end 1)
  5081. (list 'mouse-face 'highlight
  5082. 'keymap org-mouse-map))
  5083. (org-rear-nonsticky-at (match-end 1))
  5084. t)))
  5085. (defun org-outline-level ()
  5086. "Compute the outline level of the heading at point.
  5087. This function assumes that the cursor is at the beginning of a line matched
  5088. by `outline-regexp'. Otherwise it returns garbage.
  5089. If this is called at a normal headline, the level is the number of stars.
  5090. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5091. (save-excursion
  5092. (looking-at org-outline-regexp)
  5093. (1- (- (match-end 0) (match-beginning 0)))))
  5094. (defvar org-font-lock-keywords nil)
  5095. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5096. "Regular expression matching a property line.")
  5097. (defvar org-font-lock-hook nil
  5098. "Functions to be called for special font lock stuff.")
  5099. (defvar org-font-lock-set-keywords-hook nil
  5100. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5101. This is called after `org-font-lock-extra-keywords' is defined, but before
  5102. it is installed to be used by font lock. This can be useful if something
  5103. needs to be inserted at a specific position in the font-lock sequence.")
  5104. (defun org-font-lock-hook (limit)
  5105. (run-hook-with-args 'org-font-lock-hook limit))
  5106. (defun org-set-font-lock-defaults ()
  5107. (let* ((em org-fontify-emphasized-text)
  5108. (lk org-activate-links)
  5109. (org-font-lock-extra-keywords
  5110. (list
  5111. ;; Call the hook
  5112. '(org-font-lock-hook)
  5113. ;; Headlines
  5114. `(,(if org-fontify-whole-heading-line
  5115. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5116. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5117. (1 (org-get-level-face 1))
  5118. (2 (org-get-level-face 2))
  5119. (3 (org-get-level-face 3)))
  5120. ;; Table lines
  5121. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5122. (1 'org-table t))
  5123. ;; Table internals
  5124. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5125. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5126. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5127. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5128. ;; Drawers
  5129. (list org-drawer-regexp '(0 'org-special-keyword t))
  5130. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5131. ;; Properties
  5132. (list org-property-re
  5133. '(1 'org-special-keyword t)
  5134. '(3 'org-property-value t))
  5135. ;; Links
  5136. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5137. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5138. (if (memq 'plain lk) '(org-activate-plain-links))
  5139. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5140. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5141. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5142. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5143. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5144. '(org-hide-wide-columns (0 nil append))
  5145. ;; TODO keyword
  5146. (list (format org-heading-keyword-regexp-format
  5147. org-todo-regexp)
  5148. '(2 (org-get-todo-face 2) t))
  5149. ;; DONE
  5150. (if org-fontify-done-headline
  5151. (list (format org-heading-keyword-regexp-format
  5152. (concat
  5153. "\\(?:"
  5154. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5155. "\\)"))
  5156. '(2 'org-headline-done t))
  5157. nil)
  5158. ;; Priorities
  5159. '(org-font-lock-add-priority-faces)
  5160. ;; Tags
  5161. '(org-font-lock-add-tag-faces)
  5162. ;; Special keywords
  5163. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5164. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5165. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5166. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5167. ;; Emphasis
  5168. (if em
  5169. (if (featurep 'xemacs)
  5170. '(org-do-emphasis-faces (0 nil append))
  5171. '(org-do-emphasis-faces)))
  5172. ;; Checkboxes
  5173. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5174. 1 'org-checkbox prepend)
  5175. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5176. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5177. (0 (org-get-checkbox-statistics-face) t)))
  5178. ;; Description list items
  5179. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5180. 1 'bold prepend)
  5181. ;; ARCHIVEd headings
  5182. (list (concat
  5183. org-outline-regexp-bol
  5184. "\\(.*:" org-archive-tag ":.*\\)")
  5185. '(1 'org-archived prepend))
  5186. ;; Specials
  5187. '(org-do-latex-and-special-faces)
  5188. '(org-fontify-entities)
  5189. '(org-raise-scripts)
  5190. ;; Code
  5191. '(org-activate-code (1 'org-code t))
  5192. ;; COMMENT
  5193. (list (format org-heading-keyword-regexp-format
  5194. (concat "\\("
  5195. org-comment-string "\\|" org-quote-string
  5196. "\\)"))
  5197. '(2 'org-special-keyword t))
  5198. '("^#.*" (0 'font-lock-comment-face t))
  5199. ;; Blocks and meta lines
  5200. '(org-fontify-meta-lines-and-blocks)
  5201. )))
  5202. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5203. (run-hooks 'org-font-lock-set-keywords-hook)
  5204. ;; Now set the full font-lock-keywords
  5205. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5206. (org-set-local 'font-lock-defaults
  5207. '(org-font-lock-keywords t nil nil backward-paragraph))
  5208. (kill-local-variable 'font-lock-keywords) nil))
  5209. (defun org-toggle-pretty-entities ()
  5210. "Toggle the composition display of entities as UTF8 characters."
  5211. (interactive)
  5212. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5213. (org-restart-font-lock)
  5214. (if org-pretty-entities
  5215. (message "Entities are displayed as UTF8 characters")
  5216. (save-restriction
  5217. (widen)
  5218. (org-decompose-region (point-min) (point-max))
  5219. (message "Entities are displayed plain"))))
  5220. (defun org-fontify-entities (limit)
  5221. "Find an entity to fontify."
  5222. (let (ee)
  5223. (when org-pretty-entities
  5224. (catch 'match
  5225. (while (re-search-forward
  5226. "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
  5227. limit t)
  5228. (if (and (not (org-in-indented-comment-line))
  5229. (setq ee (org-entity-get (match-string 1)))
  5230. (= (length (nth 6 ee)) 1))
  5231. (progn
  5232. (add-text-properties
  5233. (match-beginning 0) (match-end 1)
  5234. (list 'font-lock-fontified t))
  5235. (compose-region (match-beginning 0) (match-end 1)
  5236. (nth 6 ee) nil)
  5237. (backward-char 1)
  5238. (throw 'match t))))
  5239. nil))))
  5240. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5241. "Fontify string S like in Org-mode."
  5242. (with-temp-buffer
  5243. (insert s)
  5244. (let ((org-odd-levels-only odd-levels))
  5245. (org-mode)
  5246. (font-lock-fontify-buffer)
  5247. (buffer-string))))
  5248. (defvar org-m nil)
  5249. (defvar org-l nil)
  5250. (defvar org-f nil)
  5251. (defun org-get-level-face (n)
  5252. "Get the right face for match N in font-lock matching of headlines."
  5253. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5254. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5255. (if org-cycle-level-faces
  5256. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5257. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5258. (cond
  5259. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5260. ((eq n 2) org-f)
  5261. (t (if org-level-color-stars-only nil org-f))))
  5262. (defun org-get-todo-face (kwd)
  5263. "Get the right face for a TODO keyword KWD.
  5264. If KWD is a number, get the corresponding match group."
  5265. (if (numberp kwd) (setq kwd (match-string kwd)))
  5266. (or (org-face-from-face-or-color
  5267. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5268. (and (member kwd org-done-keywords) 'org-done)
  5269. 'org-todo))
  5270. (defun org-face-from-face-or-color (context inherit face-or-color)
  5271. "Create a face list that inherits INHERIT, but sets the foreground color.
  5272. When FACE-OR-COLOR is not a string, just return it."
  5273. (if (stringp face-or-color)
  5274. (list :inherit inherit
  5275. (cdr (assoc context org-faces-easy-properties))
  5276. face-or-color)
  5277. face-or-color))
  5278. (defun org-font-lock-add-tag-faces (limit)
  5279. "Add the special tag faces."
  5280. (when (and org-tag-faces org-tags-special-faces-re)
  5281. (while (re-search-forward org-tags-special-faces-re limit t)
  5282. (add-text-properties (match-beginning 1) (match-end 1)
  5283. (list 'face (org-get-tag-face 1)
  5284. 'font-lock-fontified t))
  5285. (backward-char 1))))
  5286. (defun org-font-lock-add-priority-faces (limit)
  5287. "Add the special priority faces."
  5288. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5289. (add-text-properties
  5290. (match-beginning 0) (match-end 0)
  5291. (list 'face (or (org-face-from-face-or-color
  5292. 'priority 'org-special-keyword
  5293. (cdr (assoc (char-after (match-beginning 1))
  5294. org-priority-faces)))
  5295. 'org-special-keyword)
  5296. 'font-lock-fontified t))))
  5297. (defun org-get-tag-face (kwd)
  5298. "Get the right face for a TODO keyword KWD.
  5299. If KWD is a number, get the corresponding match group."
  5300. (if (numberp kwd) (setq kwd (match-string kwd)))
  5301. (or (org-face-from-face-or-color
  5302. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5303. 'org-tag))
  5304. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5305. "Remove fontification and activation overlays from links."
  5306. (font-lock-default-unfontify-region beg end)
  5307. (let* ((buffer-undo-list t)
  5308. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5309. (inhibit-modification-hooks t)
  5310. deactivate-mark buffer-file-name buffer-file-truename)
  5311. (org-decompose-region beg end)
  5312. (remove-text-properties beg end
  5313. '(mouse-face t keymap t org-linked-text t
  5314. invisible t intangible t
  5315. org-no-flyspell t org-emphasis t))
  5316. (org-remove-font-lock-display-properties beg end)))
  5317. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5318. ((raise 0.3) (height 0.7))
  5319. ((raise -0.5))
  5320. ((raise 0.5)))
  5321. "Display properties for showing superscripts and subscripts.")
  5322. (defun org-remove-font-lock-display-properties (beg end)
  5323. "Remove specific display properties that have been added by font lock.
  5324. The will remove the raise properties that are used to show superscripts
  5325. and subscripts."
  5326. (let (next prop)
  5327. (while (< beg end)
  5328. (setq next (next-single-property-change beg 'display nil end)
  5329. prop (get-text-property beg 'display))
  5330. (if (member prop org-script-display)
  5331. (put-text-property beg next 'display nil))
  5332. (setq beg next))))
  5333. (defun org-raise-scripts (limit)
  5334. "Add raise properties to sub/superscripts."
  5335. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5336. (if (re-search-forward
  5337. (if (eq org-use-sub-superscripts t)
  5338. org-match-substring-regexp
  5339. org-match-substring-with-braces-regexp)
  5340. limit t)
  5341. (let* ((pos (point)) table-p comment-p
  5342. (mpos (match-beginning 3))
  5343. (emph-p (get-text-property mpos 'org-emphasis))
  5344. (link-p (get-text-property mpos 'mouse-face))
  5345. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5346. (goto-char (point-at-bol))
  5347. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5348. comment-p (org-looking-at-p "[ \t]*#"))
  5349. (goto-char pos)
  5350. ;; FIXME: Should we go back one character here, for a_b^c
  5351. ;; (goto-char (1- pos)) ;????????????????????
  5352. (if (or comment-p emph-p link-p keyw-p)
  5353. t
  5354. (put-text-property (match-beginning 3) (match-end 0)
  5355. 'display
  5356. (if (equal (char-after (match-beginning 2)) ?^)
  5357. (nth (if table-p 3 1) org-script-display)
  5358. (nth (if table-p 2 0) org-script-display)))
  5359. (add-text-properties (match-beginning 2) (match-end 2)
  5360. (list 'invisible t
  5361. 'org-dwidth t 'org-dwidth-n 1))
  5362. (if (and (eq (char-after (match-beginning 3)) ?{)
  5363. (eq (char-before (match-end 3)) ?}))
  5364. (progn
  5365. (add-text-properties
  5366. (match-beginning 3) (1+ (match-beginning 3))
  5367. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5368. (add-text-properties
  5369. (1- (match-end 3)) (match-end 3)
  5370. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5371. t)))))
  5372. ;;;; Visibility cycling, including org-goto and indirect buffer
  5373. ;;; Cycling
  5374. (defvar org-cycle-global-status nil)
  5375. (make-variable-buffer-local 'org-cycle-global-status)
  5376. (defvar org-cycle-subtree-status nil)
  5377. (make-variable-buffer-local 'org-cycle-subtree-status)
  5378. ;;;###autoload
  5379. (defvar org-inlinetask-min-level)
  5380. (defun org-cycle (&optional arg)
  5381. "TAB-action and visibility cycling for Org-mode.
  5382. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5383. is outline visibility cycling, but it also invokes other actions
  5384. in special contexts.
  5385. - When this function is called with a prefix argument, rotate the entire
  5386. buffer through 3 states (global cycling)
  5387. 1. OVERVIEW: Show only top-level headlines.
  5388. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5389. 3. SHOW ALL: Show everything.
  5390. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5391. determined by the variable `org-startup-folded', and by any VISIBILITY
  5392. properties in the buffer.
  5393. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5394. including any drawers.
  5395. - When inside a table, re-align the table and move to the next field.
  5396. - When point is at the beginning of a headline, rotate the subtree started
  5397. by this line through 3 different states (local cycling)
  5398. 1. FOLDED: Only the main headline is shown.
  5399. 2. CHILDREN: The main headline and the direct children are shown.
  5400. From this state, you can move to one of the children
  5401. and zoom in further.
  5402. 3. SUBTREE: Show the entire subtree, including body text.
  5403. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5404. - When point is at the beginning of an empty headline and the variable
  5405. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5406. of the headline by demoting and promoting it to likely levels. This
  5407. speeds up creation document structure by pressing TAB once or several
  5408. times right after creating a new headline.
  5409. - When there is a numeric prefix, go up to a heading with level ARG, do
  5410. a `show-subtree' and return to the previous cursor position. If ARG
  5411. is negative, go up that many levels.
  5412. - When point is not at the beginning of a headline, execute the global
  5413. binding for TAB, which is re-indenting the line. See the option
  5414. `org-cycle-emulate-tab' for details.
  5415. - Special case: if point is at the beginning of the buffer and there is
  5416. no headline in line 1, this function will act as if called with prefix arg
  5417. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5418. But only if also the variable `org-cycle-global-at-bob' is t."
  5419. (interactive "P")
  5420. (org-load-modules-maybe)
  5421. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5422. (and org-cycle-level-after-item/entry-creation
  5423. (or (org-cycle-level)
  5424. (org-cycle-item-indentation))))
  5425. (let* ((limit-level
  5426. (or org-cycle-max-level
  5427. (and (boundp 'org-inlinetask-min-level)
  5428. org-inlinetask-min-level
  5429. (1- org-inlinetask-min-level))))
  5430. (nstars (and limit-level
  5431. (if org-odd-levels-only
  5432. (and limit-level (1- (* limit-level 2)))
  5433. limit-level)))
  5434. (org-outline-regexp
  5435. (if (not (eq major-mode 'org-mode))
  5436. outline-regexp
  5437. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5438. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5439. (not (looking-at org-outline-regexp))))
  5440. (org-cycle-hook
  5441. (if bob-special
  5442. (delq 'org-optimize-window-after-visibility-change
  5443. (copy-sequence org-cycle-hook))
  5444. org-cycle-hook))
  5445. (pos (point)))
  5446. (if (or bob-special (equal arg '(4)))
  5447. ;; special case: use global cycling
  5448. (setq arg t))
  5449. (cond
  5450. ((equal arg '(16))
  5451. (setq last-command 'dummy)
  5452. (org-set-startup-visibility)
  5453. (message "Startup visibility, plus VISIBILITY properties"))
  5454. ((equal arg '(64))
  5455. (show-all)
  5456. (message "Entire buffer visible, including drawers"))
  5457. ;; Table: enter it or move to the next field.
  5458. ((org-at-table-p 'any)
  5459. (if (org-at-table.el-p)
  5460. (message "Use C-c ' to edit table.el tables")
  5461. (if arg (org-table-edit-field t)
  5462. (org-table-justify-field-maybe)
  5463. (call-interactively 'org-table-next-field))))
  5464. ((run-hook-with-args-until-success
  5465. 'org-tab-after-check-for-table-hook))
  5466. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5467. ((eq arg t) (org-cycle-internal-global))
  5468. ;; Drawers: delegate to `org-flag-drawer'.
  5469. ((and org-drawers org-drawer-regexp
  5470. (save-excursion
  5471. (beginning-of-line 1)
  5472. (looking-at org-drawer-regexp)))
  5473. (org-flag-drawer ; toggle block visibility
  5474. (not (get-char-property (match-end 0) 'invisible))))
  5475. ;; Show-subtree, ARG levels up from here.
  5476. ((integerp arg)
  5477. (save-excursion
  5478. (org-back-to-heading)
  5479. (outline-up-heading (if (< arg 0) (- arg)
  5480. (- (funcall outline-level) arg)))
  5481. (org-show-subtree)))
  5482. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5483. ((and (featurep 'org-inlinetask)
  5484. (org-inlinetask-at-task-p)
  5485. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5486. (org-inlinetask-toggle-visibility))
  5487. ((org-try-cdlatex-tab))
  5488. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5489. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5490. (save-excursion (beginning-of-line 1)
  5491. (looking-at org-outline-regexp)))
  5492. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5493. (org-cycle-internal-local))
  5494. ;; From there: TAB emulation and template completion.
  5495. (buffer-read-only (org-back-to-heading))
  5496. ((run-hook-with-args-until-success
  5497. 'org-tab-after-check-for-cycling-hook))
  5498. ((org-try-structure-completion))
  5499. ((run-hook-with-args-until-success
  5500. 'org-tab-before-tab-emulation-hook))
  5501. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5502. (or (not (bolp))
  5503. (not (looking-at org-outline-regexp))))
  5504. (call-interactively (global-key-binding "\t")))
  5505. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5506. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5507. (or (and (eq org-cycle-emulate-tab 'white)
  5508. (= (match-end 0) (point-at-eol)))
  5509. (and (eq org-cycle-emulate-tab 'whitestart)
  5510. (>= (match-end 0) pos))))
  5511. t
  5512. (eq org-cycle-emulate-tab t))
  5513. (call-interactively (global-key-binding "\t")))
  5514. (t (save-excursion
  5515. (org-back-to-heading)
  5516. (org-cycle)))))))
  5517. (defun org-cycle-internal-global ()
  5518. "Do the global cycling action."
  5519. (cond
  5520. ((and (eq last-command this-command)
  5521. (eq org-cycle-global-status 'overview))
  5522. ;; We just created the overview - now do table of contents
  5523. ;; This can be slow in very large buffers, so indicate action
  5524. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5525. (message "CONTENTS...")
  5526. (org-content)
  5527. (message "CONTENTS...done")
  5528. (setq org-cycle-global-status 'contents)
  5529. (run-hook-with-args 'org-cycle-hook 'contents))
  5530. ((and (eq last-command this-command)
  5531. (eq org-cycle-global-status 'contents))
  5532. ;; We just showed the table of contents - now show everything
  5533. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5534. (show-all)
  5535. (message "SHOW ALL")
  5536. (setq org-cycle-global-status 'all)
  5537. (run-hook-with-args 'org-cycle-hook 'all))
  5538. (t
  5539. ;; Default action: go to overview
  5540. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5541. (org-overview)
  5542. (message "OVERVIEW")
  5543. (setq org-cycle-global-status 'overview)
  5544. (run-hook-with-args 'org-cycle-hook 'overview))))
  5545. (defun org-cycle-internal-local ()
  5546. "Do the local cycling action."
  5547. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5548. ;; First, determine end of headline (EOH), end of subtree or item
  5549. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5550. (save-excursion
  5551. (if (org-at-item-p)
  5552. (progn
  5553. (beginning-of-line)
  5554. (setq struct (org-list-struct))
  5555. (setq eoh (point-at-eol))
  5556. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5557. (setq has-children (org-list-has-child-p (point) struct)))
  5558. (org-back-to-heading)
  5559. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5560. (setq eos (save-excursion
  5561. (org-end-of-subtree t)
  5562. (unless (eobp)
  5563. (skip-chars-forward " \t\n"))
  5564. (if (eobp) (point) (1- (point)))))
  5565. (setq has-children
  5566. (or (save-excursion
  5567. (let ((level (funcall outline-level)))
  5568. (outline-next-heading)
  5569. (and (org-at-heading-p t)
  5570. (> (funcall outline-level) level))))
  5571. (save-excursion
  5572. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5573. ;; Determine end invisible part of buffer (EOL)
  5574. (beginning-of-line 2)
  5575. ;; XEmacs doesn't have `next-single-char-property-change'
  5576. (if (featurep 'xemacs)
  5577. (while (and (not (eobp)) ;; this is like `next-line'
  5578. (get-char-property (1- (point)) 'invisible))
  5579. (beginning-of-line 2))
  5580. (while (and (not (eobp)) ;; this is like `next-line'
  5581. (get-char-property (1- (point)) 'invisible))
  5582. (goto-char (next-single-char-property-change (point) 'invisible))
  5583. (and (eolp) (beginning-of-line 2))))
  5584. (setq eol (point)))
  5585. ;; Find out what to do next and set `this-command'
  5586. (cond
  5587. ((= eos eoh)
  5588. ;; Nothing is hidden behind this heading
  5589. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5590. (message "EMPTY ENTRY")
  5591. (setq org-cycle-subtree-status nil)
  5592. (save-excursion
  5593. (goto-char eos)
  5594. (outline-next-heading)
  5595. (if (outline-invisible-p) (org-flag-heading nil))))
  5596. ((and (or (>= eol eos)
  5597. (not (string-match "\\S-" (buffer-substring eol eos))))
  5598. (or has-children
  5599. (not (setq children-skipped
  5600. org-cycle-skip-children-state-if-no-children))))
  5601. ;; Entire subtree is hidden in one line: children view
  5602. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5603. (if (org-at-item-p)
  5604. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5605. (org-show-entry)
  5606. (show-children)
  5607. ;; Fold every list in subtree to top-level items.
  5608. (when (eq org-cycle-include-plain-lists 'integrate)
  5609. (save-excursion
  5610. (org-back-to-heading)
  5611. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5612. (beginning-of-line 1)
  5613. (let* ((struct (org-list-struct))
  5614. (prevs (org-list-prevs-alist struct))
  5615. (end (org-list-get-bottom-point struct)))
  5616. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5617. (org-list-get-all-items (point) struct prevs))
  5618. (goto-char end))))))
  5619. (message "CHILDREN")
  5620. (save-excursion
  5621. (goto-char eos)
  5622. (outline-next-heading)
  5623. (if (outline-invisible-p) (org-flag-heading nil)))
  5624. (setq org-cycle-subtree-status 'children)
  5625. (run-hook-with-args 'org-cycle-hook 'children))
  5626. ((or children-skipped
  5627. (and (eq last-command this-command)
  5628. (eq org-cycle-subtree-status 'children)))
  5629. ;; We just showed the children, or no children are there,
  5630. ;; now show everything.
  5631. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5632. (outline-flag-region eoh eos nil)
  5633. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5634. (setq org-cycle-subtree-status 'subtree)
  5635. (run-hook-with-args 'org-cycle-hook 'subtree))
  5636. (t
  5637. ;; Default action: hide the subtree.
  5638. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5639. (outline-flag-region eoh eos t)
  5640. (message "FOLDED")
  5641. (setq org-cycle-subtree-status 'folded)
  5642. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5643. ;;;###autoload
  5644. (defun org-global-cycle (&optional arg)
  5645. "Cycle the global visibility. For details see `org-cycle'.
  5646. With \\[universal-argument] prefix arg, switch to startup visibility.
  5647. With a numeric prefix, show all headlines up to that level."
  5648. (interactive "P")
  5649. (let ((org-cycle-include-plain-lists
  5650. (if (eq major-mode 'org-mode) org-cycle-include-plain-lists nil)))
  5651. (cond
  5652. ((integerp arg)
  5653. (show-all)
  5654. (hide-sublevels arg)
  5655. (setq org-cycle-global-status 'contents))
  5656. ((equal arg '(4))
  5657. (org-set-startup-visibility)
  5658. (message "Startup visibility, plus VISIBILITY properties."))
  5659. (t
  5660. (org-cycle '(4))))))
  5661. (defun org-set-startup-visibility ()
  5662. "Set the visibility required by startup options and properties."
  5663. (cond
  5664. ((eq org-startup-folded t)
  5665. (org-cycle '(4)))
  5666. ((eq org-startup-folded 'content)
  5667. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5668. (org-cycle '(4)) (org-cycle '(4)))))
  5669. (unless (eq org-startup-folded 'showeverything)
  5670. (if org-hide-block-startup (org-hide-block-all))
  5671. (org-set-visibility-according-to-property 'no-cleanup)
  5672. (org-cycle-hide-archived-subtrees 'all)
  5673. (org-cycle-hide-drawers 'all)
  5674. (org-cycle-show-empty-lines t)))
  5675. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5676. "Switch subtree visibilities according to :VISIBILITY: property."
  5677. (interactive)
  5678. (let (org-show-entry-below state)
  5679. (save-excursion
  5680. (goto-char (point-min))
  5681. (while (re-search-forward
  5682. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5683. nil t)
  5684. (setq state (match-string 1))
  5685. (save-excursion
  5686. (org-back-to-heading t)
  5687. (hide-subtree)
  5688. (org-reveal)
  5689. (cond
  5690. ((equal state '("fold" "folded"))
  5691. (hide-subtree))
  5692. ((equal state "children")
  5693. (org-show-hidden-entry)
  5694. (show-children))
  5695. ((equal state "content")
  5696. (save-excursion
  5697. (save-restriction
  5698. (org-narrow-to-subtree)
  5699. (org-content))))
  5700. ((member state '("all" "showall"))
  5701. (show-subtree)))))
  5702. (unless no-cleanup
  5703. (org-cycle-hide-archived-subtrees 'all)
  5704. (org-cycle-hide-drawers 'all)
  5705. (org-cycle-show-empty-lines 'all)))))
  5706. ;; This function uses outline-regexp instead of the more fundamental
  5707. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5708. ;; buffers, where outline-regexp is needed.
  5709. (defun org-overview ()
  5710. "Switch to overview mode, showing only top-level headlines.
  5711. Really, this shows all headlines with level equal or greater than the level
  5712. of the first headline in the buffer. This is important, because if the
  5713. first headline is not level one, then (hide-sublevels 1) gives confusing
  5714. results."
  5715. (interactive)
  5716. (let ((level (save-excursion
  5717. (goto-char (point-min))
  5718. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5719. (progn
  5720. (goto-char (match-beginning 0))
  5721. (funcall outline-level))))))
  5722. (and level (hide-sublevels level))))
  5723. (defun org-content (&optional arg)
  5724. "Show all headlines in the buffer, like a table of contents.
  5725. With numerical argument N, show content up to level N."
  5726. (interactive "P")
  5727. (save-excursion
  5728. ;; Visit all headings and show their offspring
  5729. (and (integerp arg) (org-overview))
  5730. (goto-char (point-max))
  5731. (catch 'exit
  5732. (while (and (progn (condition-case nil
  5733. (outline-previous-visible-heading 1)
  5734. (error (goto-char (point-min))))
  5735. t)
  5736. (looking-at org-outline-regexp))
  5737. (if (integerp arg)
  5738. (show-children (1- arg))
  5739. (show-branches))
  5740. (if (bobp) (throw 'exit nil))))))
  5741. (defun org-optimize-window-after-visibility-change (state)
  5742. "Adjust the window after a change in outline visibility.
  5743. This function is the default value of the hook `org-cycle-hook'."
  5744. (when (get-buffer-window (current-buffer))
  5745. (cond
  5746. ((eq state 'content) nil)
  5747. ((eq state 'all) nil)
  5748. ((eq state 'folded) nil)
  5749. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5750. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5751. (defun org-remove-empty-overlays-at (pos)
  5752. "Remove outline overlays that do not contain non-white stuff."
  5753. (mapc
  5754. (lambda (o)
  5755. (and (eq 'outline (overlay-get o 'invisible))
  5756. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5757. (overlay-end o))))
  5758. (delete-overlay o)))
  5759. (overlays-at pos)))
  5760. (defun org-clean-visibility-after-subtree-move ()
  5761. "Fix visibility issues after moving a subtree."
  5762. ;; First, find a reasonable region to look at:
  5763. ;; Start two siblings above, end three below
  5764. (let* ((beg (save-excursion
  5765. (and (org-get-last-sibling)
  5766. (org-get-last-sibling))
  5767. (point)))
  5768. (end (save-excursion
  5769. (and (org-get-next-sibling)
  5770. (org-get-next-sibling)
  5771. (org-get-next-sibling))
  5772. (if (org-at-heading-p)
  5773. (point-at-eol)
  5774. (point))))
  5775. (level (looking-at "\\*+"))
  5776. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5777. (save-excursion
  5778. (save-restriction
  5779. (narrow-to-region beg end)
  5780. (when re
  5781. ;; Properly fold already folded siblings
  5782. (goto-char (point-min))
  5783. (while (re-search-forward re nil t)
  5784. (if (and (not (outline-invisible-p))
  5785. (save-excursion
  5786. (goto-char (point-at-eol)) (outline-invisible-p)))
  5787. (hide-entry))))
  5788. (org-cycle-show-empty-lines 'overview)
  5789. (org-cycle-hide-drawers 'overview)))))
  5790. (defun org-cycle-show-empty-lines (state)
  5791. "Show empty lines above all visible headlines.
  5792. The region to be covered depends on STATE when called through
  5793. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5794. entire buffer covered. Note that an empty line is only shown if there
  5795. are at least `org-cycle-separator-lines' empty lines before the headline."
  5796. (when (not (= org-cycle-separator-lines 0))
  5797. (save-excursion
  5798. (let* ((n (abs org-cycle-separator-lines))
  5799. (re (cond
  5800. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5801. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5802. (t (let ((ns (number-to-string (- n 2))))
  5803. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5804. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5805. beg end b e)
  5806. (cond
  5807. ((memq state '(overview contents t))
  5808. (setq beg (point-min) end (point-max)))
  5809. ((memq state '(children folded))
  5810. (setq beg (point) end (progn (org-end-of-subtree t t)
  5811. (beginning-of-line 2)
  5812. (point)))))
  5813. (when beg
  5814. (goto-char beg)
  5815. (while (re-search-forward re end t)
  5816. (unless (get-char-property (match-end 1) 'invisible)
  5817. (setq e (match-end 1))
  5818. (if (< org-cycle-separator-lines 0)
  5819. (setq b (save-excursion
  5820. (goto-char (match-beginning 0))
  5821. (org-back-over-empty-lines)
  5822. (if (save-excursion
  5823. (goto-char (max (point-min) (1- (point))))
  5824. (org-at-heading-p))
  5825. (1- (point))
  5826. (point))))
  5827. (setq b (match-beginning 1)))
  5828. (outline-flag-region b e nil)))))))
  5829. ;; Never hide empty lines at the end of the file.
  5830. (save-excursion
  5831. (goto-char (point-max))
  5832. (outline-previous-heading)
  5833. (outline-end-of-heading)
  5834. (if (and (looking-at "[ \t\n]+")
  5835. (= (match-end 0) (point-max)))
  5836. (outline-flag-region (point) (match-end 0) nil))))
  5837. (defun org-show-empty-lines-in-parent ()
  5838. "Move to the parent and re-show empty lines before visible headlines."
  5839. (save-excursion
  5840. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5841. (org-cycle-show-empty-lines context))))
  5842. (defun org-files-list ()
  5843. "Return `org-agenda-files' list, plus all open org-mode files.
  5844. This is useful for operations that need to scan all of a user's
  5845. open and agenda-wise Org files."
  5846. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5847. (dolist (buf (buffer-list))
  5848. (with-current-buffer buf
  5849. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5850. (let ((file (expand-file-name (buffer-file-name))))
  5851. (unless (member file files)
  5852. (push file files))))))
  5853. files))
  5854. (defsubst org-entry-beginning-position ()
  5855. "Return the beginning position of the current entry."
  5856. (save-excursion (outline-back-to-heading t) (point)))
  5857. (defsubst org-entry-end-position ()
  5858. "Return the end position of the current entry."
  5859. (save-excursion (outline-next-heading) (point)))
  5860. (defun org-cycle-hide-drawers (state)
  5861. "Re-hide all drawers after a visibility state change."
  5862. (when (and (eq major-mode 'org-mode)
  5863. (not (memq state '(overview folded contents))))
  5864. (save-excursion
  5865. (let* ((globalp (memq state '(contents all)))
  5866. (beg (if globalp (point-min) (point)))
  5867. (end (if globalp (point-max)
  5868. (if (eq state 'children)
  5869. (save-excursion (outline-next-heading) (point))
  5870. (org-end-of-subtree t)))))
  5871. (goto-char beg)
  5872. (while (re-search-forward org-drawer-regexp end t)
  5873. (org-flag-drawer t))))))
  5874. (defun org-flag-drawer (flag)
  5875. (save-excursion
  5876. (beginning-of-line 1)
  5877. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5878. (let ((b (match-end 0)))
  5879. (if (re-search-forward
  5880. "^[ \t]*:END:"
  5881. (save-excursion (outline-next-heading) (point)) t)
  5882. (outline-flag-region b (point-at-eol) flag)
  5883. (error ":END: line missing at position %s" b))))))
  5884. (defun org-subtree-end-visible-p ()
  5885. "Is the end of the current subtree visible?"
  5886. (pos-visible-in-window-p
  5887. (save-excursion (org-end-of-subtree t) (point))))
  5888. (defun org-first-headline-recenter (&optional N)
  5889. "Move cursor to the first headline and recenter the headline.
  5890. Optional argument N means put the headline into the Nth line of the window."
  5891. (goto-char (point-min))
  5892. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5893. (beginning-of-line)
  5894. (recenter (prefix-numeric-value N))))
  5895. ;;; Saving and restoring visibility
  5896. (defun org-outline-overlay-data (&optional use-markers)
  5897. "Return a list of the locations of all outline overlays.
  5898. These are overlays with the `invisible' property value `outline'.
  5899. The return value is a list of cons cells, with start and stop
  5900. positions for each overlay.
  5901. If USE-MARKERS is set, return the positions as markers."
  5902. (let (beg end)
  5903. (save-excursion
  5904. (save-restriction
  5905. (widen)
  5906. (delq nil
  5907. (mapcar (lambda (o)
  5908. (when (eq (overlay-get o 'invisible) 'outline)
  5909. (setq beg (overlay-start o)
  5910. end (overlay-end o))
  5911. (and beg end (> end beg)
  5912. (if use-markers
  5913. (cons (move-marker (make-marker) beg)
  5914. (move-marker (make-marker) end))
  5915. (cons beg end)))))
  5916. (overlays-in (point-min) (point-max))))))))
  5917. (defun org-set-outline-overlay-data (data)
  5918. "Create visibility overlays for all positions in DATA.
  5919. DATA should have been made by `org-outline-overlay-data'."
  5920. (let (o)
  5921. (save-excursion
  5922. (save-restriction
  5923. (widen)
  5924. (show-all)
  5925. (mapc (lambda (c)
  5926. (outline-flag-region (car c) (cdr c) t))
  5927. data)))))
  5928. ;;; Folding of blocks
  5929. (defvar org-hide-block-overlays nil
  5930. "Overlays hiding blocks.")
  5931. (make-variable-buffer-local 'org-hide-block-overlays)
  5932. (defun org-block-map (function &optional start end)
  5933. "Call FUNCTION at the head of all source blocks in the current buffer.
  5934. Optional arguments START and END can be used to limit the range."
  5935. (let ((start (or start (point-min)))
  5936. (end (or end (point-max))))
  5937. (save-excursion
  5938. (goto-char start)
  5939. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5940. (save-excursion
  5941. (save-match-data
  5942. (goto-char (match-beginning 0))
  5943. (funcall function)))))))
  5944. (defun org-hide-block-toggle-all ()
  5945. "Toggle the visibility of all blocks in the current buffer."
  5946. (org-block-map #'org-hide-block-toggle))
  5947. (defun org-hide-block-all ()
  5948. "Fold all blocks in the current buffer."
  5949. (interactive)
  5950. (org-show-block-all)
  5951. (org-block-map #'org-hide-block-toggle-maybe))
  5952. (defun org-show-block-all ()
  5953. "Unfold all blocks in the current buffer."
  5954. (interactive)
  5955. (mapc 'delete-overlay org-hide-block-overlays)
  5956. (setq org-hide-block-overlays nil))
  5957. (defun org-hide-block-toggle-maybe ()
  5958. "Toggle visibility of block at point."
  5959. (interactive)
  5960. (let ((case-fold-search t))
  5961. (if (save-excursion
  5962. (beginning-of-line 1)
  5963. (looking-at org-block-regexp))
  5964. (progn (org-hide-block-toggle)
  5965. t) ;; to signal that we took action
  5966. nil))) ;; to signal that we did not
  5967. (defun org-hide-block-toggle (&optional force)
  5968. "Toggle the visibility of the current block."
  5969. (interactive)
  5970. (save-excursion
  5971. (beginning-of-line)
  5972. (if (re-search-forward org-block-regexp nil t)
  5973. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5974. (end (match-end 0)) ;; end of entire body
  5975. ov)
  5976. (if (memq t (mapcar (lambda (overlay)
  5977. (eq (overlay-get overlay 'invisible)
  5978. 'org-hide-block))
  5979. (overlays-at start)))
  5980. (if (or (not force) (eq force 'off))
  5981. (mapc (lambda (ov)
  5982. (when (member ov org-hide-block-overlays)
  5983. (setq org-hide-block-overlays
  5984. (delq ov org-hide-block-overlays)))
  5985. (when (eq (overlay-get ov 'invisible)
  5986. 'org-hide-block)
  5987. (delete-overlay ov)))
  5988. (overlays-at start)))
  5989. (setq ov (make-overlay start end))
  5990. (overlay-put ov 'invisible 'org-hide-block)
  5991. ;; make the block accessible to isearch
  5992. (overlay-put
  5993. ov 'isearch-open-invisible
  5994. (lambda (ov)
  5995. (when (member ov org-hide-block-overlays)
  5996. (setq org-hide-block-overlays
  5997. (delq ov org-hide-block-overlays)))
  5998. (when (eq (overlay-get ov 'invisible)
  5999. 'org-hide-block)
  6000. (delete-overlay ov))))
  6001. (push ov org-hide-block-overlays)))
  6002. (error "Not looking at a source block"))))
  6003. ;; org-tab-after-check-for-cycling-hook
  6004. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6005. ;; Remove overlays when changing major mode
  6006. (add-hook 'org-mode-hook
  6007. (lambda () (org-add-hook 'change-major-mode-hook
  6008. 'org-show-block-all 'append 'local)))
  6009. ;;; Org-goto
  6010. (defvar org-goto-window-configuration nil)
  6011. (defvar org-goto-marker nil)
  6012. (defvar org-goto-map
  6013. (let ((map (make-sparse-keymap)))
  6014. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6015. (while (setq cmd (pop cmds))
  6016. (substitute-key-definition cmd cmd map global-map)))
  6017. (suppress-keymap map)
  6018. (org-defkey map "\C-m" 'org-goto-ret)
  6019. (org-defkey map [(return)] 'org-goto-ret)
  6020. (org-defkey map [(left)] 'org-goto-left)
  6021. (org-defkey map [(right)] 'org-goto-right)
  6022. (org-defkey map [(control ?g)] 'org-goto-quit)
  6023. (org-defkey map "\C-i" 'org-cycle)
  6024. (org-defkey map [(tab)] 'org-cycle)
  6025. (org-defkey map [(down)] 'outline-next-visible-heading)
  6026. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6027. (if org-goto-auto-isearch
  6028. (if (fboundp 'define-key-after)
  6029. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6030. nil)
  6031. (org-defkey map "q" 'org-goto-quit)
  6032. (org-defkey map "n" 'outline-next-visible-heading)
  6033. (org-defkey map "p" 'outline-previous-visible-heading)
  6034. (org-defkey map "f" 'outline-forward-same-level)
  6035. (org-defkey map "b" 'outline-backward-same-level)
  6036. (org-defkey map "u" 'outline-up-heading))
  6037. (org-defkey map "/" 'org-occur)
  6038. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6039. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6040. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6041. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6042. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6043. map))
  6044. (defconst org-goto-help
  6045. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6046. RET=jump to location [Q]uit and return to previous location
  6047. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6048. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6049. ;; FIXME: Docstring does not mention both interfaces
  6050. (defun org-goto (&optional alternative-interface)
  6051. "Look up a different location in the current file, keeping current visibility.
  6052. When you want look-up or go to a different location in a document, the
  6053. fastest way is often to fold the entire buffer and then dive into the tree.
  6054. This method has the disadvantage, that the previous location will be folded,
  6055. which may not be what you want.
  6056. This command works around this by showing a copy of the current buffer
  6057. in an indirect buffer, in overview mode. You can dive into the tree in
  6058. that copy, use org-occur and incremental search to find a location.
  6059. When pressing RET or `Q', the command returns to the original buffer in
  6060. which the visibility is still unchanged. After RET is will also jump to
  6061. the location selected in the indirect buffer and expose the headline
  6062. hierarchy above."
  6063. (interactive "P")
  6064. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6065. (org-refile-use-outline-path t)
  6066. (org-refile-target-verify-function nil)
  6067. (interface
  6068. (if (not alternative-interface)
  6069. org-goto-interface
  6070. (if (eq org-goto-interface 'outline)
  6071. 'outline-path-completion
  6072. 'outline)))
  6073. (org-goto-start-pos (point))
  6074. (selected-point
  6075. (if (eq interface 'outline)
  6076. (car (org-get-location (current-buffer) org-goto-help))
  6077. (let ((pa (org-refile-get-location "Goto")))
  6078. (org-refile-check-position pa)
  6079. (nth 3 pa)))))
  6080. (if selected-point
  6081. (progn
  6082. (org-mark-ring-push org-goto-start-pos)
  6083. (goto-char selected-point)
  6084. (if (or (outline-invisible-p) (org-invisible-p2))
  6085. (org-show-context 'org-goto)))
  6086. (message "Quit"))))
  6087. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6088. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6089. (defvar org-goto-local-auto-isearch-map) ; defined below
  6090. (defun org-get-location (buf help)
  6091. "Let the user select a location in the Org-mode buffer BUF.
  6092. This function uses a recursive edit. It returns the selected position
  6093. or nil."
  6094. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6095. (isearch-hide-immediately nil)
  6096. (isearch-search-fun-function
  6097. (lambda () 'org-goto-local-search-headings))
  6098. (org-goto-selected-point org-goto-exit-command)
  6099. (pop-up-frames nil)
  6100. (special-display-buffer-names nil)
  6101. (special-display-regexps nil)
  6102. (special-display-function nil))
  6103. (save-excursion
  6104. (save-window-excursion
  6105. (delete-other-windows)
  6106. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6107. (org-pop-to-buffer-same-window
  6108. (condition-case nil
  6109. (make-indirect-buffer (current-buffer) "*org-goto*")
  6110. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6111. (with-output-to-temp-buffer "*Help*"
  6112. (princ help))
  6113. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6114. (setq buffer-read-only nil)
  6115. (let ((org-startup-truncated t)
  6116. (org-startup-folded nil)
  6117. (org-startup-align-all-tables nil))
  6118. (org-mode)
  6119. (org-overview))
  6120. (setq buffer-read-only t)
  6121. (if (and (boundp 'org-goto-start-pos)
  6122. (integer-or-marker-p org-goto-start-pos))
  6123. (let ((org-show-hierarchy-above t)
  6124. (org-show-siblings t)
  6125. (org-show-following-heading t))
  6126. (goto-char org-goto-start-pos)
  6127. (and (outline-invisible-p) (org-show-context)))
  6128. (goto-char (point-min)))
  6129. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6130. (message "Select location and press RET")
  6131. (use-local-map org-goto-map)
  6132. (recursive-edit)
  6133. ))
  6134. (kill-buffer "*org-goto*")
  6135. (cons org-goto-selected-point org-goto-exit-command)))
  6136. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6137. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6138. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6139. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6140. (defun org-goto-local-search-headings (string bound noerror)
  6141. "Search and make sure that any matches are in headlines."
  6142. (catch 'return
  6143. (while (if isearch-forward
  6144. (search-forward string bound noerror)
  6145. (search-backward string bound noerror))
  6146. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6147. (and (member :headline context)
  6148. (not (member :tags context))))
  6149. (throw 'return (point))))))
  6150. (defun org-goto-local-auto-isearch ()
  6151. "Start isearch."
  6152. (interactive)
  6153. (goto-char (point-min))
  6154. (let ((keys (this-command-keys)))
  6155. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6156. (isearch-mode t)
  6157. (isearch-process-search-char (string-to-char keys)))))
  6158. (defun org-goto-ret (&optional arg)
  6159. "Finish `org-goto' by going to the new location."
  6160. (interactive "P")
  6161. (setq org-goto-selected-point (point)
  6162. org-goto-exit-command 'return)
  6163. (throw 'exit nil))
  6164. (defun org-goto-left ()
  6165. "Finish `org-goto' by going to the new location."
  6166. (interactive)
  6167. (if (org-at-heading-p)
  6168. (progn
  6169. (beginning-of-line 1)
  6170. (setq org-goto-selected-point (point)
  6171. org-goto-exit-command 'left)
  6172. (throw 'exit nil))
  6173. (error "Not on a heading")))
  6174. (defun org-goto-right ()
  6175. "Finish `org-goto' by going to the new location."
  6176. (interactive)
  6177. (if (org-at-heading-p)
  6178. (progn
  6179. (setq org-goto-selected-point (point)
  6180. org-goto-exit-command 'right)
  6181. (throw 'exit nil))
  6182. (error "Not on a heading")))
  6183. (defun org-goto-quit ()
  6184. "Finish `org-goto' without cursor motion."
  6185. (interactive)
  6186. (setq org-goto-selected-point nil)
  6187. (setq org-goto-exit-command 'quit)
  6188. (throw 'exit nil))
  6189. ;;; Indirect buffer display of subtrees
  6190. (defvar org-indirect-dedicated-frame nil
  6191. "This is the frame being used for indirect tree display.")
  6192. (defvar org-last-indirect-buffer nil)
  6193. (defun org-tree-to-indirect-buffer (&optional arg)
  6194. "Create indirect buffer and narrow it to current subtree.
  6195. With numerical prefix ARG, go up to this level and then take that tree.
  6196. If ARG is negative, go up that many levels.
  6197. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6198. indirect buffer previously made with this command, to avoid proliferation of
  6199. indirect buffers. However, when you call the command with a \
  6200. \\[universal-argument] prefix, or
  6201. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6202. is kept so that you can work with several indirect buffers at the same time.
  6203. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6204. \\[universal-argument] prefix also
  6205. requests that a new frame be made for the new buffer, so that the dedicated
  6206. frame is not changed."
  6207. (interactive "P")
  6208. (let ((cbuf (current-buffer))
  6209. (cwin (selected-window))
  6210. (pos (point))
  6211. beg end level heading ibuf)
  6212. (save-excursion
  6213. (org-back-to-heading t)
  6214. (when (numberp arg)
  6215. (setq level (org-outline-level))
  6216. (if (< arg 0) (setq arg (+ level arg)))
  6217. (while (> (setq level (org-outline-level)) arg)
  6218. (outline-up-heading 1 t)))
  6219. (setq beg (point)
  6220. heading (org-get-heading))
  6221. (org-end-of-subtree t t)
  6222. (if (org-at-heading-p) (backward-char 1))
  6223. (setq end (point)))
  6224. (if (and (buffer-live-p org-last-indirect-buffer)
  6225. (not (eq org-indirect-buffer-display 'new-frame))
  6226. (not arg))
  6227. (kill-buffer org-last-indirect-buffer))
  6228. (setq ibuf (org-get-indirect-buffer cbuf)
  6229. org-last-indirect-buffer ibuf)
  6230. (cond
  6231. ((or (eq org-indirect-buffer-display 'new-frame)
  6232. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6233. (select-frame (make-frame))
  6234. (delete-other-windows)
  6235. (org-pop-to-buffer-same-window ibuf)
  6236. (org-set-frame-title heading))
  6237. ((eq org-indirect-buffer-display 'dedicated-frame)
  6238. (raise-frame
  6239. (select-frame (or (and org-indirect-dedicated-frame
  6240. (frame-live-p org-indirect-dedicated-frame)
  6241. org-indirect-dedicated-frame)
  6242. (setq org-indirect-dedicated-frame (make-frame)))))
  6243. (delete-other-windows)
  6244. (org-pop-to-buffer-same-window ibuf)
  6245. (org-set-frame-title (concat "Indirect: " heading)))
  6246. ((eq org-indirect-buffer-display 'current-window)
  6247. (org-pop-to-buffer-same-window ibuf))
  6248. ((eq org-indirect-buffer-display 'other-window)
  6249. (pop-to-buffer ibuf))
  6250. (t (error "Invalid value")))
  6251. (if (featurep 'xemacs)
  6252. (save-excursion (org-mode) (turn-on-font-lock)))
  6253. (narrow-to-region beg end)
  6254. (show-all)
  6255. (goto-char pos)
  6256. (run-hook-with-args 'org-cycle-hook 'all)
  6257. (and (window-live-p cwin) (select-window cwin))))
  6258. (defun org-get-indirect-buffer (&optional buffer)
  6259. (setq buffer (or buffer (current-buffer)))
  6260. (let ((n 1) (base (buffer-name buffer)) bname)
  6261. (while (buffer-live-p
  6262. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6263. (setq n (1+ n)))
  6264. (condition-case nil
  6265. (make-indirect-buffer buffer bname 'clone)
  6266. (error (make-indirect-buffer buffer bname)))))
  6267. (defun org-set-frame-title (title)
  6268. "Set the title of the current frame to the string TITLE."
  6269. ;; FIXME: how to name a single frame in XEmacs???
  6270. (unless (featurep 'xemacs)
  6271. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6272. ;;;; Structure editing
  6273. ;;; Inserting headlines
  6274. (defun org-previous-line-empty-p ()
  6275. (save-excursion
  6276. (and (not (bobp))
  6277. (or (beginning-of-line 0) t)
  6278. (save-match-data
  6279. (looking-at "[ \t]*$")))))
  6280. (defun org-insert-heading (&optional force-heading invisible-ok)
  6281. "Insert a new heading or item with same depth at point.
  6282. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6283. If point is at the beginning of a headline, insert a sibling before the
  6284. current headline. If point is not at the beginning, split the line,
  6285. create the new headline with the text in the current line after point
  6286. \(but see also the variable `org-M-RET-may-split-line').
  6287. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6288. This is important for non-interactive uses of the command."
  6289. (interactive "P")
  6290. (if (or (= (buffer-size) 0)
  6291. (and (not (save-excursion
  6292. (and (ignore-errors (org-back-to-heading invisible-ok))
  6293. (org-at-heading-p))))
  6294. (or force-heading (not (org-in-item-p)))))
  6295. (progn
  6296. (insert "\n* ")
  6297. (run-hooks 'org-insert-heading-hook))
  6298. (when (or force-heading (not (org-insert-item)))
  6299. (let* ((empty-line-p nil)
  6300. (level nil)
  6301. (on-heading (org-at-heading-p))
  6302. (head (save-excursion
  6303. (condition-case nil
  6304. (progn
  6305. (org-back-to-heading invisible-ok)
  6306. (when (and (not on-heading)
  6307. (featurep 'org-inlinetask)
  6308. (integerp org-inlinetask-min-level)
  6309. (>= (length (match-string 0))
  6310. org-inlinetask-min-level))
  6311. ;; Find a heading level before the inline task
  6312. (while (and (setq level (org-up-heading-safe))
  6313. (>= level org-inlinetask-min-level)))
  6314. (if (org-at-heading-p)
  6315. (org-back-to-heading invisible-ok)
  6316. (error "This should not happen")))
  6317. (setq empty-line-p (org-previous-line-empty-p))
  6318. (match-string 0))
  6319. (error "*"))))
  6320. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6321. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6322. pos hide-previous previous-pos)
  6323. (cond
  6324. ((and (org-at-heading-p) (bolp)
  6325. (or (bobp)
  6326. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6327. ;; insert before the current line
  6328. (open-line (if blank 2 1)))
  6329. ((and (bolp)
  6330. (not org-insert-heading-respect-content)
  6331. (or (bobp)
  6332. (save-excursion
  6333. (backward-char 1) (not (outline-invisible-p)))))
  6334. ;; insert right here
  6335. nil)
  6336. (t
  6337. ;; somewhere in the line
  6338. (save-excursion
  6339. (setq previous-pos (point-at-bol))
  6340. (end-of-line)
  6341. (setq hide-previous (outline-invisible-p)))
  6342. (and org-insert-heading-respect-content (org-show-subtree))
  6343. (let ((split
  6344. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6345. (save-excursion
  6346. (let ((p (point)))
  6347. (goto-char (point-at-bol))
  6348. (and (looking-at org-complex-heading-regexp)
  6349. (match-beginning 4)
  6350. (> p (match-beginning 4)))))))
  6351. tags pos)
  6352. (cond
  6353. (org-insert-heading-respect-content
  6354. (org-end-of-subtree nil t)
  6355. (when (featurep 'org-inlinetask)
  6356. (while (and (not (eobp))
  6357. (looking-at "\\(\\*+\\)[ \t]+")
  6358. (>= (length (match-string 1))
  6359. org-inlinetask-min-level))
  6360. (org-end-of-subtree nil t)))
  6361. (or (bolp) (newline))
  6362. (or (org-previous-line-empty-p)
  6363. (and blank (newline)))
  6364. (open-line 1))
  6365. ((org-at-heading-p)
  6366. (when hide-previous
  6367. (show-children)
  6368. (org-show-entry))
  6369. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6370. (setq tags (and (match-end 2) (match-string 2)))
  6371. (and (match-end 1)
  6372. (delete-region (match-beginning 1) (match-end 1)))
  6373. (setq pos (point-at-bol))
  6374. (or split (end-of-line 1))
  6375. (delete-horizontal-space)
  6376. (if (string-match "\\`\\*+\\'"
  6377. (buffer-substring (point-at-bol) (point)))
  6378. (insert " "))
  6379. (newline (if blank 2 1))
  6380. (when tags
  6381. (save-excursion
  6382. (goto-char pos)
  6383. (end-of-line 1)
  6384. (insert " " tags)
  6385. (org-set-tags nil 'align))))
  6386. (t
  6387. (or split (end-of-line 1))
  6388. (newline (if blank 2 1)))))))
  6389. (insert head) (just-one-space)
  6390. (setq pos (point))
  6391. (end-of-line 1)
  6392. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6393. (when (and org-insert-heading-respect-content hide-previous)
  6394. (save-excursion
  6395. (goto-char previous-pos)
  6396. (hide-subtree)))
  6397. (run-hooks 'org-insert-heading-hook)))))
  6398. (defun org-get-heading (&optional no-tags no-todo)
  6399. "Return the heading of the current entry, without the stars.
  6400. When NO-TAGS is non-nil, don't include tags.
  6401. When NO-TODO is non-nil, don't include TODO keywords."
  6402. (save-excursion
  6403. (org-back-to-heading t)
  6404. (cond
  6405. ((and no-tags no-todo)
  6406. (looking-at org-complex-heading-regexp)
  6407. (match-string 4))
  6408. (no-tags
  6409. (looking-at (concat org-outline-regexp
  6410. "\\(.*?\\)"
  6411. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6412. (match-string 1))
  6413. (no-todo
  6414. (looking-at org-todo-line-regexp)
  6415. (match-string 3))
  6416. (t (looking-at org-heading-regexp)
  6417. (match-string 2)))))
  6418. (defun org-heading-components ()
  6419. "Return the components of the current heading.
  6420. This is a list with the following elements:
  6421. - the level as an integer
  6422. - the reduced level, different if `org-odd-levels-only' is set.
  6423. - the TODO keyword, or nil
  6424. - the priority character, like ?A, or nil if no priority is given
  6425. - the headline text itself, or the tags string if no headline text
  6426. - the tags string, or nil."
  6427. (save-excursion
  6428. (org-back-to-heading t)
  6429. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6430. (list (length (match-string 1))
  6431. (org-reduced-level (length (match-string 1)))
  6432. (org-match-string-no-properties 2)
  6433. (and (match-end 3) (aref (match-string 3) 2))
  6434. (org-match-string-no-properties 4)
  6435. (org-match-string-no-properties 5)))))
  6436. (defun org-get-entry ()
  6437. "Get the entry text, after heading, entire subtree."
  6438. (save-excursion
  6439. (org-back-to-heading t)
  6440. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6441. (defun org-insert-heading-after-current ()
  6442. "Insert a new heading with same level as current, after current subtree."
  6443. (interactive)
  6444. (org-back-to-heading)
  6445. (org-insert-heading)
  6446. (org-move-subtree-down)
  6447. (end-of-line 1))
  6448. (defun org-insert-heading-respect-content ()
  6449. (interactive)
  6450. (let ((org-insert-heading-respect-content t))
  6451. (org-insert-heading t)))
  6452. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6453. (interactive "P")
  6454. (let ((org-insert-heading-respect-content t))
  6455. (org-insert-todo-heading force-state t)))
  6456. (defun org-insert-todo-heading (arg &optional force-heading)
  6457. "Insert a new heading with the same level and TODO state as current heading.
  6458. If the heading has no TODO state, or if the state is DONE, use the first
  6459. state (TODO by default). Also with prefix arg, force first state."
  6460. (interactive "P")
  6461. (when (or force-heading (not (org-insert-item 'checkbox)))
  6462. (org-insert-heading force-heading)
  6463. (save-excursion
  6464. (org-back-to-heading)
  6465. (outline-previous-heading)
  6466. (looking-at org-todo-line-regexp))
  6467. (let*
  6468. ((new-mark-x
  6469. (if (or arg
  6470. (not (match-beginning 2))
  6471. (member (match-string 2) org-done-keywords))
  6472. (car org-todo-keywords-1)
  6473. (match-string 2)))
  6474. (new-mark
  6475. (or
  6476. (run-hook-with-args-until-success
  6477. 'org-todo-get-default-hook new-mark-x nil)
  6478. new-mark-x)))
  6479. (beginning-of-line 1)
  6480. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6481. (if org-treat-insert-todo-heading-as-state-change
  6482. (org-todo new-mark)
  6483. (insert new-mark " "))))
  6484. (when org-provide-todo-statistics
  6485. (org-update-parent-todo-statistics))))
  6486. (defun org-insert-subheading (arg)
  6487. "Insert a new subheading and demote it.
  6488. Works for outline headings and for plain lists alike."
  6489. (interactive "P")
  6490. (org-insert-heading arg)
  6491. (cond
  6492. ((org-at-heading-p) (org-do-demote))
  6493. ((org-at-item-p) (org-indent-item))))
  6494. (defun org-insert-todo-subheading (arg)
  6495. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6496. Works for outline headings and for plain lists alike."
  6497. (interactive "P")
  6498. (org-insert-todo-heading arg)
  6499. (cond
  6500. ((org-at-heading-p) (org-do-demote))
  6501. ((org-at-item-p) (org-indent-item))))
  6502. ;;; Promotion and Demotion
  6503. (defvar org-after-demote-entry-hook nil
  6504. "Hook run after an entry has been demoted.
  6505. The cursor will be at the beginning of the entry.
  6506. When a subtree is being demoted, the hook will be called for each node.")
  6507. (defvar org-after-promote-entry-hook nil
  6508. "Hook run after an entry has been promoted.
  6509. The cursor will be at the beginning of the entry.
  6510. When a subtree is being promoted, the hook will be called for each node.")
  6511. (defun org-promote-subtree ()
  6512. "Promote the entire subtree.
  6513. See also `org-promote'."
  6514. (interactive)
  6515. (save-excursion
  6516. (org-with-limited-levels (org-map-tree 'org-promote)))
  6517. (org-fix-position-after-promote))
  6518. (defun org-demote-subtree ()
  6519. "Demote the entire subtree. See `org-demote'.
  6520. See also `org-promote'."
  6521. (interactive)
  6522. (save-excursion
  6523. (org-with-limited-levels (org-map-tree 'org-demote)))
  6524. (org-fix-position-after-promote))
  6525. (defun org-do-promote ()
  6526. "Promote the current heading higher up the tree.
  6527. If the region is active in `transient-mark-mode', promote all headings
  6528. in the region."
  6529. (interactive)
  6530. (save-excursion
  6531. (if (org-region-active-p)
  6532. (org-map-region 'org-promote (region-beginning) (region-end))
  6533. (org-promote)))
  6534. (org-fix-position-after-promote))
  6535. (defun org-do-demote ()
  6536. "Demote the current heading lower down the tree.
  6537. If the region is active in `transient-mark-mode', demote all headings
  6538. in the region."
  6539. (interactive)
  6540. (save-excursion
  6541. (if (org-region-active-p)
  6542. (org-map-region 'org-demote (region-beginning) (region-end))
  6543. (org-demote)))
  6544. (org-fix-position-after-promote))
  6545. (defun org-fix-position-after-promote ()
  6546. "Make sure that after pro/demotion cursor position is right."
  6547. (let ((pos (point)))
  6548. (when (save-excursion
  6549. (beginning-of-line 1)
  6550. (looking-at org-todo-line-regexp)
  6551. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6552. (cond ((eobp) (insert " "))
  6553. ((eolp) (insert " "))
  6554. ((equal (char-after) ?\ ) (forward-char 1))))))
  6555. (defun org-current-level ()
  6556. "Return the level of the current entry, or nil if before the first headline.
  6557. The level is the number of stars at the beginning of the headline."
  6558. (save-excursion
  6559. (org-with-limited-levels
  6560. (if (ignore-errors (org-back-to-heading t))
  6561. (funcall outline-level)))))
  6562. (defun org-get-previous-line-level ()
  6563. "Return the outline depth of the last headline before the current line.
  6564. Returns 0 for the first headline in the buffer, and nil if before the
  6565. first headline."
  6566. (let ((current-level (org-current-level))
  6567. (prev-level (when (> (line-number-at-pos) 1)
  6568. (save-excursion
  6569. (beginning-of-line 0)
  6570. (org-current-level)))))
  6571. (cond ((null current-level) nil) ; Before first headline
  6572. ((null prev-level) 0) ; At first headline
  6573. (prev-level))))
  6574. (defun org-reduced-level (l)
  6575. "Compute the effective level of a heading.
  6576. This takes into account the setting of `org-odd-levels-only'."
  6577. (cond
  6578. ((zerop l) 0)
  6579. (org-odd-levels-only (1+ (floor (/ l 2))))
  6580. (t l)))
  6581. (defun org-level-increment ()
  6582. "Return the number of stars that will be added or removed at a
  6583. time to headlines when structure editing, based on the value of
  6584. `org-odd-levels-only'."
  6585. (if org-odd-levels-only 2 1))
  6586. (defun org-get-valid-level (level &optional change)
  6587. "Rectify a level change under the influence of `org-odd-levels-only'
  6588. LEVEL is a current level, CHANGE is by how much the level should be
  6589. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6590. even level numbers will become the next higher odd number."
  6591. (if org-odd-levels-only
  6592. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6593. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6594. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6595. (max 1 (+ level (or change 0)))))
  6596. (if (boundp 'define-obsolete-function-alias)
  6597. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6598. (define-obsolete-function-alias 'org-get-legal-level
  6599. 'org-get-valid-level)
  6600. (define-obsolete-function-alias 'org-get-legal-level
  6601. 'org-get-valid-level "23.1")))
  6602. (defun org-promote ()
  6603. "Promote the current heading higher up the tree.
  6604. If the region is active in `transient-mark-mode', promote all headings
  6605. in the region."
  6606. (org-back-to-heading t)
  6607. (let* ((level (save-match-data (funcall outline-level)))
  6608. (after-change-functions (remove 'flyspell-after-change-function
  6609. after-change-functions))
  6610. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6611. (diff (abs (- level (length up-head) -1))))
  6612. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6613. (replace-match up-head nil t)
  6614. ;; Fixup tag positioning
  6615. (and org-auto-align-tags (org-set-tags nil t))
  6616. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6617. (run-hooks 'org-after-promote-entry-hook)))
  6618. (defun org-demote ()
  6619. "Demote the current heading lower down the tree.
  6620. If the region is active in `transient-mark-mode', demote all headings
  6621. in the region."
  6622. (org-back-to-heading t)
  6623. (let* ((level (save-match-data (funcall outline-level)))
  6624. (after-change-functions (remove 'flyspell-after-change-function
  6625. after-change-functions))
  6626. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6627. (diff (abs (- level (length down-head) -1))))
  6628. (replace-match down-head nil t)
  6629. ;; Fixup tag positioning
  6630. (and org-auto-align-tags (org-set-tags nil t))
  6631. (if org-adapt-indentation (org-fixup-indentation diff))
  6632. (run-hooks 'org-after-demote-entry-hook)))
  6633. (defun org-cycle-level ()
  6634. "Cycle the level of an empty headline through possible states.
  6635. This goes first to child, then to parent, level, then up the hierarchy.
  6636. After top level, it switches back to sibling level."
  6637. (interactive)
  6638. (let ((org-adapt-indentation nil))
  6639. (when (org-point-at-end-of-empty-headline)
  6640. (setq this-command 'org-cycle-level) ; Only needed for caching
  6641. (let ((cur-level (org-current-level))
  6642. (prev-level (org-get-previous-line-level)))
  6643. (cond
  6644. ;; If first headline in file, promote to top-level.
  6645. ((= prev-level 0)
  6646. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6647. do (org-do-promote)))
  6648. ;; If same level as prev, demote one.
  6649. ((= prev-level cur-level)
  6650. (org-do-demote))
  6651. ;; If parent is top-level, promote to top level if not already.
  6652. ((= prev-level 1)
  6653. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6654. do (org-do-promote)))
  6655. ;; If top-level, return to prev-level.
  6656. ((= cur-level 1)
  6657. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6658. do (org-do-demote)))
  6659. ;; If less than prev-level, promote one.
  6660. ((< cur-level prev-level)
  6661. (org-do-promote))
  6662. ;; If deeper than prev-level, promote until higher than
  6663. ;; prev-level.
  6664. ((> cur-level prev-level)
  6665. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6666. do (org-do-promote))))
  6667. t))))
  6668. (defun org-map-tree (fun)
  6669. "Call FUN for every heading underneath the current one."
  6670. (org-back-to-heading)
  6671. (let ((level (funcall outline-level)))
  6672. (save-excursion
  6673. (funcall fun)
  6674. (while (and (progn
  6675. (outline-next-heading)
  6676. (> (funcall outline-level) level))
  6677. (not (eobp)))
  6678. (funcall fun)))))
  6679. (defun org-map-region (fun beg end)
  6680. "Call FUN for every heading between BEG and END."
  6681. (let ((org-ignore-region t))
  6682. (save-excursion
  6683. (setq end (copy-marker end))
  6684. (goto-char beg)
  6685. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6686. (< (point) end))
  6687. (funcall fun))
  6688. (while (and (progn
  6689. (outline-next-heading)
  6690. (< (point) end))
  6691. (not (eobp)))
  6692. (funcall fun)))))
  6693. (defvar org-property-end-re) ; silence byte-compiler
  6694. (defun org-fixup-indentation (diff)
  6695. "Change the indentation in the current entry by DIFF.
  6696. However, if any line in the current entry has no indentation, or if it
  6697. would end up with no indentation after the change, nothing at all is done."
  6698. (save-excursion
  6699. (let ((end (save-excursion (outline-next-heading)
  6700. (point-marker)))
  6701. (prohibit (if (> diff 0)
  6702. "^\\S-"
  6703. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6704. col)
  6705. (unless (save-excursion (end-of-line 1)
  6706. (re-search-forward prohibit end t))
  6707. (while (and (< (point) end)
  6708. (re-search-forward "^[ \t]+" end t))
  6709. (goto-char (match-end 0))
  6710. (setq col (current-column))
  6711. (if (< diff 0) (replace-match ""))
  6712. (org-indent-to-column (+ diff col))))
  6713. (move-marker end nil))))
  6714. (defun org-convert-to-odd-levels ()
  6715. "Convert an org-mode file with all levels allowed to one with odd levels.
  6716. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6717. level 5 etc."
  6718. (interactive)
  6719. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6720. (let ((outline-level 'org-outline-level)
  6721. (org-odd-levels-only nil) n)
  6722. (save-excursion
  6723. (goto-char (point-min))
  6724. (while (re-search-forward "^\\*\\*+ " nil t)
  6725. (setq n (- (length (match-string 0)) 2))
  6726. (while (>= (setq n (1- n)) 0)
  6727. (org-demote))
  6728. (end-of-line 1))))))
  6729. (defun org-convert-to-oddeven-levels ()
  6730. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6731. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6732. file contains a section with an even level, conversion would
  6733. destroy the structure of the file. An error is signaled in this
  6734. case."
  6735. (interactive)
  6736. (goto-char (point-min))
  6737. ;; First check if there are no even levels
  6738. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6739. (org-show-context t)
  6740. (error "Not all levels are odd in this file. Conversion not possible"))
  6741. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6742. (let ((outline-regexp org-outline-regexp)
  6743. (outline-level 'org-outline-level)
  6744. (org-odd-levels-only nil) n)
  6745. (save-excursion
  6746. (goto-char (point-min))
  6747. (while (re-search-forward "^\\*\\*+ " nil t)
  6748. (setq n (/ (1- (length (match-string 0))) 2))
  6749. (while (>= (setq n (1- n)) 0)
  6750. (org-promote))
  6751. (end-of-line 1))))))
  6752. (defun org-tr-level (n)
  6753. "Make N odd if required."
  6754. (if org-odd-levels-only (1+ (/ n 2)) n))
  6755. ;;; Vertical tree motion, cutting and pasting of subtrees
  6756. (defun org-move-subtree-up (&optional arg)
  6757. "Move the current subtree up past ARG headlines of the same level."
  6758. (interactive "p")
  6759. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6760. (defun org-move-subtree-down (&optional arg)
  6761. "Move the current subtree down past ARG headlines of the same level."
  6762. (interactive "p")
  6763. (setq arg (prefix-numeric-value arg))
  6764. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6765. 'org-get-last-sibling))
  6766. (ins-point (make-marker))
  6767. (cnt (abs arg))
  6768. (col (current-column))
  6769. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6770. ;; Select the tree
  6771. (org-back-to-heading)
  6772. (setq beg0 (point))
  6773. (save-excursion
  6774. (setq ne-beg (org-back-over-empty-lines))
  6775. (setq beg (point)))
  6776. (save-match-data
  6777. (save-excursion (outline-end-of-heading)
  6778. (setq folded (outline-invisible-p)))
  6779. (outline-end-of-subtree))
  6780. (outline-next-heading)
  6781. (setq ne-end (org-back-over-empty-lines))
  6782. (setq end (point))
  6783. (goto-char beg0)
  6784. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6785. ;; include less whitespace
  6786. (save-excursion
  6787. (goto-char beg)
  6788. (forward-line (- ne-beg ne-end))
  6789. (setq beg (point))))
  6790. ;; Find insertion point, with error handling
  6791. (while (> cnt 0)
  6792. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6793. (progn (goto-char beg0)
  6794. (error "Cannot move past superior level or buffer limit")))
  6795. (setq cnt (1- cnt)))
  6796. (if (> arg 0)
  6797. ;; Moving forward - still need to move over subtree
  6798. (progn (org-end-of-subtree t t)
  6799. (save-excursion
  6800. (org-back-over-empty-lines)
  6801. (or (bolp) (newline)))))
  6802. (setq ne-ins (org-back-over-empty-lines))
  6803. (move-marker ins-point (point))
  6804. (setq txt (buffer-substring beg end))
  6805. (org-save-markers-in-region beg end)
  6806. (delete-region beg end)
  6807. (org-remove-empty-overlays-at beg)
  6808. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6809. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6810. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6811. (let ((bbb (point)))
  6812. (insert-before-markers txt)
  6813. (org-reinstall-markers-in-region bbb)
  6814. (move-marker ins-point bbb))
  6815. (or (bolp) (insert "\n"))
  6816. (setq ins-end (point))
  6817. (goto-char ins-point)
  6818. (org-skip-whitespace)
  6819. (when (and (< arg 0)
  6820. (org-first-sibling-p)
  6821. (> ne-ins ne-beg))
  6822. ;; Move whitespace back to beginning
  6823. (save-excursion
  6824. (goto-char ins-end)
  6825. (let ((kill-whole-line t))
  6826. (kill-line (- ne-ins ne-beg)) (point)))
  6827. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6828. (move-marker ins-point nil)
  6829. (if folded
  6830. (hide-subtree)
  6831. (org-show-entry)
  6832. (show-children)
  6833. (org-cycle-hide-drawers 'children))
  6834. (org-clean-visibility-after-subtree-move)
  6835. ;; move back to the initial column we were at
  6836. (move-to-column col)))
  6837. (defvar org-subtree-clip ""
  6838. "Clipboard for cut and paste of subtrees.
  6839. This is actually only a copy of the kill, because we use the normal kill
  6840. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6841. (defvar org-subtree-clip-folded nil
  6842. "Was the last copied subtree folded?
  6843. This is used to fold the tree back after pasting.")
  6844. (defun org-cut-subtree (&optional n)
  6845. "Cut the current subtree into the clipboard.
  6846. With prefix arg N, cut this many sequential subtrees.
  6847. This is a short-hand for marking the subtree and then cutting it."
  6848. (interactive "p")
  6849. (org-copy-subtree n 'cut))
  6850. (defun org-copy-subtree (&optional n cut force-store-markers)
  6851. "Cut the current subtree into the clipboard.
  6852. With prefix arg N, cut this many sequential subtrees.
  6853. This is a short-hand for marking the subtree and then copying it.
  6854. If CUT is non-nil, actually cut the subtree.
  6855. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6856. of some markers in the region, even if CUT is non-nil. This is
  6857. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6858. (interactive "p")
  6859. (let (beg end folded (beg0 (point)))
  6860. (if (org-called-interactively-p 'any)
  6861. (org-back-to-heading nil) ; take what looks like a subtree
  6862. (org-back-to-heading t)) ; take what is really there
  6863. (org-back-over-empty-lines)
  6864. (setq beg (point))
  6865. (skip-chars-forward " \t\r\n")
  6866. (save-match-data
  6867. (save-excursion (outline-end-of-heading)
  6868. (setq folded (outline-invisible-p)))
  6869. (condition-case nil
  6870. (org-forward-same-level (1- n) t)
  6871. (error nil))
  6872. (org-end-of-subtree t t))
  6873. (org-back-over-empty-lines)
  6874. (setq end (point))
  6875. (goto-char beg0)
  6876. (when (> end beg)
  6877. (setq org-subtree-clip-folded folded)
  6878. (when (or cut force-store-markers)
  6879. (org-save-markers-in-region beg end))
  6880. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6881. (setq org-subtree-clip (current-kill 0))
  6882. (message "%s: Subtree(s) with %d characters"
  6883. (if cut "Cut" "Copied")
  6884. (length org-subtree-clip)))))
  6885. (defun org-paste-subtree (&optional level tree for-yank)
  6886. "Paste the clipboard as a subtree, with modification of headline level.
  6887. The entire subtree is promoted or demoted in order to match a new headline
  6888. level.
  6889. If the cursor is at the beginning of a headline, the same level as
  6890. that headline is used to paste the tree
  6891. If not, the new level is derived from the *visible* headings
  6892. before and after the insertion point, and taken to be the inferior headline
  6893. level of the two. So if the previous visible heading is level 3 and the
  6894. next is level 4 (or vice versa), level 4 will be used for insertion.
  6895. This makes sure that the subtree remains an independent subtree and does
  6896. not swallow low level entries.
  6897. You can also force a different level, either by using a numeric prefix
  6898. argument, or by inserting the heading marker by hand. For example, if the
  6899. cursor is after \"*****\", then the tree will be shifted to level 5.
  6900. If optional TREE is given, use this text instead of the kill ring.
  6901. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6902. move back over whitespace before inserting, and move point to the end of
  6903. the inserted text when done."
  6904. (interactive "P")
  6905. (setq tree (or tree (and kill-ring (current-kill 0))))
  6906. (unless (org-kill-is-subtree-p tree)
  6907. (error "%s"
  6908. (substitute-command-keys
  6909. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6910. (org-with-limited-levels
  6911. (let* ((visp (not (outline-invisible-p)))
  6912. (txt tree)
  6913. (^re_ "\\(\\*+\\)[ \t]*")
  6914. (old-level (if (string-match org-outline-regexp-bol txt)
  6915. (- (match-end 0) (match-beginning 0) 1)
  6916. -1))
  6917. (force-level (cond (level (prefix-numeric-value level))
  6918. ((and (looking-at "[ \t]*$")
  6919. (string-match
  6920. "^\\*+$" (buffer-substring
  6921. (point-at-bol) (point))))
  6922. (- (match-end 1) (match-beginning 1)))
  6923. ((and (bolp)
  6924. (looking-at org-outline-regexp))
  6925. (- (match-end 0) (point) 1))
  6926. (t nil)))
  6927. (previous-level (save-excursion
  6928. (condition-case nil
  6929. (progn
  6930. (outline-previous-visible-heading 1)
  6931. (if (looking-at ^re_)
  6932. (- (match-end 0) (match-beginning 0) 1)
  6933. 1))
  6934. (error 1))))
  6935. (next-level (save-excursion
  6936. (condition-case nil
  6937. (progn
  6938. (or (looking-at org-outline-regexp)
  6939. (outline-next-visible-heading 1))
  6940. (if (looking-at ^re_)
  6941. (- (match-end 0) (match-beginning 0) 1)
  6942. 1))
  6943. (error 1))))
  6944. (new-level (or force-level (max previous-level next-level)))
  6945. (shift (if (or (= old-level -1)
  6946. (= new-level -1)
  6947. (= old-level new-level))
  6948. 0
  6949. (- new-level old-level)))
  6950. (delta (if (> shift 0) -1 1))
  6951. (func (if (> shift 0) 'org-demote 'org-promote))
  6952. (org-odd-levels-only nil)
  6953. beg end newend)
  6954. ;; Remove the forced level indicator
  6955. (if force-level
  6956. (delete-region (point-at-bol) (point)))
  6957. ;; Paste
  6958. (beginning-of-line (if (bolp) 1 2))
  6959. (unless for-yank (org-back-over-empty-lines))
  6960. (setq beg (point))
  6961. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6962. (insert-before-markers txt)
  6963. (unless (string-match "\n\\'" txt) (insert "\n"))
  6964. (setq newend (point))
  6965. (org-reinstall-markers-in-region beg)
  6966. (setq end (point))
  6967. (goto-char beg)
  6968. (skip-chars-forward " \t\n\r")
  6969. (setq beg (point))
  6970. (if (and (outline-invisible-p) visp)
  6971. (save-excursion (outline-show-heading)))
  6972. ;; Shift if necessary
  6973. (unless (= shift 0)
  6974. (save-restriction
  6975. (narrow-to-region beg end)
  6976. (while (not (= shift 0))
  6977. (org-map-region func (point-min) (point-max))
  6978. (setq shift (+ delta shift)))
  6979. (goto-char (point-min))
  6980. (setq newend (point-max))))
  6981. (when (or (org-called-interactively-p 'interactive) for-yank)
  6982. (message "Clipboard pasted as level %d subtree" new-level))
  6983. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6984. kill-ring
  6985. (eq org-subtree-clip (current-kill 0))
  6986. org-subtree-clip-folded)
  6987. ;; The tree was folded before it was killed/copied
  6988. (hide-subtree))
  6989. (and for-yank (goto-char newend)))))
  6990. (defun org-kill-is-subtree-p (&optional txt)
  6991. "Check if the current kill is an outline subtree, or a set of trees.
  6992. Returns nil if kill does not start with a headline, or if the first
  6993. headline level is not the largest headline level in the tree.
  6994. So this will actually accept several entries of equal levels as well,
  6995. which is OK for `org-paste-subtree'.
  6996. If optional TXT is given, check this string instead of the current kill."
  6997. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6998. (re (org-get-limited-outline-regexp))
  6999. (^re (concat "^" re))
  7000. (start-level (and kill
  7001. (string-match
  7002. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7003. kill)
  7004. (- (match-end 2) (match-beginning 2) 1)))
  7005. (start (1+ (or (match-beginning 2) -1))))
  7006. (if (not start-level)
  7007. (progn
  7008. nil) ;; does not even start with a heading
  7009. (catch 'exit
  7010. (while (setq start (string-match ^re kill (1+ start)))
  7011. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7012. (throw 'exit nil)))
  7013. t))))
  7014. (defvar org-markers-to-move nil
  7015. "Markers that should be moved with a cut-and-paste operation.
  7016. Those markers are stored together with their positions relative to
  7017. the start of the region.")
  7018. (defun org-save-markers-in-region (beg end)
  7019. "Check markers in region.
  7020. If these markers are between BEG and END, record their position relative
  7021. to BEG, so that after moving the block of text, we can put the markers back
  7022. into place.
  7023. This function gets called just before an entry or tree gets cut from the
  7024. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7025. called immediately, to move the markers with the entries."
  7026. (setq org-markers-to-move nil)
  7027. (when (featurep 'org-clock)
  7028. (org-clock-save-markers-for-cut-and-paste beg end))
  7029. (when (featurep 'org-agenda)
  7030. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7031. (defun org-check-and-save-marker (marker beg end)
  7032. "Check if MARKER is between BEG and END.
  7033. If yes, remember the marker and the distance to BEG."
  7034. (when (and (marker-buffer marker)
  7035. (equal (marker-buffer marker) (current-buffer)))
  7036. (if (and (>= marker beg) (< marker end))
  7037. (push (cons marker (- marker beg)) org-markers-to-move))))
  7038. (defun org-reinstall-markers-in-region (beg)
  7039. "Move all remembered markers to their position relative to BEG."
  7040. (mapc (lambda (x)
  7041. (move-marker (car x) (+ beg (cdr x))))
  7042. org-markers-to-move)
  7043. (setq org-markers-to-move nil))
  7044. (defun org-narrow-to-subtree ()
  7045. "Narrow buffer to the current subtree."
  7046. (interactive)
  7047. (save-excursion
  7048. (save-match-data
  7049. (org-with-limited-levels
  7050. (narrow-to-region
  7051. (progn (org-back-to-heading t) (point))
  7052. (progn (org-end-of-subtree t t)
  7053. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7054. (point)))))))
  7055. (defun org-narrow-to-block ()
  7056. "Narrow buffer to the current block."
  7057. (interactive)
  7058. (let* ((case-fold-search t)
  7059. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7060. "^[ \t]*#\\+end_.*")))
  7061. (if blockp
  7062. (narrow-to-region (car blockp) (cdr blockp))
  7063. (error "Not in a block"))))
  7064. (eval-when-compile
  7065. (defvar org-property-drawer-re))
  7066. (defvar org-property-start-re) ;; defined below
  7067. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7068. "Clone the task (subtree) at point N times.
  7069. The clones will be inserted as siblings.
  7070. In interactive use, the user will be prompted for the number of
  7071. clones to be produced, and for a time SHIFT, which may be a
  7072. repeater as used in time stamps, for example `+3d'.
  7073. When a valid repeater is given and the entry contains any time
  7074. stamps, the clones will become a sequence in time, with time
  7075. stamps in the subtree shifted for each clone produced. If SHIFT
  7076. is nil or the empty string, time stamps will be left alone. The
  7077. ID property of the original subtree is removed.
  7078. If the original subtree did contain time stamps with a repeater,
  7079. the following will happen:
  7080. - the repeater will be removed in each clone
  7081. - an additional clone will be produced, with the current, unshifted
  7082. date(s) in the entry.
  7083. - the original entry will be placed *after* all the clones, with
  7084. repeater intact.
  7085. - the start days in the repeater in the original entry will be shifted
  7086. to past the last clone.
  7087. I this way you can spell out a number of instances of a repeating task,
  7088. and still retain the repeater to cover future instances of the task."
  7089. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7090. (let (beg end template task idprop
  7091. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7092. (drawer-re org-drawer-regexp))
  7093. (if (not (and (integerp n) (> n 0)))
  7094. (error "Invalid number of replications %s" n))
  7095. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7096. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7097. shift)))
  7098. (error "Invalid shift specification %s" shift))
  7099. (when doshift
  7100. (setq shift-n (string-to-number (match-string 1 shift))
  7101. shift-what (cdr (assoc (match-string 2 shift)
  7102. '(("d" . day) ("w" . week)
  7103. ("m" . month) ("y" . year))))))
  7104. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7105. (setq nmin 1 nmax n)
  7106. (org-back-to-heading t)
  7107. (setq beg (point))
  7108. (setq idprop (org-entry-get nil "ID"))
  7109. (org-end-of-subtree t t)
  7110. (or (bolp) (insert "\n"))
  7111. (setq end (point))
  7112. (setq template (buffer-substring beg end))
  7113. (when (and doshift
  7114. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  7115. (delete-region beg end)
  7116. (setq end beg)
  7117. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7118. (goto-char end)
  7119. (loop for n from nmin to nmax do
  7120. ;; prepare clone
  7121. (with-temp-buffer
  7122. (insert template)
  7123. (org-mode)
  7124. (goto-char (point-min))
  7125. (org-show-subtree)
  7126. (and idprop (if org-clone-delete-id
  7127. (org-entry-delete nil "ID")
  7128. (org-id-get-create t)))
  7129. (unless (= n 0)
  7130. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7131. (kill-whole-line))
  7132. (goto-char (point-min))
  7133. (while (re-search-forward drawer-re nil t)
  7134. (mapc (lambda (d)
  7135. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7136. (goto-char (point-min))
  7137. (when doshift
  7138. (while (re-search-forward org-ts-regexp-both nil t)
  7139. (org-timestamp-change (* n shift-n) shift-what))
  7140. (unless (= n n-no-remove)
  7141. (goto-char (point-min))
  7142. (while (re-search-forward org-ts-regexp nil t)
  7143. (save-excursion
  7144. (goto-char (match-beginning 0))
  7145. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  7146. (delete-region (match-beginning 1) (match-end 1)))))))
  7147. (setq task (buffer-string)))
  7148. (insert task))
  7149. (goto-char beg)))
  7150. ;;; Outline Sorting
  7151. (defun org-sort (with-case)
  7152. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7153. Optional argument WITH-CASE means sort case-sensitively."
  7154. (interactive "P")
  7155. (cond
  7156. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7157. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7158. (t
  7159. (org-call-with-arg 'org-sort-entries with-case))))
  7160. (defun org-sort-remove-invisible (s)
  7161. (remove-text-properties 0 (length s) org-rm-props s)
  7162. (while (string-match org-bracket-link-regexp s)
  7163. (setq s (replace-match (if (match-end 2)
  7164. (match-string 3 s)
  7165. (match-string 1 s)) t t s)))
  7166. s)
  7167. (defvar org-priority-regexp) ; defined later in the file
  7168. (defvar org-after-sorting-entries-or-items-hook nil
  7169. "Hook that is run after a bunch of entries or items have been sorted.
  7170. When children are sorted, the cursor is in the parent line when this
  7171. hook gets called. When a region or a plain list is sorted, the cursor
  7172. will be in the first entry of the sorted region/list.")
  7173. (defun org-sort-entries
  7174. (&optional with-case sorting-type getkey-func compare-func property)
  7175. "Sort entries on a certain level of an outline tree.
  7176. If there is an active region, the entries in the region are sorted.
  7177. Else, if the cursor is before the first entry, sort the top-level items.
  7178. Else, the children of the entry at point are sorted.
  7179. Sorting can be alphabetically, numerically, by date/time as given by
  7180. a time stamp, by a property or by priority.
  7181. The command prompts for the sorting type unless it has been given to the
  7182. function through the SORTING-TYPE argument, which needs to be a character,
  7183. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7184. precise meaning of each character:
  7185. n Numerically, by converting the beginning of the entry/item to a number.
  7186. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7187. t By date/time, either the first active time stamp in the entry, or, if
  7188. none exist, by the first inactive one.
  7189. s By the scheduled date/time.
  7190. d By deadline date/time.
  7191. c By creation time, which is assumed to be the first inactive time stamp
  7192. at the beginning of a line.
  7193. p By priority according to the cookie.
  7194. r By the value of a property.
  7195. Capital letters will reverse the sort order.
  7196. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7197. called with point at the beginning of the record. It must return either
  7198. a string or a number that should serve as the sorting key for that record.
  7199. Comparing entries ignores case by default. However, with an optional argument
  7200. WITH-CASE, the sorting considers case as well."
  7201. (interactive "P")
  7202. (let ((case-func (if with-case 'identity 'downcase))
  7203. start beg end stars re re2
  7204. txt what tmp)
  7205. ;; Find beginning and end of region to sort
  7206. (cond
  7207. ((org-region-active-p)
  7208. ;; we will sort the region
  7209. (setq end (region-end)
  7210. what "region")
  7211. (goto-char (region-beginning))
  7212. (if (not (org-at-heading-p)) (outline-next-heading))
  7213. (setq start (point)))
  7214. ((or (org-at-heading-p)
  7215. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7216. ;; we will sort the children of the current headline
  7217. (org-back-to-heading)
  7218. (setq start (point)
  7219. end (progn (org-end-of-subtree t t)
  7220. (or (bolp) (insert "\n"))
  7221. (org-back-over-empty-lines)
  7222. (point))
  7223. what "children")
  7224. (goto-char start)
  7225. (show-subtree)
  7226. (outline-next-heading))
  7227. (t
  7228. ;; we will sort the top-level entries in this file
  7229. (goto-char (point-min))
  7230. (or (org-at-heading-p) (outline-next-heading))
  7231. (setq start (point))
  7232. (goto-char (point-max))
  7233. (beginning-of-line 1)
  7234. (when (looking-at ".*?\\S-")
  7235. ;; File ends in a non-white line
  7236. (end-of-line 1)
  7237. (insert "\n"))
  7238. (setq end (point-max))
  7239. (setq what "top-level")
  7240. (goto-char start)
  7241. (show-all)))
  7242. (setq beg (point))
  7243. (if (>= beg end) (error "Nothing to sort"))
  7244. (looking-at "\\(\\*+\\)")
  7245. (setq stars (match-string 1)
  7246. re (concat "^" (regexp-quote stars) " +")
  7247. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7248. txt (buffer-substring beg end))
  7249. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7250. (if (and (not (equal stars "*")) (string-match re2 txt))
  7251. (error "Region to sort contains a level above the first entry"))
  7252. (unless sorting-type
  7253. (message
  7254. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7255. [t]ime [s]cheduled [d]eadline [c]reated
  7256. A/N/T/S/D/C/P/O/F means reversed:"
  7257. what)
  7258. (setq sorting-type (read-char-exclusive))
  7259. (and (= (downcase sorting-type) ?f)
  7260. (setq getkey-func
  7261. (org-icompleting-read "Sort using function: "
  7262. obarray 'fboundp t nil nil))
  7263. (setq getkey-func (intern getkey-func)))
  7264. (and (= (downcase sorting-type) ?r)
  7265. (setq property
  7266. (org-icompleting-read "Property: "
  7267. (mapcar 'list (org-buffer-property-keys t))
  7268. nil t))))
  7269. (message "Sorting entries...")
  7270. (save-restriction
  7271. (narrow-to-region start end)
  7272. (let ((dcst (downcase sorting-type))
  7273. (case-fold-search nil)
  7274. (now (current-time)))
  7275. (sort-subr
  7276. (/= dcst sorting-type)
  7277. ;; This function moves to the beginning character of the "record" to
  7278. ;; be sorted.
  7279. (lambda nil
  7280. (if (re-search-forward re nil t)
  7281. (goto-char (match-beginning 0))
  7282. (goto-char (point-max))))
  7283. ;; This function moves to the last character of the "record" being
  7284. ;; sorted.
  7285. (lambda nil
  7286. (save-match-data
  7287. (condition-case nil
  7288. (outline-forward-same-level 1)
  7289. (error
  7290. (goto-char (point-max))))))
  7291. ;; This function returns the value that gets sorted against.
  7292. (lambda nil
  7293. (cond
  7294. ((= dcst ?n)
  7295. (if (looking-at org-complex-heading-regexp)
  7296. (string-to-number (match-string 4))
  7297. nil))
  7298. ((= dcst ?a)
  7299. (if (looking-at org-complex-heading-regexp)
  7300. (funcall case-func (match-string 4))
  7301. nil))
  7302. ((= dcst ?t)
  7303. (let ((end (save-excursion (outline-next-heading) (point))))
  7304. (if (or (re-search-forward org-ts-regexp end t)
  7305. (re-search-forward org-ts-regexp-both end t))
  7306. (org-time-string-to-seconds (match-string 0))
  7307. (org-float-time now))))
  7308. ((= dcst ?c)
  7309. (let ((end (save-excursion (outline-next-heading) (point))))
  7310. (if (re-search-forward
  7311. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7312. end t)
  7313. (org-time-string-to-seconds (match-string 0))
  7314. (org-float-time now))))
  7315. ((= dcst ?s)
  7316. (let ((end (save-excursion (outline-next-heading) (point))))
  7317. (if (re-search-forward org-scheduled-time-regexp end t)
  7318. (org-time-string-to-seconds (match-string 1))
  7319. (org-float-time now))))
  7320. ((= dcst ?d)
  7321. (let ((end (save-excursion (outline-next-heading) (point))))
  7322. (if (re-search-forward org-deadline-time-regexp end t)
  7323. (org-time-string-to-seconds (match-string 1))
  7324. (org-float-time now))))
  7325. ((= dcst ?p)
  7326. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7327. (string-to-char (match-string 2))
  7328. org-default-priority))
  7329. ((= dcst ?r)
  7330. (or (org-entry-get nil property) ""))
  7331. ((= dcst ?o)
  7332. (if (looking-at org-complex-heading-regexp)
  7333. (- 9999 (length (member (match-string 2)
  7334. org-todo-keywords-1)))))
  7335. ((= dcst ?f)
  7336. (if getkey-func
  7337. (progn
  7338. (setq tmp (funcall getkey-func))
  7339. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7340. tmp)
  7341. (error "Invalid key function `%s'" getkey-func)))
  7342. (t (error "Invalid sorting type `%c'" sorting-type))))
  7343. nil
  7344. (cond
  7345. ((= dcst ?a) 'string<)
  7346. ((= dcst ?f) compare-func)
  7347. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7348. (t nil)))))
  7349. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7350. (message "Sorting entries...done")))
  7351. (defun org-do-sort (table what &optional with-case sorting-type)
  7352. "Sort TABLE of WHAT according to SORTING-TYPE.
  7353. The user will be prompted for the SORTING-TYPE if the call to this
  7354. function does not specify it. WHAT is only for the prompt, to indicate
  7355. what is being sorted. The sorting key will be extracted from
  7356. the car of the elements of the table.
  7357. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7358. (unless sorting-type
  7359. (message
  7360. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7361. what)
  7362. (setq sorting-type (read-char-exclusive)))
  7363. (let ((dcst (downcase sorting-type))
  7364. extractfun comparefun)
  7365. ;; Define the appropriate functions
  7366. (cond
  7367. ((= dcst ?n)
  7368. (setq extractfun 'string-to-number
  7369. comparefun (if (= dcst sorting-type) '< '>)))
  7370. ((= dcst ?a)
  7371. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7372. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7373. comparefun (if (= dcst sorting-type)
  7374. 'string<
  7375. (lambda (a b) (and (not (string< a b))
  7376. (not (string= a b)))))))
  7377. ((= dcst ?t)
  7378. (setq extractfun
  7379. (lambda (x)
  7380. (if (or (string-match org-ts-regexp x)
  7381. (string-match org-ts-regexp-both x))
  7382. (org-float-time
  7383. (org-time-string-to-time (match-string 0 x)))
  7384. 0))
  7385. comparefun (if (= dcst sorting-type) '< '>)))
  7386. (t (error "Invalid sorting type `%c'" sorting-type)))
  7387. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7388. table)
  7389. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7390. ;;; The orgstruct minor mode
  7391. ;; Define a minor mode which can be used in other modes in order to
  7392. ;; integrate the org-mode structure editing commands.
  7393. ;; This is really a hack, because the org-mode structure commands use
  7394. ;; keys which normally belong to the major mode. Here is how it
  7395. ;; works: The minor mode defines all the keys necessary to operate the
  7396. ;; structure commands, but wraps the commands into a function which
  7397. ;; tests if the cursor is currently at a headline or a plain list
  7398. ;; item. If that is the case, the structure command is used,
  7399. ;; temporarily setting many Org-mode variables like regular
  7400. ;; expressions for filling etc. However, when any of those keys is
  7401. ;; used at a different location, function uses `key-binding' to look
  7402. ;; up if the key has an associated command in another currently active
  7403. ;; keymap (minor modes, major mode, global), and executes that
  7404. ;; command. There might be problems if any of the keys is otherwise
  7405. ;; used as a prefix key.
  7406. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7407. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7408. ;; addresses this by checking explicitly for both bindings.
  7409. (defvar orgstruct-mode-map (make-sparse-keymap)
  7410. "Keymap for the minor `orgstruct-mode'.")
  7411. (defvar org-local-vars nil
  7412. "List of local variables, for use by `orgstruct-mode'.")
  7413. ;;;###autoload
  7414. (define-minor-mode orgstruct-mode
  7415. "Toggle the minor mode `orgstruct-mode'.
  7416. This mode is for using Org-mode structure commands in other
  7417. modes. The following keys behave as if Org-mode were active, if
  7418. the cursor is on a headline, or on a plain list item (both as
  7419. defined by Org-mode).
  7420. M-up Move entry/item up
  7421. M-down Move entry/item down
  7422. M-left Promote
  7423. M-right Demote
  7424. M-S-up Move entry/item up
  7425. M-S-down Move entry/item down
  7426. M-S-left Promote subtree
  7427. M-S-right Demote subtree
  7428. M-q Fill paragraph and items like in Org-mode
  7429. C-c ^ Sort entries
  7430. C-c - Cycle list bullet
  7431. TAB Cycle item visibility
  7432. M-RET Insert new heading/item
  7433. S-M-RET Insert new TODO heading / Checkbox item
  7434. C-c C-c Set tags / toggle checkbox"
  7435. nil " OrgStruct" nil
  7436. (org-load-modules-maybe)
  7437. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7438. ;;;###autoload
  7439. (defun turn-on-orgstruct ()
  7440. "Unconditionally turn on `orgstruct-mode'."
  7441. (orgstruct-mode 1))
  7442. (defun orgstruct++-mode (&optional arg)
  7443. "Toggle `orgstruct-mode', the enhanced version of it.
  7444. In addition to setting orgstruct-mode, this also exports all indentation
  7445. and autofilling variables from org-mode into the buffer. It will also
  7446. recognize item context in multiline items.
  7447. Note that turning off orgstruct-mode will *not* remove the
  7448. indentation/paragraph settings. This can only be done by refreshing the
  7449. major mode, for example with \\[normal-mode]."
  7450. (interactive "P")
  7451. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7452. (if (< arg 1)
  7453. (orgstruct-mode -1)
  7454. (orgstruct-mode 1)
  7455. (let (var val)
  7456. (mapc
  7457. (lambda (x)
  7458. (when (string-match
  7459. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7460. (symbol-name (car x)))
  7461. (setq var (car x) val (nth 1 x))
  7462. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7463. org-local-vars)
  7464. (org-set-local 'orgstruct-is-++ t))))
  7465. (defvar orgstruct-is-++ nil
  7466. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7467. (make-variable-buffer-local 'orgstruct-is-++)
  7468. ;;;###autoload
  7469. (defun turn-on-orgstruct++ ()
  7470. "Unconditionally turn on `orgstruct++-mode'."
  7471. (orgstruct++-mode 1))
  7472. (defun orgstruct-error ()
  7473. "Error when there is no default binding for a structure key."
  7474. (interactive)
  7475. (error "This key has no function outside structure elements"))
  7476. (defun orgstruct-setup ()
  7477. "Setup orgstruct keymaps."
  7478. (let ((nfunc 0)
  7479. (bindings
  7480. (list
  7481. '([(meta up)] org-metaup)
  7482. '([(meta down)] org-metadown)
  7483. '([(meta left)] org-metaleft)
  7484. '([(meta right)] org-metaright)
  7485. '([(meta shift up)] org-shiftmetaup)
  7486. '([(meta shift down)] org-shiftmetadown)
  7487. '([(meta shift left)] org-shiftmetaleft)
  7488. '([(meta shift right)] org-shiftmetaright)
  7489. '([?\e (up)] org-metaup)
  7490. '([?\e (down)] org-metadown)
  7491. '([?\e (left)] org-metaleft)
  7492. '([?\e (right)] org-metaright)
  7493. '([?\e (shift up)] org-shiftmetaup)
  7494. '([?\e (shift down)] org-shiftmetadown)
  7495. '([?\e (shift left)] org-shiftmetaleft)
  7496. '([?\e (shift right)] org-shiftmetaright)
  7497. '([(shift up)] org-shiftup)
  7498. '([(shift down)] org-shiftdown)
  7499. '([(shift left)] org-shiftleft)
  7500. '([(shift right)] org-shiftright)
  7501. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7502. '("\M-q" fill-paragraph)
  7503. '("\C-c^" org-sort)
  7504. '("\C-c-" org-cycle-list-bullet)))
  7505. elt key fun cmd)
  7506. (while (setq elt (pop bindings))
  7507. (setq nfunc (1+ nfunc))
  7508. (setq key (org-key (car elt))
  7509. fun (nth 1 elt)
  7510. cmd (orgstruct-make-binding fun nfunc key))
  7511. (org-defkey orgstruct-mode-map key cmd))
  7512. ;; Special treatment needed for TAB and RET
  7513. (org-defkey orgstruct-mode-map [(tab)]
  7514. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7515. (org-defkey orgstruct-mode-map "\C-i"
  7516. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7517. (org-defkey orgstruct-mode-map "\M-\C-m"
  7518. (orgstruct-make-binding 'org-insert-heading 105
  7519. "\M-\C-m" [(meta return)]))
  7520. (org-defkey orgstruct-mode-map [(meta return)]
  7521. (orgstruct-make-binding 'org-insert-heading 106
  7522. [(meta return)] "\M-\C-m"))
  7523. (org-defkey orgstruct-mode-map [(shift meta return)]
  7524. (orgstruct-make-binding 'org-insert-todo-heading 107
  7525. [(meta return)] "\M-\C-m"))
  7526. (org-defkey orgstruct-mode-map "\e\C-m"
  7527. (orgstruct-make-binding 'org-insert-heading 108
  7528. "\e\C-m" [?\e (return)]))
  7529. (org-defkey orgstruct-mode-map [?\e (return)]
  7530. (orgstruct-make-binding 'org-insert-heading 109
  7531. [?\e (return)] "\e\C-m"))
  7532. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7533. (orgstruct-make-binding 'org-insert-todo-heading 110
  7534. [?\e (return)] "\e\C-m"))
  7535. (unless org-local-vars
  7536. (setq org-local-vars (org-get-local-variables)))
  7537. t))
  7538. (defun orgstruct-make-binding (fun n &rest keys)
  7539. "Create a function for binding in the structure minor mode.
  7540. FUN is the command to call inside a table. N is used to create a unique
  7541. command name. KEYS are keys that should be checked in for a command
  7542. to execute outside of tables."
  7543. (eval
  7544. (list 'defun
  7545. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7546. '(arg)
  7547. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7548. "Outside of structure, run the binding of `"
  7549. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7550. "'.")
  7551. '(interactive "p")
  7552. (list 'if
  7553. `(org-context-p 'headline 'item
  7554. (and orgstruct-is-++
  7555. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7556. 'item-body))
  7557. (list 'org-run-like-in-org-mode (list 'quote fun))
  7558. (list 'let '(orgstruct-mode)
  7559. (list 'call-interactively
  7560. (append '(or)
  7561. (mapcar (lambda (k)
  7562. (list 'key-binding k))
  7563. keys)
  7564. '('orgstruct-error))))))))
  7565. (defun org-context-p (&rest contexts)
  7566. "Check if local context is any of CONTEXTS.
  7567. Possible values in the list of contexts are `table', `headline', and `item'."
  7568. (let ((pos (point)))
  7569. (goto-char (point-at-bol))
  7570. (prog1 (or (and (memq 'table contexts)
  7571. (looking-at "[ \t]*|"))
  7572. (and (memq 'headline contexts)
  7573. (looking-at org-outline-regexp))
  7574. (and (memq 'item contexts)
  7575. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7576. (and (memq 'item-body contexts)
  7577. (org-in-item-p)))
  7578. (goto-char pos))))
  7579. (defun org-get-local-variables ()
  7580. "Return a list of all local variables in an org-mode buffer."
  7581. (let (varlist)
  7582. (with-current-buffer (get-buffer-create "*Org tmp*")
  7583. (erase-buffer)
  7584. (org-mode)
  7585. (setq varlist (buffer-local-variables)))
  7586. (kill-buffer "*Org tmp*")
  7587. (delq nil
  7588. (mapcar
  7589. (lambda (x)
  7590. (setq x
  7591. (if (symbolp x)
  7592. (list x)
  7593. (list (car x) (list 'quote (cdr x)))))
  7594. (if (string-match
  7595. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7596. (symbol-name (car x)))
  7597. x nil))
  7598. varlist))))
  7599. (defun org-clone-local-variables (from-buffer &optional regexp)
  7600. "Clone local variables from FROM-BUFFER.
  7601. Optional argument REGEXP selects variables to clone."
  7602. (mapc
  7603. (lambda (pair)
  7604. (and (symbolp (car pair))
  7605. (or (null regexp)
  7606. (string-match regexp (symbol-name (car pair))))
  7607. (set (make-local-variable (car pair))
  7608. (cdr pair))))
  7609. (buffer-local-variables from-buffer)))
  7610. ;;;###autoload
  7611. (defun org-run-like-in-org-mode (cmd)
  7612. "Run a command, pretending that the current buffer is in Org-mode.
  7613. This will temporarily bind local variables that are typically bound in
  7614. Org-mode to the values they have in Org-mode, and then interactively
  7615. call CMD."
  7616. (org-load-modules-maybe)
  7617. (unless org-local-vars
  7618. (setq org-local-vars (org-get-local-variables)))
  7619. (eval (list 'let org-local-vars
  7620. (list 'call-interactively (list 'quote cmd)))))
  7621. ;;;; Archiving
  7622. (defun org-get-category (&optional pos force-refresh)
  7623. "Get the category applying to position POS."
  7624. (if force-refresh (org-refresh-category-properties))
  7625. (let ((pos (or pos (point))))
  7626. (or (get-text-property pos 'org-category)
  7627. (progn (org-refresh-category-properties)
  7628. (get-text-property pos 'org-category)))))
  7629. (defun org-refresh-category-properties ()
  7630. "Refresh category text properties in the buffer."
  7631. (let ((def-cat (cond
  7632. ((null org-category)
  7633. (if buffer-file-name
  7634. (file-name-sans-extension
  7635. (file-name-nondirectory buffer-file-name))
  7636. "???"))
  7637. ((symbolp org-category) (symbol-name org-category))
  7638. (t org-category)))
  7639. beg end cat pos optionp)
  7640. (org-unmodified
  7641. (save-excursion
  7642. (save-restriction
  7643. (widen)
  7644. (goto-char (point-min))
  7645. (put-text-property (point) (point-max) 'org-category def-cat)
  7646. (while (re-search-forward
  7647. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7648. (setq pos (match-end 0)
  7649. optionp (equal (char-after (match-beginning 0)) ?#)
  7650. cat (org-trim (match-string 2)))
  7651. (if optionp
  7652. (setq beg (point-at-bol) end (point-max))
  7653. (org-back-to-heading t)
  7654. (setq beg (point) end (org-end-of-subtree t t)))
  7655. (put-text-property beg end 'org-category cat)
  7656. (put-text-property beg end 'org-category-position beg)
  7657. (goto-char pos)))))))
  7658. ;;;; Link Stuff
  7659. ;;; Link abbreviations
  7660. (defun org-link-expand-abbrev (link)
  7661. "Apply replacements as defined in `org-link-abbrev-alist."
  7662. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7663. (let* ((key (match-string 1 link))
  7664. (as (or (assoc key org-link-abbrev-alist-local)
  7665. (assoc key org-link-abbrev-alist)))
  7666. (tag (and (match-end 2) (match-string 3 link)))
  7667. rpl)
  7668. (if (not as)
  7669. link
  7670. (setq rpl (cdr as))
  7671. (cond
  7672. ((symbolp rpl) (funcall rpl tag))
  7673. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7674. ((string-match "%h" rpl)
  7675. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7676. (t (concat rpl tag)))))
  7677. link))
  7678. ;;; Storing and inserting links
  7679. (defvar org-insert-link-history nil
  7680. "Minibuffer history for links inserted with `org-insert-link'.")
  7681. (defvar org-stored-links nil
  7682. "Contains the links stored with `org-store-link'.")
  7683. (defvar org-store-link-plist nil
  7684. "Plist with info about the most recently link created with `org-store-link'.")
  7685. (defvar org-link-protocols nil
  7686. "Link protocols added to Org-mode using `org-add-link-type'.")
  7687. (defvar org-store-link-functions nil
  7688. "List of functions that are called to create and store a link.
  7689. Each function will be called in turn until one returns a non-nil
  7690. value. Each function should check if it is responsible for creating
  7691. this link (for example by looking at the major mode).
  7692. If not, it must exit and return nil.
  7693. If yes, it should return a non-nil value after a calling
  7694. `org-store-link-props' with a list of properties and values.
  7695. Special properties are:
  7696. :type The link prefix, like \"http\". This must be given.
  7697. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7698. This is obligatory as well.
  7699. :description Optional default description for the second pair
  7700. of brackets in an Org-mode link. The user can still change
  7701. this when inserting this link into an Org-mode buffer.
  7702. In addition to these, any additional properties can be specified
  7703. and then used in remember templates.")
  7704. (defun org-add-link-type (type &optional follow export)
  7705. "Add TYPE to the list of `org-link-types'.
  7706. Re-compute all regular expressions depending on `org-link-types'
  7707. FOLLOW and EXPORT are two functions.
  7708. FOLLOW should take the link path as the single argument and do whatever
  7709. is necessary to follow the link, for example find a file or display
  7710. a mail message.
  7711. EXPORT should format the link path for export to one of the export formats.
  7712. It should be a function accepting three arguments:
  7713. path the path of the link, the text after the prefix (like \"http:\")
  7714. desc the description of the link, if any, or a description added by
  7715. org-export-normalize-links if there is none
  7716. format the export format, a symbol like `html' or `latex' or `ascii'..
  7717. The function may use the FORMAT information to return different values
  7718. depending on the format. The return value will be put literally into
  7719. the exported file. If the return value is nil, this means Org should
  7720. do what it normally does with links which do not have EXPORT defined.
  7721. Org-mode has a built-in default for exporting links. If you are happy with
  7722. this default, there is no need to define an export function for the link
  7723. type. For a simple example of an export function, see `org-bbdb.el'."
  7724. (add-to-list 'org-link-types type t)
  7725. (org-make-link-regexps)
  7726. (if (assoc type org-link-protocols)
  7727. (setcdr (assoc type org-link-protocols) (list follow export))
  7728. (push (list type follow export) org-link-protocols)))
  7729. (defvar org-agenda-buffer-name)
  7730. ;;;###autoload
  7731. (defun org-store-link (arg)
  7732. "\\<org-mode-map>Store an org-link to the current location.
  7733. This link is added to `org-stored-links' and can later be inserted
  7734. into an org-buffer with \\[org-insert-link].
  7735. For some link types, a prefix arg is interpreted:
  7736. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7737. For file links, arg negates `org-context-in-file-links'."
  7738. (interactive "P")
  7739. (org-load-modules-maybe)
  7740. (setq org-store-link-plist nil) ; reset
  7741. (org-with-limited-levels
  7742. (let (link cpltxt desc description search txt custom-id agenda-link)
  7743. (cond
  7744. ((run-hook-with-args-until-success 'org-store-link-functions)
  7745. (setq link (plist-get org-store-link-plist :link)
  7746. desc (or (plist-get org-store-link-plist :description) link)))
  7747. ((equal (buffer-name) "*Org Edit Src Example*")
  7748. (let (label gc)
  7749. (while (or (not label)
  7750. (save-excursion
  7751. (save-restriction
  7752. (widen)
  7753. (goto-char (point-min))
  7754. (re-search-forward
  7755. (regexp-quote (format org-coderef-label-format label))
  7756. nil t))))
  7757. (when label (message "Label exists already") (sit-for 2))
  7758. (setq label (read-string "Code line label: " label)))
  7759. (end-of-line 1)
  7760. (setq link (format org-coderef-label-format label))
  7761. (setq gc (- 79 (length link)))
  7762. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7763. (insert link)
  7764. (setq link (concat "(" label ")") desc nil)))
  7765. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7766. ;; We are in the agenda, link to referenced location
  7767. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7768. (get-text-property (point) 'org-marker))))
  7769. (when m
  7770. (org-with-point-at m
  7771. (setq agenda-link
  7772. (if (org-called-interactively-p 'any)
  7773. (call-interactively 'org-store-link)
  7774. (org-store-link nil)))))))
  7775. ((eq major-mode 'calendar-mode)
  7776. (let ((cd (calendar-cursor-to-date)))
  7777. (setq link
  7778. (format-time-string
  7779. (car org-time-stamp-formats)
  7780. (apply 'encode-time
  7781. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7782. nil nil nil))))
  7783. (org-store-link-props :type "calendar" :date cd)))
  7784. ((eq major-mode 'w3-mode)
  7785. (setq cpltxt (if (and (buffer-name)
  7786. (not (string-match "Untitled" (buffer-name))))
  7787. (buffer-name)
  7788. (url-view-url t))
  7789. link (org-make-link (url-view-url t)))
  7790. (org-store-link-props :type "w3" :url (url-view-url t)))
  7791. ((eq major-mode 'w3m-mode)
  7792. (setq cpltxt (or w3m-current-title w3m-current-url)
  7793. link (org-make-link w3m-current-url))
  7794. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7795. ((setq search (run-hook-with-args-until-success
  7796. 'org-create-file-search-functions))
  7797. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7798. "::" search))
  7799. (setq cpltxt (or description link)))
  7800. ((eq major-mode 'image-mode)
  7801. (setq cpltxt (concat "file:"
  7802. (abbreviate-file-name buffer-file-name))
  7803. link (org-make-link cpltxt))
  7804. (org-store-link-props :type "image" :file buffer-file-name))
  7805. ((eq major-mode 'dired-mode)
  7806. ;; link to the file in the current line
  7807. (let ((file (dired-get-filename nil t)))
  7808. (setq file (if file
  7809. (abbreviate-file-name
  7810. (expand-file-name (dired-get-filename nil t)))
  7811. ;; otherwise, no file so use current directory.
  7812. default-directory))
  7813. (setq cpltxt (concat "file:" file)
  7814. link (org-make-link cpltxt))))
  7815. ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
  7816. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7817. (cond
  7818. ((org-in-regexp "<<\\(.*?\\)>>")
  7819. (setq cpltxt
  7820. (concat "file:"
  7821. (abbreviate-file-name
  7822. (buffer-file-name (buffer-base-buffer)))
  7823. "::" (match-string 1))
  7824. link (org-make-link cpltxt)))
  7825. ((and (featurep 'org-id)
  7826. (or (eq org-link-to-org-use-id t)
  7827. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7828. (org-called-interactively-p 'any))
  7829. (and (eq org-link-to-org-use-id
  7830. 'create-if-interactive-and-no-custom-id)
  7831. (org-called-interactively-p 'any)
  7832. (not custom-id))
  7833. (and org-link-to-org-use-id
  7834. (org-entry-get nil "ID"))))
  7835. ;; We can make a link using the ID.
  7836. (setq link (condition-case nil
  7837. (prog1 (org-id-store-link)
  7838. (setq desc (plist-get org-store-link-plist
  7839. :description)))
  7840. (error
  7841. ;; probably before first headline, link to file only
  7842. (concat "file:"
  7843. (abbreviate-file-name
  7844. (buffer-file-name (buffer-base-buffer))))))))
  7845. (t
  7846. ;; Just link to current headline
  7847. (setq cpltxt (concat "file:"
  7848. (abbreviate-file-name
  7849. (buffer-file-name (buffer-base-buffer)))))
  7850. ;; Add a context search string
  7851. (when (org-xor org-context-in-file-links arg)
  7852. (setq txt (cond
  7853. ((org-at-heading-p) nil)
  7854. ((org-region-active-p)
  7855. (buffer-substring (region-beginning) (region-end)))
  7856. (t nil)))
  7857. (when (or (null txt) (string-match "\\S-" txt))
  7858. (setq cpltxt
  7859. (concat cpltxt "::"
  7860. (condition-case nil
  7861. (org-make-org-heading-search-string txt)
  7862. (error "")))
  7863. desc (or (nth 4 (ignore-errors
  7864. (org-heading-components))) "NONE"))))
  7865. (if (string-match "::\\'" cpltxt)
  7866. (setq cpltxt (substring cpltxt 0 -2)))
  7867. (setq link (org-make-link cpltxt)))))
  7868. ((buffer-file-name (buffer-base-buffer))
  7869. ;; Just link to this file here.
  7870. (setq cpltxt (concat "file:"
  7871. (abbreviate-file-name
  7872. (buffer-file-name (buffer-base-buffer)))))
  7873. ;; Add a context string
  7874. (when (org-xor org-context-in-file-links arg)
  7875. (setq txt (if (org-region-active-p)
  7876. (buffer-substring (region-beginning) (region-end))
  7877. (buffer-substring (point-at-bol) (point-at-eol))))
  7878. ;; Only use search option if there is some text.
  7879. (when (string-match "\\S-" txt)
  7880. (setq cpltxt
  7881. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7882. desc "NONE")))
  7883. (setq link (org-make-link cpltxt)))
  7884. ((org-called-interactively-p 'interactive)
  7885. (error "Cannot link to a buffer which is not visiting a file"))
  7886. (t (setq link nil)))
  7887. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7888. (setq link (or link cpltxt)
  7889. desc (or desc cpltxt))
  7890. (if (equal desc "NONE") (setq desc nil))
  7891. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7892. (progn
  7893. (setq org-stored-links
  7894. (cons (list link desc) org-stored-links))
  7895. (message "Stored: %s" (or desc link))
  7896. (when custom-id
  7897. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7898. "::#" custom-id))
  7899. (setq org-stored-links
  7900. (cons (list link desc) org-stored-links))))
  7901. (or agenda-link (and link (org-make-link-string link desc)))))))
  7902. (defun org-store-link-props (&rest plist)
  7903. "Store link properties, extract names and addresses."
  7904. (let (x adr)
  7905. (when (setq x (plist-get plist :from))
  7906. (setq adr (mail-extract-address-components x))
  7907. (setq plist (plist-put plist :fromname (car adr)))
  7908. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7909. (when (setq x (plist-get plist :to))
  7910. (setq adr (mail-extract-address-components x))
  7911. (setq plist (plist-put plist :toname (car adr)))
  7912. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7913. (let ((from (plist-get plist :from))
  7914. (to (plist-get plist :to)))
  7915. (when (and from to org-from-is-user-regexp)
  7916. (setq plist
  7917. (plist-put plist :fromto
  7918. (if (string-match org-from-is-user-regexp from)
  7919. (concat "to %t")
  7920. (concat "from %f"))))))
  7921. (setq org-store-link-plist plist))
  7922. (defun org-add-link-props (&rest plist)
  7923. "Add these properties to the link property list."
  7924. (let (key value)
  7925. (while plist
  7926. (setq key (pop plist) value (pop plist))
  7927. (setq org-store-link-plist
  7928. (plist-put org-store-link-plist key value)))))
  7929. (defun org-email-link-description (&optional fmt)
  7930. "Return the description part of an email link.
  7931. This takes information from `org-store-link-plist' and formats it
  7932. according to FMT (default from `org-email-link-description-format')."
  7933. (setq fmt (or fmt org-email-link-description-format))
  7934. (let* ((p org-store-link-plist)
  7935. (to (plist-get p :toaddress))
  7936. (from (plist-get p :fromaddress))
  7937. (table
  7938. (list
  7939. (cons "%c" (plist-get p :fromto))
  7940. (cons "%F" (plist-get p :from))
  7941. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7942. (cons "%T" (plist-get p :to))
  7943. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7944. (cons "%s" (plist-get p :subject))
  7945. (cons "%d" (plist-get p :date))
  7946. (cons "%m" (plist-get p :message-id)))))
  7947. (when (string-match "%c" fmt)
  7948. ;; Check if the user wrote this message
  7949. (if (and org-from-is-user-regexp from to
  7950. (save-match-data (string-match org-from-is-user-regexp from)))
  7951. (setq fmt (replace-match "to %t" t t fmt))
  7952. (setq fmt (replace-match "from %f" t t fmt))))
  7953. (org-replace-escapes fmt table)))
  7954. (defun org-make-org-heading-search-string (&optional string heading)
  7955. "Make search string for STRING or current headline."
  7956. (interactive)
  7957. (let ((s (or string (org-get-heading)))
  7958. (lines org-context-in-file-links))
  7959. (unless (and string (not heading))
  7960. ;; We are using a headline, clean up garbage in there.
  7961. (if (string-match org-todo-regexp s)
  7962. (setq s (replace-match "" t t s)))
  7963. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7964. (setq s (replace-match "" t t s)))
  7965. (setq s (org-trim s))
  7966. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7967. org-comment-string "\\)") s)
  7968. (setq s (replace-match "" t t s)))
  7969. (while (string-match org-ts-regexp s)
  7970. (setq s (replace-match "" t t s))))
  7971. (or string (setq s (concat "*" s))) ; Add * for headlines
  7972. (when (and string (integerp lines) (> lines 0))
  7973. (let ((slines (org-split-string s "\n")))
  7974. (when (< lines (length slines))
  7975. (setq s (mapconcat
  7976. 'identity
  7977. (reverse (nthcdr (- (length slines) lines)
  7978. (reverse slines))) "\n")))))
  7979. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7980. (defun org-make-link (&rest strings)
  7981. "Concatenate STRINGS."
  7982. (apply 'concat strings))
  7983. (defun org-make-link-string (link &optional description)
  7984. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7985. (unless (string-match "\\S-" link)
  7986. (error "Empty link"))
  7987. (when (and description
  7988. (stringp description)
  7989. (not (string-match "\\S-" description)))
  7990. (setq description nil))
  7991. (when (stringp description)
  7992. ;; Remove brackets from the description, they are fatal.
  7993. (while (string-match "\\[" description)
  7994. (setq description (replace-match "{" t t description)))
  7995. (while (string-match "\\]" description)
  7996. (setq description (replace-match "}" t t description))))
  7997. (when (equal link description)
  7998. ;; No description needed, it is identical
  7999. (setq description nil))
  8000. (when (and (not description)
  8001. (not (string-match (org-image-file-name-regexp) link))
  8002. (not (equal link (org-link-escape link))))
  8003. (setq description (org-extract-attributes link)))
  8004. (setq link
  8005. (cond ((string-match (org-image-file-name-regexp) link) link)
  8006. ((string-match org-link-types-re link)
  8007. (concat (match-string 1 link)
  8008. (org-link-escape (substring link (match-end 1)))))
  8009. (t (org-link-escape link))))
  8010. (concat "[[" link "]"
  8011. (if description (concat "[" description "]") "")
  8012. "]"))
  8013. (defconst org-link-escape-chars
  8014. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8015. "List of characters that should be escaped in link.
  8016. This is the list that is used for internal purposes.")
  8017. (defvar org-url-encoding-use-url-hexify nil)
  8018. (defconst org-link-escape-chars-browser
  8019. '(?\ )
  8020. "List of escapes for characters that are problematic in links.
  8021. This is the list that is used before handing over to the browser.")
  8022. (defun org-link-escape (text &optional table merge)
  8023. "Return percent escaped representation of TEXT.
  8024. TEXT is a string with the text to escape.
  8025. Optional argument TABLE is a list with characters that should be
  8026. escaped. When nil, `org-link-escape-chars' is used.
  8027. If optional argument MERGE is set, merge TABLE into
  8028. `org-link-escape-chars'."
  8029. (if (and org-url-encoding-use-url-hexify (not table))
  8030. (url-hexify-string text)
  8031. (cond
  8032. ((and table merge)
  8033. (mapc (lambda (defchr)
  8034. (unless (member defchr table)
  8035. (setq table (cons defchr table)))) org-link-escape-chars))
  8036. ((null table)
  8037. (setq table org-link-escape-chars)))
  8038. (mapconcat
  8039. (lambda (char)
  8040. (if (or (member char table)
  8041. (< char 32) (= char 37) (> char 126))
  8042. (mapconcat (lambda (sequence-element)
  8043. (format "%%%.2X" sequence-element))
  8044. (or (encode-coding-char char 'utf-8)
  8045. (error "Unable to percent escape character: %s"
  8046. (char-to-string char))) "")
  8047. (char-to-string char))) text "")))
  8048. (defun org-link-unescape (str)
  8049. "Unhex hexified Unicode strings as returned from the JavaScript function
  8050. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8051. (unless (and (null str) (string= "" str))
  8052. (let ((pos 0) (case-fold-search t) unhexed)
  8053. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8054. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8055. (setq str (replace-match unhexed t t str))
  8056. (setq pos (+ pos (length unhexed))))))
  8057. str)
  8058. (defun org-link-unescape-compound (hex)
  8059. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8060. Note: this function also decodes single byte encodings like
  8061. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8062. (save-match-data
  8063. (let* ((bytes (cdr (split-string hex "%")))
  8064. (ret "")
  8065. (eat 0)
  8066. (sum 0))
  8067. (while bytes
  8068. (let* ((val (string-to-number (pop bytes) 16))
  8069. (shift-xor
  8070. (if (= 0 eat)
  8071. (cond
  8072. ((>= val 252) (cons 6 252))
  8073. ((>= val 248) (cons 5 248))
  8074. ((>= val 240) (cons 4 240))
  8075. ((>= val 224) (cons 3 224))
  8076. ((>= val 192) (cons 2 192))
  8077. (t (cons 0 0)))
  8078. (cons 6 128))))
  8079. (if (>= val 192) (setq eat (car shift-xor)))
  8080. (setq val (logxor val (cdr shift-xor)))
  8081. (setq sum (+ (lsh sum (car shift-xor)) val))
  8082. (if (> eat 0) (setq eat (- eat 1)))
  8083. (cond
  8084. ((= 0 eat) ;multi byte
  8085. (setq ret (concat ret (org-char-to-string sum)))
  8086. (setq sum 0))
  8087. ((not bytes) ; single byte(s)
  8088. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8089. )) ;; end (while bytes
  8090. ret )))
  8091. (defun org-link-unescape-single-byte-sequence (hex)
  8092. "Unhexify hex-encoded single byte character sequences."
  8093. (mapconcat (lambda (byte)
  8094. (char-to-string (string-to-number byte 16)))
  8095. (cdr (split-string hex "%")) ""))
  8096. (defun org-xor (a b)
  8097. "Exclusive or."
  8098. (if a (not b) b))
  8099. (defun org-fixup-message-id-for-http (s)
  8100. "Replace special characters in a message id, so it can be used in an http query."
  8101. (when (string-match "%" s)
  8102. (setq s (mapconcat (lambda (c)
  8103. (if (eq c ?%)
  8104. "%25"
  8105. (char-to-string c)))
  8106. s "")))
  8107. (while (string-match "<" s)
  8108. (setq s (replace-match "%3C" t t s)))
  8109. (while (string-match ">" s)
  8110. (setq s (replace-match "%3E" t t s)))
  8111. (while (string-match "@" s)
  8112. (setq s (replace-match "%40" t t s)))
  8113. s)
  8114. ;;;###autoload
  8115. (defun org-insert-link-global ()
  8116. "Insert a link like Org-mode does.
  8117. This command can be called in any mode to insert a link in Org-mode syntax."
  8118. (interactive)
  8119. (org-load-modules-maybe)
  8120. (org-run-like-in-org-mode 'org-insert-link))
  8121. (defun org-insert-link (&optional complete-file link-location default-description)
  8122. "Insert a link. At the prompt, enter the link.
  8123. Completion can be used to insert any of the link protocol prefixes like
  8124. http or ftp in use.
  8125. The history can be used to select a link previously stored with
  8126. `org-store-link'. When the empty string is entered (i.e. if you just
  8127. press RET at the prompt), the link defaults to the most recently
  8128. stored link. As SPC triggers completion in the minibuffer, you need to
  8129. use M-SPC or C-q SPC to force the insertion of a space character.
  8130. You will also be prompted for a description, and if one is given, it will
  8131. be displayed in the buffer instead of the link.
  8132. If there is already a link at point, this command will allow you to edit link
  8133. and description parts.
  8134. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8135. be selected using completion. The path to the file will be relative to the
  8136. current directory if the file is in the current directory or a subdirectory.
  8137. Otherwise, the link will be the absolute path as completed in the minibuffer
  8138. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8139. option `org-link-file-path-type'.
  8140. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8141. the current directory or below.
  8142. With three \\[universal-argument] prefixes, negate the meaning of
  8143. `org-keep-stored-link-after-insertion'.
  8144. If `org-make-link-description-function' is non-nil, this function will be
  8145. called with the link target, and the result will be the default
  8146. link description.
  8147. If the LINK-LOCATION parameter is non-nil, this value will be
  8148. used as the link location instead of reading one interactively.
  8149. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8150. be used as the default description."
  8151. (interactive "P")
  8152. (let* ((wcf (current-window-configuration))
  8153. (region (if (org-region-active-p)
  8154. (buffer-substring (region-beginning) (region-end))))
  8155. (remove (and region (list (region-beginning) (region-end))))
  8156. (desc region)
  8157. tmphist ; byte-compile incorrectly complains about this
  8158. (link link-location)
  8159. entry file all-prefixes)
  8160. (cond
  8161. (link-location) ; specified by arg, just use it.
  8162. ((org-in-regexp org-bracket-link-regexp 1)
  8163. ;; We do have a link at point, and we are going to edit it.
  8164. (setq remove (list (match-beginning 0) (match-end 0)))
  8165. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8166. (setq link (read-string "Link: "
  8167. (org-link-unescape
  8168. (org-match-string-no-properties 1)))))
  8169. ((or (org-in-regexp org-angle-link-re)
  8170. (org-in-regexp org-plain-link-re))
  8171. ;; Convert to bracket link
  8172. (setq remove (list (match-beginning 0) (match-end 0))
  8173. link (read-string "Link: "
  8174. (org-remove-angle-brackets (match-string 0)))))
  8175. ((member complete-file '((4) (16)))
  8176. ;; Completing read for file names.
  8177. (setq link (org-file-complete-link complete-file)))
  8178. (t
  8179. ;; Read link, with completion for stored links.
  8180. (with-output-to-temp-buffer "*Org Links*"
  8181. (princ "Insert a link.
  8182. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8183. (when org-stored-links
  8184. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8185. (princ (mapconcat
  8186. (lambda (x)
  8187. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8188. (reverse org-stored-links) "\n"))))
  8189. (let ((cw (selected-window)))
  8190. (select-window (get-buffer-window "*Org Links*" 'visible))
  8191. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8192. (unless (pos-visible-in-window-p (point-max))
  8193. (org-fit-window-to-buffer))
  8194. (and (window-live-p cw) (select-window cw)))
  8195. ;; Fake a link history, containing the stored links.
  8196. (setq tmphist (append (mapcar 'car org-stored-links)
  8197. org-insert-link-history))
  8198. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8199. (mapcar 'car org-link-abbrev-alist)
  8200. org-link-types))
  8201. (unwind-protect
  8202. (progn
  8203. (setq link
  8204. (let ((org-completion-use-ido nil)
  8205. (org-completion-use-iswitchb nil))
  8206. (org-completing-read
  8207. "Link: "
  8208. (append
  8209. (mapcar (lambda (x) (list (concat x ":")))
  8210. all-prefixes)
  8211. (mapcar 'car org-stored-links))
  8212. nil nil nil
  8213. 'tmphist
  8214. (car (car org-stored-links)))))
  8215. (if (not (string-match "\\S-" link))
  8216. (error "No link selected"))
  8217. (if (or (member link all-prefixes)
  8218. (and (equal ":" (substring link -1))
  8219. (member (substring link 0 -1) all-prefixes)
  8220. (setq link (substring link 0 -1))))
  8221. (setq link (org-link-try-special-completion link))))
  8222. (set-window-configuration wcf)
  8223. (kill-buffer "*Org Links*"))
  8224. (setq entry (assoc link org-stored-links))
  8225. (or entry (push link org-insert-link-history))
  8226. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8227. (not org-keep-stored-link-after-insertion))
  8228. (setq org-stored-links (delq (assoc link org-stored-links)
  8229. org-stored-links)))
  8230. (setq desc (or desc (nth 1 entry)))))
  8231. (if (string-match org-plain-link-re link)
  8232. ;; URL-like link, normalize the use of angular brackets.
  8233. (setq link (org-make-link (org-remove-angle-brackets link))))
  8234. ;; Check if we are linking to the current file with a search option
  8235. ;; If yes, simplify the link by using only the search option.
  8236. (when (and buffer-file-name
  8237. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8238. (let* ((path (match-string 1 link))
  8239. (case-fold-search nil)
  8240. (search (match-string 2 link)))
  8241. (save-match-data
  8242. (if (equal (file-truename buffer-file-name) (file-truename path))
  8243. ;; We are linking to this same file, with a search option
  8244. (setq link search)))))
  8245. ;; Check if we can/should use a relative path. If yes, simplify the link
  8246. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8247. (let* ((type (match-string 1 link))
  8248. (path (match-string 2 link))
  8249. (origpath path)
  8250. (case-fold-search nil))
  8251. (cond
  8252. ((or (eq org-link-file-path-type 'absolute)
  8253. (equal complete-file '(16)))
  8254. (setq path (abbreviate-file-name (expand-file-name path))))
  8255. ((eq org-link-file-path-type 'noabbrev)
  8256. (setq path (expand-file-name path)))
  8257. ((eq org-link-file-path-type 'relative)
  8258. (setq path (file-relative-name path)))
  8259. (t
  8260. (save-match-data
  8261. (if (string-match (concat "^" (regexp-quote
  8262. (expand-file-name
  8263. (file-name-as-directory
  8264. default-directory))))
  8265. (expand-file-name path))
  8266. ;; We are linking a file with relative path name.
  8267. (setq path (substring (expand-file-name path)
  8268. (match-end 0)))
  8269. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8270. (setq link (concat type path))
  8271. (if (equal desc origpath)
  8272. (setq desc path))))
  8273. (if org-make-link-description-function
  8274. (setq desc (funcall org-make-link-description-function link desc))
  8275. (if default-description (setq desc default-description)))
  8276. (setq desc (read-string "Description: " desc))
  8277. (unless (string-match "\\S-" desc) (setq desc nil))
  8278. (if remove (apply 'delete-region remove))
  8279. (insert (org-make-link-string link desc))))
  8280. (defun org-link-try-special-completion (type)
  8281. "If there is completion support for link type TYPE, offer it."
  8282. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8283. (if (functionp fun)
  8284. (funcall fun)
  8285. (read-string "Link (no completion support): " (concat type ":")))))
  8286. (defun org-file-complete-link (&optional arg)
  8287. "Create a file link using completion."
  8288. (let (file link)
  8289. (setq file (read-file-name "File: "))
  8290. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8291. (pwd1 (file-name-as-directory (abbreviate-file-name
  8292. (expand-file-name ".")))))
  8293. (cond
  8294. ((equal arg '(16))
  8295. (setq link (org-make-link
  8296. "file:"
  8297. (abbreviate-file-name (expand-file-name file)))))
  8298. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8299. (setq link (org-make-link "file:" (match-string 1 file))))
  8300. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8301. (expand-file-name file))
  8302. (setq link (org-make-link
  8303. "file:" (match-string 1 (expand-file-name file)))))
  8304. (t (setq link (org-make-link "file:" file)))))
  8305. link))
  8306. (defun org-completing-read (&rest args)
  8307. "Completing-read with SPACE being a normal character."
  8308. (let ((enable-recursive-minibuffers t)
  8309. (minibuffer-local-completion-map
  8310. (copy-keymap minibuffer-local-completion-map)))
  8311. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8312. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8313. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8314. (apply 'org-icompleting-read args)))
  8315. (defun org-completing-read-no-i (&rest args)
  8316. (let (org-completion-use-ido org-completion-use-iswitchb)
  8317. (apply 'org-completing-read args)))
  8318. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8319. "Use iswitch as a completing-read replacement to choose from choices.
  8320. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8321. from."
  8322. (let* ((iswitchb-use-virtual-buffers nil)
  8323. (iswitchb-make-buflist-hook
  8324. (lambda ()
  8325. (setq iswitchb-temp-buflist choices))))
  8326. (iswitchb-read-buffer prompt)))
  8327. (defun org-icompleting-read (&rest args)
  8328. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8329. (org-without-partial-completion
  8330. (if (and org-completion-use-ido
  8331. (fboundp 'ido-completing-read)
  8332. (boundp 'ido-mode) ido-mode
  8333. (listp (second args)))
  8334. (let ((ido-enter-matching-directory nil))
  8335. (apply 'ido-completing-read (concat (car args))
  8336. (if (consp (car (nth 1 args)))
  8337. (mapcar 'car (nth 1 args))
  8338. (nth 1 args))
  8339. (cddr args)))
  8340. (if (and org-completion-use-iswitchb
  8341. (boundp 'iswitchb-mode) iswitchb-mode
  8342. (listp (second args)))
  8343. (apply 'org-iswitchb-completing-read (concat (car args))
  8344. (if (consp (car (nth 1 args)))
  8345. (mapcar 'car (nth 1 args))
  8346. (nth 1 args))
  8347. (cddr args))
  8348. (apply 'completing-read args)))))
  8349. (defun org-extract-attributes (s)
  8350. "Extract the attributes cookie from a string and set as text property."
  8351. (let (a attr (start 0) key value)
  8352. (save-match-data
  8353. (when (string-match "{{\\([^}]+\\)}}$" s)
  8354. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8355. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8356. (setq key (match-string 1 a) value (match-string 2 a)
  8357. start (match-end 0)
  8358. attr (plist-put attr (intern key) value))))
  8359. (org-add-props s nil 'org-attr attr))
  8360. s))
  8361. (defun org-extract-attributes-from-string (tag)
  8362. (let (key value attr)
  8363. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8364. (setq key (match-string 1 tag) value (match-string 2 tag)
  8365. tag (replace-match "" t t tag)
  8366. attr (plist-put attr (intern key) value)))
  8367. (cons tag attr)))
  8368. (defun org-attributes-to-string (plist)
  8369. "Format a property list into an HTML attribute list."
  8370. (let ((s "") key value)
  8371. (while plist
  8372. (setq key (pop plist) value (pop plist))
  8373. (and value
  8374. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8375. s))
  8376. ;;; Opening/following a link
  8377. (defvar org-link-search-failed nil)
  8378. (defvar org-open-link-functions nil
  8379. "Hook for functions finding a plain text link.
  8380. These functions must take a single argument, the link content.
  8381. They will be called for links that look like [[link text][description]]
  8382. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8383. like a filename (e.g. \"./blue.png\").
  8384. These functions will be called *before* Org attempts to resolve the
  8385. link by doing text searches in the current buffer - so if you want a
  8386. link \"[[target]]\" to still find \"<<target>>\", your function should
  8387. handle this as a special case.
  8388. When the function does handle the link, it must return a non-nil value.
  8389. If it decides that it is not responsible for this link, it must return
  8390. nil to indicate that that Org-mode can continue with other options
  8391. like exact and fuzzy text search.")
  8392. (defun org-next-link ()
  8393. "Move forward to the next link.
  8394. If the link is in hidden text, expose it."
  8395. (interactive)
  8396. (when (and org-link-search-failed (eq this-command last-command))
  8397. (goto-char (point-min))
  8398. (message "Link search wrapped back to beginning of buffer"))
  8399. (setq org-link-search-failed nil)
  8400. (let* ((pos (point))
  8401. (ct (org-context))
  8402. (a (assoc :link ct)))
  8403. (if a (goto-char (nth 2 a)))
  8404. (if (re-search-forward org-any-link-re nil t)
  8405. (progn
  8406. (goto-char (match-beginning 0))
  8407. (if (outline-invisible-p) (org-show-context)))
  8408. (goto-char pos)
  8409. (setq org-link-search-failed t)
  8410. (error "No further link found"))))
  8411. (defun org-previous-link ()
  8412. "Move backward to the previous link.
  8413. If the link is in hidden text, expose it."
  8414. (interactive)
  8415. (when (and org-link-search-failed (eq this-command last-command))
  8416. (goto-char (point-max))
  8417. (message "Link search wrapped back to end of buffer"))
  8418. (setq org-link-search-failed nil)
  8419. (let* ((pos (point))
  8420. (ct (org-context))
  8421. (a (assoc :link ct)))
  8422. (if a (goto-char (nth 1 a)))
  8423. (if (re-search-backward org-any-link-re nil t)
  8424. (progn
  8425. (goto-char (match-beginning 0))
  8426. (if (outline-invisible-p) (org-show-context)))
  8427. (goto-char pos)
  8428. (setq org-link-search-failed t)
  8429. (error "No further link found"))))
  8430. (defun org-translate-link (s)
  8431. "Translate a link string if a translation function has been defined."
  8432. (if (and org-link-translation-function
  8433. (fboundp org-link-translation-function)
  8434. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8435. (progn
  8436. (setq s (funcall org-link-translation-function
  8437. (match-string 1) (match-string 2)))
  8438. (concat (car s) ":" (cdr s)))
  8439. s))
  8440. (defun org-translate-link-from-planner (type path)
  8441. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8442. This is still an experimental function, your mileage may vary."
  8443. (cond
  8444. ((member type '("http" "https" "news" "ftp"))
  8445. ;; standard Internet links are the same.
  8446. nil)
  8447. ((and (equal type "irc") (string-match "^//" path))
  8448. ;; Planner has two / at the beginning of an irc link, we have 1.
  8449. ;; We should have zero, actually....
  8450. (setq path (substring path 1)))
  8451. ((and (equal type "lisp") (string-match "^/" path))
  8452. ;; Planner has a slash, we do not.
  8453. (setq type "elisp" path (substring path 1)))
  8454. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8455. ;; A typical message link. Planner has the id after the final slash,
  8456. ;; we separate it with a hash mark
  8457. (setq path (concat (match-string 1 path) "#"
  8458. (org-remove-angle-brackets (match-string 2 path)))))
  8459. )
  8460. (cons type path))
  8461. (defun org-find-file-at-mouse (ev)
  8462. "Open file link or URL at mouse."
  8463. (interactive "e")
  8464. (mouse-set-point ev)
  8465. (org-open-at-point 'in-emacs))
  8466. (defun org-open-at-mouse (ev)
  8467. "Open file link or URL at mouse.
  8468. See the docstring of `org-open-file' for details."
  8469. (interactive "e")
  8470. (mouse-set-point ev)
  8471. (if (eq major-mode 'org-agenda-mode)
  8472. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8473. (org-open-at-point))
  8474. (defvar org-window-config-before-follow-link nil
  8475. "The window configuration before following a link.
  8476. This is saved in case the need arises to restore it.")
  8477. (defvar org-open-link-marker (make-marker)
  8478. "Marker pointing to the location where `org-open-at-point; was called.")
  8479. ;;;###autoload
  8480. (defun org-open-at-point-global ()
  8481. "Follow a link like Org-mode does.
  8482. This command can be called in any mode to follow a link that has
  8483. Org-mode syntax."
  8484. (interactive)
  8485. (org-run-like-in-org-mode 'org-open-at-point))
  8486. ;;;###autoload
  8487. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8488. "Open a link in the string S, as if it was in Org-mode."
  8489. (interactive "sLink: \nP")
  8490. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8491. (with-temp-buffer
  8492. (let ((org-inhibit-startup t))
  8493. (org-mode)
  8494. (insert s)
  8495. (goto-char (point-min))
  8496. (when reference-buffer
  8497. (setq org-link-abbrev-alist-local
  8498. (with-current-buffer reference-buffer
  8499. org-link-abbrev-alist-local)))
  8500. (org-open-at-point arg reference-buffer)))))
  8501. (defvar org-open-at-point-functions nil
  8502. "Hook that is run when following a link at point.
  8503. Functions in this hook must return t if they identify and follow
  8504. a link at point. If they don't find anything interesting at point,
  8505. they must return nil.")
  8506. (defun org-open-at-point (&optional arg reference-buffer)
  8507. "Open link at or after point.
  8508. If there is no link at point, this function will search forward up to
  8509. the end of the current line.
  8510. Normally, files will be opened by an appropriate application. If the
  8511. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8512. With a double prefix argument, try to open outside of Emacs, in the
  8513. application the system uses for this file type."
  8514. (interactive "P")
  8515. ;; if in a code block, then open the block's results
  8516. (unless (call-interactively #'org-babel-open-src-block-result)
  8517. (org-load-modules-maybe)
  8518. (move-marker org-open-link-marker (point))
  8519. (setq org-window-config-before-follow-link (current-window-configuration))
  8520. (org-remove-occur-highlights nil nil t)
  8521. (cond
  8522. ((and (org-at-heading-p)
  8523. (not (org-in-regexp
  8524. (concat org-plain-link-re "\\|"
  8525. org-bracket-link-regexp "\\|"
  8526. org-angle-link-re "\\|"
  8527. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8528. (not (get-text-property (point) 'org-linked-text)))
  8529. (or (org-offer-links-in-entry arg)
  8530. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8531. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8532. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8533. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8534. (not (org-in-regexp org-bracket-link-regexp)))
  8535. (org-footnote-action))
  8536. (t
  8537. (let (type path link line search (pos (point)))
  8538. (catch 'match
  8539. (save-excursion
  8540. (skip-chars-forward "^]\n\r")
  8541. (when (org-in-regexp org-bracket-link-regexp 1)
  8542. (setq link (org-extract-attributes
  8543. (org-link-unescape (org-match-string-no-properties 1))))
  8544. (while (string-match " *\n *" link)
  8545. (setq link (replace-match " " t t link)))
  8546. (setq link (org-link-expand-abbrev link))
  8547. (cond
  8548. ((or (file-name-absolute-p link)
  8549. (string-match "^\\.\\.?/" link))
  8550. (setq type "file" path link))
  8551. ((string-match org-link-re-with-space3 link)
  8552. (setq type (match-string 1 link) path (match-string 2 link)))
  8553. (t (setq type "thisfile" path link)))
  8554. (throw 'match t)))
  8555. (when (get-text-property (point) 'org-linked-text)
  8556. (setq type "thisfile"
  8557. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8558. (1+ (point)) (point))
  8559. path (buffer-substring
  8560. (or (previous-single-property-change pos 'org-linked-text)
  8561. (point-min))
  8562. (or (next-single-property-change pos 'org-linked-text)
  8563. (point-max))))
  8564. (throw 'match t))
  8565. (save-excursion
  8566. (when (or (org-in-regexp org-angle-link-re)
  8567. (org-in-regexp org-plain-link-re))
  8568. (setq type (match-string 1)
  8569. path (org-link-unescape (match-string 2)))
  8570. (throw 'match t)))
  8571. (save-excursion
  8572. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8573. (setq type "tags"
  8574. path (match-string 1))
  8575. (while (string-match ":" path)
  8576. (setq path (replace-match "+" t t path)))
  8577. (throw 'match t)))
  8578. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8579. (setq type "tree-match"
  8580. path (match-string 1))
  8581. (throw 'match t)))
  8582. (unless path
  8583. (error "No link found"))
  8584. ;; switch back to reference buffer
  8585. ;; needed when if called in a temporary buffer through
  8586. ;; org-open-link-from-string
  8587. (with-current-buffer (or reference-buffer (current-buffer))
  8588. ;; Remove any trailing spaces in path
  8589. (if (string-match " +\\'" path)
  8590. (setq path (replace-match "" t t path)))
  8591. (if (and org-link-translation-function
  8592. (fboundp org-link-translation-function))
  8593. ;; Check if we need to translate the link
  8594. (let ((tmp (funcall org-link-translation-function type path)))
  8595. (setq type (car tmp) path (cdr tmp))))
  8596. (cond
  8597. ((assoc type org-link-protocols)
  8598. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8599. ((equal type "mailto")
  8600. (let ((cmd (car org-link-mailto-program))
  8601. (args (cdr org-link-mailto-program)) args1
  8602. (address path) (subject "") a)
  8603. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8604. (setq address (match-string 1 path)
  8605. subject (org-link-escape (match-string 2 path))))
  8606. (while args
  8607. (cond
  8608. ((not (stringp (car args))) (push (pop args) args1))
  8609. (t (setq a (pop args))
  8610. (if (string-match "%a" a)
  8611. (setq a (replace-match address t t a)))
  8612. (if (string-match "%s" a)
  8613. (setq a (replace-match subject t t a)))
  8614. (push a args1))))
  8615. (apply cmd (nreverse args1))))
  8616. ((member type '("http" "https" "ftp" "news"))
  8617. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8618. (org-link-escape
  8619. path org-link-escape-chars-browser)
  8620. path))))
  8621. ((string= type "doi")
  8622. (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
  8623. (org-link-escape
  8624. path org-link-escape-chars-browser)
  8625. path))))
  8626. ((member type '("message"))
  8627. (browse-url (concat type ":" path)))
  8628. ((string= type "tags")
  8629. (org-tags-view arg path))
  8630. ((string= type "tree-match")
  8631. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8632. ((string= type "file")
  8633. (if (string-match "::\\([0-9]+\\)\\'" path)
  8634. (setq line (string-to-number (match-string 1 path))
  8635. path (substring path 0 (match-beginning 0)))
  8636. (if (string-match "::\\(.+\\)\\'" path)
  8637. (setq search (match-string 1 path)
  8638. path (substring path 0 (match-beginning 0)))))
  8639. (if (string-match "[*?{]" (file-name-nondirectory path))
  8640. (dired path)
  8641. (org-open-file path arg line search)))
  8642. ((string= type "shell")
  8643. (let ((cmd path))
  8644. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8645. (string-match org-confirm-shell-link-not-regexp cmd))
  8646. (not org-confirm-shell-link-function)
  8647. (funcall org-confirm-shell-link-function
  8648. (format "Execute \"%s\" in shell? "
  8649. (org-add-props cmd nil
  8650. 'face 'org-warning))))
  8651. (progn
  8652. (message "Executing %s" cmd)
  8653. (shell-command cmd))
  8654. (error "Abort"))))
  8655. ((string= type "elisp")
  8656. (let ((cmd path))
  8657. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8658. (string-match org-confirm-elisp-link-not-regexp cmd))
  8659. (not org-confirm-elisp-link-function)
  8660. (funcall org-confirm-elisp-link-function
  8661. (format "Execute \"%s\" as elisp? "
  8662. (org-add-props cmd nil
  8663. 'face 'org-warning))))
  8664. (message "%s => %s" cmd
  8665. (if (equal (string-to-char cmd) ?\()
  8666. (eval (read cmd))
  8667. (call-interactively (read cmd))))
  8668. (error "Abort"))))
  8669. ((and (string= type "thisfile")
  8670. (run-hook-with-args-until-success
  8671. 'org-open-link-functions path)))
  8672. ((string= type "thisfile")
  8673. (if arg
  8674. (switch-to-buffer-other-window
  8675. (org-get-buffer-for-internal-link (current-buffer)))
  8676. (org-mark-ring-push))
  8677. (let ((cmd `(org-link-search
  8678. ,path
  8679. ,(cond ((equal arg '(4)) ''occur)
  8680. ((equal arg '(16)) ''org-occur)
  8681. (t nil))
  8682. ,pos)))
  8683. (condition-case nil (let ((org-link-search-inhibit-query t))
  8684. (eval cmd))
  8685. (error (progn (widen) (eval cmd))))))
  8686. (t
  8687. (browse-url-at-point)))))))
  8688. (move-marker org-open-link-marker nil)
  8689. (run-hook-with-args 'org-follow-link-hook)))
  8690. (defun org-offer-links-in-entry (&optional nth zero)
  8691. "Offer links in the current entry and follow the selected link.
  8692. If there is only one link, follow it immediately as well.
  8693. If NTH is an integer, immediately pick the NTH link found.
  8694. If ZERO is a string, check also this string for a link, and if
  8695. there is one, offer it as link number zero."
  8696. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8697. "\\(" org-angle-link-re "\\)\\|"
  8698. "\\(" org-plain-link-re "\\)"))
  8699. (cnt ?0)
  8700. (in-emacs (if (integerp nth) nil nth))
  8701. have-zero end links link c)
  8702. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8703. (push (match-string 0 zero) links)
  8704. (setq cnt (1- cnt) have-zero t))
  8705. (save-excursion
  8706. (org-back-to-heading t)
  8707. (setq end (save-excursion (outline-next-heading) (point)))
  8708. (while (re-search-forward re end t)
  8709. (push (match-string 0) links))
  8710. (setq links (org-uniquify (reverse links))))
  8711. (cond
  8712. ((null links)
  8713. (message "No links"))
  8714. ((equal (length links) 1)
  8715. (setq link (list (car links))))
  8716. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8717. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8718. (t ; we have to select a link
  8719. (save-excursion
  8720. (save-window-excursion
  8721. (delete-other-windows)
  8722. (with-output-to-temp-buffer "*Select Link*"
  8723. (mapc (lambda (l)
  8724. (if (not (string-match org-bracket-link-regexp l))
  8725. (princ (format "[%c] %s\n" (incf cnt)
  8726. (org-remove-angle-brackets l)))
  8727. (if (match-end 3)
  8728. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8729. (match-string 3 l) (match-string 1 l)))
  8730. (princ (format "[%c] %s\n" (incf cnt)
  8731. (match-string 1 l))))))
  8732. links))
  8733. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8734. (message "Select link to open, RET to open all:")
  8735. (setq c (read-char-exclusive))
  8736. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8737. (when (equal c ?q) (error "Abort"))
  8738. (if (equal c ?\C-m)
  8739. (setq link links)
  8740. (setq nth (- c ?0))
  8741. (if have-zero (setq nth (1+ nth)))
  8742. (unless (and (integerp nth) (>= (length links) nth))
  8743. (error "Invalid link selection"))
  8744. (setq link (list (nth (1- nth) links))))))
  8745. (if link
  8746. (let ((buf (current-buffer)))
  8747. (dolist (l link)
  8748. (org-open-link-from-string l in-emacs buf))
  8749. t)
  8750. nil)))
  8751. ;; Add special file links that specify the way of opening
  8752. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8753. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8754. (defun org-open-file-with-system (path)
  8755. "Open file at PATH using the system way of opening it."
  8756. (org-open-file path 'system))
  8757. (defun org-open-file-with-emacs (path)
  8758. "Open file at PATH in Emacs."
  8759. (org-open-file path 'emacs))
  8760. (defun org-remove-file-link-modifiers ()
  8761. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8762. (goto-char (point-min))
  8763. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8764. (org-if-unprotected
  8765. (replace-match "file:" t t))))
  8766. (eval-after-load "org-exp"
  8767. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8768. 'org-remove-file-link-modifiers))
  8769. ;;;; Time estimates
  8770. (defun org-get-effort (&optional pom)
  8771. "Get the effort estimate for the current entry."
  8772. (org-entry-get pom org-effort-property))
  8773. ;;; File search
  8774. (defvar org-create-file-search-functions nil
  8775. "List of functions to construct the right search string for a file link.
  8776. These functions are called in turn with point at the location to
  8777. which the link should point.
  8778. A function in the hook should first test if it would like to
  8779. handle this file type, for example by checking the `major-mode'
  8780. or the file extension. If it decides not to handle this file, it
  8781. should just return nil to give other functions a chance. If it
  8782. does handle the file, it must return the search string to be used
  8783. when following the link. The search string will be part of the
  8784. file link, given after a double colon, and `org-open-at-point'
  8785. will automatically search for it. If special measures must be
  8786. taken to make the search successful, another function should be
  8787. added to the companion hook `org-execute-file-search-functions',
  8788. which see.
  8789. A function in this hook may also use `setq' to set the variable
  8790. `description' to provide a suggestion for the descriptive text to
  8791. be used for this link when it gets inserted into an Org-mode
  8792. buffer with \\[org-insert-link].")
  8793. (defvar org-execute-file-search-functions nil
  8794. "List of functions to execute a file search triggered by a link.
  8795. Functions added to this hook must accept a single argument, the
  8796. search string that was part of the file link, the part after the
  8797. double colon. The function must first check if it would like to
  8798. handle this search, for example by checking the `major-mode' or
  8799. the file extension. If it decides not to handle this search, it
  8800. should just return nil to give other functions a chance. If it
  8801. does handle the search, it must return a non-nil value to keep
  8802. other functions from trying.
  8803. Each function can access the current prefix argument through the
  8804. variable `current-prefix-argument'. Note that a single prefix is
  8805. used to force opening a link in Emacs, so it may be good to only
  8806. use a numeric or double prefix to guide the search function.
  8807. In case this is needed, a function in this hook can also restore
  8808. the window configuration before `org-open-at-point' was called using:
  8809. (set-window-configuration org-window-config-before-follow-link)")
  8810. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8811. (defun org-link-search (s &optional type avoid-pos stealth)
  8812. "Search for a link search option.
  8813. If S is surrounded by forward slashes, it is interpreted as a
  8814. regular expression. In org-mode files, this will create an `org-occur'
  8815. sparse tree. In ordinary files, `occur' will be used to list matches.
  8816. If the current buffer is in `dired-mode', grep will be used to search
  8817. in all files. If AVOID-POS is given, ignore matches near that position.
  8818. When optional argument STEALTH is non-nil, do not modify
  8819. visibility around point, thus ignoring
  8820. `org-show-hierarchy-above', `org-show-following-heading' and
  8821. `org-show-siblings' variables."
  8822. (let ((case-fold-search t)
  8823. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8824. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8825. (append '(("") (" ") ("\t") ("\n"))
  8826. org-emphasis-alist)
  8827. "\\|") "\\)"))
  8828. (pos (point))
  8829. (pre nil) (post nil)
  8830. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8831. (cond
  8832. ;; First check if there are any special search functions
  8833. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8834. ;; Now try the builtin stuff
  8835. ((and (equal (string-to-char s0) ?#)
  8836. (> (length s0) 1)
  8837. (save-excursion
  8838. (goto-char (point-min))
  8839. (and
  8840. (re-search-forward
  8841. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8842. (setq type 'dedicated
  8843. pos (match-beginning 0))))
  8844. ;; There is an exact target for this
  8845. (goto-char pos)
  8846. (org-back-to-heading t)))
  8847. ((save-excursion
  8848. (goto-char (point-min))
  8849. (and
  8850. (re-search-forward
  8851. (concat "<<" (regexp-quote s0) ">>") nil t)
  8852. (setq type 'dedicated
  8853. pos (match-beginning 0))))
  8854. ;; There is an exact target for this
  8855. (goto-char pos))
  8856. ((and (string-match "^(\\(.*\\))$" s0)
  8857. (save-excursion
  8858. (goto-char (point-min))
  8859. (and
  8860. (re-search-forward
  8861. (concat "[^[]" (regexp-quote
  8862. (format org-coderef-label-format
  8863. (match-string 1 s0))))
  8864. nil t)
  8865. (setq type 'dedicated
  8866. pos (1+ (match-beginning 0))))))
  8867. ;; There is a coderef target for this
  8868. (goto-char pos))
  8869. ((string-match "^/\\(.*\\)/$" s)
  8870. ;; A regular expression
  8871. (cond
  8872. ((eq major-mode 'org-mode)
  8873. (org-occur (match-string 1 s)))
  8874. ;;((eq major-mode 'dired-mode)
  8875. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8876. (t (org-do-occur (match-string 1 s)))))
  8877. ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
  8878. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8879. (goto-char (point-min))
  8880. (cond
  8881. ((let (case-fold-search)
  8882. (re-search-forward (format org-complex-heading-regexp-format
  8883. (regexp-quote s))
  8884. nil t))
  8885. ;; OK, found a match
  8886. (setq type 'dedicated)
  8887. (goto-char (match-beginning 0)))
  8888. ((and (not org-link-search-inhibit-query)
  8889. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8890. (y-or-n-p "No match - create this as a new heading? "))
  8891. (goto-char (point-max))
  8892. (or (bolp) (newline))
  8893. (insert "* " s "\n")
  8894. (beginning-of-line 0))
  8895. (t
  8896. (goto-char pos)
  8897. (error "No match"))))
  8898. (t
  8899. ;; A normal search string
  8900. (when (equal (string-to-char s) ?*)
  8901. ;; Anchor on headlines, post may include tags.
  8902. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8903. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8904. s (substring s 1)))
  8905. (remove-text-properties
  8906. 0 (length s)
  8907. '(face nil mouse-face nil keymap nil fontified nil) s)
  8908. ;; Make a series of regular expressions to find a match
  8909. (setq words (org-split-string s "[ \n\r\t]+")
  8910. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8911. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8912. "\\)" markers)
  8913. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8914. re2a (concat "[ \t\r\n]" re2a_)
  8915. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8916. re4 (concat "[^a-zA-Z_]" re4_)
  8917. re1 (concat pre re2 post)
  8918. re3 (concat pre (if pre re4_ re4) post)
  8919. re5 (concat pre ".*" re4)
  8920. re2 (concat pre re2)
  8921. re2a (concat pre (if pre re2a_ re2a))
  8922. re4 (concat pre (if pre re4_ re4))
  8923. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8924. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8925. re5 "\\)"
  8926. ))
  8927. (cond
  8928. ((eq type 'org-occur) (org-occur reall))
  8929. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8930. (t (goto-char (point-min))
  8931. (setq type 'fuzzy)
  8932. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8933. (org-search-not-self 1 re1 nil t)
  8934. (org-search-not-self 1 re2 nil t)
  8935. (org-search-not-self 1 re2a nil t)
  8936. (org-search-not-self 1 re3 nil t)
  8937. (org-search-not-self 1 re4 nil t)
  8938. (org-search-not-self 1 re5 nil t)
  8939. )
  8940. (goto-char (match-beginning 1))
  8941. (goto-char pos)
  8942. (error "No match"))))))
  8943. (and (eq major-mode 'org-mode)
  8944. (not stealth)
  8945. (org-show-context 'link-search))
  8946. type))
  8947. (defun org-search-not-self (group &rest args)
  8948. "Execute `re-search-forward', but only accept matches that do not
  8949. enclose the position of `org-open-link-marker'."
  8950. (let ((m org-open-link-marker))
  8951. (catch 'exit
  8952. (while (apply 're-search-forward args)
  8953. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8954. (goto-char (match-end group))
  8955. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8956. (> (match-beginning 0) (marker-position m))
  8957. (< (match-end 0) (marker-position m)))
  8958. (save-match-data
  8959. (or (not (org-in-regexp
  8960. org-bracket-link-analytic-regexp 1))
  8961. (not (match-end 4)) ; no description
  8962. (and (<= (match-beginning 4) (point))
  8963. (>= (match-end 4) (point))))))
  8964. (throw 'exit (point))))))))
  8965. (defun org-get-buffer-for-internal-link (buffer)
  8966. "Return a buffer to be used for displaying the link target of internal links."
  8967. (cond
  8968. ((not org-display-internal-link-with-indirect-buffer)
  8969. buffer)
  8970. ((string-match "(Clone)$" (buffer-name buffer))
  8971. (message "Buffer is already a clone, not making another one")
  8972. ;; we also do not modify visibility in this case
  8973. buffer)
  8974. (t ; make a new indirect buffer for displaying the link
  8975. (let* ((bn (buffer-name buffer))
  8976. (ibn (concat bn "(Clone)"))
  8977. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8978. (with-current-buffer ib (org-overview))
  8979. ib))))
  8980. (defun org-do-occur (regexp &optional cleanup)
  8981. "Call the Emacs command `occur'.
  8982. If CLEANUP is non-nil, remove the printout of the regular expression
  8983. in the *Occur* buffer. This is useful if the regex is long and not useful
  8984. to read."
  8985. (occur regexp)
  8986. (when cleanup
  8987. (let ((cwin (selected-window)) win beg end)
  8988. (when (setq win (get-buffer-window "*Occur*"))
  8989. (select-window win))
  8990. (goto-char (point-min))
  8991. (when (re-search-forward "match[a-z]+" nil t)
  8992. (setq beg (match-end 0))
  8993. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8994. (setq end (1- (match-beginning 0)))))
  8995. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8996. (goto-char (point-min))
  8997. (select-window cwin))))
  8998. ;;; The mark ring for links jumps
  8999. (defvar org-mark-ring nil
  9000. "Mark ring for positions before jumps in Org-mode.")
  9001. (defvar org-mark-ring-last-goto nil
  9002. "Last position in the mark ring used to go back.")
  9003. ;; Fill and close the ring
  9004. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9005. (loop for i from 1 to org-mark-ring-length do
  9006. (push (make-marker) org-mark-ring))
  9007. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9008. org-mark-ring)
  9009. (defun org-mark-ring-push (&optional pos buffer)
  9010. "Put the current position or POS into the mark ring and rotate it."
  9011. (interactive)
  9012. (setq pos (or pos (point)))
  9013. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9014. (move-marker (car org-mark-ring)
  9015. (or pos (point))
  9016. (or buffer (current-buffer)))
  9017. (message "%s"
  9018. (substitute-command-keys
  9019. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9020. (defun org-mark-ring-goto (&optional n)
  9021. "Jump to the previous position in the mark ring.
  9022. With prefix arg N, jump back that many stored positions. When
  9023. called several times in succession, walk through the entire ring.
  9024. Org-mode commands jumping to a different position in the current file,
  9025. or to another Org-mode file, automatically push the old position
  9026. onto the ring."
  9027. (interactive "p")
  9028. (let (p m)
  9029. (if (eq last-command this-command)
  9030. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9031. (setq p org-mark-ring))
  9032. (setq org-mark-ring-last-goto p)
  9033. (setq m (car p))
  9034. (org-pop-to-buffer-same-window (marker-buffer m))
  9035. (goto-char m)
  9036. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9037. (defun org-remove-angle-brackets (s)
  9038. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9039. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9040. s)
  9041. (defun org-add-angle-brackets (s)
  9042. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9043. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9044. s)
  9045. (defun org-remove-double-quotes (s)
  9046. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9047. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9048. s)
  9049. ;;; Following specific links
  9050. (defun org-follow-timestamp-link ()
  9051. (cond
  9052. ((org-at-date-range-p t)
  9053. (let ((org-agenda-start-on-weekday)
  9054. (t1 (match-string 1))
  9055. (t2 (match-string 2)))
  9056. (setq t1 (time-to-days (org-time-string-to-time t1))
  9057. t2 (time-to-days (org-time-string-to-time t2)))
  9058. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9059. ((org-at-timestamp-p t)
  9060. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9061. (substring (match-string 1) 0 10)))
  9062. 1))
  9063. (t (error "This should not happen"))))
  9064. ;;; Following file links
  9065. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9066. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9067. (declare-function mailcap-mime-info
  9068. "mailcap" (string &optional request no-decode))
  9069. (defvar org-wait nil)
  9070. (defun org-open-file (path &optional in-emacs line search)
  9071. "Open the file at PATH.
  9072. First, this expands any special file name abbreviations. Then the
  9073. configuration variable `org-file-apps' is checked if it contains an
  9074. entry for this file type, and if yes, the corresponding command is launched.
  9075. If no application is found, Emacs simply visits the file.
  9076. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9077. With a double \\[universal-argument] \\[universal-argument] \
  9078. prefix arg, Org tries to avoid opening in Emacs
  9079. and to use an external application to visit the file.
  9080. Optional LINE specifies a line to go to, optional SEARCH a string
  9081. to search for. If LINE or SEARCH is given, the file will be
  9082. opened in Emacs, unless an entry from org-file-apps that makes
  9083. use of groups in a regexp matches.
  9084. If you want to change the way frames are used when following a
  9085. link, please customize `org-link-frame-setup'.
  9086. If the file does not exist, an error is thrown."
  9087. (let* ((file (if (equal path "")
  9088. buffer-file-name
  9089. (substitute-in-file-name (expand-file-name path))))
  9090. (file-apps (append org-file-apps (org-default-apps)))
  9091. (apps (org-remove-if
  9092. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9093. (apps-dlink (org-remove-if-not
  9094. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9095. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9096. (dirp (if remp nil (file-directory-p file)))
  9097. (file (if (and dirp org-open-directory-means-index-dot-org)
  9098. (concat (file-name-as-directory file) "index.org")
  9099. file))
  9100. (a-m-a-p (assq 'auto-mode apps))
  9101. (dfile (downcase file))
  9102. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9103. (link (cond ((and (eq line nil)
  9104. (eq search nil))
  9105. file)
  9106. (line
  9107. (concat file "::" (number-to-string line)))
  9108. (search
  9109. (concat file "::" search))))
  9110. (dlink (downcase link))
  9111. (old-buffer (current-buffer))
  9112. (old-pos (point))
  9113. (old-mode major-mode)
  9114. ext cmd link-match-data)
  9115. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9116. (setq ext (match-string 1 dfile))
  9117. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9118. (setq ext (match-string 1 dfile))))
  9119. (cond
  9120. ((member in-emacs '((16) system))
  9121. (setq cmd (cdr (assoc 'system apps))))
  9122. (in-emacs (setq cmd 'emacs))
  9123. (t
  9124. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9125. (and dirp (cdr (assoc 'directory apps)))
  9126. ; first, try matching against apps-dlink
  9127. ; if we get a match here, store the match data for later
  9128. (let ((match (assoc-default dlink apps-dlink
  9129. 'string-match)))
  9130. (if match
  9131. (progn (setq link-match-data (match-data))
  9132. match)
  9133. (progn (setq in-emacs (or in-emacs line search))
  9134. nil))) ; if we have no match in apps-dlink,
  9135. ; always open the file in emacs if line or search
  9136. ; is given (for backwards compatibility)
  9137. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9138. 'string-match)
  9139. (cdr (assoc ext apps))
  9140. (cdr (assoc t apps))))))
  9141. (when (eq cmd 'system)
  9142. (setq cmd (cdr (assoc 'system apps))))
  9143. (when (eq cmd 'default)
  9144. (setq cmd (cdr (assoc t apps))))
  9145. (when (eq cmd 'mailcap)
  9146. (require 'mailcap)
  9147. (mailcap-parse-mailcaps)
  9148. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9149. (command (mailcap-mime-info mime-type)))
  9150. (if (stringp command)
  9151. (setq cmd command)
  9152. (setq cmd 'emacs))))
  9153. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9154. (not (file-exists-p file))
  9155. (not org-open-non-existing-files))
  9156. (error "No such file: %s" file))
  9157. (cond
  9158. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9159. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9160. (while (string-match "['\"]%s['\"]" cmd)
  9161. (setq cmd (replace-match "%s" t t cmd)))
  9162. (while (string-match "%s" cmd)
  9163. (setq cmd (replace-match
  9164. (save-match-data
  9165. (shell-quote-argument
  9166. (convert-standard-filename file)))
  9167. t t cmd)))
  9168. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9169. (save-match-data
  9170. (let ((match-index 1)
  9171. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9172. (set-match-data link-match-data)
  9173. (while (<= match-index number-of-groups)
  9174. (let ((regex (concat "%" (number-to-string match-index)))
  9175. (replace-with (match-string match-index dlink)))
  9176. (while (string-match regex cmd)
  9177. (setq cmd (replace-match replace-with t t cmd))))
  9178. (setq match-index (+ match-index 1)))))
  9179. (save-window-excursion
  9180. (start-process-shell-command cmd nil cmd)
  9181. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9182. ))
  9183. ((or (stringp cmd)
  9184. (eq cmd 'emacs))
  9185. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9186. (widen)
  9187. (if line (org-goto-line line)
  9188. (if search (org-link-search search))))
  9189. ((consp cmd)
  9190. (let ((file (convert-standard-filename file)))
  9191. (save-match-data
  9192. (set-match-data link-match-data)
  9193. (eval cmd))))
  9194. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9195. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  9196. (or (not (equal old-buffer (current-buffer)))
  9197. (not (equal old-pos (point))))
  9198. (org-mark-ring-push old-pos old-buffer))))
  9199. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9200. "This function returns non-nil if `entry' uses a regular
  9201. expression which should be matched against the whole link by
  9202. org-open-file.
  9203. It assumes that is the case when the entry uses a regular
  9204. expression which has at least one grouping construct and the
  9205. action is either a lisp form or a command string containing
  9206. '%1', i.e. using at least one subexpression match as a
  9207. parameter."
  9208. (let ((selector (car entry))
  9209. (action (cdr entry)))
  9210. (if (stringp selector)
  9211. (and (> (regexp-opt-depth selector) 0)
  9212. (or (and (stringp action)
  9213. (string-match "%[0-9]" action))
  9214. (consp action)))
  9215. nil)))
  9216. (defun org-default-apps ()
  9217. "Return the default applications for this operating system."
  9218. (cond
  9219. ((eq system-type 'darwin)
  9220. org-file-apps-defaults-macosx)
  9221. ((eq system-type 'windows-nt)
  9222. org-file-apps-defaults-windowsnt)
  9223. (t org-file-apps-defaults-gnu)))
  9224. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9225. "Convert extensions to regular expressions in the cars of LIST.
  9226. Also, weed out any non-string entries, because the return value is used
  9227. only for regexp matching.
  9228. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9229. point to the symbol `emacs', indicating that the file should
  9230. be opened in Emacs."
  9231. (append
  9232. (delq nil
  9233. (mapcar (lambda (x)
  9234. (if (not (stringp (car x)))
  9235. nil
  9236. (if (string-match "\\W" (car x))
  9237. x
  9238. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9239. list))
  9240. (if add-auto-mode
  9241. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9242. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9243. (defun org-file-remote-p (file)
  9244. "Test whether FILE specifies a location on a remote system.
  9245. Return non-nil if the location is indeed remote.
  9246. For example, the filename \"/user@host:/foo\" specifies a location
  9247. on the system \"/user@host:\"."
  9248. (cond ((fboundp 'file-remote-p)
  9249. (file-remote-p file))
  9250. ((fboundp 'tramp-handle-file-remote-p)
  9251. (tramp-handle-file-remote-p file))
  9252. ((and (boundp 'ange-ftp-name-format)
  9253. (string-match (car ange-ftp-name-format) file))
  9254. t)
  9255. (t nil)))
  9256. ;;;; Refiling
  9257. (defun org-get-org-file ()
  9258. "Read a filename, with default directory `org-directory'."
  9259. (let ((default (or org-default-notes-file remember-data-file)))
  9260. (read-file-name (format "File name [%s]: " default)
  9261. (file-name-as-directory org-directory)
  9262. default)))
  9263. (defun org-notes-order-reversed-p ()
  9264. "Check if the current file should receive notes in reversed order."
  9265. (cond
  9266. ((not org-reverse-note-order) nil)
  9267. ((eq t org-reverse-note-order) t)
  9268. ((not (listp org-reverse-note-order)) nil)
  9269. (t (catch 'exit
  9270. (let ((all org-reverse-note-order)
  9271. entry)
  9272. (while (setq entry (pop all))
  9273. (if (string-match (car entry) buffer-file-name)
  9274. (throw 'exit (cdr entry))))
  9275. nil)))))
  9276. (defvar org-refile-target-table nil
  9277. "The list of refile targets, created by `org-refile'.")
  9278. (defvar org-agenda-new-buffers nil
  9279. "Buffers created to visit agenda files.")
  9280. (defvar org-refile-cache nil
  9281. "Cache for refile targets.")
  9282. (defvar org-refile-markers nil
  9283. "All the markers used for caching refile locations.")
  9284. (defun org-refile-marker (pos)
  9285. "Get a new refile marker, but only if caching is in use."
  9286. (if (not org-refile-use-cache)
  9287. pos
  9288. (let ((m (make-marker)))
  9289. (move-marker m pos)
  9290. (push m org-refile-markers)
  9291. m)))
  9292. (defun org-refile-cache-clear ()
  9293. "Clear the refile cache and disable all the markers."
  9294. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9295. (setq org-refile-markers nil)
  9296. (setq org-refile-cache nil)
  9297. (message "Refile cache has been cleared"))
  9298. (defun org-refile-cache-check-set (set)
  9299. "Check if all the markers in the cache still have live buffers."
  9300. (let (marker)
  9301. (catch 'exit
  9302. (while (and set (setq marker (nth 3 (pop set))))
  9303. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9304. (when (and marker (null (marker-buffer marker)))
  9305. (message "not found") (sit-for 3)
  9306. (throw 'exit nil)))
  9307. t)))
  9308. (defun org-refile-cache-put (set &rest identifiers)
  9309. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9310. (let* ((key (sha1 (prin1-to-string identifiers)))
  9311. (entry (assoc key org-refile-cache)))
  9312. (if entry
  9313. (setcdr entry set)
  9314. (push (cons key set) org-refile-cache))))
  9315. (defun org-refile-cache-get (&rest identifiers)
  9316. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9317. (cond
  9318. ((not org-refile-cache) nil)
  9319. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9320. (t
  9321. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9322. org-refile-cache))))
  9323. (and set (org-refile-cache-check-set set) set)))))
  9324. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9325. "Produce a table with refile targets."
  9326. (let ((case-fold-search nil)
  9327. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9328. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9329. targets tgs txt re files f desc descre fast-path-p level pos0)
  9330. (message "Getting targets...")
  9331. (with-current-buffer (or default-buffer (current-buffer))
  9332. (while (setq entry (pop entries))
  9333. (setq files (car entry) desc (cdr entry))
  9334. (setq fast-path-p nil)
  9335. (cond
  9336. ((null files) (setq files (list (current-buffer))))
  9337. ((eq files 'org-agenda-files)
  9338. (setq files (org-agenda-files 'unrestricted)))
  9339. ((and (symbolp files) (fboundp files))
  9340. (setq files (funcall files)))
  9341. ((and (symbolp files) (boundp files))
  9342. (setq files (symbol-value files))))
  9343. (if (stringp files) (setq files (list files)))
  9344. (cond
  9345. ((eq (car desc) :tag)
  9346. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9347. ((eq (car desc) :todo)
  9348. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9349. ((eq (car desc) :regexp)
  9350. (setq descre (cdr desc)))
  9351. ((eq (car desc) :level)
  9352. (setq descre (concat "^\\*\\{" (number-to-string
  9353. (if org-odd-levels-only
  9354. (1- (* 2 (cdr desc)))
  9355. (cdr desc)))
  9356. "\\}[ \t]")))
  9357. ((eq (car desc) :maxlevel)
  9358. (setq fast-path-p t)
  9359. (setq descre (concat "^\\*\\{1," (number-to-string
  9360. (if org-odd-levels-only
  9361. (1- (* 2 (cdr desc)))
  9362. (cdr desc)))
  9363. "\\}[ \t]")))
  9364. (t (error "Bad refiling target description %s" desc)))
  9365. (while (setq f (pop files))
  9366. (with-current-buffer
  9367. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9368. (or
  9369. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9370. (progn
  9371. (if (bufferp f) (setq f (buffer-file-name
  9372. (buffer-base-buffer f))))
  9373. (setq f (and f (expand-file-name f)))
  9374. (if (eq org-refile-use-outline-path 'file)
  9375. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9376. (save-excursion
  9377. (save-restriction
  9378. (widen)
  9379. (goto-char (point-min))
  9380. (while (re-search-forward descre nil t)
  9381. (goto-char (setq pos0 (point-at-bol)))
  9382. (catch 'next
  9383. (when org-refile-target-verify-function
  9384. (save-match-data
  9385. (or (funcall org-refile-target-verify-function)
  9386. (throw 'next t))))
  9387. (when (and (looking-at org-complex-heading-regexp)
  9388. (not (member (match-string 4) excluded-entries))
  9389. (match-string 4))
  9390. (setq level (org-reduced-level
  9391. (- (match-end 1) (match-beginning 1)))
  9392. txt (org-link-display-format (match-string 4))
  9393. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9394. re (format org-complex-heading-regexp-format
  9395. (regexp-quote (match-string 4))))
  9396. (when org-refile-use-outline-path
  9397. (setq txt (mapconcat
  9398. 'org-protect-slash
  9399. (append
  9400. (if (eq org-refile-use-outline-path
  9401. 'file)
  9402. (list (file-name-nondirectory
  9403. (buffer-file-name
  9404. (buffer-base-buffer))))
  9405. (if (eq org-refile-use-outline-path
  9406. 'full-file-path)
  9407. (list (buffer-file-name
  9408. (buffer-base-buffer)))))
  9409. (org-get-outline-path fast-path-p
  9410. level txt)
  9411. (list txt))
  9412. "/")))
  9413. (push (list txt f re (org-refile-marker (point)))
  9414. tgs)))
  9415. (when (= (point) pos0)
  9416. ;; verification function has not moved point
  9417. (goto-char (point-at-eol))))))))
  9418. (when org-refile-use-cache
  9419. (org-refile-cache-put tgs (buffer-file-name) descre))
  9420. (setq targets (append tgs targets))
  9421. ))))
  9422. (message "Getting targets...done")
  9423. (nreverse targets)))
  9424. (defun org-protect-slash (s)
  9425. (while (string-match "/" s)
  9426. (setq s (replace-match "\\" t t s)))
  9427. s)
  9428. (defvar org-olpa (make-vector 20 nil))
  9429. (defun org-get-outline-path (&optional fastp level heading)
  9430. "Return the outline path to the current entry, as a list.
  9431. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9432. routine which makes outline path derivations for an entire file,
  9433. avoiding backtracing. Refile target collection makes use of that."
  9434. (if fastp
  9435. (progn
  9436. (if (> level 19)
  9437. (error "Outline path failure, more than 19 levels"))
  9438. (loop for i from level upto 19 do
  9439. (aset org-olpa i nil))
  9440. (prog1
  9441. (delq nil (append org-olpa nil))
  9442. (aset org-olpa level heading)))
  9443. (let (rtn case-fold-search)
  9444. (save-excursion
  9445. (save-restriction
  9446. (widen)
  9447. (while (org-up-heading-safe)
  9448. (when (looking-at org-complex-heading-regexp)
  9449. (push (org-match-string-no-properties 4) rtn)))
  9450. rtn)))))
  9451. (defun org-format-outline-path (path &optional width prefix)
  9452. "Format the outline path PATH for display.
  9453. Width is the maximum number of characters that is available.
  9454. Prefix is a prefix to be included in the returned string,
  9455. such as the file name."
  9456. (setq width (or width 79))
  9457. (if prefix (setq width (- width (length prefix))))
  9458. (if (not path)
  9459. (or prefix "")
  9460. (let* ((nsteps (length path))
  9461. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9462. (maxwidth (if (<= total-width width)
  9463. 10000 ;; everything fits
  9464. ;; we need to shorten the level headings
  9465. (/ (- width nsteps) nsteps)))
  9466. (org-odd-levels-only nil)
  9467. (n 0)
  9468. (total (1+ (length prefix))))
  9469. (setq maxwidth (max maxwidth 10))
  9470. (concat prefix
  9471. (mapconcat
  9472. (lambda (h)
  9473. (setq n (1+ n))
  9474. (if (and (= n nsteps) (< maxwidth 10000))
  9475. (setq maxwidth (- total-width total)))
  9476. (if (< (length h) maxwidth)
  9477. (progn (setq total (+ total (length h) 1)) h)
  9478. (setq h (substring h 0 (- maxwidth 2))
  9479. total (+ total maxwidth 1))
  9480. (if (string-match "[ \t]+\\'" h)
  9481. (setq h (substring h 0 (match-beginning 0))))
  9482. (setq h (concat h "..")))
  9483. (org-add-props h nil 'face
  9484. (nth (% (1- n) org-n-level-faces)
  9485. org-level-faces))
  9486. h)
  9487. path "/")))))
  9488. (defun org-display-outline-path (&optional file current)
  9489. "Display the current outline path in the echo area."
  9490. (interactive "P")
  9491. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9492. (case-fold-search nil)
  9493. (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
  9494. (if current (setq path (append path
  9495. (save-excursion
  9496. (org-back-to-heading t)
  9497. (if (looking-at org-complex-heading-regexp)
  9498. (list (match-string 4)))))))
  9499. (message "%s"
  9500. (org-format-outline-path
  9501. path
  9502. (1- (frame-width))
  9503. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9504. (defvar org-refile-history nil
  9505. "History for refiling operations.")
  9506. (defvar org-after-refile-insert-hook nil
  9507. "Hook run after `org-refile' has inserted its stuff at the new location.
  9508. Note that this is still *before* the stuff will be removed from
  9509. the *old* location.")
  9510. (defvar org-capture-last-stored-marker)
  9511. (defun org-refile (&optional goto default-buffer rfloc)
  9512. "Move the entry or entries at point to another heading.
  9513. The list of target headings is compiled using the information in
  9514. `org-refile-targets', which see.
  9515. At the target location, the entry is filed as a subitem of the target
  9516. heading. Depending on `org-reverse-note-order', the new subitem will
  9517. either be the first or the last subitem.
  9518. If there is an active region, all entries in that region will be moved.
  9519. However, the region must fulfill the requirement that the first heading
  9520. is the first one sets the top-level of the moved text - at most siblings
  9521. below it are allowed.
  9522. With prefix arg GOTO, the command will only visit the target location
  9523. and not actually move anything.
  9524. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9525. go to the location where the last refiling operation has put the subtree.
  9526. With a prefix argument of `2', refile to the running clock.
  9527. RFLOC can be a refile location obtained in a different way.
  9528. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9529. If you are using target caching (see `org-refile-use-cache'),
  9530. You have to clear the target cache in order to find new targets.
  9531. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9532. prefix argument (`C-u C-u C-u C-c C-w')."
  9533. (interactive "P")
  9534. (if (member goto '(0 (64)))
  9535. (org-refile-cache-clear)
  9536. (let* ((cbuf (current-buffer))
  9537. (regionp (org-region-active-p))
  9538. (region-start (and regionp (region-beginning)))
  9539. (region-end (and regionp (region-end)))
  9540. (region-length (and regionp (- region-end region-start)))
  9541. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9542. pos it nbuf file re level reversed)
  9543. (setq last-command nil)
  9544. (when regionp
  9545. (goto-char region-start)
  9546. (or (bolp) (goto-char (point-at-bol)))
  9547. (setq region-start (point))
  9548. (unless (or (org-kill-is-subtree-p
  9549. (buffer-substring region-start region-end))
  9550. (prog1 org-refile-active-region-within-subtree
  9551. (org-toggle-heading)))
  9552. (error "The region is not a (sequence of) subtree(s)")))
  9553. (if (equal goto '(16))
  9554. (org-refile-goto-last-stored)
  9555. (when (or
  9556. (and (equal goto 2)
  9557. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9558. (prog1
  9559. (setq it (list (or org-clock-heading "running clock")
  9560. (buffer-file-name
  9561. (marker-buffer org-clock-hd-marker))
  9562. ""
  9563. (marker-position org-clock-hd-marker)))
  9564. (setq goto nil)))
  9565. (setq it (or rfloc
  9566. (let (heading-text)
  9567. (save-excursion
  9568. (unless goto
  9569. (org-back-to-heading t)
  9570. (setq heading-text
  9571. (nth 4 (org-heading-components))))
  9572. (org-refile-get-location
  9573. (cond (goto "Goto")
  9574. (regionp "Refile region to")
  9575. (t (concat "Refile subtree \""
  9576. heading-text "\" to")))
  9577. default-buffer
  9578. org-refile-allow-creating-parent-nodes
  9579. goto))))))
  9580. (setq file (nth 1 it)
  9581. re (nth 2 it)
  9582. pos (nth 3 it))
  9583. (if (and (not goto)
  9584. pos
  9585. (equal (buffer-file-name) file)
  9586. (if regionp
  9587. (and (>= pos region-start)
  9588. (<= pos region-end))
  9589. (and (>= pos (point))
  9590. (< pos (save-excursion
  9591. (org-end-of-subtree t t))))))
  9592. (error "Cannot refile to position inside the tree or region"))
  9593. (setq nbuf (or (find-buffer-visiting file)
  9594. (find-file-noselect file)))
  9595. (if goto
  9596. (progn
  9597. (org-pop-to-buffer-same-window nbuf)
  9598. (goto-char pos)
  9599. (org-show-context 'org-goto))
  9600. (if regionp
  9601. (progn
  9602. (org-kill-new (buffer-substring region-start region-end))
  9603. (org-save-markers-in-region region-start region-end))
  9604. (org-copy-subtree 1 nil t))
  9605. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9606. (find-file-noselect file)))
  9607. (setq reversed (org-notes-order-reversed-p))
  9608. (save-excursion
  9609. (save-restriction
  9610. (widen)
  9611. (if pos
  9612. (progn
  9613. (goto-char pos)
  9614. (looking-at org-outline-regexp)
  9615. (setq level (org-get-valid-level (funcall outline-level) 1))
  9616. (goto-char
  9617. (if reversed
  9618. (or (outline-next-heading) (point-max))
  9619. (or (save-excursion (org-get-next-sibling))
  9620. (org-end-of-subtree t t)
  9621. (point-max)))))
  9622. (setq level 1)
  9623. (if (not reversed)
  9624. (goto-char (point-max))
  9625. (goto-char (point-min))
  9626. (or (outline-next-heading) (goto-char (point-max)))))
  9627. (if (not (bolp)) (newline))
  9628. (org-paste-subtree level)
  9629. (when org-log-refile
  9630. (org-add-log-setup 'refile nil nil 'findpos
  9631. org-log-refile)
  9632. (unless (eq org-log-refile 'note)
  9633. (save-excursion (org-add-log-note))))
  9634. (and org-auto-align-tags (org-set-tags nil t))
  9635. (bookmark-set "org-refile-last-stored")
  9636. ;; If we are refiling for capture, make sure that the
  9637. ;; last-capture pointers point here
  9638. (when (org-bound-and-true-p org-refile-for-capture)
  9639. (bookmark-set "org-capture-last-stored-marker")
  9640. (move-marker org-capture-last-stored-marker (point)))
  9641. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9642. (run-hooks 'org-after-refile-insert-hook))))
  9643. (if regionp
  9644. (delete-region (point) (+ (point) region-length))
  9645. (org-cut-subtree))
  9646. (when (featurep 'org-inlinetask)
  9647. (org-inlinetask-remove-END-maybe))
  9648. (setq org-markers-to-move nil)
  9649. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9650. (defun org-refile-goto-last-stored ()
  9651. "Go to the location where the last refile was stored."
  9652. (interactive)
  9653. (bookmark-jump "org-refile-last-stored")
  9654. (message "This is the location of the last refile"))
  9655. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9656. no-exclude)
  9657. "Prompt the user for a refile location, using PROMPT.
  9658. PROMPT should not be suffixed with a colon and a space, because
  9659. this function appends the default value from
  9660. `org-refile-history' automatically, if that is not empty.
  9661. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9662. this is used for the GOTO interface."
  9663. (let ((org-refile-targets org-refile-targets)
  9664. (org-refile-use-outline-path org-refile-use-outline-path)
  9665. excluded-entries)
  9666. (when (and (eq major-mode 'org-mode)
  9667. (not org-refile-use-cache)
  9668. (not no-exclude))
  9669. (org-map-tree
  9670. (lambda()
  9671. (setq excluded-entries
  9672. (append excluded-entries (list (org-get-heading t t)))))))
  9673. (setq org-refile-target-table
  9674. (org-refile-get-targets default-buffer excluded-entries)))
  9675. (unless org-refile-target-table
  9676. (error "No refile targets"))
  9677. (let* ((prompt (concat prompt
  9678. (and (car org-refile-history)
  9679. (concat " (default " (car org-refile-history) ")"))
  9680. ": "))
  9681. (cbuf (current-buffer))
  9682. (partial-completion-mode nil)
  9683. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9684. (cfunc (if (and org-refile-use-outline-path
  9685. org-outline-path-complete-in-steps)
  9686. 'org-olpath-completing-read
  9687. 'org-icompleting-read))
  9688. (extra (if org-refile-use-outline-path "/" ""))
  9689. (filename (and cfn (expand-file-name cfn)))
  9690. (tbl (mapcar
  9691. (lambda (x)
  9692. (if (and (not (member org-refile-use-outline-path
  9693. '(file full-file-path)))
  9694. (not (equal filename (nth 1 x))))
  9695. (cons (concat (car x) extra " ("
  9696. (file-name-nondirectory (nth 1 x)) ")")
  9697. (cdr x))
  9698. (cons (concat (car x) extra) (cdr x))))
  9699. org-refile-target-table))
  9700. (completion-ignore-case t)
  9701. pa answ parent-target child parent old-hist)
  9702. (setq old-hist org-refile-history)
  9703. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9704. nil 'org-refile-history (car org-refile-history)))
  9705. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9706. (org-refile-check-position pa)
  9707. (if pa
  9708. (progn
  9709. (when (or (not org-refile-history)
  9710. (not (eq old-hist org-refile-history))
  9711. (not (equal (car pa) (car org-refile-history))))
  9712. (setq org-refile-history
  9713. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9714. org-refile-history
  9715. (cdr org-refile-history))))
  9716. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9717. (pop org-refile-history)))
  9718. pa)
  9719. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9720. (progn
  9721. (setq parent (match-string 1 answ)
  9722. child (match-string 2 answ))
  9723. (setq parent-target (or (assoc parent tbl)
  9724. (assoc (concat parent "/") tbl)))
  9725. (when (and parent-target
  9726. (or (eq new-nodes t)
  9727. (and (eq new-nodes 'confirm)
  9728. (y-or-n-p (format "Create new node \"%s\"? "
  9729. child)))))
  9730. (org-refile-new-child parent-target child)))
  9731. (error "Invalid target location")))))
  9732. (defun org-refile-check-position (refile-pointer)
  9733. "Check if the refile pointer matches the readline to which it points."
  9734. (let* ((file (nth 1 refile-pointer))
  9735. (re (nth 2 refile-pointer))
  9736. (pos (nth 3 refile-pointer))
  9737. buffer)
  9738. (when (org-string-nw-p re)
  9739. (setq buffer (if (markerp pos)
  9740. (marker-buffer pos)
  9741. (or (find-buffer-visiting file)
  9742. (find-file-noselect file))))
  9743. (with-current-buffer buffer
  9744. (save-excursion
  9745. (save-restriction
  9746. (widen)
  9747. (goto-char pos)
  9748. (beginning-of-line 1)
  9749. (unless (org-looking-at-p re)
  9750. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9751. (defun org-refile-new-child (parent-target child)
  9752. "Use refile target PARENT-TARGET to add new CHILD below it."
  9753. (unless parent-target
  9754. (error "Cannot find parent for new node"))
  9755. (let ((file (nth 1 parent-target))
  9756. (pos (nth 3 parent-target))
  9757. level)
  9758. (with-current-buffer (or (find-buffer-visiting file)
  9759. (find-file-noselect file))
  9760. (save-excursion
  9761. (save-restriction
  9762. (widen)
  9763. (if pos
  9764. (goto-char pos)
  9765. (goto-char (point-max))
  9766. (if (not (bolp)) (newline)))
  9767. (when (looking-at org-outline-regexp)
  9768. (setq level (funcall outline-level))
  9769. (org-end-of-subtree t t))
  9770. (org-back-over-empty-lines)
  9771. (insert "\n" (make-string
  9772. (if pos (org-get-valid-level level 1) 1) ?*)
  9773. " " child "\n")
  9774. (beginning-of-line 0)
  9775. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9776. (defun org-olpath-completing-read (prompt collection &rest args)
  9777. "Read an outline path like a file name."
  9778. (let ((thetable collection)
  9779. (org-completion-use-ido nil) ; does not work with ido.
  9780. (org-completion-use-iswitchb nil)) ; or iswitchb
  9781. (apply
  9782. 'org-icompleting-read prompt
  9783. (lambda (string predicate &optional flag)
  9784. (let (rtn r f (l (length string)))
  9785. (cond
  9786. ((eq flag nil)
  9787. ;; try completion
  9788. (try-completion string thetable))
  9789. ((eq flag t)
  9790. ;; all-completions
  9791. (setq rtn (all-completions string thetable predicate))
  9792. (mapcar
  9793. (lambda (x)
  9794. (setq r (substring x l))
  9795. (if (string-match " ([^)]*)$" x)
  9796. (setq f (match-string 0 x))
  9797. (setq f ""))
  9798. (if (string-match "/" r)
  9799. (concat string (substring r 0 (match-end 0)) f)
  9800. x))
  9801. rtn))
  9802. ((eq flag 'lambda)
  9803. ;; exact match?
  9804. (assoc string thetable)))
  9805. ))
  9806. args)))
  9807. ;;;; Dynamic blocks
  9808. (defun org-find-dblock (name)
  9809. "Find the first dynamic block with name NAME in the buffer.
  9810. If not found, stay at current position and return nil."
  9811. (let (pos)
  9812. (save-excursion
  9813. (goto-char (point-min))
  9814. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9815. nil t)
  9816. (match-beginning 0))))
  9817. (if pos (goto-char pos))
  9818. pos))
  9819. (defconst org-dblock-start-re
  9820. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9821. "Matches the start line of a dynamic block, with parameters.")
  9822. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9823. "Matches the end of a dynamic block.")
  9824. (defun org-create-dblock (plist)
  9825. "Create a dynamic block section, with parameters taken from PLIST.
  9826. PLIST must contain a :name entry which is used as name of the block."
  9827. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9828. (end-of-line 1)
  9829. (newline))
  9830. (let ((col (current-column))
  9831. (name (plist-get plist :name)))
  9832. (insert "#+BEGIN: " name)
  9833. (while plist
  9834. (if (eq (car plist) :name)
  9835. (setq plist (cddr plist))
  9836. (insert " " (prin1-to-string (pop plist)))))
  9837. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9838. (beginning-of-line -2)))
  9839. (defun org-prepare-dblock ()
  9840. "Prepare dynamic block for refresh.
  9841. This empties the block, puts the cursor at the insert position and returns
  9842. the property list including an extra property :name with the block name."
  9843. (unless (looking-at org-dblock-start-re)
  9844. (error "Not at a dynamic block"))
  9845. (let* ((begdel (1+ (match-end 0)))
  9846. (name (org-no-properties (match-string 1)))
  9847. (params (append (list :name name)
  9848. (read (concat "(" (match-string 3) ")")))))
  9849. (save-excursion
  9850. (beginning-of-line 1)
  9851. (skip-chars-forward " \t")
  9852. (setq params (plist-put params :indentation-column (current-column))))
  9853. (unless (re-search-forward org-dblock-end-re nil t)
  9854. (error "Dynamic block not terminated"))
  9855. (setq params
  9856. (append params
  9857. (list :content (buffer-substring
  9858. begdel (match-beginning 0)))))
  9859. (delete-region begdel (match-beginning 0))
  9860. (goto-char begdel)
  9861. (open-line 1)
  9862. params))
  9863. (defun org-map-dblocks (&optional command)
  9864. "Apply COMMAND to all dynamic blocks in the current buffer.
  9865. If COMMAND is not given, use `org-update-dblock'."
  9866. (let ((cmd (or command 'org-update-dblock)))
  9867. (save-excursion
  9868. (goto-char (point-min))
  9869. (while (re-search-forward org-dblock-start-re nil t)
  9870. (goto-char (match-beginning 0))
  9871. (save-excursion
  9872. (condition-case nil
  9873. (funcall cmd)
  9874. (error (message "Error during update of dynamic block"))))
  9875. (unless (re-search-forward org-dblock-end-re nil t)
  9876. (error "Dynamic block not terminated"))))))
  9877. (defun org-dblock-update (&optional arg)
  9878. "User command for updating dynamic blocks.
  9879. Update the dynamic block at point. With prefix ARG, update all dynamic
  9880. blocks in the buffer."
  9881. (interactive "P")
  9882. (if arg
  9883. (org-update-all-dblocks)
  9884. (or (looking-at org-dblock-start-re)
  9885. (org-beginning-of-dblock))
  9886. (org-update-dblock)))
  9887. (defun org-update-dblock ()
  9888. "Update the dynamic block at point.
  9889. This means to empty the block, parse for parameters and then call
  9890. the correct writing function."
  9891. (interactive)
  9892. (save-window-excursion
  9893. (let* ((pos (point))
  9894. (line (org-current-line))
  9895. (params (org-prepare-dblock))
  9896. (name (plist-get params :name))
  9897. (indent (plist-get params :indentation-column))
  9898. (cmd (intern (concat "org-dblock-write:" name))))
  9899. (message "Updating dynamic block `%s' at line %d..." name line)
  9900. (funcall cmd params)
  9901. (message "Updating dynamic block `%s' at line %d...done" name line)
  9902. (goto-char pos)
  9903. (when (and indent (> indent 0))
  9904. (setq indent (make-string indent ?\ ))
  9905. (save-excursion
  9906. (org-beginning-of-dblock)
  9907. (forward-line 1)
  9908. (while (not (looking-at org-dblock-end-re))
  9909. (insert indent)
  9910. (beginning-of-line 2))
  9911. (when (looking-at org-dblock-end-re)
  9912. (and (looking-at "[ \t]+")
  9913. (replace-match ""))
  9914. (insert indent)))))))
  9915. (defun org-beginning-of-dblock ()
  9916. "Find the beginning of the dynamic block at point.
  9917. Error if there is no such block at point."
  9918. (let ((pos (point))
  9919. beg)
  9920. (end-of-line 1)
  9921. (if (and (re-search-backward org-dblock-start-re nil t)
  9922. (setq beg (match-beginning 0))
  9923. (re-search-forward org-dblock-end-re nil t)
  9924. (> (match-end 0) pos))
  9925. (goto-char beg)
  9926. (goto-char pos)
  9927. (error "Not in a dynamic block"))))
  9928. (defun org-update-all-dblocks ()
  9929. "Update all dynamic blocks in the buffer.
  9930. This function can be used in a hook."
  9931. (interactive)
  9932. (when (eq major-mode 'org-mode)
  9933. (org-map-dblocks 'org-update-dblock)))
  9934. ;;;; Completion
  9935. (defconst org-additional-option-like-keywords
  9936. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9937. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9938. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9939. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9940. "BEGIN:" "END:"
  9941. "ORGTBL" "TBLFM:" "TBLNAME:"
  9942. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9943. "BEGIN_QUOTE" "END_QUOTE"
  9944. "BEGIN_VERSE" "END_VERSE"
  9945. "BEGIN_CENTER" "END_CENTER"
  9946. "BEGIN_SRC" "END_SRC"
  9947. "BEGIN_RESULT" "END_RESULT"
  9948. "NAME:" "RESULTS:"
  9949. "HEADER:" "HEADERS:"
  9950. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9951. "CAPTION:" "LABEL:"
  9952. "SETUPFILE:"
  9953. "INCLUDE:"
  9954. "BIND:"
  9955. "MACRO:"))
  9956. (defcustom org-structure-template-alist
  9957. '(
  9958. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  9959. "<src lang=\"?\">\n\n</src>")
  9960. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  9961. "<example>\n?\n</example>")
  9962. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  9963. "<quote>\n?\n</quote>")
  9964. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  9965. "<verse>\n?\n</verse>")
  9966. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  9967. "<center>\n?\n</center>")
  9968. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  9969. "<literal style=\"latex\">\n?\n</literal>")
  9970. ("L" "#+LaTeX: "
  9971. "<literal style=\"latex\">?</literal>")
  9972. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  9973. "<literal style=\"html\">\n?\n</literal>")
  9974. ("H" "#+HTML: "
  9975. "<literal style=\"html\">?</literal>")
  9976. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  9977. ("A" "#+ASCII: ")
  9978. ("i" "#+INDEX: ?"
  9979. "#+INDEX: ?")
  9980. ("I" "#+INCLUDE %file ?"
  9981. "<include file=%file markup=\"?\">")
  9982. )
  9983. "Structure completion elements.
  9984. This is a list of abbreviation keys and values. The value gets inserted
  9985. if you type `<' followed by the key and then press the completion key,
  9986. usually `M-TAB'. %file will be replaced by a file name after prompting
  9987. for the file using completion. The cursor will be placed at the position
  9988. of the `?` in the template.
  9989. There are two templates for each key, the first uses the original Org syntax,
  9990. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9991. the default when the /org-mtags.el/ module has been loaded. See also the
  9992. variable `org-mtags-prefer-muse-templates'."
  9993. :group 'org-completion
  9994. :type '(repeat
  9995. (string :tag "Key")
  9996. (string :tag "Template")
  9997. (string :tag "Muse Template")))
  9998. (defun org-try-structure-completion ()
  9999. "Try to complete a structure template before point.
  10000. This looks for strings like \"<e\" on an otherwise empty line and
  10001. expands them."
  10002. (let ((l (buffer-substring (point-at-bol) (point)))
  10003. a)
  10004. (when (and (looking-at "[ \t]*$")
  10005. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10006. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10007. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10008. (match-beginning 1)) a)
  10009. t)))
  10010. (defun org-complete-expand-structure-template (start cell)
  10011. "Expand a structure template."
  10012. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10013. (rpl (nth (if musep 2 1) cell))
  10014. (ind ""))
  10015. (delete-region start (point))
  10016. (when (string-match "\\`#\\+" rpl)
  10017. (cond
  10018. ((bolp))
  10019. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10020. (setq ind (buffer-substring (point-at-bol) (point))))
  10021. (t (newline))))
  10022. (setq start (point))
  10023. (if (string-match "%file" rpl)
  10024. (setq rpl (replace-match
  10025. (concat
  10026. "\""
  10027. (save-match-data
  10028. (abbreviate-file-name (read-file-name "Include file: ")))
  10029. "\"")
  10030. t t rpl)))
  10031. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10032. (concat "\n" ind)))
  10033. (insert rpl)
  10034. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10035. ;;;; TODO, DEADLINE, Comments
  10036. (defun org-toggle-comment ()
  10037. "Change the COMMENT state of an entry."
  10038. (interactive)
  10039. (save-excursion
  10040. (org-back-to-heading)
  10041. (let (case-fold-search)
  10042. (cond
  10043. ((looking-at (format org-heading-keyword-regexp-format
  10044. org-comment-string))
  10045. (goto-char (match-end 1))
  10046. (looking-at (concat " +" org-comment-string))
  10047. (replace-match "" t t)
  10048. (when (eolp) (insert " ")))
  10049. ((looking-at org-outline-regexp)
  10050. (goto-char (match-end 0))
  10051. (insert org-comment-string " "))))))
  10052. (defvar org-last-todo-state-is-todo nil
  10053. "This is non-nil when the last TODO state change led to a TODO state.
  10054. If the last change removed the TODO tag or switched to DONE, then
  10055. this is nil.")
  10056. (defvar org-setting-tags nil) ; dynamically skipped
  10057. (defvar org-todo-setup-filter-hook nil
  10058. "Hook for functions that pre-filter todo specs.
  10059. Each function takes a todo spec and returns either nil or the spec
  10060. transformed into canonical form." )
  10061. (defvar org-todo-get-default-hook nil
  10062. "Hook for functions that get a default item for todo.
  10063. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10064. nil or a string to be used for the todo mark." )
  10065. (defvar org-agenda-headline-snapshot-before-repeat)
  10066. (defun org-current-effective-time ()
  10067. "Return current time adjusted for `org-extend-today-until' variable"
  10068. (let* ((ct (org-current-time))
  10069. (dct (decode-time ct))
  10070. (ct1
  10071. (if (and org-use-effective-time
  10072. (< (nth 2 dct) org-extend-today-until))
  10073. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10074. ct)))
  10075. ct1))
  10076. (defun org-todo-yesterday (&optional arg)
  10077. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10078. (interactive "P")
  10079. (if (eq major-mode 'org-agenda-mode)
  10080. (apply 'org-agenda-todo-yesterday arg)
  10081. (let* ((hour (third (decode-time
  10082. (org-current-time))))
  10083. (org-extend-today-until (1+ hour)))
  10084. (org-todo arg))))
  10085. (defun org-todo (&optional arg)
  10086. "Change the TODO state of an item.
  10087. The state of an item is given by a keyword at the start of the heading,
  10088. like
  10089. *** TODO Write paper
  10090. *** DONE Call mom
  10091. The different keywords are specified in the variable `org-todo-keywords'.
  10092. By default the available states are \"TODO\" and \"DONE\".
  10093. So for this example: when the item starts with TODO, it is changed to DONE.
  10094. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10095. DONE are present, add TODO at the beginning of the heading.
  10096. With \\[universal-argument] prefix arg, use completion to determine the new \
  10097. state.
  10098. With numeric prefix arg, switch to that state.
  10099. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10100. keywords (nextset).
  10101. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10102. With a numeric prefix arg of 0, inhibit note taking for the change.
  10103. For calling through lisp, arg is also interpreted in the following way:
  10104. 'none -> empty state
  10105. \"\"(empty string) -> switch to empty state
  10106. 'done -> switch to DONE
  10107. 'nextset -> switch to the next set of keywords
  10108. 'previousset -> switch to the previous set of keywords
  10109. \"WAITING\" -> switch to the specified keyword, but only if it
  10110. really is a member of `org-todo-keywords'."
  10111. (interactive "P")
  10112. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10113. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10114. 'region-start-level 'region))
  10115. org-loop-over-headlines-in-active-region)
  10116. (org-map-entries
  10117. `(org-todo ,arg)
  10118. org-loop-over-headlines-in-active-region
  10119. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10120. (if (equal arg '(16)) (setq arg 'nextset))
  10121. (let ((org-blocker-hook org-blocker-hook)
  10122. (case-fold-search nil))
  10123. (when (equal arg '(64))
  10124. (setq arg nil org-blocker-hook nil))
  10125. (when (and org-blocker-hook
  10126. (or org-inhibit-blocking
  10127. (org-entry-get nil "NOBLOCKING")))
  10128. (setq org-blocker-hook nil))
  10129. (save-excursion
  10130. (catch 'exit
  10131. (org-back-to-heading t)
  10132. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10133. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10134. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10135. (let* ((match-data (match-data))
  10136. (startpos (point-at-bol))
  10137. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10138. (org-log-done org-log-done)
  10139. (org-log-repeat org-log-repeat)
  10140. (org-todo-log-states org-todo-log-states)
  10141. (org-inhibit-logging
  10142. (if (equal arg 0)
  10143. (progn (setq arg nil) 'note) org-inhibit-logging))
  10144. (this (match-string 1))
  10145. (hl-pos (match-beginning 0))
  10146. (head (org-get-todo-sequence-head this))
  10147. (ass (assoc head org-todo-kwd-alist))
  10148. (interpret (nth 1 ass))
  10149. (done-word (nth 3 ass))
  10150. (final-done-word (nth 4 ass))
  10151. (last-state (or this ""))
  10152. (completion-ignore-case t)
  10153. (member (member this org-todo-keywords-1))
  10154. (tail (cdr member))
  10155. (state (cond
  10156. ((and org-todo-key-trigger
  10157. (or (and (equal arg '(4))
  10158. (eq org-use-fast-todo-selection 'prefix))
  10159. (and (not arg) org-use-fast-todo-selection
  10160. (not (eq org-use-fast-todo-selection
  10161. 'prefix)))))
  10162. ;; Use fast selection
  10163. (org-fast-todo-selection))
  10164. ((and (equal arg '(4))
  10165. (or (not org-use-fast-todo-selection)
  10166. (not org-todo-key-trigger)))
  10167. ;; Read a state with completion
  10168. (org-icompleting-read
  10169. "State: " (mapcar (lambda(x) (list x))
  10170. org-todo-keywords-1)
  10171. nil t))
  10172. ((eq arg 'right)
  10173. (if this
  10174. (if tail (car tail) nil)
  10175. (car org-todo-keywords-1)))
  10176. ((eq arg 'left)
  10177. (if (equal member org-todo-keywords-1)
  10178. nil
  10179. (if this
  10180. (nth (- (length org-todo-keywords-1)
  10181. (length tail) 2)
  10182. org-todo-keywords-1)
  10183. (org-last org-todo-keywords-1))))
  10184. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10185. (setq arg nil))) ; hack to fall back to cycling
  10186. (arg
  10187. ;; user or caller requests a specific state
  10188. (cond
  10189. ((equal arg "") nil)
  10190. ((eq arg 'none) nil)
  10191. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10192. ((eq arg 'nextset)
  10193. (or (car (cdr (member head org-todo-heads)))
  10194. (car org-todo-heads)))
  10195. ((eq arg 'previousset)
  10196. (let ((org-todo-heads (reverse org-todo-heads)))
  10197. (or (car (cdr (member head org-todo-heads)))
  10198. (car org-todo-heads))))
  10199. ((car (member arg org-todo-keywords-1)))
  10200. ((stringp arg)
  10201. (error "State `%s' not valid in this file" arg))
  10202. ((nth (1- (prefix-numeric-value arg))
  10203. org-todo-keywords-1))))
  10204. ((null member) (or head (car org-todo-keywords-1)))
  10205. ((equal this final-done-word) nil) ;; -> make empty
  10206. ((null tail) nil) ;; -> first entry
  10207. ((memq interpret '(type priority))
  10208. (if (eq this-command last-command)
  10209. (car tail)
  10210. (if (> (length tail) 0)
  10211. (or done-word (car org-done-keywords))
  10212. nil)))
  10213. (t
  10214. (car tail))))
  10215. (state (or
  10216. (run-hook-with-args-until-success
  10217. 'org-todo-get-default-hook state last-state)
  10218. state))
  10219. (next (if state (concat " " state " ") " "))
  10220. (change-plist (list :type 'todo-state-change :from this :to state
  10221. :position startpos))
  10222. dolog now-done-p)
  10223. (when org-blocker-hook
  10224. (setq org-last-todo-state-is-todo
  10225. (not (member this org-done-keywords)))
  10226. (unless (save-excursion
  10227. (save-match-data
  10228. (org-with-wide-buffer
  10229. (run-hook-with-args-until-failure
  10230. 'org-blocker-hook change-plist))))
  10231. (if (org-called-interactively-p 'interactive)
  10232. (error "TODO state change from %s to %s blocked" this state)
  10233. ;; fail silently
  10234. (message "TODO state change from %s to %s blocked" this state)
  10235. (throw 'exit nil))))
  10236. (store-match-data match-data)
  10237. (replace-match next t t)
  10238. (unless (pos-visible-in-window-p hl-pos)
  10239. (message "TODO state changed to %s" (org-trim next)))
  10240. (unless head
  10241. (setq head (org-get-todo-sequence-head state)
  10242. ass (assoc head org-todo-kwd-alist)
  10243. interpret (nth 1 ass)
  10244. done-word (nth 3 ass)
  10245. final-done-word (nth 4 ass)))
  10246. (when (memq arg '(nextset previousset))
  10247. (message "Keyword-Set %d/%d: %s"
  10248. (- (length org-todo-sets) -1
  10249. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  10250. (length org-todo-sets)
  10251. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  10252. (setq org-last-todo-state-is-todo
  10253. (not (member state org-done-keywords)))
  10254. (setq now-done-p (and (member state org-done-keywords)
  10255. (not (member this org-done-keywords))))
  10256. (and logging (org-local-logging logging))
  10257. (when (and (or org-todo-log-states org-log-done)
  10258. (not (eq org-inhibit-logging t))
  10259. (not (memq arg '(nextset previousset))))
  10260. ;; we need to look at recording a time and note
  10261. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10262. (nth 2 (assoc this org-todo-log-states))))
  10263. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10264. (setq dolog 'time))
  10265. (when (and state
  10266. (member state org-not-done-keywords)
  10267. (not (member this org-not-done-keywords)))
  10268. ;; This is now a todo state and was not one before
  10269. ;; If there was a CLOSED time stamp, get rid of it.
  10270. (org-add-planning-info nil nil 'closed))
  10271. (when (and now-done-p org-log-done)
  10272. ;; It is now done, and it was not done before
  10273. (org-add-planning-info 'closed (org-current-effective-time))
  10274. (if (and (not dolog) (eq 'note org-log-done))
  10275. (org-add-log-setup 'done state this 'findpos 'note)))
  10276. (when (and state dolog)
  10277. ;; This is a non-nil state, and we need to log it
  10278. (org-add-log-setup 'state state this 'findpos dolog)))
  10279. ;; Fixup tag positioning
  10280. (org-todo-trigger-tag-changes state)
  10281. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10282. (when org-provide-todo-statistics
  10283. (org-update-parent-todo-statistics))
  10284. (run-hooks 'org-after-todo-state-change-hook)
  10285. (if (and arg (not (member state org-done-keywords)))
  10286. (setq head (org-get-todo-sequence-head state)))
  10287. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10288. ;; Do we need to trigger a repeat?
  10289. (when now-done-p
  10290. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10291. ;; This is for the agenda, take a snapshot of the headline.
  10292. (save-match-data
  10293. (setq org-agenda-headline-snapshot-before-repeat
  10294. (org-get-heading))))
  10295. (org-auto-repeat-maybe state))
  10296. ;; Fixup cursor location if close to the keyword
  10297. (if (and (outline-on-heading-p)
  10298. (not (bolp))
  10299. (save-excursion (beginning-of-line 1)
  10300. (looking-at org-todo-line-regexp))
  10301. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10302. (progn
  10303. (goto-char (or (match-end 2) (match-end 1)))
  10304. (and (looking-at " ") (just-one-space))))
  10305. (when org-trigger-hook
  10306. (save-excursion
  10307. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10308. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10309. "Block turning an entry into a TODO, using the hierarchy.
  10310. This checks whether the current task should be blocked from state
  10311. changes. Such blocking occurs when:
  10312. 1. The task has children which are not all in a completed state.
  10313. 2. A task has a parent with the property :ORDERED:, and there
  10314. are siblings prior to the current task with incomplete
  10315. status.
  10316. 3. The parent of the task is blocked because it has siblings that should
  10317. be done first, or is child of a block grandparent TODO entry."
  10318. (if (not org-enforce-todo-dependencies)
  10319. t ; if locally turned off don't block
  10320. (catch 'dont-block
  10321. ;; If this is not a todo state change, or if this entry is already DONE,
  10322. ;; do not block
  10323. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10324. (member (plist-get change-plist :from)
  10325. (cons 'done org-done-keywords))
  10326. (member (plist-get change-plist :to)
  10327. (cons 'todo org-not-done-keywords))
  10328. (not (plist-get change-plist :to)))
  10329. (throw 'dont-block t))
  10330. ;; If this task has children, and any are undone, it's blocked
  10331. (save-excursion
  10332. (org-back-to-heading t)
  10333. (let ((this-level (funcall outline-level)))
  10334. (outline-next-heading)
  10335. (let ((child-level (funcall outline-level)))
  10336. (while (and (not (eobp))
  10337. (> child-level this-level))
  10338. ;; this todo has children, check whether they are all
  10339. ;; completed
  10340. (if (and (not (org-entry-is-done-p))
  10341. (org-entry-is-todo-p))
  10342. (throw 'dont-block nil))
  10343. (outline-next-heading)
  10344. (setq child-level (funcall outline-level))))))
  10345. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10346. ;; any previous siblings are undone, it's blocked
  10347. (save-excursion
  10348. (org-back-to-heading t)
  10349. (let* ((pos (point))
  10350. (parent-pos (and (org-up-heading-safe) (point))))
  10351. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10352. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10353. (forward-line 1)
  10354. (re-search-forward org-not-done-heading-regexp pos t))
  10355. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10356. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10357. (while t
  10358. (goto-char parent-pos)
  10359. (if (not (looking-at org-not-done-heading-regexp))
  10360. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10361. (setq pos (point))
  10362. (setq parent-pos (and (org-up-heading-safe) (point)))
  10363. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10364. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10365. (forward-line 1)
  10366. (re-search-forward org-not-done-heading-regexp pos t))
  10367. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10368. (defcustom org-track-ordered-property-with-tag nil
  10369. "Should the ORDERED property also be shown as a tag?
  10370. The ORDERED property decides if an entry should require subtasks to be
  10371. completed in sequence. Since a property is not very visible, setting
  10372. this option means that toggling the ORDERED property with the command
  10373. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10374. not relevant for the behavior, but it makes things more visible.
  10375. Note that toggling the tag with tags commands will not change the property
  10376. and therefore not influence behavior!
  10377. This can be t, meaning the tag ORDERED should be used, It can also be a
  10378. string to select a different tag for this task."
  10379. :group 'org-todo
  10380. :type '(choice
  10381. (const :tag "No tracking" nil)
  10382. (const :tag "Track with ORDERED tag" t)
  10383. (string :tag "Use other tag")))
  10384. (defun org-toggle-ordered-property ()
  10385. "Toggle the ORDERED property of the current entry.
  10386. For better visibility, you can track the value of this property with a tag.
  10387. See variable `org-track-ordered-property-with-tag'."
  10388. (interactive)
  10389. (let* ((t1 org-track-ordered-property-with-tag)
  10390. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10391. (save-excursion
  10392. (org-back-to-heading)
  10393. (if (org-entry-get nil "ORDERED")
  10394. (progn
  10395. (org-delete-property "ORDERED")
  10396. (and tag (org-toggle-tag tag 'off))
  10397. (message "Subtasks can be completed in arbitrary order"))
  10398. (org-entry-put nil "ORDERED" "t")
  10399. (and tag (org-toggle-tag tag 'on))
  10400. (message "Subtasks must be completed in sequence")))))
  10401. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10402. (defun org-block-todo-from-checkboxes (change-plist)
  10403. "Block turning an entry into a TODO, using checkboxes.
  10404. This checks whether the current task should be blocked from state
  10405. changes because there are unchecked boxes in this entry."
  10406. (if (not org-enforce-todo-checkbox-dependencies)
  10407. t ; if locally turned off don't block
  10408. (catch 'dont-block
  10409. ;; If this is not a todo state change, or if this entry is already DONE,
  10410. ;; do not block
  10411. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10412. (member (plist-get change-plist :from)
  10413. (cons 'done org-done-keywords))
  10414. (member (plist-get change-plist :to)
  10415. (cons 'todo org-not-done-keywords))
  10416. (not (plist-get change-plist :to)))
  10417. (throw 'dont-block t))
  10418. ;; If this task has checkboxes that are not checked, it's blocked
  10419. (save-excursion
  10420. (org-back-to-heading t)
  10421. (let ((beg (point)) end)
  10422. (outline-next-heading)
  10423. (setq end (point))
  10424. (goto-char beg)
  10425. (if (org-list-search-forward
  10426. (concat (org-item-beginning-re)
  10427. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10428. "\\[[- ]\\]")
  10429. end t)
  10430. (progn
  10431. (if (boundp 'org-blocked-by-checkboxes)
  10432. (setq org-blocked-by-checkboxes t))
  10433. (throw 'dont-block nil)))))
  10434. t))) ; do not block
  10435. (defun org-entry-blocked-p ()
  10436. "Is the current entry blocked?"
  10437. (if (org-entry-get nil "NOBLOCKING")
  10438. nil ;; Never block this entry
  10439. (not
  10440. (run-hook-with-args-until-failure
  10441. 'org-blocker-hook
  10442. (list :type 'todo-state-change
  10443. :position (point)
  10444. :from 'todo
  10445. :to 'done)))))
  10446. (defun org-update-statistics-cookies (all)
  10447. "Update the statistics cookie, either from TODO or from checkboxes.
  10448. This should be called with the cursor in a line with a statistics cookie."
  10449. (interactive "P")
  10450. (if all
  10451. (progn
  10452. (org-update-checkbox-count 'all)
  10453. (org-map-entries 'org-update-parent-todo-statistics))
  10454. (if (not (org-at-heading-p))
  10455. (org-update-checkbox-count)
  10456. (let ((pos (move-marker (make-marker) (point)))
  10457. end l1 l2)
  10458. (ignore-errors (org-back-to-heading t))
  10459. (if (not (org-at-heading-p))
  10460. (org-update-checkbox-count)
  10461. (setq l1 (org-outline-level))
  10462. (setq end (save-excursion
  10463. (outline-next-heading)
  10464. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10465. (point)))
  10466. (if (and (save-excursion
  10467. (re-search-forward
  10468. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10469. (not (save-excursion (re-search-forward
  10470. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10471. (org-update-checkbox-count)
  10472. (if (and l2 (> l2 l1))
  10473. (progn
  10474. (goto-char end)
  10475. (org-update-parent-todo-statistics))
  10476. (goto-char pos)
  10477. (beginning-of-line 1)
  10478. (while (re-search-forward
  10479. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10480. (point-at-eol) t)
  10481. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10482. (goto-char pos)
  10483. (move-marker pos nil)))))
  10484. (defvar org-entry-property-inherited-from) ;; defined below
  10485. (defun org-update-parent-todo-statistics ()
  10486. "Update any statistics cookie in the parent of the current headline.
  10487. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10488. the entire subtree and this will travel up the hierarchy and update
  10489. statistics everywhere."
  10490. (let* ((prop (save-excursion (org-up-heading-safe)
  10491. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10492. (recursive (or (not org-hierarchical-todo-statistics)
  10493. (and prop (string-match "\\<recursive\\>" prop))))
  10494. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10495. 0))
  10496. (first t)
  10497. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10498. level ltoggle l1 new ndel
  10499. (cnt-all 0) (cnt-done 0) is-percent kwd
  10500. checkbox-beg ov ovs ove cookie-present)
  10501. (catch 'exit
  10502. (save-excursion
  10503. (beginning-of-line 1)
  10504. (setq ltoggle (funcall outline-level))
  10505. ;; Three situations are to consider:
  10506. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10507. ;; to the top-level ancestor on the headline;
  10508. ;; 2. If parent has "recursive" property, repeat up to the
  10509. ;; headline setting that property, taking inheritance into
  10510. ;; account;
  10511. ;; 3. Else, move up to direct parent and proceed only once.
  10512. (while (and (setq level (org-up-heading-safe))
  10513. (or recursive first)
  10514. (>= (point) lim))
  10515. (setq first nil cookie-present nil)
  10516. (unless (and level
  10517. (not (string-match
  10518. "\\<checkbox\\>"
  10519. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10520. "")))))
  10521. (throw 'exit nil))
  10522. (while (re-search-forward box-re (point-at-eol) t)
  10523. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10524. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10525. (save-match-data
  10526. (unless (outline-next-heading) (throw 'exit nil))
  10527. (while (and (looking-at org-complex-heading-regexp)
  10528. (> (setq l1 (length (match-string 1))) level))
  10529. (setq kwd (and (or recursive (= l1 ltoggle))
  10530. (match-string 2)))
  10531. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10532. (and (listp org-provide-todo-statistics)
  10533. (or (member kwd org-provide-todo-statistics)
  10534. (member kwd org-done-keywords))))
  10535. (setq cnt-all (1+ cnt-all))
  10536. (if (eq org-provide-todo-statistics t)
  10537. (and kwd (setq cnt-all (1+ cnt-all)))))
  10538. (and (member kwd org-done-keywords)
  10539. (setq cnt-done (1+ cnt-done)))
  10540. (outline-next-heading)))
  10541. (setq new
  10542. (if is-percent
  10543. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10544. (format "[%d/%d]" cnt-done cnt-all))
  10545. ndel (- (match-end 0) checkbox-beg))
  10546. ;; handle overlays when updating cookie from column view
  10547. (when (setq ov (car (overlays-at checkbox-beg)))
  10548. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10549. (delete-overlay ov))
  10550. (goto-char checkbox-beg)
  10551. (insert new)
  10552. (delete-region (point) (+ (point) ndel))
  10553. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10554. (when ov (move-overlay ov ovs ove)))
  10555. (when cookie-present
  10556. (run-hook-with-args 'org-after-todo-statistics-hook
  10557. cnt-done (- cnt-all cnt-done))))))
  10558. (run-hooks 'org-todo-statistics-hook)))
  10559. (defvar org-after-todo-statistics-hook nil
  10560. "Hook that is called after a TODO statistics cookie has been updated.
  10561. Each function is called with two arguments: the number of not-done entries
  10562. and the number of done entries.
  10563. For example, the following function, when added to this hook, will switch
  10564. an entry to DONE when all children are done, and back to TODO when new
  10565. entries are set to a TODO status. Note that this hook is only called
  10566. when there is a statistics cookie in the headline!
  10567. (defun org-summary-todo (n-done n-not-done)
  10568. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10569. (let (org-log-done org-log-states) ; turn off logging
  10570. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10571. ")
  10572. (defvar org-todo-statistics-hook nil
  10573. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10574. This hook runs even if there is no statistics cookie present, in which case
  10575. `org-after-todo-statistics-hook' would not run.")
  10576. (defun org-todo-trigger-tag-changes (state)
  10577. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10578. (let ((l org-todo-state-tags-triggers)
  10579. changes)
  10580. (when (or (not state) (equal state ""))
  10581. (setq changes (append changes (cdr (assoc "" l)))))
  10582. (when (and (stringp state) (> (length state) 0))
  10583. (setq changes (append changes (cdr (assoc state l)))))
  10584. (when (member state org-not-done-keywords)
  10585. (setq changes (append changes (cdr (assoc 'todo l)))))
  10586. (when (member state org-done-keywords)
  10587. (setq changes (append changes (cdr (assoc 'done l)))))
  10588. (dolist (c changes)
  10589. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10590. (defun org-local-logging (value)
  10591. "Get logging settings from a property VALUE."
  10592. (let* (words w a)
  10593. ;; directly set the variables, they are already local.
  10594. (setq org-log-done nil
  10595. org-log-repeat nil
  10596. org-todo-log-states nil)
  10597. (setq words (org-split-string value))
  10598. (while (setq w (pop words))
  10599. (cond
  10600. ((setq a (assoc w org-startup-options))
  10601. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10602. (set (nth 1 a) (nth 2 a))))
  10603. ((setq a (org-extract-log-state-settings w))
  10604. (and (member (car a) org-todo-keywords-1)
  10605. (push a org-todo-log-states)))))))
  10606. (defun org-get-todo-sequence-head (kwd)
  10607. "Return the head of the TODO sequence to which KWD belongs.
  10608. If KWD is not set, check if there is a text property remembering the
  10609. right sequence."
  10610. (let (p)
  10611. (cond
  10612. ((not kwd)
  10613. (or (get-text-property (point-at-bol) 'org-todo-head)
  10614. (progn
  10615. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10616. nil (point-at-eol)))
  10617. (get-text-property p 'org-todo-head))))
  10618. ((not (member kwd org-todo-keywords-1))
  10619. (car org-todo-keywords-1))
  10620. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10621. (defun org-fast-todo-selection ()
  10622. "Fast TODO keyword selection with single keys.
  10623. Returns the new TODO keyword, or nil if no state change should occur."
  10624. (let* ((fulltable org-todo-key-alist)
  10625. (done-keywords org-done-keywords) ;; needed for the faces.
  10626. (maxlen (apply 'max (mapcar
  10627. (lambda (x)
  10628. (if (stringp (car x)) (string-width (car x)) 0))
  10629. fulltable)))
  10630. (expert nil)
  10631. (fwidth (+ maxlen 3 1 3))
  10632. (ncol (/ (- (window-width) 4) fwidth))
  10633. tg cnt e c tbl
  10634. groups ingroup)
  10635. (save-excursion
  10636. (save-window-excursion
  10637. (if expert
  10638. (set-buffer (get-buffer-create " *Org todo*"))
  10639. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10640. (erase-buffer)
  10641. (org-set-local 'org-done-keywords done-keywords)
  10642. (setq tbl fulltable cnt 0)
  10643. (while (setq e (pop tbl))
  10644. (cond
  10645. ((equal e '(:startgroup))
  10646. (push '() groups) (setq ingroup t)
  10647. (when (not (= cnt 0))
  10648. (setq cnt 0)
  10649. (insert "\n"))
  10650. (insert "{ "))
  10651. ((equal e '(:endgroup))
  10652. (setq ingroup nil cnt 0)
  10653. (insert "}\n"))
  10654. ((equal e '(:newline))
  10655. (when (not (= cnt 0))
  10656. (setq cnt 0)
  10657. (insert "\n")
  10658. (setq e (car tbl))
  10659. (while (equal (car tbl) '(:newline))
  10660. (insert "\n")
  10661. (setq tbl (cdr tbl)))))
  10662. (t
  10663. (setq tg (car e) c (cdr e))
  10664. (if ingroup (push tg (car groups)))
  10665. (setq tg (org-add-props tg nil 'face
  10666. (org-get-todo-face tg)))
  10667. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10668. (insert "[" c "] " tg (make-string
  10669. (- fwidth 4 (length tg)) ?\ ))
  10670. (when (= (setq cnt (1+ cnt)) ncol)
  10671. (insert "\n")
  10672. (if ingroup (insert " "))
  10673. (setq cnt 0)))))
  10674. (insert "\n")
  10675. (goto-char (point-min))
  10676. (if (not expert) (org-fit-window-to-buffer))
  10677. (message "[a-z..]:Set [SPC]:clear")
  10678. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10679. (cond
  10680. ((or (= c ?\C-g)
  10681. (and (= c ?q) (not (rassoc c fulltable))))
  10682. (setq quit-flag t))
  10683. ((= c ?\ ) nil)
  10684. ((setq e (rassoc c fulltable) tg (car e))
  10685. tg)
  10686. (t (setq quit-flag t)))))))
  10687. (defun org-entry-is-todo-p ()
  10688. (member (org-get-todo-state) org-not-done-keywords))
  10689. (defun org-entry-is-done-p ()
  10690. (member (org-get-todo-state) org-done-keywords))
  10691. (defun org-get-todo-state ()
  10692. (save-excursion
  10693. (org-back-to-heading t)
  10694. (and (looking-at org-todo-line-regexp)
  10695. (match-end 2)
  10696. (match-string 2))))
  10697. (defun org-at-date-range-p (&optional inactive-ok)
  10698. "Is the cursor inside a date range?"
  10699. (interactive)
  10700. (save-excursion
  10701. (catch 'exit
  10702. (let ((pos (point)))
  10703. (skip-chars-backward "^[<\r\n")
  10704. (skip-chars-backward "<[")
  10705. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10706. (>= (match-end 0) pos)
  10707. (throw 'exit t))
  10708. (skip-chars-backward "^<[\r\n")
  10709. (skip-chars-backward "<[")
  10710. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10711. (>= (match-end 0) pos)
  10712. (throw 'exit t)))
  10713. nil)))
  10714. (defun org-get-repeat (&optional tagline)
  10715. "Check if there is a deadline/schedule with repeater in this entry."
  10716. (save-match-data
  10717. (save-excursion
  10718. (org-back-to-heading t)
  10719. (and (re-search-forward (if tagline
  10720. (concat tagline "\\s-*" org-repeat-re)
  10721. org-repeat-re)
  10722. (org-entry-end-position) t)
  10723. (match-string-no-properties 1)))))
  10724. (defvar org-last-changed-timestamp)
  10725. (defvar org-last-inserted-timestamp)
  10726. (defvar org-log-post-message)
  10727. (defvar org-log-note-purpose)
  10728. (defvar org-log-note-how)
  10729. (defvar org-log-note-extra)
  10730. (defun org-auto-repeat-maybe (done-word)
  10731. "Check if the current headline contains a repeated deadline/schedule.
  10732. If yes, set TODO state back to what it was and change the base date
  10733. of repeating deadline/scheduled time stamps to new date.
  10734. This function is run automatically after each state change to a DONE state."
  10735. ;; last-state is dynamically scoped into this function
  10736. (let* ((repeat (org-get-repeat))
  10737. (aa (assoc last-state org-todo-kwd-alist))
  10738. (interpret (nth 1 aa))
  10739. (head (nth 2 aa))
  10740. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10741. (msg "Entry repeats: ")
  10742. (org-log-done nil)
  10743. (org-todo-log-states nil)
  10744. re type n what ts time to-state)
  10745. (when repeat
  10746. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10747. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10748. org-todo-repeat-to-state))
  10749. (unless (and to-state (member to-state org-todo-keywords-1))
  10750. (setq to-state (if (eq interpret 'type) last-state head)))
  10751. (org-todo to-state)
  10752. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10753. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10754. (org-time-stamp-format t t))))
  10755. (when org-log-repeat
  10756. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10757. (memq 'org-add-log-note post-command-hook))
  10758. ;; OK, we are already setup for some record
  10759. (if (eq org-log-repeat 'note)
  10760. ;; make sure we take a note, not only a time stamp
  10761. (setq org-log-note-how 'note))
  10762. ;; Set up for taking a record
  10763. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10764. last-state
  10765. 'findpos org-log-repeat)))
  10766. (org-back-to-heading t)
  10767. (org-add-planning-info nil nil 'closed)
  10768. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10769. org-deadline-time-regexp "\\)\\|\\("
  10770. org-ts-regexp "\\)"))
  10771. (while (re-search-forward
  10772. re (save-excursion (outline-next-heading) (point)) t)
  10773. (setq type (if (match-end 1) org-scheduled-string
  10774. (if (match-end 3) org-deadline-string "Plain:"))
  10775. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10776. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10777. (setq n (string-to-number (match-string 2 ts))
  10778. what (match-string 3 ts))
  10779. (if (equal what "w") (setq n (* n 7) what "d"))
  10780. ;; Preparation, see if we need to modify the start date for the change
  10781. (when (match-end 1)
  10782. (setq time (save-match-data (org-time-string-to-time ts)))
  10783. (cond
  10784. ((equal (match-string 1 ts) ".")
  10785. ;; Shift starting date to today
  10786. (org-timestamp-change
  10787. (- (org-today) (time-to-days time))
  10788. 'day))
  10789. ((equal (match-string 1 ts) "+")
  10790. (let ((nshiftmax 10) (nshift 0))
  10791. (while (or (= nshift 0)
  10792. (<= (time-to-days time)
  10793. (time-to-days (current-time))))
  10794. (when (= (incf nshift) nshiftmax)
  10795. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10796. (error "Abort")))
  10797. (org-timestamp-change n (cdr (assoc what whata)))
  10798. (org-at-timestamp-p t)
  10799. (setq ts (match-string 1))
  10800. (setq time (save-match-data (org-time-string-to-time ts)))))
  10801. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10802. ;; rematch, so that we have everything in place for the real shift
  10803. (org-at-timestamp-p t)
  10804. (setq ts (match-string 1))
  10805. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10806. (org-timestamp-change n (cdr (assoc what whata)))
  10807. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10808. (setq org-log-post-message msg)
  10809. (message "%s" msg))))
  10810. (defun org-show-todo-tree (arg)
  10811. "Make a compact tree which shows all headlines marked with TODO.
  10812. The tree will show the lines where the regexp matches, and all higher
  10813. headlines above the match.
  10814. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10815. With a numeric prefix N, construct a sparse tree for the Nth element
  10816. of `org-todo-keywords-1'."
  10817. (interactive "P")
  10818. (let ((case-fold-search nil)
  10819. (kwd-re
  10820. (cond ((null arg) org-not-done-regexp)
  10821. ((equal arg '(4))
  10822. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10823. (mapcar 'list org-todo-keywords-1))))
  10824. (concat "\\("
  10825. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10826. "\\)\\>")))
  10827. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10828. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10829. org-todo-keywords-1)))
  10830. (t (error "Invalid prefix argument: %s" arg)))))
  10831. (message "%d TODO entries found"
  10832. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10833. (defun org-deadline (&optional remove time)
  10834. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10835. With argument REMOVE, remove any deadline from the item.
  10836. With argument TIME, set the deadline at the corresponding date. TIME
  10837. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10838. (interactive "P")
  10839. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10840. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10841. 'region-start-level 'region))
  10842. org-loop-over-headlines-in-active-region)
  10843. (org-map-entries
  10844. `(org-deadline ',remove ,time)
  10845. org-loop-over-headlines-in-active-region
  10846. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10847. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10848. (repeater (and old-date
  10849. (string-match
  10850. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10851. old-date)
  10852. (match-string 1 old-date))))
  10853. (if remove
  10854. (progn
  10855. (when (and old-date org-log-redeadline)
  10856. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10857. org-log-redeadline))
  10858. (org-remove-timestamp-with-keyword org-deadline-string)
  10859. (message "Item no longer has a deadline."))
  10860. (org-add-planning-info 'deadline time 'closed)
  10861. (when (and old-date org-log-redeadline
  10862. (not (equal old-date
  10863. (substring org-last-inserted-timestamp 1 -1))))
  10864. (org-add-log-setup 'redeadline nil old-date 'findpos
  10865. org-log-redeadline))
  10866. (when repeater
  10867. (save-excursion
  10868. (org-back-to-heading t)
  10869. (when (re-search-forward (concat org-deadline-string " "
  10870. org-last-inserted-timestamp)
  10871. (save-excursion
  10872. (outline-next-heading) (point)) t)
  10873. (goto-char (1- (match-end 0)))
  10874. (insert " " repeater)
  10875. (setq org-last-inserted-timestamp
  10876. (concat (substring org-last-inserted-timestamp 0 -1)
  10877. " " repeater
  10878. (substring org-last-inserted-timestamp -1))))))
  10879. (message "Deadline on %s" org-last-inserted-timestamp)))))
  10880. (defun org-schedule (&optional remove time)
  10881. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10882. With argument REMOVE, remove any scheduling date from the item.
  10883. With argument TIME, scheduled at the corresponding date. TIME can
  10884. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10885. (interactive "P")
  10886. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10887. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10888. 'region-start-level 'region))
  10889. org-loop-over-headlines-in-active-region)
  10890. (org-map-entries
  10891. `(org-schedule ',remove ,time)
  10892. org-loop-over-headlines-in-active-region
  10893. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10894. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10895. (repeater (and old-date
  10896. (string-match
  10897. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10898. old-date)
  10899. (match-string 1 old-date))))
  10900. (if remove
  10901. (progn
  10902. (when (and old-date org-log-reschedule)
  10903. (org-add-log-setup 'delschedule nil old-date 'findpos
  10904. org-log-reschedule))
  10905. (org-remove-timestamp-with-keyword org-scheduled-string)
  10906. (message "Item is no longer scheduled."))
  10907. (org-add-planning-info 'scheduled time 'closed)
  10908. (when (and old-date org-log-reschedule
  10909. (not (equal old-date
  10910. (substring org-last-inserted-timestamp 1 -1))))
  10911. (org-add-log-setup 'reschedule nil old-date 'findpos
  10912. org-log-reschedule))
  10913. (when repeater
  10914. (save-excursion
  10915. (org-back-to-heading t)
  10916. (when (re-search-forward (concat org-scheduled-string " "
  10917. org-last-inserted-timestamp)
  10918. (save-excursion
  10919. (outline-next-heading) (point)) t)
  10920. (goto-char (1- (match-end 0)))
  10921. (insert " " repeater)
  10922. (setq org-last-inserted-timestamp
  10923. (concat (substring org-last-inserted-timestamp 0 -1)
  10924. " " repeater
  10925. (substring org-last-inserted-timestamp -1))))))
  10926. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  10927. (defun org-get-scheduled-time (pom &optional inherit)
  10928. "Get the scheduled time as a time tuple, of a format suitable
  10929. for calling org-schedule with, or if there is no scheduling,
  10930. returns nil."
  10931. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10932. (when time
  10933. (apply 'encode-time (org-parse-time-string time)))))
  10934. (defun org-get-deadline-time (pom &optional inherit)
  10935. "Get the deadline as a time tuple, of a format suitable for
  10936. calling org-deadline with, or if there is no scheduling, returns
  10937. nil."
  10938. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10939. (when time
  10940. (apply 'encode-time (org-parse-time-string time)))))
  10941. (defun org-remove-timestamp-with-keyword (keyword)
  10942. "Remove all time stamps with KEYWORD in the current entry."
  10943. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10944. beg)
  10945. (save-excursion
  10946. (org-back-to-heading t)
  10947. (setq beg (point))
  10948. (outline-next-heading)
  10949. (while (re-search-backward re beg t)
  10950. (replace-match "")
  10951. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10952. (equal (char-before) ?\ ))
  10953. (backward-delete-char 1)
  10954. (if (string-match "^[ \t]*$" (buffer-substring
  10955. (point-at-bol) (point-at-eol)))
  10956. (delete-region (point-at-bol)
  10957. (min (point-max) (1+ (point-at-eol))))))))))
  10958. (defun org-add-planning-info (what &optional time &rest remove)
  10959. "Insert new timestamp with keyword in the line directly after the headline.
  10960. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  10961. If non is given, the user is prompted for a date.
  10962. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10963. be removed."
  10964. (interactive)
  10965. (let (org-time-was-given org-end-time-was-given ts
  10966. end default-time default-input)
  10967. (catch 'exit
  10968. (when (and (memq what '(scheduled deadline))
  10969. (or (not time)
  10970. (and (stringp time)
  10971. (string-match "^[-+]+[0-9]" time))))
  10972. ;; Try to get a default date/time from existing timestamp
  10973. (save-excursion
  10974. (org-back-to-heading t)
  10975. (setq end (save-excursion (outline-next-heading) (point)))
  10976. (when (re-search-forward (if (eq what 'scheduled)
  10977. org-scheduled-time-regexp
  10978. org-deadline-time-regexp)
  10979. end t)
  10980. (setq ts (match-string 1)
  10981. default-time
  10982. (apply 'encode-time (org-parse-time-string ts))
  10983. default-input (and ts (org-get-compact-tod ts))))))
  10984. (when what
  10985. (setq time
  10986. (if (stringp time)
  10987. ;; This is a string (relative or absolute), set proper date
  10988. (apply 'encode-time
  10989. (org-read-date-analyze
  10990. time default-time (decode-time default-time)))
  10991. ;; If necessary, get the time from the user
  10992. (or time (org-read-date nil 'to-time nil nil
  10993. default-time default-input)))))
  10994. (when (and org-insert-labeled-timestamps-at-point
  10995. (member what '(scheduled deadline)))
  10996. (insert
  10997. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10998. (org-insert-time-stamp time org-time-was-given
  10999. nil nil nil (list org-end-time-was-given))
  11000. (setq what nil))
  11001. (save-excursion
  11002. (save-restriction
  11003. (let (col list elt ts buffer-invisibility-spec)
  11004. (org-back-to-heading t)
  11005. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11006. (goto-char (match-end 1))
  11007. (setq col (current-column))
  11008. (goto-char (match-end 0))
  11009. (if (eobp) (insert "\n") (forward-char 1))
  11010. (when (and (not what)
  11011. (not (looking-at
  11012. (concat "[ \t]*"
  11013. org-keyword-time-not-clock-regexp))))
  11014. ;; Nothing to add, nothing to remove...... :-)
  11015. (throw 'exit nil))
  11016. (if (and (not (looking-at org-outline-regexp))
  11017. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11018. "[^\r\n]*"))
  11019. (not (equal (match-string 1) org-clock-string)))
  11020. (narrow-to-region (match-beginning 0) (match-end 0))
  11021. (insert-before-markers "\n")
  11022. (backward-char 1)
  11023. (narrow-to-region (point) (point))
  11024. (and org-adapt-indentation (org-indent-to-column col)))
  11025. ;; Check if we have to remove something.
  11026. (setq list (cons what remove))
  11027. (while list
  11028. (setq elt (pop list))
  11029. (when (or (and (eq elt 'scheduled)
  11030. (re-search-forward org-scheduled-time-regexp nil t))
  11031. (and (eq elt 'deadline)
  11032. (re-search-forward org-deadline-time-regexp nil t))
  11033. (and (eq elt 'closed)
  11034. (re-search-forward org-closed-time-regexp nil t)))
  11035. (replace-match "")
  11036. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11037. (and (looking-at "[ \t]+") (replace-match ""))
  11038. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11039. (when what
  11040. (insert
  11041. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11042. (cond ((eq what 'scheduled) org-scheduled-string)
  11043. ((eq what 'deadline) org-deadline-string)
  11044. ((eq what 'closed) org-closed-string))
  11045. " ")
  11046. (setq ts (org-insert-time-stamp
  11047. time
  11048. (or org-time-was-given
  11049. (and (eq what 'closed) org-log-done-with-time))
  11050. (eq what 'closed)
  11051. nil nil (list org-end-time-was-given)))
  11052. (insert
  11053. (if (not (or (bolp) (eq (char-before) ?\ )
  11054. (memq (char-after) '(32 10))
  11055. (eobp))) " " ""))
  11056. (end-of-line 1))
  11057. (goto-char (point-min))
  11058. (widen)
  11059. (if (and (looking-at "[ \t]*\n")
  11060. (equal (char-before) ?\n))
  11061. (delete-region (1- (point)) (point-at-eol)))
  11062. ts))))))
  11063. (defvar org-log-note-marker (make-marker))
  11064. (defvar org-log-note-purpose nil)
  11065. (defvar org-log-note-state nil)
  11066. (defvar org-log-note-previous-state nil)
  11067. (defvar org-log-note-how nil)
  11068. (defvar org-log-note-extra nil)
  11069. (defvar org-log-note-window-configuration nil)
  11070. (defvar org-log-note-return-to (make-marker))
  11071. (defvar org-log-note-effective-time nil
  11072. "Remembered current time so that dynamically scoped
  11073. `org-extend-today-until' affects tha timestamps in state change
  11074. log")
  11075. (defvar org-log-post-message nil
  11076. "Message to be displayed after a log note has been stored.
  11077. The auto-repeater uses this.")
  11078. (defun org-add-note ()
  11079. "Add a note to the current entry.
  11080. This is done in the same way as adding a state change note."
  11081. (interactive)
  11082. (org-add-log-setup 'note nil nil 'findpos nil))
  11083. (defvar org-property-end-re)
  11084. (defun org-add-log-setup (&optional purpose state prev-state
  11085. findpos how extra)
  11086. "Set up the post command hook to take a note.
  11087. If this is about to TODO state change, the new state is expected in STATE.
  11088. When FINDPOS is non-nil, find the correct position for the note in
  11089. the current entry. If not, assume that it can be inserted at point.
  11090. HOW is an indicator what kind of note should be created.
  11091. EXTRA is additional text that will be inserted into the notes buffer."
  11092. (let* ((org-log-into-drawer (org-log-into-drawer))
  11093. (drawer (cond ((stringp org-log-into-drawer)
  11094. org-log-into-drawer)
  11095. (org-log-into-drawer "LOGBOOK")
  11096. (t nil))))
  11097. (save-restriction
  11098. (save-excursion
  11099. (when findpos
  11100. (org-back-to-heading t)
  11101. (narrow-to-region (point) (save-excursion
  11102. (outline-next-heading) (point)))
  11103. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11104. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11105. "[^\r\n]*\\)?"))
  11106. (goto-char (match-end 0))
  11107. (cond
  11108. (drawer
  11109. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11110. nil t)
  11111. (progn
  11112. (goto-char (match-end 0))
  11113. (or org-log-states-order-reversed
  11114. (and (re-search-forward org-property-end-re nil t)
  11115. (goto-char (1- (match-beginning 0))))))
  11116. (insert "\n:" drawer ":\n:END:")
  11117. (beginning-of-line 0)
  11118. (org-indent-line-function)
  11119. (beginning-of-line 2)
  11120. (org-indent-line-function)
  11121. (end-of-line 0)))
  11122. ((and org-log-state-notes-insert-after-drawers
  11123. (save-excursion
  11124. (forward-line) (looking-at org-drawer-regexp)))
  11125. (forward-line)
  11126. (while (looking-at org-drawer-regexp)
  11127. (goto-char (match-end 0))
  11128. (re-search-forward org-property-end-re (point-max) t)
  11129. (forward-line))
  11130. (forward-line -1)))
  11131. (unless org-log-states-order-reversed
  11132. (and (= (char-after) ?\n) (forward-char 1))
  11133. (org-skip-over-state-notes)
  11134. (skip-chars-backward " \t\n\r")))
  11135. (move-marker org-log-note-marker (point))
  11136. (setq org-log-note-purpose purpose
  11137. org-log-note-state state
  11138. org-log-note-previous-state prev-state
  11139. org-log-note-how how
  11140. org-log-note-extra extra
  11141. org-log-note-effective-time (org-current-effective-time))
  11142. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11143. (defun org-skip-over-state-notes ()
  11144. "Skip past the list of State notes in an entry."
  11145. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11146. (when (ignore-errors (goto-char (org-in-item-p)))
  11147. (let* ((struct (org-list-struct))
  11148. (prevs (org-list-prevs-alist struct)))
  11149. (while (looking-at "[ \t]*- State")
  11150. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11151. (org-list-get-item-end (point) struct)))))))
  11152. (defun org-add-log-note (&optional purpose)
  11153. "Pop up a window for taking a note, and add this note later at point."
  11154. (remove-hook 'post-command-hook 'org-add-log-note)
  11155. (setq org-log-note-window-configuration (current-window-configuration))
  11156. (delete-other-windows)
  11157. (move-marker org-log-note-return-to (point))
  11158. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11159. (goto-char org-log-note-marker)
  11160. (org-switch-to-buffer-other-window "*Org Note*")
  11161. (erase-buffer)
  11162. (if (memq org-log-note-how '(time state))
  11163. (let (current-prefix-arg) (org-store-log-note))
  11164. (let ((org-inhibit-startup t)) (org-mode))
  11165. (insert (format "# Insert note for %s.
  11166. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11167. (cond
  11168. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11169. ((eq org-log-note-purpose 'done) "closed todo item")
  11170. ((eq org-log-note-purpose 'state)
  11171. (format "state change from \"%s\" to \"%s\""
  11172. (or org-log-note-previous-state "")
  11173. (or org-log-note-state "")))
  11174. ((eq org-log-note-purpose 'reschedule)
  11175. "rescheduling")
  11176. ((eq org-log-note-purpose 'delschedule)
  11177. "no longer scheduled")
  11178. ((eq org-log-note-purpose 'redeadline)
  11179. "changing deadline")
  11180. ((eq org-log-note-purpose 'deldeadline)
  11181. "removing deadline")
  11182. ((eq org-log-note-purpose 'refile)
  11183. "refiling")
  11184. ((eq org-log-note-purpose 'note)
  11185. "this entry")
  11186. (t (error "This should not happen")))))
  11187. (if org-log-note-extra (insert org-log-note-extra))
  11188. (org-set-local 'org-finish-function 'org-store-log-note)
  11189. (run-hooks 'org-log-buffer-setup-hook)))
  11190. (defvar org-note-abort nil) ; dynamically scoped
  11191. (defun org-store-log-note ()
  11192. "Finish taking a log note, and insert it to where it belongs."
  11193. (let ((txt (buffer-string))
  11194. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11195. lines ind bul)
  11196. (kill-buffer (current-buffer))
  11197. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11198. (setq txt (replace-match "" t t txt)))
  11199. (if (string-match "\\s-+\\'" txt)
  11200. (setq txt (replace-match "" t t txt)))
  11201. (setq lines (org-split-string txt "\n"))
  11202. (when (and note (string-match "\\S-" note))
  11203. (setq note
  11204. (org-replace-escapes
  11205. note
  11206. (list (cons "%u" (user-login-name))
  11207. (cons "%U" user-full-name)
  11208. (cons "%t" (format-time-string
  11209. (org-time-stamp-format 'long 'inactive)
  11210. org-log-note-effective-time))
  11211. (cons "%T" (format-time-string
  11212. (org-time-stamp-format 'long nil)
  11213. org-log-note-effective-time))
  11214. (cons "%d" (format-time-string
  11215. (org-time-stamp-format nil 'inactive)
  11216. org-log-note-effective-time))
  11217. (cons "%D" (format-time-string
  11218. (org-time-stamp-format nil nil)
  11219. org-log-note-effective-time))
  11220. (cons "%s" (if org-log-note-state
  11221. (concat "\"" org-log-note-state "\"")
  11222. ""))
  11223. (cons "%S" (if org-log-note-previous-state
  11224. (concat "\"" org-log-note-previous-state "\"")
  11225. "\"\"")))))
  11226. (if lines (setq note (concat note " \\\\")))
  11227. (push note lines))
  11228. (when (or current-prefix-arg org-note-abort)
  11229. (when org-log-into-drawer
  11230. (org-remove-empty-drawer-at
  11231. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11232. org-log-note-marker))
  11233. (setq lines nil))
  11234. (when lines
  11235. (with-current-buffer (marker-buffer org-log-note-marker)
  11236. (save-excursion
  11237. (goto-char org-log-note-marker)
  11238. (move-marker org-log-note-marker nil)
  11239. (end-of-line 1)
  11240. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11241. (setq ind (save-excursion
  11242. (if (ignore-errors (goto-char (org-in-item-p)))
  11243. (let ((struct (org-list-struct)))
  11244. (org-list-get-ind
  11245. (org-list-get-top-point struct) struct))
  11246. (skip-chars-backward " \r\t\n")
  11247. (cond
  11248. ((and (org-at-heading-p)
  11249. org-adapt-indentation)
  11250. (1+ (org-current-level)))
  11251. ((org-at-heading-p) 0)
  11252. (t (org-get-indentation))))))
  11253. (setq bul (org-list-bullet-string "-"))
  11254. (org-indent-line-to ind)
  11255. (insert bul (pop lines))
  11256. (let ((ind-body (+ (length bul) ind)))
  11257. (while lines
  11258. (insert "\n")
  11259. (org-indent-line-to ind-body)
  11260. (insert (pop lines))))
  11261. (message "Note stored")
  11262. (org-back-to-heading t)
  11263. (org-cycle-hide-drawers 'children)))))
  11264. (set-window-configuration org-log-note-window-configuration)
  11265. (with-current-buffer (marker-buffer org-log-note-return-to)
  11266. (goto-char org-log-note-return-to))
  11267. (move-marker org-log-note-return-to nil)
  11268. (and org-log-post-message (message "%s" org-log-post-message)))
  11269. (defun org-remove-empty-drawer-at (drawer pos)
  11270. "Remove an empty drawer DRAWER at position POS.
  11271. POS may also be a marker."
  11272. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11273. (save-excursion
  11274. (save-restriction
  11275. (widen)
  11276. (goto-char pos)
  11277. (if (org-in-regexp
  11278. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11279. (replace-match ""))))))
  11280. (defun org-sparse-tree (&optional arg)
  11281. "Create a sparse tree, prompt for the details.
  11282. This command can create sparse trees. You first need to select the type
  11283. of match used to create the tree:
  11284. t Show all TODO entries.
  11285. T Show entries with a specific TODO keyword.
  11286. m Show entries selected by a tags/property match.
  11287. p Enter a property name and its value (both with completion on existing
  11288. names/values) and show entries with that property.
  11289. r Show entries matching a regular expression (`/' can be used as well)
  11290. d Show deadlines due within `org-deadline-warning-days'.
  11291. b Show deadlines and scheduled items before a date.
  11292. a Show deadlines and scheduled items after a date."
  11293. (interactive "P")
  11294. (let (ans kwd value)
  11295. (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")
  11296. (setq ans (read-char-exclusive))
  11297. (cond
  11298. ((equal ans ?d)
  11299. (call-interactively 'org-check-deadlines))
  11300. ((equal ans ?b)
  11301. (call-interactively 'org-check-before-date))
  11302. ((equal ans ?a)
  11303. (call-interactively 'org-check-after-date))
  11304. ((equal ans ?D)
  11305. (call-interactively 'org-check-dates-range))
  11306. ((equal ans ?t)
  11307. (org-show-todo-tree nil))
  11308. ((equal ans ?T)
  11309. (org-show-todo-tree '(4)))
  11310. ((member ans '(?T ?m))
  11311. (call-interactively 'org-match-sparse-tree))
  11312. ((member ans '(?p ?P))
  11313. (setq kwd (org-icompleting-read "Property: "
  11314. (mapcar 'list (org-buffer-property-keys))))
  11315. (setq value (org-icompleting-read "Value: "
  11316. (mapcar 'list (org-property-values kwd))))
  11317. (unless (string-match "\\`{.*}\\'" value)
  11318. (setq value (concat "\"" value "\"")))
  11319. (org-match-sparse-tree arg (concat kwd "=" value)))
  11320. ((member ans '(?r ?R ?/))
  11321. (call-interactively 'org-occur))
  11322. (t (error "No such sparse tree command \"%c\"" ans)))))
  11323. (defvar org-occur-highlights nil
  11324. "List of overlays used for occur matches.")
  11325. (make-variable-buffer-local 'org-occur-highlights)
  11326. (defvar org-occur-parameters nil
  11327. "Parameters of the active org-occur calls.
  11328. This is a list, each call to org-occur pushes as cons cell,
  11329. containing the regular expression and the callback, onto the list.
  11330. The list can contain several entries if `org-occur' has been called
  11331. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11332. will only contain one set of parameters. When the highlights are
  11333. removed (for example with `C-c C-c', or with the next edit (depending
  11334. on `org-remove-highlights-with-change'), this variable is emptied
  11335. as well.")
  11336. (make-variable-buffer-local 'org-occur-parameters)
  11337. (defun org-occur (regexp &optional keep-previous callback)
  11338. "Make a compact tree which shows all matches of REGEXP.
  11339. The tree will show the lines where the regexp matches, and all higher
  11340. headlines above the match. It will also show the heading after the match,
  11341. to make sure editing the matching entry is easy.
  11342. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11343. call to `org-occur' will be kept, to allow stacking of calls to this
  11344. command.
  11345. If CALLBACK is non-nil, it is a function which is called to confirm
  11346. that the match should indeed be shown."
  11347. (interactive "sRegexp: \nP")
  11348. (when (equal regexp "")
  11349. (error "Regexp cannot be empty"))
  11350. (unless keep-previous
  11351. (org-remove-occur-highlights nil nil t))
  11352. (push (cons regexp callback) org-occur-parameters)
  11353. (let ((cnt 0))
  11354. (save-excursion
  11355. (goto-char (point-min))
  11356. (if (or (not keep-previous) ; do not want to keep
  11357. (not org-occur-highlights)) ; no previous matches
  11358. ;; hide everything
  11359. (org-overview))
  11360. (while (re-search-forward regexp nil t)
  11361. (when (or (not callback)
  11362. (save-match-data (funcall callback)))
  11363. (setq cnt (1+ cnt))
  11364. (when org-highlight-sparse-tree-matches
  11365. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11366. (org-show-context 'occur-tree))))
  11367. (when org-remove-highlights-with-change
  11368. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11369. nil 'local))
  11370. (unless org-sparse-tree-open-archived-trees
  11371. (org-hide-archived-subtrees (point-min) (point-max)))
  11372. (run-hooks 'org-occur-hook)
  11373. (if (org-called-interactively-p 'interactive)
  11374. (message "%d match(es) for regexp %s" cnt regexp))
  11375. cnt))
  11376. (defun org-occur-next-match (&optional n reset)
  11377. "Function for `next-error-function' to find sparse tree matches.
  11378. N is the number of matches to move, when negative move backwards.
  11379. RESET is entirely ignored - this function always goes back to the
  11380. starting point when no match is found."
  11381. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11382. (search-func (if (< n 0)
  11383. 'previous-single-char-property-change
  11384. 'next-single-char-property-change))
  11385. (n (abs n))
  11386. (pos (point))
  11387. p1)
  11388. (catch 'exit
  11389. (while (setq p1 (funcall search-func (point) 'org-type))
  11390. (when (equal p1 limit)
  11391. (goto-char pos)
  11392. (error "No more matches"))
  11393. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11394. (setq n (1- n))
  11395. (when (= n 0)
  11396. (goto-char p1)
  11397. (throw 'exit (point))))
  11398. (goto-char p1))
  11399. (goto-char p1)
  11400. (error "No more matches"))))
  11401. (defun org-show-context (&optional key)
  11402. "Make sure point and context are visible.
  11403. How much context is shown depends upon the variables
  11404. `org-show-hierarchy-above', `org-show-following-heading',
  11405. `org-show-entry-below' and `org-show-siblings'."
  11406. (let ((heading-p (org-at-heading-p t))
  11407. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11408. (following-p (org-get-alist-option org-show-following-heading key))
  11409. (entry-p (org-get-alist-option org-show-entry-below key))
  11410. (siblings-p (org-get-alist-option org-show-siblings key)))
  11411. (catch 'exit
  11412. ;; Show heading or entry text
  11413. (if (and heading-p (not entry-p))
  11414. (org-flag-heading nil) ; only show the heading
  11415. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11416. (org-show-hidden-entry))) ; show entire entry
  11417. (when following-p
  11418. ;; Show next sibling, or heading below text
  11419. (save-excursion
  11420. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11421. (org-flag-heading nil))))
  11422. (when siblings-p (org-show-siblings))
  11423. (when hierarchy-p
  11424. ;; show all higher headings, possibly with siblings
  11425. (save-excursion
  11426. (while (and (condition-case nil
  11427. (progn (org-up-heading-all 1) t)
  11428. (error nil))
  11429. (not (bobp)))
  11430. (org-flag-heading nil)
  11431. (when siblings-p (org-show-siblings))))))))
  11432. (defvar org-reveal-start-hook nil
  11433. "Hook run before revealing a location.")
  11434. (defun org-reveal (&optional siblings)
  11435. "Show current entry, hierarchy above it, and the following headline.
  11436. This can be used to show a consistent set of context around locations
  11437. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11438. not t for the search context.
  11439. With optional argument SIBLINGS, on each level of the hierarchy all
  11440. siblings are shown. This repairs the tree structure to what it would
  11441. look like when opened with hierarchical calls to `org-cycle'.
  11442. With double optional argument \\[universal-argument] \\[universal-argument], \
  11443. go to the parent and show the
  11444. entire tree."
  11445. (interactive "P")
  11446. (run-hooks 'org-reveal-start-hook)
  11447. (let ((org-show-hierarchy-above t)
  11448. (org-show-following-heading t)
  11449. (org-show-siblings (if siblings t org-show-siblings)))
  11450. (org-show-context nil))
  11451. (when (equal siblings '(16))
  11452. (save-excursion
  11453. (when (org-up-heading-safe)
  11454. (org-show-subtree)
  11455. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11456. (defun org-highlight-new-match (beg end)
  11457. "Highlight from BEG to END and mark the highlight is an occur headline."
  11458. (let ((ov (make-overlay beg end)))
  11459. (overlay-put ov 'face 'secondary-selection)
  11460. (overlay-put ov 'org-type 'org-occur)
  11461. (push ov org-occur-highlights)))
  11462. (defun org-remove-occur-highlights (&optional beg end noremove)
  11463. "Remove the occur highlights from the buffer.
  11464. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11465. from the `before-change-functions' in the current buffer."
  11466. (interactive)
  11467. (unless org-inhibit-highlight-removal
  11468. (mapc 'delete-overlay org-occur-highlights)
  11469. (setq org-occur-highlights nil)
  11470. (setq org-occur-parameters nil)
  11471. (unless noremove
  11472. (remove-hook 'before-change-functions
  11473. 'org-remove-occur-highlights 'local))))
  11474. ;;;; Priorities
  11475. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11476. "Regular expression matching the priority indicator.")
  11477. (defvar org-remove-priority-next-time nil)
  11478. (defun org-priority-up ()
  11479. "Increase the priority of the current item."
  11480. (interactive)
  11481. (org-priority 'up))
  11482. (defun org-priority-down ()
  11483. "Decrease the priority of the current item."
  11484. (interactive)
  11485. (org-priority 'down))
  11486. (defun org-priority (&optional action)
  11487. "Change the priority of an item by ARG.
  11488. ACTION can be `set', `up', `down', or a character."
  11489. (interactive)
  11490. (unless org-enable-priority-commands
  11491. (error "Priority commands are disabled"))
  11492. (setq action (or action 'set))
  11493. (let (current new news have remove)
  11494. (save-excursion
  11495. (org-back-to-heading t)
  11496. (if (looking-at org-priority-regexp)
  11497. (setq current (string-to-char (match-string 2))
  11498. have t))
  11499. (cond
  11500. ((eq action 'remove)
  11501. (setq remove t new ?\ ))
  11502. ((or (eq action 'set)
  11503. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11504. (if (not (eq action 'set))
  11505. (setq new action)
  11506. (message "Priority %c-%c, SPC to remove: "
  11507. org-highest-priority org-lowest-priority)
  11508. (save-match-data
  11509. (setq new (read-char-exclusive))))
  11510. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11511. (= (upcase org-lowest-priority) org-lowest-priority))
  11512. (setq new (upcase new)))
  11513. (cond ((equal new ?\ ) (setq remove t))
  11514. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11515. (error "Priority must be between `%c' and `%c'"
  11516. org-highest-priority org-lowest-priority))))
  11517. ((eq action 'up)
  11518. (setq new (if have
  11519. (1- current) ; normal cycling
  11520. ;; last priority was empty
  11521. (if (eq last-command this-command)
  11522. org-lowest-priority ; wrap around empty to lowest
  11523. ;; default
  11524. (if org-priority-start-cycle-with-default
  11525. org-default-priority
  11526. (1- org-default-priority))))))
  11527. ((eq action 'down)
  11528. (setq new (if have
  11529. (1+ current) ; normal cycling
  11530. ;; last priority was empty
  11531. (if (eq last-command this-command)
  11532. org-highest-priority ; wrap around empty to highest
  11533. ;; default
  11534. (if org-priority-start-cycle-with-default
  11535. org-default-priority
  11536. (1+ org-default-priority))))))
  11537. (t (error "Invalid action")))
  11538. (if (or (< (upcase new) org-highest-priority)
  11539. (> (upcase new) org-lowest-priority))
  11540. (if (and (memq action '(up down))
  11541. (not have) (not (eq last-command this-command)))
  11542. ;; `new' is from default priority
  11543. (error
  11544. "The default can not be set, see `org-default-priority' why")
  11545. ;; normal cycling: `new' is beyond highest/lowest priority
  11546. ;; and is wrapped around to the empty priority
  11547. (setq remove t)))
  11548. (setq news (format "%c" new))
  11549. (if have
  11550. (if remove
  11551. (replace-match "" t t nil 1)
  11552. (replace-match news t t nil 2))
  11553. (if remove
  11554. (error "No priority cookie found in line")
  11555. (let ((case-fold-search nil))
  11556. (looking-at org-todo-line-regexp))
  11557. (if (match-end 2)
  11558. (progn
  11559. (goto-char (match-end 2))
  11560. (insert " [#" news "]"))
  11561. (goto-char (match-beginning 3))
  11562. (insert "[#" news "] "))))
  11563. (org-preserve-lc (org-set-tags nil 'align)))
  11564. (if remove
  11565. (message "Priority removed")
  11566. (message "Priority of current item set to %s" news))))
  11567. (defun org-get-priority (s)
  11568. "Find priority cookie and return priority."
  11569. (if (functionp org-get-priority-function)
  11570. (funcall org-get-priority-function)
  11571. (save-match-data
  11572. (if (not (string-match org-priority-regexp s))
  11573. (* 1000 (- org-lowest-priority org-default-priority))
  11574. (* 1000 (- org-lowest-priority
  11575. (string-to-char (match-string 2 s))))))))
  11576. ;;;; Tags
  11577. (defvar org-agenda-archives-mode)
  11578. (defvar org-map-continue-from nil
  11579. "Position from where mapping should continue.
  11580. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11581. (defvar org-scanner-tags nil
  11582. "The current tag list while the tags scanner is running.")
  11583. (defvar org-trust-scanner-tags nil
  11584. "Should `org-get-tags-at' use the tags for the scanner.
  11585. This is for internal dynamical scoping only.
  11586. When this is non-nil, the function `org-get-tags-at' will return the value
  11587. of `org-scanner-tags' instead of building the list by itself. This
  11588. can lead to large speed-ups when the tags scanner is used in a file with
  11589. many entries, and when the list of tags is retrieved, for example to
  11590. obtain a list of properties. Building the tags list for each entry in such
  11591. a file becomes an N^2 operation - but with this variable set, it scales
  11592. as N.")
  11593. (defun org-scan-tags (action matcher &optional todo-only start-level)
  11594. "Scan headline tags with inheritance and produce output ACTION.
  11595. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11596. or `agenda' to produce an entry list for an agenda view. It can also be
  11597. a Lisp form or a function that should be called at each matched headline, in
  11598. this case the return value is a list of all return values from these calls.
  11599. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11600. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11601. only lines with a TODO keyword are included in the output.
  11602. START-LEVEL can be a string with asterisks, reducing the scope to
  11603. headlines matching this string."
  11604. (require 'org-agenda)
  11605. (let* ((re (concat "^"
  11606. (if start-level
  11607. ;; Get the correct level to match
  11608. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11609. org-outline-regexp)
  11610. " *\\(\\<\\("
  11611. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11612. (org-re
  11613. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11614. (props (list 'face 'default
  11615. 'done-face 'org-agenda-done
  11616. 'undone-face 'default
  11617. 'mouse-face 'highlight
  11618. 'org-not-done-regexp org-not-done-regexp
  11619. 'org-todo-regexp org-todo-regexp
  11620. 'org-complex-heading-regexp org-complex-heading-regexp
  11621. 'help-echo
  11622. (format "mouse-2 or RET jump to org file %s"
  11623. (abbreviate-file-name
  11624. (or (buffer-file-name (buffer-base-buffer))
  11625. (buffer-name (buffer-base-buffer)))))))
  11626. (case-fold-search nil)
  11627. (org-map-continue-from nil)
  11628. lspos tags
  11629. (tags-alist (list (cons 0 org-file-tags)))
  11630. (llast 0) rtn rtn1 level category i txt
  11631. todo marker entry priority)
  11632. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11633. (setq action (list 'lambda nil action)))
  11634. (save-excursion
  11635. (goto-char (point-min))
  11636. (when (eq action 'sparse-tree)
  11637. (org-overview)
  11638. (org-remove-occur-highlights))
  11639. (while (re-search-forward re nil t)
  11640. (setq org-map-continue-from nil)
  11641. (catch :skip
  11642. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11643. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11644. (goto-char (setq lspos (match-beginning 0)))
  11645. (setq level (org-reduced-level (funcall outline-level))
  11646. category (org-get-category))
  11647. (setq i llast llast level)
  11648. ;; remove tag lists from same and sublevels
  11649. (while (>= i level)
  11650. (when (setq entry (assoc i tags-alist))
  11651. (setq tags-alist (delete entry tags-alist)))
  11652. (setq i (1- i)))
  11653. ;; add the next tags
  11654. (when tags
  11655. (setq tags (org-split-string tags ":")
  11656. tags-alist
  11657. (cons (cons level tags) tags-alist)))
  11658. ;; compile tags for current headline
  11659. (setq tags-list
  11660. (if org-use-tag-inheritance
  11661. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11662. tags)
  11663. org-scanner-tags tags-list)
  11664. (when org-use-tag-inheritance
  11665. (setcdr (car tags-alist)
  11666. (mapcar (lambda (x)
  11667. (setq x (copy-sequence x))
  11668. (org-add-prop-inherited x))
  11669. (cdar tags-alist))))
  11670. (when (and tags org-use-tag-inheritance
  11671. (or (not (eq t org-use-tag-inheritance))
  11672. org-tags-exclude-from-inheritance))
  11673. ;; selective inheritance, remove uninherited ones
  11674. (setcdr (car tags-alist)
  11675. (org-remove-uninherited-tags (cdar tags-alist))))
  11676. (when (and
  11677. ;; eval matcher only when the todo condition is OK
  11678. (and (or (not todo-only) (member todo org-not-done-keywords))
  11679. (let ((case-fold-search t)) (eval matcher)))
  11680. ;; Call the skipper, but return t if it does not skip,
  11681. ;; so that the `and' form continues evaluating
  11682. (progn
  11683. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11684. t)
  11685. ;; Check if timestamps are deselecting this entry
  11686. (or (not todo-only)
  11687. (and (member todo org-not-done-keywords)
  11688. (or (not org-agenda-tags-todo-honor-ignore-options)
  11689. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11690. ;; Extra check for the archive tag
  11691. ;; FIXME: Does the skipper already do this????
  11692. (or
  11693. (not (member org-archive-tag tags-list))
  11694. ;; we have an archive tag, should we use this anyway?
  11695. (or (not org-agenda-skip-archived-trees)
  11696. (and (eq action 'agenda) org-agenda-archives-mode))))
  11697. ;; select this headline
  11698. (cond
  11699. ((eq action 'sparse-tree)
  11700. (and org-highlight-sparse-tree-matches
  11701. (org-get-heading) (match-end 0)
  11702. (org-highlight-new-match
  11703. (match-beginning 0) (match-beginning 1)))
  11704. (org-show-context 'tags-tree))
  11705. ((eq action 'agenda)
  11706. (setq txt (org-agenda-format-item
  11707. ""
  11708. (concat
  11709. (if (eq org-tags-match-list-sublevels 'indented)
  11710. (make-string (1- level) ?.) "")
  11711. (org-get-heading))
  11712. category
  11713. tags-list)
  11714. priority (org-get-priority txt))
  11715. (goto-char lspos)
  11716. (setq marker (org-agenda-new-marker))
  11717. (org-add-props txt props
  11718. 'org-marker marker 'org-hd-marker marker 'org-category category
  11719. 'todo-state todo
  11720. 'priority priority 'type "tagsmatch")
  11721. (push txt rtn))
  11722. ((functionp action)
  11723. (setq org-map-continue-from nil)
  11724. (save-excursion
  11725. (setq rtn1 (funcall action))
  11726. (push rtn1 rtn)))
  11727. (t (error "Invalid action")))
  11728. ;; if we are to skip sublevels, jump to end of subtree
  11729. (unless org-tags-match-list-sublevels
  11730. (org-end-of-subtree t)
  11731. (backward-char 1))))
  11732. ;; Get the correct position from where to continue
  11733. (if org-map-continue-from
  11734. (goto-char org-map-continue-from)
  11735. (and (= (point) lspos) (end-of-line 1)))))
  11736. (when (and (eq action 'sparse-tree)
  11737. (not org-sparse-tree-open-archived-trees))
  11738. (org-hide-archived-subtrees (point-min) (point-max)))
  11739. (nreverse rtn)))
  11740. (defun org-remove-uninherited-tags (tags)
  11741. "Remove all tags that are not inherited from the list TAGS."
  11742. (cond
  11743. ((eq org-use-tag-inheritance t)
  11744. (if org-tags-exclude-from-inheritance
  11745. (org-delete-all org-tags-exclude-from-inheritance tags)
  11746. tags))
  11747. ((not org-use-tag-inheritance) nil)
  11748. ((stringp org-use-tag-inheritance)
  11749. (delq nil (mapcar
  11750. (lambda (x)
  11751. (if (and (string-match org-use-tag-inheritance x)
  11752. (not (member x org-tags-exclude-from-inheritance)))
  11753. x nil))
  11754. tags)))
  11755. ((listp org-use-tag-inheritance)
  11756. (delq nil (mapcar
  11757. (lambda (x)
  11758. (if (member x org-use-tag-inheritance) x nil))
  11759. tags)))))
  11760. (defvar todo-only) ;; dynamically scoped
  11761. (defun org-match-sparse-tree (&optional todo-only match)
  11762. "Create a sparse tree according to tags string MATCH.
  11763. MATCH can contain positive and negative selection of tags, like
  11764. \"+WORK+URGENT-WITHBOSS\".
  11765. If optional argument TODO-ONLY is non-nil, only select lines that are
  11766. also TODO lines."
  11767. (interactive "P")
  11768. (org-prepare-agenda-buffers (list (current-buffer)))
  11769. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11770. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11771. (defvar org-cached-props nil)
  11772. (defun org-cached-entry-get (pom property)
  11773. (if (or (eq t org-use-property-inheritance)
  11774. (and (stringp org-use-property-inheritance)
  11775. (string-match org-use-property-inheritance property))
  11776. (and (listp org-use-property-inheritance)
  11777. (member property org-use-property-inheritance)))
  11778. ;; Caching is not possible, check it directly
  11779. (org-entry-get pom property 'inherit)
  11780. ;; Get all properties, so that we can do complicated checks easily
  11781. (cdr (assoc property (or org-cached-props
  11782. (setq org-cached-props
  11783. (org-entry-properties pom)))))))
  11784. (defun org-global-tags-completion-table (&optional files)
  11785. "Return the list of all tags in all agenda buffer/files.
  11786. Optional FILES argument is a list of files to which can be used
  11787. instead of the agenda files."
  11788. (save-excursion
  11789. (org-uniquify
  11790. (delq nil
  11791. (apply 'append
  11792. (mapcar
  11793. (lambda (file)
  11794. (set-buffer (find-file-noselect file))
  11795. (append (org-get-buffer-tags)
  11796. (mapcar (lambda (x) (if (stringp (car-safe x))
  11797. (list (car-safe x)) nil))
  11798. org-tag-alist)))
  11799. (if (and files (car files))
  11800. files
  11801. (org-agenda-files))))))))
  11802. (defun org-make-tags-matcher (match)
  11803. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11804. ;; todo-only is scoped dynamically into this function, and the function
  11805. ;; may change it if the matcher asks for it.
  11806. (unless match
  11807. ;; Get a new match request, with completion
  11808. (let ((org-last-tags-completion-table
  11809. (org-global-tags-completion-table)))
  11810. (setq match (org-completing-read-no-i
  11811. "Match: " 'org-tags-completion-function nil nil nil
  11812. 'org-tags-history))))
  11813. ;; Parse the string and create a lisp form
  11814. (let ((match0 match)
  11815. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11816. minus tag mm
  11817. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11818. orterms term orlist re-p str-p level-p level-op time-p
  11819. prop-p pn pv po gv rest)
  11820. (if (string-match "/+" match)
  11821. ;; match contains also a todo-matching request
  11822. (progn
  11823. (setq tagsmatch (substring match 0 (match-beginning 0))
  11824. todomatch (substring match (match-end 0)))
  11825. (if (string-match "^!" todomatch)
  11826. (setq todo-only t todomatch (substring todomatch 1)))
  11827. (if (string-match "^\\s-*$" todomatch)
  11828. (setq todomatch nil)))
  11829. ;; only matching tags
  11830. (setq tagsmatch match todomatch nil))
  11831. ;; Make the tags matcher
  11832. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11833. (setq tagsmatcher t)
  11834. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11835. (while (setq term (pop orterms))
  11836. (while (and (equal (substring term -1) "\\") orterms)
  11837. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11838. (while (string-match re term)
  11839. (setq rest (substring term (match-end 0))
  11840. minus (and (match-end 1)
  11841. (equal (match-string 1 term) "-"))
  11842. tag (save-match-data (replace-regexp-in-string
  11843. "\\\\-" "-"
  11844. (match-string 2 term)))
  11845. re-p (equal (string-to-char tag) ?{)
  11846. level-p (match-end 4)
  11847. prop-p (match-end 5)
  11848. mm (cond
  11849. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11850. (level-p
  11851. (setq level-op (org-op-to-function (match-string 3 term)))
  11852. `(,level-op level ,(string-to-number
  11853. (match-string 4 term))))
  11854. (prop-p
  11855. (setq pn (match-string 5 term)
  11856. po (match-string 6 term)
  11857. pv (match-string 7 term)
  11858. re-p (equal (string-to-char pv) ?{)
  11859. str-p (equal (string-to-char pv) ?\")
  11860. time-p (save-match-data
  11861. (string-match "^\"[[<].*[]>]\"$" pv))
  11862. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11863. (if time-p (setq pv (org-matcher-time pv)))
  11864. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11865. (cond
  11866. ((equal pn "CATEGORY")
  11867. (setq gv '(get-text-property (point) 'org-category)))
  11868. ((equal pn "TODO")
  11869. (setq gv 'todo))
  11870. (t
  11871. (setq gv `(org-cached-entry-get nil ,pn))))
  11872. (if re-p
  11873. (if (eq po 'org<>)
  11874. `(not (string-match ,pv (or ,gv "")))
  11875. `(string-match ,pv (or ,gv "")))
  11876. (if str-p
  11877. `(,po (or ,gv "") ,pv)
  11878. `(,po (string-to-number (or ,gv ""))
  11879. ,(string-to-number pv) ))))
  11880. (t `(member ,tag tags-list)))
  11881. mm (if minus (list 'not mm) mm)
  11882. term rest)
  11883. (push mm tagsmatcher))
  11884. (push (if (> (length tagsmatcher) 1)
  11885. (cons 'and tagsmatcher)
  11886. (car tagsmatcher))
  11887. orlist)
  11888. (setq tagsmatcher nil))
  11889. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11890. (setq tagsmatcher
  11891. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11892. ;; Make the todo matcher
  11893. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11894. (setq todomatcher t)
  11895. (setq orterms (org-split-string todomatch "|") orlist nil)
  11896. (while (setq term (pop orterms))
  11897. (while (string-match re term)
  11898. (setq minus (and (match-end 1)
  11899. (equal (match-string 1 term) "-"))
  11900. kwd (match-string 2 term)
  11901. re-p (equal (string-to-char kwd) ?{)
  11902. term (substring term (match-end 0))
  11903. mm (if re-p
  11904. `(string-match ,(substring kwd 1 -1) todo)
  11905. (list 'equal 'todo kwd))
  11906. mm (if minus (list 'not mm) mm))
  11907. (push mm todomatcher))
  11908. (push (if (> (length todomatcher) 1)
  11909. (cons 'and todomatcher)
  11910. (car todomatcher))
  11911. orlist)
  11912. (setq todomatcher nil))
  11913. (setq todomatcher (if (> (length orlist) 1)
  11914. (cons 'or orlist) (car orlist))))
  11915. ;; Return the string and lisp forms of the matcher
  11916. (setq matcher (if todomatcher
  11917. (list 'and tagsmatcher todomatcher)
  11918. tagsmatcher))
  11919. (cons match0 matcher)))
  11920. (defun org-op-to-function (op &optional stringp)
  11921. "Turn an operator into the appropriate function."
  11922. (setq op
  11923. (cond
  11924. ((equal op "<" ) '(< string< org-time<))
  11925. ((equal op ">" ) '(> org-string> org-time>))
  11926. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11927. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11928. ((member op '("=" "==")) '(= string= org-time=))
  11929. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11930. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11931. (defun org<> (a b) (not (= a b)))
  11932. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11933. (defun org-string>= (a b) (not (string< a b)))
  11934. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11935. (defun org-string<> (a b) (not (string= a b)))
  11936. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11937. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11938. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11939. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11940. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11941. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11942. (defun org-2ft (s)
  11943. "Convert S to a floating point time.
  11944. If S is already a number, just return it. If it is a string, parse
  11945. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11946. (cond
  11947. ((numberp s) s)
  11948. ((stringp s)
  11949. (condition-case nil
  11950. (float-time (apply 'encode-time (org-parse-time-string s)))
  11951. (error 0.)))
  11952. (t 0.)))
  11953. (defun org-time-today ()
  11954. "Time in seconds today at 0:00.
  11955. Returns the float number of seconds since the beginning of the
  11956. epoch to the beginning of today (00:00)."
  11957. (float-time (apply 'encode-time
  11958. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11959. (defun org-matcher-time (s)
  11960. "Interpret a time comparison value."
  11961. (save-match-data
  11962. (cond
  11963. ((string= s "<now>") (float-time))
  11964. ((string= s "<today>") (org-time-today))
  11965. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11966. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11967. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11968. (+ (org-time-today)
  11969. (* (string-to-number (match-string 1 s))
  11970. (cdr (assoc (match-string 2 s)
  11971. '(("d" . 86400.0) ("w" . 604800.0)
  11972. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11973. (t (org-2ft s)))))
  11974. (defun org-match-any-p (re list)
  11975. "Does re match any element of list?"
  11976. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11977. (delq nil list))
  11978. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11979. (defvar org-tags-overlay (make-overlay 1 1))
  11980. (org-detach-overlay org-tags-overlay)
  11981. (defun org-get-local-tags-at (&optional pos)
  11982. "Get a list of tags defined in the current headline."
  11983. (org-get-tags-at pos 'local))
  11984. (defun org-get-local-tags ()
  11985. "Get a list of tags defined in the current headline."
  11986. (org-get-tags-at nil 'local))
  11987. (defun org-get-tags-at (&optional pos local)
  11988. "Get a list of all headline tags applicable at POS.
  11989. POS defaults to point. If tags are inherited, the list contains
  11990. the targets in the same sequence as the headlines appear, i.e.
  11991. the tags of the current headline come last.
  11992. When LOCAL is non-nil, only return tags from the current headline,
  11993. ignore inherited ones."
  11994. (interactive)
  11995. (if (and org-trust-scanner-tags
  11996. (or (not pos) (equal pos (point)))
  11997. (not local))
  11998. org-scanner-tags
  11999. (let (tags ltags lastpos parent)
  12000. (save-excursion
  12001. (save-restriction
  12002. (widen)
  12003. (goto-char (or pos (point)))
  12004. (save-match-data
  12005. (catch 'done
  12006. (condition-case nil
  12007. (progn
  12008. (org-back-to-heading t)
  12009. (while (not (equal lastpos (point)))
  12010. (setq lastpos (point))
  12011. (when (looking-at
  12012. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12013. (setq ltags (org-split-string
  12014. (org-match-string-no-properties 1) ":"))
  12015. (when parent
  12016. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12017. (setq tags (append
  12018. (if parent
  12019. (org-remove-uninherited-tags ltags)
  12020. ltags)
  12021. tags)))
  12022. (or org-use-tag-inheritance (throw 'done t))
  12023. (if local (throw 'done t))
  12024. (or (org-up-heading-safe) (error nil))
  12025. (setq parent t)))
  12026. (error nil)))))
  12027. (if local
  12028. tags
  12029. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12030. (defun org-add-prop-inherited (s)
  12031. (add-text-properties 0 (length s) '(inherited t) s)
  12032. s)
  12033. (defun org-toggle-tag (tag &optional onoff)
  12034. "Toggle the tag TAG for the current line.
  12035. If ONOFF is `on' or `off', don't toggle but set to this state."
  12036. (let (res current)
  12037. (save-excursion
  12038. (org-back-to-heading t)
  12039. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12040. (point-at-eol) t)
  12041. (progn
  12042. (setq current (match-string 1))
  12043. (replace-match ""))
  12044. (setq current ""))
  12045. (setq current (nreverse (org-split-string current ":")))
  12046. (cond
  12047. ((eq onoff 'on)
  12048. (setq res t)
  12049. (or (member tag current) (push tag current)))
  12050. ((eq onoff 'off)
  12051. (or (not (member tag current)) (setq current (delete tag current))))
  12052. (t (if (member tag current)
  12053. (setq current (delete tag current))
  12054. (setq res t)
  12055. (push tag current))))
  12056. (end-of-line 1)
  12057. (if current
  12058. (progn
  12059. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12060. (org-set-tags nil t))
  12061. (delete-horizontal-space))
  12062. (run-hooks 'org-after-tags-change-hook))
  12063. res))
  12064. (defun org-align-tags-here (to-col)
  12065. ;; Assumes that this is a headline
  12066. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12067. (beginning-of-line 1)
  12068. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12069. (< pos (match-beginning 2)))
  12070. (progn
  12071. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12072. (goto-char (match-beginning 1))
  12073. (insert " ")
  12074. (delete-region (point) (1+ (match-beginning 2)))
  12075. (setq ncol (max (current-column)
  12076. (1+ col)
  12077. (if (> to-col 0)
  12078. to-col
  12079. (- (abs to-col) tags-l))))
  12080. (setq p (point))
  12081. (insert (make-string (- ncol (current-column)) ?\ ))
  12082. (setq ncol (current-column))
  12083. (when indent-tabs-mode (tabify p (point-at-eol)))
  12084. (org-move-to-column (min ncol col) t))
  12085. (goto-char pos))))
  12086. (defun org-set-tags-command (&optional arg just-align)
  12087. "Call the set-tags command for the current entry."
  12088. (interactive "P")
  12089. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12090. (org-set-tags arg just-align)
  12091. (save-excursion
  12092. (org-back-to-heading t)
  12093. (org-set-tags arg just-align))))
  12094. (defun org-set-tags-to (data)
  12095. "Set the tags of the current entry to DATA, replacing the current tags.
  12096. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12097. If DATA is nil or the empty string, any tags will be removed."
  12098. (interactive "sTags: ")
  12099. (setq data
  12100. (cond
  12101. ((eq data nil) "")
  12102. ((equal data "") "")
  12103. ((stringp data)
  12104. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12105. ":"))
  12106. ((listp data)
  12107. (concat ":" (mapconcat 'identity data ":") ":"))
  12108. (t nil)))
  12109. (when data
  12110. (save-excursion
  12111. (org-back-to-heading t)
  12112. (when (looking-at org-complex-heading-regexp)
  12113. (if (match-end 5)
  12114. (progn
  12115. (goto-char (match-beginning 5))
  12116. (insert data)
  12117. (delete-region (point) (point-at-eol))
  12118. (org-set-tags nil 'align))
  12119. (goto-char (point-at-eol))
  12120. (insert " " data)
  12121. (org-set-tags nil 'align)))
  12122. (beginning-of-line 1)
  12123. (if (looking-at ".*?\\([ \t]+\\)$")
  12124. (delete-region (match-beginning 1) (match-end 1))))))
  12125. (defun org-align-all-tags ()
  12126. "Align the tags i all headings."
  12127. (interactive)
  12128. (save-excursion
  12129. (or (ignore-errors (org-back-to-heading t))
  12130. (outline-next-heading))
  12131. (if (org-at-heading-p)
  12132. (org-set-tags t)
  12133. (message "No headings"))))
  12134. (defvar org-indent-indentation-per-level)
  12135. (defun org-set-tags (&optional arg just-align)
  12136. "Set the tags for the current headline.
  12137. With prefix ARG, realign all tags in headings in the current buffer."
  12138. (interactive "P")
  12139. (let* ((re org-outline-regexp-bol)
  12140. (current (unless arg (org-get-tags-string)))
  12141. (col (current-column))
  12142. (org-setting-tags t)
  12143. table current-tags inherited-tags ; computed below when needed
  12144. tags p0 c0 c1 rpl di tc level)
  12145. (if arg
  12146. (save-excursion
  12147. (goto-char (point-min))
  12148. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12149. (while (re-search-forward re nil t)
  12150. (org-set-tags nil t)
  12151. (end-of-line 1)))
  12152. (message "All tags realigned to column %d" org-tags-column))
  12153. (if just-align
  12154. (setq tags current)
  12155. ;; Get a new set of tags from the user
  12156. (save-excursion
  12157. (setq table (append org-tag-persistent-alist
  12158. (or org-tag-alist (org-get-buffer-tags))
  12159. (and
  12160. org-complete-tags-always-offer-all-agenda-tags
  12161. (org-global-tags-completion-table
  12162. (org-agenda-files))))
  12163. org-last-tags-completion-table table
  12164. current-tags (org-split-string current ":")
  12165. inherited-tags (nreverse
  12166. (nthcdr (length current-tags)
  12167. (nreverse (org-get-tags-at))))
  12168. tags
  12169. (if (or (eq t org-use-fast-tag-selection)
  12170. (and org-use-fast-tag-selection
  12171. (delq nil (mapcar 'cdr table))))
  12172. (org-fast-tag-selection
  12173. current-tags inherited-tags table
  12174. (if org-fast-tag-selection-include-todo
  12175. org-todo-key-alist))
  12176. (let ((org-add-colon-after-tag-completion t))
  12177. (org-trim
  12178. (org-icompleting-read "Tags: "
  12179. 'org-tags-completion-function
  12180. nil nil current 'org-tags-history))))))
  12181. (while (string-match "[-+&]+" tags)
  12182. ;; No boolean logic, just a list
  12183. (setq tags (replace-match ":" t t tags))))
  12184. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12185. (if org-tags-sort-function
  12186. (setq tags (mapconcat 'identity
  12187. (sort (org-split-string
  12188. tags (org-re "[^[:alnum:]_@#%]+"))
  12189. org-tags-sort-function) ":")))
  12190. (if (string-match "\\`[\t ]*\\'" tags)
  12191. (setq tags "")
  12192. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12193. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12194. ;; Insert new tags at the correct column
  12195. (beginning-of-line 1)
  12196. (setq level (or (and (looking-at org-outline-regexp)
  12197. (- (match-end 0) (point) 1))
  12198. 1))
  12199. (cond
  12200. ((and (equal current "") (equal tags "")))
  12201. ((re-search-forward
  12202. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12203. (point-at-eol) t)
  12204. (if (equal tags "")
  12205. (setq rpl "")
  12206. (goto-char (match-beginning 0))
  12207. (setq c0 (current-column)
  12208. ;; compute offset for the case of org-indent-mode active
  12209. di (if org-indent-mode
  12210. (* (1- org-indent-indentation-per-level) (1- level))
  12211. 0)
  12212. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12213. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12214. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12215. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12216. (replace-match rpl t t)
  12217. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12218. tags)
  12219. (t (error "Tags alignment failed")))
  12220. (org-move-to-column col)
  12221. (unless just-align
  12222. (run-hooks 'org-after-tags-change-hook)))))
  12223. (defun org-change-tag-in-region (beg end tag off)
  12224. "Add or remove TAG for each entry in the region.
  12225. This works in the agenda, and also in an org-mode buffer."
  12226. (interactive
  12227. (list (region-beginning) (region-end)
  12228. (let ((org-last-tags-completion-table
  12229. (if (eq major-mode 'org-mode)
  12230. (org-get-buffer-tags)
  12231. (org-global-tags-completion-table))))
  12232. (org-icompleting-read
  12233. "Tag: " 'org-tags-completion-function nil nil nil
  12234. 'org-tags-history))
  12235. (progn
  12236. (message "[s]et or [r]emove? ")
  12237. (equal (read-char-exclusive) ?r))))
  12238. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12239. (let ((agendap (equal major-mode 'org-agenda-mode))
  12240. l1 l2 m buf pos newhead (cnt 0))
  12241. (goto-char end)
  12242. (setq l2 (1- (org-current-line)))
  12243. (goto-char beg)
  12244. (setq l1 (org-current-line))
  12245. (loop for l from l1 to l2 do
  12246. (org-goto-line l)
  12247. (setq m (get-text-property (point) 'org-hd-marker))
  12248. (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
  12249. (and agendap m))
  12250. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12251. pos (if agendap m (point)))
  12252. (with-current-buffer buf
  12253. (save-excursion
  12254. (save-restriction
  12255. (goto-char pos)
  12256. (setq cnt (1+ cnt))
  12257. (org-toggle-tag tag (if off 'off 'on))
  12258. (setq newhead (org-get-heading)))))
  12259. (and agendap (org-agenda-change-all-lines newhead m))))
  12260. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12261. (defun org-tags-completion-function (string predicate &optional flag)
  12262. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12263. (confirm (lambda (x) (stringp (car x)))))
  12264. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12265. (setq s1 (match-string 1 string)
  12266. s2 (match-string 2 string))
  12267. (setq s1 "" s2 string))
  12268. (cond
  12269. ((eq flag nil)
  12270. ;; try completion
  12271. (setq rtn (try-completion s2 ctable confirm))
  12272. (if (stringp rtn)
  12273. (setq rtn
  12274. (concat s1 s2 (substring rtn (length s2))
  12275. (if (and org-add-colon-after-tag-completion
  12276. (assoc rtn ctable))
  12277. ":" ""))))
  12278. rtn)
  12279. ((eq flag t)
  12280. ;; all-completions
  12281. (all-completions s2 ctable confirm)
  12282. )
  12283. ((eq flag 'lambda)
  12284. ;; exact match?
  12285. (assoc s2 ctable)))
  12286. ))
  12287. (defun org-fast-tag-insert (kwd tags face &optional end)
  12288. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12289. (insert (format "%-12s" (concat kwd ":"))
  12290. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12291. (or end "")))
  12292. (defun org-fast-tag-show-exit (flag)
  12293. (save-excursion
  12294. (org-goto-line 3)
  12295. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12296. (replace-match ""))
  12297. (when flag
  12298. (end-of-line 1)
  12299. (org-move-to-column (- (window-width) 19) t)
  12300. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12301. (defun org-set-current-tags-overlay (current prefix)
  12302. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12303. (if (featurep 'xemacs)
  12304. (org-overlay-display org-tags-overlay (concat prefix s)
  12305. 'secondary-selection)
  12306. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12307. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12308. (defvar org-last-tag-selection-key nil)
  12309. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12310. "Fast tag selection with single keys.
  12311. CURRENT is the current list of tags in the headline, INHERITED is the
  12312. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12313. possibly with grouping information. TODO-TABLE is a similar table with
  12314. TODO keywords, should these have keys assigned to them.
  12315. If the keys are nil, a-z are automatically assigned.
  12316. Returns the new tags string, or nil to not change the current settings."
  12317. (let* ((fulltable (append table todo-table))
  12318. (maxlen (apply 'max (mapcar
  12319. (lambda (x)
  12320. (if (stringp (car x)) (string-width (car x)) 0))
  12321. fulltable)))
  12322. (buf (current-buffer))
  12323. (expert (eq org-fast-tag-selection-single-key 'expert))
  12324. (buffer-tags nil)
  12325. (fwidth (+ maxlen 3 1 3))
  12326. (ncol (/ (- (window-width) 4) fwidth))
  12327. (i-face 'org-done)
  12328. (c-face 'org-todo)
  12329. tg cnt e c char c1 c2 ntable tbl rtn
  12330. ov-start ov-end ov-prefix
  12331. (exit-after-next org-fast-tag-selection-single-key)
  12332. (done-keywords org-done-keywords)
  12333. groups ingroup)
  12334. (save-excursion
  12335. (beginning-of-line 1)
  12336. (if (looking-at
  12337. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12338. (setq ov-start (match-beginning 1)
  12339. ov-end (match-end 1)
  12340. ov-prefix "")
  12341. (setq ov-start (1- (point-at-eol))
  12342. ov-end (1+ ov-start))
  12343. (skip-chars-forward "^\n\r")
  12344. (setq ov-prefix
  12345. (concat
  12346. (buffer-substring (1- (point)) (point))
  12347. (if (> (current-column) org-tags-column)
  12348. " "
  12349. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12350. (move-overlay org-tags-overlay ov-start ov-end)
  12351. (save-window-excursion
  12352. (if expert
  12353. (set-buffer (get-buffer-create " *Org tags*"))
  12354. (delete-other-windows)
  12355. (split-window-vertically)
  12356. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12357. (erase-buffer)
  12358. (org-set-local 'org-done-keywords done-keywords)
  12359. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12360. (org-fast-tag-insert "Current" current c-face "\n\n")
  12361. (org-fast-tag-show-exit exit-after-next)
  12362. (org-set-current-tags-overlay current ov-prefix)
  12363. (setq tbl fulltable char ?a cnt 0)
  12364. (while (setq e (pop tbl))
  12365. (cond
  12366. ((equal (car e) :startgroup)
  12367. (push '() groups) (setq ingroup t)
  12368. (when (not (= cnt 0))
  12369. (setq cnt 0)
  12370. (insert "\n"))
  12371. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12372. ((equal (car e) :endgroup)
  12373. (setq ingroup nil cnt 0)
  12374. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12375. ((equal e '(:newline))
  12376. (when (not (= cnt 0))
  12377. (setq cnt 0)
  12378. (insert "\n")
  12379. (setq e (car tbl))
  12380. (while (equal (car tbl) '(:newline))
  12381. (insert "\n")
  12382. (setq tbl (cdr tbl)))))
  12383. (t
  12384. (setq tg (copy-sequence (car e)) c2 nil)
  12385. (if (cdr e)
  12386. (setq c (cdr e))
  12387. ;; automatically assign a character.
  12388. (setq c1 (string-to-char
  12389. (downcase (substring
  12390. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12391. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12392. (while (or (rassoc char ntable) (rassoc char table))
  12393. (setq char (1+ char)))
  12394. (setq c2 c1))
  12395. (setq c (or c2 char)))
  12396. (if ingroup (push tg (car groups)))
  12397. (setq tg (org-add-props tg nil 'face
  12398. (cond
  12399. ((not (assoc tg table))
  12400. (org-get-todo-face tg))
  12401. ((member tg current) c-face)
  12402. ((member tg inherited) i-face)
  12403. (t nil))))
  12404. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12405. (insert "[" c "] " tg (make-string
  12406. (- fwidth 4 (length tg)) ?\ ))
  12407. (push (cons tg c) ntable)
  12408. (when (= (setq cnt (1+ cnt)) ncol)
  12409. (insert "\n")
  12410. (if ingroup (insert " "))
  12411. (setq cnt 0)))))
  12412. (setq ntable (nreverse ntable))
  12413. (insert "\n")
  12414. (goto-char (point-min))
  12415. (if (not expert) (org-fit-window-to-buffer))
  12416. (setq rtn
  12417. (catch 'exit
  12418. (while t
  12419. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12420. (if (not groups) "no " "")
  12421. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12422. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12423. (setq org-last-tag-selection-key c)
  12424. (cond
  12425. ((= c ?\r) (throw 'exit t))
  12426. ((= c ?!)
  12427. (setq groups (not groups))
  12428. (goto-char (point-min))
  12429. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12430. ((= c ?\C-c)
  12431. (if (not expert)
  12432. (org-fast-tag-show-exit
  12433. (setq exit-after-next (not exit-after-next)))
  12434. (setq expert nil)
  12435. (delete-other-windows)
  12436. (set-window-buffer (split-window-vertically) " *Org tags*")
  12437. (org-switch-to-buffer-other-window " *Org tags*")
  12438. (org-fit-window-to-buffer)))
  12439. ((or (= c ?\C-g)
  12440. (and (= c ?q) (not (rassoc c ntable))))
  12441. (org-detach-overlay org-tags-overlay)
  12442. (setq quit-flag t))
  12443. ((= c ?\ )
  12444. (setq current nil)
  12445. (if exit-after-next (setq exit-after-next 'now)))
  12446. ((= c ?\t)
  12447. (condition-case nil
  12448. (setq tg (org-icompleting-read
  12449. "Tag: "
  12450. (or buffer-tags
  12451. (with-current-buffer buf
  12452. (org-get-buffer-tags)))))
  12453. (quit (setq tg "")))
  12454. (when (string-match "\\S-" tg)
  12455. (add-to-list 'buffer-tags (list tg))
  12456. (if (member tg current)
  12457. (setq current (delete tg current))
  12458. (push tg current)))
  12459. (if exit-after-next (setq exit-after-next 'now)))
  12460. ((setq e (rassoc c todo-table) tg (car e))
  12461. (with-current-buffer buf
  12462. (save-excursion (org-todo tg)))
  12463. (if exit-after-next (setq exit-after-next 'now)))
  12464. ((setq e (rassoc c ntable) tg (car e))
  12465. (if (member tg current)
  12466. (setq current (delete tg current))
  12467. (loop for g in groups do
  12468. (if (member tg g)
  12469. (mapc (lambda (x)
  12470. (setq current (delete x current)))
  12471. g)))
  12472. (push tg current))
  12473. (if exit-after-next (setq exit-after-next 'now))))
  12474. ;; Create a sorted list
  12475. (setq current
  12476. (sort current
  12477. (lambda (a b)
  12478. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12479. (if (eq exit-after-next 'now) (throw 'exit t))
  12480. (goto-char (point-min))
  12481. (beginning-of-line 2)
  12482. (delete-region (point) (point-at-eol))
  12483. (org-fast-tag-insert "Current" current c-face)
  12484. (org-set-current-tags-overlay current ov-prefix)
  12485. (while (re-search-forward
  12486. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12487. (setq tg (match-string 1))
  12488. (add-text-properties
  12489. (match-beginning 1) (match-end 1)
  12490. (list 'face
  12491. (cond
  12492. ((member tg current) c-face)
  12493. ((member tg inherited) i-face)
  12494. (t (get-text-property (match-beginning 1) 'face))))))
  12495. (goto-char (point-min)))))
  12496. (org-detach-overlay org-tags-overlay)
  12497. (if rtn
  12498. (mapconcat 'identity current ":")
  12499. nil))))
  12500. (defun org-get-tags-string ()
  12501. "Get the TAGS string in the current headline."
  12502. (unless (org-at-heading-p t)
  12503. (error "Not on a heading"))
  12504. (save-excursion
  12505. (beginning-of-line 1)
  12506. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12507. (org-match-string-no-properties 1)
  12508. "")))
  12509. (defun org-get-tags ()
  12510. "Get the list of tags specified in the current headline."
  12511. (org-split-string (org-get-tags-string) ":"))
  12512. (defun org-get-buffer-tags ()
  12513. "Get a table of all tags used in the buffer, for completion."
  12514. (let (tags)
  12515. (save-excursion
  12516. (goto-char (point-min))
  12517. (while (re-search-forward
  12518. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12519. (when (equal (char-after (point-at-bol 0)) ?*)
  12520. (mapc (lambda (x) (add-to-list 'tags x))
  12521. (org-split-string (org-match-string-no-properties 1) ":")))))
  12522. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12523. (mapcar 'list tags)))
  12524. ;;;; The mapping API
  12525. ;;;###autoload
  12526. (defun org-map-entries (func &optional match scope &rest skip)
  12527. "Call FUNC at each headline selected by MATCH in SCOPE.
  12528. FUNC is a function or a lisp form. The function will be called without
  12529. arguments, with the cursor positioned at the beginning of the headline.
  12530. The return values of all calls to the function will be collected and
  12531. returned as a list.
  12532. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12533. does not need to preserve point. After evaluation, the cursor will be
  12534. moved to the end of the line (presumably of the headline of the
  12535. processed entry) and search continues from there. Under some
  12536. circumstances, this may not produce the wanted results. For example,
  12537. if you have removed (e.g. archived) the current (sub)tree it could
  12538. mean that the next entry will be skipped entirely. In such cases, you
  12539. can specify the position from where search should continue by making
  12540. FUNC set the variable `org-map-continue-from' to the desired buffer
  12541. position.
  12542. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12543. Only headlines that are matched by this query will be considered during
  12544. the iteration. When MATCH is nil or t, all headlines will be
  12545. visited by the iteration.
  12546. SCOPE determines the scope of this command. It can be any of:
  12547. nil The current buffer, respecting the restriction if any
  12548. tree The subtree started with the entry at point
  12549. region The entries within the active region, if any
  12550. region-start-level
  12551. The entries within the active region, but only those at
  12552. the same level than the first one.
  12553. file The current buffer, without restriction
  12554. file-with-archives
  12555. The current buffer, and any archives associated with it
  12556. agenda All agenda files
  12557. agenda-with-archives
  12558. All agenda files with any archive files associated with them
  12559. \(file1 file2 ...)
  12560. If this is a list, all files in the list will be scanned
  12561. The remaining args are treated as settings for the skipping facilities of
  12562. the scanner. The following items can be given here:
  12563. archive skip trees with the archive tag.
  12564. comment skip trees with the COMMENT keyword
  12565. function or Emacs Lisp form:
  12566. will be used as value for `org-agenda-skip-function', so whenever
  12567. the function returns t, FUNC will not be called for that
  12568. entry and search will continue from the point where the
  12569. function leaves it.
  12570. If your function needs to retrieve the tags including inherited tags
  12571. at the *current* entry, you can use the value of the variable
  12572. `org-scanner-tags' which will be much faster than getting the value
  12573. with `org-get-tags-at'. If your function gets properties with
  12574. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12575. to t around the call to `org-entry-properties' to get the same speedup.
  12576. Note that if your function moves around to retrieve tags and properties at
  12577. a *different* entry, you cannot use these techniques."
  12578. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12579. (not (org-region-active-p)))
  12580. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12581. (org-agenda-skip-archived-trees (memq 'archive skip))
  12582. (org-agenda-skip-comment-trees (memq 'comment skip))
  12583. (org-agenda-skip-function
  12584. (car (org-delete-all '(comment archive) skip)))
  12585. (org-tags-match-list-sublevels t)
  12586. (start-level (eq scope 'region-start-level))
  12587. matcher file res
  12588. org-todo-keywords-for-agenda
  12589. org-done-keywords-for-agenda
  12590. org-todo-keyword-alist-for-agenda
  12591. org-drawers-for-agenda
  12592. org-tag-alist-for-agenda)
  12593. (cond
  12594. ((eq match t) (setq matcher t))
  12595. ((eq match nil) (setq matcher t))
  12596. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12597. (save-excursion
  12598. (save-restriction
  12599. (cond ((eq scope 'tree)
  12600. (org-back-to-heading t)
  12601. (org-narrow-to-subtree)
  12602. (setq scope nil))
  12603. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12604. (org-region-active-p))
  12605. ;; If needed, set start-level to a string like "2"
  12606. (when start-level
  12607. (save-excursion
  12608. (goto-char (region-beginning))
  12609. (unless (org-at-heading-p) (outline-next-heading))
  12610. (setq start-level (org-current-level))))
  12611. (narrow-to-region (region-beginning)
  12612. (save-excursion
  12613. (goto-char (region-end))
  12614. (unless (and (bolp) (org-at-heading-p))
  12615. (outline-next-heading))
  12616. (point)))
  12617. (setq scope nil)))
  12618. (if (not scope)
  12619. (progn
  12620. (org-prepare-agenda-buffers
  12621. (list (buffer-file-name (current-buffer))))
  12622. (setq res (org-scan-tags func matcher nil start-level)))
  12623. ;; Get the right scope
  12624. (cond
  12625. ((and scope (listp scope) (symbolp (car scope)))
  12626. (setq scope (eval scope)))
  12627. ((eq scope 'agenda)
  12628. (setq scope (org-agenda-files t)))
  12629. ((eq scope 'agenda-with-archives)
  12630. (setq scope (org-agenda-files t))
  12631. (setq scope (org-add-archive-files scope)))
  12632. ((eq scope 'file)
  12633. (setq scope (list (buffer-file-name))))
  12634. ((eq scope 'file-with-archives)
  12635. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12636. (org-prepare-agenda-buffers scope)
  12637. (while (setq file (pop scope))
  12638. (with-current-buffer (org-find-base-buffer-visiting file)
  12639. (save-excursion
  12640. (save-restriction
  12641. (widen)
  12642. (goto-char (point-min))
  12643. (setq res (append res (org-scan-tags func matcher))))))))))
  12644. res)))
  12645. ;;;; Properties
  12646. ;;; Setting and retrieving properties
  12647. (defconst org-special-properties
  12648. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12649. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12650. "The special properties valid in Org-mode.
  12651. These are properties that are not defined in the property drawer,
  12652. but in some other way.")
  12653. (defconst org-default-properties
  12654. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12655. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12656. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12657. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12658. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12659. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12660. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12661. "Some properties that are used by Org-mode for various purposes.
  12662. Being in this list makes sure that they are offered for completion.")
  12663. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12664. "Regular expression matching the first line of a property drawer.")
  12665. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12666. "Regular expression matching the last line of a property drawer.")
  12667. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12668. "Regular expression matching the first line of a property drawer.")
  12669. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12670. "Regular expression matching the first line of a property drawer.")
  12671. (defconst org-property-drawer-re
  12672. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12673. org-property-end-re "\\)\n?")
  12674. "Matches an entire property drawer.")
  12675. (defconst org-clock-drawer-re
  12676. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12677. org-property-end-re "\\)\n?")
  12678. "Matches an entire clock drawer.")
  12679. (defsubst org-re-property (property)
  12680. "Return a regexp matching PROPERTY.
  12681. Match group 1 will be set to the value "
  12682. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12683. (defun org-property-action ()
  12684. "Do an action on properties."
  12685. (interactive)
  12686. (let (c)
  12687. (org-at-property-p)
  12688. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12689. (setq c (read-char-exclusive))
  12690. (cond
  12691. ((equal c ?s)
  12692. (call-interactively 'org-set-property))
  12693. ((equal c ?d)
  12694. (call-interactively 'org-delete-property))
  12695. ((equal c ?D)
  12696. (call-interactively 'org-delete-property-globally))
  12697. ((equal c ?c)
  12698. (call-interactively 'org-compute-property-at-point))
  12699. (t (error "No such property action %c" c)))))
  12700. (defun org-set-effort (&optional value)
  12701. "Set the effort property of the current entry.
  12702. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12703. allowed value."
  12704. (interactive "P")
  12705. (if (equal value 0) (setq value 10))
  12706. (let* ((completion-ignore-case t)
  12707. (prop org-effort-property)
  12708. (cur (org-entry-get nil prop))
  12709. (allowed (org-property-get-allowed-values nil prop 'table))
  12710. (existing (mapcar 'list (org-property-values prop)))
  12711. rpl
  12712. (val (cond
  12713. ((stringp value) value)
  12714. ((and allowed (integerp value))
  12715. (or (car (nth (1- value) allowed))
  12716. (car (org-last allowed))))
  12717. (allowed
  12718. (message "Select 1-9,0, [RET%s]: %s"
  12719. (if cur (concat "=" cur) "")
  12720. (mapconcat 'car allowed " "))
  12721. (setq rpl (read-char-exclusive))
  12722. (if (equal rpl ?\r)
  12723. cur
  12724. (setq rpl (- rpl ?0))
  12725. (if (equal rpl 0) (setq rpl 10))
  12726. (if (and (> rpl 0) (<= rpl (length allowed)))
  12727. (car (nth (1- rpl) allowed))
  12728. (org-completing-read "Effort: " allowed nil))))
  12729. (t
  12730. (let (org-completion-use-ido org-completion-use-iswitchb)
  12731. (org-completing-read
  12732. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12733. (concat "[" cur "]") "")
  12734. ": ")
  12735. existing nil nil "" nil cur))))))
  12736. (unless (equal (org-entry-get nil prop) val)
  12737. (org-entry-put nil prop val))
  12738. (message "%s is now %s" prop val)))
  12739. (defun org-at-property-p ()
  12740. "Is cursor inside a property drawer?"
  12741. (save-excursion
  12742. (beginning-of-line 1)
  12743. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12744. (save-match-data ;; Used by calling procedures
  12745. (let ((p (point))
  12746. (range (unless (org-before-first-heading-p)
  12747. (org-get-property-block))))
  12748. (and range (<= (car range) p) (< p (cdr range))))))))
  12749. (defun org-get-property-block (&optional beg end force)
  12750. "Return the (beg . end) range of the body of the property drawer.
  12751. BEG and END can be beginning and end of subtree, if not given
  12752. they will be found.
  12753. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12754. (catch 'exit
  12755. (save-excursion
  12756. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12757. (end (or end (progn (outline-next-heading) (point)))))
  12758. (goto-char beg)
  12759. (if (re-search-forward org-property-start-re end t)
  12760. (setq beg (1+ (match-end 0)))
  12761. (if force
  12762. (save-excursion
  12763. (org-insert-property-drawer)
  12764. (setq end (progn (outline-next-heading) (point))))
  12765. (throw 'exit nil))
  12766. (goto-char beg)
  12767. (if (re-search-forward org-property-start-re end t)
  12768. (setq beg (1+ (match-end 0)))))
  12769. (if (re-search-forward org-property-end-re end t)
  12770. (setq end (match-beginning 0))
  12771. (or force (throw 'exit nil))
  12772. (goto-char beg)
  12773. (setq end beg)
  12774. (org-indent-line-function)
  12775. (insert ":END:\n"))
  12776. (cons beg end)))))
  12777. (defun org-entry-properties (&optional pom which specific)
  12778. "Get all properties of the entry at point-or-marker POM.
  12779. This includes the TODO keyword, the tags, time strings for deadline,
  12780. scheduled, and clocking, and any additional properties defined in the
  12781. entry. The return value is an alist, keys may occur multiple times
  12782. if the property key was used several times.
  12783. POM may also be nil, in which case the current entry is used.
  12784. If WHICH is nil or `all', get all properties. If WHICH is
  12785. `special' or `standard', only get that subclass. If WHICH
  12786. is a string only get exactly this property. SPECIFIC can be a string, the
  12787. specific property we are interested in. Specifying it can speed
  12788. things up because then unnecessary parsing is avoided."
  12789. (setq which (or which 'all))
  12790. (org-with-point-at pom
  12791. (let ((clockstr (substring org-clock-string 0 -1))
  12792. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12793. (case-fold-search nil)
  12794. beg end range props sum-props key key1 value string clocksum)
  12795. (save-excursion
  12796. (when (condition-case nil
  12797. (and (eq major-mode 'org-mode) (org-back-to-heading t))
  12798. (error nil))
  12799. (setq beg (point))
  12800. (setq sum-props (get-text-property (point) 'org-summaries))
  12801. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12802. (outline-next-heading)
  12803. (setq end (point))
  12804. (when (memq which '(all special))
  12805. ;; Get the special properties, like TODO and tags
  12806. (goto-char beg)
  12807. (when (and (or (not specific) (string= specific "TODO"))
  12808. (looking-at org-todo-line-regexp) (match-end 2))
  12809. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12810. (when (and (or (not specific) (string= specific "PRIORITY"))
  12811. (looking-at org-priority-regexp))
  12812. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12813. (when (or (not specific) (string= specific "FILE"))
  12814. (push (cons "FILE" buffer-file-name) props))
  12815. (when (and (or (not specific) (string= specific "TAGS"))
  12816. (setq value (org-get-tags-string))
  12817. (string-match "\\S-" value))
  12818. (push (cons "TAGS" value) props))
  12819. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12820. (setq value (org-get-tags-at)))
  12821. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12822. ":"))
  12823. props))
  12824. (when (or (not specific) (string= specific "BLOCKED"))
  12825. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12826. (when (or (not specific)
  12827. (member specific
  12828. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12829. "TIMESTAMP" "TIMESTAMP_IA")))
  12830. (catch 'match
  12831. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12832. (setq key (if (match-end 1)
  12833. (substring (org-match-string-no-properties 1)
  12834. 0 -1))
  12835. string (if (equal key clockstr)
  12836. (org-no-properties
  12837. (org-trim
  12838. (buffer-substring
  12839. (match-beginning 3) (goto-char
  12840. (point-at-eol)))))
  12841. (substring (org-match-string-no-properties 3)
  12842. 1 -1)))
  12843. ;; Get the correct property name from the key. This is
  12844. ;; necessary if the user has configured time keywords.
  12845. (setq key1 (concat key ":"))
  12846. (cond
  12847. ((not key)
  12848. (setq key
  12849. (if (= (char-after (match-beginning 3)) ?\[)
  12850. "TIMESTAMP_IA" "TIMESTAMP")))
  12851. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12852. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12853. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12854. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12855. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12856. (progn
  12857. (push (cons key string) props)
  12858. ;; no need to search further if match is found
  12859. (throw 'match t))
  12860. (when (or (equal key "CLOCK") (not (assoc key props)))
  12861. (push (cons key string) props))))))
  12862. )
  12863. (when (memq which '(all standard))
  12864. ;; Get the standard properties, like :PROP: ...
  12865. (setq range (org-get-property-block beg end))
  12866. (when range
  12867. (goto-char (car range))
  12868. (while (re-search-forward
  12869. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12870. (cdr range) t)
  12871. (setq key (org-match-string-no-properties 1)
  12872. value (org-trim (or (org-match-string-no-properties 2) "")))
  12873. (unless (member key excluded)
  12874. (push (cons key (or value "")) props)))))
  12875. (if clocksum
  12876. (push (cons "CLOCKSUM"
  12877. (org-columns-number-to-string (/ (float clocksum) 60.)
  12878. 'add_times))
  12879. props))
  12880. (unless (assoc "CATEGORY" props)
  12881. (push (cons "CATEGORY" (org-get-category)) props))
  12882. (append sum-props (nreverse props)))))))
  12883. (defun org-entry-get (pom property &optional inherit literal-nil)
  12884. "Get value of PROPERTY for entry at point-or-marker POM.
  12885. If INHERIT is non-nil and the entry does not have the property,
  12886. then also check higher levels of the hierarchy.
  12887. If INHERIT is the symbol `selective', use inheritance only if the setting
  12888. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12889. If the property is present but empty, the return value is the empty string.
  12890. If the property is not present at all, nil is returned.
  12891. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12892. do not interpret it as the list atom nil. This is used for inheritance
  12893. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12894. (org-with-point-at pom
  12895. (if (and inherit (if (eq inherit 'selective)
  12896. (org-property-inherit-p property)
  12897. t))
  12898. (org-entry-get-with-inheritance property literal-nil)
  12899. (if (member property org-special-properties)
  12900. ;; We need a special property. Use `org-entry-properties' to
  12901. ;; retrieve it, but specify the wanted property
  12902. (cdr (assoc property (org-entry-properties nil 'special property)))
  12903. (let ((range (unless (org-before-first-heading-p)
  12904. (org-get-property-block))))
  12905. (when (and range (goto-char (car range)))
  12906. ((lambda (val) (when val (if literal-nil val (org-not-nil val))))
  12907. (cond
  12908. ((re-search-forward
  12909. (org-re-property property) (cdr range) t)
  12910. (if (match-end 1) (org-match-string-no-properties 1) ""))
  12911. ((re-search-forward
  12912. (org-re-property (concat property "+")) (cdr range) t)
  12913. (cdr (assoc
  12914. property
  12915. (org-update-property-plist
  12916. (concat property "+")
  12917. (if (match-end 1) (org-match-string-no-properties 1) "")
  12918. (list (or (assoc property org-file-properties)
  12919. (assoc property org-global-properties)
  12920. (assoc property org-global-properties-fixed)
  12921. ))))))))))))))
  12922. (defun org-property-or-variable-value (var &optional inherit)
  12923. "Check if there is a property fixing the value of VAR.
  12924. If yes, return this value. If not, return the current value of the variable."
  12925. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12926. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12927. (read prop)
  12928. (symbol-value var))))
  12929. (defun org-entry-delete (pom property)
  12930. "Delete the property PROPERTY from entry at point-or-marker POM."
  12931. (org-with-point-at pom
  12932. (if (member property org-special-properties)
  12933. nil ; cannot delete these properties.
  12934. (let ((range (org-get-property-block)))
  12935. (if (and range
  12936. (goto-char (car range))
  12937. (re-search-forward
  12938. (org-re-property property)
  12939. (cdr range) t))
  12940. (progn
  12941. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12942. t)
  12943. nil)))))
  12944. ;; Multi-values properties are properties that contain multiple values
  12945. ;; These values are assumed to be single words, separated by whitespace.
  12946. (defun org-entry-add-to-multivalued-property (pom property value)
  12947. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12948. (let* ((old (org-entry-get pom property))
  12949. (values (and old (org-split-string old "[ \t]"))))
  12950. (setq value (org-entry-protect-space value))
  12951. (unless (member value values)
  12952. (setq values (cons value values))
  12953. (org-entry-put pom property
  12954. (mapconcat 'identity values " ")))))
  12955. (defun org-entry-remove-from-multivalued-property (pom property value)
  12956. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12957. (let* ((old (org-entry-get pom property))
  12958. (values (and old (org-split-string old "[ \t]"))))
  12959. (setq value (org-entry-protect-space value))
  12960. (when (member value values)
  12961. (setq values (delete value values))
  12962. (org-entry-put pom property
  12963. (mapconcat 'identity values " ")))))
  12964. (defun org-entry-member-in-multivalued-property (pom property value)
  12965. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12966. (let* ((old (org-entry-get pom property))
  12967. (values (and old (org-split-string old "[ \t]"))))
  12968. (setq value (org-entry-protect-space value))
  12969. (member value values)))
  12970. (defun org-entry-get-multivalued-property (pom property)
  12971. "Return a list of values in a multivalued property."
  12972. (let* ((value (org-entry-get pom property))
  12973. (values (and value (org-split-string value "[ \t]"))))
  12974. (mapcar 'org-entry-restore-space values)))
  12975. (defun org-entry-put-multivalued-property (pom property &rest values)
  12976. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12977. VALUES should be a list of strings. Spaces will be protected."
  12978. (org-entry-put pom property
  12979. (mapconcat 'org-entry-protect-space values " "))
  12980. (let* ((value (org-entry-get pom property))
  12981. (values (and value (org-split-string value "[ \t]"))))
  12982. (mapcar 'org-entry-restore-space values)))
  12983. (defun org-entry-protect-space (s)
  12984. "Protect spaces and newline in string S."
  12985. (while (string-match " " s)
  12986. (setq s (replace-match "%20" t t s)))
  12987. (while (string-match "\n" s)
  12988. (setq s (replace-match "%0A" t t s)))
  12989. s)
  12990. (defun org-entry-restore-space (s)
  12991. "Restore spaces and newline in string S."
  12992. (while (string-match "%20" s)
  12993. (setq s (replace-match " " t t s)))
  12994. (while (string-match "%0A" s)
  12995. (setq s (replace-match "\n" t t s)))
  12996. s)
  12997. (defvar org-entry-property-inherited-from (make-marker)
  12998. "Marker pointing to the entry from where a property was inherited.
  12999. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13000. location of the entry where the inheritance search matched. If there was
  13001. no match, the marker will point nowhere.
  13002. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13003. is set.")
  13004. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13005. "Get entry property, and search higher levels if not present.
  13006. The search will stop at the first ancestor which has the property defined.
  13007. If the value found is \"nil\", return nil to show that the property
  13008. should be considered as undefined (this is the meaning of nil here).
  13009. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13010. (move-marker org-entry-property-inherited-from nil)
  13011. (let (tmp)
  13012. (unless (org-before-first-heading-p)
  13013. (save-excursion
  13014. (save-restriction
  13015. (widen)
  13016. (catch 'ex
  13017. (while t
  13018. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13019. (org-back-to-heading t)
  13020. (move-marker org-entry-property-inherited-from (point))
  13021. (throw 'ex tmp))
  13022. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13023. (setq tmp (or tmp
  13024. (cdr (assoc property org-file-properties))
  13025. (cdr (assoc property org-global-properties))
  13026. (cdr (assoc property org-global-properties-fixed))))
  13027. (if literal-nil tmp (org-not-nil tmp))))
  13028. (defvar org-property-changed-functions nil
  13029. "Hook called when the value of a property has changed.
  13030. Each hook function should accept two arguments, the name of the property
  13031. and the new value.")
  13032. (defun org-entry-put (pom property value)
  13033. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13034. (org-with-point-at pom
  13035. (org-back-to-heading t)
  13036. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13037. range)
  13038. (cond
  13039. ((equal property "TODO")
  13040. (when (and (stringp value) (string-match "\\S-" value)
  13041. (not (member value org-todo-keywords-1)))
  13042. (error "\"%s\" is not a valid TODO state" value))
  13043. (if (or (not value)
  13044. (not (string-match "\\S-" value)))
  13045. (setq value 'none))
  13046. (org-todo value)
  13047. (org-set-tags nil 'align))
  13048. ((equal property "PRIORITY")
  13049. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13050. (string-to-char value) ?\ ))
  13051. (org-set-tags nil 'align))
  13052. ((equal property "SCHEDULED")
  13053. (if (re-search-forward org-scheduled-time-regexp end t)
  13054. (cond
  13055. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13056. ((eq value 'later) (org-timestamp-change 1 'day))
  13057. (t (call-interactively 'org-schedule)))
  13058. (call-interactively 'org-schedule)))
  13059. ((equal property "DEADLINE")
  13060. (if (re-search-forward org-deadline-time-regexp end t)
  13061. (cond
  13062. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13063. ((eq value 'later) (org-timestamp-change 1 'day))
  13064. (t (call-interactively 'org-deadline)))
  13065. (call-interactively 'org-deadline)))
  13066. ((member property org-special-properties)
  13067. (error "The %s property can not yet be set with `org-entry-put'"
  13068. property))
  13069. (t ; a non-special property
  13070. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13071. (setq range (org-get-property-block beg end 'force))
  13072. (goto-char (car range))
  13073. (if (re-search-forward
  13074. (org-re-property property) (cdr range) t)
  13075. (progn
  13076. (delete-region (match-beginning 0) (match-end 0))
  13077. (goto-char (match-beginning 0)))
  13078. (goto-char (cdr range))
  13079. (insert "\n")
  13080. (backward-char 1)
  13081. (org-indent-line-function))
  13082. (insert ":" property ":")
  13083. (and value (insert " " value))
  13084. (org-indent-line-function)))))
  13085. (run-hook-with-args 'org-property-changed-functions property value)))
  13086. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13087. "Get all property keys in the current buffer.
  13088. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13089. like tags and TODO state.
  13090. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13091. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13092. and others.
  13093. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13094. formats in the current buffer."
  13095. (let (rtn range cfmt s p)
  13096. (save-excursion
  13097. (save-restriction
  13098. (widen)
  13099. (goto-char (point-min))
  13100. (while (re-search-forward org-property-start-re nil t)
  13101. (setq range (org-get-property-block))
  13102. (goto-char (car range))
  13103. (while (re-search-forward
  13104. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13105. (cdr range) t)
  13106. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13107. (outline-next-heading))))
  13108. (when include-specials
  13109. (setq rtn (append org-special-properties rtn)))
  13110. (when include-defaults
  13111. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13112. (add-to-list 'rtn org-effort-property))
  13113. (when include-columns
  13114. (save-excursion
  13115. (save-restriction
  13116. (widen)
  13117. (goto-char (point-min))
  13118. (while (re-search-forward
  13119. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13120. nil t)
  13121. (setq cfmt (match-string 2) s 0)
  13122. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13123. cfmt s)
  13124. (setq s (match-end 0)
  13125. p (match-string 1 cfmt))
  13126. (unless (or (equal p "ITEM")
  13127. (member p org-special-properties))
  13128. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13129. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13130. (defun org-property-values (key)
  13131. "Return a list of all values of property KEY in the current buffer."
  13132. (save-excursion
  13133. (save-restriction
  13134. (widen)
  13135. (goto-char (point-min))
  13136. (let ((re (org-re-property key))
  13137. values)
  13138. (while (re-search-forward re nil t)
  13139. (add-to-list 'values (org-trim (match-string 1))))
  13140. (delete "" values)))))
  13141. (defun org-insert-property-drawer ()
  13142. "Insert a property drawer into the current entry."
  13143. (org-back-to-heading t)
  13144. (looking-at org-outline-regexp)
  13145. (let ((indent (if org-adapt-indentation
  13146. (- (match-end 0) (match-beginning 0))
  13147. 0))
  13148. (beg (point))
  13149. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13150. end hiddenp)
  13151. (outline-next-heading)
  13152. (setq end (point))
  13153. (goto-char beg)
  13154. (while (re-search-forward re end t))
  13155. (setq hiddenp (outline-invisible-p))
  13156. (end-of-line 1)
  13157. (and (equal (char-after) ?\n) (forward-char 1))
  13158. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13159. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13160. ;; just skip this line
  13161. (beginning-of-line 2)
  13162. ;; Drawer start, find the end
  13163. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13164. (beginning-of-line 1)))
  13165. (org-skip-over-state-notes)
  13166. (skip-chars-backward " \t\n\r")
  13167. (if (eq (char-before) ?*) (forward-char 1))
  13168. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13169. (beginning-of-line 0)
  13170. (org-indent-to-column indent)
  13171. (beginning-of-line 2)
  13172. (org-indent-to-column indent)
  13173. (beginning-of-line 0)
  13174. (if hiddenp
  13175. (save-excursion
  13176. (org-back-to-heading t)
  13177. (hide-entry))
  13178. (org-flag-drawer t))))
  13179. (defun org-insert-drawer (&optional arg drawer)
  13180. "Insert a drawer at point.
  13181. Optional argument DRAWER, when non-nil, is a string representing
  13182. drawer's name. Otherwise, the user is prompted for a name.
  13183. If a region is active, insert the drawer around that region
  13184. instead.
  13185. Point is left between drawer's boundaries."
  13186. (interactive "P")
  13187. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13188. "LOGBOOK"))
  13189. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13190. ;; internally by Org. They are meant to be inserted
  13191. ;; automatically.
  13192. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13193. ;; Remove system drawers from list. Note: For some reason,
  13194. ;; `org-completing-read' ignores the predicate while
  13195. ;; `completing-read' handles it fine.
  13196. (drawer (if arg "PROPERTIES"
  13197. (or drawer
  13198. (completing-read
  13199. "Drawer: " org-drawers
  13200. (lambda (d) (not (member d system-drawers))))))))
  13201. (cond
  13202. ;; With C-u, fall back on `org-insert-property-drawer'
  13203. (arg (org-insert-property-drawer))
  13204. ;; With an active region, insert a drawer at point.
  13205. ((not (org-region-active-p))
  13206. (progn
  13207. (unless (bolp) (insert "\n"))
  13208. (insert (format ":%s:\n\n:END:\n" drawer))
  13209. (forward-line -2)))
  13210. ;; Otherwise, insert the drawer at point
  13211. (t
  13212. (let ((rbeg (region-beginning))
  13213. (rend (copy-marker (region-end))))
  13214. (unwind-protect
  13215. (progn
  13216. (goto-char rbeg)
  13217. (beginning-of-line)
  13218. (when (save-excursion
  13219. (re-search-forward org-outline-regexp-bol rend t))
  13220. (error "Drawers cannot contain headlines"))
  13221. ;; Position point at the beginning of the first
  13222. ;; non-blank line in region. Insert drawer's opening
  13223. ;; there, then indent it.
  13224. (org-skip-whitespace)
  13225. (beginning-of-line)
  13226. (insert ":" drawer ":\n")
  13227. (forward-line -1)
  13228. (indent-for-tab-command)
  13229. ;; Move point to the beginning of the first blank line
  13230. ;; after the last non-blank line in region. Insert
  13231. ;; drawer's closing, then indent it.
  13232. (goto-char rend)
  13233. (skip-chars-backward " \r\t\n")
  13234. (insert "\n:END:")
  13235. (indent-for-tab-command)
  13236. (unless (eolp) (insert "\n")))
  13237. ;; Clear marker, whatever the outcome of insertion is.
  13238. (set-marker rend nil)))))))
  13239. (defvar org-property-set-functions-alist nil
  13240. "Property set function alist.
  13241. Each entry should have the following format:
  13242. (PROPERTY . READ-FUNCTION)
  13243. The read function will be called with the same argument as
  13244. `org-completing-read'.")
  13245. (defun org-set-property-function (property)
  13246. "Get the function that should be used to set PROPERTY.
  13247. This is computed according to `org-property-set-functions-alist'."
  13248. (or (cdr (assoc property org-property-set-functions-alist))
  13249. 'org-completing-read))
  13250. (defun org-read-property-value (property)
  13251. "Read PROPERTY value from user."
  13252. (let* ((completion-ignore-case t)
  13253. (allowed (org-property-get-allowed-values nil property 'table))
  13254. (cur (org-entry-get nil property))
  13255. (prompt (concat property " value"
  13256. (if (and cur (string-match "\\S-" cur))
  13257. (concat " [" cur "]") "") ": "))
  13258. (set-function (org-set-property-function property))
  13259. (val (if allowed
  13260. (funcall set-function prompt allowed nil
  13261. (not (get-text-property 0 'org-unrestricted
  13262. (caar allowed))))
  13263. (let (org-completion-use-ido org-completion-use-iswitchb)
  13264. (funcall set-function prompt
  13265. (mapcar 'list (org-property-values property))
  13266. nil nil "" nil cur)))))
  13267. (if (equal val "")
  13268. cur
  13269. val)))
  13270. (defvar org-last-set-property nil)
  13271. (defun org-read-property-name ()
  13272. "Read a property name."
  13273. (let* ((completion-ignore-case t)
  13274. (keys (org-buffer-property-keys nil t t))
  13275. (default-prop (or (save-excursion
  13276. (save-match-data
  13277. (beginning-of-line)
  13278. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13279. (null (string= (match-string 1) "END"))
  13280. (match-string 1))))
  13281. org-last-set-property))
  13282. (property (org-icompleting-read
  13283. (concat "Property"
  13284. (if default-prop (concat " [" default-prop "]") "")
  13285. ": ")
  13286. (mapcar 'list keys)
  13287. nil nil nil nil
  13288. default-prop
  13289. )))
  13290. (if (member property keys)
  13291. property
  13292. (or (cdr (assoc (downcase property)
  13293. (mapcar (lambda (x) (cons (downcase x) x))
  13294. keys)))
  13295. property))))
  13296. (defun org-set-property (property value)
  13297. "In the current entry, set PROPERTY to VALUE.
  13298. When called interactively, this will prompt for a property name, offering
  13299. completion on existing and default properties. And then it will prompt
  13300. for a value, offering completion either on allowed values (via an inherited
  13301. xxx_ALL property) or on existing values in other instances of this property
  13302. in the current file."
  13303. (interactive (list nil nil))
  13304. (let* ((property (or property (org-read-property-name)))
  13305. (value (or value (org-read-property-value property)))
  13306. (fn (assoc property org-properties-postprocess-alist)))
  13307. (setq org-last-set-property property)
  13308. ;; Possibly postprocess the inserted value:
  13309. (when fn (setq value (funcall (cadr fn) value)))
  13310. (unless (equal (org-entry-get nil property) value)
  13311. (org-entry-put nil property value))))
  13312. (defun org-delete-property (property)
  13313. "In the current entry, delete PROPERTY."
  13314. (interactive
  13315. (let* ((completion-ignore-case t)
  13316. (prop (org-icompleting-read "Property: "
  13317. (org-entry-properties nil 'standard))))
  13318. (list prop)))
  13319. (message "Property %s %s" property
  13320. (if (org-entry-delete nil property)
  13321. "deleted"
  13322. "was not present in the entry")))
  13323. (defun org-delete-property-globally (property)
  13324. "Remove PROPERTY globally, from all entries."
  13325. (interactive
  13326. (let* ((completion-ignore-case t)
  13327. (prop (org-icompleting-read
  13328. "Globally remove property: "
  13329. (mapcar 'list (org-buffer-property-keys)))))
  13330. (list prop)))
  13331. (save-excursion
  13332. (save-restriction
  13333. (widen)
  13334. (goto-char (point-min))
  13335. (let ((cnt 0))
  13336. (while (re-search-forward
  13337. (org-re-property property)
  13338. nil t)
  13339. (setq cnt (1+ cnt))
  13340. (replace-match ""))
  13341. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13342. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13343. (defun org-compute-property-at-point ()
  13344. "Compute the property at point.
  13345. This looks for an enclosing column format, extracts the operator and
  13346. then applies it to the property in the column format's scope."
  13347. (interactive)
  13348. (unless (org-at-property-p)
  13349. (error "Not at a property"))
  13350. (let ((prop (org-match-string-no-properties 2)))
  13351. (org-columns-get-format-and-top-level)
  13352. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13353. (error "No operator defined for property %s" prop))
  13354. (org-columns-compute prop)))
  13355. (defvar org-property-allowed-value-functions nil
  13356. "Hook for functions supplying allowed values for a specific property.
  13357. The functions must take a single argument, the name of the property, and
  13358. return a flat list of allowed values. If \":ETC\" is one of
  13359. the values, this means that these values are intended as defaults for
  13360. completion, but that other values should be allowed too.
  13361. The functions must return nil if they are not responsible for this
  13362. property.")
  13363. (defun org-property-get-allowed-values (pom property &optional table)
  13364. "Get allowed values for the property PROPERTY.
  13365. When TABLE is non-nil, return an alist that can directly be used for
  13366. completion."
  13367. (let (vals)
  13368. (cond
  13369. ((equal property "TODO")
  13370. (setq vals (org-with-point-at pom
  13371. (append org-todo-keywords-1 '("")))))
  13372. ((equal property "PRIORITY")
  13373. (let ((n org-lowest-priority))
  13374. (while (>= n org-highest-priority)
  13375. (push (char-to-string n) vals)
  13376. (setq n (1- n)))))
  13377. ((member property org-special-properties))
  13378. ((setq vals (run-hook-with-args-until-success
  13379. 'org-property-allowed-value-functions property)))
  13380. (t
  13381. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13382. (when (and vals (string-match "\\S-" vals))
  13383. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13384. (setq vals (mapcar (lambda (x)
  13385. (cond ((stringp x) x)
  13386. ((numberp x) (number-to-string x))
  13387. ((symbolp x) (symbol-name x))
  13388. (t "???")))
  13389. vals)))))
  13390. (when (member ":ETC" vals)
  13391. (setq vals (remove ":ETC" vals))
  13392. (org-add-props (car vals) '(org-unrestricted t)))
  13393. (if table (mapcar 'list vals) vals)))
  13394. (defun org-property-previous-allowed-value (&optional previous)
  13395. "Switch to the next allowed value for this property."
  13396. (interactive)
  13397. (org-property-next-allowed-value t))
  13398. (defun org-property-next-allowed-value (&optional previous)
  13399. "Switch to the next allowed value for this property."
  13400. (interactive)
  13401. (unless (org-at-property-p)
  13402. (error "Not at a property"))
  13403. (let* ((key (match-string 2))
  13404. (value (match-string 3))
  13405. (allowed (or (org-property-get-allowed-values (point) key)
  13406. (and (member value '("[ ]" "[-]" "[X]"))
  13407. '("[ ]" "[X]"))))
  13408. nval)
  13409. (unless allowed
  13410. (error "Allowed values for this property have not been defined"))
  13411. (if previous (setq allowed (reverse allowed)))
  13412. (if (member value allowed)
  13413. (setq nval (car (cdr (member value allowed)))))
  13414. (setq nval (or nval (car allowed)))
  13415. (if (equal nval value)
  13416. (error "Only one allowed value for this property"))
  13417. (org-at-property-p)
  13418. (replace-match (concat " :" key ": " nval) t t)
  13419. (org-indent-line-function)
  13420. (beginning-of-line 1)
  13421. (skip-chars-forward " \t")
  13422. (run-hook-with-args 'org-property-changed-functions key nval)))
  13423. (defun org-find-olp (path &optional this-buffer)
  13424. "Return a marker pointing to the entry at outline path OLP.
  13425. If anything goes wrong, throw an error.
  13426. You can wrap this call to catch the error like this:
  13427. (condition-case msg
  13428. (org-mobile-locate-entry (match-string 4))
  13429. (error (nth 1 msg)))
  13430. The return value will then be either a string with the error message,
  13431. or a marker if everything is OK.
  13432. If THIS-BUFFER is set, the outline path does not contain a file,
  13433. only headings."
  13434. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13435. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13436. (level 1)
  13437. (lmin 1)
  13438. (lmax 1)
  13439. limit re end found pos heading cnt flevel)
  13440. (unless buffer (error "File not found :%s" file))
  13441. (with-current-buffer buffer
  13442. (save-excursion
  13443. (save-restriction
  13444. (widen)
  13445. (setq limit (point-max))
  13446. (goto-char (point-min))
  13447. (while (setq heading (pop path))
  13448. (setq re (format org-complex-heading-regexp-format
  13449. (regexp-quote heading)))
  13450. (setq cnt 0 pos (point))
  13451. (while (re-search-forward re end t)
  13452. (setq level (- (match-end 1) (match-beginning 1)))
  13453. (if (and (>= level lmin) (<= level lmax))
  13454. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13455. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13456. lmax heading))
  13457. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13458. lmax heading))
  13459. (goto-char found)
  13460. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13461. (setq end (save-excursion (org-end-of-subtree t t))))
  13462. (when (org-at-heading-p)
  13463. (move-marker (make-marker) (point))))))))
  13464. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13465. "Find node HEADING in BUFFER.
  13466. Return a marker to the heading if it was found, or nil if not.
  13467. If POS-ONLY is set, return just the position instead of a marker.
  13468. The heading text must match exact, but it may have a TODO keyword,
  13469. a priority cookie and tags in the standard locations."
  13470. (with-current-buffer (or buffer (current-buffer))
  13471. (save-excursion
  13472. (save-restriction
  13473. (widen)
  13474. (goto-char (point-min))
  13475. (let (case-fold-search)
  13476. (if (re-search-forward
  13477. (format org-complex-heading-regexp-format
  13478. (regexp-quote heading)) nil t)
  13479. (if pos-only
  13480. (match-beginning 0)
  13481. (move-marker (make-marker) (match-beginning 0)))))))))
  13482. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13483. "Find Org node headline HEADING in all .org files in directory DIR.
  13484. When the target headline is found, return a marker to this location."
  13485. (let ((files (directory-files (or dir default-directory)
  13486. nil "\\`[^.#].*\\.org\\'"))
  13487. file visiting m buffer)
  13488. (catch 'found
  13489. (while (setq file (pop files))
  13490. (message "trying %s" file)
  13491. (setq visiting (org-find-base-buffer-visiting file))
  13492. (setq buffer (or visiting (find-file-noselect file)))
  13493. (setq m (org-find-exact-headline-in-buffer
  13494. heading buffer))
  13495. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13496. (and m (throw 'found m))))))
  13497. (defun org-find-entry-with-id (ident)
  13498. "Locate the entry that contains the ID property with exact value IDENT.
  13499. IDENT can be a string, a symbol or a number, this function will search for
  13500. the string representation of it.
  13501. Return the position where this entry starts, or nil if there is no such entry."
  13502. (interactive "sID: ")
  13503. (let ((id (cond
  13504. ((stringp ident) ident)
  13505. ((symbol-name ident) (symbol-name ident))
  13506. ((numberp ident) (number-to-string ident))
  13507. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13508. (case-fold-search nil))
  13509. (save-excursion
  13510. (save-restriction
  13511. (widen)
  13512. (goto-char (point-min))
  13513. (when (re-search-forward
  13514. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13515. nil t)
  13516. (org-back-to-heading t)
  13517. (point))))))
  13518. ;;;; Timestamps
  13519. (defvar org-last-changed-timestamp nil)
  13520. (defvar org-last-inserted-timestamp nil
  13521. "The last time stamp inserted with `org-insert-time-stamp'.")
  13522. (defvar org-time-was-given) ; dynamically scoped parameter
  13523. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13524. (defvar org-ts-what) ; dynamically scoped parameter
  13525. (defun org-time-stamp (arg &optional inactive)
  13526. "Prompt for a date/time and insert a time stamp.
  13527. If the user specifies a time like HH:MM, or if this command is called
  13528. with a prefix argument, the time stamp will contain date and time.
  13529. Otherwise, only the date will be included. All parts of a date not
  13530. specified by the user will be filled in from the current date/time.
  13531. So if you press just return without typing anything, the time stamp
  13532. will represent the current date/time. If there is already a timestamp
  13533. at the cursor, it will be modified."
  13534. (interactive "P")
  13535. (let* ((ts nil)
  13536. (default-time
  13537. ;; Default time is either today, or, when entering a range,
  13538. ;; the range start.
  13539. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13540. (save-excursion
  13541. (re-search-backward
  13542. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13543. (- (point) 20) t)))
  13544. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13545. (current-time)))
  13546. (default-input (and ts (org-get-compact-tod ts)))
  13547. (repeater (save-excursion
  13548. (save-match-data
  13549. (beginning-of-line)
  13550. (when (re-search-forward
  13551. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13552. (save-excursion (progn (end-of-line) (point))) t)
  13553. (match-string 0)))))
  13554. org-time-was-given org-end-time-was-given time)
  13555. (cond
  13556. ((and (org-at-timestamp-p t)
  13557. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13558. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13559. (insert "--")
  13560. (setq time (let ((this-command this-command))
  13561. (org-read-date arg 'totime nil nil
  13562. default-time default-input inactive)))
  13563. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13564. ((org-at-timestamp-p t)
  13565. (setq time (let ((this-command this-command))
  13566. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13567. (when (org-at-timestamp-p t) ; just to get the match data
  13568. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13569. (replace-match "")
  13570. (setq org-last-changed-timestamp
  13571. (org-insert-time-stamp
  13572. time (or org-time-was-given arg)
  13573. inactive nil nil (list org-end-time-was-given)))
  13574. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13575. (setq org-last-changed-timestamp
  13576. (concat (substring org-last-inserted-timestamp 0 -1)
  13577. " " repeater ">"))))
  13578. (message "Timestamp updated"))
  13579. (t
  13580. (setq time (let ((this-command this-command))
  13581. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13582. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13583. nil nil (list org-end-time-was-given))))))
  13584. ;; FIXME: can we use this for something else, like computing time differences?
  13585. (defun org-get-compact-tod (s)
  13586. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13587. (let* ((t1 (match-string 1 s))
  13588. (h1 (string-to-number (match-string 2 s)))
  13589. (m1 (string-to-number (match-string 3 s)))
  13590. (t2 (and (match-end 4) (match-string 5 s)))
  13591. (h2 (and t2 (string-to-number (match-string 6 s))))
  13592. (m2 (and t2 (string-to-number (match-string 7 s))))
  13593. dh dm)
  13594. (if (not t2)
  13595. t1
  13596. (setq dh (- h2 h1) dm (- m2 m1))
  13597. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13598. (concat t1 "+" (number-to-string dh)
  13599. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13600. (defun org-time-stamp-inactive (&optional arg)
  13601. "Insert an inactive time stamp.
  13602. An inactive time stamp is enclosed in square brackets instead of angle
  13603. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13604. does not link to the calendar and cannot be changed with the S-cursor keys.
  13605. So these are more for recording a certain time/date."
  13606. (interactive "P")
  13607. (org-time-stamp arg 'inactive))
  13608. (defvar org-date-ovl (make-overlay 1 1))
  13609. (overlay-put org-date-ovl 'face 'org-warning)
  13610. (org-detach-overlay org-date-ovl)
  13611. (defvar org-ans1) ; dynamically scoped parameter
  13612. (defvar org-ans2) ; dynamically scoped parameter
  13613. (defvar org-plain-time-of-day-regexp) ; defined below
  13614. (defvar org-overriding-default-time nil) ; dynamically scoped
  13615. (defvar org-read-date-overlay nil)
  13616. (defvar org-dcst nil) ; dynamically scoped
  13617. (defvar org-read-date-history nil)
  13618. (defvar org-read-date-final-answer nil)
  13619. (defvar org-read-date-analyze-futurep nil)
  13620. (defvar org-read-date-analyze-forced-year nil)
  13621. (defun org-read-date (&optional with-time to-time from-string prompt
  13622. default-time default-input inactive)
  13623. "Read a date, possibly a time, and make things smooth for the user.
  13624. The prompt will suggest to enter an ISO date, but you can also enter anything
  13625. which will at least partially be understood by `parse-time-string'.
  13626. Unrecognized parts of the date will default to the current day, month, year,
  13627. hour and minute. If this command is called to replace a timestamp at point,
  13628. or to enter the second timestamp of a range, the default time is taken
  13629. from the existing stamp. Furthermore, the command prefers the future,
  13630. so if you are giving a date where the year is not given, and the day-month
  13631. combination is already past in the current year, it will assume you
  13632. mean next year. For details, see the manual. A few examples:
  13633. 3-2-5 --> 2003-02-05
  13634. feb 15 --> currentyear-02-15
  13635. 2/15 --> currentyear-02-15
  13636. sep 12 9 --> 2009-09-12
  13637. 12:45 --> today 12:45
  13638. 22 sept 0:34 --> currentyear-09-22 0:34
  13639. 12 --> currentyear-currentmonth-12
  13640. Fri --> nearest Friday (today or later)
  13641. etc.
  13642. Furthermore you can specify a relative date by giving, as the *first* thing
  13643. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13644. change in days weeks, months, years.
  13645. With a single plus or minus, the date is relative to today. With a double
  13646. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13647. +4d --> four days from today
  13648. +4 --> same as above
  13649. +2w --> two weeks from today
  13650. ++5 --> five days from default date
  13651. The function understands only English month and weekday abbreviations,
  13652. but this can be configured with the variables `parse-time-months' and
  13653. `parse-time-weekdays'.
  13654. While prompting, a calendar is popped up - you can also select the
  13655. date with the mouse (button 1). The calendar shows a period of three
  13656. months. To scroll it to other months, use the keys `>' and `<'.
  13657. If you don't like the calendar, turn it off with
  13658. \(setq org-read-date-popup-calendar nil)
  13659. With optional argument TO-TIME, the date will immediately be converted
  13660. to an internal time.
  13661. With an optional argument WITH-TIME, the prompt will suggest to also
  13662. insert a time. Note that when WITH-TIME is not set, you can still
  13663. enter a time, and this function will inform the calling routine about
  13664. this change. The calling routine may then choose to change the format
  13665. used to insert the time stamp into the buffer to include the time.
  13666. With optional argument FROM-STRING, read from this string instead from
  13667. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13668. the time/date that is used for everything that is not specified by the
  13669. user."
  13670. (require 'parse-time)
  13671. (let* ((org-time-stamp-rounding-minutes
  13672. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13673. (org-dcst org-display-custom-times)
  13674. (ct (org-current-time))
  13675. (def (or org-overriding-default-time default-time ct))
  13676. (defdecode (decode-time def))
  13677. (dummy (progn
  13678. (when (< (nth 2 defdecode) org-extend-today-until)
  13679. (setcar (nthcdr 2 defdecode) -1)
  13680. (setcar (nthcdr 1 defdecode) 59)
  13681. (setq def (apply 'encode-time defdecode)
  13682. defdecode (decode-time def)))))
  13683. (calendar-frame-setup nil)
  13684. (calendar-setup nil)
  13685. (calendar-move-hook nil)
  13686. (calendar-view-diary-initially-flag nil)
  13687. (calendar-view-holidays-initially-flag nil)
  13688. (timestr (format-time-string
  13689. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13690. (prompt (concat (if prompt (concat prompt " ") "")
  13691. (format "Date+time [%s]: " timestr)))
  13692. ans (org-ans0 "") org-ans1 org-ans2 final)
  13693. (cond
  13694. (from-string (setq ans from-string))
  13695. (org-read-date-popup-calendar
  13696. (save-excursion
  13697. (save-window-excursion
  13698. (calendar)
  13699. (unwind-protect
  13700. (progn
  13701. (calendar-forward-day (- (time-to-days def)
  13702. (calendar-absolute-from-gregorian
  13703. (calendar-current-date))))
  13704. (org-eval-in-calendar nil t)
  13705. (let* ((old-map (current-local-map))
  13706. (map (copy-keymap calendar-mode-map))
  13707. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13708. (org-defkey map (kbd "RET") 'org-calendar-select)
  13709. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13710. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13711. (org-defkey minibuffer-local-map [(meta shift left)]
  13712. (lambda () (interactive)
  13713. (org-eval-in-calendar '(calendar-backward-month 1))))
  13714. (org-defkey minibuffer-local-map [(meta shift right)]
  13715. (lambda () (interactive)
  13716. (org-eval-in-calendar '(calendar-forward-month 1))))
  13717. (org-defkey minibuffer-local-map [(meta shift up)]
  13718. (lambda () (interactive)
  13719. (org-eval-in-calendar '(calendar-backward-year 1))))
  13720. (org-defkey minibuffer-local-map [(meta shift down)]
  13721. (lambda () (interactive)
  13722. (org-eval-in-calendar '(calendar-forward-year 1))))
  13723. (org-defkey minibuffer-local-map [?\e (shift left)]
  13724. (lambda () (interactive)
  13725. (org-eval-in-calendar '(calendar-backward-month 1))))
  13726. (org-defkey minibuffer-local-map [?\e (shift right)]
  13727. (lambda () (interactive)
  13728. (org-eval-in-calendar '(calendar-forward-month 1))))
  13729. (org-defkey minibuffer-local-map [?\e (shift up)]
  13730. (lambda () (interactive)
  13731. (org-eval-in-calendar '(calendar-backward-year 1))))
  13732. (org-defkey minibuffer-local-map [?\e (shift down)]
  13733. (lambda () (interactive)
  13734. (org-eval-in-calendar '(calendar-forward-year 1))))
  13735. (org-defkey minibuffer-local-map [(shift up)]
  13736. (lambda () (interactive)
  13737. (org-eval-in-calendar '(calendar-backward-week 1))))
  13738. (org-defkey minibuffer-local-map [(shift down)]
  13739. (lambda () (interactive)
  13740. (org-eval-in-calendar '(calendar-forward-week 1))))
  13741. (org-defkey minibuffer-local-map [(shift left)]
  13742. (lambda () (interactive)
  13743. (org-eval-in-calendar '(calendar-backward-day 1))))
  13744. (org-defkey minibuffer-local-map [(shift right)]
  13745. (lambda () (interactive)
  13746. (org-eval-in-calendar '(calendar-forward-day 1))))
  13747. (org-defkey minibuffer-local-map ">"
  13748. (lambda () (interactive)
  13749. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13750. (org-defkey minibuffer-local-map "<"
  13751. (lambda () (interactive)
  13752. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13753. (org-defkey minibuffer-local-map "\C-v"
  13754. (lambda () (interactive)
  13755. (org-eval-in-calendar
  13756. '(calendar-scroll-left-three-months 1))))
  13757. (org-defkey minibuffer-local-map "\M-v"
  13758. (lambda () (interactive)
  13759. (org-eval-in-calendar
  13760. '(calendar-scroll-right-three-months 1))))
  13761. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13762. (unwind-protect
  13763. (progn
  13764. (use-local-map map)
  13765. (setq org-read-date-inactive inactive)
  13766. (add-hook 'post-command-hook 'org-read-date-display)
  13767. (setq org-ans0 (read-string prompt default-input
  13768. 'org-read-date-history nil))
  13769. ;; org-ans0: from prompt
  13770. ;; org-ans1: from mouse click
  13771. ;; org-ans2: from calendar motion
  13772. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13773. (remove-hook 'post-command-hook 'org-read-date-display)
  13774. (use-local-map old-map)
  13775. (when org-read-date-overlay
  13776. (delete-overlay org-read-date-overlay)
  13777. (setq org-read-date-overlay nil)))))
  13778. (bury-buffer "*Calendar*")))))
  13779. (t ; Naked prompt only
  13780. (unwind-protect
  13781. (setq ans (read-string prompt default-input
  13782. 'org-read-date-history timestr))
  13783. (when org-read-date-overlay
  13784. (delete-overlay org-read-date-overlay)
  13785. (setq org-read-date-overlay nil)))))
  13786. (setq final (org-read-date-analyze ans def defdecode))
  13787. (when org-read-date-analyze-forced-year
  13788. (message "Year was forced into %s"
  13789. (if org-read-date-force-compatible-dates
  13790. "compatible range (1970-2037)"
  13791. "range representable on this machine"))
  13792. (ding))
  13793. ;; One round trip to get rid of 34th of August and stuff like that....
  13794. (setq final (decode-time (apply 'encode-time final)))
  13795. (setq org-read-date-final-answer ans)
  13796. (if to-time
  13797. (apply 'encode-time final)
  13798. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13799. (format "%04d-%02d-%02d %02d:%02d"
  13800. (nth 5 final) (nth 4 final) (nth 3 final)
  13801. (nth 2 final) (nth 1 final))
  13802. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13803. (defvar def)
  13804. (defvar defdecode)
  13805. (defvar with-time)
  13806. (defvar org-read-date-inactive)
  13807. (defun org-read-date-display ()
  13808. "Display the current date prompt interpretation in the minibuffer."
  13809. (when org-read-date-display-live
  13810. (when org-read-date-overlay
  13811. (delete-overlay org-read-date-overlay))
  13812. (let ((p (point)))
  13813. (end-of-line 1)
  13814. (while (not (equal (buffer-substring
  13815. (max (point-min) (- (point) 4)) (point))
  13816. " "))
  13817. (insert " "))
  13818. (goto-char p))
  13819. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13820. " " (or org-ans1 org-ans2)))
  13821. (org-end-time-was-given nil)
  13822. (f (org-read-date-analyze ans def defdecode))
  13823. (fmts (if org-dcst
  13824. org-time-stamp-custom-formats
  13825. org-time-stamp-formats))
  13826. (fmt (if (or with-time
  13827. (and (boundp 'org-time-was-given) org-time-was-given))
  13828. (cdr fmts)
  13829. (car fmts)))
  13830. (txt (format-time-string fmt (apply 'encode-time f)))
  13831. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  13832. (txt (concat "=> " txt)))
  13833. (when (and org-end-time-was-given
  13834. (string-match org-plain-time-of-day-regexp txt))
  13835. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13836. org-end-time-was-given
  13837. (substring txt (match-end 0)))))
  13838. (when org-read-date-analyze-futurep
  13839. (setq txt (concat txt " (=>F)")))
  13840. (setq org-read-date-overlay
  13841. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13842. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13843. (defun org-read-date-analyze (ans def defdecode)
  13844. "Analyze the combined answer of the date prompt."
  13845. ;; FIXME: cleanup and comment
  13846. (let ((nowdecode (decode-time (current-time)))
  13847. delta deltan deltaw deltadef year month day
  13848. hour minute second wday pm h2 m2 tl wday1
  13849. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13850. (setq org-read-date-analyze-futurep nil
  13851. org-read-date-analyze-forced-year nil)
  13852. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13853. (setq ans "+0"))
  13854. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13855. (setq ans (replace-match "" t t ans)
  13856. deltan (car delta)
  13857. deltaw (nth 1 delta)
  13858. deltadef (nth 2 delta)))
  13859. ;; Check if there is an iso week date in there
  13860. ;; If yes, store the info and postpone interpreting it until the rest
  13861. ;; of the parsing is done
  13862. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13863. (setq iso-year (if (match-end 1)
  13864. (org-small-year-to-year
  13865. (string-to-number (match-string 1 ans))))
  13866. iso-weekday (if (match-end 3)
  13867. (string-to-number (match-string 3 ans)))
  13868. iso-week (string-to-number (match-string 2 ans)))
  13869. (setq ans (replace-match "" t t ans)))
  13870. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13871. (when (string-match
  13872. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13873. (setq year (if (match-end 2)
  13874. (string-to-number (match-string 2 ans))
  13875. (progn (setq kill-year t)
  13876. (string-to-number (format-time-string "%Y"))))
  13877. month (string-to-number (match-string 3 ans))
  13878. day (string-to-number (match-string 4 ans)))
  13879. (if (< year 100) (setq year (+ 2000 year)))
  13880. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13881. t nil ans)))
  13882. ;; Help matching dotted european dates
  13883. (when (string-match
  13884. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13885. (setq year (if (match-end 3)
  13886. (string-to-number (match-string 3 ans))
  13887. (progn (setq kill-year t)
  13888. (string-to-number (format-time-string "%Y"))))
  13889. day (string-to-number (match-string 1 ans))
  13890. month (string-to-number (match-string 2 ans))
  13891. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13892. t nil ans)))
  13893. ;; Help matching american dates, like 5/30 or 5/30/7
  13894. (when (string-match
  13895. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13896. (setq year (if (match-end 4)
  13897. (string-to-number (match-string 4 ans))
  13898. (progn (setq kill-year t)
  13899. (string-to-number (format-time-string "%Y"))))
  13900. month (string-to-number (match-string 1 ans))
  13901. day (string-to-number (match-string 2 ans)))
  13902. (if (< year 100) (setq year (+ 2000 year)))
  13903. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13904. t nil ans)))
  13905. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13906. ;; If there is a time with am/pm, and *no* time without it, we convert
  13907. ;; so that matching will be successful.
  13908. (loop for i from 1 to 2 do ; twice, for end time as well
  13909. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13910. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13911. (setq hour (string-to-number (match-string 1 ans))
  13912. minute (if (match-end 3)
  13913. (string-to-number (match-string 3 ans))
  13914. 0)
  13915. pm (equal ?p
  13916. (string-to-char (downcase (match-string 4 ans)))))
  13917. (if (and (= hour 12) (not pm))
  13918. (setq hour 0)
  13919. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13920. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13921. t t ans))))
  13922. ;; Check if a time range is given as a duration
  13923. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13924. (setq hour (string-to-number (match-string 1 ans))
  13925. h2 (+ hour (string-to-number (match-string 3 ans)))
  13926. minute (string-to-number (match-string 2 ans))
  13927. m2 (+ minute (if (match-end 5) (string-to-number
  13928. (match-string 5 ans))0)))
  13929. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13930. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13931. t t ans)))
  13932. ;; Check if there is a time range
  13933. (when (boundp 'org-end-time-was-given)
  13934. (setq org-time-was-given nil)
  13935. (when (and (string-match org-plain-time-of-day-regexp ans)
  13936. (match-end 8))
  13937. (setq org-end-time-was-given (match-string 8 ans))
  13938. (setq ans (concat (substring ans 0 (match-beginning 7))
  13939. (substring ans (match-end 7))))))
  13940. (setq tl (parse-time-string ans)
  13941. day (or (nth 3 tl) (nth 3 defdecode))
  13942. month (or (nth 4 tl)
  13943. (if (and org-read-date-prefer-future
  13944. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13945. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13946. (nth 4 defdecode)))
  13947. year (or (and (not kill-year) (nth 5 tl))
  13948. (if (and org-read-date-prefer-future
  13949. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13950. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13951. (nth 5 defdecode)))
  13952. hour (or (nth 2 tl) (nth 2 defdecode))
  13953. minute (or (nth 1 tl) (nth 1 defdecode))
  13954. second (or (nth 0 tl) 0)
  13955. wday (nth 6 tl))
  13956. (when (and (eq org-read-date-prefer-future 'time)
  13957. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13958. (equal day (nth 3 nowdecode))
  13959. (equal month (nth 4 nowdecode))
  13960. (equal year (nth 5 nowdecode))
  13961. (nth 2 tl)
  13962. (or (< (nth 2 tl) (nth 2 nowdecode))
  13963. (and (= (nth 2 tl) (nth 2 nowdecode))
  13964. (nth 1 tl)
  13965. (< (nth 1 tl) (nth 1 nowdecode)))))
  13966. (setq day (1+ day)
  13967. futurep t))
  13968. ;; Special date definitions below
  13969. (cond
  13970. (iso-week
  13971. ;; There was an iso week
  13972. (require 'cal-iso)
  13973. (setq futurep nil)
  13974. (setq year (or iso-year year)
  13975. day (or iso-weekday wday 1)
  13976. wday nil ; to make sure that the trigger below does not match
  13977. iso-date (calendar-gregorian-from-absolute
  13978. (calendar-absolute-from-iso
  13979. (list iso-week day year))))
  13980. ; FIXME: Should we also push ISO weeks into the future?
  13981. ; (when (and org-read-date-prefer-future
  13982. ; (not iso-year)
  13983. ; (< (calendar-absolute-from-gregorian iso-date)
  13984. ; (time-to-days (current-time))))
  13985. ; (setq year (1+ year)
  13986. ; iso-date (calendar-gregorian-from-absolute
  13987. ; (calendar-absolute-from-iso
  13988. ; (list iso-week day year)))))
  13989. (setq month (car iso-date)
  13990. year (nth 2 iso-date)
  13991. day (nth 1 iso-date)))
  13992. (deltan
  13993. (setq futurep nil)
  13994. (unless deltadef
  13995. (let ((now (decode-time (current-time))))
  13996. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13997. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13998. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13999. ((equal deltaw "m") (setq month (+ month deltan)))
  14000. ((equal deltaw "y") (setq year (+ year deltan)))))
  14001. ((and wday (not (nth 3 tl)))
  14002. (setq futurep nil)
  14003. ;; Weekday was given, but no day, so pick that day in the week
  14004. ;; on or after the derived date.
  14005. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14006. (unless (equal wday wday1)
  14007. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14008. (if (and (boundp 'org-time-was-given)
  14009. (nth 2 tl))
  14010. (setq org-time-was-given t))
  14011. (if (< year 100) (setq year (+ 2000 year)))
  14012. ;; Check of the date is representable
  14013. (if org-read-date-force-compatible-dates
  14014. (progn
  14015. (if (< year 1970)
  14016. (setq year 1970 org-read-date-analyze-forced-year t))
  14017. (if (> year 2037)
  14018. (setq year 2037 org-read-date-analyze-forced-year t)))
  14019. (condition-case nil
  14020. (ignore (encode-time second minute hour day month year))
  14021. (error
  14022. (setq year (nth 5 defdecode))
  14023. (setq org-read-date-analyze-forced-year t))))
  14024. (setq org-read-date-analyze-futurep futurep)
  14025. (list second minute hour day month year)))
  14026. (defvar parse-time-weekdays)
  14027. (defun org-read-date-get-relative (s today default)
  14028. "Check string S for special relative date string.
  14029. TODAY and DEFAULT are internal times, for today and for a default.
  14030. Return shift list (N what def-flag)
  14031. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14032. N is the number of WHATs to shift.
  14033. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14034. the DEFAULT date rather than TODAY."
  14035. (require 'parse-time)
  14036. (when (and
  14037. (string-match
  14038. (concat
  14039. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14040. "\\([0-9]+\\)?"
  14041. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14042. "\\([ \t]\\|$\\)") s)
  14043. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14044. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14045. (string-to-char (substring (match-string 1 s) -1))
  14046. ?+))
  14047. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14048. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14049. (what (if (match-end 3) (match-string 3 s) "d"))
  14050. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14051. (date (if rel default today))
  14052. (wday (nth 6 (decode-time date)))
  14053. delta)
  14054. (if wday1
  14055. (progn
  14056. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14057. (if (= dir ?-) (setq delta (- delta 7)))
  14058. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14059. (list delta "d" rel))
  14060. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14061. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14062. "Turn a user-specified date into the internal representation.
  14063. The internal representation needed by the calendar is (month day year).
  14064. This is a wrapper to handle the brain-dead convention in calendar that
  14065. user function argument order change dependent on argument order."
  14066. (if (boundp 'calendar-date-style)
  14067. (cond
  14068. ((eq calendar-date-style 'american)
  14069. (list arg1 arg2 arg3))
  14070. ((eq calendar-date-style 'european)
  14071. (list arg2 arg1 arg3))
  14072. ((eq calendar-date-style 'iso)
  14073. (list arg2 arg3 arg1)))
  14074. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14075. (if (org-bound-and-true-p european-calendar-style)
  14076. (list arg2 arg1 arg3)
  14077. (list arg1 arg2 arg3)))))
  14078. (defun org-eval-in-calendar (form &optional keepdate)
  14079. "Eval FORM in the calendar window and return to current window.
  14080. Also, store the cursor date in variable org-ans2."
  14081. (let ((sf (selected-frame))
  14082. (sw (selected-window)))
  14083. (select-window (get-buffer-window "*Calendar*" t))
  14084. (eval form)
  14085. (when (and (not keepdate) (calendar-cursor-to-date))
  14086. (let* ((date (calendar-cursor-to-date))
  14087. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14088. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14089. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14090. (select-window sw)
  14091. (org-select-frame-set-input-focus sf)))
  14092. (defun org-calendar-select ()
  14093. "Return to `org-read-date' with the date currently selected.
  14094. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14095. (interactive)
  14096. (when (calendar-cursor-to-date)
  14097. (let* ((date (calendar-cursor-to-date))
  14098. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14099. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14100. (if (active-minibuffer-window) (exit-minibuffer))))
  14101. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14102. "Insert a date stamp for the date given by the internal TIME.
  14103. WITH-HM means use the stamp format that includes the time of the day.
  14104. INACTIVE means use square brackets instead of angular ones, so that the
  14105. stamp will not contribute to the agenda.
  14106. PRE and POST are optional strings to be inserted before and after the
  14107. stamp.
  14108. The command returns the inserted time stamp."
  14109. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14110. stamp)
  14111. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14112. (insert-before-markers (or pre ""))
  14113. (when (listp extra)
  14114. (setq extra (car extra))
  14115. (if (and (stringp extra)
  14116. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14117. (setq extra (format "-%02d:%02d"
  14118. (string-to-number (match-string 1 extra))
  14119. (string-to-number (match-string 2 extra))))
  14120. (setq extra nil)))
  14121. (when extra
  14122. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14123. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14124. (insert-before-markers (or post ""))
  14125. (setq org-last-inserted-timestamp stamp)))
  14126. (defun org-toggle-time-stamp-overlays ()
  14127. "Toggle the use of custom time stamp formats."
  14128. (interactive)
  14129. (setq org-display-custom-times (not org-display-custom-times))
  14130. (unless org-display-custom-times
  14131. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14132. (while (setq p (next-single-property-change p 'display))
  14133. (if (and (get-text-property p 'display)
  14134. (eq (get-text-property p 'face) 'org-date))
  14135. (remove-text-properties
  14136. p (setq p (next-single-property-change p 'display))
  14137. '(display t))))
  14138. (set-buffer-modified-p bmp)))
  14139. (if (featurep 'xemacs)
  14140. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14141. (org-restart-font-lock)
  14142. (setq org-table-may-need-update t)
  14143. (if org-display-custom-times
  14144. (message "Time stamps are overlaid with custom format")
  14145. (message "Time stamp overlays removed")))
  14146. (defun org-display-custom-time (beg end)
  14147. "Overlay modified time stamp format over timestamp between BEG and END."
  14148. (let* ((ts (buffer-substring beg end))
  14149. t1 w1 with-hm tf time str w2 (off 0))
  14150. (save-match-data
  14151. (setq t1 (org-parse-time-string ts t))
  14152. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  14153. (setq off (- (match-end 0) (match-beginning 0)))))
  14154. (setq end (- end off))
  14155. (setq w1 (- end beg)
  14156. with-hm (and (nth 1 t1) (nth 2 t1))
  14157. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14158. time (org-fix-decoded-time t1)
  14159. str (org-add-props
  14160. (format-time-string
  14161. (substring tf 1 -1) (apply 'encode-time time))
  14162. nil 'mouse-face 'highlight)
  14163. w2 (length str))
  14164. (if (not (= w2 w1))
  14165. (add-text-properties (1+ beg) (+ 2 beg)
  14166. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14167. (if (featurep 'xemacs)
  14168. (progn
  14169. (put-text-property beg end 'invisible t)
  14170. (put-text-property beg end 'end-glyph (make-glyph str)))
  14171. (put-text-property beg end 'display str))))
  14172. (defun org-translate-time (string)
  14173. "Translate all timestamps in STRING to custom format.
  14174. But do this only if the variable `org-display-custom-times' is set."
  14175. (when org-display-custom-times
  14176. (save-match-data
  14177. (let* ((start 0)
  14178. (re org-ts-regexp-both)
  14179. t1 with-hm inactive tf time str beg end)
  14180. (while (setq start (string-match re string start))
  14181. (setq beg (match-beginning 0)
  14182. end (match-end 0)
  14183. t1 (save-match-data
  14184. (org-parse-time-string (substring string beg end) t))
  14185. with-hm (and (nth 1 t1) (nth 2 t1))
  14186. inactive (equal (substring string beg (1+ beg)) "[")
  14187. tf (funcall (if with-hm 'cdr 'car)
  14188. org-time-stamp-custom-formats)
  14189. time (org-fix-decoded-time t1)
  14190. str (format-time-string
  14191. (concat
  14192. (if inactive "[" "<") (substring tf 1 -1)
  14193. (if inactive "]" ">"))
  14194. (apply 'encode-time time))
  14195. string (replace-match str t t string)
  14196. start (+ start (length str)))))))
  14197. string)
  14198. (defun org-fix-decoded-time (time)
  14199. "Set 0 instead of nil for the first 6 elements of time.
  14200. Don't touch the rest."
  14201. (let ((n 0))
  14202. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14203. (defun org-days-to-time (timestamp-string)
  14204. "Difference between TIMESTAMP-STRING and now in days."
  14205. (- (time-to-days (org-time-string-to-time timestamp-string))
  14206. (time-to-days (current-time))))
  14207. (defun org-deadline-close (timestamp-string &optional ndays)
  14208. "Is the time in TIMESTAMP-STRING close to the current date?"
  14209. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14210. (and (< (org-days-to-time timestamp-string) ndays)
  14211. (not (org-entry-is-done-p))))
  14212. (defun org-get-wdays (ts)
  14213. "Get the deadline lead time appropriate for timestring TS."
  14214. (cond
  14215. ((<= org-deadline-warning-days 0)
  14216. ;; 0 or negative, enforce this value no matter what
  14217. (- org-deadline-warning-days))
  14218. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14219. ;; lead time is specified.
  14220. (floor (* (string-to-number (match-string 1 ts))
  14221. (cdr (assoc (match-string 2 ts)
  14222. '(("d" . 1) ("w" . 7)
  14223. ("m" . 30.4) ("y" . 365.25)))))))
  14224. ;; go for the default.
  14225. (t org-deadline-warning-days)))
  14226. (defun org-calendar-select-mouse (ev)
  14227. "Return to `org-read-date' with the date currently selected.
  14228. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14229. (interactive "e")
  14230. (mouse-set-point ev)
  14231. (when (calendar-cursor-to-date)
  14232. (let* ((date (calendar-cursor-to-date))
  14233. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14234. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14235. (if (active-minibuffer-window) (exit-minibuffer))))
  14236. (defun org-check-deadlines (ndays)
  14237. "Check if there are any deadlines due or past due.
  14238. A deadline is considered due if it happens within `org-deadline-warning-days'
  14239. days from today's date. If the deadline appears in an entry marked DONE,
  14240. it is not shown. The prefix arg NDAYS can be used to test that many
  14241. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14242. (interactive "P")
  14243. (let* ((org-warn-days
  14244. (cond
  14245. ((equal ndays '(4)) 100000)
  14246. (ndays (prefix-numeric-value ndays))
  14247. (t (abs org-deadline-warning-days))))
  14248. (case-fold-search nil)
  14249. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14250. (callback
  14251. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14252. (message "%d deadlines past-due or due within %d days"
  14253. (org-occur regexp nil callback)
  14254. org-warn-days)))
  14255. (defun org-check-before-date (date)
  14256. "Check if there are deadlines or scheduled entries before DATE."
  14257. (interactive (list (org-read-date)))
  14258. (let ((case-fold-search nil)
  14259. (regexp (concat "\\<\\(" org-deadline-string
  14260. "\\|" org-scheduled-string
  14261. "\\) *<\\([^>]+\\)>"))
  14262. (callback
  14263. (lambda () (time-less-p
  14264. (org-time-string-to-time (match-string 2))
  14265. (org-time-string-to-time date)))))
  14266. (message "%d entries before %s"
  14267. (org-occur regexp nil callback) date)))
  14268. (defun org-check-after-date (date)
  14269. "Check if there are deadlines or scheduled entries after DATE."
  14270. (interactive (list (org-read-date)))
  14271. (let ((case-fold-search nil)
  14272. (regexp (concat "\\<\\(" org-deadline-string
  14273. "\\|" org-scheduled-string
  14274. "\\) *<\\([^>]+\\)>"))
  14275. (callback
  14276. (lambda () (not
  14277. (time-less-p
  14278. (org-time-string-to-time (match-string 2))
  14279. (org-time-string-to-time date))))))
  14280. (message "%d entries after %s"
  14281. (org-occur regexp nil callback) date)))
  14282. (defun org-check-dates-range (start-date end-date)
  14283. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14284. (interactive (list (org-read-date nil nil nil "Range starts")
  14285. (org-read-date nil nil nil "Range end")))
  14286. (let ((case-fold-search nil)
  14287. (regexp (concat "\\<\\(" org-deadline-string
  14288. "\\|" org-scheduled-string
  14289. "\\) *<\\([^>]+\\)>"))
  14290. (callback
  14291. (lambda ()
  14292. (let ((match (match-string 2)))
  14293. (and
  14294. (not (time-less-p
  14295. (org-time-string-to-time match)
  14296. (org-time-string-to-time start-date)))
  14297. (time-less-p
  14298. (org-time-string-to-time match)
  14299. (org-time-string-to-time end-date)))))))
  14300. (message "%d entries between %s and %s"
  14301. (org-occur regexp nil callback) start-date end-date)))
  14302. (defun org-evaluate-time-range (&optional to-buffer)
  14303. "Evaluate a time range by computing the difference between start and end.
  14304. Normally the result is just printed in the echo area, but with prefix arg
  14305. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14306. If the time range is actually in a table, the result is inserted into the
  14307. next column.
  14308. For time difference computation, a year is assumed to be exactly 365
  14309. days in order to avoid rounding problems."
  14310. (interactive "P")
  14311. (or
  14312. (org-clock-update-time-maybe)
  14313. (save-excursion
  14314. (unless (org-at-date-range-p t)
  14315. (goto-char (point-at-bol))
  14316. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14317. (if (not (org-at-date-range-p t))
  14318. (error "Not at a time-stamp range, and none found in current line")))
  14319. (let* ((ts1 (match-string 1))
  14320. (ts2 (match-string 2))
  14321. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14322. (match-end (match-end 0))
  14323. (time1 (org-time-string-to-time ts1))
  14324. (time2 (org-time-string-to-time ts2))
  14325. (t1 (org-float-time time1))
  14326. (t2 (org-float-time time2))
  14327. (diff (abs (- t2 t1)))
  14328. (negative (< (- t2 t1) 0))
  14329. ;; (ys (floor (* 365 24 60 60)))
  14330. (ds (* 24 60 60))
  14331. (hs (* 60 60))
  14332. (fy "%dy %dd %02d:%02d")
  14333. (fy1 "%dy %dd")
  14334. (fd "%dd %02d:%02d")
  14335. (fd1 "%dd")
  14336. (fh "%02d:%02d")
  14337. y d h m align)
  14338. (if havetime
  14339. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14340. y 0
  14341. d (floor (/ diff ds)) diff (mod diff ds)
  14342. h (floor (/ diff hs)) diff (mod diff hs)
  14343. m (floor (/ diff 60)))
  14344. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14345. y 0
  14346. d (floor (+ (/ diff ds) 0.5))
  14347. h 0 m 0))
  14348. (if (not to-buffer)
  14349. (message "%s" (org-make-tdiff-string y d h m))
  14350. (if (org-at-table-p)
  14351. (progn
  14352. (goto-char match-end)
  14353. (setq align t)
  14354. (and (looking-at " *|") (goto-char (match-end 0))))
  14355. (goto-char match-end))
  14356. (if (looking-at
  14357. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14358. (replace-match ""))
  14359. (if negative (insert " -"))
  14360. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14361. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14362. (insert " " (format fh h m))))
  14363. (if align (org-table-align))
  14364. (message "Time difference inserted")))))
  14365. (defun org-make-tdiff-string (y d h m)
  14366. (let ((fmt "")
  14367. (l nil))
  14368. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14369. l (push y l)))
  14370. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14371. l (push d l)))
  14372. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14373. l (push h l)))
  14374. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14375. l (push m l)))
  14376. (apply 'format fmt (nreverse l))))
  14377. (defun org-time-string-to-time (s &optional buffer pos)
  14378. (condition-case errdata
  14379. (apply 'encode-time (org-parse-time-string s))
  14380. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14381. s (if (not (and buffer pos))
  14382. ""
  14383. (format " at %d in buffer `%s'" pos buffer))
  14384. (cdr errdata)))))
  14385. (defun org-time-string-to-seconds (s)
  14386. (org-float-time (org-time-string-to-time s)))
  14387. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14388. "Convert a time stamp to an absolute day number.
  14389. If there is a specifier for a cyclic time stamp, get the closest date to
  14390. DAYNR.
  14391. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14392. The variable date is bound by the calendar when this is called."
  14393. (cond
  14394. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14395. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14396. daynr
  14397. (+ daynr 1000)))
  14398. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  14399. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14400. (time-to-days (current-time))) (match-string 0 s)
  14401. prefer show-all))
  14402. (t (time-to-days
  14403. (condition-case errdata
  14404. (apply 'encode-time (org-parse-time-string s))
  14405. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14406. s (if (not (and buffer pos))
  14407. ""
  14408. (format " at %d in buffer `%s'" pos buffer))
  14409. (cdr errdata))))))))
  14410. (defun org-days-to-iso-week (days)
  14411. "Return the iso week number."
  14412. (require 'cal-iso)
  14413. (car (calendar-iso-from-absolute days)))
  14414. (defun org-small-year-to-year (year)
  14415. "Convert 2-digit years into 4-digit years.
  14416. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14417. The year 2000 cannot be abbreviated. Any year larger than 99
  14418. is returned unchanged."
  14419. (if (< year 38)
  14420. (setq year (+ 2000 year))
  14421. (if (< year 100)
  14422. (setq year (+ 1900 year))))
  14423. year)
  14424. (defun org-time-from-absolute (d)
  14425. "Return the time corresponding to date D.
  14426. D may be an absolute day number, or a calendar-type list (month day year)."
  14427. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14428. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14429. (defun org-calendar-holiday ()
  14430. "List of holidays, for Diary display in Org-mode."
  14431. (require 'holidays)
  14432. (let ((hl (funcall
  14433. (if (fboundp 'calendar-check-holidays)
  14434. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14435. (if hl (mapconcat 'identity hl "; "))))
  14436. (defun org-diary-sexp-entry (sexp entry date)
  14437. "Process a SEXP diary ENTRY for DATE."
  14438. (require 'diary-lib)
  14439. (let ((result (if calendar-debug-sexp
  14440. (let ((stack-trace-on-error t))
  14441. (eval (car (read-from-string sexp))))
  14442. (condition-case nil
  14443. (eval (car (read-from-string sexp)))
  14444. (error
  14445. (beep)
  14446. (message "Bad sexp at line %d in %s: %s"
  14447. (org-current-line)
  14448. (buffer-file-name) sexp)
  14449. (sleep-for 2))))))
  14450. (cond ((stringp result) (split-string result "; "))
  14451. ((and (consp result)
  14452. (not (consp (cdr result)))
  14453. (stringp (cdr result))) (cdr result))
  14454. ((and (consp result)
  14455. (stringp (car result))) result)
  14456. (result entry)
  14457. (t nil))))
  14458. (defun org-diary-to-ical-string (frombuf)
  14459. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14460. This uses the icalendar.el library."
  14461. (let* ((tmpdir (if (featurep 'xemacs)
  14462. (temp-directory)
  14463. temporary-file-directory))
  14464. (tmpfile (make-temp-name
  14465. (expand-file-name "orgics" tmpdir)))
  14466. buf rtn b e)
  14467. (with-current-buffer frombuf
  14468. (icalendar-export-region (point-min) (point-max) tmpfile)
  14469. (setq buf (find-buffer-visiting tmpfile))
  14470. (set-buffer buf)
  14471. (goto-char (point-min))
  14472. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14473. (setq b (match-beginning 0)))
  14474. (goto-char (point-max))
  14475. (if (re-search-backward "^END:VEVENT" nil t)
  14476. (setq e (match-end 0)))
  14477. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14478. (kill-buffer buf)
  14479. (delete-file tmpfile)
  14480. rtn))
  14481. (defun org-closest-date (start current change prefer show-all)
  14482. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14483. When PREFER is `past', return a date that is either CURRENT or past.
  14484. When PREFER is `future', return a date that is either CURRENT or future.
  14485. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14486. ;; Make the proper lists from the dates
  14487. (catch 'exit
  14488. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14489. dn dw sday cday n1 n2 n0
  14490. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14491. (setq start (org-date-to-gregorian start)
  14492. current (org-date-to-gregorian
  14493. (if show-all
  14494. current
  14495. (time-to-days (current-time))))
  14496. sday (calendar-absolute-from-gregorian start)
  14497. cday (calendar-absolute-from-gregorian current))
  14498. (if (<= cday sday) (throw 'exit sday))
  14499. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14500. (setq dn (string-to-number (match-string 1 change))
  14501. dw (cdr (assoc (match-string 2 change) a1)))
  14502. (error "Invalid change specifier: %s" change))
  14503. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14504. (cond
  14505. ((eq dw 'day)
  14506. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14507. n2 (+ n1 dn)))
  14508. ((eq dw 'year)
  14509. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14510. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14511. (setq date1 (list m d y1)
  14512. n1 (calendar-absolute-from-gregorian date1)
  14513. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14514. n2 (calendar-absolute-from-gregorian date2)))
  14515. ((eq dw 'month)
  14516. ;; approx number of month between the two dates
  14517. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14518. ;; How often does dn fit in there?
  14519. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14520. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14521. m (+ m nm)
  14522. ny (floor (/ m 12))
  14523. y (+ y ny)
  14524. m (- m (* ny 12)))
  14525. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14526. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14527. (setq m2 (+ m dn) y2 y)
  14528. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14529. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14530. (while (<= n2 cday)
  14531. (setq n1 n2 m m2 y y2)
  14532. (setq m2 (+ m dn) y2 y)
  14533. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14534. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14535. ;; Make sure n1 is the earlier date
  14536. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14537. (if show-all
  14538. (cond
  14539. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14540. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14541. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14542. (cond
  14543. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14544. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14545. (t (if (= cday n1) n1 n2)))))))
  14546. (defun org-date-to-gregorian (date)
  14547. "Turn any specification of DATE into a Gregorian date for the calendar."
  14548. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14549. ((and (listp date) (= (length date) 3)) date)
  14550. ((stringp date)
  14551. (setq date (org-parse-time-string date))
  14552. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14553. ((listp date)
  14554. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14555. (defun org-parse-time-string (s &optional nodefault)
  14556. "Parse the standard Org-mode time string.
  14557. This should be a lot faster than the normal `parse-time-string'.
  14558. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14559. hour and minute fields will be nil if not given."
  14560. (if (string-match org-ts-regexp0 s)
  14561. (list 0
  14562. (if (or (match-beginning 8) (not nodefault))
  14563. (string-to-number (or (match-string 8 s) "0")))
  14564. (if (or (match-beginning 7) (not nodefault))
  14565. (string-to-number (or (match-string 7 s) "0")))
  14566. (string-to-number (match-string 4 s))
  14567. (string-to-number (match-string 3 s))
  14568. (string-to-number (match-string 2 s))
  14569. nil nil nil)
  14570. (error "Not a standard Org-mode time string: %s" s)))
  14571. (defun org-timestamp-up (&optional arg)
  14572. "Increase the date item at the cursor by one.
  14573. If the cursor is on the year, change the year. If it is on the month,
  14574. the day or the time, change that.
  14575. With prefix ARG, change by that many units."
  14576. (interactive "p")
  14577. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14578. (defun org-timestamp-down (&optional arg)
  14579. "Decrease the date item at the cursor by one.
  14580. If the cursor is on the year, change the year. If it is on the month,
  14581. the day or the time, change that.
  14582. With prefix ARG, change by that many units."
  14583. (interactive "p")
  14584. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14585. (defun org-timestamp-up-day (&optional arg)
  14586. "Increase the date in the time stamp by one day.
  14587. With prefix ARG, change that many days."
  14588. (interactive "p")
  14589. (if (and (not (org-at-timestamp-p t))
  14590. (org-at-heading-p))
  14591. (org-todo 'up)
  14592. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14593. (defun org-timestamp-down-day (&optional arg)
  14594. "Decrease the date in the time stamp by one day.
  14595. With prefix ARG, change that many days."
  14596. (interactive "p")
  14597. (if (and (not (org-at-timestamp-p t))
  14598. (org-at-heading-p))
  14599. (org-todo 'down)
  14600. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14601. (defun org-at-timestamp-p (&optional inactive-ok)
  14602. "Determine if the cursor is in or at a timestamp."
  14603. (interactive)
  14604. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14605. (pos (point))
  14606. (ans (or (looking-at tsr)
  14607. (save-excursion
  14608. (skip-chars-backward "^[<\n\r\t")
  14609. (if (> (point) (point-min)) (backward-char 1))
  14610. (and (looking-at tsr)
  14611. (> (- (match-end 0) pos) -1))))))
  14612. (and ans
  14613. (boundp 'org-ts-what)
  14614. (setq org-ts-what
  14615. (cond
  14616. ((= pos (match-beginning 0)) 'bracket)
  14617. ;; Point is considered to be "on the bracket" whether
  14618. ;; it's really on it or right after it.
  14619. ((or (= pos (1- (match-end 0)))
  14620. (= pos (match-end 0))) 'bracket)
  14621. ((org-pos-in-match-range pos 2) 'year)
  14622. ((org-pos-in-match-range pos 3) 'month)
  14623. ((org-pos-in-match-range pos 7) 'hour)
  14624. ((org-pos-in-match-range pos 8) 'minute)
  14625. ((or (org-pos-in-match-range pos 4)
  14626. (org-pos-in-match-range pos 5)) 'day)
  14627. ((and (> pos (or (match-end 8) (match-end 5)))
  14628. (< pos (match-end 0)))
  14629. (- pos (or (match-end 8) (match-end 5))))
  14630. (t 'day))))
  14631. ans))
  14632. (defun org-toggle-timestamp-type ()
  14633. "Toggle the type (<active> or [inactive]) of a time stamp."
  14634. (interactive)
  14635. (when (org-at-timestamp-p t)
  14636. (let ((beg (match-beginning 0)) (end (match-end 0))
  14637. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14638. (save-excursion
  14639. (goto-char beg)
  14640. (while (re-search-forward "[][<>]" end t)
  14641. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14642. t t)))
  14643. (message "Timestamp is now %sactive"
  14644. (if (equal (char-after beg) ?<) "" "in")))))
  14645. (defun org-timestamp-change (n &optional what updown)
  14646. "Change the date in the time stamp at point.
  14647. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14648. `year', `minute', `second'. If WHAT is not given, the cursor position
  14649. in the timestamp determines what will be changed."
  14650. (let ((origin (point)) origin-cat
  14651. with-hm inactive
  14652. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14653. org-ts-what
  14654. extra rem
  14655. ts time time0)
  14656. (if (not (org-at-timestamp-p t))
  14657. (error "Not at a timestamp"))
  14658. (if (and (not what) (eq org-ts-what 'bracket))
  14659. (org-toggle-timestamp-type)
  14660. ;; Point isn't on brackets. Remember the part of the time-stamp
  14661. ;; the point was in. Indeed, size of time-stamps may change,
  14662. ;; but point must be kept in the same category nonetheless.
  14663. (setq origin-cat org-ts-what)
  14664. (if (and (not what) (not (eq org-ts-what 'day))
  14665. org-display-custom-times
  14666. (get-text-property (point) 'display)
  14667. (not (get-text-property (1- (point)) 'display)))
  14668. (setq org-ts-what 'day))
  14669. (setq org-ts-what (or what org-ts-what)
  14670. inactive (= (char-after (match-beginning 0)) ?\[)
  14671. ts (match-string 0))
  14672. (replace-match "")
  14673. (if (string-match
  14674. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14675. ts)
  14676. (setq extra (match-string 1 ts)))
  14677. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14678. (setq with-hm t))
  14679. (setq time0 (org-parse-time-string ts))
  14680. (when (and updown
  14681. (eq org-ts-what 'minute)
  14682. (not current-prefix-arg))
  14683. ;; This looks like s-up and s-down. Change by one rounding step.
  14684. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14685. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14686. (setcar (cdr time0) (+ (nth 1 time0)
  14687. (if (> n 0) (- rem) (- dm rem))))))
  14688. (setq time
  14689. (encode-time (or (car time0) 0)
  14690. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14691. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14692. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14693. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14694. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14695. (nthcdr 6 time0)))
  14696. (when (and (member org-ts-what '(hour minute))
  14697. extra
  14698. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14699. (setq extra (org-modify-ts-extra
  14700. extra
  14701. (if (eq org-ts-what 'hour) 2 5)
  14702. n dm)))
  14703. (when (integerp org-ts-what)
  14704. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14705. (if (eq what 'calendar)
  14706. (let ((cal-date (org-get-date-from-calendar)))
  14707. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14708. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14709. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14710. (setcar time0 (or (car time0) 0))
  14711. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14712. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14713. (setq time (apply 'encode-time time0))))
  14714. ;; Insert the new time-stamp, and ensure point stays in the same
  14715. ;; category as before (i.e. not after the last position in that
  14716. ;; category).
  14717. (let ((pos (point)))
  14718. ;; Stay before inserted string. `save-excursion' is of no use.
  14719. (setq org-last-changed-timestamp
  14720. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14721. (goto-char pos))
  14722. (save-match-data
  14723. (looking-at org-ts-regexp3)
  14724. (goto-char (cond
  14725. ;; `day' category ends before `hour' if any, or at
  14726. ;; the end of the day name.
  14727. ((eq origin-cat 'day)
  14728. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14729. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14730. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14731. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14732. ;; `year' and `month' have both fixed size: point
  14733. ;; couldn't have moved into another part.
  14734. (t origin))))
  14735. ;; Update clock if on a CLOCK line.
  14736. (org-clock-update-time-maybe)
  14737. ;; Try to recenter the calendar window, if any.
  14738. (if (and org-calendar-follow-timestamp-change
  14739. (get-buffer-window "*Calendar*" t)
  14740. (memq org-ts-what '(day month year)))
  14741. (org-recenter-calendar (time-to-days time))))))
  14742. (defun org-modify-ts-extra (s pos n dm)
  14743. "Change the different parts of the lead-time and repeat fields in timestamp."
  14744. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14745. ng h m new rem)
  14746. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14747. (cond
  14748. ((or (org-pos-in-match-range pos 2)
  14749. (org-pos-in-match-range pos 3))
  14750. (setq m (string-to-number (match-string 3 s))
  14751. h (string-to-number (match-string 2 s)))
  14752. (if (org-pos-in-match-range pos 2)
  14753. (setq h (+ h n))
  14754. (setq n (* dm (org-no-warnings (signum n))))
  14755. (when (not (= 0 (setq rem (% m dm))))
  14756. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14757. (setq m (+ m n)))
  14758. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14759. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14760. (setq h (min 24 (max 0 h)))
  14761. (setq ng 1 new (format "-%02d:%02d" h m)))
  14762. ((org-pos-in-match-range pos 6)
  14763. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14764. ((org-pos-in-match-range pos 5)
  14765. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14766. ((org-pos-in-match-range pos 9)
  14767. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14768. ((org-pos-in-match-range pos 8)
  14769. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14770. (when ng
  14771. (setq s (concat
  14772. (substring s 0 (match-beginning ng))
  14773. new
  14774. (substring s (match-end ng))))))
  14775. s))
  14776. (defun org-recenter-calendar (date)
  14777. "If the calendar is visible, recenter it to DATE."
  14778. (let* ((win (selected-window))
  14779. (cwin (get-buffer-window "*Calendar*" t))
  14780. (calendar-move-hook nil))
  14781. (when cwin
  14782. (select-window cwin)
  14783. (calendar-goto-date (if (listp date) date
  14784. (calendar-gregorian-from-absolute date)))
  14785. (select-window win))))
  14786. (defun org-goto-calendar (&optional arg)
  14787. "Go to the Emacs calendar at the current date.
  14788. If there is a time stamp in the current line, go to that date.
  14789. A prefix ARG can be used to force the current date."
  14790. (interactive "P")
  14791. (let ((tsr org-ts-regexp) diff
  14792. (calendar-move-hook nil)
  14793. (calendar-view-holidays-initially-flag nil)
  14794. (calendar-view-diary-initially-flag nil))
  14795. (if (or (org-at-timestamp-p)
  14796. (save-excursion
  14797. (beginning-of-line 1)
  14798. (looking-at (concat ".*" tsr))))
  14799. (let ((d1 (time-to-days (current-time)))
  14800. (d2 (time-to-days
  14801. (org-time-string-to-time (match-string 1)))))
  14802. (setq diff (- d2 d1))))
  14803. (calendar)
  14804. (calendar-goto-today)
  14805. (if (and diff (not arg)) (calendar-forward-day diff))))
  14806. (defun org-get-date-from-calendar ()
  14807. "Return a list (month day year) of date at point in calendar."
  14808. (with-current-buffer "*Calendar*"
  14809. (save-match-data
  14810. (calendar-cursor-to-date))))
  14811. (defun org-date-from-calendar ()
  14812. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14813. If there is already a time stamp at the cursor position, update it."
  14814. (interactive)
  14815. (if (org-at-timestamp-p t)
  14816. (org-timestamp-change 0 'calendar)
  14817. (let ((cal-date (org-get-date-from-calendar)))
  14818. (org-insert-time-stamp
  14819. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14820. (defun org-minutes-to-hh:mm-string (m)
  14821. "Compute H:MM from a number of minutes."
  14822. (let ((h (/ m 60)))
  14823. (setq m (- m (* 60 h)))
  14824. (format org-time-clocksum-format h m)))
  14825. (defun org-hh:mm-string-to-minutes (s)
  14826. "Convert a string H:MM to a number of minutes.
  14827. If the string is just a number, interpret it as minutes.
  14828. In fact, the first hh:mm or number in the string will be taken,
  14829. there can be extra stuff in the string.
  14830. If no number is found, the return value is 0."
  14831. (cond
  14832. ((integerp s) s)
  14833. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14834. (+ (* (string-to-number (match-string 1 s)) 60)
  14835. (string-to-number (match-string 2 s))))
  14836. ((string-match "\\([0-9]+\\)" s)
  14837. (string-to-number (match-string 1 s)))
  14838. (t 0)))
  14839. (defcustom org-effort-durations
  14840. `(("h" . 60)
  14841. ("d" . ,(* 60 8))
  14842. ("w" . ,(* 60 8 5))
  14843. ("m" . ,(* 60 8 5 4))
  14844. ("y" . ,(* 60 8 5 40)))
  14845. "Conversion factor to minutes for an effort modifier.
  14846. Each entry has the form (MODIFIER . MINUTES).
  14847. In an effort string, a number followed by MODIFIER is multiplied
  14848. by the specified number of MINUTES to obtain an effort in
  14849. minutes.
  14850. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14851. effort string \"2hours\" is equivalent to 120 minutes."
  14852. :group 'org-agenda
  14853. :type '(alist :key-type (string :tag "Modifier")
  14854. :value-type (number :tag "Minutes")))
  14855. (defun org-duration-string-to-minutes (s)
  14856. "Convert a duration string S to minutes.
  14857. A bare number is interpreted as minutes, modifiers can be set by
  14858. customizing `org-effort-durations' (which see).
  14859. Entries containing a colon are interpreted as H:MM by
  14860. `org-hh:mm-string-to-minutes'."
  14861. (let ((result 0)
  14862. (re (concat "\\([0-9]+\\) *\\("
  14863. (regexp-opt (mapcar 'car org-effort-durations))
  14864. "\\)")))
  14865. (while (string-match re s)
  14866. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14867. (string-to-number (match-string 1 s))))
  14868. (setq s (replace-match "" nil t s)))
  14869. (incf result (org-hh:mm-string-to-minutes s))
  14870. result))
  14871. ;;;; Files
  14872. (defun org-save-all-org-buffers ()
  14873. "Save all Org-mode buffers without user confirmation."
  14874. (interactive)
  14875. (message "Saving all Org-mode buffers...")
  14876. (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
  14877. (when (featurep 'org-id) (org-id-locations-save))
  14878. (message "Saving all Org-mode buffers... done"))
  14879. (defun org-revert-all-org-buffers ()
  14880. "Revert all Org-mode buffers.
  14881. Prompt for confirmation when there are unsaved changes.
  14882. Be sure you know what you are doing before letting this function
  14883. overwrite your changes.
  14884. This function is useful in a setup where one tracks org files
  14885. with a version control system, to revert on one machine after pulling
  14886. changes from another. I believe the procedure must be like this:
  14887. 1. M-x org-save-all-org-buffers
  14888. 2. Pull changes from the other machine, resolve conflicts
  14889. 3. M-x org-revert-all-org-buffers"
  14890. (interactive)
  14891. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14892. (error "Abort"))
  14893. (save-excursion
  14894. (save-window-excursion
  14895. (mapc
  14896. (lambda (b)
  14897. (when (and (with-current-buffer b (eq major-mode 'org-mode))
  14898. (with-current-buffer b buffer-file-name))
  14899. (org-pop-to-buffer-same-window b)
  14900. (revert-buffer t 'no-confirm)))
  14901. (buffer-list))
  14902. (when (and (featurep 'org-id) org-id-track-globally)
  14903. (org-id-locations-load)))))
  14904. ;;;; Agenda files
  14905. ;;;###autoload
  14906. (defun org-switchb (&optional arg)
  14907. "Switch between Org buffers.
  14908. With one prefix argument, restrict available buffers to files.
  14909. With two prefix arguments, restrict available buffers to agenda files.
  14910. Defaults to `iswitchb' for buffer name completion.
  14911. Set `org-completion-use-ido' to make it use ido instead."
  14912. (interactive "P")
  14913. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14914. ((equal arg '(16)) (org-buffer-list 'agenda))
  14915. (t (org-buffer-list))))
  14916. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14917. (org-completion-use-ido org-completion-use-ido))
  14918. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14919. (setq org-completion-use-iswitchb t))
  14920. (org-pop-to-buffer-same-window
  14921. (org-icompleting-read "Org buffer: "
  14922. (mapcar 'list (mapcar 'buffer-name blist))
  14923. nil t))))
  14924. ;;; Define some older names previously used for this functionality
  14925. ;;;###autoload
  14926. (defalias 'org-ido-switchb 'org-switchb)
  14927. ;;;###autoload
  14928. (defalias 'org-iswitchb 'org-switchb)
  14929. (defun org-buffer-list (&optional predicate exclude-tmp)
  14930. "Return a list of Org buffers.
  14931. PREDICATE can be `export', `files' or `agenda'.
  14932. export restrict the list to Export buffers.
  14933. files restrict the list to buffers visiting Org files.
  14934. agenda restrict the list to buffers visiting agenda files.
  14935. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14936. (let* ((bfn nil)
  14937. (agenda-files (and (eq predicate 'agenda)
  14938. (mapcar 'file-truename (org-agenda-files t))))
  14939. (filter
  14940. (cond
  14941. ((eq predicate 'files)
  14942. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  14943. ((eq predicate 'export)
  14944. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14945. ((eq predicate 'agenda)
  14946. (lambda (b)
  14947. (with-current-buffer b
  14948. (and (eq major-mode 'org-mode)
  14949. (setq bfn (buffer-file-name b))
  14950. (member (file-truename bfn) agenda-files)))))
  14951. (t (lambda (b) (with-current-buffer b
  14952. (or (eq major-mode 'org-mode)
  14953. (string-match "\*Org .*Export"
  14954. (buffer-name b)))))))))
  14955. (delq nil
  14956. (mapcar
  14957. (lambda(b)
  14958. (if (and (funcall filter b)
  14959. (or (not exclude-tmp)
  14960. (not (string-match "tmp" (buffer-name b)))))
  14961. b
  14962. nil))
  14963. (buffer-list)))))
  14964. (defun org-agenda-files (&optional unrestricted archives)
  14965. "Get the list of agenda files.
  14966. Optional UNRESTRICTED means return the full list even if a restriction
  14967. is currently in place.
  14968. When ARCHIVES is t, include all archive files that are really being
  14969. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14970. `org-agenda-archives-mode' is t."
  14971. (let ((files
  14972. (cond
  14973. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14974. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14975. ((listp org-agenda-files) org-agenda-files)
  14976. (t (error "Invalid value of `org-agenda-files'")))))
  14977. (setq files (apply 'append
  14978. (mapcar (lambda (f)
  14979. (if (file-directory-p f)
  14980. (directory-files
  14981. f t org-agenda-file-regexp)
  14982. (list f)))
  14983. files)))
  14984. (when org-agenda-skip-unavailable-files
  14985. (setq files (delq nil
  14986. (mapcar (function
  14987. (lambda (file)
  14988. (and (file-readable-p file) file)))
  14989. files))))
  14990. (when (or (eq archives t)
  14991. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14992. (setq files (org-add-archive-files files)))
  14993. files))
  14994. (defun org-agenda-file-p (&optional file)
  14995. "Return non-nil, if FILE is an agenda file.
  14996. If FILE is omitted, use the file associated with the current
  14997. buffer."
  14998. (member (or file (buffer-file-name))
  14999. (org-agenda-files t)))
  15000. (defun org-edit-agenda-file-list ()
  15001. "Edit the list of agenda files.
  15002. Depending on setup, this either uses customize to edit the variable
  15003. `org-agenda-files', or it visits the file that is holding the list. In the
  15004. latter case, the buffer is set up in a way that saving it automatically kills
  15005. the buffer and restores the previous window configuration."
  15006. (interactive)
  15007. (if (stringp org-agenda-files)
  15008. (let ((cw (current-window-configuration)))
  15009. (find-file org-agenda-files)
  15010. (org-set-local 'org-window-configuration cw)
  15011. (org-add-hook 'after-save-hook
  15012. (lambda ()
  15013. (set-window-configuration
  15014. (prog1 org-window-configuration
  15015. (kill-buffer (current-buffer))))
  15016. (org-install-agenda-files-menu)
  15017. (message "New agenda file list installed"))
  15018. nil 'local)
  15019. (message "%s" (substitute-command-keys
  15020. "Edit list and finish with \\[save-buffer]")))
  15021. (customize-variable 'org-agenda-files)))
  15022. (defun org-store-new-agenda-file-list (list)
  15023. "Set new value for the agenda file list and save it correctly."
  15024. (if (stringp org-agenda-files)
  15025. (let ((fe (org-read-agenda-file-list t)) b u)
  15026. (while (setq b (find-buffer-visiting org-agenda-files))
  15027. (kill-buffer b))
  15028. (with-temp-file org-agenda-files
  15029. (insert
  15030. (mapconcat
  15031. (lambda (f) ;; Keep un-expanded entries.
  15032. (if (setq u (assoc f fe))
  15033. (cdr u)
  15034. f))
  15035. list "\n")
  15036. "\n")))
  15037. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15038. (org-insert-mode-line-in-empty-file nil))
  15039. (setq org-agenda-files list)
  15040. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15041. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15042. "Read the list of agenda files from a file.
  15043. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15044. filenames, used by `org-store-new-agenda-file-list' to write back
  15045. un-expanded file names."
  15046. (when (file-directory-p org-agenda-files)
  15047. (error "`org-agenda-files' cannot be a single directory"))
  15048. (when (stringp org-agenda-files)
  15049. (with-temp-buffer
  15050. (insert-file-contents org-agenda-files)
  15051. (mapcar
  15052. (lambda (f)
  15053. (let ((e (expand-file-name (substitute-in-file-name f)
  15054. org-directory)))
  15055. (if pair-with-expansion
  15056. (cons e f)
  15057. e)))
  15058. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15059. ;;;###autoload
  15060. (defun org-cycle-agenda-files ()
  15061. "Cycle through the files in `org-agenda-files'.
  15062. If the current buffer visits an agenda file, find the next one in the list.
  15063. If the current buffer does not, find the first agenda file."
  15064. (interactive)
  15065. (let* ((fs (org-agenda-files t))
  15066. (files (append fs (list (car fs))))
  15067. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15068. file)
  15069. (unless files (error "No agenda files"))
  15070. (catch 'exit
  15071. (while (setq file (pop files))
  15072. (if (equal (file-truename file) tcf)
  15073. (when (car files)
  15074. (find-file (car files))
  15075. (throw 'exit t))))
  15076. (find-file (car fs)))
  15077. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15078. (defun org-agenda-file-to-front (&optional to-end)
  15079. "Move/add the current file to the top of the agenda file list.
  15080. If the file is not present in the list, it is added to the front. If it is
  15081. present, it is moved there. With optional argument TO-END, add/move to the
  15082. end of the list."
  15083. (interactive "P")
  15084. (let ((org-agenda-skip-unavailable-files nil)
  15085. (file-alist (mapcar (lambda (x)
  15086. (cons (file-truename x) x))
  15087. (org-agenda-files t)))
  15088. (ctf (file-truename buffer-file-name))
  15089. x had)
  15090. (setq x (assoc ctf file-alist) had x)
  15091. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15092. (if to-end
  15093. (setq file-alist (append (delq x file-alist) (list x)))
  15094. (setq file-alist (cons x (delq x file-alist))))
  15095. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15096. (org-install-agenda-files-menu)
  15097. (message "File %s to %s of agenda file list"
  15098. (if had "moved" "added") (if to-end "end" "front"))))
  15099. (defun org-remove-file (&optional file)
  15100. "Remove current file from the list of files in variable `org-agenda-files'.
  15101. These are the files which are being checked for agenda entries.
  15102. Optional argument FILE means use this file instead of the current."
  15103. (interactive)
  15104. (let* ((org-agenda-skip-unavailable-files nil)
  15105. (file (or file buffer-file-name))
  15106. (true-file (file-truename file))
  15107. (afile (abbreviate-file-name file))
  15108. (files (delq nil (mapcar
  15109. (lambda (x)
  15110. (if (equal true-file
  15111. (file-truename x))
  15112. nil x))
  15113. (org-agenda-files t)))))
  15114. (if (not (= (length files) (length (org-agenda-files t))))
  15115. (progn
  15116. (org-store-new-agenda-file-list files)
  15117. (org-install-agenda-files-menu)
  15118. (message "Removed file: %s" afile))
  15119. (message "File was not in list: %s (not removed)" afile))))
  15120. (defun org-file-menu-entry (file)
  15121. (vector file (list 'find-file file) t))
  15122. (defun org-check-agenda-file (file)
  15123. "Make sure FILE exists. If not, ask user what to do."
  15124. (when (not (file-exists-p file))
  15125. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15126. (abbreviate-file-name file))
  15127. (let ((r (downcase (read-char-exclusive))))
  15128. (cond
  15129. ((equal r ?r)
  15130. (org-remove-file file)
  15131. (throw 'nextfile t))
  15132. (t (error "Abort"))))))
  15133. (defun org-get-agenda-file-buffer (file)
  15134. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15135. it to the list of buffers which might be released later."
  15136. (let ((buf (org-find-base-buffer-visiting file)))
  15137. (if buf
  15138. buf ; just return it
  15139. ;; Make a new buffer and remember it
  15140. (setq buf (find-file-noselect file))
  15141. (if buf (push buf org-agenda-new-buffers))
  15142. buf)))
  15143. (defun org-release-buffers (blist)
  15144. "Release all buffers in list, asking the user for confirmation when needed.
  15145. When a buffer is unmodified, it is just killed. When modified, it is saved
  15146. \(if the user agrees) and then killed."
  15147. (let (buf file)
  15148. (while (setq buf (pop blist))
  15149. (setq file (buffer-file-name buf))
  15150. (when (and (buffer-modified-p buf)
  15151. file
  15152. (y-or-n-p (format "Save file %s? " file)))
  15153. (with-current-buffer buf (save-buffer)))
  15154. (kill-buffer buf))))
  15155. (defun org-prepare-agenda-buffers (files)
  15156. "Create buffers for all agenda files, protect archived trees and comments."
  15157. (interactive)
  15158. (let ((pa '(:org-archived t))
  15159. (pc '(:org-comment t))
  15160. (pall '(:org-archived t :org-comment t))
  15161. (inhibit-read-only t)
  15162. (rea (concat ":" org-archive-tag ":"))
  15163. bmp file re)
  15164. (save-excursion
  15165. (save-restriction
  15166. (while (setq file (pop files))
  15167. (catch 'nextfile
  15168. (if (bufferp file)
  15169. (set-buffer file)
  15170. (org-check-agenda-file file)
  15171. (set-buffer (org-get-agenda-file-buffer file)))
  15172. (widen)
  15173. (setq bmp (buffer-modified-p))
  15174. (org-refresh-category-properties)
  15175. (setq org-todo-keywords-for-agenda
  15176. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15177. (setq org-done-keywords-for-agenda
  15178. (append org-done-keywords-for-agenda org-done-keywords))
  15179. (setq org-todo-keyword-alist-for-agenda
  15180. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15181. (setq org-drawers-for-agenda
  15182. (append org-drawers-for-agenda org-drawers))
  15183. (setq org-tag-alist-for-agenda
  15184. (append org-tag-alist-for-agenda org-tag-alist))
  15185. (save-excursion
  15186. (remove-text-properties (point-min) (point-max) pall)
  15187. (when org-agenda-skip-archived-trees
  15188. (goto-char (point-min))
  15189. (while (re-search-forward rea nil t)
  15190. (if (org-at-heading-p t)
  15191. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15192. (goto-char (point-min))
  15193. (setq re (format org-heading-keyword-regexp-format
  15194. org-comment-string))
  15195. (while (re-search-forward re nil t)
  15196. (add-text-properties
  15197. (match-beginning 0) (org-end-of-subtree t) pc)))
  15198. (set-buffer-modified-p bmp)))))
  15199. (setq org-todo-keywords-for-agenda
  15200. (org-uniquify org-todo-keywords-for-agenda))
  15201. (setq org-todo-keyword-alist-for-agenda
  15202. (org-uniquify org-todo-keyword-alist-for-agenda)
  15203. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15204. ;;;; Embedded LaTeX
  15205. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15206. "Keymap for the minor `org-cdlatex-mode'.")
  15207. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15208. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15209. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15210. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15211. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15212. (defvar org-cdlatex-texmathp-advice-is-done nil
  15213. "Flag remembering if we have applied the advice to texmathp already.")
  15214. (define-minor-mode org-cdlatex-mode
  15215. "Toggle the minor `org-cdlatex-mode'.
  15216. This mode supports entering LaTeX environment and math in LaTeX fragments
  15217. in Org-mode.
  15218. \\{org-cdlatex-mode-map}"
  15219. nil " OCDL" nil
  15220. (when org-cdlatex-mode
  15221. (require 'cdlatex)
  15222. (run-hooks 'cdlatex-mode-hook)
  15223. (cdlatex-compute-tables))
  15224. (unless org-cdlatex-texmathp-advice-is-done
  15225. (setq org-cdlatex-texmathp-advice-is-done t)
  15226. (defadvice texmathp (around org-math-always-on activate)
  15227. "Always return t in org-mode buffers.
  15228. This is because we want to insert math symbols without dollars even outside
  15229. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15230. an embedded LaTeX fragment, let texmathp do its job.
  15231. \\[org-cdlatex-mode-map]"
  15232. (interactive)
  15233. (let (p)
  15234. (cond
  15235. ((not (eq major-mode 'org-mode)) ad-do-it)
  15236. ((eq this-command 'cdlatex-math-symbol)
  15237. (setq ad-return-value t
  15238. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15239. (t
  15240. (let ((p (org-inside-LaTeX-fragment-p)))
  15241. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15242. (setq ad-return-value t
  15243. texmathp-why '("Org-mode embedded math" . 0))
  15244. (if p ad-do-it)))))))))
  15245. (defun turn-on-org-cdlatex ()
  15246. "Unconditionally turn on `org-cdlatex-mode'."
  15247. (org-cdlatex-mode 1))
  15248. (defun org-inside-LaTeX-fragment-p ()
  15249. "Test if point is inside a LaTeX fragment.
  15250. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15251. sequence appearing also before point.
  15252. Even though the matchers for math are configurable, this function assumes
  15253. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15254. delimiters are skipped when they have been removed by customization.
  15255. The return value is nil, or a cons cell with the delimiter and the
  15256. position of this delimiter.
  15257. This function does a reasonably good job, but can locally be fooled by
  15258. for example currency specifications. For example it will assume being in
  15259. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15260. fragments that are properly closed, but during editing, we have to live
  15261. with the uncertainty caused by missing closing delimiters. This function
  15262. looks only before point, not after."
  15263. (catch 'exit
  15264. (let ((pos (point))
  15265. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15266. (lim (progn
  15267. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15268. (point)))
  15269. dd-on str (start 0) m re)
  15270. (goto-char pos)
  15271. (when dodollar
  15272. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15273. re (nth 1 (assoc "$" org-latex-regexps)))
  15274. (while (string-match re str start)
  15275. (cond
  15276. ((= (match-end 0) (length str))
  15277. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15278. ((= (match-end 0) (- (length str) 5))
  15279. (throw 'exit nil))
  15280. (t (setq start (match-end 0))))))
  15281. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15282. (goto-char pos)
  15283. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15284. (and (match-beginning 2) (throw 'exit nil))
  15285. ;; count $$
  15286. (while (re-search-backward "\\$\\$" lim t)
  15287. (setq dd-on (not dd-on)))
  15288. (goto-char pos)
  15289. (if dd-on (cons "$$" m))))))
  15290. (defun org-inside-latex-macro-p ()
  15291. "Is point inside a LaTeX macro or its arguments?"
  15292. (save-match-data
  15293. (org-in-regexp
  15294. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15295. (defun org-try-cdlatex-tab ()
  15296. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15297. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15298. - inside a LaTeX fragment, or
  15299. - after the first word in a line, where an abbreviation expansion could
  15300. insert a LaTeX environment."
  15301. (when org-cdlatex-mode
  15302. (cond
  15303. ;; Before any word on the line: No expansion possible.
  15304. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15305. ;; Just after first word on the line: Expand it. Make sure it
  15306. ;; cannot happen on headlines, though.
  15307. ((save-excursion
  15308. (skip-chars-backward "a-zA-Z0-9*")
  15309. (skip-chars-backward " \t")
  15310. (and (bolp) (not (org-at-heading-p))))
  15311. (cdlatex-tab) t)
  15312. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15313. (defun org-cdlatex-underscore-caret (&optional arg)
  15314. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15315. Revert to the normal definition outside of these fragments."
  15316. (interactive "P")
  15317. (if (org-inside-LaTeX-fragment-p)
  15318. (call-interactively 'cdlatex-sub-superscript)
  15319. (let (org-cdlatex-mode)
  15320. (call-interactively (key-binding (vector last-input-event))))))
  15321. (defun org-cdlatex-math-modify (&optional arg)
  15322. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15323. Revert to the normal definition outside of these fragments."
  15324. (interactive "P")
  15325. (if (org-inside-LaTeX-fragment-p)
  15326. (call-interactively 'cdlatex-math-modify)
  15327. (let (org-cdlatex-mode)
  15328. (call-interactively (key-binding (vector last-input-event))))))
  15329. (defvar org-latex-fragment-image-overlays nil
  15330. "List of overlays carrying the images of latex fragments.")
  15331. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15332. (defun org-remove-latex-fragment-image-overlays ()
  15333. "Remove all overlays with LaTeX fragment images in current buffer."
  15334. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15335. (setq org-latex-fragment-image-overlays nil))
  15336. (defun org-preview-latex-fragment (&optional subtree)
  15337. "Preview the LaTeX fragment at point, or all locally or globally.
  15338. If the cursor is in a LaTeX fragment, create the image and overlay
  15339. it over the source code. If there is no fragment at point, display
  15340. all fragments in the current text, from one headline to the next. With
  15341. prefix SUBTREE, display all fragments in the current subtree. With a
  15342. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15343. the cursor is before the first headline,
  15344. display all fragments in the buffer.
  15345. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15346. (interactive "P")
  15347. (unless buffer-file-name
  15348. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15349. (org-remove-latex-fragment-image-overlays)
  15350. (save-excursion
  15351. (save-restriction
  15352. (let (beg end at msg)
  15353. (cond
  15354. ((or (equal subtree '(16))
  15355. (not (save-excursion
  15356. (re-search-backward org-outline-regexp-bol nil t))))
  15357. (setq beg (point-min) end (point-max)
  15358. msg "Creating images for buffer...%s"))
  15359. ((equal subtree '(4))
  15360. (org-back-to-heading)
  15361. (setq beg (point) end (org-end-of-subtree t)
  15362. msg "Creating images for subtree...%s"))
  15363. (t
  15364. (if (setq at (org-inside-LaTeX-fragment-p))
  15365. (goto-char (max (point-min) (- (cdr at) 2)))
  15366. (org-back-to-heading))
  15367. (setq beg (point) end (progn (outline-next-heading) (point))
  15368. msg (if at "Creating image...%s"
  15369. "Creating images for entry...%s"))))
  15370. (message msg "")
  15371. (narrow-to-region beg end)
  15372. (goto-char beg)
  15373. (org-format-latex
  15374. (concat "ltxpng/" (file-name-sans-extension
  15375. (file-name-nondirectory
  15376. buffer-file-name)))
  15377. default-directory 'overlays msg at 'forbuffer 'dvipng)
  15378. (message msg "done. Use `C-c C-c' to remove images.")))))
  15379. (defvar org-latex-regexps
  15380. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15381. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15382. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15383. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15384. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15385. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15386. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15387. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15388. "Regular expressions for matching embedded LaTeX.")
  15389. (defvar org-export-have-math nil) ;; dynamic scoping
  15390. (defun org-format-latex (prefix &optional dir overlays msg at
  15391. forbuffer processing-type)
  15392. "Replace LaTeX fragments with links to an image, and produce images.
  15393. Some of the options can be changed using the variable
  15394. `org-format-latex-options'."
  15395. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15396. (let* ((prefixnodir (file-name-nondirectory prefix))
  15397. (absprefix (expand-file-name prefix dir))
  15398. (todir (file-name-directory absprefix))
  15399. (opt org-format-latex-options)
  15400. (matchers (plist-get opt :matchers))
  15401. (re-list org-latex-regexps)
  15402. (org-format-latex-header-extra
  15403. (plist-get (org-infile-export-plist) :latex-header-extra))
  15404. (cnt 0) txt hash link beg end re e checkdir
  15405. executables-checked string
  15406. m n block-type block linkfile movefile ov)
  15407. ;; Check the different regular expressions
  15408. (while (setq e (pop re-list))
  15409. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15410. block (if block-type "\n\n" ""))
  15411. (when (member m matchers)
  15412. (goto-char (point-min))
  15413. (while (re-search-forward re nil t)
  15414. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15415. (not (get-text-property (match-beginning n)
  15416. 'org-protected))
  15417. (or (not overlays)
  15418. (not (eq (get-char-property (match-beginning n)
  15419. 'org-overlay-type)
  15420. 'org-latex-overlay))))
  15421. (setq org-export-have-math t)
  15422. (cond
  15423. ((eq processing-type 'verbatim)
  15424. ;; Leave the text verbatim, just protect it
  15425. (add-text-properties (match-beginning n) (match-end n)
  15426. '(org-protected t)))
  15427. ((eq processing-type 'mathjax)
  15428. ;; Prepare for MathJax processing
  15429. (setq string (match-string n))
  15430. (if (member m '("$" "$1"))
  15431. (save-excursion
  15432. (delete-region (match-beginning n) (match-end n))
  15433. (goto-char (match-beginning n))
  15434. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15435. "\\)")
  15436. '(org-protected t))))
  15437. (add-text-properties (match-beginning n) (match-end n)
  15438. '(org-protected t))))
  15439. ((eq processing-type 'dvipng)
  15440. ;; Process to an image
  15441. (setq txt (match-string n)
  15442. beg (match-beginning n) end (match-end n)
  15443. cnt (1+ cnt))
  15444. (let (print-length print-level) ; make sure full list is printed
  15445. (setq hash (sha1 (prin1-to-string
  15446. (list org-format-latex-header
  15447. org-format-latex-header-extra
  15448. org-export-latex-default-packages-alist
  15449. org-export-latex-packages-alist
  15450. org-format-latex-options
  15451. forbuffer txt)))
  15452. linkfile (format "%s_%s.png" prefix hash)
  15453. movefile (format "%s_%s.png" absprefix hash)))
  15454. (setq link (concat block "[[file:" linkfile "]]" block))
  15455. (if msg (message msg cnt))
  15456. (goto-char beg)
  15457. (unless checkdir ; make sure the directory exists
  15458. (setq checkdir t)
  15459. (or (file-directory-p todir) (make-directory todir t)))
  15460. (unless executables-checked
  15461. (org-check-external-command
  15462. "latex" "needed to convert LaTeX fragments to images")
  15463. (org-check-external-command
  15464. "dvipng" "needed to convert LaTeX fragments to images")
  15465. (setq executables-checked t))
  15466. (unless (file-exists-p movefile)
  15467. (org-create-formula-image
  15468. txt movefile opt forbuffer))
  15469. (if overlays
  15470. (progn
  15471. (mapc (lambda (o)
  15472. (if (eq (overlay-get o 'org-overlay-type)
  15473. 'org-latex-overlay)
  15474. (delete-overlay o)))
  15475. (overlays-in beg end))
  15476. (setq ov (make-overlay beg end))
  15477. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15478. (if (featurep 'xemacs)
  15479. (progn
  15480. (overlay-put ov 'invisible t)
  15481. (overlay-put
  15482. ov 'end-glyph
  15483. (make-glyph (vector 'png :file movefile))))
  15484. (overlay-put
  15485. ov 'display
  15486. (list 'image :type 'png :file movefile :ascent 'center)))
  15487. (push ov org-latex-fragment-image-overlays)
  15488. (goto-char end))
  15489. (delete-region beg end)
  15490. (insert (org-add-props link
  15491. (list 'org-latex-src
  15492. (replace-regexp-in-string
  15493. "\"" "" txt)
  15494. 'org-latex-src-embed-type
  15495. (if block-type 'paragraph 'character))))))
  15496. ((eq processing-type 'mathml)
  15497. ;; Process to MathML
  15498. (unless executables-checked
  15499. (unless (save-match-data (org-format-latex-mathml-available-p))
  15500. (error "LaTeX to MathML converter not configured"))
  15501. (setq executables-checked t))
  15502. (setq txt (match-string n)
  15503. beg (match-beginning n) end (match-end n)
  15504. cnt (1+ cnt))
  15505. (if msg (message msg cnt))
  15506. (goto-char beg)
  15507. (delete-region beg end)
  15508. (insert (org-format-latex-as-mathml
  15509. txt block-type prefix dir)))
  15510. (t
  15511. (error "Unknown conversion type %s for latex fragments"
  15512. processing-type)))))))))
  15513. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15514. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15515. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15516. sucessful, return the portion between \"<math...> </math>\"
  15517. elements otherwise return nil. When MATHML-FILE is specified,
  15518. write the results in to that file. When invoked as an
  15519. interactive command, prompt for LATEX-FRAG, with initial value
  15520. set to the current active region and echo the results for user
  15521. inspection."
  15522. (interactive (list (let ((frag (when (region-active-p)
  15523. (buffer-substring-no-properties
  15524. (region-beginning) (region-end)))))
  15525. (read-string "LaTeX Fragment: " frag nil frag))))
  15526. (unless latex-frag (error "Invalid latex-frag"))
  15527. (let* ((tmp-in-file (file-relative-name
  15528. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15529. (ignore (write-region latex-frag nil tmp-in-file))
  15530. (tmp-out-file (file-relative-name
  15531. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15532. (cmd (format-spec
  15533. org-latex-to-mathml-convert-command
  15534. `((?j . ,(shell-quote-argument
  15535. (expand-file-name org-latex-to-mathml-jar-file)))
  15536. (?I . ,(shell-quote-argument tmp-in-file))
  15537. (?o . ,(shell-quote-argument tmp-out-file)))))
  15538. mathml shell-command-output)
  15539. (when (org-called-interactively-p 'any)
  15540. (unless (org-format-latex-mathml-available-p)
  15541. (error "LaTeX to MathML converter not configured")))
  15542. (message "Running %s" cmd)
  15543. (setq shell-command-output (shell-command-to-string cmd))
  15544. (setq mathml
  15545. (when (file-readable-p tmp-out-file)
  15546. (with-current-buffer (find-file-noselect tmp-out-file t)
  15547. (goto-char (point-min))
  15548. (when (re-search-forward
  15549. (concat
  15550. (regexp-quote
  15551. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15552. "\\(.\\|\n\\)*"
  15553. (regexp-quote "</math>")) nil t)
  15554. (prog1 (match-string 0) (kill-buffer))))))
  15555. (cond
  15556. (mathml
  15557. (setq mathml
  15558. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15559. (when mathml-file
  15560. (write-region mathml nil mathml-file))
  15561. (when (org-called-interactively-p 'any)
  15562. (message mathml)))
  15563. ((message "LaTeX to MathML conversion failed")
  15564. (message shell-command-output)))
  15565. (delete-file tmp-in-file)
  15566. (when (file-exists-p tmp-out-file)
  15567. (delete-file tmp-out-file))
  15568. mathml))
  15569. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15570. prefix &optional dir)
  15571. "Use `org-create-math-formula' but check local cache first."
  15572. (let* ((absprefix (expand-file-name prefix dir))
  15573. (print-length nil) (print-level nil)
  15574. (formula-id (concat
  15575. "formula-"
  15576. (sha1
  15577. (prin1-to-string
  15578. (list latex-frag
  15579. org-latex-to-mathml-convert-command)))))
  15580. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15581. (formula-cache-dir (file-name-directory formula-cache)))
  15582. (unless (file-directory-p formula-cache-dir)
  15583. (make-directory formula-cache-dir t))
  15584. (unless (file-exists-p formula-cache)
  15585. (org-create-math-formula latex-frag formula-cache))
  15586. (if (file-exists-p formula-cache)
  15587. ;; Successful conversion. Return the link to MathML file.
  15588. (org-add-props
  15589. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15590. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15591. 'org-latex-src-embed-type (if latex-frag-type
  15592. 'paragraph 'character)))
  15593. ;; Failed conversion. Return the LaTeX fragment verbatim
  15594. (add-text-properties
  15595. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15596. latex-frag)))
  15597. ;; This function borrows from Ganesh Swami's latex2png.el
  15598. (defun org-create-formula-image (string tofile options buffer)
  15599. "This calls dvipng."
  15600. (require 'org-latex)
  15601. (let* ((tmpdir (if (featurep 'xemacs)
  15602. (temp-directory)
  15603. temporary-file-directory))
  15604. (texfilebase (make-temp-name
  15605. (expand-file-name "orgtex" tmpdir)))
  15606. (texfile (concat texfilebase ".tex"))
  15607. (dvifile (concat texfilebase ".dvi"))
  15608. (pngfile (concat texfilebase ".png"))
  15609. (fnh (if (featurep 'xemacs)
  15610. (font-height (get-face-font 'default))
  15611. (face-attribute 'default :height nil)))
  15612. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15613. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15614. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15615. "Black"))
  15616. (bg (or (plist-get options (if buffer :background :html-background))
  15617. "Transparent")))
  15618. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15619. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15620. (with-temp-file texfile
  15621. (insert (org-splice-latex-header
  15622. org-format-latex-header
  15623. org-export-latex-default-packages-alist
  15624. org-export-latex-packages-alist t
  15625. org-format-latex-header-extra))
  15626. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15627. (require 'org-latex)
  15628. (org-export-latex-fix-inputenc))
  15629. (let ((dir default-directory))
  15630. (condition-case nil
  15631. (progn
  15632. (cd tmpdir)
  15633. (call-process "latex" nil nil nil texfile))
  15634. (error nil))
  15635. (cd dir))
  15636. (if (not (file-exists-p dvifile))
  15637. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15638. (condition-case nil
  15639. (call-process "dvipng" nil nil nil
  15640. "-fg" fg "-bg" bg
  15641. "-D" dpi
  15642. ;;"-x" scale "-y" scale
  15643. "-T" "tight"
  15644. "-o" pngfile
  15645. dvifile)
  15646. (error nil))
  15647. (if (not (file-exists-p pngfile))
  15648. (if org-format-latex-signal-error
  15649. (error "Failed to create png file from %s" texfile)
  15650. (message "Failed to create png file from %s" texfile)
  15651. nil)
  15652. ;; Use the requested file name and clean up
  15653. (copy-file pngfile tofile 'replace)
  15654. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15655. (delete-file (concat texfilebase e)))
  15656. pngfile))))
  15657. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15658. "Fill a LaTeX header template TPL.
  15659. In the template, the following place holders will be recognized:
  15660. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15661. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15662. [PACKAGES] \\usepackage statements for PKG
  15663. [NO-PACKAGES] do not include PKG
  15664. [EXTRA] the string EXTRA
  15665. [NO-EXTRA] do not include EXTRA
  15666. For backward compatibility, if both the positive and the negative place
  15667. holder is missing, the positive one (without the \"NO-\") will be
  15668. assumed to be present at the end of the template.
  15669. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15670. EXTRA is a string.
  15671. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15672. (let (rpl (end ""))
  15673. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15674. (setq rpl (if (or (match-end 1) (not def-pkg))
  15675. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15676. tpl (replace-match rpl t t tpl))
  15677. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15678. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15679. (setq rpl (if (or (match-end 1) (not pkg))
  15680. "" (org-latex-packages-to-string pkg snippets-p t))
  15681. tpl (replace-match rpl t t tpl))
  15682. (if pkg (setq end
  15683. (concat end "\n"
  15684. (org-latex-packages-to-string pkg snippets-p)))))
  15685. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15686. (setq rpl (if (or (match-end 1) (not extra))
  15687. "" (concat extra "\n"))
  15688. tpl (replace-match rpl t t tpl))
  15689. (if (and extra (string-match "\\S-" extra))
  15690. (setq end (concat end "\n" extra))))
  15691. (if (string-match "\\S-" end)
  15692. (concat tpl "\n" end)
  15693. tpl)))
  15694. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15695. "Turn an alist of packages into a string with the \\usepackage macros."
  15696. (setq pkg (mapconcat (lambda(p)
  15697. (cond
  15698. ((stringp p) p)
  15699. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15700. (format "%% Package %s omitted" (cadr p)))
  15701. ((equal "" (car p))
  15702. (format "\\usepackage{%s}" (cadr p)))
  15703. (t
  15704. (format "\\usepackage[%s]{%s}"
  15705. (car p) (cadr p)))))
  15706. pkg
  15707. "\n"))
  15708. (if newline (concat pkg "\n") pkg))
  15709. (defun org-dvipng-color (attr)
  15710. "Return an rgb color specification for dvipng."
  15711. (apply 'format "rgb %s %s %s"
  15712. (mapcar 'org-normalize-color
  15713. (color-values (face-attribute 'default attr nil)))))
  15714. (defun org-normalize-color (value)
  15715. "Return string to be used as color value for an RGB component."
  15716. (format "%g" (/ value 65535.0)))
  15717. ;; Image display
  15718. (defvar org-inline-image-overlays nil)
  15719. (make-variable-buffer-local 'org-inline-image-overlays)
  15720. (defun org-toggle-inline-images (&optional include-linked)
  15721. "Toggle the display of inline images.
  15722. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15723. (interactive "P")
  15724. (if org-inline-image-overlays
  15725. (progn
  15726. (org-remove-inline-images)
  15727. (message "Inline image display turned off"))
  15728. (org-display-inline-images include-linked)
  15729. (if org-inline-image-overlays
  15730. (message "%d images displayed inline"
  15731. (length org-inline-image-overlays))
  15732. (message "No images to display inline"))))
  15733. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15734. "Display inline images.
  15735. Normally only links without a description part are inlined, because this
  15736. is how it will work for export. When INCLUDE-LINKED is set, also links
  15737. with a description part will be inlined. This can be nice for a quick
  15738. look at those images, but it does not reflect what exported files will look
  15739. like.
  15740. When REFRESH is set, refresh existing images between BEG and END.
  15741. This will create new image displays only if necessary.
  15742. BEG and END default to the buffer boundaries."
  15743. (interactive "P")
  15744. (unless refresh
  15745. (org-remove-inline-images)
  15746. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15747. (save-excursion
  15748. (save-restriction
  15749. (widen)
  15750. (setq beg (or beg (point-min)) end (or end (point-max)))
  15751. (goto-char (point-min))
  15752. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15753. (substring (org-image-file-name-regexp) 0 -2)
  15754. "\\)\\]" (if include-linked "" "\\]")))
  15755. old file ov img)
  15756. (while (re-search-forward re end t)
  15757. (setq old (get-char-property-and-overlay (match-beginning 1)
  15758. 'org-image-overlay))
  15759. (setq file (expand-file-name
  15760. (concat (or (match-string 3) "") (match-string 4))))
  15761. (when (file-exists-p file)
  15762. (if (and (car-safe old) refresh)
  15763. (image-refresh (overlay-get (cdr old) 'display))
  15764. (setq img (save-match-data (create-image file)))
  15765. (when img
  15766. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15767. (overlay-put ov 'display img)
  15768. (overlay-put ov 'face 'default)
  15769. (overlay-put ov 'org-image-overlay t)
  15770. (overlay-put ov 'modification-hooks
  15771. (list 'org-display-inline-modification-hook))
  15772. (push ov org-inline-image-overlays)))))))))
  15773. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15774. "Remove inline-display overlay if a corresponding region is modified."
  15775. (let ((inhibit-modification-hooks t))
  15776. (when (and ov after)
  15777. (delete ov org-inline-image-overlays)
  15778. (delete-overlay ov))))
  15779. (defun org-remove-inline-images ()
  15780. "Remove inline display of images."
  15781. (interactive)
  15782. (mapc 'delete-overlay org-inline-image-overlays)
  15783. (setq org-inline-image-overlays nil))
  15784. ;;;; Key bindings
  15785. ;; Outline functions from `outline-mode-prefix-map'
  15786. ;; that can be remapped in Org:
  15787. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  15788. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  15789. (define-key org-mode-map [remap outline-forward-same-level]
  15790. 'org-forward-same-level)
  15791. (define-key org-mode-map [remap outline-backward-same-level]
  15792. 'org-backward-same-level)
  15793. (define-key org-mode-map [remap show-branches]
  15794. 'org-kill-note-or-show-branches)
  15795. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  15796. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  15797. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  15798. ;; Outline functions from `outline-mode-prefix-map'
  15799. ;; that can not be remapped in Org:
  15800. ;; - the column "key binding" shows whether the Outline function is still
  15801. ;; available in Org mode on the same key that it has been bound to in
  15802. ;; Outline mode:
  15803. ;; - "overridden": key used for a different functionality in Org mode
  15804. ;; - else: key still bound to the same Outline function in Org mode
  15805. ;; | Outline function | key binding | Org replacement |
  15806. ;; |------------------------------------+-------------+-----------------------|
  15807. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  15808. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  15809. ;; | `show-children' | `C-c C-i' | visibility cycling |
  15810. ;; | `hide-subtree' | overridden | visibility cycling |
  15811. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  15812. ;; | `hide-body' | overridden | no replacement |
  15813. ;; | `show-all' | overridden | no replacement |
  15814. ;; | `hide-entry' | overridden | visibility cycling |
  15815. ;; | `show-entry' | overridden | no replacement |
  15816. ;; | `hide-leaves' | overridden | no replacement |
  15817. ;; | `hide-sublevels' | overridden | no replacement |
  15818. ;; | `hide-other' | overridden | no replacement |
  15819. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  15820. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  15821. ;; Make `C-c C-x' a prefix key
  15822. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15823. ;; TAB key with modifiers
  15824. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15825. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15826. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15827. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15828. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15829. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15830. ;; The following line is necessary under Suse GNU/Linux
  15831. (unless (featurep 'xemacs)
  15832. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15833. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15834. (define-key org-mode-map [backtab] 'org-shifttab)
  15835. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15836. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15837. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15838. ;; Cursor keys with modifiers
  15839. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15840. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15841. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15842. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15843. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15844. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15845. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15846. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15847. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15848. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15849. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15850. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15851. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15852. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15853. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  15854. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  15855. ;; Babel keys
  15856. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15857. (mapc (lambda (pair)
  15858. (define-key org-babel-map (car pair) (cdr pair)))
  15859. org-babel-key-bindings)
  15860. ;;; Extra keys for tty access.
  15861. ;; We only set them when really needed because otherwise the
  15862. ;; menus don't show the simple keys
  15863. (when (or org-use-extra-keys
  15864. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15865. (not window-system))
  15866. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15867. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15868. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15869. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15870. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15871. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15872. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15873. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15874. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15875. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15876. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15877. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15878. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15879. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15880. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15881. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15882. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15883. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15884. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15885. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15886. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15887. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15888. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15889. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15890. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15891. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15892. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15893. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15894. ;; All the other keys
  15895. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15896. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15897. (if (boundp 'narrow-map)
  15898. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15899. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15900. (if (boundp 'narrow-map)
  15901. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15902. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15903. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15904. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15905. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15906. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15907. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15908. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  15909. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15910. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15911. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15912. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15913. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15914. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15915. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15916. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15917. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15918. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15919. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15920. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15921. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15922. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15923. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15924. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  15925. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15926. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15927. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15928. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15929. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15930. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15931. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15932. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15933. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15934. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15935. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15936. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15937. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15938. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15939. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15940. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15941. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15942. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15943. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15944. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15945. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15946. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15947. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15948. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15949. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15950. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15951. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15952. (org-defkey org-mode-map "\C-m" 'org-return)
  15953. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15954. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15955. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15956. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15957. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15958. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15959. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15960. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15961. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15962. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15963. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15964. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15965. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15966. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15967. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15968. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15969. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15970. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15971. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15972. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15973. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  15974. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15975. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15976. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15977. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15978. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15979. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15980. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15981. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15982. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15983. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15984. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15985. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15986. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15987. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15988. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15989. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15990. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15991. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15992. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15993. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15994. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15995. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15996. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15997. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15998. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15999. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16000. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16001. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16002. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16003. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16004. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16005. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16006. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16007. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16008. (when (featurep 'xemacs)
  16009. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16010. (defconst org-speed-commands-default
  16011. '(
  16012. ("Outline Navigation")
  16013. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16014. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16015. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16016. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16017. ("u" . (org-speed-move-safe 'outline-up-heading))
  16018. ("j" . org-goto)
  16019. ("g" . (org-refile t))
  16020. ("Outline Visibility")
  16021. ("c" . org-cycle)
  16022. ("C" . org-shifttab)
  16023. (" " . org-display-outline-path)
  16024. ("Outline Structure Editing")
  16025. ("U" . org-shiftmetaup)
  16026. ("D" . org-shiftmetadown)
  16027. ("r" . org-metaright)
  16028. ("l" . org-metaleft)
  16029. ("R" . org-shiftmetaright)
  16030. ("L" . org-shiftmetaleft)
  16031. ("i" . (progn (forward-char 1) (call-interactively
  16032. 'org-insert-heading-respect-content)))
  16033. ("^" . org-sort)
  16034. ("w" . org-refile)
  16035. ("a" . org-archive-subtree-default-with-confirmation)
  16036. ("." . org-mark-subtree)
  16037. ("Clock Commands")
  16038. ("I" . org-clock-in)
  16039. ("O" . org-clock-out)
  16040. ("Meta Data Editing")
  16041. ("t" . org-todo)
  16042. ("0" . (org-priority ?\ ))
  16043. ("1" . (org-priority ?A))
  16044. ("2" . (org-priority ?B))
  16045. ("3" . (org-priority ?C))
  16046. (";" . org-set-tags-command)
  16047. ("e" . org-set-effort)
  16048. ("Agenda Views etc")
  16049. ("v" . org-agenda)
  16050. ("/" . org-sparse-tree)
  16051. ("Misc")
  16052. ("o" . org-open-at-point)
  16053. ("?" . org-speed-command-help)
  16054. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16055. (">" . (org-agenda-remove-restriction-lock))
  16056. )
  16057. "The default speed commands.")
  16058. (defun org-print-speed-command (e)
  16059. (if (> (length (car e)) 1)
  16060. (progn
  16061. (princ "\n")
  16062. (princ (car e))
  16063. (princ "\n")
  16064. (princ (make-string (length (car e)) ?-))
  16065. (princ "\n"))
  16066. (princ (car e))
  16067. (princ " ")
  16068. (if (symbolp (cdr e))
  16069. (princ (symbol-name (cdr e)))
  16070. (prin1 (cdr e)))
  16071. (princ "\n")))
  16072. (defun org-speed-command-help ()
  16073. "Show the available speed commands."
  16074. (interactive)
  16075. (if (not org-use-speed-commands)
  16076. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16077. (with-output-to-temp-buffer "*Help*"
  16078. (princ "User-defined Speed commands\n===========================\n")
  16079. (mapc 'org-print-speed-command org-speed-commands-user)
  16080. (princ "\n")
  16081. (princ "Built-in Speed commands\n=======================\n")
  16082. (mapc 'org-print-speed-command org-speed-commands-default))
  16083. (with-current-buffer "*Help*"
  16084. (setq truncate-lines t))))
  16085. (defun org-speed-move-safe (cmd)
  16086. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16087. If not, return to the original position and throw an error."
  16088. (interactive)
  16089. (let ((pos (point)))
  16090. (call-interactively cmd)
  16091. (unless (and (bolp) (org-at-heading-p))
  16092. (goto-char pos)
  16093. (error "Boundary reached while executing %s" cmd))))
  16094. (defvar org-self-insert-command-undo-counter 0)
  16095. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16096. (defvar org-speed-command nil)
  16097. (defun org-speed-command-default-hook (keys)
  16098. "Hook for activating single-letter speed commands.
  16099. `org-speed-commands-default' specifies a minimal command set.
  16100. Use `org-speed-commands-user' for further customization."
  16101. (when (or (and (bolp) (looking-at org-outline-regexp))
  16102. (and (functionp org-use-speed-commands)
  16103. (funcall org-use-speed-commands)))
  16104. (cdr (assoc keys (append org-speed-commands-user
  16105. org-speed-commands-default)))))
  16106. (defun org-babel-speed-command-hook (keys)
  16107. "Hook for activating single-letter code block commands."
  16108. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16109. (cdr (assoc keys org-babel-key-bindings))))
  16110. (defcustom org-speed-command-hook
  16111. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16112. "Hook for activating speed commands at strategic locations.
  16113. Hook functions are called in sequence until a valid handler is
  16114. found.
  16115. Each hook takes a single argument, a user-pressed command key
  16116. which is also a `self-insert-command' from the global map.
  16117. Within the hook, examine the cursor position and the command key
  16118. and return nil or a valid handler as appropriate. Handler could
  16119. be one of an interactive command, a function, or a form.
  16120. Set `org-use-speed-commands' to non-nil value to enable this
  16121. hook. The default setting is `org-speed-command-default-hook'."
  16122. :group 'org-structure
  16123. :type 'hook)
  16124. (defun org-self-insert-command (N)
  16125. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16126. If the cursor is in a table looking at whitespace, the whitespace is
  16127. overwritten, and the table is not marked as requiring realignment."
  16128. (interactive "p")
  16129. (org-check-before-invisible-edit 'insert)
  16130. (cond
  16131. ((and org-use-speed-commands
  16132. (setq org-speed-command
  16133. (run-hook-with-args-until-success
  16134. 'org-speed-command-hook (this-command-keys))))
  16135. (cond
  16136. ((commandp org-speed-command)
  16137. (setq this-command org-speed-command)
  16138. (call-interactively org-speed-command))
  16139. ((functionp org-speed-command)
  16140. (funcall org-speed-command))
  16141. ((and org-speed-command (listp org-speed-command))
  16142. (eval org-speed-command))
  16143. (t (let (org-use-speed-commands)
  16144. (call-interactively 'org-self-insert-command)))))
  16145. ((and
  16146. (org-table-p)
  16147. (progn
  16148. ;; check if we blank the field, and if that triggers align
  16149. (and (featurep 'org-table) org-table-auto-blank-field
  16150. (member last-command
  16151. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16152. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16153. ;; got extra space, this field does not determine column width
  16154. (let (org-table-may-need-update) (org-table-blank-field))
  16155. ;; no extra space, this field may determine column width
  16156. (org-table-blank-field)))
  16157. t)
  16158. (eq N 1)
  16159. (looking-at "[^|\n]* |"))
  16160. (let (org-table-may-need-update)
  16161. (goto-char (1- (match-end 0)))
  16162. (delete-char -1)
  16163. (goto-char (match-beginning 0))
  16164. (self-insert-command N)))
  16165. (t
  16166. (setq org-table-may-need-update t)
  16167. (self-insert-command N)
  16168. (org-fix-tags-on-the-fly)
  16169. (if org-self-insert-cluster-for-undo
  16170. (if (not (eq last-command 'org-self-insert-command))
  16171. (setq org-self-insert-command-undo-counter 1)
  16172. (if (>= org-self-insert-command-undo-counter 20)
  16173. (setq org-self-insert-command-undo-counter 1)
  16174. (and (> org-self-insert-command-undo-counter 0)
  16175. buffer-undo-list (listp buffer-undo-list)
  16176. (not (cadr buffer-undo-list)) ; remove nil entry
  16177. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16178. (setq org-self-insert-command-undo-counter
  16179. (1+ org-self-insert-command-undo-counter))))))))
  16180. (defun org-check-before-invisible-edit (kind)
  16181. "Check is editing if kind KIND would be dangerous with invisible text around.
  16182. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16183. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16184. (if (and org-catch-invisible-edits
  16185. (or (not (boundp 'visible-mode)) (not visible-mode))
  16186. (or (get-char-property (point) 'invisible)
  16187. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16188. ;; OK, we need to take a closer look
  16189. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16190. (invisible-before-point (if (bobp) nil (get-char-property
  16191. (1- (point)) 'invisible)))
  16192. (border-and-ok-direction
  16193. (or
  16194. ;; Check if we are acting predictably before invisible text
  16195. (and invisible-at-point (not invisible-before-point)
  16196. (memq kind '(insert delete-backward)))
  16197. ;; Check if we are acting predictably after invisible text
  16198. ;; This works not well, and I have turned it off. It seems
  16199. ;; better to always show and stop after invisible text.
  16200. ;; (and (not invisible-at-point) invisible-before-point
  16201. ;; (memq kind '(insert delete)))
  16202. )))
  16203. (when (or (memq invisible-at-point '(outline org-hide-block))
  16204. (memq invisible-before-point '(outline org-hide-block)))
  16205. (if (eq org-catch-invisible-edits 'error)
  16206. (error "Editing in invisible areas is prohibited - make visible first"))
  16207. ;; Make the area visible
  16208. (save-excursion
  16209. (if invisible-before-point
  16210. (goto-char (previous-single-char-property-change
  16211. (point) 'invisible)))
  16212. (org-cycle))
  16213. (cond
  16214. ((eq org-catch-invisible-edits 'show)
  16215. ;; That's it, we do the edit after showing
  16216. (message
  16217. "Unfolding invisible region around point before editing")
  16218. (sit-for 1))
  16219. ((and (eq org-catch-invisible-edits 'smart)
  16220. border-and-ok-direction)
  16221. (message "Unfolding invisible region around point before editing"))
  16222. (t
  16223. ;; Don't do the edit, make the user repeat it in full visibility
  16224. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16225. (defun org-fix-tags-on-the-fly ()
  16226. (when (and (equal (char-after (point-at-bol)) ?*)
  16227. (org-at-heading-p))
  16228. (org-align-tags-here org-tags-column)))
  16229. (defun org-delete-backward-char (N)
  16230. "Like `delete-backward-char', insert whitespace at field end in tables.
  16231. When deleting backwards, in tables this function will insert whitespace in
  16232. front of the next \"|\" separator, to keep the table aligned. The table will
  16233. still be marked for re-alignment if the field did fill the entire column,
  16234. because, in this case the deletion might narrow the column."
  16235. (interactive "p")
  16236. (org-check-before-invisible-edit 'delete-backward)
  16237. (if (and (org-table-p)
  16238. (eq N 1)
  16239. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16240. (looking-at ".*?|"))
  16241. (let ((pos (point))
  16242. (noalign (looking-at "[^|\n\r]* |"))
  16243. (c org-table-may-need-update))
  16244. (backward-delete-char N)
  16245. (if (not overwrite-mode)
  16246. (progn
  16247. (skip-chars-forward "^|")
  16248. (insert " ")
  16249. (goto-char (1- pos))))
  16250. ;; noalign: if there were two spaces at the end, this field
  16251. ;; does not determine the width of the column.
  16252. (if noalign (setq org-table-may-need-update c)))
  16253. (backward-delete-char N)
  16254. (org-fix-tags-on-the-fly)))
  16255. (defun org-delete-char (N)
  16256. "Like `delete-char', but insert whitespace at field end in tables.
  16257. When deleting characters, in tables this function will insert whitespace in
  16258. front of the next \"|\" separator, to keep the table aligned. The table will
  16259. still be marked for re-alignment if the field did fill the entire column,
  16260. because, in this case the deletion might narrow the column."
  16261. (interactive "p")
  16262. (org-check-before-invisible-edit 'delete)
  16263. (if (and (org-table-p)
  16264. (not (bolp))
  16265. (not (= (char-after) ?|))
  16266. (eq N 1))
  16267. (if (looking-at ".*?|")
  16268. (let ((pos (point))
  16269. (noalign (looking-at "[^|\n\r]* |"))
  16270. (c org-table-may-need-update))
  16271. (replace-match (concat
  16272. (substring (match-string 0) 1 -1)
  16273. " |"))
  16274. (goto-char pos)
  16275. ;; noalign: if there were two spaces at the end, this field
  16276. ;; does not determine the width of the column.
  16277. (if noalign (setq org-table-may-need-update c)))
  16278. (delete-char N))
  16279. (delete-char N)
  16280. (org-fix-tags-on-the-fly)))
  16281. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16282. (put 'org-self-insert-command 'delete-selection t)
  16283. (put 'orgtbl-self-insert-command 'delete-selection t)
  16284. (put 'org-delete-char 'delete-selection 'supersede)
  16285. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16286. (put 'org-yank 'delete-selection 'yank)
  16287. ;; Make `flyspell-mode' delay after some commands
  16288. (put 'org-self-insert-command 'flyspell-delayed t)
  16289. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16290. (put 'org-delete-char 'flyspell-delayed t)
  16291. (put 'org-delete-backward-char 'flyspell-delayed t)
  16292. ;; Make pabbrev-mode expand after org-mode commands
  16293. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16294. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16295. ;; How to do this: Measure non-white length of current string
  16296. ;; If equal to column width, we should realign.
  16297. (defun org-remap (map &rest commands)
  16298. "In MAP, remap the functions given in COMMANDS.
  16299. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16300. (let (new old)
  16301. (while commands
  16302. (setq old (pop commands) new (pop commands))
  16303. (if (fboundp 'command-remapping)
  16304. (org-defkey map (vector 'remap old) new)
  16305. (substitute-key-definition old new map global-map)))))
  16306. (when (eq org-enable-table-editor 'optimized)
  16307. ;; If the user wants maximum table support, we need to hijack
  16308. ;; some standard editing functions
  16309. (org-remap org-mode-map
  16310. 'self-insert-command 'org-self-insert-command
  16311. 'delete-char 'org-delete-char
  16312. 'delete-backward-char 'org-delete-backward-char)
  16313. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16314. (defvar org-ctrl-c-ctrl-c-hook nil
  16315. "Hook for functions attaching themselves to `C-c C-c'.
  16316. This can be used to add additional functionality to the C-c C-c
  16317. key which executes context-dependent commands. This hook is run
  16318. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16319. run after the last test.
  16320. Each function will be called with no arguments. The function
  16321. must check if the context is appropriate for it to act. If yes,
  16322. it should do its thing and then return a non-nil value. If the
  16323. context is wrong, just do nothing and return nil.")
  16324. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16325. "Hook for functions attaching themselves to `C-c C-c'.
  16326. This can be used to add additional functionality to the C-c C-c
  16327. key which executes context-dependent commands. This hook is run
  16328. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16329. before the first test.
  16330. Each function will be called with no arguments. The function
  16331. must check if the context is appropriate for it to act. If yes,
  16332. it should do its thing and then return a non-nil value. If the
  16333. context is wrong, just do nothing and return nil.")
  16334. (defvar org-tab-first-hook nil
  16335. "Hook for functions to attach themselves to TAB.
  16336. See `org-ctrl-c-ctrl-c-hook' for more information.
  16337. This hook runs as the first action when TAB is pressed, even before
  16338. `org-cycle' messes around with the `outline-regexp' to cater for
  16339. inline tasks and plain list item folding.
  16340. If any function in this hook returns t, any other actions that
  16341. would have been caused by TAB (such as table field motion or visibility
  16342. cycling) will not occur.")
  16343. (defvar org-tab-after-check-for-table-hook nil
  16344. "Hook for functions to attach themselves to TAB.
  16345. See `org-ctrl-c-ctrl-c-hook' for more information.
  16346. This hook runs after it has been established that the cursor is not in a
  16347. table, but before checking if the cursor is in a headline or if global cycling
  16348. should be done.
  16349. If any function in this hook returns t, not other actions like visibility
  16350. cycling will be done.")
  16351. (defvar org-tab-after-check-for-cycling-hook nil
  16352. "Hook for functions to attach themselves to TAB.
  16353. See `org-ctrl-c-ctrl-c-hook' for more information.
  16354. This hook runs after it has been established that not table field motion and
  16355. not visibility should be done because of current context. This is probably
  16356. the place where a package like yasnippets can hook in.")
  16357. (defvar org-tab-before-tab-emulation-hook nil
  16358. "Hook for functions to attach themselves to TAB.
  16359. See `org-ctrl-c-ctrl-c-hook' for more information.
  16360. This hook runs after every other options for TAB have been exhausted, but
  16361. before indentation and \t insertion takes place.")
  16362. (defvar org-metaleft-hook nil
  16363. "Hook for functions attaching themselves to `M-left'.
  16364. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16365. (defvar org-metaright-hook nil
  16366. "Hook for functions attaching themselves to `M-right'.
  16367. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16368. (defvar org-metaup-hook nil
  16369. "Hook for functions attaching themselves to `M-up'.
  16370. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16371. (defvar org-metadown-hook nil
  16372. "Hook for functions attaching themselves to `M-down'.
  16373. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16374. (defvar org-shiftmetaleft-hook nil
  16375. "Hook for functions attaching themselves to `M-S-left'.
  16376. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16377. (defvar org-shiftmetaright-hook nil
  16378. "Hook for functions attaching themselves to `M-S-right'.
  16379. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16380. (defvar org-shiftmetaup-hook nil
  16381. "Hook for functions attaching themselves to `M-S-up'.
  16382. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16383. (defvar org-shiftmetadown-hook nil
  16384. "Hook for functions attaching themselves to `M-S-down'.
  16385. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16386. (defvar org-metareturn-hook nil
  16387. "Hook for functions attaching themselves to `M-RET'.
  16388. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16389. (defvar org-shiftup-hook nil
  16390. "Hook for functions attaching themselves to `S-up'.
  16391. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16392. (defvar org-shiftup-final-hook nil
  16393. "Hook for functions attaching themselves to `S-up'.
  16394. This one runs after all other options except shift-select have been excluded.
  16395. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16396. (defvar org-shiftdown-hook nil
  16397. "Hook for functions attaching themselves to `S-down'.
  16398. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16399. (defvar org-shiftdown-final-hook nil
  16400. "Hook for functions attaching themselves to `S-down'.
  16401. This one runs after all other options except shift-select have been excluded.
  16402. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16403. (defvar org-shiftleft-hook nil
  16404. "Hook for functions attaching themselves to `S-left'.
  16405. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16406. (defvar org-shiftleft-final-hook nil
  16407. "Hook for functions attaching themselves to `S-left'.
  16408. This one runs after all other options except shift-select have been excluded.
  16409. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16410. (defvar org-shiftright-hook nil
  16411. "Hook for functions attaching themselves to `S-right'.
  16412. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16413. (defvar org-shiftright-final-hook nil
  16414. "Hook for functions attaching themselves to `S-right'.
  16415. This one runs after all other options except shift-select have been excluded.
  16416. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16417. (defun org-modifier-cursor-error ()
  16418. "Throw an error, a modified cursor command was applied in wrong context."
  16419. (error "This command is active in special context like tables, headlines or items"))
  16420. (defun org-shiftselect-error ()
  16421. "Throw an error because Shift-Cursor command was applied in wrong context."
  16422. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16423. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16424. (error "This command works only in special context like headlines or timestamps")))
  16425. (defun org-call-for-shift-select (cmd)
  16426. (let ((this-command-keys-shift-translated t))
  16427. (call-interactively cmd)))
  16428. (defun org-shifttab (&optional arg)
  16429. "Global visibility cycling or move to previous table field.
  16430. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16431. on context.
  16432. See the individual commands for more information."
  16433. (interactive "P")
  16434. (cond
  16435. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16436. ((integerp arg)
  16437. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16438. (message "Content view to level: %d" arg)
  16439. (org-content (prefix-numeric-value arg2))
  16440. (setq org-cycle-global-status 'overview)))
  16441. (t (call-interactively 'org-global-cycle))))
  16442. (defun org-shiftmetaleft ()
  16443. "Promote subtree or delete table column.
  16444. Calls `org-promote-subtree', `org-outdent-item',
  16445. or `org-table-delete-column', depending on context.
  16446. See the individual commands for more information."
  16447. (interactive)
  16448. (cond
  16449. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16450. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16451. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16452. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  16453. (t (org-modifier-cursor-error))))
  16454. (defun org-shiftmetaright ()
  16455. "Demote subtree or insert table column.
  16456. Calls `org-demote-subtree', `org-indent-item',
  16457. or `org-table-insert-column', depending on context.
  16458. See the individual commands for more information."
  16459. (interactive)
  16460. (cond
  16461. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16462. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16463. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16464. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  16465. (t (org-modifier-cursor-error))))
  16466. (defun org-shiftmetaup (&optional arg)
  16467. "Move subtree up or kill table row.
  16468. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16469. `org-move-item-up' depending on context. See the individual commands
  16470. for more information."
  16471. (interactive "P")
  16472. (cond
  16473. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16474. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16475. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16476. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16477. (t (org-modifier-cursor-error))))
  16478. (defun org-shiftmetadown (&optional arg)
  16479. "Move subtree down or insert table row.
  16480. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16481. `org-move-item-down', depending on context. See the individual
  16482. commands for more information."
  16483. (interactive "P")
  16484. (cond
  16485. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16486. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16487. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16488. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16489. (t (org-modifier-cursor-error))))
  16490. (defsubst org-hidden-tree-error ()
  16491. (error
  16492. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16493. (defun org-metaleft (&optional arg)
  16494. "Promote heading or move table column to left.
  16495. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16496. With no specific context, calls the Emacs default `backward-word'.
  16497. See the individual commands for more information."
  16498. (interactive "P")
  16499. (cond
  16500. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16501. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16502. ((org-with-limited-levels
  16503. (or (org-at-heading-p)
  16504. (and (org-region-active-p)
  16505. (save-excursion
  16506. (goto-char (region-beginning))
  16507. (org-at-heading-p)))))
  16508. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16509. (call-interactively 'org-do-promote))
  16510. ;; At an inline task.
  16511. ((org-at-heading-p)
  16512. (call-interactively 'org-inlinetask-promote))
  16513. ((or (org-at-item-p)
  16514. (and (org-region-active-p)
  16515. (save-excursion
  16516. (goto-char (region-beginning))
  16517. (org-at-item-p))))
  16518. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16519. (call-interactively 'org-outdent-item))
  16520. (t (call-interactively 'backward-word))))
  16521. (defun org-metaright (&optional arg)
  16522. "Demote subtree or move table column to right.
  16523. Calls `org-do-demote' or `org-table-move-column', depending on context.
  16524. With no specific context, calls the Emacs default `forward-word'.
  16525. See the individual commands for more information."
  16526. (interactive "P")
  16527. (cond
  16528. ((run-hook-with-args-until-success 'org-metaright-hook))
  16529. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16530. ((org-with-limited-levels
  16531. (or (org-at-heading-p)
  16532. (and (org-region-active-p)
  16533. (save-excursion
  16534. (goto-char (region-beginning))
  16535. (org-at-heading-p)))))
  16536. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16537. (call-interactively 'org-do-demote))
  16538. ;; At an inline task.
  16539. ((org-at-heading-p)
  16540. (call-interactively 'org-inlinetask-demote))
  16541. ((or (org-at-item-p)
  16542. (and (org-region-active-p)
  16543. (save-excursion
  16544. (goto-char (region-beginning))
  16545. (org-at-item-p))))
  16546. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16547. (call-interactively 'org-indent-item))
  16548. (t (call-interactively 'forward-word))))
  16549. (defun org-check-for-hidden (what)
  16550. "Check if there are hidden headlines/items in the current visual line.
  16551. WHAT can be either `headlines' or `items'. If the current line is
  16552. an outline or item heading and it has a folded subtree below it,
  16553. this function returns t, nil otherwise."
  16554. (let ((re (cond
  16555. ((eq what 'headlines) org-outline-regexp-bol)
  16556. ((eq what 'items) (org-item-beginning-re))
  16557. (t (error "This should not happen"))))
  16558. beg end)
  16559. (save-excursion
  16560. (catch 'exit
  16561. (unless (org-region-active-p)
  16562. (setq beg (point-at-bol))
  16563. (beginning-of-line 2)
  16564. (while (and (not (eobp)) ;; this is like `next-line'
  16565. (get-char-property (1- (point)) 'invisible))
  16566. (beginning-of-line 2))
  16567. (setq end (point))
  16568. (goto-char beg)
  16569. (goto-char (point-at-eol))
  16570. (setq end (max end (point)))
  16571. (while (re-search-forward re end t)
  16572. (if (get-char-property (match-beginning 0) 'invisible)
  16573. (throw 'exit t))))
  16574. nil))))
  16575. (defun org-metaup (&optional arg)
  16576. "Move subtree up or move table row up.
  16577. Calls `org-move-subtree-up' or `org-table-move-row' or
  16578. `org-move-item-up', depending on context. See the individual commands
  16579. for more information."
  16580. (interactive "P")
  16581. (cond
  16582. ((run-hook-with-args-until-success 'org-metaup-hook))
  16583. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16584. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16585. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16586. (t (transpose-lines 1) (beginning-of-line -1))))
  16587. (defun org-metadown (&optional arg)
  16588. "Move subtree down or move table row down.
  16589. Calls `org-move-subtree-down' or `org-table-move-row' or
  16590. `org-move-item-down', depending on context. See the individual
  16591. commands for more information."
  16592. (interactive "P")
  16593. (cond
  16594. ((run-hook-with-args-until-success 'org-metadown-hook))
  16595. ((org-at-table-p) (call-interactively 'org-table-move-row))
  16596. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16597. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16598. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  16599. (defun org-shiftup (&optional arg)
  16600. "Increase item in timestamp or increase priority of current headline.
  16601. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  16602. depending on context. See the individual commands for more information."
  16603. (interactive "P")
  16604. (cond
  16605. ((run-hook-with-args-until-success 'org-shiftup-hook))
  16606. ((and org-support-shift-select (org-region-active-p))
  16607. (org-call-for-shift-select 'previous-line))
  16608. ((org-at-timestamp-p t)
  16609. (call-interactively (if org-edit-timestamp-down-means-later
  16610. 'org-timestamp-down 'org-timestamp-up)))
  16611. ((and (not (eq org-support-shift-select 'always))
  16612. org-enable-priority-commands
  16613. (org-at-heading-p))
  16614. (call-interactively 'org-priority-up))
  16615. ((and (not org-support-shift-select) (org-at-item-p))
  16616. (call-interactively 'org-previous-item))
  16617. ((org-clocktable-try-shift 'up arg))
  16618. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  16619. (org-support-shift-select
  16620. (org-call-for-shift-select 'previous-line))
  16621. (t (org-shiftselect-error))))
  16622. (defun org-shiftdown (&optional arg)
  16623. "Decrease item in timestamp or decrease priority of current headline.
  16624. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  16625. depending on context. See the individual commands for more information."
  16626. (interactive "P")
  16627. (cond
  16628. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  16629. ((and org-support-shift-select (org-region-active-p))
  16630. (org-call-for-shift-select 'next-line))
  16631. ((org-at-timestamp-p t)
  16632. (call-interactively (if org-edit-timestamp-down-means-later
  16633. 'org-timestamp-up 'org-timestamp-down)))
  16634. ((and (not (eq org-support-shift-select 'always))
  16635. org-enable-priority-commands
  16636. (org-at-heading-p))
  16637. (call-interactively 'org-priority-down))
  16638. ((and (not org-support-shift-select) (org-at-item-p))
  16639. (call-interactively 'org-next-item))
  16640. ((org-clocktable-try-shift 'down arg))
  16641. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  16642. (org-support-shift-select
  16643. (org-call-for-shift-select 'next-line))
  16644. (t (org-shiftselect-error))))
  16645. (defun org-shiftright (&optional arg)
  16646. "Cycle the thing at point or in the current line, depending on context.
  16647. Depending on context, this does one of the following:
  16648. - switch a timestamp at point one day into the future
  16649. - on a headline, switch to the next TODO keyword.
  16650. - on an item, switch entire list to the next bullet type
  16651. - on a property line, switch to the next allowed value
  16652. - on a clocktable definition line, move time block into the future"
  16653. (interactive "P")
  16654. (cond
  16655. ((run-hook-with-args-until-success 'org-shiftright-hook))
  16656. ((and org-support-shift-select (org-region-active-p))
  16657. (org-call-for-shift-select 'forward-char))
  16658. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  16659. ((and (not (eq org-support-shift-select 'always))
  16660. (org-at-heading-p))
  16661. (let ((org-inhibit-logging
  16662. (not org-treat-S-cursor-todo-selection-as-state-change))
  16663. (org-inhibit-blocking
  16664. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16665. (org-call-with-arg 'org-todo 'right)))
  16666. ((or (and org-support-shift-select
  16667. (not (eq org-support-shift-select 'always))
  16668. (org-at-item-bullet-p))
  16669. (and (not org-support-shift-select) (org-at-item-p)))
  16670. (org-call-with-arg 'org-cycle-list-bullet nil))
  16671. ((and (not (eq org-support-shift-select 'always))
  16672. (org-at-property-p))
  16673. (call-interactively 'org-property-next-allowed-value))
  16674. ((org-clocktable-try-shift 'right arg))
  16675. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  16676. (org-support-shift-select
  16677. (org-call-for-shift-select 'forward-char))
  16678. (t (org-shiftselect-error))))
  16679. (defun org-shiftleft (&optional arg)
  16680. "Cycle the thing at point or in the current line, depending on context.
  16681. Depending on context, this does one of the following:
  16682. - switch a timestamp at point one day into the past
  16683. - on a headline, switch to the previous TODO keyword.
  16684. - on an item, switch entire list to the previous bullet type
  16685. - on a property line, switch to the previous allowed value
  16686. - on a clocktable definition line, move time block into the past"
  16687. (interactive "P")
  16688. (cond
  16689. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16690. ((and org-support-shift-select (org-region-active-p))
  16691. (org-call-for-shift-select 'backward-char))
  16692. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16693. ((and (not (eq org-support-shift-select 'always))
  16694. (org-at-heading-p))
  16695. (let ((org-inhibit-logging
  16696. (not org-treat-S-cursor-todo-selection-as-state-change))
  16697. (org-inhibit-blocking
  16698. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16699. (org-call-with-arg 'org-todo 'left)))
  16700. ((or (and org-support-shift-select
  16701. (not (eq org-support-shift-select 'always))
  16702. (org-at-item-bullet-p))
  16703. (and (not org-support-shift-select) (org-at-item-p)))
  16704. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16705. ((and (not (eq org-support-shift-select 'always))
  16706. (org-at-property-p))
  16707. (call-interactively 'org-property-previous-allowed-value))
  16708. ((org-clocktable-try-shift 'left arg))
  16709. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16710. (org-support-shift-select
  16711. (org-call-for-shift-select 'backward-char))
  16712. (t (org-shiftselect-error))))
  16713. (defun org-shiftcontrolright ()
  16714. "Switch to next TODO set."
  16715. (interactive)
  16716. (cond
  16717. ((and org-support-shift-select (org-region-active-p))
  16718. (org-call-for-shift-select 'forward-word))
  16719. ((and (not (eq org-support-shift-select 'always))
  16720. (org-at-heading-p))
  16721. (org-call-with-arg 'org-todo 'nextset))
  16722. (org-support-shift-select
  16723. (org-call-for-shift-select 'forward-word))
  16724. (t (org-shiftselect-error))))
  16725. (defun org-shiftcontrolleft ()
  16726. "Switch to previous TODO set."
  16727. (interactive)
  16728. (cond
  16729. ((and org-support-shift-select (org-region-active-p))
  16730. (org-call-for-shift-select 'backward-word))
  16731. ((and (not (eq org-support-shift-select 'always))
  16732. (org-at-heading-p))
  16733. (org-call-with-arg 'org-todo 'previousset))
  16734. (org-support-shift-select
  16735. (org-call-for-shift-select 'backward-word))
  16736. (t (org-shiftselect-error))))
  16737. (defun org-shiftcontrolup ()
  16738. "Change timestamps synchronously up in CLOCK log lines."
  16739. (interactive)
  16740. (cond ((and (not org-support-shift-select)
  16741. (org-at-clock-log-p)
  16742. (org-at-timestamp-p t))
  16743. (org-clock-timestamps-up))
  16744. (t (org-shiftselect-error))))
  16745. (defun org-shiftcontroldown ()
  16746. "Change timestamps synchronously down in CLOCK log lines."
  16747. (interactive)
  16748. (cond ((and (not org-support-shift-select)
  16749. (org-at-clock-log-p)
  16750. (org-at-timestamp-p t))
  16751. (org-clock-timestamps-down))
  16752. (t (org-shiftselect-error))))
  16753. (defun org-ctrl-c-ret ()
  16754. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16755. (interactive)
  16756. (cond
  16757. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16758. (t (call-interactively 'org-insert-heading))))
  16759. (defun org-find-visible ()
  16760. (let ((s (point)))
  16761. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16762. (get-char-property s 'invisible)))
  16763. s))
  16764. (defun org-find-invisible ()
  16765. (let ((s (point)))
  16766. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  16767. (not (get-char-property s 'invisible))))
  16768. s))
  16769. (defun org-copy-visible (beg end)
  16770. "Copy the visible parts of the region."
  16771. (interactive "r")
  16772. (let (snippets s)
  16773. (save-excursion
  16774. (save-restriction
  16775. (narrow-to-region beg end)
  16776. (setq s (goto-char (point-min)))
  16777. (while (not (= (point) (point-max)))
  16778. (goto-char (org-find-invisible))
  16779. (push (buffer-substring s (point)) snippets)
  16780. (setq s (goto-char (org-find-visible))))))
  16781. (kill-new (apply 'concat (nreverse snippets)))))
  16782. (defun org-copy-special ()
  16783. "Copy region in table or copy current subtree.
  16784. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16785. See the individual commands for more information."
  16786. (interactive)
  16787. (call-interactively
  16788. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16789. (defun org-cut-special ()
  16790. "Cut region in table or cut current subtree.
  16791. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16792. See the individual commands for more information."
  16793. (interactive)
  16794. (call-interactively
  16795. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16796. (defun org-paste-special (arg)
  16797. "Paste rectangular region into table, or past subtree relative to level.
  16798. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16799. See the individual commands for more information."
  16800. (interactive "P")
  16801. (if (org-at-table-p)
  16802. (org-table-paste-rectangle)
  16803. (org-paste-subtree arg)))
  16804. (defun org-edit-special (&optional arg)
  16805. "Call a special editor for the stuff at point.
  16806. When at a table, call the formula editor with `org-table-edit-formulas'.
  16807. When at the first line of an src example, call `org-edit-src-code'.
  16808. When in an #+include line, visit the include file. Otherwise call
  16809. `ffap' to visit the file at point."
  16810. (interactive)
  16811. ;; possibly prep session before editing source
  16812. (when arg
  16813. (let* ((info (org-babel-get-src-block-info))
  16814. (lang (nth 0 info))
  16815. (params (nth 2 info))
  16816. (session (cdr (assoc :session params))))
  16817. (when (and info session) ;; we are in a source-code block with a session
  16818. (funcall
  16819. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16820. (cond ;; proceed with `org-edit-special'
  16821. ((save-excursion
  16822. (beginning-of-line 1)
  16823. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16824. (find-file (org-trim (match-string 1))))
  16825. ((org-edit-src-code))
  16826. ((org-edit-fixed-width-region))
  16827. ((org-at-table.el-p)
  16828. (org-edit-src-code))
  16829. ((or (org-at-table-p)
  16830. (save-excursion
  16831. (beginning-of-line 1)
  16832. (looking-at "[ \t]*#\\+TBLFM:")))
  16833. (call-interactively 'org-table-edit-formulas))
  16834. (t (call-interactively 'ffap))))
  16835. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  16836. (defun org-ctrl-c-ctrl-c (&optional arg)
  16837. "Set tags in headline, or update according to changed information at point.
  16838. This command does many different things, depending on context:
  16839. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16840. this is what we do.
  16841. - If the cursor is on a statistics cookie, update it.
  16842. - If the cursor is in a headline, prompt for tags and insert them
  16843. into the current line, aligned to `org-tags-column'. When called
  16844. with prefix arg, realign all tags in the current buffer.
  16845. - If the cursor is in one of the special #+KEYWORD lines, this
  16846. triggers scanning the buffer for these lines and updating the
  16847. information.
  16848. - If the cursor is inside a table, realign the table. This command
  16849. works even if the automatic table editor has been turned off.
  16850. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16851. the entire table.
  16852. - If the cursor is at a footnote reference or definition, jump to
  16853. the corresponding definition or references, respectively.
  16854. - If the cursor is a the beginning of a dynamic block, update it.
  16855. - If the current buffer is a capture buffer, close note and file it.
  16856. - If the cursor is on a <<<target>>>, update radio targets and
  16857. corresponding links in this buffer.
  16858. - If the cursor is on a numbered item in a plain list, renumber the
  16859. ordered list.
  16860. - If the cursor is on a checkbox, toggle it.
  16861. - If the cursor is on a code block, evaluate it. The variable
  16862. `org-confirm-babel-evaluate' can be used to control prompting
  16863. before code block evaluation, by default every code block
  16864. evaluation requires confirmation. Code block evaluation can be
  16865. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16866. (interactive "P")
  16867. (let ((org-enable-table-editor t))
  16868. (cond
  16869. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16870. org-occur-highlights
  16871. org-latex-fragment-image-overlays)
  16872. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16873. (org-remove-occur-highlights)
  16874. (org-remove-latex-fragment-image-overlays)
  16875. (message "Temporary highlights/overlays removed from current buffer"))
  16876. ((and (local-variable-p 'org-finish-function (current-buffer))
  16877. (fboundp org-finish-function))
  16878. (funcall org-finish-function))
  16879. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16880. ((org-in-regexp org-ts-regexp-both)
  16881. (org-timestamp-change 0 'day))
  16882. ((or (looking-at org-property-start-re)
  16883. (org-at-property-p))
  16884. (call-interactively 'org-property-action))
  16885. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  16886. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16887. (or (org-at-heading-p) (org-at-item-p)))
  16888. (call-interactively 'org-update-statistics-cookies))
  16889. ((org-at-heading-p) (call-interactively 'org-set-tags))
  16890. ((org-at-table.el-p)
  16891. (message "Use C-c ' to edit table.el tables"))
  16892. ((org-at-table-p)
  16893. (org-table-maybe-eval-formula)
  16894. (if arg
  16895. (call-interactively 'org-table-recalculate)
  16896. (org-table-maybe-recalculate-line))
  16897. (call-interactively 'org-table-align)
  16898. (orgtbl-send-table 'maybe))
  16899. ((or (org-footnote-at-reference-p)
  16900. (org-footnote-at-definition-p))
  16901. (call-interactively 'org-footnote-action))
  16902. ((org-at-item-checkbox-p)
  16903. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16904. ;; list only if at top item.
  16905. (let* ((cbox (match-string 1))
  16906. (struct (org-list-struct))
  16907. (old-struct (copy-tree struct))
  16908. (parents (org-list-parents-alist struct))
  16909. (orderedp (org-entry-get nil "ORDERED"))
  16910. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16911. block-item)
  16912. ;; Use a light version of `org-toggle-checkbox' to avoid
  16913. ;; computing list structure twice.
  16914. (let ((new-box (cond
  16915. ((equal arg '(16)) "[-]")
  16916. ((equal arg '(4)) nil)
  16917. ((equal "[X]" cbox) "[ ]")
  16918. (t "[X]"))))
  16919. (if (and firstp arg)
  16920. ;; If at first item of sub-list, remove check-box from
  16921. ;; every item at the same level.
  16922. (mapc
  16923. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  16924. (org-list-get-all-items
  16925. (point-at-bol) struct (org-list-prevs-alist struct)))
  16926. (org-list-set-checkbox (point-at-bol) struct new-box)))
  16927. ;; Replicate `org-list-write-struct', while grabbing a return
  16928. ;; value from `org-list-struct-fix-box'.
  16929. (org-list-struct-fix-ind struct parents 2)
  16930. (org-list-struct-fix-item-end struct)
  16931. (let ((prevs (org-list-prevs-alist struct)))
  16932. (org-list-struct-fix-bul struct prevs)
  16933. (org-list-struct-fix-ind struct parents)
  16934. (setq block-item
  16935. (org-list-struct-fix-box struct parents prevs orderedp)))
  16936. (org-list-struct-apply-struct struct old-struct)
  16937. (org-update-checkbox-count-maybe)
  16938. (when block-item
  16939. (message
  16940. "Checkboxes were removed due to unchecked box at line %d"
  16941. (org-current-line block-item)))
  16942. (when firstp (org-list-send-list 'maybe))))
  16943. ((org-at-item-p)
  16944. ;; Cursor at an item: repair list. Do checkbox related actions
  16945. ;; only if function was called with an argument. Send list only
  16946. ;; if at top item.
  16947. (let* ((struct (org-list-struct))
  16948. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16949. old-struct)
  16950. (when arg
  16951. (setq old-struct (copy-tree struct))
  16952. (if firstp
  16953. ;; If at first item of sub-list, add check-box to every
  16954. ;; item at the same level.
  16955. (mapc
  16956. (lambda (pos)
  16957. (unless (org-list-get-checkbox pos struct)
  16958. (org-list-set-checkbox pos struct "[ ]")))
  16959. (org-list-get-all-items
  16960. (point-at-bol) struct (org-list-prevs-alist struct)))
  16961. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  16962. (org-list-write-struct
  16963. struct (org-list-parents-alist struct) old-struct)
  16964. (when arg (org-update-checkbox-count-maybe))
  16965. (when firstp (org-list-send-list 'maybe))))
  16966. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16967. ;; Dynamic block
  16968. (beginning-of-line 1)
  16969. (save-excursion (org-update-dblock)))
  16970. ((save-excursion
  16971. (beginning-of-line 1)
  16972. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16973. (cond
  16974. ((equal (match-string 1) "TBLFM")
  16975. ;; Recalculate the table before this line
  16976. (save-excursion
  16977. (beginning-of-line 1)
  16978. (skip-chars-backward " \r\n\t")
  16979. (if (org-at-table-p)
  16980. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16981. (t
  16982. (let ((org-inhibit-startup-visibility-stuff t)
  16983. (org-startup-align-all-tables nil))
  16984. (when (boundp 'org-table-coordinate-overlays)
  16985. (mapc 'delete-overlay org-table-coordinate-overlays)
  16986. (setq org-table-coordinate-overlays nil))
  16987. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16988. (message "Local setup has been refreshed"))))
  16989. ((org-clock-update-time-maybe))
  16990. (t
  16991. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  16992. (error "C-c C-c can do nothing useful at this location"))))))
  16993. (defun org-mode-restart ()
  16994. "Restart Org-mode, to scan again for special lines.
  16995. Also updates the keyword regular expressions."
  16996. (interactive)
  16997. (org-mode)
  16998. (message "Org-mode restarted"))
  16999. (defun org-kill-note-or-show-branches ()
  17000. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17001. (interactive)
  17002. (if (not org-finish-function)
  17003. (progn
  17004. (hide-subtree)
  17005. (call-interactively 'show-branches))
  17006. (let ((org-note-abort t))
  17007. (funcall org-finish-function))))
  17008. (defun org-return (&optional indent)
  17009. "Goto next table row or insert a newline.
  17010. Calls `org-table-next-row' or `newline', depending on context.
  17011. See the individual commands for more information."
  17012. (interactive)
  17013. (cond
  17014. ((or (bobp) (org-in-src-block-p))
  17015. (if indent (newline-and-indent) (newline)))
  17016. ((org-at-table-p)
  17017. (org-table-justify-field-maybe)
  17018. (call-interactively 'org-table-next-row))
  17019. ;; when `newline-and-indent' is called within a list, make sure
  17020. ;; text moved stays inside the item.
  17021. ((and (org-in-item-p) indent)
  17022. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17023. (progn
  17024. (save-match-data (newline))
  17025. (org-indent-line-to (length (match-string 0))))
  17026. (let ((ind (org-get-indentation)))
  17027. (newline)
  17028. (if (org-looking-back org-list-end-re)
  17029. (org-indent-line-function)
  17030. (org-indent-line-to ind)))))
  17031. ((and org-return-follows-link
  17032. (let ((tprop (get-text-property (point) 'face)))
  17033. (or (eq tprop 'org-link)
  17034. (and (listp tprop) (memq 'org-link tprop)))))
  17035. (call-interactively 'org-open-at-point))
  17036. ((and (org-at-heading-p)
  17037. (looking-at
  17038. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17039. (org-show-entry)
  17040. (end-of-line 1)
  17041. (newline))
  17042. (t (if indent (newline-and-indent) (newline)))))
  17043. (defun org-return-indent ()
  17044. "Goto next table row or insert a newline and indent.
  17045. Calls `org-table-next-row' or `newline-and-indent', depending on
  17046. context. See the individual commands for more information."
  17047. (interactive)
  17048. (org-return t))
  17049. (defun org-ctrl-c-star ()
  17050. "Compute table, or change heading status of lines.
  17051. Calls `org-table-recalculate' or `org-toggle-heading',
  17052. depending on context."
  17053. (interactive)
  17054. (cond
  17055. ((org-at-table-p)
  17056. (call-interactively 'org-table-recalculate))
  17057. (t
  17058. ;; Convert all lines in region to list items
  17059. (call-interactively 'org-toggle-heading))))
  17060. (defun org-ctrl-c-minus ()
  17061. "Insert separator line in table or modify bullet status of line.
  17062. Also turns a plain line or a region of lines into list items.
  17063. Calls `org-table-insert-hline', `org-toggle-item', or
  17064. `org-cycle-list-bullet', depending on context."
  17065. (interactive)
  17066. (cond
  17067. ((org-at-table-p)
  17068. (call-interactively 'org-table-insert-hline))
  17069. ((org-region-active-p)
  17070. (call-interactively 'org-toggle-item))
  17071. ((org-in-item-p)
  17072. (call-interactively 'org-cycle-list-bullet))
  17073. (t
  17074. (call-interactively 'org-toggle-item))))
  17075. (defun org-toggle-item (arg)
  17076. "Convert headings or normal lines to items, items to normal lines.
  17077. If there is no active region, only the current line is considered.
  17078. If the first non blank line in the region is an headline, convert
  17079. all headlines to items, shifting text accordingly.
  17080. If it is an item, convert all items to normal lines.
  17081. If it is normal text, change region into an item. With a prefix
  17082. argument ARG, change each line in region into an item."
  17083. (interactive "P")
  17084. (let ((shift-text
  17085. (function
  17086. ;; Shift text in current section to IND, from point to END.
  17087. ;; The function leaves point to END line.
  17088. (lambda (ind end)
  17089. (let ((min-i 1000) (end (copy-marker end)))
  17090. ;; First determine the minimum indentation (MIN-I) of
  17091. ;; the text.
  17092. (save-excursion
  17093. (catch 'exit
  17094. (while (< (point) end)
  17095. (let ((i (org-get-indentation)))
  17096. (cond
  17097. ;; Skip blank lines and inline tasks.
  17098. ((looking-at "^[ \t]*$"))
  17099. ((looking-at org-outline-regexp-bol))
  17100. ;; We can't find less than 0 indentation.
  17101. ((zerop i) (throw 'exit (setq min-i 0)))
  17102. ((< i min-i) (setq min-i i))))
  17103. (forward-line))))
  17104. ;; Then indent each line so that a line indented to
  17105. ;; MIN-I becomes indented to IND. Ignore blank lines
  17106. ;; and inline tasks in the process.
  17107. (let ((delta (- ind min-i)))
  17108. (while (< (point) end)
  17109. (unless (or (looking-at "^[ \t]*$")
  17110. (looking-at org-outline-regexp-bol))
  17111. (org-indent-line-to (+ (org-get-indentation) delta)))
  17112. (forward-line)))))))
  17113. (skip-blanks
  17114. (function
  17115. ;; Return beginning of first non-blank line, starting from
  17116. ;; line at POS.
  17117. (lambda (pos)
  17118. (save-excursion
  17119. (goto-char pos)
  17120. (skip-chars-forward " \r\t\n")
  17121. (point-at-bol)))))
  17122. beg end)
  17123. ;; Determine boundaries of changes.
  17124. (if (org-region-active-p)
  17125. (setq beg (funcall skip-blanks (region-beginning))
  17126. end (copy-marker (region-end)))
  17127. (setq beg (funcall skip-blanks (point-at-bol))
  17128. end (copy-marker (point-at-eol))))
  17129. ;; Depending on the starting line, choose an action on the text
  17130. ;; between BEG and END.
  17131. (org-with-limited-levels
  17132. (save-excursion
  17133. (goto-char beg)
  17134. (cond
  17135. ;; Case 1. Start at an item: de-itemize. Note that it only
  17136. ;; happens when a region is active: `org-ctrl-c-minus'
  17137. ;; would call `org-cycle-list-bullet' otherwise.
  17138. ((org-at-item-p)
  17139. (while (< (point) end)
  17140. (when (org-at-item-p)
  17141. (skip-chars-forward " \t")
  17142. (delete-region (point) (match-end 0)))
  17143. (forward-line)))
  17144. ;; Case 2. Start at an heading: convert to items.
  17145. ((org-at-heading-p)
  17146. (let* ((bul (org-list-bullet-string "-"))
  17147. (bul-len (length bul))
  17148. ;; Indentation of the first heading. It should be
  17149. ;; relative to the indentation of its parent, if any.
  17150. (start-ind (save-excursion
  17151. (cond
  17152. ((not org-adapt-indentation) 0)
  17153. ((not (outline-previous-heading)) 0)
  17154. (t (length (match-string 0))))))
  17155. ;; Level of first heading. Further headings will be
  17156. ;; compared to it to determine hierarchy in the list.
  17157. (ref-level (org-reduced-level (org-outline-level))))
  17158. (while (< (point) end)
  17159. (let* ((level (org-reduced-level (org-outline-level)))
  17160. (delta (max 0 (- level ref-level))))
  17161. ;; If current headline is less indented than the first
  17162. ;; one, set it as reference, in order to preserve
  17163. ;; subtrees.
  17164. (when (< level ref-level) (setq ref-level level))
  17165. (replace-match bul t t)
  17166. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17167. ;; Ensure all text down to END (or SECTION-END) belongs
  17168. ;; to the newly created item.
  17169. (let ((section-end (save-excursion
  17170. (or (outline-next-heading) (point)))))
  17171. (forward-line)
  17172. (funcall shift-text
  17173. (+ start-ind (* (1+ delta) bul-len))
  17174. (min end section-end)))))))
  17175. ;; Case 3. Normal line with ARG: turn each non-item line into
  17176. ;; an item.
  17177. (arg
  17178. (while (< (point) end)
  17179. (unless (or (org-at-heading-p) (org-at-item-p))
  17180. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17181. (replace-match
  17182. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17183. (forward-line)))
  17184. ;; Case 4. Normal line without ARG: make the first line of
  17185. ;; region an item, and shift indentation of others
  17186. ;; lines to set them as item's body.
  17187. (t (let* ((bul (org-list-bullet-string "-"))
  17188. (bul-len (length bul))
  17189. (ref-ind (org-get-indentation)))
  17190. (skip-chars-forward " \t")
  17191. (insert bul)
  17192. (forward-line)
  17193. (while (< (point) end)
  17194. ;; Ensure that lines less indented than first one
  17195. ;; still get included in item body.
  17196. (funcall shift-text
  17197. (+ ref-ind bul-len)
  17198. (min end (save-excursion (or (outline-next-heading)
  17199. (point)))))
  17200. (forward-line)))))))))
  17201. (defun org-toggle-heading (&optional nstars)
  17202. "Convert headings to normal text, or items or text to headings.
  17203. If there is no active region, only the current line is considered.
  17204. If the first non blank line is an headline, remove the stars from
  17205. all headlines in the region.
  17206. If it is a plain list item, turn all plain list items into headings.
  17207. If it is a normal line, turn each and every normal line (i.e. not
  17208. an heading or an item) in the region into a heading.
  17209. When converting a line into a heading, the number of stars is chosen
  17210. such that the lines become children of the current entry. However,
  17211. when a prefix argument is given, its value determines the number of
  17212. stars to add."
  17213. (interactive "P")
  17214. (let ((skip-blanks
  17215. (function
  17216. ;; Return beginning of first non-blank line, starting from
  17217. ;; line at POS.
  17218. (lambda (pos)
  17219. (save-excursion
  17220. (goto-char pos)
  17221. (skip-chars-forward " \r\t\n")
  17222. (point-at-bol)))))
  17223. beg end)
  17224. ;; Determine boundaries of changes. If region ends at a bol, do
  17225. ;; not consider the last line to be in the region.
  17226. (if (org-region-active-p)
  17227. (setq beg (funcall skip-blanks (region-beginning))
  17228. end (copy-marker (save-excursion
  17229. (goto-char (region-end))
  17230. (if (bolp) (point) (point-at-eol)))))
  17231. (setq beg (funcall skip-blanks (point-at-bol))
  17232. end (copy-marker (point-at-eol))))
  17233. ;; Ensure inline tasks don't count as headings.
  17234. (org-with-limited-levels
  17235. (save-excursion
  17236. (goto-char beg)
  17237. (cond
  17238. ;; Case 1. Started at an heading: de-star headings.
  17239. ((org-at-heading-p)
  17240. (while (< (point) end)
  17241. (when (org-at-heading-p t)
  17242. (looking-at org-outline-regexp) (replace-match ""))
  17243. (forward-line)))
  17244. ;; Case 2. Started at an item: change items into headlines.
  17245. ;; One star will be added by `org-list-to-subtree'.
  17246. ((org-at-item-p)
  17247. (let* ((stars (make-string
  17248. (if nstars
  17249. ;; subtract the star that will be added again by
  17250. ;; `org-list-to-subtree'
  17251. (1- (prefix-numeric-value current-prefix-arg))
  17252. (or (org-current-level) 0))
  17253. ?*))
  17254. (add-stars
  17255. (cond (nstars "") ; stars from prefix only
  17256. ((equal stars "") "") ; before first heading
  17257. (org-odd-levels-only "*") ; inside heading, odd
  17258. (t "")))) ; inside heading, oddeven
  17259. (while (< (point) end)
  17260. (when (org-at-item-p)
  17261. ;; Pay attention to cases when region ends before list.
  17262. (let* ((struct (org-list-struct))
  17263. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17264. (save-restriction
  17265. (narrow-to-region (point) list-end)
  17266. (insert
  17267. (org-list-to-subtree
  17268. (org-list-parse-list t)
  17269. '(:istart (concat stars add-stars (funcall get-stars depth))
  17270. :icount (concat stars add-stars (funcall get-stars depth))))))))
  17271. (forward-line))))
  17272. ;; Case 3. Started at normal text: make every line an heading,
  17273. ;; skipping headlines and items.
  17274. (t (let* ((stars (make-string
  17275. (if nstars
  17276. (prefix-numeric-value current-prefix-arg)
  17277. (or (org-current-level) 0))
  17278. ?*))
  17279. (add-stars
  17280. (cond (nstars "") ; stars from prefix only
  17281. ((equal stars "") "*") ; before first heading
  17282. (org-odd-levels-only "**") ; inside heading, odd
  17283. (t "*"))) ; inside heading, oddeven
  17284. (rpl (concat stars add-stars " ")))
  17285. (while (< (point) end)
  17286. (when (and (not (org-at-heading-p)) (not (org-at-item-p))
  17287. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17288. (replace-match (concat rpl (match-string 2))))
  17289. (forward-line)))))))))
  17290. (defun org-meta-return (&optional arg)
  17291. "Insert a new heading or wrap a region in a table.
  17292. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17293. See the individual commands for more information."
  17294. (interactive "P")
  17295. (cond
  17296. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17297. ((or (org-at-drawer-p) (org-at-property-p))
  17298. (newline-and-indent))
  17299. ((org-at-table-p)
  17300. (call-interactively 'org-table-wrap-region))
  17301. (t (call-interactively 'org-insert-heading))))
  17302. ;;; Menu entries
  17303. ;; Define the Org-mode menus
  17304. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17305. '("Tbl"
  17306. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17307. ["Next Field" org-cycle (org-at-table-p)]
  17308. ["Previous Field" org-shifttab (org-at-table-p)]
  17309. ["Next Row" org-return (org-at-table-p)]
  17310. "--"
  17311. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17312. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17313. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17314. "--"
  17315. ("Column"
  17316. ["Move Column Left" org-metaleft (org-at-table-p)]
  17317. ["Move Column Right" org-metaright (org-at-table-p)]
  17318. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17319. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17320. ("Row"
  17321. ["Move Row Up" org-metaup (org-at-table-p)]
  17322. ["Move Row Down" org-metadown (org-at-table-p)]
  17323. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17324. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17325. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17326. "--"
  17327. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17328. ("Rectangle"
  17329. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17330. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17331. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17332. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17333. "--"
  17334. ("Calculate"
  17335. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17336. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17337. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17338. "--"
  17339. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17340. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17341. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17342. "--"
  17343. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17344. "--"
  17345. ["Sum Column/Rectangle" org-table-sum
  17346. (or (org-at-table-p) (org-region-active-p))]
  17347. ["Which Column?" org-table-current-column (org-at-table-p)])
  17348. ["Debug Formulas"
  17349. org-table-toggle-formula-debugger
  17350. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17351. ["Show Col/Row Numbers"
  17352. org-table-toggle-coordinate-overlays
  17353. :style toggle
  17354. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17355. "--"
  17356. ["Create" org-table-create (and (not (org-at-table-p))
  17357. org-enable-table-editor)]
  17358. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17359. ["Import from File" org-table-import (not (org-at-table-p))]
  17360. ["Export to File" org-table-export (org-at-table-p)]
  17361. "--"
  17362. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17363. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17364. '("Org"
  17365. ("Show/Hide"
  17366. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17367. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17368. ["Sparse Tree..." org-sparse-tree t]
  17369. ["Reveal Context" org-reveal t]
  17370. ["Show All" show-all t]
  17371. "--"
  17372. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17373. "--"
  17374. ["New Heading" org-insert-heading t]
  17375. ("Navigate Headings"
  17376. ["Up" outline-up-heading t]
  17377. ["Next" outline-next-visible-heading t]
  17378. ["Previous" outline-previous-visible-heading t]
  17379. ["Next Same Level" outline-forward-same-level t]
  17380. ["Previous Same Level" outline-backward-same-level t]
  17381. "--"
  17382. ["Jump" org-goto t])
  17383. ("Edit Structure"
  17384. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17385. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17386. "--"
  17387. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17388. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17389. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17390. "--"
  17391. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17392. "--"
  17393. ["Copy visible text" org-copy-visible t]
  17394. "--"
  17395. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17396. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17397. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17398. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17399. "--"
  17400. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17401. "--"
  17402. ["Convert to odd levels" org-convert-to-odd-levels t]
  17403. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17404. ("Editing"
  17405. ["Emphasis..." org-emphasize t]
  17406. ["Edit Source Example" org-edit-special t]
  17407. "--"
  17408. ["Footnote new/jump" org-footnote-action t]
  17409. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17410. ("Archive"
  17411. ["Archive (default method)" org-archive-subtree-default t]
  17412. "--"
  17413. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17414. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17415. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17416. )
  17417. "--"
  17418. ("Hyperlinks"
  17419. ["Store Link (Global)" org-store-link t]
  17420. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17421. ["Insert Link" org-insert-link t]
  17422. ["Follow Link" org-open-at-point t]
  17423. "--"
  17424. ["Next link" org-next-link t]
  17425. ["Previous link" org-previous-link t]
  17426. "--"
  17427. ["Descriptive Links"
  17428. org-toggle-link-display
  17429. :style radio
  17430. :selected org-descriptive-links
  17431. ]
  17432. ["Literal Links"
  17433. org-toggle-link-display
  17434. :style radio
  17435. :selected (not org-descriptive-links)])
  17436. "--"
  17437. ("TODO Lists"
  17438. ["TODO/DONE/-" org-todo t]
  17439. ("Select keyword"
  17440. ["Next keyword" org-shiftright (org-at-heading-p)]
  17441. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17442. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17443. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17444. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17445. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17446. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17447. "--"
  17448. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17449. :selected org-enforce-todo-dependencies :style toggle :active t]
  17450. "Settings for tree at point"
  17451. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17452. :selected (org-entry-get nil "ORDERED")
  17453. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17454. ["Do Children parallel" org-toggle-ordered-property :style radio
  17455. :selected (not (org-entry-get nil "ORDERED"))
  17456. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17457. "--"
  17458. ["Set Priority" org-priority t]
  17459. ["Priority Up" org-shiftup t]
  17460. ["Priority Down" org-shiftdown t]
  17461. "--"
  17462. ["Get news from all feeds" org-feed-update-all t]
  17463. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17464. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17465. ("TAGS and Properties"
  17466. ["Set Tags" org-set-tags-command t]
  17467. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17468. "--"
  17469. ["Set property" org-set-property t]
  17470. ["Column view of properties" org-columns t]
  17471. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17472. ("Dates and Scheduling"
  17473. ["Timestamp" org-time-stamp t]
  17474. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17475. ("Change Date"
  17476. ["1 Day Later" org-shiftright t]
  17477. ["1 Day Earlier" org-shiftleft t]
  17478. ["1 ... Later" org-shiftup t]
  17479. ["1 ... Earlier" org-shiftdown t])
  17480. ["Compute Time Range" org-evaluate-time-range t]
  17481. ["Schedule Item" org-schedule t]
  17482. ["Deadline" org-deadline t]
  17483. "--"
  17484. ["Custom time format" org-toggle-time-stamp-overlays
  17485. :style radio :selected org-display-custom-times]
  17486. "--"
  17487. ["Goto Calendar" org-goto-calendar t]
  17488. ["Date from Calendar" org-date-from-calendar t]
  17489. "--"
  17490. ["Start/Restart Timer" org-timer-start t]
  17491. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17492. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17493. ["Insert Timer String" org-timer t]
  17494. ["Insert Timer Item" org-timer-item t])
  17495. ("Logging work"
  17496. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17497. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17498. ["Clock out" org-clock-out t]
  17499. ["Clock cancel" org-clock-cancel t]
  17500. "--"
  17501. ["Mark as default task" org-clock-mark-default-task t]
  17502. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17503. ["Goto running clock" org-clock-goto t]
  17504. "--"
  17505. ["Display times" org-clock-display t]
  17506. ["Create clock table" org-clock-report t]
  17507. "--"
  17508. ["Record DONE time"
  17509. (progn (setq org-log-done (not org-log-done))
  17510. (message "Switching to %s will %s record a timestamp"
  17511. (car org-done-keywords)
  17512. (if org-log-done "automatically" "not")))
  17513. :style toggle :selected org-log-done])
  17514. "--"
  17515. ["Agenda Command..." org-agenda t]
  17516. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17517. ("File List for Agenda")
  17518. ("Special views current file"
  17519. ["TODO Tree" org-show-todo-tree t]
  17520. ["Check Deadlines" org-check-deadlines t]
  17521. ["Timeline" org-timeline t]
  17522. ["Tags/Property tree" org-match-sparse-tree t])
  17523. "--"
  17524. ["Export/Publish..." org-export t]
  17525. ("LaTeX"
  17526. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17527. :selected org-cdlatex-mode]
  17528. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17529. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17530. ["Modify math symbol" org-cdlatex-math-modify
  17531. (org-inside-LaTeX-fragment-p)]
  17532. ["Insert citation" org-reftex-citation t]
  17533. "--"
  17534. ["Template for BEAMER" org-insert-beamer-options-template t])
  17535. "--"
  17536. ("MobileOrg"
  17537. ["Push Files and Views" org-mobile-push t]
  17538. ["Get Captured and Flagged" org-mobile-pull t]
  17539. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17540. "--"
  17541. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17542. "--"
  17543. ("Documentation"
  17544. ["Show Version" org-version t]
  17545. ["Info Documentation" org-info t])
  17546. ("Customize"
  17547. ["Browse Org Group" org-customize t]
  17548. "--"
  17549. ["Expand This Menu" org-create-customize-menu
  17550. (fboundp 'customize-menu-create)])
  17551. ["Send bug report" org-submit-bug-report t]
  17552. "--"
  17553. ("Refresh/Reload"
  17554. ["Refresh setup current buffer" org-mode-restart t]
  17555. ["Reload Org (after update)" org-reload t]
  17556. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17557. ))
  17558. (defun org-info (&optional node)
  17559. "Read documentation for Org-mode in the info system.
  17560. With optional NODE, go directly to that node."
  17561. (interactive)
  17562. (info (format "(org)%s" (or node ""))))
  17563. ;;;###autoload
  17564. (defun org-submit-bug-report ()
  17565. "Submit a bug report on Org-mode via mail.
  17566. Don't hesitate to report any problems or inaccurate documentation.
  17567. If you don't have setup sending mail from (X)Emacs, please copy the
  17568. output buffer into your mail program, as it gives us important
  17569. information about your Org-mode version and configuration."
  17570. (interactive)
  17571. (require 'reporter)
  17572. (org-load-modules-maybe)
  17573. (org-require-autoloaded-modules)
  17574. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  17575. (reporter-submit-bug-report
  17576. "emacs-orgmode@gnu.org"
  17577. (org-version)
  17578. (let (list)
  17579. (save-window-excursion
  17580. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  17581. (delete-other-windows)
  17582. (erase-buffer)
  17583. (insert "You are about to submit a bug report to the Org-mode mailing list.
  17584. We would like to add your full Org-mode and Outline configuration to the
  17585. bug report. This greatly simplifies the work of the maintainer and
  17586. other experts on the mailing list.
  17587. HOWEVER, some variables you have customized may contain private
  17588. information. The names of customers, colleagues, or friends, might
  17589. appear in the form of file names, tags, todo states, or search strings.
  17590. If you answer yes to the prompt, you might want to check and remove
  17591. such private information before sending the email.")
  17592. (add-text-properties (point-min) (point-max) '(face org-warning))
  17593. (when (yes-or-no-p "Include your Org-mode configuration ")
  17594. (mapatoms
  17595. (lambda (v)
  17596. (and (boundp v)
  17597. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  17598. (or (and (symbol-value v)
  17599. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  17600. (and
  17601. (get v 'custom-type) (get v 'standard-value)
  17602. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  17603. (push v list)))))
  17604. (kill-buffer (get-buffer "*Warn about privacy*"))
  17605. list))
  17606. nil nil
  17607. "Remember to cover the basics, that is, what you expected to happen and
  17608. what in fact did happen. You don't know how to make a good report? See
  17609. http://orgmode.org/manual/Feedback.html#Feedback
  17610. Your bug report will be posted to the Org-mode mailing list.
  17611. ------------------------------------------------------------------------")
  17612. (save-excursion
  17613. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  17614. (replace-match "\\1Bug: \\3 [\\2]")))))
  17615. (defun org-install-agenda-files-menu ()
  17616. (let ((bl (buffer-list)))
  17617. (save-excursion
  17618. (while bl
  17619. (set-buffer (pop bl))
  17620. (if (eq major-mode 'org-mode) (setq bl nil)))
  17621. (when (eq major-mode 'org-mode)
  17622. (easy-menu-change
  17623. '("Org") "File List for Agenda"
  17624. (append
  17625. (list
  17626. ["Edit File List" (org-edit-agenda-file-list) t]
  17627. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  17628. ["Remove Current File from List" org-remove-file t]
  17629. ["Cycle through agenda files" org-cycle-agenda-files t]
  17630. ["Occur in all agenda files" org-occur-in-agenda-files t]
  17631. "--")
  17632. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  17633. ;;;; Documentation
  17634. ;;;###autoload
  17635. (defun org-require-autoloaded-modules ()
  17636. (interactive)
  17637. (mapc 'require
  17638. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  17639. org-docbook org-exp org-html org-icalendar
  17640. org-id org-latex
  17641. org-publish org-remember org-table
  17642. org-timer org-xoxo)))
  17643. ;;;###autoload
  17644. (defun org-reload (&optional uncompiled)
  17645. "Reload all org lisp files.
  17646. With prefix arg UNCOMPILED, load the uncompiled versions."
  17647. (interactive "P")
  17648. (require 'find-func)
  17649. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  17650. (dir-org (file-name-directory (org-find-library-name "org")))
  17651. (dir-org-contrib (ignore-errors
  17652. (file-name-directory
  17653. (org-find-library-name "org-contribdir"))))
  17654. (babel-files
  17655. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  17656. (append (list nil "comint" "eval" "exp" "keys"
  17657. "lob" "ref" "table" "tangle")
  17658. (delq nil
  17659. (mapcar
  17660. (lambda (lang)
  17661. (when (cdr lang) (symbol-name (car lang))))
  17662. org-babel-load-languages)))))
  17663. (files
  17664. (append (directory-files dir-org t file-re)
  17665. babel-files
  17666. (and dir-org-contrib
  17667. (directory-files dir-org-contrib t file-re))))
  17668. (remove-re (concat (if (featurep 'xemacs)
  17669. "org-colview" "org-colview-xemacs")
  17670. "\\'")))
  17671. (setq files (mapcar 'file-name-sans-extension files))
  17672. (setq files (mapcar
  17673. (lambda (x) (if (string-match remove-re x) nil x))
  17674. files))
  17675. (setq files (delq nil files))
  17676. (mapc
  17677. (lambda (f)
  17678. (when (featurep (intern (file-name-nondirectory f)))
  17679. (if (and (not uncompiled)
  17680. (file-exists-p (concat f ".elc")))
  17681. (load (concat f ".elc") nil nil t)
  17682. (load (concat f ".el") nil nil t))))
  17683. files))
  17684. (org-version))
  17685. ;;;###autoload
  17686. (defun org-customize ()
  17687. "Call the customize function with org as argument."
  17688. (interactive)
  17689. (org-load-modules-maybe)
  17690. (org-require-autoloaded-modules)
  17691. (customize-browse 'org))
  17692. (defun org-create-customize-menu ()
  17693. "Create a full customization menu for Org-mode, insert it into the menu."
  17694. (interactive)
  17695. (org-load-modules-maybe)
  17696. (org-require-autoloaded-modules)
  17697. (if (fboundp 'customize-menu-create)
  17698. (progn
  17699. (easy-menu-change
  17700. '("Org") "Customize"
  17701. `(["Browse Org group" org-customize t]
  17702. "--"
  17703. ,(customize-menu-create 'org)
  17704. ["Set" Custom-set t]
  17705. ["Save" Custom-save t]
  17706. ["Reset to Current" Custom-reset-current t]
  17707. ["Reset to Saved" Custom-reset-saved t]
  17708. ["Reset to Standard Settings" Custom-reset-standard t]))
  17709. (message "\"Org\"-menu now contains full customization menu"))
  17710. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  17711. ;;;; Miscellaneous stuff
  17712. ;;; Generally useful functions
  17713. (defun org-get-at-bol (property)
  17714. "Get text property PROPERTY at beginning of line."
  17715. (get-text-property (point-at-bol) property))
  17716. (defun org-find-text-property-in-string (prop s)
  17717. "Return the first non-nil value of property PROP in string S."
  17718. (or (get-text-property 0 prop s)
  17719. (get-text-property (or (next-single-property-change 0 prop s) 0)
  17720. prop s)))
  17721. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  17722. "Display the given MESSAGE as a warning."
  17723. (if (fboundp 'display-warning)
  17724. (display-warning 'org message
  17725. (if (featurep 'xemacs) 'warning :warning))
  17726. (let ((buf (get-buffer-create "*Org warnings*")))
  17727. (with-current-buffer buf
  17728. (goto-char (point-max))
  17729. (insert "Warning (Org): " message)
  17730. (unless (bolp)
  17731. (newline)))
  17732. (display-buffer buf)
  17733. (sit-for 0))))
  17734. (defun org-eval (form)
  17735. "Eval FORM and return result."
  17736. (condition-case error
  17737. (eval form)
  17738. (error (format "%%![Error: %s]" error))))
  17739. (defun org-in-commented-line ()
  17740. "Is point in a line starting with `#'?"
  17741. (equal (char-after (point-at-bol)) ?#))
  17742. (defun org-in-indented-comment-line ()
  17743. "Is point in a line starting with `#' after some white space?"
  17744. (save-excursion
  17745. (save-match-data
  17746. (goto-char (point-at-bol))
  17747. (looking-at "[ \t]*#"))))
  17748. (defun org-in-verbatim-emphasis ()
  17749. (save-match-data
  17750. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17751. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17752. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17753. (if (and marker (marker-buffer marker)
  17754. (buffer-live-p (marker-buffer marker)))
  17755. (progn
  17756. (org-pop-to-buffer-same-window (marker-buffer marker))
  17757. (if (or (> marker (point-max)) (< marker (point-min)))
  17758. (widen))
  17759. (goto-char marker)
  17760. (org-show-context 'org-goto))
  17761. (if bookmark
  17762. (bookmark-jump bookmark)
  17763. (error "Cannot find location"))))
  17764. (defun org-quote-csv-field (s)
  17765. "Quote field for inclusion in CSV material."
  17766. (if (string-match "[\",]" s)
  17767. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17768. s))
  17769. (defun org-force-self-insert (N)
  17770. "Needed to enforce self-insert under remapping."
  17771. (interactive "p")
  17772. (self-insert-command N))
  17773. (defun org-string-width (s)
  17774. "Compute width of string, ignoring invisible characters.
  17775. This ignores character with invisibility property `org-link', and also
  17776. characters with property `org-cwidth', because these will become invisible
  17777. upon the next fontification round."
  17778. (let (b l)
  17779. (when (or (eq t buffer-invisibility-spec)
  17780. (assq 'org-link buffer-invisibility-spec))
  17781. (while (setq b (text-property-any 0 (length s)
  17782. 'invisible 'org-link s))
  17783. (setq s (concat (substring s 0 b)
  17784. (substring s (or (next-single-property-change
  17785. b 'invisible s) (length s)))))))
  17786. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17787. (setq s (concat (substring s 0 b)
  17788. (substring s (or (next-single-property-change
  17789. b 'org-cwidth s) (length s))))))
  17790. (setq l (string-width s) b -1)
  17791. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17792. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17793. l))
  17794. (defun org-shorten-string (s maxlength)
  17795. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17796. If the string is shorter or has length MAXLENGTH, just return the
  17797. original string. If it is longer, the functions finds a space in the
  17798. string, breaks this string off at that locations and adds three dots
  17799. as ellipsis. Including the ellipsis, the string will not be longer
  17800. than MAXLENGTH. If finding a good breaking point in the string does
  17801. not work, the string is just chopped off in the middle of a word
  17802. if necessary."
  17803. (if (<= (length s) maxlength)
  17804. s
  17805. (let* ((n (max (- maxlength 4) 1))
  17806. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17807. (if (string-match re s)
  17808. (concat (match-string 1 s) "...")
  17809. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17810. (defun org-get-indentation (&optional line)
  17811. "Get the indentation of the current line, interpreting tabs.
  17812. When LINE is given, assume it represents a line and compute its indentation."
  17813. (if line
  17814. (if (string-match "^ *" (org-remove-tabs line))
  17815. (match-end 0))
  17816. (save-excursion
  17817. (beginning-of-line 1)
  17818. (skip-chars-forward " \t")
  17819. (current-column))))
  17820. (defun org-get-string-indentation (s)
  17821. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17822. (let ((n -1) (i 0) (w tab-width) c)
  17823. (catch 'exit
  17824. (while (< (setq n (1+ n)) (length s))
  17825. (setq c (aref s n))
  17826. (cond ((= c ?\ ) (setq i (1+ i)))
  17827. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17828. (t (throw 'exit t)))))
  17829. i))
  17830. (defun org-remove-tabs (s &optional width)
  17831. "Replace tabulators in S with spaces.
  17832. Assumes that s is a single line, starting in column 0."
  17833. (setq width (or width tab-width))
  17834. (while (string-match "\t" s)
  17835. (setq s (replace-match
  17836. (make-string
  17837. (- (* width (/ (+ (match-beginning 0) width) width))
  17838. (match-beginning 0)) ?\ )
  17839. t t s)))
  17840. s)
  17841. (defun org-fix-indentation (line ind)
  17842. "Fix indentation in LINE.
  17843. IND is a cons cell with target and minimum indentation.
  17844. If the current indentation in LINE is smaller than the minimum,
  17845. leave it alone. If it is larger than ind, set it to the target."
  17846. (let* ((l (org-remove-tabs line))
  17847. (i (org-get-indentation l))
  17848. (i1 (car ind)) (i2 (cdr ind)))
  17849. (if (>= i i2) (setq l (substring line i2)))
  17850. (if (> i1 0)
  17851. (concat (make-string i1 ?\ ) l)
  17852. l)))
  17853. (defun org-remove-indentation (code &optional n)
  17854. "Remove the maximum common indentation from the lines in CODE.
  17855. N may optionally be the number of spaces to remove."
  17856. (with-temp-buffer
  17857. (insert code)
  17858. (org-do-remove-indentation n)
  17859. (buffer-string)))
  17860. (defun org-do-remove-indentation (&optional n)
  17861. "Remove the maximum common indentation from the buffer."
  17862. (untabify (point-min) (point-max))
  17863. (let ((min 10000) re)
  17864. (if n
  17865. (setq min n)
  17866. (goto-char (point-min))
  17867. (while (re-search-forward "^ *[^ \n]" nil t)
  17868. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17869. (unless (or (= min 0) (= min 10000))
  17870. (setq re (format "^ \\{%d\\}" min))
  17871. (goto-char (point-min))
  17872. (while (re-search-forward re nil t)
  17873. (replace-match "")
  17874. (end-of-line 1))
  17875. min)))
  17876. (defun org-fill-template (template alist)
  17877. "Find each %key of ALIST in TEMPLATE and replace it."
  17878. (let ((case-fold-search nil)
  17879. entry key value)
  17880. (setq alist (sort (copy-sequence alist)
  17881. (lambda (a b) (< (length (car a)) (length (car b))))))
  17882. (while (setq entry (pop alist))
  17883. (setq template
  17884. (replace-regexp-in-string
  17885. (concat "%" (regexp-quote (car entry)))
  17886. (cdr entry) template t t)))
  17887. template))
  17888. (defun org-base-buffer (buffer)
  17889. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17890. (if (not buffer)
  17891. buffer
  17892. (or (buffer-base-buffer buffer)
  17893. buffer)))
  17894. (defun org-trim (s)
  17895. "Remove whitespace at beginning and end of string."
  17896. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17897. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17898. s)
  17899. (defun org-wrap (string &optional width lines)
  17900. "Wrap string to either a number of lines, or a width in characters.
  17901. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17902. that costs. If there is a word longer than WIDTH, the text is actually
  17903. wrapped to the length of that word.
  17904. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17905. many lines, whatever width that takes.
  17906. The return value is a list of lines, without newlines at the end."
  17907. (let* ((words (org-split-string string "[ \t\n]+"))
  17908. (maxword (apply 'max (mapcar 'org-string-width words)))
  17909. w ll)
  17910. (cond (width
  17911. (org-do-wrap words (max maxword width)))
  17912. (lines
  17913. (setq w maxword)
  17914. (setq ll (org-do-wrap words maxword))
  17915. (if (<= (length ll) lines)
  17916. ll
  17917. (setq ll words)
  17918. (while (> (length ll) lines)
  17919. (setq w (1+ w))
  17920. (setq ll (org-do-wrap words w)))
  17921. ll))
  17922. (t (error "Cannot wrap this")))))
  17923. (defun org-do-wrap (words width)
  17924. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17925. (let (lines line)
  17926. (while words
  17927. (setq line (pop words))
  17928. (while (and words (< (+ (length line) (length (car words))) width))
  17929. (setq line (concat line " " (pop words))))
  17930. (setq lines (push line lines)))
  17931. (nreverse lines)))
  17932. (defun org-split-string (string &optional separators)
  17933. "Splits STRING into substrings at SEPARATORS.
  17934. No empty strings are returned if there are matches at the beginning
  17935. and end of string."
  17936. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17937. (start 0)
  17938. notfirst
  17939. (list nil))
  17940. (while (and (string-match rexp string
  17941. (if (and notfirst
  17942. (= start (match-beginning 0))
  17943. (< start (length string)))
  17944. (1+ start) start))
  17945. (< (match-beginning 0) (length string)))
  17946. (setq notfirst t)
  17947. (or (eq (match-beginning 0) 0)
  17948. (and (eq (match-beginning 0) (match-end 0))
  17949. (eq (match-beginning 0) start))
  17950. (setq list
  17951. (cons (substring string start (match-beginning 0))
  17952. list)))
  17953. (setq start (match-end 0)))
  17954. (or (eq start (length string))
  17955. (setq list
  17956. (cons (substring string start)
  17957. list)))
  17958. (nreverse list)))
  17959. (defun org-quote-vert (s)
  17960. "Replace \"|\" with \"\\vert\"."
  17961. (while (string-match "|" s)
  17962. (setq s (replace-match "\\vert" t t s)))
  17963. s)
  17964. (defun org-uuidgen-p (s)
  17965. "Is S an ID created by UUIDGEN?"
  17966. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17967. (defun org-in-src-block-p nil
  17968. "Whether point is in a code source block."
  17969. (let (ov)
  17970. (when (setq ov (overlays-at (point)))
  17971. (memq 'org-block-background
  17972. (overlay-properties
  17973. (car ov))))))
  17974. (defun org-context ()
  17975. "Return a list of contexts of the current cursor position.
  17976. If several contexts apply, all are returned.
  17977. Each context entry is a list with a symbol naming the context, and
  17978. two positions indicating start and end of the context. Possible
  17979. contexts are:
  17980. :headline anywhere in a headline
  17981. :headline-stars on the leading stars in a headline
  17982. :todo-keyword on a TODO keyword (including DONE) in a headline
  17983. :tags on the TAGS in a headline
  17984. :priority on the priority cookie in a headline
  17985. :item on the first line of a plain list item
  17986. :item-bullet on the bullet/number of a plain list item
  17987. :checkbox on the checkbox in a plain list item
  17988. :table in an org-mode table
  17989. :table-special on a special filed in a table
  17990. :table-table in a table.el table
  17991. :clocktable in a clocktable
  17992. :src-block in a source block
  17993. :link on a hyperlink
  17994. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  17995. :target on a <<target>>
  17996. :radio-target on a <<<radio-target>>>
  17997. :latex-fragment on a LaTeX fragment
  17998. :latex-preview on a LaTeX fragment with overlaid preview image
  17999. This function expects the position to be visible because it uses font-lock
  18000. faces as a help to recognize the following contexts: :table-special, :link,
  18001. and :keyword."
  18002. (let* ((f (get-text-property (point) 'face))
  18003. (faces (if (listp f) f (list f)))
  18004. (case-fold-search t)
  18005. (p (point)) clist o)
  18006. ;; First the large context
  18007. (cond
  18008. ((org-at-heading-p t)
  18009. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18010. (when (progn
  18011. (beginning-of-line 1)
  18012. (looking-at org-todo-line-tags-regexp))
  18013. (push (org-point-in-group p 1 :headline-stars) clist)
  18014. (push (org-point-in-group p 2 :todo-keyword) clist)
  18015. (push (org-point-in-group p 4 :tags) clist))
  18016. (goto-char p)
  18017. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18018. (if (looking-at "\\[#[A-Z0-9]\\]")
  18019. (push (org-point-in-group p 0 :priority) clist)))
  18020. ((org-at-item-p)
  18021. (push (org-point-in-group p 2 :item-bullet) clist)
  18022. (push (list :item (point-at-bol)
  18023. (save-excursion (org-end-of-item) (point)))
  18024. clist)
  18025. (and (org-at-item-checkbox-p)
  18026. (push (org-point-in-group p 0 :checkbox) clist)))
  18027. ((org-at-table-p)
  18028. (push (list :table (org-table-begin) (org-table-end)) clist)
  18029. (if (memq 'org-formula faces)
  18030. (push (list :table-special
  18031. (previous-single-property-change p 'face)
  18032. (next-single-property-change p 'face)) clist)))
  18033. ((org-at-table-p 'any)
  18034. (push (list :table-table) clist)))
  18035. (goto-char p)
  18036. ;; New the "medium" contexts: clocktables, source blocks
  18037. (cond ((org-in-clocktable-p)
  18038. (push (list :clocktable
  18039. (and (or (looking-at "#\\+BEGIN: clocktable")
  18040. (search-backward "#+BEGIN: clocktable" nil t))
  18041. (match-beginning 0))
  18042. (and (re-search-forward "#\\+END:?" nil t)
  18043. (match-end 0))) clist))
  18044. ((org-in-src-block-p)
  18045. (push (list :src-block
  18046. (and (or (looking-at "#\\+BEGIN_SRC")
  18047. (search-backward "#+BEGIN_SRC" nil t))
  18048. (match-beginning 0))
  18049. (and (search-forward "#+END_SRC" nil t)
  18050. (match-beginning 0))) clist)))
  18051. (goto-char p)
  18052. ;; Now the small context
  18053. (cond
  18054. ((org-at-timestamp-p)
  18055. (push (org-point-in-group p 0 :timestamp) clist))
  18056. ((memq 'org-link faces)
  18057. (push (list :link
  18058. (previous-single-property-change p 'face)
  18059. (next-single-property-change p 'face)) clist))
  18060. ((memq 'org-special-keyword faces)
  18061. (push (list :keyword
  18062. (previous-single-property-change p 'face)
  18063. (next-single-property-change p 'face)) clist))
  18064. ((org-at-target-p)
  18065. (push (org-point-in-group p 0 :target) clist)
  18066. (goto-char (1- (match-beginning 0)))
  18067. (if (looking-at org-radio-target-regexp)
  18068. (push (org-point-in-group p 0 :radio-target) clist))
  18069. (goto-char p))
  18070. ((setq o (car (delq nil
  18071. (mapcar
  18072. (lambda (x)
  18073. (if (memq x org-latex-fragment-image-overlays) x))
  18074. (overlays-at (point))))))
  18075. (push (list :latex-fragment
  18076. (overlay-start o) (overlay-end o)) clist)
  18077. (push (list :latex-preview
  18078. (overlay-start o) (overlay-end o)) clist))
  18079. ((org-inside-LaTeX-fragment-p)
  18080. ;; FIXME: positions wrong.
  18081. (push (list :latex-fragment (point) (point)) clist)))
  18082. (setq clist (nreverse (delq nil clist)))
  18083. clist))
  18084. ;; FIXME: Compare with at-regexp-p Do we need both?
  18085. (defun org-in-regexp (re &optional nlines visually)
  18086. "Check if point is inside a match of regexp.
  18087. Normally only the current line is checked, but you can include NLINES extra
  18088. lines both before and after point into the search.
  18089. If VISUALLY is set, require that the cursor is not after the match but
  18090. really on, so that the block visually is on the match."
  18091. (catch 'exit
  18092. (let ((pos (point))
  18093. (eol (point-at-eol (+ 1 (or nlines 0))))
  18094. (inc (if visually 1 0)))
  18095. (save-excursion
  18096. (beginning-of-line (- 1 (or nlines 0)))
  18097. (while (re-search-forward re eol t)
  18098. (if (and (<= (match-beginning 0) pos)
  18099. (>= (+ inc (match-end 0)) pos))
  18100. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18101. (defun org-at-regexp-p (regexp)
  18102. "Is point inside a match of REGEXP in the current line?"
  18103. (catch 'exit
  18104. (save-excursion
  18105. (let ((pos (point)) (end (point-at-eol)))
  18106. (beginning-of-line 1)
  18107. (while (re-search-forward regexp end t)
  18108. (if (and (<= (match-beginning 0) pos)
  18109. (>= (match-end 0) pos))
  18110. (throw 'exit t)))
  18111. nil))))
  18112. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18113. "Non-nil when point is between matches of START-RE and END-RE.
  18114. Also return a non-nil value when point is on one of the matches.
  18115. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18116. buffer positions. Default values are the positions of headlines
  18117. surrounding the point.
  18118. The functions returns a cons cell whose car (resp. cdr) is the
  18119. position before START-RE (resp. after END-RE)."
  18120. (save-match-data
  18121. (let ((pos (point))
  18122. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18123. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18124. beg end)
  18125. (save-excursion
  18126. ;; Point is on a block when on START-RE or if START-RE can be
  18127. ;; found before it...
  18128. (and (or (org-at-regexp-p start-re)
  18129. (re-search-backward start-re limit-up t))
  18130. (setq beg (match-beginning 0))
  18131. ;; ... and END-RE after it...
  18132. (goto-char (match-end 0))
  18133. (re-search-forward end-re limit-down t)
  18134. (> (setq end (match-end 0)) pos)
  18135. ;; ... without another START-RE in-between.
  18136. (goto-char (match-beginning 0))
  18137. (not (re-search-backward start-re (1+ beg) t))
  18138. ;; Return value.
  18139. (cons beg end))))))
  18140. (defun org-in-block-p (names)
  18141. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18142. NAMES is a list of strings containing names of blocks.
  18143. Return first block name matched, or nil. Beware that in case of
  18144. nested blocks, the returned name may not belong to the closest
  18145. block from point."
  18146. (save-match-data
  18147. (catch 'exit
  18148. (let ((case-fold-search t)
  18149. (lim-up (save-excursion (outline-previous-heading)))
  18150. (lim-down (save-excursion (outline-next-heading))))
  18151. (mapc (lambda (name)
  18152. (let ((n (regexp-quote name)))
  18153. (when (org-between-regexps-p
  18154. (concat "^[ \t]*#\\+begin_" n)
  18155. (concat "^[ \t]*#\\+end_" n)
  18156. lim-up lim-down)
  18157. (throw 'exit n))))
  18158. names))
  18159. nil)))
  18160. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18161. "Call `multi-occur' with buffers for all agenda files."
  18162. (interactive "sOrg-files matching: \np")
  18163. (let* ((files (org-agenda-files))
  18164. (tnames (mapcar 'file-truename files))
  18165. (extra org-agenda-text-search-extra-files)
  18166. f)
  18167. (when (eq (car extra) 'agenda-archives)
  18168. (setq extra (cdr extra))
  18169. (setq files (org-add-archive-files files)))
  18170. (while (setq f (pop extra))
  18171. (unless (member (file-truename f) tnames)
  18172. (add-to-list 'files f 'append)
  18173. (add-to-list 'tnames (file-truename f) 'append)))
  18174. (multi-occur
  18175. (mapcar (lambda (x)
  18176. (with-current-buffer
  18177. (or (get-file-buffer x) (find-file-noselect x))
  18178. (widen)
  18179. (current-buffer)))
  18180. files)
  18181. regexp)))
  18182. (if (boundp 'occur-mode-find-occurrence-hook)
  18183. ;; Emacs 23
  18184. (add-hook 'occur-mode-find-occurrence-hook
  18185. (lambda ()
  18186. (when (eq major-mode 'org-mode)
  18187. (org-reveal))))
  18188. ;; Emacs 22
  18189. (defadvice occur-mode-goto-occurrence
  18190. (after org-occur-reveal activate)
  18191. (and (eq major-mode 'org-mode) (org-reveal)))
  18192. (defadvice occur-mode-goto-occurrence-other-window
  18193. (after org-occur-reveal activate)
  18194. (and (eq major-mode 'org-mode) (org-reveal)))
  18195. (defadvice occur-mode-display-occurrence
  18196. (after org-occur-reveal activate)
  18197. (when (eq major-mode 'org-mode)
  18198. (let ((pos (occur-mode-find-occurrence)))
  18199. (with-current-buffer (marker-buffer pos)
  18200. (save-excursion
  18201. (goto-char pos)
  18202. (org-reveal)))))))
  18203. (defun org-occur-link-in-agenda-files ()
  18204. "Create a link and search for it in the agendas.
  18205. The link is not stored in `org-stored-links', it is just created
  18206. for the search purpose."
  18207. (interactive)
  18208. (let ((link (condition-case nil
  18209. (org-store-link nil)
  18210. (error "Unable to create a link to here"))))
  18211. (org-occur-in-agenda-files (regexp-quote link))))
  18212. (defun org-uniquify (list)
  18213. "Remove duplicate elements from LIST."
  18214. (let (res)
  18215. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18216. res))
  18217. (defun org-delete-all (elts list)
  18218. "Remove all elements in ELTS from LIST."
  18219. (while elts
  18220. (setq list (delete (pop elts) list)))
  18221. list)
  18222. (defun org-count (cl-item cl-seq)
  18223. "Count the number of occurrences of ITEM in SEQ.
  18224. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18225. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18226. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18227. (while (< cl-start cl-end)
  18228. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18229. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18230. (setq cl-start (1+ cl-start)))
  18231. cl-count))
  18232. (defun org-remove-if (predicate seq)
  18233. "Remove everything from SEQ that fulfills PREDICATE."
  18234. (let (res e)
  18235. (while seq
  18236. (setq e (pop seq))
  18237. (if (not (funcall predicate e)) (push e res)))
  18238. (nreverse res)))
  18239. (defun org-remove-if-not (predicate seq)
  18240. "Remove everything from SEQ that does not fulfill PREDICATE."
  18241. (let (res e)
  18242. (while seq
  18243. (setq e (pop seq))
  18244. (if (funcall predicate e) (push e res)))
  18245. (nreverse res)))
  18246. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18247. "Reduce two-argument FUNCTION across SEQ.
  18248. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18249. \":initial-value\" removed."
  18250. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18251. (cadr (memq :initial-value cl-keys)))
  18252. (cl-seq (pop cl-seq))
  18253. (t (funcall cl-func)))))
  18254. (while cl-seq
  18255. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18256. cl-accum))
  18257. (defun org-back-over-empty-lines ()
  18258. "Move backwards over whitespace, to the beginning of the first empty line.
  18259. Returns the number of empty lines passed."
  18260. (let ((pos (point)))
  18261. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18262. (skip-chars-backward " \t\n\r")
  18263. (unless (eobp)
  18264. (forward-line -1)))
  18265. (beginning-of-line 2)
  18266. (goto-char (min (point) pos))
  18267. (count-lines (point) pos)))
  18268. (defun org-skip-whitespace ()
  18269. (skip-chars-forward " \t\n\r"))
  18270. (defun org-point-in-group (point group &optional context)
  18271. "Check if POINT is in match-group GROUP.
  18272. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18273. match. If the match group does not exist or point is not inside it,
  18274. return nil."
  18275. (and (match-beginning group)
  18276. (>= point (match-beginning group))
  18277. (<= point (match-end group))
  18278. (if context
  18279. (list context (match-beginning group) (match-end group))
  18280. t)))
  18281. (defun org-switch-to-buffer-other-window (&rest args)
  18282. "Switch to buffer in a second window on the current frame.
  18283. In particular, do not allow pop-up frames.
  18284. Returns the newly created buffer."
  18285. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18286. special-display-function)
  18287. (apply 'switch-to-buffer-other-window args)))
  18288. (defun org-combine-plists (&rest plists)
  18289. "Create a single property list from all plists in PLISTS.
  18290. The process starts by copying the first list, and then setting properties
  18291. from the other lists. Settings in the last list are the most significant
  18292. ones and overrule settings in the other lists."
  18293. (let ((rtn (copy-sequence (pop plists)))
  18294. p v ls)
  18295. (while plists
  18296. (setq ls (pop plists))
  18297. (while ls
  18298. (setq p (pop ls) v (pop ls))
  18299. (setq rtn (plist-put rtn p v))))
  18300. rtn))
  18301. (defun org-move-line-down (arg)
  18302. "Move the current line down. With prefix argument, move it past ARG lines."
  18303. (interactive "p")
  18304. (let ((col (current-column))
  18305. beg end pos)
  18306. (beginning-of-line 1) (setq beg (point))
  18307. (beginning-of-line 2) (setq end (point))
  18308. (beginning-of-line (+ 1 arg))
  18309. (setq pos (move-marker (make-marker) (point)))
  18310. (insert (delete-and-extract-region beg end))
  18311. (goto-char pos)
  18312. (org-move-to-column col)))
  18313. (defun org-move-line-up (arg)
  18314. "Move the current line up. With prefix argument, move it past ARG lines."
  18315. (interactive "p")
  18316. (let ((col (current-column))
  18317. beg end pos)
  18318. (beginning-of-line 1) (setq beg (point))
  18319. (beginning-of-line 2) (setq end (point))
  18320. (beginning-of-line (- arg))
  18321. (setq pos (move-marker (make-marker) (point)))
  18322. (insert (delete-and-extract-region beg end))
  18323. (goto-char pos)
  18324. (org-move-to-column col)))
  18325. (defun org-replace-escapes (string table)
  18326. "Replace %-escapes in STRING with values in TABLE.
  18327. TABLE is an association list with keys like \"%a\" and string values.
  18328. The sequences in STRING may contain normal field width and padding information,
  18329. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18330. so values can contain further %-escapes if they are define later in TABLE."
  18331. (let ((tbl (copy-alist table))
  18332. (case-fold-search nil)
  18333. (pchg 0)
  18334. e re rpl)
  18335. (while (setq e (pop tbl))
  18336. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18337. (when (and (cdr e) (string-match re (cdr e)))
  18338. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18339. (safe "SREF"))
  18340. (add-text-properties 0 3 (list 'sref sref) safe)
  18341. (setcdr e (replace-match safe t t (cdr e)))))
  18342. (while (string-match re string)
  18343. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18344. (cdr e)))
  18345. (setq string (replace-match rpl t t string))))
  18346. (while (setq pchg (next-property-change pchg string))
  18347. (let ((sref (get-text-property pchg 'sref string)))
  18348. (when (and sref (string-match "SREF" string pchg))
  18349. (setq string (replace-match sref t t string)))))
  18350. string))
  18351. (defun org-sublist (list start end)
  18352. "Return a section of LIST, from START to END.
  18353. Counting starts at 1."
  18354. (let (rtn (c start))
  18355. (setq list (nthcdr (1- start) list))
  18356. (while (and list (<= c end))
  18357. (push (pop list) rtn)
  18358. (setq c (1+ c)))
  18359. (nreverse rtn)))
  18360. (defun org-find-base-buffer-visiting (file)
  18361. "Like `find-buffer-visiting' but always return the base buffer and
  18362. not an indirect buffer."
  18363. (let ((buf (or (get-file-buffer file)
  18364. (find-buffer-visiting file))))
  18365. (if buf
  18366. (or (buffer-base-buffer buf) buf)
  18367. nil)))
  18368. (defun org-image-file-name-regexp (&optional extensions)
  18369. "Return regexp matching the file names of images.
  18370. If EXTENSIONS is given, only match these."
  18371. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18372. (image-file-name-regexp)
  18373. (let ((image-file-name-extensions
  18374. (or extensions
  18375. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18376. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18377. (concat "\\."
  18378. (regexp-opt (nconc (mapcar 'upcase
  18379. image-file-name-extensions)
  18380. image-file-name-extensions)
  18381. t)
  18382. "\\'"))))
  18383. (defun org-file-image-p (file &optional extensions)
  18384. "Return non-nil if FILE is an image."
  18385. (save-match-data
  18386. (string-match (org-image-file-name-regexp extensions) file)))
  18387. (defun org-get-cursor-date ()
  18388. "Return the date at cursor in as a time.
  18389. This works in the calendar and in the agenda, anywhere else it just
  18390. returns the current time."
  18391. (let (date day defd)
  18392. (cond
  18393. ((eq major-mode 'calendar-mode)
  18394. (setq date (calendar-cursor-to-date)
  18395. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18396. ((eq major-mode 'org-agenda-mode)
  18397. (setq day (get-text-property (point) 'day))
  18398. (if day
  18399. (setq date (calendar-gregorian-from-absolute day)
  18400. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18401. (nth 2 date))))))
  18402. (or defd (current-time))))
  18403. (defvar org-agenda-action-marker (make-marker)
  18404. "Marker pointing to the entry for the next agenda action.")
  18405. (defun org-mark-entry-for-agenda-action ()
  18406. "Mark the current entry as target of an agenda action.
  18407. Agenda actions are actions executed from the agenda with the key `k',
  18408. which make use of the date at the cursor."
  18409. (interactive)
  18410. (move-marker org-agenda-action-marker
  18411. (save-excursion (org-back-to-heading t) (point))
  18412. (current-buffer))
  18413. (message
  18414. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18415. (defun org-mark-subtree ()
  18416. "Mark the current subtree.
  18417. This puts point at the start of the current subtree, and mark at the end.
  18418. If point is in an inline task, mark that task instead."
  18419. (interactive)
  18420. (let ((inline-task-p
  18421. (and (featurep 'org-inlinetask)
  18422. (org-inlinetask-in-task-p)))
  18423. (beg))
  18424. ;; Get beginning of subtree
  18425. (cond
  18426. (inline-task-p (org-inlinetask-goto-beginning))
  18427. ((org-at-heading-p) (beginning-of-line))
  18428. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18429. (setq beg (point))
  18430. ;; Get end of it
  18431. (if inline-task-p
  18432. (org-inlinetask-goto-end)
  18433. (org-end-of-subtree))
  18434. ;; Mark zone
  18435. (push-mark (point) nil t)
  18436. (goto-char beg)))
  18437. ;;; Paragraph filling stuff.
  18438. ;; We want this to be just right, so use the full arsenal.
  18439. (defun org-indent-line-function ()
  18440. "Indent line depending on context."
  18441. (interactive)
  18442. (let* ((pos (point))
  18443. (itemp (org-at-item-p))
  18444. (case-fold-search t)
  18445. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18446. (inline-task-p (and (featurep 'org-inlinetask)
  18447. (org-inlinetask-in-task-p)))
  18448. (inline-re (and inline-task-p
  18449. (org-inlinetask-outline-regexp)))
  18450. column)
  18451. (beginning-of-line 1)
  18452. (cond
  18453. ;; Comments
  18454. ((looking-at "# ") (setq column 0))
  18455. ;; Headings
  18456. ((looking-at org-outline-regexp) (setq column 0))
  18457. ;; Included files
  18458. ((looking-at "#\\+include:") (setq column 0))
  18459. ;; Footnote definition
  18460. ((looking-at org-footnote-definition-re) (setq column 0))
  18461. ;; Literal examples
  18462. ((looking-at "[ \t]*:\\( \\|$\\)")
  18463. (setq column (org-get-indentation))) ; do nothing
  18464. ;; Lists
  18465. ((ignore-errors (goto-char (org-in-item-p)))
  18466. (setq column (if itemp
  18467. (org-get-indentation)
  18468. (org-list-item-body-column (point))))
  18469. (goto-char pos))
  18470. ;; Drawers
  18471. ((and (looking-at "[ \t]*:END:")
  18472. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18473. (save-excursion
  18474. (goto-char (1- (match-beginning 1)))
  18475. (setq column (current-column))))
  18476. ;; Special blocks
  18477. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18478. (save-excursion
  18479. (re-search-backward
  18480. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18481. (setq column (org-get-indentation (match-string 0))))
  18482. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18483. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18484. (save-excursion
  18485. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18486. (setq column
  18487. (cond ((equal (downcase (match-string 1)) "src")
  18488. ;; src blocks: let `org-edit-src-exit' handle them
  18489. (org-get-indentation))
  18490. ((equal (downcase (match-string 1)) "example")
  18491. (max (org-get-indentation)
  18492. (org-get-indentation (match-string 0))))
  18493. (t
  18494. (org-get-indentation (match-string 0))))))
  18495. ;; This line has nothing special, look at the previous relevant
  18496. ;; line to compute indentation
  18497. (t
  18498. (beginning-of-line 0)
  18499. (while (and (not (bobp))
  18500. (not (looking-at org-drawer-regexp))
  18501. ;; When point started in an inline task, do not move
  18502. ;; above task starting line.
  18503. (not (and inline-task-p (looking-at inline-re)))
  18504. ;; Skip drawers, blocks, empty lines, verbatim,
  18505. ;; comments, tables, footnotes definitions, lists,
  18506. ;; inline tasks.
  18507. (or (and (looking-at "[ \t]*:END:")
  18508. (re-search-backward org-drawer-regexp nil t))
  18509. (and (looking-at "[ \t]*#\\+end_")
  18510. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18511. (looking-at "[ \t]*[\n:#|]")
  18512. (looking-at org-footnote-definition-re)
  18513. (and (ignore-errors (goto-char (org-in-item-p)))
  18514. (goto-char
  18515. (org-list-get-top-point (org-list-struct))))
  18516. (and (not inline-task-p)
  18517. (featurep 'org-inlinetask)
  18518. (org-inlinetask-in-task-p)
  18519. (or (org-inlinetask-goto-beginning) t))))
  18520. (beginning-of-line 0))
  18521. (cond
  18522. ;; There was an heading above.
  18523. ((looking-at "\\*+[ \t]+")
  18524. (if (not org-adapt-indentation)
  18525. (setq column 0)
  18526. (goto-char (match-end 0))
  18527. (setq column (current-column))))
  18528. ;; A drawer had started and is unfinished
  18529. ((looking-at org-drawer-regexp)
  18530. (goto-char (1- (match-beginning 1)))
  18531. (setq column (current-column)))
  18532. ;; Else, nothing noticeable found: get indentation and go on.
  18533. (t (setq column (org-get-indentation))))))
  18534. ;; Now apply indentation and move cursor accordingly
  18535. (goto-char pos)
  18536. (if (<= (current-column) (current-indentation))
  18537. (org-indent-line-to column)
  18538. (save-excursion (org-indent-line-to column)))
  18539. ;; Special polishing for properties, see `org-property-format'
  18540. (setq column (current-column))
  18541. (beginning-of-line 1)
  18542. (if (looking-at
  18543. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18544. (replace-match (concat (match-string 1)
  18545. (format org-property-format
  18546. (match-string 2) (match-string 3)))
  18547. t t))
  18548. (org-move-to-column column)))
  18549. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  18550. "Variable to store copy of `adaptive-fill-regexp'.
  18551. Since `adaptive-fill-regexp' is set to never match, we need to
  18552. store a backup of its value before entering `org-mode' so that
  18553. the functionality can be provided as a fall-back.")
  18554. (defun org-set-autofill-regexps ()
  18555. (interactive)
  18556. ;; In the paragraph separator we include headlines, because filling
  18557. ;; text in a line directly attached to a headline would otherwise
  18558. ;; fill the headline as well.
  18559. (org-set-local 'comment-start-skip "^#+[ \t]*")
  18560. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  18561. ;; The paragraph starter includes hand-formatted lists.
  18562. (org-set-local
  18563. 'paragraph-start
  18564. (concat
  18565. "\f" "\\|"
  18566. "[ ]*$" "\\|"
  18567. org-outline-regexp "\\|"
  18568. "[ \t]*#" "\\|"
  18569. (org-item-re) "\\|"
  18570. "[ \t]*[:|]" "\\|"
  18571. "\\$\\$" "\\|"
  18572. "\\\\\\(begin\\|end\\|[][]\\)"))
  18573. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  18574. ;; But only if the user has not turned off tables or fixed-width regions
  18575. (org-set-local
  18576. 'auto-fill-inhibit-regexp
  18577. (concat org-outline-regexp
  18578. "\\|#\\+"
  18579. "\\|[ \t]*" org-keyword-time-regexp
  18580. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  18581. (concat
  18582. "\\|[ \t]*["
  18583. (if org-enable-table-editor "|" "")
  18584. (if org-enable-fixed-width-editor ":" "")
  18585. "]"))))
  18586. ;; We use our own fill-paragraph function, to make sure that tables
  18587. ;; and fixed-width regions are not wrapped. That function will pass
  18588. ;; through to `fill-paragraph' when appropriate.
  18589. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  18590. ;; Prevent auto-fill from inserting unwanted new items.
  18591. (if (boundp 'fill-nobreak-predicate)
  18592. (org-set-local 'fill-nobreak-predicate
  18593. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  18594. fill-nobreak-predicate
  18595. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate))))
  18596. ;; Adaptive filling: To get full control, first make sure that
  18597. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  18598. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  18599. (org-set-local 'org-adaptive-fill-regexp-backup
  18600. adaptive-fill-regexp))
  18601. (org-set-local 'adaptive-fill-regexp "\000")
  18602. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  18603. (org-set-local 'adaptive-fill-function
  18604. 'org-adaptive-fill-function)
  18605. (org-set-local
  18606. 'align-mode-rules-list
  18607. '((org-in-buffer-settings
  18608. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  18609. (modes . '(org-mode))))))
  18610. (defun org-fill-item-nobreak-p ()
  18611. "Non-nil when a line break at point would insert a new item."
  18612. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  18613. (defun org-fill-paragraph (&optional justify)
  18614. "Re-align a table, pass through to fill-paragraph if no table."
  18615. (let ((table-p (org-at-table-p))
  18616. (table.el-p (org-at-table.el-p))
  18617. (itemp (org-in-item-p)))
  18618. (cond ((and (equal (char-after (point-at-bol)) ?*)
  18619. (save-excursion (goto-char (point-at-bol))
  18620. (looking-at org-outline-regexp)))
  18621. t) ; skip headlines
  18622. (table.el-p t) ; skip table.el tables
  18623. (table-p (org-table-align) t) ; align Org tables
  18624. (itemp ; align text in items
  18625. (let* ((struct (save-excursion (goto-char itemp)
  18626. (org-list-struct)))
  18627. (parents (org-list-parents-alist struct))
  18628. (children (org-list-get-children itemp struct parents))
  18629. beg end prev next prefix)
  18630. ;; Determine in which part of item point is: before
  18631. ;; first child, after last child, between two
  18632. ;; sub-lists, or simply in item if there's no child.
  18633. (cond
  18634. ((not children)
  18635. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18636. beg itemp
  18637. end (org-list-get-item-end itemp struct)))
  18638. ((< (point) (setq next (car children)))
  18639. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  18640. beg itemp
  18641. end next))
  18642. ((> (point) (setq prev (car (last children))))
  18643. (setq beg (org-list-get-item-end prev struct)
  18644. end (org-list-get-item-end itemp struct)
  18645. prefix (save-excursion
  18646. (goto-char beg)
  18647. (skip-chars-forward " \t")
  18648. (make-string (current-column) ?\ ))))
  18649. (t (catch 'exit
  18650. (while (setq next (pop children))
  18651. (if (> (point) next)
  18652. (setq prev next)
  18653. (setq beg (org-list-get-item-end prev struct)
  18654. end next
  18655. prefix (save-excursion
  18656. (goto-char beg)
  18657. (skip-chars-forward " \t")
  18658. (make-string (current-column) ?\ )))
  18659. (throw 'exit nil))))))
  18660. ;; Use `fill-paragraph' with buffer narrowed to item
  18661. ;; without any child, and with our computed PREFIX.
  18662. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18663. (save-restriction
  18664. (narrow-to-region beg end)
  18665. (save-excursion (fill-paragraph justify)))) t))
  18666. ;; Special case where point is not in a list but is on
  18667. ;; a paragraph adjacent to a list: make sure this paragraph
  18668. ;; doesn't get merged with the end of the list by narrowing
  18669. ;; buffer first.
  18670. ((save-excursion (forward-paragraph -1)
  18671. (setq itemp (org-in-item-p)))
  18672. (let ((struct (save-excursion (goto-char itemp)
  18673. (org-list-struct))))
  18674. (save-restriction
  18675. (narrow-to-region (org-list-get-bottom-point struct)
  18676. (save-excursion (forward-paragraph 1)
  18677. (point)))
  18678. (fill-paragraph justify) t)))
  18679. ;; Else simply call `fill-paragraph'.
  18680. (t nil))))
  18681. ;; For reference, this is the default value of adaptive-fill-regexp
  18682. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  18683. (defun org-adaptive-fill-function ()
  18684. "Return a fill prefix for org-mode files."
  18685. (let (itemp)
  18686. (save-excursion
  18687. (cond
  18688. ;; Comment line
  18689. ((looking-at "#[ \t]+")
  18690. (match-string-no-properties 0))
  18691. ;; Plain list item
  18692. ((org-at-item-p)
  18693. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  18694. ;; Point is in a list after `backward-paragraph': original
  18695. ;; point wasn't in the list, or filling would have been taken
  18696. ;; care of by `org-auto-fill-function', but the list and the
  18697. ;; real paragraph are not separated by a blank line. Thus, move
  18698. ;; point after the list to go back to real paragraph and
  18699. ;; determine fill-prefix.
  18700. ((setq itemp (org-in-item-p))
  18701. (goto-char itemp)
  18702. (let* ((struct (org-list-struct))
  18703. (bottom (org-list-get-bottom-point struct)))
  18704. (goto-char bottom)
  18705. (make-string (org-get-indentation) ?\ )))
  18706. ;; Other text
  18707. ((looking-at org-adaptive-fill-regexp-backup)
  18708. (match-string-no-properties 0))))))
  18709. (defun org-auto-fill-function ()
  18710. "Auto-fill function."
  18711. (let (itemp prefix)
  18712. ;; When in a list, compute an appropriate fill-prefix and make
  18713. ;; sure it will be used by `do-auto-fill'.
  18714. (if (setq itemp (org-in-item-p))
  18715. (progn
  18716. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  18717. (flet ((fill-context-prefix (from to &optional flr) prefix))
  18718. (do-auto-fill)))
  18719. ;; Else just use `do-auto-fill'.
  18720. (do-auto-fill))))
  18721. ;;; Other stuff.
  18722. (defun org-toggle-fixed-width-section (arg)
  18723. "Toggle the fixed-width export.
  18724. If there is no active region, the QUOTE keyword at the current headline is
  18725. inserted or removed. When present, it causes the text between this headline
  18726. and the next to be exported as fixed-width text, and unmodified.
  18727. If there is an active region, this command adds or removes a colon as the
  18728. first character of this line. If the first character of a line is a colon,
  18729. this line is also exported in fixed-width font."
  18730. (interactive "P")
  18731. (let* ((cc 0)
  18732. (regionp (org-region-active-p))
  18733. (beg (if regionp (region-beginning) (point)))
  18734. (end (if regionp (region-end)))
  18735. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  18736. (case-fold-search nil)
  18737. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  18738. off)
  18739. (if regionp
  18740. (save-excursion
  18741. (goto-char beg)
  18742. (setq cc (current-column))
  18743. (beginning-of-line 1)
  18744. (setq off (looking-at re))
  18745. (while (> nlines 0)
  18746. (setq nlines (1- nlines))
  18747. (beginning-of-line 1)
  18748. (cond
  18749. (arg
  18750. (org-move-to-column cc t)
  18751. (insert ": \n")
  18752. (forward-line -1))
  18753. ((and off (looking-at re))
  18754. (replace-match "" t t nil 1))
  18755. ((not off) (org-move-to-column cc t) (insert ": ")))
  18756. (forward-line 1)))
  18757. (save-excursion
  18758. (org-back-to-heading)
  18759. (cond
  18760. ((looking-at (format org-heading-keyword-regexp-format
  18761. org-quote-string))
  18762. (goto-char (match-end 1))
  18763. (looking-at (concat " +" org-quote-string))
  18764. (replace-match "" t t)
  18765. (when (eolp) (insert " ")))
  18766. ((looking-at org-outline-regexp)
  18767. (goto-char (match-end 0))
  18768. (insert org-quote-string " ")))))))
  18769. (defun org-reftex-citation ()
  18770. "Use reftex-citation to insert a citation into the buffer.
  18771. This looks for a line like
  18772. #+BIBLIOGRAPHY: foo plain option:-d
  18773. and derives from it that foo.bib is the bibliography file relevant
  18774. for this document. It then installs the necessary environment for RefTeX
  18775. to work in this buffer and calls `reftex-citation' to insert a citation
  18776. into the buffer.
  18777. Export of such citations to both LaTeX and HTML is handled by the contributed
  18778. package org-exp-bibtex by Taru Karttunen."
  18779. (interactive)
  18780. (let ((reftex-docstruct-symbol 'rds)
  18781. (reftex-cite-format "\\cite{%l}")
  18782. rds bib)
  18783. (save-excursion
  18784. (save-restriction
  18785. (widen)
  18786. (let ((case-fold-search t)
  18787. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  18788. (if (not (save-excursion
  18789. (or (re-search-forward re nil t)
  18790. (re-search-backward re nil t))))
  18791. (error "No bibliography defined in file")
  18792. (setq bib (concat (match-string 1) ".bib")
  18793. rds (list (list 'bib bib)))))))
  18794. (call-interactively 'reftex-citation)))
  18795. ;;;; Functions extending outline functionality
  18796. (defun org-beginning-of-line (&optional arg)
  18797. "Go to the beginning of the current line. If that is invisible, continue
  18798. to a visible line beginning. This makes the function of C-a more intuitive.
  18799. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18800. first attempt, and only move to after the tags when the cursor is already
  18801. beyond the end of the headline."
  18802. (interactive "P")
  18803. (let ((pos (point))
  18804. (special (if (consp org-special-ctrl-a/e)
  18805. (car org-special-ctrl-a/e)
  18806. org-special-ctrl-a/e))
  18807. refpos)
  18808. (if (org-bound-and-true-p line-move-visual)
  18809. (beginning-of-visual-line 1)
  18810. (beginning-of-line 1))
  18811. (if (and arg (fboundp 'move-beginning-of-line))
  18812. (call-interactively 'move-beginning-of-line)
  18813. (if (bobp)
  18814. nil
  18815. (backward-char 1)
  18816. (if (org-truely-invisible-p)
  18817. (while (and (not (bobp)) (org-truely-invisible-p))
  18818. (backward-char 1)
  18819. (beginning-of-line 1))
  18820. (forward-char 1))))
  18821. (when special
  18822. (cond
  18823. ((and (looking-at org-complex-heading-regexp)
  18824. (= (char-after (match-end 1)) ?\ ))
  18825. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  18826. (point-at-eol)))
  18827. (goto-char
  18828. (if (eq special t)
  18829. (cond ((> pos refpos) refpos)
  18830. ((= pos (point)) refpos)
  18831. (t (point)))
  18832. (cond ((> pos (point)) (point))
  18833. ((not (eq last-command this-command)) (point))
  18834. (t refpos)))))
  18835. ((org-at-item-p)
  18836. ;; Set special position at first white space character after
  18837. ;; bullet, and check-box, if any.
  18838. (let ((after-bullet
  18839. (progn (looking-at org-list-full-item-re)
  18840. (let ((bul (or (match-end 3) (match-end 1))))
  18841. (if (= (char-after bul) ? ) (1+ bul) bul)))))
  18842. ;; Special case: Move point to special position when
  18843. ;; currently after it or at beginning of line.
  18844. (if (eq special t)
  18845. (when (or (> pos after-bullet) (= (point) pos))
  18846. (goto-char after-bullet))
  18847. ;; Reversed case: Move point to special position when
  18848. ;; point was already at beginning of line and command is
  18849. ;; repeated.
  18850. (when (and (= (point) pos) (eq last-command this-command))
  18851. (goto-char after-bullet)))))))
  18852. (org-no-warnings
  18853. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18854. (defun org-end-of-line (&optional arg)
  18855. "Go to the end of the line.
  18856. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18857. first attempt, and only move to after the tags when the cursor is already
  18858. beyond the end of the headline."
  18859. (interactive "P")
  18860. (let ((special (if (consp org-special-ctrl-a/e)
  18861. (cdr org-special-ctrl-a/e)
  18862. org-special-ctrl-a/e)))
  18863. (cond
  18864. ((or (not special) arg
  18865. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  18866. (call-interactively
  18867. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18868. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18869. (t 'end-of-line))))
  18870. ((org-at-heading-p)
  18871. (let ((pos (point)))
  18872. (beginning-of-line 1)
  18873. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18874. (if (eq special t)
  18875. (if (or (< pos (match-beginning 1))
  18876. (= pos (match-end 0)))
  18877. (goto-char (match-beginning 1))
  18878. (goto-char (match-end 0)))
  18879. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18880. (goto-char (match-end 0))
  18881. (goto-char (match-beginning 1))))
  18882. (call-interactively (if (fboundp 'move-end-of-line)
  18883. 'move-end-of-line
  18884. 'end-of-line)))))
  18885. ((org-at-drawer-p)
  18886. (move-end-of-line 1)
  18887. (when (overlays-at (1- (point))) (backward-char 1)))
  18888. ;; At an item: Move before any hidden text.
  18889. (t (call-interactively 'end-of-line)))
  18890. (org-no-warnings
  18891. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18892. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18893. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18894. (defun org-backward-sentence (&optional arg)
  18895. "Go to beginning of sentence, or beginning of table field.
  18896. This will call `backward-sentence' or `org-table-beginning-of-field',
  18897. depending on context."
  18898. (interactive "P")
  18899. (cond
  18900. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18901. (t (call-interactively 'backward-sentence))))
  18902. (defun org-forward-sentence (&optional arg)
  18903. "Go to end of sentence, or end of table field.
  18904. This will call `forward-sentence' or `org-table-end-of-field',
  18905. depending on context."
  18906. (interactive "P")
  18907. (cond
  18908. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18909. (t (call-interactively 'forward-sentence))))
  18910. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18911. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18912. (defun org-kill-line (&optional arg)
  18913. "Kill line, to tags or end of line."
  18914. (interactive "P")
  18915. (cond
  18916. ((or (not org-special-ctrl-k)
  18917. (bolp)
  18918. (not (org-at-heading-p)))
  18919. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18920. org-ctrl-k-protect-subtree)
  18921. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18922. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18923. (error "C-k aborted - would kill hidden subtree")))
  18924. (call-interactively
  18925. (if visual-line-mode 'kill-visual-line 'kill-line)))
  18926. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18927. (kill-region (point) (match-beginning 1))
  18928. (org-set-tags nil t))
  18929. (t (kill-region (point) (point-at-eol)))))
  18930. (define-key org-mode-map "\C-k" 'org-kill-line)
  18931. (defun org-yank (&optional arg)
  18932. "Yank. If the kill is a subtree, treat it specially.
  18933. This command will look at the current kill and check if is a single
  18934. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18935. cursor is at the beginning of a line or after the stars of a currently
  18936. empty headline, then the yank is handled specially. How exactly depends
  18937. on the value of the following variables, both set by default.
  18938. org-yank-folded-subtrees
  18939. When set, the subtree(s) will be folded after insertion, but only
  18940. if doing so would now swallow text after the yanked text.
  18941. org-yank-adjusted-subtrees
  18942. When set, the subtree will be promoted or demoted in order to
  18943. fit into the local outline tree structure, which means that the level
  18944. will be adjusted so that it becomes the smaller one of the two
  18945. *visible* surrounding headings.
  18946. Any prefix to this command will cause `yank' to be called directly with
  18947. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18948. will just
  18949. plainly yank the text as it is.
  18950. \[1] The test checks if the first non-white line is a heading
  18951. and if there are no other headings with fewer stars."
  18952. (interactive "P")
  18953. (org-yank-generic 'yank arg))
  18954. (defun org-yank-generic (command arg)
  18955. "Perform some yank-like command.
  18956. This function implements the behavior described in the `org-yank'
  18957. documentation. However, it has been generalized to work for any
  18958. interactive command with similar behavior."
  18959. ;; pretend to be command COMMAND
  18960. (setq this-command command)
  18961. (if arg
  18962. (call-interactively command)
  18963. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18964. (and (org-kill-is-subtree-p)
  18965. (or (bolp)
  18966. (and (looking-at "[ \t]*$")
  18967. (string-match
  18968. "\\`\\*+\\'"
  18969. (buffer-substring (point-at-bol) (point)))))))
  18970. swallowp)
  18971. (cond
  18972. ((and subtreep org-yank-folded-subtrees)
  18973. (let ((beg (point))
  18974. end)
  18975. (if (and subtreep org-yank-adjusted-subtrees)
  18976. (org-paste-subtree nil nil 'for-yank)
  18977. (call-interactively command))
  18978. (setq end (point))
  18979. (goto-char beg)
  18980. (when (and (bolp) subtreep
  18981. (not (setq swallowp
  18982. (org-yank-folding-would-swallow-text beg end))))
  18983. (org-with-limited-levels
  18984. (or (looking-at org-outline-regexp)
  18985. (re-search-forward org-outline-regexp-bol end t))
  18986. (while (and (< (point) end) (looking-at org-outline-regexp))
  18987. (hide-subtree)
  18988. (org-cycle-show-empty-lines 'folded)
  18989. (condition-case nil
  18990. (outline-forward-same-level 1)
  18991. (error (goto-char end))))))
  18992. (when swallowp
  18993. (message
  18994. "Inserted text not folded because that would swallow text"))
  18995. (goto-char end)
  18996. (skip-chars-forward " \t\n\r")
  18997. (beginning-of-line 1)
  18998. (push-mark beg 'nomsg)))
  18999. ((and subtreep org-yank-adjusted-subtrees)
  19000. (let ((beg (point-at-bol)))
  19001. (org-paste-subtree nil nil 'for-yank)
  19002. (push-mark beg 'nomsg)))
  19003. (t
  19004. (call-interactively command))))))
  19005. (defun org-yank-folding-would-swallow-text (beg end)
  19006. "Would hide-subtree at BEG swallow any text after END?"
  19007. (let (level)
  19008. (org-with-limited-levels
  19009. (save-excursion
  19010. (goto-char beg)
  19011. (when (or (looking-at org-outline-regexp)
  19012. (re-search-forward org-outline-regexp-bol end t))
  19013. (setq level (org-outline-level)))
  19014. (goto-char end)
  19015. (skip-chars-forward " \t\r\n\v\f")
  19016. (if (or (eobp)
  19017. (and (bolp) (looking-at org-outline-regexp)
  19018. (<= (org-outline-level) level)))
  19019. nil ; Nothing would be swallowed
  19020. t))))) ; something would swallow
  19021. (define-key org-mode-map "\C-y" 'org-yank)
  19022. (defun org-truely-invisible-p ()
  19023. "Check if point is at a character currently not visible.
  19024. This version does not only check the character property, but also
  19025. `visible-mode'."
  19026. ;; Early versions of noutline don't have `outline-invisible-p'.
  19027. (if (org-bound-and-true-p visible-mode)
  19028. nil
  19029. (outline-invisible-p)))
  19030. (defun org-invisible-p2 ()
  19031. "Check if point is at a character currently not visible."
  19032. (save-excursion
  19033. (if (and (eolp) (not (bobp))) (backward-char 1))
  19034. ;; Early versions of noutline don't have `outline-invisible-p'.
  19035. (outline-invisible-p)))
  19036. (defun org-back-to-heading (&optional invisible-ok)
  19037. "Call `outline-back-to-heading', but provide a better error message."
  19038. (condition-case nil
  19039. (outline-back-to-heading invisible-ok)
  19040. (error (error "Before first headline at position %d in buffer %s"
  19041. (point) (current-buffer)))))
  19042. (defun org-beginning-of-defun ()
  19043. "Go to the beginning of the subtree, i.e. back to the heading."
  19044. (org-back-to-heading))
  19045. (defun org-end-of-defun ()
  19046. "Go to the end of the subtree."
  19047. (org-end-of-subtree nil t))
  19048. (defun org-before-first-heading-p ()
  19049. "Before first heading?"
  19050. (save-excursion
  19051. (end-of-line)
  19052. (null (re-search-backward org-outline-regexp-bol nil t))))
  19053. (defun org-at-heading-p (&optional ignored)
  19054. (outline-on-heading-p t))
  19055. ;; Compatibility alias with Org versions < 7.8.03
  19056. (defalias 'org-on-heading-p 'org-at-heading-p)
  19057. (defun org-at-drawer-p nil
  19058. "Whether point is at a drawer."
  19059. (save-excursion
  19060. (move-beginning-of-line 1)
  19061. (looking-at org-drawer-regexp)))
  19062. (defun org-point-at-end-of-empty-headline ()
  19063. "If point is at the end of an empty headline, return t, else nil.
  19064. If the heading only contains a TODO keyword, it is still still considered
  19065. empty."
  19066. (and (looking-at "[ \t]*$")
  19067. (save-excursion
  19068. (beginning-of-line 1)
  19069. (let ((case-fold-search nil))
  19070. (looking-at org-todo-line-regexp)))
  19071. (string= (match-string 3) "")))
  19072. (defun org-at-heading-or-item-p ()
  19073. (or (org-at-heading-p) (org-at-item-p)))
  19074. (defun org-at-target-p ()
  19075. (or (org-in-regexp org-radio-target-regexp)
  19076. (org-in-regexp org-target-regexp)))
  19077. ;; Compatibility alias with Org versions < 7.8.03
  19078. (defalias 'org-on-target-p 'org-at-target-p)
  19079. (defun org-up-heading-all (arg)
  19080. "Move to the heading line of which the present line is a subheading.
  19081. This function considers both visible and invisible heading lines.
  19082. With argument, move up ARG levels."
  19083. (if (fboundp 'outline-up-heading-all)
  19084. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19085. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19086. (defun org-up-heading-safe ()
  19087. "Move to the heading line of which the present line is a subheading.
  19088. This version will not throw an error. It will return the level of the
  19089. headline found, or nil if no higher level is found.
  19090. Also, this function will be a lot faster than `outline-up-heading',
  19091. because it relies on stars being the outline starters. This can really
  19092. make a significant difference in outlines with very many siblings."
  19093. (let (start-level re)
  19094. (org-back-to-heading t)
  19095. (setq start-level (funcall outline-level))
  19096. (if (equal start-level 1)
  19097. nil
  19098. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19099. (if (re-search-backward re nil t)
  19100. (funcall outline-level)))))
  19101. (defun org-first-sibling-p ()
  19102. "Is this heading the first child of its parents?"
  19103. (interactive)
  19104. (let ((re org-outline-regexp-bol)
  19105. level l)
  19106. (unless (org-at-heading-p t)
  19107. (error "Not at a heading"))
  19108. (setq level (funcall outline-level))
  19109. (save-excursion
  19110. (if (not (re-search-backward re nil t))
  19111. t
  19112. (setq l (funcall outline-level))
  19113. (< l level)))))
  19114. (defun org-goto-sibling (&optional previous)
  19115. "Goto the next sibling, even if it is invisible.
  19116. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19117. when a sibling was found. When none is found, return nil and don't
  19118. move point."
  19119. (let ((fun (if previous 're-search-backward 're-search-forward))
  19120. (pos (point))
  19121. (re org-outline-regexp-bol)
  19122. level l)
  19123. (when (condition-case nil (org-back-to-heading t) (error nil))
  19124. (setq level (funcall outline-level))
  19125. (catch 'exit
  19126. (or previous (forward-char 1))
  19127. (while (funcall fun re nil t)
  19128. (setq l (funcall outline-level))
  19129. (when (< l level) (goto-char pos) (throw 'exit nil))
  19130. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19131. (goto-char pos)
  19132. nil))))
  19133. (defun org-show-siblings ()
  19134. "Show all siblings of the current headline."
  19135. (save-excursion
  19136. (while (org-goto-sibling) (org-flag-heading nil)))
  19137. (save-excursion
  19138. (while (org-goto-sibling 'previous)
  19139. (org-flag-heading nil))))
  19140. (defun org-goto-first-child ()
  19141. "Goto the first child, even if it is invisible.
  19142. Return t when a child was found. Otherwise don't move point and
  19143. return nil."
  19144. (let (level (pos (point)) (re org-outline-regexp-bol))
  19145. (when (condition-case nil (org-back-to-heading t) (error nil))
  19146. (setq level (outline-level))
  19147. (forward-char 1)
  19148. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19149. (progn (goto-char (match-beginning 0)) t)
  19150. (goto-char pos) nil))))
  19151. (defun org-show-hidden-entry ()
  19152. "Show an entry where even the heading is hidden."
  19153. (save-excursion
  19154. (org-show-entry)))
  19155. (defun org-flag-heading (flag &optional entry)
  19156. "Flag the current heading. FLAG non-nil means make invisible.
  19157. When ENTRY is non-nil, show the entire entry."
  19158. (save-excursion
  19159. (org-back-to-heading t)
  19160. ;; Check if we should show the entire entry
  19161. (if entry
  19162. (progn
  19163. (org-show-entry)
  19164. (save-excursion
  19165. (and (outline-next-heading)
  19166. (org-flag-heading nil))))
  19167. (outline-flag-region (max (point-min) (1- (point)))
  19168. (save-excursion (outline-end-of-heading) (point))
  19169. flag))))
  19170. (defun org-get-next-sibling ()
  19171. "Move to next heading of the same level, and return point.
  19172. If there is no such heading, return nil.
  19173. This is like outline-next-sibling, but invisible headings are ok."
  19174. (let ((level (funcall outline-level)))
  19175. (outline-next-heading)
  19176. (while (and (not (eobp)) (> (funcall outline-level) level))
  19177. (outline-next-heading))
  19178. (if (or (eobp) (< (funcall outline-level) level))
  19179. nil
  19180. (point))))
  19181. (defun org-get-last-sibling ()
  19182. "Move to previous heading of the same level, and return point.
  19183. If there is no such heading, return nil."
  19184. (let ((opoint (point))
  19185. (level (funcall outline-level)))
  19186. (outline-previous-heading)
  19187. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19188. (while (and (> (funcall outline-level) level)
  19189. (not (bobp)))
  19190. (outline-previous-heading))
  19191. (if (< (funcall outline-level) level)
  19192. nil
  19193. (point)))))
  19194. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19195. ;; This contains an exact copy of the original function, but it uses
  19196. ;; `org-back-to-heading', to make it work also in invisible
  19197. ;; trees. And is uses an invisible-OK argument.
  19198. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19199. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19200. ;; with many children and grandchildren etc, this can be much faster
  19201. ;; than the outline version.
  19202. (org-back-to-heading invisible-OK)
  19203. (let ((first t)
  19204. (level (funcall outline-level)))
  19205. (if (and (eq major-mode 'org-mode) (< level 1000))
  19206. ;; A true heading (not a plain list item), in Org-mode
  19207. ;; This means we can easily find the end by looking
  19208. ;; only for the right number of stars. Using a regexp to do
  19209. ;; this is so much faster than using a Lisp loop.
  19210. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19211. (forward-char 1)
  19212. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19213. ;; something else, do it the slow way
  19214. (while (and (not (eobp))
  19215. (or first (> (funcall outline-level) level)))
  19216. (setq first nil)
  19217. (outline-next-heading)))
  19218. (unless to-heading
  19219. (if (memq (preceding-char) '(?\n ?\^M))
  19220. (progn
  19221. ;; Go to end of line before heading
  19222. (forward-char -1)
  19223. (if (memq (preceding-char) '(?\n ?\^M))
  19224. ;; leave blank line before heading
  19225. (forward-char -1))))))
  19226. (point))
  19227. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19228. "Use Org version in org-mode, for dramatic speed-up."
  19229. (if (eq major-mode 'org-mode)
  19230. (progn
  19231. (org-end-of-subtree nil t)
  19232. (unless (eobp) (backward-char 1)))
  19233. ad-do-it))
  19234. (defun org-end-of-meta-data-and-drawers ()
  19235. "Jump to the first text after meta data and drawers in the current entry.
  19236. This will move over empty lines, lines with planning time stamps,
  19237. clocking lines, and drawers."
  19238. (org-back-to-heading t)
  19239. (let ((end (save-excursion (outline-next-heading) (point)))
  19240. (re (concat "\\(" org-drawer-regexp "\\)"
  19241. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19242. (forward-line 1)
  19243. (while (re-search-forward re end t)
  19244. (if (not (match-end 1))
  19245. ;; empty or planning line
  19246. (forward-line 1)
  19247. ;; a drawer, find the end
  19248. (re-search-forward "^[ \t]*:END:" end 'move)
  19249. (forward-line 1)))
  19250. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19251. (point)))
  19252. (defun org-forward-same-level (arg &optional invisible-ok)
  19253. "Move forward to the arg'th subheading at same level as this one.
  19254. Stop at the first and last subheadings of a superior heading.
  19255. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19256. it wil also look at invisible ones."
  19257. (interactive "p")
  19258. (org-back-to-heading invisible-ok)
  19259. (org-at-heading-p)
  19260. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19261. (re (format "^\\*\\{1,%d\\} " level))
  19262. l)
  19263. (forward-char 1)
  19264. (while (> arg 0)
  19265. (while (and (re-search-forward re nil 'move)
  19266. (setq l (- (match-end 0) (match-beginning 0) 1))
  19267. (= l level)
  19268. (not invisible-ok)
  19269. (progn (backward-char 1) (outline-invisible-p)))
  19270. (if (< l level) (setq arg 1)))
  19271. (setq arg (1- arg)))
  19272. (beginning-of-line 1)))
  19273. (defun org-backward-same-level (arg &optional invisible-ok)
  19274. "Move backward to the arg'th subheading at same level as this one.
  19275. Stop at the first and last subheadings of a superior heading."
  19276. (interactive "p")
  19277. (org-back-to-heading)
  19278. (org-at-heading-p)
  19279. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19280. (re (format "^\\*\\{1,%d\\} " level))
  19281. l)
  19282. (while (> arg 0)
  19283. (while (and (re-search-backward re nil 'move)
  19284. (setq l (- (match-end 0) (match-beginning 0) 1))
  19285. (= l level)
  19286. (not invisible-ok)
  19287. (outline-invisible-p))
  19288. (if (< l level) (setq arg 1)))
  19289. (setq arg (1- arg)))))
  19290. (defun org-show-subtree ()
  19291. "Show everything after this heading at deeper levels."
  19292. (outline-flag-region
  19293. (point)
  19294. (save-excursion
  19295. (org-end-of-subtree t t))
  19296. nil))
  19297. (defun org-show-entry ()
  19298. "Show the body directly following this heading.
  19299. Show the heading too, if it is currently invisible."
  19300. (interactive)
  19301. (save-excursion
  19302. (condition-case nil
  19303. (progn
  19304. (org-back-to-heading t)
  19305. (outline-flag-region
  19306. (max (point-min) (1- (point)))
  19307. (save-excursion
  19308. (if (re-search-forward
  19309. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19310. (match-beginning 1)
  19311. (point-max)))
  19312. nil)
  19313. (org-cycle-hide-drawers 'children))
  19314. (error nil))))
  19315. (defun org-make-options-regexp (kwds &optional extra)
  19316. "Make a regular expression for keyword lines."
  19317. (concat
  19318. "^"
  19319. "#?[ \t]*\\+\\("
  19320. (mapconcat 'regexp-quote kwds "\\|")
  19321. (if extra (concat "\\|" extra))
  19322. "\\):[ \t]*"
  19323. "\\(.*\\)"))
  19324. ;; Make isearch reveal the necessary context
  19325. (defun org-isearch-end ()
  19326. "Reveal context after isearch exits."
  19327. (when isearch-success ; only if search was successful
  19328. (if (featurep 'xemacs)
  19329. ;; Under XEmacs, the hook is run in the correct place,
  19330. ;; we directly show the context.
  19331. (org-show-context 'isearch)
  19332. ;; In Emacs the hook runs *before* restoring the overlays.
  19333. ;; So we have to use a one-time post-command-hook to do this.
  19334. ;; (Emacs 22 has a special variable, see function `org-mode')
  19335. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19336. isearch-mode-end-hook-quit)
  19337. ;; Only when the isearch was not quitted.
  19338. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19339. 'append 'local)))))
  19340. (defun org-isearch-post-command ()
  19341. "Remove self from hook, and show context."
  19342. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19343. (org-show-context 'isearch))
  19344. ;;;; Integration with and fixes for other packages
  19345. ;;; Imenu support
  19346. (defvar org-imenu-markers nil
  19347. "All markers currently used by Imenu.")
  19348. (make-variable-buffer-local 'org-imenu-markers)
  19349. (defun org-imenu-new-marker (&optional pos)
  19350. "Return a new marker for use by Imenu, and remember the marker."
  19351. (let ((m (make-marker)))
  19352. (move-marker m (or pos (point)))
  19353. (push m org-imenu-markers)
  19354. m))
  19355. (defun org-imenu-get-tree ()
  19356. "Produce the index for Imenu."
  19357. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19358. (setq org-imenu-markers nil)
  19359. (let* ((n org-imenu-depth)
  19360. (re (concat "^" (org-get-limited-outline-regexp)))
  19361. (subs (make-vector (1+ n) nil))
  19362. (last-level 0)
  19363. m level head)
  19364. (save-excursion
  19365. (save-restriction
  19366. (widen)
  19367. (goto-char (point-max))
  19368. (while (re-search-backward re nil t)
  19369. (setq level (org-reduced-level (funcall outline-level)))
  19370. (when (<= level n)
  19371. (looking-at org-complex-heading-regexp)
  19372. (setq head (org-link-display-format
  19373. (org-match-string-no-properties 4))
  19374. m (org-imenu-new-marker))
  19375. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19376. (if (>= level last-level)
  19377. (push (cons head m) (aref subs level))
  19378. (push (cons head (aref subs (1+ level))) (aref subs level))
  19379. (loop for i from (1+ level) to n do (aset subs i nil)))
  19380. (setq last-level level)))))
  19381. (aref subs 1)))
  19382. (eval-after-load "imenu"
  19383. '(progn
  19384. (add-hook 'imenu-after-jump-hook
  19385. (lambda ()
  19386. (if (eq major-mode 'org-mode)
  19387. (org-show-context 'org-goto))))))
  19388. (defun org-link-display-format (link)
  19389. "Replace a link with either the description, or the link target
  19390. if no description is present"
  19391. (save-match-data
  19392. (if (string-match org-bracket-link-analytic-regexp link)
  19393. (replace-match (if (match-end 5)
  19394. (match-string 5 link)
  19395. (concat (match-string 1 link)
  19396. (match-string 3 link)))
  19397. nil t link)
  19398. link)))
  19399. (defun org-toggle-link-display ()
  19400. "Toggle the literal or descriptive display of links."
  19401. (interactive)
  19402. (if org-descriptive-links
  19403. (progn (org-remove-from-invisibility-spec '(org-link))
  19404. (org-restart-font-lock)
  19405. (setq org-descriptive-links nil))
  19406. (progn (add-to-invisibility-spec '(org-link))
  19407. (org-restart-font-lock)
  19408. (setq org-descriptive-links t))))
  19409. ;; Speedbar support
  19410. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19411. "Overlay marking the agenda restriction line in speedbar.")
  19412. (overlay-put org-speedbar-restriction-lock-overlay
  19413. 'face 'org-agenda-restriction-lock)
  19414. (overlay-put org-speedbar-restriction-lock-overlay
  19415. 'help-echo "Agendas are currently limited to this item.")
  19416. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19417. (defun org-speedbar-set-agenda-restriction ()
  19418. "Restrict future agenda commands to the location at point in speedbar.
  19419. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19420. (interactive)
  19421. (require 'org-agenda)
  19422. (let (p m tp np dir txt)
  19423. (cond
  19424. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19425. 'org-imenu t))
  19426. (setq m (get-text-property p 'org-imenu-marker))
  19427. (with-current-buffer (marker-buffer m)
  19428. (goto-char m)
  19429. (org-agenda-set-restriction-lock 'subtree)))
  19430. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19431. 'speedbar-function 'speedbar-find-file))
  19432. (setq tp (previous-single-property-change
  19433. (1+ p) 'speedbar-function)
  19434. np (next-single-property-change
  19435. tp 'speedbar-function)
  19436. dir (speedbar-line-directory)
  19437. txt (buffer-substring-no-properties (or tp (point-min))
  19438. (or np (point-max))))
  19439. (with-current-buffer (find-file-noselect
  19440. (let ((default-directory dir))
  19441. (expand-file-name txt)))
  19442. (unless (eq major-mode 'org-mode)
  19443. (error "Cannot restrict to non-Org-mode file"))
  19444. (org-agenda-set-restriction-lock 'file)))
  19445. (t (error "Don't know how to restrict Org-mode's agenda")))
  19446. (move-overlay org-speedbar-restriction-lock-overlay
  19447. (point-at-bol) (point-at-eol))
  19448. (setq current-prefix-arg nil)
  19449. (org-agenda-maybe-redo)))
  19450. (eval-after-load "speedbar"
  19451. '(progn
  19452. (speedbar-add-supported-extension ".org")
  19453. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19454. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19455. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19456. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19457. (add-hook 'speedbar-visiting-tag-hook
  19458. (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
  19459. ;;; Fixes and Hacks for problems with other packages
  19460. ;; Make flyspell not check words in links, to not mess up our keymap
  19461. (defun org-mode-flyspell-verify ()
  19462. "Don't let flyspell put overlays at active buttons, or on
  19463. {todo,all-time,additional-option-like}-keywords."
  19464. (let ((pos (max (1- (point)) (point-min)))
  19465. (word (thing-at-point 'word)))
  19466. (and (not (get-text-property pos 'keymap))
  19467. (not (get-text-property pos 'org-no-flyspell))
  19468. (not (member word org-todo-keywords-1))
  19469. (not (member word org-all-time-keywords))
  19470. (not (member word org-additional-option-like-keywords)))))
  19471. (defun org-remove-flyspell-overlays-in (beg end)
  19472. "Remove flyspell overlays in region."
  19473. (and (org-bound-and-true-p flyspell-mode)
  19474. (fboundp 'flyspell-delete-region-overlays)
  19475. (flyspell-delete-region-overlays beg end))
  19476. (add-text-properties beg end '(org-no-flyspell t)))
  19477. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  19478. (eval-after-load "bookmark"
  19479. '(if (boundp 'bookmark-after-jump-hook)
  19480. ;; We can use the hook
  19481. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  19482. ;; Hook not available, use advice
  19483. (defadvice bookmark-jump (after org-make-visible activate)
  19484. "Make the position visible."
  19485. (org-bookmark-jump-unhide))))
  19486. ;; Make sure saveplace shows the location if it was hidden
  19487. (eval-after-load "saveplace"
  19488. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  19489. "Make the position visible."
  19490. (org-bookmark-jump-unhide)))
  19491. ;; Make sure ecb shows the location if it was hidden
  19492. (eval-after-load "ecb"
  19493. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  19494. "Make hierarchy visible when jumping into location from ECB tree buffer."
  19495. (if (eq major-mode 'org-mode)
  19496. (org-show-context))))
  19497. (defun org-bookmark-jump-unhide ()
  19498. "Unhide the current position, to show the bookmark location."
  19499. (and (eq major-mode 'org-mode)
  19500. (or (outline-invisible-p)
  19501. (save-excursion (goto-char (max (point-min) (1- (point))))
  19502. (outline-invisible-p)))
  19503. (org-show-context 'bookmark-jump)))
  19504. ;; Make session.el ignore our circular variable
  19505. (eval-after-load "session"
  19506. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  19507. ;;;; Experimental code
  19508. (defun org-closed-in-range ()
  19509. "Sparse tree of items closed in a certain time range.
  19510. Still experimental, may disappear in the future."
  19511. (interactive)
  19512. ;; Get the time interval from the user.
  19513. (let* ((time1 (org-float-time
  19514. (org-read-date nil 'to-time nil "Starting date: ")))
  19515. (time2 (org-float-time
  19516. (org-read-date nil 'to-time nil "End date:")))
  19517. ;; callback function
  19518. (callback (lambda ()
  19519. (let ((time
  19520. (org-float-time
  19521. (apply 'encode-time
  19522. (org-parse-time-string
  19523. (match-string 1))))))
  19524. ;; check if time in interval
  19525. (and (>= time time1) (<= time time2))))))
  19526. ;; make tree, check each match with the callback
  19527. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  19528. ;;;; Finish up
  19529. (provide 'org)
  19530. (run-hooks 'org-load-hook)
  19531. ;;; org.el ends here