org.el 700 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.36trans
  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. (unless (boundp 'calendar-view-holidays-initially-flag)
  74. (defvaralias 'calendar-view-holidays-initially-flag
  75. 'view-calendar-holidays-initially))
  76. (unless (boundp 'calendar-view-diary-initially-flag)
  77. (defvaralias 'calendar-view-diary-initially-flag
  78. 'view-diary-entries-initially))
  79. (unless (boundp 'diary-fancy-buffer)
  80. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  81. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  82. ;; the file noutline.el being loaded.
  83. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  84. ;; We require noutline, which might be provided in outline.el
  85. (require 'outline) (require 'noutline)
  86. ;; Other stuff we need.
  87. (require 'time-date)
  88. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  89. (require 'easymenu)
  90. (require 'overlay)
  91. (require 'org-macs)
  92. (require 'org-entities)
  93. (require 'org-compat)
  94. (require 'org-faces)
  95. (require 'org-list)
  96. (require 'org-src)
  97. (require 'org-footnote)
  98. ;;;; Customization variables
  99. ;;; Version
  100. (defconst org-version "6.36trans"
  101. "The version number of the file org.el.")
  102. (defun org-version (&optional here)
  103. "Show the org-mode version in the echo area.
  104. With prefix arg HERE, insert it at point."
  105. (interactive "P")
  106. (let* ((origin default-directory)
  107. (version org-version)
  108. (git-version)
  109. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  110. (when (and (file-exists-p (expand-file-name ".git" dir))
  111. (executable-find "git"))
  112. (unwind-protect
  113. (progn
  114. (cd dir)
  115. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  116. (with-current-buffer "*Shell Command Output*"
  117. (goto-char (point-min))
  118. (setq git-version (buffer-substring (point) (point-at-eol))))
  119. (subst-char-in-string ?- ?. git-version t)
  120. (when (string-match "\\S-"
  121. (shell-command-to-string
  122. "git diff-index --name-only HEAD --"))
  123. (setq git-version (concat git-version ".dirty")))
  124. (setq version (concat version " (" git-version ")"))))
  125. (cd origin)))
  126. (setq version (format "Org-mode version %s" version))
  127. (if here (insert version))
  128. (message version)))
  129. ;;; Compatibility constants
  130. ;;; The custom variables
  131. (defgroup org nil
  132. "Outline-based notes management and organizer."
  133. :tag "Org"
  134. :group 'outlines
  135. :group 'calendar)
  136. (defcustom org-mode-hook nil
  137. "Mode hook for Org-mode, run after the mode was turned on."
  138. :group 'org
  139. :type 'hook)
  140. (defcustom org-load-hook nil
  141. "Hook that is run after org.el has been loaded."
  142. :group 'org
  143. :type 'hook)
  144. (defvar org-modules) ; defined below
  145. (defvar org-modules-loaded nil
  146. "Have the modules been loaded already?")
  147. (defun org-load-modules-maybe (&optional force)
  148. "Load all extensions listed in `org-modules'."
  149. (when (or force (not org-modules-loaded))
  150. (mapc (lambda (ext)
  151. (condition-case nil (require ext)
  152. (error (message "Problems while trying to load feature `%s'" ext))))
  153. org-modules)
  154. (setq org-modules-loaded t)))
  155. (defun org-set-modules (var value)
  156. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  157. (set var value)
  158. (when (featurep 'org)
  159. (org-load-modules-maybe 'force)))
  160. (when (org-bound-and-true-p org-modules)
  161. (let ((a (member 'org-infojs org-modules)))
  162. (and a (setcar a 'org-jsinfo))))
  163. (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)
  164. "Modules that should always be loaded together with org.el.
  165. If a description starts with <C>, the file is not part of Emacs
  166. and loading it will require that you have downloaded and properly installed
  167. the org-mode distribution.
  168. You can also use this system to load external packages (i.e. neither Org
  169. core modules, nor modules from the CONTRIB directory). Just add symbols
  170. to the end of the list. If the package is called org-xyz.el, then you need
  171. to add the symbol `xyz', and the package must have a call to
  172. (provide 'org-xyz)"
  173. :group 'org
  174. :set 'org-set-modules
  175. :type
  176. '(set :greedy t
  177. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  178. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  179. (const :tag " crypt: Encryption of subtrees" org-crypt)
  180. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  181. (const :tag " docview: Links to doc-view buffers" org-docview)
  182. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  183. (const :tag " id: Global IDs for identifying entries" org-id)
  184. (const :tag " info: Links to Info nodes" org-info)
  185. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  186. (const :tag " habit: Track your consistency with habits" org-habit)
  187. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  188. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  189. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  190. (const :tag " mew Links to Mew folders/messages" org-mew)
  191. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  192. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  193. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  194. (const :tag " vm: Links to VM folders/messages" org-vm)
  195. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  196. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  197. (const :tag " mouse: Additional mouse support" org-mouse)
  198. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  199. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  200. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  201. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  202. (const :tag "C collector: Collect properties into tables" org-collector)
  203. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  204. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  205. (const :tag "C eval: Include command output as text" org-eval)
  206. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  207. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  208. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  209. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  210. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  211. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  212. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  213. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  214. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  215. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  216. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  217. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  218. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  219. (const :tag "C registry: A registry for Org-mode links" org-registry)
  220. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  221. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  222. (const :tag "C secretary: Team management with org-mode" org-secretary)
  223. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  224. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  225. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  226. (const :tag "C track: Keep up with Org-mode development" org-track)
  227. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  228. (defcustom org-support-shift-select nil
  229. "Non-nil means make shift-cursor commands select text when possible.
  230. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  231. selecting a region, or enlarge thusly regions started in this way.
  232. In Org-mode, in special contexts, these same keys are used for other
  233. purposes, important enough to compete with shift selection. Org tries
  234. to balance these needs by supporting `shift-select-mode' outside these
  235. special contexts, under control of this variable.
  236. The default of this variable is nil, to avoid confusing behavior. Shifted
  237. cursor keys will then execute Org commands in the following contexts:
  238. - on a headline, changing TODO state (left/right) and priority (up/down)
  239. - on a time stamp, changing the time
  240. - in a plain list item, changing the bullet type
  241. - in a property definition line, switching between allowed values
  242. - in the BEGIN line of a clock table (changing the time block).
  243. Outside these contexts, the commands will throw an error.
  244. When this variable is t and the cursor is not in a special context,
  245. Org-mode will support shift-selection for making and enlarging regions.
  246. To make this more effective, the bullet cycling will no longer happen
  247. anywhere in an item line, but only if the cursor is exactly on the bullet.
  248. If you set this variable to the symbol `always', then the keys
  249. will not be special in headlines, property lines, and item lines, to make
  250. shift selection work there as well. If this is what you want, you can
  251. use the following alternative commands: `C-c C-t' and `C-c ,' to
  252. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  253. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  254. edited by hand or in column view.
  255. However, when the cursor is on a timestamp, shift-cursor commands
  256. will still edit the time stamp - this is just too good to give up.
  257. XEmacs user should have this variable set to nil, because shift-select-mode
  258. is Emacs 23 only."
  259. :group 'org
  260. :type '(choice
  261. (const :tag "Never" nil)
  262. (const :tag "When outside special context" t)
  263. (const :tag "Everywhere except timestamps" always)))
  264. (defgroup org-startup nil
  265. "Options concerning startup of Org-mode."
  266. :tag "Org Startup"
  267. :group 'org)
  268. (defcustom org-startup-folded t
  269. "Non-nil means entering Org-mode will switch to OVERVIEW.
  270. This can also be configured on a per-file basis by adding one of
  271. the following lines anywhere in the buffer:
  272. #+STARTUP: fold (or `overview', this is equivalent)
  273. #+STARTUP: nofold (or `showall', this is equivalent)
  274. #+STARTUP: content
  275. #+STARTUP: showeverything"
  276. :group 'org-startup
  277. :type '(choice
  278. (const :tag "nofold: show all" nil)
  279. (const :tag "fold: overview" t)
  280. (const :tag "content: all headlines" content)
  281. (const :tag "show everything, even drawers" showeverything)))
  282. (defcustom org-startup-truncated t
  283. "Non-nil means entering Org-mode will set `truncate-lines'.
  284. This is useful since some lines containing links can be very long and
  285. uninteresting. Also tables look terrible when wrapped."
  286. :group 'org-startup
  287. :type 'boolean)
  288. (defcustom org-startup-indented nil
  289. "Non-nil means turn on `org-indent-mode' on startup.
  290. This can also be configured on a per-file basis by adding one of
  291. the following lines anywhere in the buffer:
  292. #+STARTUP: indent
  293. #+STARTUP: noindent"
  294. :group 'org-structure
  295. :type '(choice
  296. (const :tag "Not" nil)
  297. (const :tag "Globally (slow on startup in large files)" t)))
  298. (defcustom org-startup-with-beamer-mode nil
  299. "Non-nil means turn on `org-beamer-mode' on startup.
  300. This can also be configured on a per-file basis by adding one of
  301. the following lines anywhere in the buffer:
  302. #+STARTUP: beamer"
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-startup-align-all-tables nil
  306. "Non-nil means align all tables when visiting a file.
  307. This is useful when the column width in tables is forced with <N> cookies
  308. in table fields. Such tables will look correct only after the first re-align.
  309. This can also be configured on a per-file basis by adding one of
  310. the following lines anywhere in the buffer:
  311. #+STARTUP: align
  312. #+STARTUP: noalign"
  313. :group 'org-startup
  314. :type 'boolean)
  315. (defcustom org-insert-mode-line-in-empty-file nil
  316. "Non-nil means insert the first line setting Org-mode in empty files.
  317. When the function `org-mode' is called interactively in an empty file, this
  318. normally means that the file name does not automatically trigger Org-mode.
  319. To ensure that the file will always be in Org-mode in the future, a
  320. line enforcing Org-mode will be inserted into the buffer, if this option
  321. has been set."
  322. :group 'org-startup
  323. :type 'boolean)
  324. (defcustom org-replace-disputed-keys nil
  325. "Non-nil means use alternative key bindings for some keys.
  326. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  327. These keys are also used by other packages like shift-selection-mode'
  328. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  329. If you want to use Org-mode together with one of these other modes,
  330. or more generally if you would like to move some Org-mode commands to
  331. other keys, set this variable and configure the keys with the variable
  332. `org-disputed-keys'.
  333. This option is only relevant at load-time of Org-mode, and must be set
  334. *before* org.el is loaded. Changing it requires a restart of Emacs to
  335. become effective."
  336. :group 'org-startup
  337. :type 'boolean)
  338. (defcustom org-use-extra-keys nil
  339. "Non-nil means use extra key sequence definitions for certain
  340. commands. This happens automatically if you run XEmacs or if
  341. window-system is nil. This variable lets you do the same
  342. manually. You must set it before loading org.
  343. Example: on Carbon Emacs 22 running graphically, with an external
  344. keyboard on a Powerbook, the default way of setting M-left might
  345. not work for either Alt or ESC. Setting this variable will make
  346. it work for ESC."
  347. :group 'org-startup
  348. :type 'boolean)
  349. (if (fboundp 'defvaralias)
  350. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  351. (defcustom org-disputed-keys
  352. '(([(shift up)] . [(meta p)])
  353. ([(shift down)] . [(meta n)])
  354. ([(shift left)] . [(meta -)])
  355. ([(shift right)] . [(meta +)])
  356. ([(control shift right)] . [(meta shift +)])
  357. ([(control shift left)] . [(meta shift -)]))
  358. "Keys for which Org-mode and other modes compete.
  359. This is an alist, cars are the default keys, second element specifies
  360. the alternative to use when `org-replace-disputed-keys' is t.
  361. Keys can be specified in any syntax supported by `define-key'.
  362. The value of this option takes effect only at Org-mode's startup,
  363. therefore you'll have to restart Emacs to apply it after changing."
  364. :group 'org-startup
  365. :type 'alist)
  366. (defun org-key (key)
  367. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  368. Or return the original if not disputed.
  369. Also apply the trnaslations defined in `org-xemacs-key-equivalents'."
  370. (when org-replace-disputed-keys
  371. (let* ((nkey (key-description key))
  372. (x (org-find-if (lambda (x)
  373. (equal (key-description (car x)) nkey))
  374. org-disputed-keys)))
  375. (setq key (if x (cdr x) key))))
  376. (when (featurep 'xemacs)
  377. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  378. key)
  379. (defun org-find-if (predicate seq)
  380. (catch 'exit
  381. (while seq
  382. (if (funcall predicate (car seq))
  383. (throw 'exit (car seq))
  384. (pop seq)))))
  385. (defun org-defkey (keymap key def)
  386. "Define a key, possibly translated, as returned by `org-key'."
  387. (define-key keymap (org-key key) def))
  388. (defcustom org-ellipsis nil
  389. "The ellipsis to use in the Org-mode outline.
  390. When nil, just use the standard three dots. When a string, use that instead,
  391. When a face, use the standard 3 dots, but with the specified face.
  392. The change affects only Org-mode (which will then use its own display table).
  393. Changing this requires executing `M-x org-mode' in a buffer to become
  394. effective."
  395. :group 'org-startup
  396. :type '(choice (const :tag "Default" nil)
  397. (face :tag "Face" :value org-warning)
  398. (string :tag "String" :value "...#")))
  399. (defvar org-display-table nil
  400. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  401. (defgroup org-keywords nil
  402. "Keywords in Org-mode."
  403. :tag "Org Keywords"
  404. :group 'org)
  405. (defcustom org-deadline-string "DEADLINE:"
  406. "String to mark deadline entries.
  407. A deadline is this string, followed by a time stamp. Should be a word,
  408. terminated by a colon. You can insert a schedule keyword and
  409. a timestamp with \\[org-deadline].
  410. Changes become only effective after restarting Emacs."
  411. :group 'org-keywords
  412. :type 'string)
  413. (defcustom org-scheduled-string "SCHEDULED:"
  414. "String to mark scheduled TODO entries.
  415. A schedule is this string, followed by a time stamp. Should be a word,
  416. terminated by a colon. You can insert a schedule keyword and
  417. a timestamp with \\[org-schedule].
  418. Changes become only effective after restarting Emacs."
  419. :group 'org-keywords
  420. :type 'string)
  421. (defcustom org-closed-string "CLOSED:"
  422. "String used as the prefix for timestamps logging closing a TODO entry."
  423. :group 'org-keywords
  424. :type 'string)
  425. (defcustom org-clock-string "CLOCK:"
  426. "String used as prefix for timestamps clocking work hours on an item."
  427. :group 'org-keywords
  428. :type 'string)
  429. (defcustom org-comment-string "COMMENT"
  430. "Entries starting with this keyword will never be exported.
  431. An entry can be toggled between COMMENT and normal with
  432. \\[org-toggle-comment].
  433. Changes become only effective after restarting Emacs."
  434. :group 'org-keywords
  435. :type 'string)
  436. (defcustom org-quote-string "QUOTE"
  437. "Entries starting with this keyword will be exported in fixed-width font.
  438. Quoting applies only to the text in the entry following the headline, and does
  439. not extend beyond the next headline, even if that is lower level.
  440. An entry can be toggled between QUOTE and normal with
  441. \\[org-toggle-fixed-width-section]."
  442. :group 'org-keywords
  443. :type 'string)
  444. (defconst org-repeat-re
  445. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  446. "Regular expression for specifying repeated events.
  447. After a match, group 1 contains the repeat expression.")
  448. (defgroup org-structure nil
  449. "Options concerning the general structure of Org-mode files."
  450. :tag "Org Structure"
  451. :group 'org)
  452. (defgroup org-reveal-location nil
  453. "Options about how to make context of a location visible."
  454. :tag "Org Reveal Location"
  455. :group 'org-structure)
  456. (defconst org-context-choice
  457. '(choice
  458. (const :tag "Always" t)
  459. (const :tag "Never" nil)
  460. (repeat :greedy t :tag "Individual contexts"
  461. (cons
  462. (choice :tag "Context"
  463. (const agenda)
  464. (const org-goto)
  465. (const occur-tree)
  466. (const tags-tree)
  467. (const link-search)
  468. (const mark-goto)
  469. (const bookmark-jump)
  470. (const isearch)
  471. (const default))
  472. (boolean))))
  473. "Contexts for the reveal options.")
  474. (defcustom org-show-hierarchy-above '((default . t))
  475. "Non-nil means show full hierarchy when revealing a location.
  476. Org-mode often shows locations in an org-mode file which might have
  477. been invisible before. When this is set, the hierarchy of headings
  478. above the exposed location is shown.
  479. Turning this off for example for sparse trees makes them very compact.
  480. Instead of t, this can also be an alist specifying this option for different
  481. contexts. Valid contexts are
  482. agenda when exposing an entry from the agenda
  483. org-goto when using the command `org-goto' on key C-c C-j
  484. occur-tree when using the command `org-occur' on key C-c /
  485. tags-tree when constructing a sparse tree based on tags matches
  486. link-search when exposing search matches associated with a link
  487. mark-goto when exposing the jump goal of a mark
  488. bookmark-jump when exposing a bookmark location
  489. isearch when exiting from an incremental search
  490. default default for all contexts not set explicitly"
  491. :group 'org-reveal-location
  492. :type org-context-choice)
  493. (defcustom org-show-following-heading '((default . nil))
  494. "Non-nil means show following heading when revealing a location.
  495. Org-mode often shows locations in an org-mode file which might have
  496. been invisible before. When this is set, the heading following the
  497. match is shown.
  498. Turning this off for example for sparse trees makes them very compact,
  499. but makes it harder to edit the location of the match. In such a case,
  500. use the command \\[org-reveal] to show more context.
  501. Instead of t, this can also be an alist specifying this option for different
  502. contexts. See `org-show-hierarchy-above' for valid contexts."
  503. :group 'org-reveal-location
  504. :type org-context-choice)
  505. (defcustom org-show-siblings '((default . nil) (isearch t))
  506. "Non-nil means show all sibling heading when revealing a location.
  507. Org-mode often shows locations in an org-mode file which might have
  508. been invisible before. When this is set, the sibling of the current entry
  509. heading are all made visible. If `org-show-hierarchy-above' is t,
  510. the same happens on each level of the hierarchy above the current entry.
  511. By default this is on for the isearch context, off for all other contexts.
  512. Turning this off for example for sparse trees makes them very compact,
  513. but makes it harder to edit the location of the match. In such a case,
  514. use the command \\[org-reveal] to show more context.
  515. Instead of t, this can also be an alist specifying this option for different
  516. contexts. See `org-show-hierarchy-above' for valid contexts."
  517. :group 'org-reveal-location
  518. :type org-context-choice)
  519. (defcustom org-show-entry-below '((default . nil))
  520. "Non-nil means show the entry below a headline when revealing a location.
  521. Org-mode often shows locations in an org-mode file which might have
  522. been invisible before. When this is set, the text below the headline that is
  523. exposed is also shown.
  524. By default this is off for all contexts.
  525. Instead of t, this can also be an alist specifying this option for different
  526. contexts. See `org-show-hierarchy-above' for valid contexts."
  527. :group 'org-reveal-location
  528. :type org-context-choice)
  529. (defcustom org-indirect-buffer-display 'other-window
  530. "How should indirect tree buffers be displayed?
  531. This applies to indirect buffers created with the commands
  532. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  533. Valid values are:
  534. current-window Display in the current window
  535. other-window Just display in another window.
  536. dedicated-frame Create one new frame, and re-use it each time.
  537. new-frame Make a new frame each time. Note that in this case
  538. previously-made indirect buffers are kept, and you need to
  539. kill these buffers yourself."
  540. :group 'org-structure
  541. :group 'org-agenda-windows
  542. :type '(choice
  543. (const :tag "In current window" current-window)
  544. (const :tag "In current frame, other window" other-window)
  545. (const :tag "Each time a new frame" new-frame)
  546. (const :tag "One dedicated frame" dedicated-frame)))
  547. (defcustom org-use-speed-commands nil
  548. "Non-nil means activate single letter commands at beginning of a headline.
  549. This may also be a function to test for appropriate locations where speed
  550. commands should be active."
  551. :group 'org-structure
  552. :type '(choice
  553. (const :tag "Never" nil)
  554. (const :tag "At beginning of headline stars" t)
  555. (function)))
  556. (defcustom org-speed-commands-user nil
  557. "Alist of additional speed commands.
  558. This list will be checked before `org-speed-commands-default'
  559. when the variable `org-use-speed-commands' is non-nil
  560. and when the cursor is at the beginning of a headline.
  561. The car if each entry is a string with a single letter, which must
  562. be assigned to `self-insert-command' in the global map.
  563. The cdr is either a command to be called interactively, a function
  564. to be called, or a form to be evaluated.
  565. An entry that is just a list with a single string will be interpreted
  566. as a descriptive headline that will be added when listing the speed
  567. copmmands in the Help buffer using the `?' speed command."
  568. :group 'org-structure
  569. :type '(repeat :value ("k" . ignore)
  570. (choice :value ("k" . ignore)
  571. (list :tag "Descriptive Headline" (string :tag "Headline"))
  572. (cons :tag "Letter and Command"
  573. (string :tag "Command letter")
  574. (choice
  575. (function)
  576. (sexp))))))
  577. (defgroup org-cycle nil
  578. "Options concerning visibility cycling in Org-mode."
  579. :tag "Org Cycle"
  580. :group 'org-structure)
  581. (defcustom org-cycle-skip-children-state-if-no-children t
  582. "Non-nil means skip CHILDREN state in entries that don't have any."
  583. :group 'org-cycle
  584. :type 'boolean)
  585. (defcustom org-cycle-max-level nil
  586. "Maximum level which should still be subject to visibility cycling.
  587. Levels higher than this will, for cycling, be treated as text, not a headline.
  588. When `org-odd-levels-only' is set, a value of N in this variable actually
  589. means 2N-1 stars as the limiting headline.
  590. When nil, cycle all levels.
  591. Note that the limiting level of cycling is also influenced by
  592. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  593. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  594. than its value."
  595. :group 'org-cycle
  596. :type '(choice
  597. (const :tag "No limit" nil)
  598. (integer :tag "Maximum level")))
  599. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  600. "Names of drawers. Drawers are not opened by cycling on the headline above.
  601. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  602. this:
  603. :DRAWERNAME:
  604. .....
  605. :END:
  606. The drawer \"PROPERTIES\" is special for capturing properties through
  607. the property API.
  608. Drawers can be defined on the per-file basis with a line like:
  609. #+DRAWERS: HIDDEN STATE PROPERTIES"
  610. :group 'org-structure
  611. :group 'org-cycle
  612. :type '(repeat (string :tag "Drawer Name")))
  613. (defcustom org-hide-block-startup nil
  614. "Non-nil means entering Org-mode will fold all blocks.
  615. This can also be set in on a per-file basis with
  616. #+STARTUP: hideblocks
  617. #+STARTUP: showblocks"
  618. :group 'org-startup
  619. :group 'org-cycle
  620. :type 'boolean)
  621. (defcustom org-cycle-global-at-bob nil
  622. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  623. This makes it possible to do global cycling without having to use S-TAB or
  624. C-u TAB. For this special case to work, the first line of the buffer
  625. must not be a headline - it may be empty or some other text. When used in
  626. this way, `org-cycle-hook' is disables temporarily, to make sure the
  627. cursor stays at the beginning of the buffer.
  628. When this option is nil, don't do anything special at the beginning
  629. of the buffer."
  630. :group 'org-cycle
  631. :type 'boolean)
  632. (defcustom org-cycle-level-after-item/entry-creation t
  633. "Non-nil means cycle entry level or item indentation in new empty entries.
  634. When the cursor is at the end of an empty headline, i.e with only stars
  635. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  636. and then all possible anchestor states, before returning to the original state.
  637. This makes data entry extremely fast: M-RET to create a new headline,
  638. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  639. When the cursor is at the end of an empty plain list item, one TAB will
  640. make it a subitem, two or more tabs will back up to make this an item
  641. higher up in the item hierarchy."
  642. :group 'org-cycle
  643. :type 'boolean)
  644. (defcustom org-cycle-emulate-tab t
  645. "Where should `org-cycle' emulate TAB.
  646. nil Never
  647. white Only in completely white lines
  648. whitestart Only at the beginning of lines, before the first non-white char
  649. t Everywhere except in headlines
  650. exc-hl-bol Everywhere except at the start of a headline
  651. If TAB is used in a place where it does not emulate TAB, the current subtree
  652. visibility is cycled."
  653. :group 'org-cycle
  654. :type '(choice (const :tag "Never" nil)
  655. (const :tag "Only in completely white lines" white)
  656. (const :tag "Before first char in a line" whitestart)
  657. (const :tag "Everywhere except in headlines" t)
  658. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  659. ))
  660. (defcustom org-cycle-separator-lines 2
  661. "Number of empty lines needed to keep an empty line between collapsed trees.
  662. If you leave an empty line between the end of a subtree and the following
  663. headline, this empty line is hidden when the subtree is folded.
  664. Org-mode will leave (exactly) one empty line visible if the number of
  665. empty lines is equal or larger to the number given in this variable.
  666. So the default 2 means at least 2 empty lines after the end of a subtree
  667. are needed to produce free space between a collapsed subtree and the
  668. following headline.
  669. If the number is negative, and the number of empty lines is at least -N,
  670. all empty lines are shown.
  671. Special case: when 0, never leave empty lines in collapsed view."
  672. :group 'org-cycle
  673. :type 'integer)
  674. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  675. (defcustom org-pre-cycle-hook nil
  676. "Hook that is run before visibility cycling is happening.
  677. The function(s) in this hook must accept a single argument which indicates
  678. the new state that will be set right after running this hook. The
  679. argument is a symbol. Before a global state change, it can have the values
  680. `overview', `content', or `all'. Before a local state change, it can have
  681. the values `folded', `children', or `subtree'."
  682. :group 'org-cycle
  683. :type 'hook)
  684. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  685. org-cycle-hide-drawers
  686. org-cycle-show-empty-lines
  687. org-optimize-window-after-visibility-change)
  688. "Hook that is run after `org-cycle' has changed the buffer visibility.
  689. The function(s) in this hook must accept a single argument which indicates
  690. the new state that was set by the most recent `org-cycle' command. The
  691. argument is a symbol. After a global state change, it can have the values
  692. `overview', `content', or `all'. After a local state change, it can have
  693. the values `folded', `children', or `subtree'."
  694. :group 'org-cycle
  695. :type 'hook)
  696. (defgroup org-edit-structure nil
  697. "Options concerning structure editing in Org-mode."
  698. :tag "Org Edit Structure"
  699. :group 'org-structure)
  700. (defcustom org-odd-levels-only nil
  701. "Non-nil means skip even levels and only use odd levels for the outline.
  702. This has the effect that two stars are being added/taken away in
  703. promotion/demotion commands. It also influences how levels are
  704. handled by the exporters.
  705. Changing it requires restart of `font-lock-mode' to become effective
  706. for fontification also in regions already fontified.
  707. You may also set this on a per-file basis by adding one of the following
  708. lines to the buffer:
  709. #+STARTUP: odd
  710. #+STARTUP: oddeven"
  711. :group 'org-edit-structure
  712. :group 'org-appearance
  713. :type 'boolean)
  714. (defcustom org-adapt-indentation t
  715. "Non-nil means adapt indentation to outline node level.
  716. When this variable is set, Org assumes that you write outlines by
  717. indenting text in each node to align with the headline (after the stars).
  718. The following issues are influenced by this variable:
  719. - When this is set and the *entire* text in an entry is indented, the
  720. indentation is increased by one space in a demotion command, and
  721. decreased by one in a promotion command. If any line in the entry
  722. body starts with text at column 0, indentation is not changed at all.
  723. - Property drawers and planning information is inserted indented when
  724. this variable s set. When nil, they will not be indented.
  725. - TAB indents a line relative to context. The lines below a headline
  726. will be indented when this variable is set.
  727. Note that this is all about true indentation, by adding and removing
  728. space characters. See also `org-indent.el' which does level-dependent
  729. indentation in a virtual way, i.e. at display time in Emacs."
  730. :group 'org-edit-structure
  731. :type 'boolean)
  732. (defcustom org-special-ctrl-a/e nil
  733. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  734. When t, `C-a' will bring back the cursor to the beginning of the
  735. headline text, i.e. after the stars and after a possible TODO keyword.
  736. In an item, this will be the position after the bullet.
  737. When the cursor is already at that position, another `C-a' will bring
  738. it to the beginning of the line.
  739. `C-e' will jump to the end of the headline, ignoring the presence of tags
  740. in the headline. A second `C-e' will then jump to the true end of the
  741. line, after any tags. This also means that, when this variable is
  742. non-nil, `C-e' also will never jump beyond the end of the heading of a
  743. folded section, i.e. not after the ellipses.
  744. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  745. going to the true line boundary first. Only a directly following, identical
  746. keypress will bring the cursor to the special positions.
  747. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  748. set separately."
  749. :group 'org-edit-structure
  750. :type '(choice
  751. (const :tag "off" nil)
  752. (const :tag "on: after stars/bullet and before tags first" t)
  753. (const :tag "reversed: true line boundary first" reversed)
  754. (cons :tag "Set C-a and C-e separately"
  755. (choice :tag "Special C-a"
  756. (const :tag "off" nil)
  757. (const :tag "on: after stars/bullet first" t)
  758. (const :tag "reversed: before stars/bullet first" reversed))
  759. (choice :tag "Special C-e"
  760. (const :tag "off" nil)
  761. (const :tag "on: before tags first" t)
  762. (const :tag "reversed: after tags first" reversed)))))
  763. (if (fboundp 'defvaralias)
  764. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  765. (defcustom org-special-ctrl-k nil
  766. "Non-nil means `C-k' will behave specially in headlines.
  767. When nil, `C-k' will call the default `kill-line' command.
  768. When t, the following will happen while the cursor is in the headline:
  769. - When the cursor is at the beginning of a headline, kill the entire
  770. line and possible the folded subtree below the line.
  771. - When in the middle of the headline text, kill the headline up to the tags.
  772. - When after the headline text, kill the tags."
  773. :group 'org-edit-structure
  774. :type 'boolean)
  775. (defcustom org-yank-folded-subtrees t
  776. "Non-nil means when yanking subtrees, fold them.
  777. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  778. it starts with a heading and all other headings in it are either children
  779. or siblings, then fold all the subtrees. However, do this only if no
  780. text after the yank would be swallowed into a folded tree by this action."
  781. :group 'org-edit-structure
  782. :type 'boolean)
  783. (defcustom org-yank-adjusted-subtrees nil
  784. "Non-nil means when yanking subtrees, adjust the level.
  785. With this setting, `org-paste-subtree' is used to insert the subtree, see
  786. this function for details."
  787. :group 'org-edit-structure
  788. :type 'boolean)
  789. (defcustom org-M-RET-may-split-line '((default . t))
  790. "Non-nil means M-RET will split the line at the cursor position.
  791. When nil, it will go to the end of the line before making a
  792. new line.
  793. You may also set this option in a different way for different
  794. contexts. Valid contexts are:
  795. headline when creating a new headline
  796. item when creating a new item
  797. table in a table field
  798. default the value to be used for all contexts not explicitly
  799. customized"
  800. :group 'org-structure
  801. :group 'org-table
  802. :type '(choice
  803. (const :tag "Always" t)
  804. (const :tag "Never" nil)
  805. (repeat :greedy t :tag "Individual contexts"
  806. (cons
  807. (choice :tag "Context"
  808. (const headline)
  809. (const item)
  810. (const table)
  811. (const default))
  812. (boolean)))))
  813. (defcustom org-insert-heading-respect-content nil
  814. "Non-nil means insert new headings after the current subtree.
  815. When nil, the new heading is created directly after the current line.
  816. The commands \\[org-insert-heading-respect-content] and
  817. \\[org-insert-todo-heading-respect-content] turn this variable on
  818. for the duration of the command."
  819. :group 'org-structure
  820. :type 'boolean)
  821. (defcustom org-blank-before-new-entry '((heading . auto)
  822. (plain-list-item . auto))
  823. "Should `org-insert-heading' leave a blank line before new heading/item?
  824. The value is an alist, with `heading' and `plain-list-item' as car,
  825. and a boolean flag as cdr. For plain lists, if the variable
  826. `org-empty-line-terminates-plain-lists' is set, the setting here
  827. is ignored and no empty line is inserted, to keep the list in tact."
  828. :group 'org-edit-structure
  829. :type '(list
  830. (cons (const heading)
  831. (choice (const :tag "Never" nil)
  832. (const :tag "Always" t)
  833. (const :tag "Auto" auto)))
  834. (cons (const plain-list-item)
  835. (choice (const :tag "Never" nil)
  836. (const :tag "Always" t)
  837. (const :tag "Auto" auto)))))
  838. (defcustom org-insert-heading-hook nil
  839. "Hook being run after inserting a new heading."
  840. :group 'org-edit-structure
  841. :type 'hook)
  842. (defcustom org-enable-fixed-width-editor t
  843. "Non-nil means lines starting with \":\" are treated as fixed-width.
  844. This currently only means they are never auto-wrapped.
  845. When nil, such lines will be treated like ordinary lines.
  846. See also the QUOTE keyword."
  847. :group 'org-edit-structure
  848. :type 'boolean)
  849. (defcustom org-goto-auto-isearch t
  850. "Non-nil means typing characters in org-goto starts incremental search."
  851. :group 'org-edit-structure
  852. :type 'boolean)
  853. (defgroup org-sparse-trees nil
  854. "Options concerning sparse trees in Org-mode."
  855. :tag "Org Sparse Trees"
  856. :group 'org-structure)
  857. (defcustom org-highlight-sparse-tree-matches t
  858. "Non-nil means highlight all matches that define a sparse tree.
  859. The highlights will automatically disappear the next time the buffer is
  860. changed by an edit command."
  861. :group 'org-sparse-trees
  862. :type 'boolean)
  863. (defcustom org-remove-highlights-with-change t
  864. "Non-nil means any change to the buffer will remove temporary highlights.
  865. Such highlights are created by `org-occur' and `org-clock-display'.
  866. When nil, `C-c C-c needs to be used to get rid of the highlights.
  867. The highlights created by `org-preview-latex-fragment' always need
  868. `C-c C-c' to be removed."
  869. :group 'org-sparse-trees
  870. :group 'org-time
  871. :type 'boolean)
  872. (defcustom org-occur-hook '(org-first-headline-recenter)
  873. "Hook that is run after `org-occur' has constructed a sparse tree.
  874. This can be used to recenter the window to show as much of the structure
  875. as possible."
  876. :group 'org-sparse-trees
  877. :type 'hook)
  878. (defgroup org-imenu-and-speedbar nil
  879. "Options concerning imenu and speedbar in Org-mode."
  880. :tag "Org Imenu and Speedbar"
  881. :group 'org-structure)
  882. (defcustom org-imenu-depth 2
  883. "The maximum level for Imenu access to Org-mode headlines.
  884. This also applied for speedbar access."
  885. :group 'org-imenu-and-speedbar
  886. :type 'integer)
  887. (defgroup org-table nil
  888. "Options concerning tables in Org-mode."
  889. :tag "Org Table"
  890. :group 'org)
  891. (defcustom org-enable-table-editor 'optimized
  892. "Non-nil means lines starting with \"|\" are handled by the table editor.
  893. When nil, such lines will be treated like ordinary lines.
  894. When equal to the symbol `optimized', the table editor will be optimized to
  895. do the following:
  896. - Automatic overwrite mode in front of whitespace in table fields.
  897. This makes the structure of the table stay in tact as long as the edited
  898. field does not exceed the column width.
  899. - Minimize the number of realigns. Normally, the table is aligned each time
  900. TAB or RET are pressed to move to another field. With optimization this
  901. happens only if changes to a field might have changed the column width.
  902. Optimization requires replacing the functions `self-insert-command',
  903. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  904. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  905. very good at guessing when a re-align will be necessary, but you can always
  906. force one with \\[org-ctrl-c-ctrl-c].
  907. If you would like to use the optimized version in Org-mode, but the
  908. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  909. This variable can be used to turn on and off the table editor during a session,
  910. but in order to toggle optimization, a restart is required.
  911. See also the variable `org-table-auto-blank-field'."
  912. :group 'org-table
  913. :type '(choice
  914. (const :tag "off" nil)
  915. (const :tag "on" t)
  916. (const :tag "on, optimized" optimized)))
  917. (defcustom org-self-insert-cluster-for-undo t
  918. "Non-nil means cluster self-insert commands for undo when possible.
  919. If this is set, then, like in the Emacs command loop, 20 consecutive
  920. characters will be undone together.
  921. This is configurable, because there is some impact on typing performance."
  922. :group 'org-table
  923. :type 'boolean)
  924. (defcustom org-table-tab-recognizes-table.el t
  925. "Non-nil means TAB will automatically notice a table.el table.
  926. When it sees such a table, it moves point into it and - if necessary -
  927. calls `table-recognize-table'."
  928. :group 'org-table-editing
  929. :type 'boolean)
  930. (defgroup org-link nil
  931. "Options concerning links in Org-mode."
  932. :tag "Org Link"
  933. :group 'org)
  934. (defvar org-link-abbrev-alist-local nil
  935. "Buffer-local version of `org-link-abbrev-alist', which see.
  936. The value of this is taken from the #+LINK lines.")
  937. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  938. (defcustom org-link-abbrev-alist nil
  939. "Alist of link abbreviations.
  940. The car of each element is a string, to be replaced at the start of a link.
  941. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  942. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  943. [[linkkey:tag][description]]
  944. The 'linkkey' must be a word word, starting with a letter, followed
  945. by letters, numbers, '-' or '_'.
  946. If REPLACE is a string, the tag will simply be appended to create the link.
  947. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  948. the placeholder \"%h\" will cause a url-encoded version of the tag to
  949. be inserted at that point (see the function `url-hexify-string').
  950. REPLACE may also be a function that will be called with the tag as the
  951. only argument to create the link, which should be returned as a string.
  952. See the manual for examples."
  953. :group 'org-link
  954. :type '(repeat
  955. (cons
  956. (string :tag "Protocol")
  957. (choice
  958. (string :tag "Format")
  959. (function)))))
  960. (defcustom org-descriptive-links t
  961. "Non-nil means hide link part and only show description of bracket links.
  962. Bracket links are like [[link][description]]. This variable sets the initial
  963. state in new org-mode buffers. The setting can then be toggled on a
  964. per-buffer basis from the Org->Hyperlinks menu."
  965. :group 'org-link
  966. :type 'boolean)
  967. (defcustom org-link-file-path-type 'adaptive
  968. "How the path name in file links should be stored.
  969. Valid values are:
  970. relative Relative to the current directory, i.e. the directory of the file
  971. into which the link is being inserted.
  972. absolute Absolute path, if possible with ~ for home directory.
  973. noabbrev Absolute path, no abbreviation of home directory.
  974. adaptive Use relative path for files in the current directory and sub-
  975. directories of it. For other files, use an absolute path."
  976. :group 'org-link
  977. :type '(choice
  978. (const relative)
  979. (const absolute)
  980. (const noabbrev)
  981. (const adaptive)))
  982. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  983. "Types of links that should be activated in Org-mode files.
  984. This is a list of symbols, each leading to the activation of a certain link
  985. type. In principle, it does not hurt to turn on most link types - there may
  986. be a small gain when turning off unused link types. The types are:
  987. bracket The recommended [[link][description]] or [[link]] links with hiding.
  988. angular Links in angular brackets that may contain whitespace like
  989. <bbdb:Carsten Dominik>.
  990. plain Plain links in normal text, no whitespace, like http://google.com.
  991. radio Text that is matched by a radio target, see manual for details.
  992. tag Tag settings in a headline (link to tag search).
  993. date Time stamps (link to calendar).
  994. footnote Footnote labels.
  995. Changing this variable requires a restart of Emacs to become effective."
  996. :group 'org-link
  997. :type '(set :greedy t
  998. (const :tag "Double bracket links (new style)" bracket)
  999. (const :tag "Angular bracket links (old style)" angular)
  1000. (const :tag "Plain text links" plain)
  1001. (const :tag "Radio target matches" radio)
  1002. (const :tag "Tags" tag)
  1003. (const :tag "Timestamps" date)
  1004. (const :tag "Footnotes" footnote)))
  1005. (defcustom org-make-link-description-function nil
  1006. "Function to use to generate link descriptions from links. If
  1007. nil the link location will be used. This function must take two
  1008. parameters; the first is the link and the second the description
  1009. org-insert-link has generated, and should return the description
  1010. to use."
  1011. :group 'org-link
  1012. :type 'function)
  1013. (defgroup org-link-store nil
  1014. "Options concerning storing links in Org-mode."
  1015. :tag "Org Store Link"
  1016. :group 'org-link)
  1017. (defcustom org-email-link-description-format "Email %c: %.30s"
  1018. "Format of the description part of a link to an email or usenet message.
  1019. The following %-escapes will be replaced by corresponding information:
  1020. %F full \"From\" field
  1021. %f name, taken from \"From\" field, address if no name
  1022. %T full \"To\" field
  1023. %t first name in \"To\" field, address if no name
  1024. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1025. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1026. %s subject
  1027. %m message-id.
  1028. You may use normal field width specification between the % and the letter.
  1029. This is for example useful to limit the length of the subject.
  1030. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1031. :group 'org-link-store
  1032. :type 'string)
  1033. (defcustom org-from-is-user-regexp
  1034. (let (r1 r2)
  1035. (when (and user-mail-address (not (string= user-mail-address "")))
  1036. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1037. (when (and user-full-name (not (string= user-full-name "")))
  1038. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1039. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1040. "Regexp matched against the \"From:\" header of an email or usenet message.
  1041. It should match if the message is from the user him/herself."
  1042. :group 'org-link-store
  1043. :type 'regexp)
  1044. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1045. "Non-nil means storing a link to an Org file will use entry IDs.
  1046. Note that before this variable is even considered, org-id must be loaded,
  1047. so please customize `org-modules' and turn it on.
  1048. The variable can have the following values:
  1049. t Create an ID if needed to make a link to the current entry.
  1050. create-if-interactive
  1051. If `org-store-link' is called directly (interactively, as a user
  1052. command), do create an ID to support the link. But when doing the
  1053. job for remember, only use the ID if it already exists. The
  1054. purpose of this setting is to avoid proliferation of unwanted
  1055. IDs, just because you happen to be in an Org file when you
  1056. call `org-remember' that automatically and preemptively
  1057. creates a link. If you do want to get an ID link in a remember
  1058. template to an entry not having an ID, create it first by
  1059. explicitly creating a link to it, using `C-c C-l' first.
  1060. create-if-interactive-and-no-custom-id
  1061. Like create-if-interactive, but do not create an ID if there is
  1062. a CUSTOM_ID property defined in the entry. This is the default.
  1063. use-existing
  1064. Use existing ID, do not create one.
  1065. nil Never use an ID to make a link, instead link using a text search for
  1066. the headline text."
  1067. :group 'org-link-store
  1068. :type '(choice
  1069. (const :tag "Create ID to make link" t)
  1070. (const :tag "Create if storing link interactively"
  1071. create-if-interactive)
  1072. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1073. create-if-interactive-and-no-custom-id)
  1074. (const :tag "Only use existing" use-existing)
  1075. (const :tag "Do not use ID to create link" nil)))
  1076. (defcustom org-context-in-file-links t
  1077. "Non-nil means file links from `org-store-link' contain context.
  1078. A search string will be added to the file name with :: as separator and
  1079. used to find the context when the link is activated by the command
  1080. `org-open-at-point'.
  1081. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1082. negates this setting for the duration of the command."
  1083. :group 'org-link-store
  1084. :type 'boolean)
  1085. (defcustom org-keep-stored-link-after-insertion nil
  1086. "Non-nil means keep link in list for entire session.
  1087. The command `org-store-link' adds a link pointing to the current
  1088. location to an internal list. These links accumulate during a session.
  1089. The command `org-insert-link' can be used to insert links into any
  1090. Org-mode file (offering completion for all stored links). When this
  1091. option is nil, every link which has been inserted once using \\[org-insert-link]
  1092. will be removed from the list, to make completing the unused links
  1093. more efficient."
  1094. :group 'org-link-store
  1095. :type 'boolean)
  1096. (defgroup org-link-follow nil
  1097. "Options concerning following links in Org-mode."
  1098. :tag "Org Follow Link"
  1099. :group 'org-link)
  1100. (defcustom org-link-translation-function nil
  1101. "Function to translate links with different syntax to Org syntax.
  1102. This can be used to translate links created for example by the Planner
  1103. or emacs-wiki packages to Org syntax.
  1104. The function must accept two parameters, a TYPE containing the link
  1105. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1106. which is everything after the link protocol. It should return a cons
  1107. with possibly modified values of type and path.
  1108. Org contains a function for this, so if you set this variable to
  1109. `org-translate-link-from-planner', you should be able follow many
  1110. links created by planner."
  1111. :group 'org-link-follow
  1112. :type 'function)
  1113. (defcustom org-follow-link-hook nil
  1114. "Hook that is run after a link has been followed."
  1115. :group 'org-link-follow
  1116. :type 'hook)
  1117. (defcustom org-tab-follows-link nil
  1118. "Non-nil means on links TAB will follow the link.
  1119. Needs to be set before org.el is loaded.
  1120. This really should not be used, it does not make sense, and the
  1121. implementation is bad."
  1122. :group 'org-link-follow
  1123. :type 'boolean)
  1124. (defcustom org-return-follows-link nil
  1125. "Non-nil means on links RET will follow the link."
  1126. :group 'org-link-follow
  1127. :type 'boolean)
  1128. (defcustom org-mouse-1-follows-link
  1129. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1130. "Non-nil means mouse-1 on a link will follow the link.
  1131. A longer mouse click will still set point. Does not work on XEmacs.
  1132. Needs to be set before org.el is loaded."
  1133. :group 'org-link-follow
  1134. :type 'boolean)
  1135. (defcustom org-mark-ring-length 4
  1136. "Number of different positions to be recorded in the ring
  1137. Changing this requires a restart of Emacs to work correctly."
  1138. :group 'org-link-follow
  1139. :type 'integer)
  1140. (defcustom org-link-frame-setup
  1141. '((vm . vm-visit-folder-other-frame)
  1142. (gnus . gnus-other-frame)
  1143. (file . find-file-other-window))
  1144. "Setup the frame configuration for following links.
  1145. When following a link with Emacs, it may often be useful to display
  1146. this link in another window or frame. This variable can be used to
  1147. set this up for the different types of links.
  1148. For VM, use any of
  1149. `vm-visit-folder'
  1150. `vm-visit-folder-other-frame'
  1151. For Gnus, use any of
  1152. `gnus'
  1153. `gnus-other-frame'
  1154. `org-gnus-no-new-news'
  1155. For FILE, use any of
  1156. `find-file'
  1157. `find-file-other-window'
  1158. `find-file-other-frame'
  1159. For the calendar, use the variable `calendar-setup'.
  1160. For BBDB, it is currently only possible to display the matches in
  1161. another window."
  1162. :group 'org-link-follow
  1163. :type '(list
  1164. (cons (const vm)
  1165. (choice
  1166. (const vm-visit-folder)
  1167. (const vm-visit-folder-other-window)
  1168. (const vm-visit-folder-other-frame)))
  1169. (cons (const gnus)
  1170. (choice
  1171. (const gnus)
  1172. (const gnus-other-frame)
  1173. (const org-gnus-no-new-news)))
  1174. (cons (const file)
  1175. (choice
  1176. (const find-file)
  1177. (const find-file-other-window)
  1178. (const find-file-other-frame)))))
  1179. (defcustom org-display-internal-link-with-indirect-buffer nil
  1180. "Non-nil means use indirect buffer to display infile links.
  1181. Activating internal links (from one location in a file to another location
  1182. in the same file) normally just jumps to the location. When the link is
  1183. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1184. another window. When this option is set, the other window actually displays
  1185. an indirect buffer clone of the current buffer, to avoid any visibility
  1186. changes to the current buffer."
  1187. :group 'org-link-follow
  1188. :type 'boolean)
  1189. (defcustom org-open-non-existing-files nil
  1190. "Non-nil means `org-open-file' will open non-existing files.
  1191. When nil, an error will be generated.
  1192. This variable applies only to external applications because they
  1193. might choke on non-existing files. If the link is to a file that
  1194. will be opened in Emacs, the variable is ignored."
  1195. :group 'org-link-follow
  1196. :type 'boolean)
  1197. (defcustom org-open-directory-means-index-dot-org nil
  1198. "Non-nil means a link to a directory really means to index.org.
  1199. When nil, following a directory link will run dired or open a finder/explorer
  1200. window on that directory."
  1201. :group 'org-link-follow
  1202. :type 'boolean)
  1203. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1204. "Function and arguments to call for following mailto links.
  1205. This is a list with the first element being a lisp function, and the
  1206. remaining elements being arguments to the function. In string arguments,
  1207. %a will be replaced by the address, and %s will be replaced by the subject
  1208. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1209. :group 'org-link-follow
  1210. :type '(choice
  1211. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1212. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1213. (const :tag "message-mail" (message-mail "%a" "%s"))
  1214. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1215. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1216. "Non-nil means ask for confirmation before executing shell links.
  1217. Shell links can be dangerous: just think about a link
  1218. [[shell:rm -rf ~/*][Google Search]]
  1219. This link would show up in your Org-mode document as \"Google Search\",
  1220. but really it would remove your entire home directory.
  1221. Therefore we advise against setting this variable to nil.
  1222. Just change it to `y-or-n-p' if you want to confirm with a
  1223. single keystroke rather than having to type \"yes\"."
  1224. :group 'org-link-follow
  1225. :type '(choice
  1226. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1227. (const :tag "with y-or-n (faster)" y-or-n-p)
  1228. (const :tag "no confirmation (dangerous)" nil)))
  1229. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1230. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1231. Elisp links can be dangerous: just think about a link
  1232. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1233. This link would show up in your Org-mode document as \"Google Search\",
  1234. but really it would remove your entire home directory.
  1235. Therefore we advise against setting this variable to nil.
  1236. Just change it to `y-or-n-p' if you want to confirm with a
  1237. single keystroke rather than having to type \"yes\"."
  1238. :group 'org-link-follow
  1239. :type '(choice
  1240. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1241. (const :tag "with y-or-n (faster)" y-or-n-p)
  1242. (const :tag "no confirmation (dangerous)" nil)))
  1243. (defconst org-file-apps-defaults-gnu
  1244. '((remote . emacs)
  1245. (system . mailcap)
  1246. (t . mailcap))
  1247. "Default file applications on a UNIX or GNU/Linux system.
  1248. See `org-file-apps'.")
  1249. (defconst org-file-apps-defaults-macosx
  1250. '((remote . emacs)
  1251. (t . "open %s")
  1252. (system . "open %s")
  1253. ("ps.gz" . "gv %s")
  1254. ("eps.gz" . "gv %s")
  1255. ("dvi" . "xdvi %s")
  1256. ("fig" . "xfig %s"))
  1257. "Default file applications on a MacOS X system.
  1258. The system \"open\" is known as a default, but we use X11 applications
  1259. for some files for which the OS does not have a good default.
  1260. See `org-file-apps'.")
  1261. (defconst org-file-apps-defaults-windowsnt
  1262. (list
  1263. '(remote . emacs)
  1264. (cons t
  1265. (list (if (featurep 'xemacs)
  1266. 'mswindows-shell-execute
  1267. 'w32-shell-execute)
  1268. "open" 'file))
  1269. (cons 'system
  1270. (list (if (featurep 'xemacs)
  1271. 'mswindows-shell-execute
  1272. 'w32-shell-execute)
  1273. "open" 'file)))
  1274. "Default file applications on a Windows NT system.
  1275. The system \"open\" is used for most files.
  1276. See `org-file-apps'.")
  1277. (defcustom org-file-apps
  1278. '(
  1279. (auto-mode . emacs)
  1280. ("\\.mm\\'" . default)
  1281. ("\\.x?html?\\'" . default)
  1282. ("\\.pdf\\'" . default)
  1283. )
  1284. "External applications for opening `file:path' items in a document.
  1285. Org-mode uses system defaults for different file types, but
  1286. you can use this variable to set the application for a given file
  1287. extension. The entries in this list are cons cells where the car identifies
  1288. files and the cdr the corresponding command. Possible values for the
  1289. file identifier are
  1290. \"regex\" Regular expression matched against the file name. For backward
  1291. compatibility, this can also be a string with only alphanumeric
  1292. characters, which is then interpreted as an extension.
  1293. `directory' Matches a directory
  1294. `remote' Matches a remote file, accessible through tramp or efs.
  1295. Remote files most likely should be visited through Emacs
  1296. because external applications cannot handle such paths.
  1297. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1298. so all files Emacs knows how to handle. Using this with
  1299. command `emacs' will open most files in Emacs. Beware that this
  1300. will also open html files inside Emacs, unless you add
  1301. (\"html\" . default) to the list as well.
  1302. t Default for files not matched by any of the other options.
  1303. `system' The system command to open files, like `open' on Windows
  1304. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1305. that will be selected if you call `C-c C-o' with a double
  1306. `C-u C-u' prefix.
  1307. Possible values for the command are:
  1308. `emacs' The file will be visited by the current Emacs process.
  1309. `default' Use the default application for this file type, which is the
  1310. association for t in the list, most likely in the system-specific
  1311. part.
  1312. This can be used to overrule an unwanted setting in the
  1313. system-specific variable.
  1314. `system' Use the system command for opening files, like \"open\".
  1315. This command is specified by the entry whose car is `system'.
  1316. Most likely, the system-specific version of this variable
  1317. does define this command, but you can overrule/replace it
  1318. here.
  1319. string A command to be executed by a shell; %s will be replaced
  1320. by the path to the file.
  1321. sexp A Lisp form which will be evaluated. The file path will
  1322. be available in the Lisp variable `file'.
  1323. For more examples, see the system specific constants
  1324. `org-file-apps-defaults-macosx'
  1325. `org-file-apps-defaults-windowsnt'
  1326. `org-file-apps-defaults-gnu'."
  1327. :group 'org-link-follow
  1328. :type '(repeat
  1329. (cons (choice :value ""
  1330. (string :tag "Extension")
  1331. (const :tag "System command to open files" system)
  1332. (const :tag "Default for unrecognized files" t)
  1333. (const :tag "Remote file" remote)
  1334. (const :tag "Links to a directory" directory)
  1335. (const :tag "Any files that have Emacs modes"
  1336. auto-mode))
  1337. (choice :value ""
  1338. (const :tag "Visit with Emacs" emacs)
  1339. (const :tag "Use default" default)
  1340. (const :tag "Use the system command" system)
  1341. (string :tag "Command")
  1342. (sexp :tag "Lisp form")))))
  1343. (defgroup org-refile nil
  1344. "Options concerning refiling entries in Org-mode."
  1345. :tag "Org Refile"
  1346. :group 'org)
  1347. (defcustom org-directory "~/org"
  1348. "Directory with org files.
  1349. This is just a default location to look for Org files. There is no need
  1350. at all to put your files into this directory. It is only used in the
  1351. following situations:
  1352. 1. When a remember template specifies a target file that is not an
  1353. absolute path. The path will then be interpreted relative to
  1354. `org-directory'
  1355. 2. When a remember note is filed away in an interactive way (when exiting the
  1356. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1357. with `org-directory' as the default path."
  1358. :group 'org-refile
  1359. :group 'org-remember
  1360. :type 'directory)
  1361. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1362. "Default target for storing notes.
  1363. Used by the hooks for remember.el. This can be a string, or nil to mean
  1364. the value of `remember-data-file'.
  1365. You can set this on a per-template basis with the variable
  1366. `org-remember-templates'."
  1367. :group 'org-refile
  1368. :group 'org-remember
  1369. :type '(choice
  1370. (const :tag "Default from remember-data-file" nil)
  1371. file))
  1372. (defcustom org-goto-interface 'outline
  1373. "The default interface to be used for `org-goto'.
  1374. Allowed values are:
  1375. outline The interface shows an outline of the relevant file
  1376. and the correct heading is found by moving through
  1377. the outline or by searching with incremental search.
  1378. outline-path-completion Headlines in the current buffer are offered via
  1379. completion. This is the interface also used by
  1380. the refile command."
  1381. :group 'org-refile
  1382. :type '(choice
  1383. (const :tag "Outline" outline)
  1384. (const :tag "Outline-path-completion" outline-path-completion)))
  1385. (defcustom org-goto-max-level 5
  1386. "Maximum level to be considered when running org-goto with refile interface."
  1387. :group 'org-refile
  1388. :type 'integer)
  1389. (defcustom org-reverse-note-order nil
  1390. "Non-nil means store new notes at the beginning of a file or entry.
  1391. When nil, new notes will be filed to the end of a file or entry.
  1392. This can also be a list with cons cells of regular expressions that
  1393. are matched against file names, and values."
  1394. :group 'org-remember
  1395. :group 'org-refile
  1396. :type '(choice
  1397. (const :tag "Reverse always" t)
  1398. (const :tag "Reverse never" nil)
  1399. (repeat :tag "By file name regexp"
  1400. (cons regexp boolean))))
  1401. (defcustom org-log-refile nil
  1402. "Information to record when a task is refiled.
  1403. Possible values are:
  1404. nil Don't add anything
  1405. time Add a time stamp to the task
  1406. note Prompt for a note and add it with template `org-log-note-headings'
  1407. This option can also be set with on a per-file-basis with
  1408. #+STARTUP: nologrefile
  1409. #+STARTUP: logrefile
  1410. #+STARTUP: lognoterefile
  1411. You can have local logging settings for a subtree by setting the LOGGING
  1412. property to one or more of these keywords.
  1413. When bulk-refiling from the agenda, the value `note' is forbidden and
  1414. will temporarily be changed to `time'."
  1415. :group 'org-refile
  1416. :group 'org-progress
  1417. :type '(choice
  1418. (const :tag "No logging" nil)
  1419. (const :tag "Record timestamp" time)
  1420. (const :tag "Record timestamp with note." note)))
  1421. (defcustom org-refile-targets nil
  1422. "Targets for refiling entries with \\[org-refile].
  1423. This is list of cons cells. Each cell contains:
  1424. - a specification of the files to be considered, either a list of files,
  1425. or a symbol whose function or variable value will be used to retrieve
  1426. a file name or a list of file names. If you use `org-agenda-files' for
  1427. that, all agenda files will be scanned for targets. Nil means consider
  1428. headings in the current buffer.
  1429. - A specification of how to find candidate refile targets. This may be
  1430. any of:
  1431. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1432. This tag has to be present in all target headlines, inheritance will
  1433. not be considered.
  1434. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1435. todo keyword.
  1436. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1437. headlines that are refiling targets.
  1438. - a cons cell (:level . N). Any headline of level N is considered a target.
  1439. Note that, when `org-odd-levels-only' is set, level corresponds to
  1440. order in hierarchy, not to the number of stars.
  1441. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1442. Note that, when `org-odd-levels-only' is set, level corresponds to
  1443. order in hierarchy, not to the number of stars.
  1444. You can set the variable `org-refile-target-verify-function' to a function
  1445. to verify each headline found by the simple critery above.
  1446. When this variable is nil, all top-level headlines in the current buffer
  1447. are used, equivalent to the value `((nil . (:level . 1))'."
  1448. :group 'org-refile
  1449. :type '(repeat
  1450. (cons
  1451. (choice :value org-agenda-files
  1452. (const :tag "All agenda files" org-agenda-files)
  1453. (const :tag "Current buffer" nil)
  1454. (function) (variable) (file))
  1455. (choice :tag "Identify target headline by"
  1456. (cons :tag "Specific tag" (const :value :tag) (string))
  1457. (cons :tag "TODO keyword" (const :value :todo) (string))
  1458. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1459. (cons :tag "Level number" (const :value :level) (integer))
  1460. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1461. (defcustom org-refile-target-verify-function nil
  1462. "Function to verify if the headline at point should be a refile target.
  1463. The function will be called without arguments, with point at the
  1464. beginning of the headline. It should return t and leave point
  1465. where it is if the headline is a valid target for refiling.
  1466. If the target should not be selected, the function must return nil.
  1467. In addition to this, it may move point to a place from where the search
  1468. should be continued. For example, the function may decide that the entire
  1469. subtree of the current entry should be excluded and move point to the end
  1470. of the subtree."
  1471. :group 'org-refile
  1472. :type 'function)
  1473. (defcustom org-refile-use-outline-path nil
  1474. "Non-nil means provide refile targets as paths.
  1475. So a level 3 headline will be available as level1/level2/level3.
  1476. When the value is `file', also include the file name (without directory)
  1477. into the path. In this case, you can also stop the completion after
  1478. the file name, to get entries inserted as top level in the file.
  1479. When `full-file-path', include the full file path."
  1480. :group 'org-refile
  1481. :type '(choice
  1482. (const :tag "Not" nil)
  1483. (const :tag "Yes" t)
  1484. (const :tag "Start with file name" file)
  1485. (const :tag "Start with full file path" full-file-path)))
  1486. (defcustom org-outline-path-complete-in-steps t
  1487. "Non-nil means complete the outline path in hierarchical steps.
  1488. When Org-mode uses the refile interface to select an outline path
  1489. \(see variable `org-refile-use-outline-path'), the completion of
  1490. the path can be done is a single go, or if can be done in steps down
  1491. the headline hierarchy. Going in steps is probably the best if you
  1492. do not use a special completion package like `ido' or `icicles'.
  1493. However, when using these packages, going in one step can be very
  1494. fast, while still showing the whole path to the entry."
  1495. :group 'org-refile
  1496. :type 'boolean)
  1497. (defcustom org-refile-allow-creating-parent-nodes nil
  1498. "Non-nil means allow to create new nodes as refile targets.
  1499. New nodes are then created by adding \"/new node name\" to the completion
  1500. of an existing node. When the value of this variable is `confirm',
  1501. new node creation must be confirmed by the user (recommended)
  1502. When nil, the completion must match an existing entry.
  1503. Note that, if the new heading is not seen by the criteria
  1504. listed in `org-refile-targets', multiple instances of the same
  1505. heading would be created by trying again to file under the new
  1506. heading."
  1507. :group 'org-refile
  1508. :type '(choice
  1509. (const :tag "Never" nil)
  1510. (const :tag "Always" t)
  1511. (const :tag "Prompt for confirmation" confirm)))
  1512. (defgroup org-todo nil
  1513. "Options concerning TODO items in Org-mode."
  1514. :tag "Org TODO"
  1515. :group 'org)
  1516. (defgroup org-progress nil
  1517. "Options concerning Progress logging in Org-mode."
  1518. :tag "Org Progress"
  1519. :group 'org-time)
  1520. (defvar org-todo-interpretation-widgets
  1521. '(
  1522. (:tag "Sequence (cycling hits every state)" sequence)
  1523. (:tag "Type (cycling directly to DONE)" type))
  1524. "The available interpretation symbols for customizing
  1525. `org-todo-keywords'.
  1526. Interested libraries should add to this list.")
  1527. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1528. "List of TODO entry keyword sequences and their interpretation.
  1529. \\<org-mode-map>This is a list of sequences.
  1530. Each sequence starts with a symbol, either `sequence' or `type',
  1531. indicating if the keywords should be interpreted as a sequence of
  1532. action steps, or as different types of TODO items. The first
  1533. keywords are states requiring action - these states will select a headline
  1534. for inclusion into the global TODO list Org-mode produces. If one of
  1535. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1536. signify that no further action is necessary. If \"|\" is not found,
  1537. the last keyword is treated as the only DONE state of the sequence.
  1538. The command \\[org-todo] cycles an entry through these states, and one
  1539. additional state where no keyword is present. For details about this
  1540. cycling, see the manual.
  1541. TODO keywords and interpretation can also be set on a per-file basis with
  1542. the special #+SEQ_TODO and #+TYP_TODO lines.
  1543. Each keyword can optionally specify a character for fast state selection
  1544. \(in combination with the variable `org-use-fast-todo-selection')
  1545. and specifiers for state change logging, using the same syntax
  1546. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1547. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1548. indicates to record a time stamp each time this state is selected.
  1549. Each keyword may also specify if a timestamp or a note should be
  1550. recorded when entering or leaving the state, by adding additional
  1551. characters in the parenthesis after the keyword. This looks like this:
  1552. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1553. record only the time of the state change. With X and Y being either
  1554. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1555. Y when leaving the state if and only if the *target* state does not
  1556. define X. You may omit any of the fast-selection key or X or /Y,
  1557. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1558. For backward compatibility, this variable may also be just a list
  1559. of keywords - in this case the interpretation (sequence or type) will be
  1560. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1561. :group 'org-todo
  1562. :group 'org-keywords
  1563. :type '(choice
  1564. (repeat :tag "Old syntax, just keywords"
  1565. (string :tag "Keyword"))
  1566. (repeat :tag "New syntax"
  1567. (cons
  1568. (choice
  1569. :tag "Interpretation"
  1570. ;;Quick and dirty way to see
  1571. ;;`org-todo-interpretations'. This takes the
  1572. ;;place of item arguments
  1573. :convert-widget
  1574. (lambda (widget)
  1575. (widget-put widget
  1576. :args (mapcar
  1577. #'(lambda (x)
  1578. (widget-convert
  1579. (cons 'const x)))
  1580. org-todo-interpretation-widgets))
  1581. widget))
  1582. (repeat
  1583. (string :tag "Keyword"))))))
  1584. (defvar org-todo-keywords-1 nil
  1585. "All TODO and DONE keywords active in a buffer.")
  1586. (make-variable-buffer-local 'org-todo-keywords-1)
  1587. (defvar org-todo-keywords-for-agenda nil)
  1588. (defvar org-done-keywords-for-agenda nil)
  1589. (defvar org-drawers-for-agenda nil)
  1590. (defvar org-todo-keyword-alist-for-agenda nil)
  1591. (defvar org-tag-alist-for-agenda nil)
  1592. (defvar org-agenda-contributing-files nil)
  1593. (defvar org-not-done-keywords nil)
  1594. (make-variable-buffer-local 'org-not-done-keywords)
  1595. (defvar org-done-keywords nil)
  1596. (make-variable-buffer-local 'org-done-keywords)
  1597. (defvar org-todo-heads nil)
  1598. (make-variable-buffer-local 'org-todo-heads)
  1599. (defvar org-todo-sets nil)
  1600. (make-variable-buffer-local 'org-todo-sets)
  1601. (defvar org-todo-log-states nil)
  1602. (make-variable-buffer-local 'org-todo-log-states)
  1603. (defvar org-todo-kwd-alist nil)
  1604. (make-variable-buffer-local 'org-todo-kwd-alist)
  1605. (defvar org-todo-key-alist nil)
  1606. (make-variable-buffer-local 'org-todo-key-alist)
  1607. (defvar org-todo-key-trigger nil)
  1608. (make-variable-buffer-local 'org-todo-key-trigger)
  1609. (defcustom org-todo-interpretation 'sequence
  1610. "Controls how TODO keywords are interpreted.
  1611. This variable is in principle obsolete and is only used for
  1612. backward compatibility, if the interpretation of todo keywords is
  1613. not given already in `org-todo-keywords'. See that variable for
  1614. more information."
  1615. :group 'org-todo
  1616. :group 'org-keywords
  1617. :type '(choice (const sequence)
  1618. (const type)))
  1619. (defcustom org-use-fast-todo-selection t
  1620. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1621. This variable describes if and under what circumstances the cycling
  1622. mechanism for TODO keywords will be replaced by a single-key, direct
  1623. selection scheme.
  1624. When nil, fast selection is never used.
  1625. When the symbol `prefix', it will be used when `org-todo' is called with
  1626. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1627. in an agenda buffer.
  1628. When t, fast selection is used by default. In this case, the prefix
  1629. argument forces cycling instead.
  1630. In all cases, the special interface is only used if access keys have actually
  1631. been assigned by the user, i.e. if keywords in the configuration are followed
  1632. by a letter in parenthesis, like TODO(t)."
  1633. :group 'org-todo
  1634. :type '(choice
  1635. (const :tag "Never" nil)
  1636. (const :tag "By default" t)
  1637. (const :tag "Only with C-u C-c C-t" prefix)))
  1638. (defcustom org-provide-todo-statistics t
  1639. "Non-nil means update todo statistics after insert and toggle.
  1640. ALL-HEADLINES means update todo statistics by including headlines
  1641. with no TODO keyword as well, counting them as not done.
  1642. A list of TODO keywords means the same, but skip keywords that are
  1643. not in this list.
  1644. When this is set, todo statistics is updated in the parent of the
  1645. current entry each time a todo state is changed."
  1646. :group 'org-todo
  1647. :type '(choice
  1648. (const :tag "Yes, only for TODO entries" t)
  1649. (const :tag "Yes, including all entries" 'all-headlines)
  1650. (repeat :tag "Yes, for TODOs in this list"
  1651. (string :tag "TODO keyword"))
  1652. (other :tag "No TODO statistics" nil)))
  1653. (defcustom org-hierarchical-todo-statistics t
  1654. "Non-nil means TODO statistics covers just direct children.
  1655. When nil, all entries in the subtree are considered.
  1656. This has only an effect if `org-provide-todo-statistics' is set.
  1657. To set this to nil for only a single subtree, use a COOKIE_DATA
  1658. property and include the word \"recursive\" into the value."
  1659. :group 'org-todo
  1660. :type 'boolean)
  1661. (defcustom org-after-todo-state-change-hook nil
  1662. "Hook which is run after the state of a TODO item was changed.
  1663. The new state (a string with a TODO keyword, or nil) is available in the
  1664. Lisp variable `state'."
  1665. :group 'org-todo
  1666. :type 'hook)
  1667. (defvar org-blocker-hook nil
  1668. "Hook for functions that are allowed to block a state change.
  1669. Each function gets as its single argument a property list, see
  1670. `org-trigger-hook' for more information about this list.
  1671. If any of the functions in this hook returns nil, the state change
  1672. is blocked.")
  1673. (defvar org-trigger-hook nil
  1674. "Hook for functions that are triggered by a state change.
  1675. Each function gets as its single argument a property list with at least
  1676. the following elements:
  1677. (:type type-of-change :position pos-at-entry-start
  1678. :from old-state :to new-state)
  1679. Depending on the type, more properties may be present.
  1680. This mechanism is currently implemented for:
  1681. TODO state changes
  1682. ------------------
  1683. :type todo-state-change
  1684. :from previous state (keyword as a string), or nil, or a symbol
  1685. 'todo' or 'done', to indicate the general type of state.
  1686. :to new state, like in :from")
  1687. (defcustom org-enforce-todo-dependencies nil
  1688. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1689. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1690. be blocked if any prior sibling is not yet done.
  1691. Finally, if the parent is blocked because of ordered siblings of its own,
  1692. the child will also be blocked.
  1693. This variable needs to be set before org.el is loaded, and you need to
  1694. restart Emacs after a change to make the change effective. The only way
  1695. to change is while Emacs is running is through the customize interface."
  1696. :set (lambda (var val)
  1697. (set var val)
  1698. (if val
  1699. (add-hook 'org-blocker-hook
  1700. 'org-block-todo-from-children-or-siblings-or-parent)
  1701. (remove-hook 'org-blocker-hook
  1702. 'org-block-todo-from-children-or-siblings-or-parent)))
  1703. :group 'org-todo
  1704. :type 'boolean)
  1705. (defcustom org-enforce-todo-checkbox-dependencies nil
  1706. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1707. When this is nil, checkboxes have no influence on switching TODO states.
  1708. When non-nil, you first need to check off all check boxes before the TODO
  1709. entry can be switched to DONE.
  1710. This variable needs to be set before org.el is loaded, and you need to
  1711. restart Emacs after a change to make the change effective. The only way
  1712. to change is while Emacs is running is through the customize interface."
  1713. :set (lambda (var val)
  1714. (set var val)
  1715. (if val
  1716. (add-hook 'org-blocker-hook
  1717. 'org-block-todo-from-checkboxes)
  1718. (remove-hook 'org-blocker-hook
  1719. 'org-block-todo-from-checkboxes)))
  1720. :group 'org-todo
  1721. :type 'boolean)
  1722. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1723. "Non-nil means inserting a TODO heading is treated as state change.
  1724. So when the command \\[org-insert-todo-heading] is used, state change
  1725. logging will apply if appropriate. When nil, the new TODO item will
  1726. be inserted directly, and no logging will take place."
  1727. :group 'org-todo
  1728. :type 'boolean)
  1729. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1730. "Non-nil means switching TODO states with S-cursor counts as state change.
  1731. This is the default behavior. However, setting this to nil allows a
  1732. convenient way to select a TODO state and bypass any logging associated
  1733. with that."
  1734. :group 'org-todo
  1735. :type 'boolean)
  1736. (defcustom org-todo-state-tags-triggers nil
  1737. "Tag changes that should be triggered by TODO state changes.
  1738. This is a list. Each entry is
  1739. (state-change (tag . flag) .......)
  1740. State-change can be a string with a state, and empty string to indicate the
  1741. state that has no TODO keyword, or it can be one of the symbols `todo'
  1742. or `done', meaning any not-done or done state, respectively."
  1743. :group 'org-todo
  1744. :group 'org-tags
  1745. :type '(repeat
  1746. (cons (choice :tag "When changing to"
  1747. (const :tag "Not-done state" todo)
  1748. (const :tag "Done state" done)
  1749. (string :tag "State"))
  1750. (repeat
  1751. (cons :tag "Tag action"
  1752. (string :tag "Tag")
  1753. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1754. (defcustom org-log-done nil
  1755. "Information to record when a task moves to the DONE state.
  1756. Possible values are:
  1757. nil Don't add anything, just change the keyword
  1758. time Add a time stamp to the task
  1759. note Prompt for a note and add it with template `org-log-note-headings'
  1760. This option can also be set with on a per-file-basis with
  1761. #+STARTUP: nologdone
  1762. #+STARTUP: logdone
  1763. #+STARTUP: lognotedone
  1764. You can have local logging settings for a subtree by setting the LOGGING
  1765. property to one or more of these keywords."
  1766. :group 'org-todo
  1767. :group 'org-progress
  1768. :type '(choice
  1769. (const :tag "No logging" nil)
  1770. (const :tag "Record CLOSED timestamp" time)
  1771. (const :tag "Record CLOSED timestamp with note." note)))
  1772. ;; Normalize old uses of org-log-done.
  1773. (cond
  1774. ((eq org-log-done t) (setq org-log-done 'time))
  1775. ((and (listp org-log-done) (memq 'done org-log-done))
  1776. (setq org-log-done 'note)))
  1777. (defcustom org-log-reschedule nil
  1778. "Information to record when the scheduling date of a tasks is modified.
  1779. Possible values are:
  1780. nil Don't add anything, just change the date
  1781. time Add a time stamp to the task
  1782. note Prompt for a note and add it with template `org-log-note-headings'
  1783. This option can also be set with on a per-file-basis with
  1784. #+STARTUP: nologreschedule
  1785. #+STARTUP: logreschedule
  1786. #+STARTUP: lognotereschedule"
  1787. :group 'org-todo
  1788. :group 'org-progress
  1789. :type '(choice
  1790. (const :tag "No logging" nil)
  1791. (const :tag "Record timestamp" time)
  1792. (const :tag "Record timestamp with note." note)))
  1793. (defcustom org-log-redeadline nil
  1794. "Information to record when the deadline date of a tasks is modified.
  1795. Possible values are:
  1796. nil Don't add anything, just change the date
  1797. time Add a time stamp to the task
  1798. note Prompt for a note and add it with template `org-log-note-headings'
  1799. This option can also be set with on a per-file-basis with
  1800. #+STARTUP: nologredeadline
  1801. #+STARTUP: logredeadline
  1802. #+STARTUP: lognoteredeadline
  1803. You can have local logging settings for a subtree by setting the LOGGING
  1804. property to one or more of these keywords."
  1805. :group 'org-todo
  1806. :group 'org-progress
  1807. :type '(choice
  1808. (const :tag "No logging" nil)
  1809. (const :tag "Record timestamp" time)
  1810. (const :tag "Record timestamp with note." note)))
  1811. (defcustom org-log-note-clock-out nil
  1812. "Non-nil means record a note when clocking out of an item.
  1813. This can also be configured on a per-file basis by adding one of
  1814. the following lines anywhere in the buffer:
  1815. #+STARTUP: lognoteclock-out
  1816. #+STARTUP: nolognoteclock-out"
  1817. :group 'org-todo
  1818. :group 'org-progress
  1819. :type 'boolean)
  1820. (defcustom org-log-done-with-time t
  1821. "Non-nil means the CLOSED time stamp will contain date and time.
  1822. When nil, only the date will be recorded."
  1823. :group 'org-progress
  1824. :type 'boolean)
  1825. (defcustom org-log-note-headings
  1826. '((done . "CLOSING NOTE %t")
  1827. (state . "State %-12s from %-12S %t")
  1828. (note . "Note taken on %t")
  1829. (reschedule . "Rescheduled from %S on %t")
  1830. (delschedule . "Not scheduled, was %S on %t")
  1831. (redeadline . "New deadline from %S on %t")
  1832. (deldeadline . "Removed deadline, was %S on %t")
  1833. (refile . "Refiled on %t")
  1834. (clock-out . ""))
  1835. "Headings for notes added to entries.
  1836. The value is an alist, with the car being a symbol indicating the note
  1837. context, and the cdr is the heading to be used. The heading may also be the
  1838. empty string.
  1839. %t in the heading will be replaced by a time stamp.
  1840. %s will be replaced by the new TODO state, in double quotes.
  1841. %S will be replaced by the old TODO state, in double quotes.
  1842. %u will be replaced by the user name.
  1843. %U will be replaced by the full user name.
  1844. In fact, it is not a good idea to change the `state' entry, because
  1845. agenda log mode depends on the format of these entries."
  1846. :group 'org-todo
  1847. :group 'org-progress
  1848. :type '(list :greedy t
  1849. (cons (const :tag "Heading when closing an item" done) string)
  1850. (cons (const :tag
  1851. "Heading when changing todo state (todo sequence only)"
  1852. state) string)
  1853. (cons (const :tag "Heading when just taking a note" note) string)
  1854. (cons (const :tag "Heading when clocking out" clock-out) string)
  1855. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1856. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1857. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1858. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1859. (cons (const :tag "Heading when refiling" refile) string)))
  1860. (unless (assq 'note org-log-note-headings)
  1861. (push '(note . "%t") org-log-note-headings))
  1862. (defcustom org-log-into-drawer nil
  1863. "Non-nil means insert state change notes and time stamps into a drawer.
  1864. When nil, state changes notes will be inserted after the headline and
  1865. any scheduling and clock lines, but not inside a drawer.
  1866. The value of this variable should be the name of the drawer to use.
  1867. LOGBOOK is proposed at the default drawer for this purpose, you can
  1868. also set this to a string to define the drawer of your choice.
  1869. A value of t is also allowed, representing \"LOGBOOK\".
  1870. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1871. will be ignored.
  1872. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1873. a subtree."
  1874. :group 'org-todo
  1875. :group 'org-progress
  1876. :type '(choice
  1877. (const :tag "Not into a drawer" nil)
  1878. (const :tag "LOGBOOK" t)
  1879. (string :tag "Other")))
  1880. (if (fboundp 'defvaralias)
  1881. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1882. (defun org-log-into-drawer ()
  1883. "Return the value of `org-log-into-drawer', but let properties overrule.
  1884. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1885. used instead of the default value."
  1886. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1887. (cond
  1888. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1889. ((equal p "t") "LOGBOOK")
  1890. (t p))))
  1891. (defcustom org-log-state-notes-insert-after-drawers nil
  1892. "Non-nil means insert state change notes after any drawers in entry.
  1893. Only the drawers that *immediately* follow the headline and the
  1894. deadline/scheduled line are skipped.
  1895. When nil, insert notes right after the heading and perhaps the line
  1896. with deadline/scheduling if present.
  1897. This variable will have no effect if `org-log-into-drawer' is
  1898. set."
  1899. :group 'org-todo
  1900. :group 'org-progress
  1901. :type 'boolean)
  1902. (defcustom org-log-states-order-reversed t
  1903. "Non-nil means the latest state note will be directly after heading.
  1904. When nil, the state change notes will be ordered according to time."
  1905. :group 'org-todo
  1906. :group 'org-progress
  1907. :type 'boolean)
  1908. (defcustom org-todo-repeat-to-state nil
  1909. "The TODO state to which a repeater should return the repeating task.
  1910. By default this is the first task in a TODO sequence, or the previous state
  1911. in a TODO_TYP set. But you can specify another task here.
  1912. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  1913. :group 'org-todo
  1914. :type '(choice (const :tag "Head of sequence" nil)
  1915. (string :tag "Specific state")))
  1916. (defcustom org-log-repeat 'time
  1917. "Non-nil means record moving through the DONE state when triggering repeat.
  1918. An auto-repeating task is immediately switched back to TODO when
  1919. marked DONE. If you are not logging state changes (by adding \"@\"
  1920. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1921. record a closing note, there will be no record of the task moving
  1922. through DONE. This variable forces taking a note anyway.
  1923. nil Don't force a record
  1924. time Record a time stamp
  1925. note Record a note
  1926. This option can also be set with on a per-file-basis with
  1927. #+STARTUP: logrepeat
  1928. #+STARTUP: lognoterepeat
  1929. #+STARTUP: nologrepeat
  1930. You can have local logging settings for a subtree by setting the LOGGING
  1931. property to one or more of these keywords."
  1932. :group 'org-todo
  1933. :group 'org-progress
  1934. :type '(choice
  1935. (const :tag "Don't force a record" nil)
  1936. (const :tag "Force recording the DONE state" time)
  1937. (const :tag "Force recording a note with the DONE state" note)))
  1938. (defgroup org-priorities nil
  1939. "Priorities in Org-mode."
  1940. :tag "Org Priorities"
  1941. :group 'org-todo)
  1942. (defcustom org-enable-priority-commands t
  1943. "Non-nil means priority commands are active.
  1944. When nil, these commands will be disabled, so that you never accidentally
  1945. set a priority."
  1946. :group 'org-priorities
  1947. :type 'boolean)
  1948. (defcustom org-highest-priority ?A
  1949. "The highest priority of TODO items. A character like ?A, ?B etc.
  1950. Must have a smaller ASCII number than `org-lowest-priority'."
  1951. :group 'org-priorities
  1952. :type 'character)
  1953. (defcustom org-lowest-priority ?C
  1954. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1955. Must have a larger ASCII number than `org-highest-priority'."
  1956. :group 'org-priorities
  1957. :type 'character)
  1958. (defcustom org-default-priority ?B
  1959. "The default priority of TODO items.
  1960. This is the priority an item get if no explicit priority is given."
  1961. :group 'org-priorities
  1962. :type 'character)
  1963. (defcustom org-priority-start-cycle-with-default t
  1964. "Non-nil means start with default priority when starting to cycle.
  1965. When this is nil, the first step in the cycle will be (depending on the
  1966. command used) one higher or lower that the default priority."
  1967. :group 'org-priorities
  1968. :type 'boolean)
  1969. (defgroup org-time nil
  1970. "Options concerning time stamps and deadlines in Org-mode."
  1971. :tag "Org Time"
  1972. :group 'org)
  1973. (defcustom org-insert-labeled-timestamps-at-point nil
  1974. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  1975. When nil, these labeled time stamps are forces into the second line of an
  1976. entry, just after the headline. When scheduling from the global TODO list,
  1977. the time stamp will always be forced into the second line."
  1978. :group 'org-time
  1979. :type 'boolean)
  1980. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1981. "Formats for `format-time-string' which are used for time stamps.
  1982. It is not recommended to change this constant.")
  1983. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1984. "Number of minutes to round time stamps to.
  1985. These are two values, the first applies when first creating a time stamp.
  1986. The second applies when changing it with the commands `S-up' and `S-down'.
  1987. When changing the time stamp, this means that it will change in steps
  1988. of N minutes, as given by the second value.
  1989. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1990. numbers should be factors of 60, so for example 5, 10, 15.
  1991. When this is larger than 1, you can still force an exact time-stamp by using
  1992. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1993. and by using a prefix arg to `S-up/down' to specify the exact number
  1994. of minutes to shift."
  1995. :group 'org-time
  1996. :get '(lambda (var) ; Make sure both elements are there
  1997. (if (integerp (default-value var))
  1998. (list (default-value var) 5)
  1999. (default-value var)))
  2000. :type '(list
  2001. (integer :tag "when inserting times")
  2002. (integer :tag "when modifying times")))
  2003. ;; Normalize old customizations of this variable.
  2004. (when (integerp org-time-stamp-rounding-minutes)
  2005. (setq org-time-stamp-rounding-minutes
  2006. (list org-time-stamp-rounding-minutes
  2007. org-time-stamp-rounding-minutes)))
  2008. (defcustom org-display-custom-times nil
  2009. "Non-nil means overlay custom formats over all time stamps.
  2010. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2011. To turn this on on a per-file basis, insert anywhere in the file:
  2012. #+STARTUP: customtime"
  2013. :group 'org-time
  2014. :set 'set-default
  2015. :type 'sexp)
  2016. (make-variable-buffer-local 'org-display-custom-times)
  2017. (defcustom org-time-stamp-custom-formats
  2018. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2019. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2020. These are overlayed over the default ISO format if the variable
  2021. `org-display-custom-times' is set. Time like %H:%M should be at the
  2022. end of the second format. The custom formats are also honored by export
  2023. commands, if custom time display is turned on at the time of export."
  2024. :group 'org-time
  2025. :type 'sexp)
  2026. (defun org-time-stamp-format (&optional long inactive)
  2027. "Get the right format for a time string."
  2028. (let ((f (if long (cdr org-time-stamp-formats)
  2029. (car org-time-stamp-formats))))
  2030. (if inactive
  2031. (concat "[" (substring f 1 -1) "]")
  2032. f)))
  2033. (defcustom org-time-clocksum-format "%d:%02d"
  2034. "The format string used when creating CLOCKSUM lines, or when
  2035. org-mode generates a time duration."
  2036. :group 'org-time
  2037. :type 'string)
  2038. (defcustom org-time-clocksum-use-fractional nil
  2039. "If non-nil, \\[org-clock-display] uses fractional times.
  2040. org-mode generates a time duration."
  2041. :group 'org-time
  2042. :type 'boolean)
  2043. (defcustom org-time-clocksum-fractional-format "%.2f"
  2044. "The format string used when creating CLOCKSUM lines, or when
  2045. org-mode generates a time duration."
  2046. :group 'org-time
  2047. :type 'string)
  2048. (defcustom org-deadline-warning-days 14
  2049. "No. of days before expiration during which a deadline becomes active.
  2050. This variable governs the display in sparse trees and in the agenda.
  2051. When 0 or negative, it means use this number (the absolute value of it)
  2052. even if a deadline has a different individual lead time specified.
  2053. Custom commands can set this variable in the options section."
  2054. :group 'org-time
  2055. :group 'org-agenda-daily/weekly
  2056. :type 'integer)
  2057. (defcustom org-read-date-prefer-future t
  2058. "Non-nil means assume future for incomplete date input from user.
  2059. This affects the following situations:
  2060. 1. The user gives a month but not a year.
  2061. For example, if it is april and you enter \"feb 2\", this will be read
  2062. as feb 2, *next* year. \"May 5\", however, will be this year.
  2063. 2. The user gives a day, but no month.
  2064. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2065. read this as the third of *next* month. However, if you enter \"17\",
  2066. it will be considered as *this* month.
  2067. If you set this variable to the symbol `time', then also the following
  2068. will work:
  2069. 3. If the user gives a time, but no day. If the time is before now,
  2070. to will be interpreted as tomorrow.
  2071. Currently none of this works for ISO week specifications.
  2072. When this option is nil, the current day, month and year will always be
  2073. used as defaults."
  2074. :group 'org-time
  2075. :type '(choice
  2076. (const :tag "Never" nil)
  2077. (const :tag "Check month and day" t)
  2078. (const :tag "Check month, day, and time" time)))
  2079. (defcustom org-read-date-display-live t
  2080. "Non-nil means display current interpretation of date prompt live.
  2081. This display will be in an overlay, in the minibuffer."
  2082. :group 'org-time
  2083. :type 'boolean)
  2084. (defcustom org-read-date-popup-calendar t
  2085. "Non-nil means pop up a calendar when prompting for a date.
  2086. In the calendar, the date can be selected with mouse-1. However, the
  2087. minibuffer will also be active, and you can simply enter the date as well.
  2088. When nil, only the minibuffer will be available."
  2089. :group 'org-time
  2090. :type 'boolean)
  2091. (if (fboundp 'defvaralias)
  2092. (defvaralias 'org-popup-calendar-for-date-prompt
  2093. 'org-read-date-popup-calendar))
  2094. (defcustom org-read-date-minibuffer-setup-hook nil
  2095. "Hook to be used to set up keys for the date/time interface.
  2096. Add key definitions to `minibuffer-local-map', which will be a temporary
  2097. copy."
  2098. :group 'org-time
  2099. :type 'hook)
  2100. (defcustom org-extend-today-until 0
  2101. "The hour when your day really ends. Must be an integer.
  2102. This has influence for the following applications:
  2103. - When switching the agenda to \"today\". It it is still earlier than
  2104. the time given here, the day recognized as TODAY is actually yesterday.
  2105. - When a date is read from the user and it is still before the time given
  2106. here, the current date and time will be assumed to be yesterday, 23:59.
  2107. Also, timestamps inserted in remember templates follow this rule.
  2108. IMPORTANT: This is a feature whose implementation is and likely will
  2109. remain incomplete. Really, it is only here because past midnight seems to
  2110. be the favorite working time of John Wiegley :-)"
  2111. :group 'org-time
  2112. :type 'integer)
  2113. (defcustom org-edit-timestamp-down-means-later nil
  2114. "Non-nil means S-down will increase the time in a time stamp.
  2115. When nil, S-up will increase."
  2116. :group 'org-time
  2117. :type 'boolean)
  2118. (defcustom org-calendar-follow-timestamp-change t
  2119. "Non-nil means make the calendar window follow timestamp changes.
  2120. When a timestamp is modified and the calendar window is visible, it will be
  2121. moved to the new date."
  2122. :group 'org-time
  2123. :type 'boolean)
  2124. (defgroup org-tags nil
  2125. "Options concerning tags in Org-mode."
  2126. :tag "Org Tags"
  2127. :group 'org)
  2128. (defcustom org-tag-alist nil
  2129. "List of tags allowed in Org-mode files.
  2130. When this list is nil, Org-mode will base TAG input on what is already in the
  2131. buffer.
  2132. The value of this variable is an alist, the car of each entry must be a
  2133. keyword as a string, the cdr may be a character that is used to select
  2134. that tag through the fast-tag-selection interface.
  2135. See the manual for details."
  2136. :group 'org-tags
  2137. :type '(repeat
  2138. (choice
  2139. (cons (string :tag "Tag name")
  2140. (character :tag "Access char"))
  2141. (list :tag "Start radio group"
  2142. (const :startgroup)
  2143. (option (string :tag "Group description")))
  2144. (list :tag "End radio group"
  2145. (const :endgroup)
  2146. (option (string :tag "Group description")))
  2147. (const :tag "New line" (:newline)))))
  2148. (defcustom org-tag-persistent-alist nil
  2149. "List of tags that will always appear in all Org-mode files.
  2150. This is in addition to any in buffer settings or customizations
  2151. of `org-tag-alist'.
  2152. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2153. The value of this variable is an alist, the car of each entry must be a
  2154. keyword as a string, the cdr may be a character that is used to select
  2155. that tag through the fast-tag-selection interface.
  2156. See the manual for details.
  2157. To disable these tags on a per-file basis, insert anywhere in the file:
  2158. #+STARTUP: noptag"
  2159. :group 'org-tags
  2160. :type '(repeat
  2161. (choice
  2162. (cons (string :tag "Tag name")
  2163. (character :tag "Access char"))
  2164. (const :tag "Start radio group" (:startgroup))
  2165. (const :tag "End radio group" (:endgroup))
  2166. (const :tag "New line" (:newline)))))
  2167. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2168. "If non-nil, always offer completion for all tags of all agenda files.
  2169. Instead of customizing this variable directly, you might want to
  2170. set it locally for remember buffers, because there no list of
  2171. tags in that file can be created dynamically (there are none).
  2172. (add-hook 'org-remember-mode-hook
  2173. (lambda ()
  2174. (set (make-local-variable
  2175. 'org-complete-tags-always-offer-all-agenda-tags)
  2176. t)))"
  2177. :group 'org-tags
  2178. :type 'boolean)
  2179. (defvar org-file-tags nil
  2180. "List of tags that can be inherited by all entries in the file.
  2181. The tags will be inherited if the variable `org-use-tag-inheritance'
  2182. says they should be.
  2183. This variable is populated from #+FILETAGS lines.")
  2184. (defcustom org-use-fast-tag-selection 'auto
  2185. "Non-nil means use fast tag selection scheme.
  2186. This is a special interface to select and deselect tags with single keys.
  2187. When nil, fast selection is never used.
  2188. When the symbol `auto', fast selection is used if and only if selection
  2189. characters for tags have been configured, either through the variable
  2190. `org-tag-alist' or through a #+TAGS line in the buffer.
  2191. When t, fast selection is always used and selection keys are assigned
  2192. automatically if necessary."
  2193. :group 'org-tags
  2194. :type '(choice
  2195. (const :tag "Always" t)
  2196. (const :tag "Never" nil)
  2197. (const :tag "When selection characters are configured" 'auto)))
  2198. (defcustom org-fast-tag-selection-single-key nil
  2199. "Non-nil means fast tag selection exits after first change.
  2200. When nil, you have to press RET to exit it.
  2201. During fast tag selection, you can toggle this flag with `C-c'.
  2202. This variable can also have the value `expert'. In this case, the window
  2203. displaying the tags menu is not even shown, until you press C-c again."
  2204. :group 'org-tags
  2205. :type '(choice
  2206. (const :tag "No" nil)
  2207. (const :tag "Yes" t)
  2208. (const :tag "Expert" expert)))
  2209. (defvar org-fast-tag-selection-include-todo nil
  2210. "Non-nil means fast tags selection interface will also offer TODO states.
  2211. This is an undocumented feature, you should not rely on it.")
  2212. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2213. "The column to which tags should be indented in a headline.
  2214. If this number is positive, it specifies the column. If it is negative,
  2215. it means that the tags should be flushright to that column. For example,
  2216. -80 works well for a normal 80 character screen."
  2217. :group 'org-tags
  2218. :type 'integer)
  2219. (defcustom org-auto-align-tags t
  2220. "Non-nil means realign tags after pro/demotion of TODO state change.
  2221. These operations change the length of a headline and therefore shift
  2222. the tags around. With this options turned on, after each such operation
  2223. the tags are again aligned to `org-tags-column'."
  2224. :group 'org-tags
  2225. :type 'boolean)
  2226. (defcustom org-use-tag-inheritance t
  2227. "Non-nil means tags in levels apply also for sublevels.
  2228. When nil, only the tags directly given in a specific line apply there.
  2229. This may also be a list of tags that should be inherited, or a regexp that
  2230. matches tags that should be inherited. Additional control is possible
  2231. with the variable `org-tags-exclude-from-inheritance' which gives an
  2232. explicit list of tags to be excluded from inheritance., even if the value of
  2233. `org-use-tag-inheritance' would select it for inheritance.
  2234. If this option is t, a match early-on in a tree can lead to a large
  2235. number of matches in the subtree when constructing the agenda or creating
  2236. a sparse tree. If you only want to see the first match in a tree during
  2237. a search, check out the variable `org-tags-match-list-sublevels'."
  2238. :group 'org-tags
  2239. :type '(choice
  2240. (const :tag "Not" nil)
  2241. (const :tag "Always" t)
  2242. (repeat :tag "Specific tags" (string :tag "Tag"))
  2243. (regexp :tag "Tags matched by regexp")))
  2244. (defcustom org-tags-exclude-from-inheritance nil
  2245. "List of tags that should never be inherited.
  2246. This is a way to exclude a few tags from inheritance. For way to do
  2247. the opposite, to actively allow inheritance for selected tags,
  2248. see the variable `org-use-tag-inheritance'."
  2249. :group 'org-tags
  2250. :type '(repeat (string :tag "Tag")))
  2251. (defun org-tag-inherit-p (tag)
  2252. "Check if TAG is one that should be inherited."
  2253. (cond
  2254. ((member tag org-tags-exclude-from-inheritance) nil)
  2255. ((eq org-use-tag-inheritance t) t)
  2256. ((not org-use-tag-inheritance) nil)
  2257. ((stringp org-use-tag-inheritance)
  2258. (string-match org-use-tag-inheritance tag))
  2259. ((listp org-use-tag-inheritance)
  2260. (member tag org-use-tag-inheritance))
  2261. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2262. (defcustom org-tags-match-list-sublevels t
  2263. "Non-nil means list also sublevels of headlines matching a search.
  2264. This variable applies to tags/property searches, and also to stuck
  2265. projects because this search is based on a tags match as well.
  2266. When set to the symbol `indented', sublevels are indented with
  2267. leading dots.
  2268. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2269. the sublevels of a headline matching a tag search often also match
  2270. the same search. Listing all of them can create very long lists.
  2271. Setting this variable to nil causes subtrees of a match to be skipped.
  2272. This variable is semi-obsolete and probably should always be true. It
  2273. is better to limit inheritance to certain tags using the variables
  2274. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2275. :group 'org-tags
  2276. :type '(choice
  2277. (const :tag "No, don't list them" nil)
  2278. (const :tag "Yes, do list them" t)
  2279. (const :tag "List them, indented with leading dots" indented)))
  2280. (defcustom org-tags-sort-function nil
  2281. "When set, tags are sorted using this function as a comparator"
  2282. :group 'org-tags
  2283. :type '(choice
  2284. (const :tag "No sorting" nil)
  2285. (const :tag "Alphabetical" string<)
  2286. (const :tag "Reverse alphabetical" string>)
  2287. (function :tag "Custom function" nil)))
  2288. (defvar org-tags-history nil
  2289. "History of minibuffer reads for tags.")
  2290. (defvar org-last-tags-completion-table nil
  2291. "The last used completion table for tags.")
  2292. (defvar org-after-tags-change-hook nil
  2293. "Hook that is run after the tags in a line have changed.")
  2294. (defgroup org-properties nil
  2295. "Options concerning properties in Org-mode."
  2296. :tag "Org Properties"
  2297. :group 'org)
  2298. (defcustom org-property-format "%-10s %s"
  2299. "How property key/value pairs should be formatted by `indent-line'.
  2300. When `indent-line' hits a property definition, it will format the line
  2301. according to this format, mainly to make sure that the values are
  2302. lined-up with respect to each other."
  2303. :group 'org-properties
  2304. :type 'string)
  2305. (defcustom org-use-property-inheritance nil
  2306. "Non-nil means properties apply also for sublevels.
  2307. This setting is chiefly used during property searches. Turning it on can
  2308. cause significant overhead when doing a search, which is why it is not
  2309. on by default.
  2310. When nil, only the properties directly given in the current entry count.
  2311. When t, every property is inherited. The value may also be a list of
  2312. properties that should have inheritance, or a regular expression matching
  2313. properties that should be inherited.
  2314. However, note that some special properties use inheritance under special
  2315. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2316. and the properties ending in \"_ALL\" when they are used as descriptor
  2317. for valid values of a property.
  2318. Note for programmers:
  2319. When querying an entry with `org-entry-get', you can control if inheritance
  2320. should be used. By default, `org-entry-get' looks only at the local
  2321. properties. You can request inheritance by setting the inherit argument
  2322. to t (to force inheritance) or to `selective' (to respect the setting
  2323. in this variable)."
  2324. :group 'org-properties
  2325. :type '(choice
  2326. (const :tag "Not" nil)
  2327. (const :tag "Always" t)
  2328. (repeat :tag "Specific properties" (string :tag "Property"))
  2329. (regexp :tag "Properties matched by regexp")))
  2330. (defun org-property-inherit-p (property)
  2331. "Check if PROPERTY is one that should be inherited."
  2332. (cond
  2333. ((eq org-use-property-inheritance t) t)
  2334. ((not org-use-property-inheritance) nil)
  2335. ((stringp org-use-property-inheritance)
  2336. (string-match org-use-property-inheritance property))
  2337. ((listp org-use-property-inheritance)
  2338. (member property org-use-property-inheritance))
  2339. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2340. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2341. "The default column format, if no other format has been defined.
  2342. This variable can be set on the per-file basis by inserting a line
  2343. #+COLUMNS: %25ITEM ....."
  2344. :group 'org-properties
  2345. :type 'string)
  2346. (defcustom org-columns-ellipses ".."
  2347. "The ellipses to be used when a field in column view is truncated.
  2348. When this is the empty string, as many characters as possible are shown,
  2349. but then there will be no visual indication that the field has been truncated.
  2350. When this is a string of length N, the last N characters of a truncated
  2351. field are replaced by this string. If the column is narrower than the
  2352. ellipses string, only part of the ellipses string will be shown."
  2353. :group 'org-properties
  2354. :type 'string)
  2355. (defcustom org-columns-modify-value-for-display-function nil
  2356. "Function that modifies values for display in column view.
  2357. For example, it can be used to cut out a certain part from a time stamp.
  2358. The function must take 2 arguments:
  2359. column-title The title of the column (*not* the property name)
  2360. value The value that should be modified.
  2361. The function should return the value that should be displayed,
  2362. or nil if the normal value should be used."
  2363. :group 'org-properties
  2364. :type 'function)
  2365. (defcustom org-effort-property "Effort"
  2366. "The property that is being used to keep track of effort estimates.
  2367. Effort estimates given in this property need to have the format H:MM."
  2368. :group 'org-properties
  2369. :group 'org-progress
  2370. :type '(string :tag "Property"))
  2371. (defconst org-global-properties-fixed
  2372. '(("VISIBILITY_ALL" . "folded children content all")
  2373. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2374. "List of property/value pairs that can be inherited by any entry.
  2375. These are fixed values, for the preset properties. The user variable
  2376. that can be used to add to this list is `org-global-properties'.
  2377. The entries in this list are cons cells where the car is a property
  2378. name and cdr is a string with the value. If the value represents
  2379. multiple items like an \"_ALL\" property, separate the items by
  2380. spaces.")
  2381. (defcustom org-global-properties nil
  2382. "List of property/value pairs that can be inherited by any entry.
  2383. This list will be combined with the constant `org-global-properties-fixed'.
  2384. The entries in this list are cons cells where the car is a property
  2385. name and cdr is a string with the value.
  2386. You can set buffer-local values for the same purpose in the variable
  2387. `org-file-properties' this by adding lines like
  2388. #+PROPERTY: NAME VALUE"
  2389. :group 'org-properties
  2390. :type '(repeat
  2391. (cons (string :tag "Property")
  2392. (string :tag "Value"))))
  2393. (defvar org-file-properties nil
  2394. "List of property/value pairs that can be inherited by any entry.
  2395. Valid for the current buffer.
  2396. This variable is populated from #+PROPERTY lines.")
  2397. (make-variable-buffer-local 'org-file-properties)
  2398. (defgroup org-agenda nil
  2399. "Options concerning agenda views in Org-mode."
  2400. :tag "Org Agenda"
  2401. :group 'org)
  2402. (defvar org-category nil
  2403. "Variable used by org files to set a category for agenda display.
  2404. Such files should use a file variable to set it, for example
  2405. # -*- mode: org; org-category: \"ELisp\"
  2406. or contain a special line
  2407. #+CATEGORY: ELisp
  2408. If the file does not specify a category, then file's base name
  2409. is used instead.")
  2410. (make-variable-buffer-local 'org-category)
  2411. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2412. (defcustom org-agenda-files nil
  2413. "The files to be used for agenda display.
  2414. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2415. \\[org-remove-file]. You can also use customize to edit the list.
  2416. If an entry is a directory, all files in that directory that are matched by
  2417. `org-agenda-file-regexp' will be part of the file list.
  2418. If the value of the variable is not a list but a single file name, then
  2419. the list of agenda files is actually stored and maintained in that file, one
  2420. agenda file per line. In this file paths can be given relative to
  2421. `org-directory'. Tilde expansion and environment variable substitution
  2422. are also made."
  2423. :group 'org-agenda
  2424. :type '(choice
  2425. (repeat :tag "List of files and directories" file)
  2426. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2427. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2428. "Regular expression to match files for `org-agenda-files'.
  2429. If any element in the list in that variable contains a directory instead
  2430. of a normal file, all files in that directory that are matched by this
  2431. regular expression will be included."
  2432. :group 'org-agenda
  2433. :type 'regexp)
  2434. (defcustom org-agenda-text-search-extra-files nil
  2435. "List of extra files to be searched by text search commands.
  2436. These files will be search in addition to the agenda files by the
  2437. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2438. Note that these files will only be searched for text search commands,
  2439. not for the other agenda views like todo lists, tag searches or the weekly
  2440. agenda. This variable is intended to list notes and possibly archive files
  2441. that should also be searched by these two commands.
  2442. In fact, if the first element in the list is the symbol `agenda-archives',
  2443. than all archive files of all agenda files will be added to the search
  2444. scope."
  2445. :group 'org-agenda
  2446. :type '(set :greedy t
  2447. (const :tag "Agenda Archives" agenda-archives)
  2448. (repeat :inline t (file))))
  2449. (if (fboundp 'defvaralias)
  2450. (defvaralias 'org-agenda-multi-occur-extra-files
  2451. 'org-agenda-text-search-extra-files))
  2452. (defcustom org-agenda-skip-unavailable-files nil
  2453. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2454. A nil value means to remove them, after a query, from the list."
  2455. :group 'org-agenda
  2456. :type 'boolean)
  2457. (defcustom org-calendar-to-agenda-key [?c]
  2458. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2459. The command `org-calendar-goto-agenda' will be bound to this key. The
  2460. default is the character `c' because then `c' can be used to switch back and
  2461. forth between agenda and calendar."
  2462. :group 'org-agenda
  2463. :type 'sexp)
  2464. (defcustom org-calendar-agenda-action-key [?k]
  2465. "The key to be installed in `calendar-mode-map' for agenda-action.
  2466. The command `org-agenda-action' will be bound to this key. The
  2467. default is the character `k' because we use the same key in the agenda."
  2468. :group 'org-agenda
  2469. :type 'sexp)
  2470. (defcustom org-calendar-insert-diary-entry-key [?i]
  2471. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2472. This option is irrelevant until `org-agenda-diary-file' has been configured
  2473. to point to an Org-mode file. When that is the case, the command
  2474. `org-agenda-diary-entry' will be bound to the key given here, by default
  2475. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2476. if you want to continue doing this, you need to change this to a different
  2477. key."
  2478. :group 'org-agenda
  2479. :type 'sexp)
  2480. (defcustom org-agenda-diary-file 'diary-file
  2481. "File to which to add new entries with the `i' key in agenda and calendar.
  2482. When this is the symbol `diary-file', the functionality in the Emacs
  2483. calendar will be used to add entries to the `diary-file'. But when this
  2484. points to a file, `org-agenda-diary-entry' will be used instead."
  2485. :group 'org-agenda
  2486. :type '(choice
  2487. (const :tag "The standard Emacs diary file" diary-file)
  2488. (file :tag "Special Org file diary entries")))
  2489. (eval-after-load "calendar"
  2490. '(progn
  2491. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2492. 'org-calendar-goto-agenda)
  2493. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2494. 'org-agenda-action)
  2495. (add-hook 'calendar-mode-hook
  2496. (lambda ()
  2497. (unless (eq org-agenda-diary-file 'diary-file)
  2498. (define-key calendar-mode-map
  2499. org-calendar-insert-diary-entry-key
  2500. 'org-agenda-diary-entry))))))
  2501. (defgroup org-latex nil
  2502. "Options for embedding LaTeX code into Org-mode."
  2503. :tag "Org LaTeX"
  2504. :group 'org)
  2505. (defcustom org-format-latex-options
  2506. '(:foreground default :background default :scale 1.0
  2507. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2508. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2509. "Options for creating images from LaTeX fragments.
  2510. This is a property list with the following properties:
  2511. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2512. `default' means use the foreground of the default face.
  2513. :background the background color, or \"Transparent\".
  2514. `default' means use the background of the default face.
  2515. :scale a scaling factor for the size of the images.
  2516. :html-foreground, :html-background, :html-scale
  2517. the same numbers for HTML export.
  2518. :matchers a list indicating which matchers should be used to
  2519. find LaTeX fragments. Valid members of this list are:
  2520. \"begin\" find environments
  2521. \"$1\" find single characters surrounded by $.$
  2522. \"$\" find math expressions surrounded by $...$
  2523. \"$$\" find math expressions surrounded by $$....$$
  2524. \"\\(\" find math expressions surrounded by \\(...\\)
  2525. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2526. :group 'org-latex
  2527. :type 'plist)
  2528. (defcustom org-format-latex-signal-error t
  2529. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2530. When nil, just push out a message."
  2531. :group 'org-latex
  2532. :type 'boolean)
  2533. (defcustom org-format-latex-header "\\documentclass{article}
  2534. \\usepackage[usenames]{color}
  2535. \\usepackage{amsmath}
  2536. \\usepackage[mathscr]{eucal}
  2537. \\pagestyle{empty} % do not remove
  2538. \[PACKAGES]
  2539. \[DEFAULT-PACKAGES]
  2540. % The settings below are copied from fullpage.sty
  2541. \\setlength{\\textwidth}{\\paperwidth}
  2542. \\addtolength{\\textwidth}{-3cm}
  2543. \\setlength{\\oddsidemargin}{1.5cm}
  2544. \\addtolength{\\oddsidemargin}{-2.54cm}
  2545. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2546. \\setlength{\\textheight}{\\paperheight}
  2547. \\addtolength{\\textheight}{-\\headheight}
  2548. \\addtolength{\\textheight}{-\\headsep}
  2549. \\addtolength{\\textheight}{-\\footskip}
  2550. \\addtolength{\\textheight}{-3cm}
  2551. \\setlength{\\topmargin}{1.5cm}
  2552. \\addtolength{\\topmargin}{-2.54cm}"
  2553. "The document header used for processing LaTeX fragments.
  2554. It is imperative that this header make sure that no page number
  2555. appears on the page. The package defined in the variables
  2556. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2557. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2558. will be appended."
  2559. :group 'org-latex
  2560. :type 'string)
  2561. (defvar org-format-latex-header-extra nil)
  2562. (defun org-set-packages-alist (var val)
  2563. "Set the packages alist and make sure it has 3 elements per entry."
  2564. (set var (mapcar (lambda (x)
  2565. (if (and (consp x) (= (length x) 2))
  2566. (list (car x) (nth 1 x) t)
  2567. x))
  2568. val)))
  2569. (defun org-get-packages-alist (var)
  2570. "Get the packages alist and make sure it has 3 elements per entry."
  2571. (mapcar (lambda (x)
  2572. (if (and (consp x) (= (length x) 2))
  2573. (list (car x) (nth 1 x) t)
  2574. x))
  2575. (default-value var)))
  2576. ;; The following variables are defined here because is it also used
  2577. ;; when formatting latex fragments. Originally it was part of the
  2578. ;; LaTeX exporter, which is why the name includes "export".
  2579. (defcustom org-export-latex-default-packages-alist
  2580. '(("AUTO" "inputenc" t)
  2581. ("T1" "fontenc" t)
  2582. ("" "fixltx2e" nil)
  2583. ("" "graphicx" t)
  2584. ("" "longtable" nil)
  2585. ("" "float" nil)
  2586. ("" "wrapfig" nil)
  2587. ("" "soul" t)
  2588. ("" "t1enc" t)
  2589. ("" "textcomp" t)
  2590. ("" "marvosym" t)
  2591. ("" "wasysym" t)
  2592. ("" "latexsym" t)
  2593. ("" "amssymb" t)
  2594. ("" "hyperref" nil)
  2595. "\\tolerance=1000"
  2596. )
  2597. "Alist of default packages to be inserted in the header.
  2598. Change this only if one of the packages here causes an incompatibility
  2599. with another package you are using.
  2600. The packages in this list are needed by one part or another of Org-mode
  2601. to function properly.
  2602. - inputenc, fontenc, t1enc: for basic font and character selection
  2603. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2604. for interpreting the entities in `org-entities'. You can skip some of these
  2605. packages if you don't use any of the symbols in it.
  2606. - graphicx: for including images
  2607. - float, wrapfig: for figure placement
  2608. - longtable: for long tables
  2609. - hyperref: for cross references
  2610. Therefore you should not modify this variable unless you know what you
  2611. are doing. The one reason to change it anyway is that you might be loading
  2612. some other package that conflicts with one of the default packages.
  2613. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2614. If SNIPPET-FLAG is t, the package also needs to be included when
  2615. compiling LaTeX snippets into images for inclusion into HTML."
  2616. :group 'org-export-latex
  2617. :set 'org-set-packages-alist
  2618. :get 'org-get-packages-alist
  2619. :type '(repeat
  2620. (choice
  2621. (list :tag "options/package pair"
  2622. (string :tag "options")
  2623. (string :tag "package")
  2624. (boolean :tag "Snippet"))
  2625. (string :tag "A line of LaTeX"))))
  2626. (defcustom org-export-latex-packages-alist nil
  2627. "Alist of packages to be inserted in every LaTeX header.
  2628. These will be inserted after `org-export-latex-default-packages-alist'.
  2629. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2630. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2631. turning LaTeX snippets into images for inclusion into HTML.
  2632. Make sure that you only list packages here which:
  2633. - you want in every file
  2634. - do not conflict with the default packages in
  2635. `org-export-latex-default-packages-alist'
  2636. - do not conflict with the setup in `org-format-latex-header'."
  2637. :group 'org-export-latex
  2638. :set 'org-set-packages-alist
  2639. :get 'org-get-packages-alist
  2640. :type '(repeat
  2641. (choice
  2642. (list :tag "options/package pair"
  2643. (string :tag "options")
  2644. (string :tag "package")
  2645. (boolean :tag "Snippet"))
  2646. (string :tag "A line of LaTeX"))))
  2647. (defgroup org-appearance nil
  2648. "Settings for Org-mode appearance."
  2649. :tag "Org Appearance"
  2650. :group 'org)
  2651. (defcustom org-level-color-stars-only nil
  2652. "Non-nil means fontify only the stars in each headline.
  2653. When nil, the entire headline is fontified.
  2654. Changing it requires restart of `font-lock-mode' to become effective
  2655. also in regions already fontified."
  2656. :group 'org-appearance
  2657. :type 'boolean)
  2658. (defcustom org-hide-leading-stars nil
  2659. "Non-nil means hide the first N-1 stars in a headline.
  2660. This works by using the face `org-hide' for these stars. This
  2661. face is white for a light background, and black for a dark
  2662. background. You may have to customize the face `org-hide' to
  2663. make this work.
  2664. Changing it requires restart of `font-lock-mode' to become effective
  2665. also in regions already fontified.
  2666. You may also set this on a per-file basis by adding one of the following
  2667. lines to the buffer:
  2668. #+STARTUP: hidestars
  2669. #+STARTUP: showstars"
  2670. :group 'org-appearance
  2671. :type 'boolean)
  2672. (defcustom org-hidden-keywords nil
  2673. "List of keywords that should be hidden when typed in the org buffer.
  2674. For example, add #+TITLE to this list in order to make the
  2675. document title appear in the buffer without the initial #+TITLE:
  2676. keyword."
  2677. :group 'org-appearance
  2678. :type '(set (const :tag "#+AUTHOR" author)
  2679. (const :tag "#+DATE" date)
  2680. (const :tag "#+EMAIL" email)
  2681. (const :tag "#+TITLE" title)))
  2682. (defcustom org-fontify-done-headline nil
  2683. "Non-nil means change the face of a headline if it is marked DONE.
  2684. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2685. When this is non-nil, the headline after the keyword is set to the
  2686. `org-headline-done' as an additional indication."
  2687. :group 'org-appearance
  2688. :type 'boolean)
  2689. (defcustom org-fontify-emphasized-text t
  2690. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2691. Changing this variable requires a restart of Emacs to take effect."
  2692. :group 'org-appearance
  2693. :type 'boolean)
  2694. (defcustom org-fontify-whole-heading-line nil
  2695. "Non-nil means fontify the whole line for headings.
  2696. This is useful when setting a background color for the
  2697. org-level-* faces."
  2698. :group 'org-appearance
  2699. :type 'boolean)
  2700. (defcustom org-highlight-latex-fragments-and-specials nil
  2701. "Non-nil means fontify what is treated specially by the exporters."
  2702. :group 'org-appearance
  2703. :type 'boolean)
  2704. (defcustom org-hide-emphasis-markers nil
  2705. "Non-nil mean font-lock should hide the emphasis marker characters."
  2706. :group 'org-appearance
  2707. :type 'boolean)
  2708. (defvar org-emph-re nil
  2709. "Regular expression for matching emphasis.")
  2710. (defvar org-verbatim-re nil
  2711. "Regular expression for matching verbatim text.")
  2712. (defvar org-emphasis-regexp-components) ; defined just below
  2713. (defvar org-emphasis-alist) ; defined just below
  2714. (defun org-set-emph-re (var val)
  2715. "Set variable and compute the emphasis regular expression."
  2716. (set var val)
  2717. (when (and (boundp 'org-emphasis-alist)
  2718. (boundp 'org-emphasis-regexp-components)
  2719. org-emphasis-alist org-emphasis-regexp-components)
  2720. (let* ((e org-emphasis-regexp-components)
  2721. (pre (car e))
  2722. (post (nth 1 e))
  2723. (border (nth 2 e))
  2724. (body (nth 3 e))
  2725. (nl (nth 4 e))
  2726. (body1 (concat body "*?"))
  2727. (markers (mapconcat 'car org-emphasis-alist ""))
  2728. (vmarkers (mapconcat
  2729. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2730. org-emphasis-alist "")))
  2731. ;; make sure special characters appear at the right position in the class
  2732. (if (string-match "\\^" markers)
  2733. (setq markers (concat (replace-match "" t t markers) "^")))
  2734. (if (string-match "-" markers)
  2735. (setq markers (concat (replace-match "" t t markers) "-")))
  2736. (if (string-match "\\^" vmarkers)
  2737. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2738. (if (string-match "-" vmarkers)
  2739. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2740. (if (> nl 0)
  2741. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2742. (int-to-string nl) "\\}")))
  2743. ;; Make the regexp
  2744. (setq org-emph-re
  2745. (concat "\\([" pre "]\\|^\\)"
  2746. "\\("
  2747. "\\([" markers "]\\)"
  2748. "\\("
  2749. "[^" border "]\\|"
  2750. "[^" border "]"
  2751. body1
  2752. "[^" border "]"
  2753. "\\)"
  2754. "\\3\\)"
  2755. "\\([" post "]\\|$\\)"))
  2756. (setq org-verbatim-re
  2757. (concat "\\([" pre "]\\|^\\)"
  2758. "\\("
  2759. "\\([" vmarkers "]\\)"
  2760. "\\("
  2761. "[^" border "]\\|"
  2762. "[^" border "]"
  2763. body1
  2764. "[^" border "]"
  2765. "\\)"
  2766. "\\3\\)"
  2767. "\\([" post "]\\|$\\)")))))
  2768. (defcustom org-emphasis-regexp-components
  2769. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2770. "Components used to build the regular expression for emphasis.
  2771. This is a list with 6 entries. Terminology: In an emphasis string
  2772. like \" *strong word* \", we call the initial space PREMATCH, the final
  2773. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2774. and \"trong wor\" is the body. The different components in this variable
  2775. specify what is allowed/forbidden in each part:
  2776. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2777. post Chars allowed as postmatch. End of line will be allowed too.
  2778. border The chars *forbidden* as border characters.
  2779. body-regexp A regexp like \".\" to match a body character. Don't use
  2780. non-shy groups here, and don't allow newline here.
  2781. newline The maximum number of newlines allowed in an emphasis exp.
  2782. Use customize to modify this, or restart Emacs after changing it."
  2783. :group 'org-appearance
  2784. :set 'org-set-emph-re
  2785. :type '(list
  2786. (sexp :tag "Allowed chars in pre ")
  2787. (sexp :tag "Allowed chars in post ")
  2788. (sexp :tag "Forbidden chars in border ")
  2789. (sexp :tag "Regexp for body ")
  2790. (integer :tag "number of newlines allowed")
  2791. (option (boolean :tag "Please ignore this button"))))
  2792. (defcustom org-emphasis-alist
  2793. `(("*" bold "<b>" "</b>")
  2794. ("/" italic "<i>" "</i>")
  2795. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2796. ("=" org-code "<code>" "</code>" verbatim)
  2797. ("~" org-verbatim "<code>" "</code>" verbatim)
  2798. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2799. "<del>" "</del>")
  2800. )
  2801. "Special syntax for emphasized text.
  2802. Text starting and ending with a special character will be emphasized, for
  2803. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2804. characters, the face to be used by font-lock for highlighting in Org-mode
  2805. Emacs buffers, and the HTML tags to be used for this.
  2806. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2807. Use customize to modify this, or restart Emacs after changing it."
  2808. :group 'org-appearance
  2809. :set 'org-set-emph-re
  2810. :type '(repeat
  2811. (list
  2812. (string :tag "Marker character")
  2813. (choice
  2814. (face :tag "Font-lock-face")
  2815. (plist :tag "Face property list"))
  2816. (string :tag "HTML start tag")
  2817. (string :tag "HTML end tag")
  2818. (option (const verbatim)))))
  2819. (defvar org-protecting-blocks
  2820. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2821. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2822. This is needed for font-lock setup.")
  2823. ;;; Miscellaneous options
  2824. (defgroup org-completion nil
  2825. "Completion in Org-mode."
  2826. :tag "Org Completion"
  2827. :group 'org)
  2828. (defcustom org-completion-use-ido nil
  2829. "Non-nil means use ido completion wherever possible.
  2830. Note that `ido-mode' must be active for this variable to be relevant.
  2831. If you decide to turn this variable on, you might well want to turn off
  2832. `org-outline-path-complete-in-steps'.
  2833. See also `org-completion-use-iswitchb'."
  2834. :group 'org-completion
  2835. :type 'boolean)
  2836. (defcustom org-completion-use-iswitchb nil
  2837. "Non-nil means use iswitchb completion wherever possible.
  2838. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2839. If you decide to turn this variable on, you might well want to turn off
  2840. `org-outline-path-complete-in-steps'.
  2841. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2842. :group 'org-completion
  2843. :type 'boolean)
  2844. (defcustom org-completion-fallback-command 'hippie-expand
  2845. "The expansion command called by \\[org-complete] in normal context.
  2846. Normal means no org-mode-specific context."
  2847. :group 'org-completion
  2848. :type 'function)
  2849. ;;; Functions and variables from their packages
  2850. ;; Declared here to avoid compiler warnings
  2851. ;; XEmacs only
  2852. (defvar outline-mode-menu-heading)
  2853. (defvar outline-mode-menu-show)
  2854. (defvar outline-mode-menu-hide)
  2855. (defvar zmacs-regions) ; XEmacs regions
  2856. ;; Emacs only
  2857. (defvar mark-active)
  2858. ;; Various packages
  2859. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2860. (declare-function calendar-forward-day "cal-move" (arg))
  2861. (declare-function calendar-goto-date "cal-move" (date))
  2862. (declare-function calendar-goto-today "cal-move" ())
  2863. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2864. (defvar calc-embedded-close-formula)
  2865. (defvar calc-embedded-open-formula)
  2866. (declare-function cdlatex-tab "ext:cdlatex" ())
  2867. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2868. (defvar font-lock-unfontify-region-function)
  2869. (declare-function iswitchb-read-buffer "iswitchb"
  2870. (prompt &optional default require-match start matches-set))
  2871. (defvar iswitchb-temp-buflist)
  2872. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2873. (defvar org-agenda-tags-todo-honor-ignore-options)
  2874. (declare-function org-agenda-skip "org-agenda" ())
  2875. (declare-function
  2876. org-format-agenda-item "org-agenda"
  2877. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2878. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2879. (declare-function org-agenda-change-all-lines "org-agenda"
  2880. (newhead hdmarker &optional fixface just-this))
  2881. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2882. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2883. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2884. (beg end))
  2885. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2886. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2887. "org-agenda" (&optional end))
  2888. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2889. (declare-function org-indent-mode "org-indent" (&optional arg))
  2890. (declare-function parse-time-string "parse-time" (string))
  2891. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2892. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  2893. (defvar remember-data-file)
  2894. (defvar texmathp-why)
  2895. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2896. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2897. (defvar w3m-current-url)
  2898. (defvar w3m-current-title)
  2899. (defvar org-latex-regexps)
  2900. ;;; Autoload and prepare some org modules
  2901. ;; Some table stuff that needs to be defined here, because it is used
  2902. ;; by the functions setting up org-mode or checking for table context.
  2903. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2904. "Detects an org-type or table-type table.")
  2905. (defconst org-table-line-regexp "^[ \t]*|"
  2906. "Detects an org-type table line.")
  2907. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2908. "Detects an org-type table line.")
  2909. (defconst org-table-hline-regexp "^[ \t]*|-"
  2910. "Detects an org-type table hline.")
  2911. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2912. "Detects a table-type table hline.")
  2913. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2914. "Searching from within a table (any type) this finds the first line
  2915. outside the table.")
  2916. ;; Autoload the functions in org-table.el that are needed by functions here.
  2917. (eval-and-compile
  2918. (org-autoload "org-table"
  2919. '(org-table-align org-table-begin org-table-blank-field
  2920. org-table-convert org-table-convert-region org-table-copy-down
  2921. org-table-copy-region org-table-create
  2922. org-table-create-or-convert-from-region
  2923. org-table-create-with-table.el org-table-current-dline
  2924. org-table-cut-region org-table-delete-column org-table-edit-field
  2925. org-table-edit-formulas org-table-end org-table-eval-formula
  2926. org-table-export org-table-field-info
  2927. org-table-get-stored-formulas org-table-goto-column
  2928. org-table-hline-and-move org-table-import org-table-insert-column
  2929. org-table-insert-hline org-table-insert-row org-table-iterate
  2930. org-table-justify-field-maybe org-table-kill-row
  2931. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2932. org-table-move-column org-table-move-column-left
  2933. org-table-move-column-right org-table-move-row
  2934. org-table-move-row-down org-table-move-row-up
  2935. org-table-next-field org-table-next-row org-table-paste-rectangle
  2936. org-table-previous-field org-table-recalculate
  2937. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2938. org-table-toggle-coordinate-overlays
  2939. org-table-toggle-formula-debugger org-table-wrap-region
  2940. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2941. (defun org-at-table-p (&optional table-type)
  2942. "Return t if the cursor is inside an org-type table.
  2943. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2944. (if org-enable-table-editor
  2945. (save-excursion
  2946. (beginning-of-line 1)
  2947. (looking-at (if table-type org-table-any-line-regexp
  2948. org-table-line-regexp)))
  2949. nil))
  2950. (defsubst org-table-p () (org-at-table-p))
  2951. (defun org-at-table.el-p ()
  2952. "Return t if and only if we are at a table.el table."
  2953. (and (org-at-table-p 'any)
  2954. (save-excursion
  2955. (goto-char (org-table-begin 'any))
  2956. (looking-at org-table1-hline-regexp))))
  2957. (defun org-table-recognize-table.el ()
  2958. "If there is a table.el table nearby, recognize it and move into it."
  2959. (if org-table-tab-recognizes-table.el
  2960. (if (org-at-table.el-p)
  2961. (progn
  2962. (beginning-of-line 1)
  2963. (if (looking-at org-table-dataline-regexp)
  2964. nil
  2965. (if (looking-at org-table1-hline-regexp)
  2966. (progn
  2967. (beginning-of-line 2)
  2968. (if (looking-at org-table-any-border-regexp)
  2969. (beginning-of-line -1)))))
  2970. (if (re-search-forward "|" (org-table-end t) t)
  2971. (progn
  2972. (require 'table)
  2973. (if (table--at-cell-p (point))
  2974. t
  2975. (message "recognizing table.el table...")
  2976. (table-recognize-table)
  2977. (message "recognizing table.el table...done")))
  2978. (error "This should not happen..."))
  2979. t)
  2980. nil)
  2981. nil))
  2982. (defun org-at-table-hline-p ()
  2983. "Return t if the cursor is inside a hline in a table."
  2984. (if org-enable-table-editor
  2985. (save-excursion
  2986. (beginning-of-line 1)
  2987. (looking-at org-table-hline-regexp))
  2988. nil))
  2989. (defvar org-table-clean-did-remove-column nil)
  2990. (defun org-table-map-tables (function &optional quietly)
  2991. "Apply FUNCTION to the start of all tables in the buffer."
  2992. (save-excursion
  2993. (save-restriction
  2994. (widen)
  2995. (goto-char (point-min))
  2996. (while (re-search-forward org-table-any-line-regexp nil t)
  2997. (unless quietly
  2998. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  2999. (beginning-of-line 1)
  3000. (when (looking-at org-table-line-regexp)
  3001. (save-excursion (funcall function))
  3002. (or (looking-at org-table-line-regexp)
  3003. (forward-char 1)))
  3004. (re-search-forward org-table-any-border-regexp nil 1))))
  3005. (unless quietly (message "Mapping tables: done")))
  3006. ;; Declare and autoload functions from org-exp.el & Co
  3007. (declare-function org-default-export-plist "org-exp")
  3008. (declare-function org-infile-export-plist "org-exp")
  3009. (declare-function org-get-current-options "org-exp")
  3010. (eval-and-compile
  3011. (org-autoload "org-exp"
  3012. '(org-export org-export-visible
  3013. org-insert-export-options-template
  3014. org-table-clean-before-export))
  3015. (org-autoload "org-ascii"
  3016. '(org-export-as-ascii org-export-ascii-preprocess
  3017. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3018. org-export-region-as-ascii))
  3019. (org-autoload "org-latex"
  3020. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3021. org-replace-region-by-latex org-export-region-as-latex
  3022. org-export-as-latex org-export-as-pdf
  3023. org-export-as-pdf-and-open))
  3024. (org-autoload "org-html"
  3025. '(org-export-as-html-and-open
  3026. org-export-as-html-batch org-export-as-html-to-buffer
  3027. org-replace-region-by-html org-export-region-as-html
  3028. org-export-as-html))
  3029. (org-autoload "org-docbook"
  3030. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3031. org-replace-region-by-docbook org-export-region-as-docbook
  3032. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3033. org-export-as-docbook))
  3034. (org-autoload "org-icalendar"
  3035. '(org-export-icalendar-this-file
  3036. org-export-icalendar-all-agenda-files
  3037. org-export-icalendar-combine-agenda-files))
  3038. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3039. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3040. ;; Declare and autoload functions from org-agenda.el
  3041. (eval-and-compile
  3042. (org-autoload "org-agenda"
  3043. '(org-agenda org-agenda-list org-search-view
  3044. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3045. org-diary org-agenda-to-appt
  3046. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3047. ;; Autoload org-remember
  3048. (eval-and-compile
  3049. (org-autoload "org-remember"
  3050. '(org-remember-insinuate org-remember-annotation
  3051. org-remember-apply-template org-remember org-remember-handler)))
  3052. ;; Autoload org-clock.el
  3053. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3054. (beg end))
  3055. (declare-function org-clock-update-mode-line "org-clock" ())
  3056. (declare-function org-resolve-clocks "org-clock"
  3057. (&optional also-non-dangling-p prompt last-valid))
  3058. (defvar org-clock-start-time)
  3059. (defvar org-clock-marker (make-marker)
  3060. "Marker recording the last clock-in.")
  3061. (defvar org-clock-hd-marker (make-marker)
  3062. "Marker recording the last clock-in, but the headline position.")
  3063. (defvar org-clock-heading ""
  3064. "The heading of the current clock entry.")
  3065. (defun org-clock-is-active ()
  3066. "Return non-nil if clock is currently running.
  3067. The return value is actually the clock marker."
  3068. (marker-buffer org-clock-marker))
  3069. (eval-and-compile
  3070. (org-autoload
  3071. "org-clock"
  3072. '(org-clock-in org-clock-out org-clock-cancel
  3073. org-clock-goto org-clock-sum org-clock-display
  3074. org-clock-remove-overlays org-clock-report
  3075. org-clocktable-shift org-dblock-write:clocktable
  3076. org-get-clocktable org-resolve-clocks)))
  3077. (defun org-clock-update-time-maybe ()
  3078. "If this is a CLOCK line, update it and return t.
  3079. Otherwise, return nil."
  3080. (interactive)
  3081. (save-excursion
  3082. (beginning-of-line 1)
  3083. (skip-chars-forward " \t")
  3084. (when (looking-at org-clock-string)
  3085. (let ((re (concat "[ \t]*" org-clock-string
  3086. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3087. "\\([ \t]*=>.*\\)?\\)?"))
  3088. ts te h m s neg)
  3089. (cond
  3090. ((not (looking-at re))
  3091. nil)
  3092. ((not (match-end 2))
  3093. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3094. (> org-clock-marker (point))
  3095. (<= org-clock-marker (point-at-eol)))
  3096. ;; The clock is running here
  3097. (setq org-clock-start-time
  3098. (apply 'encode-time
  3099. (org-parse-time-string (match-string 1))))
  3100. (org-clock-update-mode-line)))
  3101. (t
  3102. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3103. (end-of-line 1)
  3104. (setq ts (match-string 1)
  3105. te (match-string 3))
  3106. (setq s (- (org-float-time
  3107. (apply 'encode-time (org-parse-time-string te)))
  3108. (org-float-time
  3109. (apply 'encode-time (org-parse-time-string ts))))
  3110. neg (< s 0)
  3111. s (abs s)
  3112. h (floor (/ s 3600))
  3113. s (- s (* 3600 h))
  3114. m (floor (/ s 60))
  3115. s (- s (* 60 s)))
  3116. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3117. t))))))
  3118. (defun org-check-running-clock ()
  3119. "Check if the current buffer contains the running clock.
  3120. If yes, offer to stop it and to save the buffer with the changes."
  3121. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3122. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3123. (buffer-name))))
  3124. (org-clock-out)
  3125. (when (y-or-n-p "Save changed buffer?")
  3126. (save-buffer))))
  3127. (defun org-clocktable-try-shift (dir n)
  3128. "Check if this line starts a clock table, if yes, shift the time block."
  3129. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3130. (org-clocktable-shift dir n)))
  3131. ;; Autoload org-timer.el
  3132. (eval-and-compile
  3133. (org-autoload
  3134. "org-timer"
  3135. '(org-timer-start org-timer org-timer-item
  3136. org-timer-change-times-in-region
  3137. org-timer-set-timer
  3138. org-timer-reset-timers
  3139. org-timer-show-remaining-time)))
  3140. ;; Autoload org-feed.el
  3141. (eval-and-compile
  3142. (org-autoload
  3143. "org-feed"
  3144. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3145. ;; Autoload org-indent.el
  3146. ;; Define the variable already here, to make sure we have it.
  3147. (defvar org-indent-mode nil
  3148. "Non-nil if Org-Indent mode is enabled.
  3149. Use the command `org-indent-mode' to change this variable.")
  3150. (eval-and-compile
  3151. (org-autoload
  3152. "org-indent"
  3153. '(org-indent-mode)))
  3154. ;; Autoload org-mobile.el
  3155. (eval-and-compile
  3156. (org-autoload
  3157. "org-mobile"
  3158. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3159. ;; Autoload archiving code
  3160. ;; The stuff that is needed for cycling and tags has to be defined here.
  3161. (defgroup org-archive nil
  3162. "Options concerning archiving in Org-mode."
  3163. :tag "Org Archive"
  3164. :group 'org-structure)
  3165. (defcustom org-archive-location "%s_archive::"
  3166. "The location where subtrees should be archived.
  3167. The value of this variable is a string, consisting of two parts,
  3168. separated by a double-colon. The first part is a filename and
  3169. the second part is a headline.
  3170. When the filename is omitted, archiving happens in the same file.
  3171. %s in the filename will be replaced by the current file
  3172. name (without the directory part). Archiving to a different file
  3173. is useful to keep archived entries from contributing to the
  3174. Org-mode Agenda.
  3175. The archived entries will be filed as subtrees of the specified
  3176. headline. When the headline is omitted, the subtrees are simply
  3177. filed away at the end of the file, as top-level entries. Also in
  3178. the heading you can use %s to represent the file name, this can be
  3179. useful when using the same archive for a number of different files.
  3180. Here are a few examples:
  3181. \"%s_archive::\"
  3182. If the current file is Projects.org, archive in file
  3183. Projects.org_archive, as top-level trees. This is the default.
  3184. \"::* Archived Tasks\"
  3185. Archive in the current file, under the top-level headline
  3186. \"* Archived Tasks\".
  3187. \"~/org/archive.org::\"
  3188. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3189. \"~/org/archive.org::From %s\"
  3190. Archive in file ~/org/archive.org (absolute path), under headlines
  3191. \"From FILENAME\" where file name is the current file name.
  3192. \"basement::** Finished Tasks\"
  3193. Archive in file ./basement (relative path), as level 3 trees
  3194. below the level 2 heading \"** Finished Tasks\".
  3195. You may set this option on a per-file basis by adding to the buffer a
  3196. line like
  3197. #+ARCHIVE: basement::** Finished Tasks
  3198. You may also define it locally for a subtree by setting an ARCHIVE property
  3199. in the entry. If such a property is found in an entry, or anywhere up
  3200. the hierarchy, it will be used."
  3201. :group 'org-archive
  3202. :type 'string)
  3203. (defcustom org-archive-tag "ARCHIVE"
  3204. "The tag that marks a subtree as archived.
  3205. An archived subtree does not open during visibility cycling, and does
  3206. not contribute to the agenda listings.
  3207. After changing this, font-lock must be restarted in the relevant buffers to
  3208. get the proper fontification."
  3209. :group 'org-archive
  3210. :group 'org-keywords
  3211. :type 'string)
  3212. (defcustom org-agenda-skip-archived-trees t
  3213. "Non-nil means the agenda will skip any items located in archived trees.
  3214. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3215. variable is no longer recommended, you should leave it at the value t.
  3216. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3217. :group 'org-archive
  3218. :group 'org-agenda-skip
  3219. :type 'boolean)
  3220. (defcustom org-columns-skip-archived-trees t
  3221. "Non-nil means ignore archived trees when creating column view."
  3222. :group 'org-archive
  3223. :group 'org-properties
  3224. :type 'boolean)
  3225. (defcustom org-cycle-open-archived-trees nil
  3226. "Non-nil means `org-cycle' will open archived trees.
  3227. An archived tree is a tree marked with the tag ARCHIVE.
  3228. When nil, archived trees will stay folded. You can still open them with
  3229. normal outline commands like `show-all', but not with the cycling commands."
  3230. :group 'org-archive
  3231. :group 'org-cycle
  3232. :type 'boolean)
  3233. (defcustom org-sparse-tree-open-archived-trees nil
  3234. "Non-nil means sparse tree construction shows matches in archived trees.
  3235. When nil, matches in these trees are highlighted, but the trees are kept in
  3236. collapsed state."
  3237. :group 'org-archive
  3238. :group 'org-sparse-trees
  3239. :type 'boolean)
  3240. (defun org-cycle-hide-archived-subtrees (state)
  3241. "Re-hide all archived subtrees after a visibility state change."
  3242. (when (and (not org-cycle-open-archived-trees)
  3243. (not (memq state '(overview folded))))
  3244. (save-excursion
  3245. (let* ((globalp (memq state '(contents all)))
  3246. (beg (if globalp (point-min) (point)))
  3247. (end (if globalp (point-max) (org-end-of-subtree t))))
  3248. (org-hide-archived-subtrees beg end)
  3249. (goto-char beg)
  3250. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3251. (message "%s" (substitute-command-keys
  3252. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3253. (defun org-force-cycle-archived ()
  3254. "Cycle subtree even if it is archived."
  3255. (interactive)
  3256. (setq this-command 'org-cycle)
  3257. (let ((org-cycle-open-archived-trees t))
  3258. (call-interactively 'org-cycle)))
  3259. (defun org-hide-archived-subtrees (beg end)
  3260. "Re-hide all archived subtrees after a visibility state change."
  3261. (save-excursion
  3262. (let* ((re (concat ":" org-archive-tag ":")))
  3263. (goto-char beg)
  3264. (while (re-search-forward re end t)
  3265. (when (org-on-heading-p)
  3266. (org-flag-subtree t)
  3267. (org-end-of-subtree t))))))
  3268. (defun org-flag-subtree (flag)
  3269. (save-excursion
  3270. (org-back-to-heading t)
  3271. (outline-end-of-heading)
  3272. (outline-flag-region (point)
  3273. (progn (org-end-of-subtree t) (point))
  3274. flag)))
  3275. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3276. (eval-and-compile
  3277. (org-autoload "org-archive"
  3278. '(org-add-archive-files org-archive-subtree
  3279. org-archive-to-archive-sibling org-toggle-archive-tag
  3280. org-archive-subtree-default
  3281. org-archive-subtree-default-with-confirmation)))
  3282. ;; Autoload Column View Code
  3283. (declare-function org-columns-number-to-string "org-colview")
  3284. (declare-function org-columns-get-format-and-top-level "org-colview")
  3285. (declare-function org-columns-compute "org-colview")
  3286. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3287. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3288. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3289. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3290. ;; Autoload ID code
  3291. (declare-function org-id-store-link "org-id")
  3292. (declare-function org-id-locations-load "org-id")
  3293. (declare-function org-id-locations-save "org-id")
  3294. (defvar org-id-track-globally)
  3295. (org-autoload "org-id"
  3296. '(org-id-get-create org-id-new org-id-copy org-id-get
  3297. org-id-get-with-outline-path-completion
  3298. org-id-get-with-outline-drilling
  3299. org-id-goto org-id-find org-id-store-link))
  3300. ;; Autoload Plotting Code
  3301. (org-autoload "org-plot"
  3302. '(org-plot/gnuplot))
  3303. ;;; Variables for pre-computed regular expressions, all buffer local
  3304. (defvar org-drawer-regexp nil
  3305. "Matches first line of a hidden block.")
  3306. (make-variable-buffer-local 'org-drawer-regexp)
  3307. (defvar org-todo-regexp nil
  3308. "Matches any of the TODO state keywords.")
  3309. (make-variable-buffer-local 'org-todo-regexp)
  3310. (defvar org-not-done-regexp nil
  3311. "Matches any of the TODO state keywords except the last one.")
  3312. (make-variable-buffer-local 'org-not-done-regexp)
  3313. (defvar org-not-done-heading-regexp nil
  3314. "Matches a TODO headline that is not done.")
  3315. (make-variable-buffer-local 'org-not-done-regexp)
  3316. (defvar org-todo-line-regexp nil
  3317. "Matches a headline and puts TODO state into group 2 if present.")
  3318. (make-variable-buffer-local 'org-todo-line-regexp)
  3319. (defvar org-complex-heading-regexp nil
  3320. "Matches a headline and puts everything into groups:
  3321. group 1: the stars
  3322. group 2: The todo keyword, maybe
  3323. group 3: Priority cookie
  3324. group 4: True headline
  3325. group 5: Tags")
  3326. (make-variable-buffer-local 'org-complex-heading-regexp)
  3327. (defvar org-complex-heading-regexp-format nil)
  3328. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3329. (defvar org-todo-line-tags-regexp nil
  3330. "Matches a headline and puts TODO state into group 2 if present.
  3331. Also put tags into group 4 if tags are present.")
  3332. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3333. (defvar org-nl-done-regexp nil
  3334. "Matches newline followed by a headline with the DONE keyword.")
  3335. (make-variable-buffer-local 'org-nl-done-regexp)
  3336. (defvar org-looking-at-done-regexp nil
  3337. "Matches the DONE keyword a point.")
  3338. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3339. (defvar org-ds-keyword-length 12
  3340. "Maximum length of the Deadline and SCHEDULED keywords.")
  3341. (make-variable-buffer-local 'org-ds-keyword-length)
  3342. (defvar org-deadline-regexp nil
  3343. "Matches the DEADLINE keyword.")
  3344. (make-variable-buffer-local 'org-deadline-regexp)
  3345. (defvar org-deadline-time-regexp nil
  3346. "Matches the DEADLINE keyword together with a time stamp.")
  3347. (make-variable-buffer-local 'org-deadline-time-regexp)
  3348. (defvar org-deadline-line-regexp nil
  3349. "Matches the DEADLINE keyword and the rest of the line.")
  3350. (make-variable-buffer-local 'org-deadline-line-regexp)
  3351. (defvar org-scheduled-regexp nil
  3352. "Matches the SCHEDULED keyword.")
  3353. (make-variable-buffer-local 'org-scheduled-regexp)
  3354. (defvar org-scheduled-time-regexp nil
  3355. "Matches the SCHEDULED keyword together with a time stamp.")
  3356. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3357. (defvar org-closed-time-regexp nil
  3358. "Matches the CLOSED keyword together with a time stamp.")
  3359. (make-variable-buffer-local 'org-closed-time-regexp)
  3360. (defvar org-keyword-time-regexp nil
  3361. "Matches any of the 4 keywords, together with the time stamp.")
  3362. (make-variable-buffer-local 'org-keyword-time-regexp)
  3363. (defvar org-keyword-time-not-clock-regexp nil
  3364. "Matches any of the 3 keywords, together with the time stamp.")
  3365. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3366. (defvar org-maybe-keyword-time-regexp nil
  3367. "Matches a timestamp, possibly preceeded by a keyword.")
  3368. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3369. (defvar org-planning-or-clock-line-re nil
  3370. "Matches a line with planning or clock info.")
  3371. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3372. (defvar org-all-time-keywords nil
  3373. "List of time keywords.")
  3374. (make-variable-buffer-local 'org-all-time-keywords)
  3375. (defconst org-plain-time-of-day-regexp
  3376. (concat
  3377. "\\(\\<[012]?[0-9]"
  3378. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3379. "\\(--?"
  3380. "\\(\\<[012]?[0-9]"
  3381. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3382. "\\)?")
  3383. "Regular expression to match a plain time or time range.
  3384. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3385. groups carry important information:
  3386. 0 the full match
  3387. 1 the first time, range or not
  3388. 8 the second time, if it is a range.")
  3389. (defconst org-plain-time-extension-regexp
  3390. (concat
  3391. "\\(\\<[012]?[0-9]"
  3392. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3393. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3394. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3395. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3396. groups carry important information:
  3397. 0 the full match
  3398. 7 hours of duration
  3399. 9 minutes of duration")
  3400. (defconst org-stamp-time-of-day-regexp
  3401. (concat
  3402. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3403. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3404. "\\(--?"
  3405. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3406. "Regular expression to match a timestamp time or time range.
  3407. After a match, the following groups carry important information:
  3408. 0 the full match
  3409. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3410. 2 the first time, range or not
  3411. 4 the second time, if it is a range.")
  3412. (defconst org-startup-options
  3413. '(("fold" org-startup-folded t)
  3414. ("overview" org-startup-folded t)
  3415. ("nofold" org-startup-folded nil)
  3416. ("showall" org-startup-folded nil)
  3417. ("showeverything" org-startup-folded showeverything)
  3418. ("content" org-startup-folded content)
  3419. ("indent" org-startup-indented t)
  3420. ("noindent" org-startup-indented nil)
  3421. ("hidestars" org-hide-leading-stars t)
  3422. ("showstars" org-hide-leading-stars nil)
  3423. ("odd" org-odd-levels-only t)
  3424. ("oddeven" org-odd-levels-only nil)
  3425. ("align" org-startup-align-all-tables t)
  3426. ("noalign" org-startup-align-all-tables nil)
  3427. ("customtime" org-display-custom-times t)
  3428. ("logdone" org-log-done time)
  3429. ("lognotedone" org-log-done note)
  3430. ("nologdone" org-log-done nil)
  3431. ("lognoteclock-out" org-log-note-clock-out t)
  3432. ("nolognoteclock-out" org-log-note-clock-out nil)
  3433. ("logrepeat" org-log-repeat state)
  3434. ("lognoterepeat" org-log-repeat note)
  3435. ("nologrepeat" org-log-repeat nil)
  3436. ("logreschedule" org-log-reschedule time)
  3437. ("lognotereschedule" org-log-reschedule note)
  3438. ("nologreschedule" org-log-reschedule nil)
  3439. ("logredeadline" org-log-redeadline time)
  3440. ("lognoteredeadline" org-log-redeadline note)
  3441. ("nologredeadline" org-log-redeadline nil)
  3442. ("logrefile" org-log-refile time)
  3443. ("lognoterefile" org-log-refile note)
  3444. ("nologrefile" org-log-refile nil)
  3445. ("fninline" org-footnote-define-inline t)
  3446. ("nofninline" org-footnote-define-inline nil)
  3447. ("fnlocal" org-footnote-section nil)
  3448. ("fnauto" org-footnote-auto-label t)
  3449. ("fnprompt" org-footnote-auto-label nil)
  3450. ("fnconfirm" org-footnote-auto-label confirm)
  3451. ("fnplain" org-footnote-auto-label plain)
  3452. ("fnadjust" org-footnote-auto-adjust t)
  3453. ("nofnadjust" org-footnote-auto-adjust nil)
  3454. ("constcgs" constants-unit-system cgs)
  3455. ("constSI" constants-unit-system SI)
  3456. ("noptag" org-tag-persistent-alist nil)
  3457. ("hideblocks" org-hide-block-startup t)
  3458. ("nohideblocks" org-hide-block-startup nil)
  3459. ("beamer" org-startup-with-beamer-mode t))
  3460. "Variable associated with STARTUP options for org-mode.
  3461. Each element is a list of three items: The startup options as written
  3462. in the #+STARTUP line, the corresponding variable, and the value to
  3463. set this variable to if the option is found. An optional forth element PUSH
  3464. means to push this value onto the list in the variable.")
  3465. (defun org-set-regexps-and-options ()
  3466. "Precompute regular expressions for current buffer."
  3467. (when (org-mode-p)
  3468. (org-set-local 'org-todo-kwd-alist nil)
  3469. (org-set-local 'org-todo-key-alist nil)
  3470. (org-set-local 'org-todo-key-trigger nil)
  3471. (org-set-local 'org-todo-keywords-1 nil)
  3472. (org-set-local 'org-done-keywords nil)
  3473. (org-set-local 'org-todo-heads nil)
  3474. (org-set-local 'org-todo-sets nil)
  3475. (org-set-local 'org-todo-log-states nil)
  3476. (org-set-local 'org-file-properties nil)
  3477. (org-set-local 'org-file-tags nil)
  3478. (let ((re (org-make-options-regexp
  3479. '("CATEGORY" "TODO" "COLUMNS"
  3480. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3481. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3482. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3483. (splitre "[ \t]+")
  3484. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3485. tail sep kws1 prio props ftags drawers beamer-p
  3486. ext-setup-or-nil setup-contents (start 0))
  3487. (save-excursion
  3488. (save-restriction
  3489. (widen)
  3490. (goto-char (point-min))
  3491. (while (or (and ext-setup-or-nil
  3492. (string-match re ext-setup-or-nil start)
  3493. (setq start (match-end 0)))
  3494. (and (setq ext-setup-or-nil nil start 0)
  3495. (re-search-forward re nil t)))
  3496. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3497. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3498. (cond
  3499. ((equal key "CATEGORY")
  3500. (if (string-match "[ \t]+$" value)
  3501. (setq value (replace-match "" t t value)))
  3502. (setq cat value))
  3503. ((member key '("SEQ_TODO" "TODO"))
  3504. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3505. ((equal key "TYP_TODO")
  3506. (push (cons 'type (org-split-string value splitre)) kwds))
  3507. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3508. ;; general TODO-like setup
  3509. (push (cons (intern (downcase (match-string 1 key)))
  3510. (org-split-string value splitre)) kwds))
  3511. ((equal key "TAGS")
  3512. (setq tags (append tags (if tags '("\\n") nil)
  3513. (org-split-string value splitre))))
  3514. ((equal key "COLUMNS")
  3515. (org-set-local 'org-columns-default-format value))
  3516. ((equal key "LINK")
  3517. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3518. (push (cons (match-string 1 value)
  3519. (org-trim (match-string 2 value)))
  3520. links)))
  3521. ((equal key "PRIORITIES")
  3522. (setq prio (org-split-string value " +")))
  3523. ((equal key "PROPERTY")
  3524. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3525. (push (cons (match-string 1 value) (match-string 2 value))
  3526. props)))
  3527. ((equal key "FILETAGS")
  3528. (when (string-match "\\S-" value)
  3529. (setq ftags
  3530. (append
  3531. ftags
  3532. (apply 'append
  3533. (mapcar (lambda (x) (org-split-string x ":"))
  3534. (org-split-string value)))))))
  3535. ((equal key "DRAWERS")
  3536. (setq drawers (org-split-string value splitre)))
  3537. ((equal key "CONSTANTS")
  3538. (setq const (append const (org-split-string value splitre))))
  3539. ((equal key "STARTUP")
  3540. (let ((opts (org-split-string value splitre))
  3541. l var val)
  3542. (while (setq l (pop opts))
  3543. (when (setq l (assoc l org-startup-options))
  3544. (setq var (nth 1 l) val (nth 2 l))
  3545. (if (not (nth 3 l))
  3546. (set (make-local-variable var) val)
  3547. (if (not (listp (symbol-value var)))
  3548. (set (make-local-variable var) nil))
  3549. (set (make-local-variable var) (symbol-value var))
  3550. (add-to-list var val))))))
  3551. ((equal key "ARCHIVE")
  3552. (string-match " *$" value)
  3553. (setq arch (replace-match "" t t value))
  3554. (remove-text-properties 0 (length arch)
  3555. '(face t fontified t) arch))
  3556. ((equal key "LATEX_CLASS")
  3557. (setq beamer-p (equal value "beamer")))
  3558. ((equal key "SETUPFILE")
  3559. (setq setup-contents (org-file-contents
  3560. (expand-file-name
  3561. (org-remove-double-quotes value))
  3562. 'noerror))
  3563. (if (not ext-setup-or-nil)
  3564. (setq ext-setup-or-nil setup-contents start 0)
  3565. (setq ext-setup-or-nil
  3566. (concat (substring ext-setup-or-nil 0 start)
  3567. "\n" setup-contents "\n"
  3568. (substring ext-setup-or-nil start)))))
  3569. ))))
  3570. (when cat
  3571. (org-set-local 'org-category (intern cat))
  3572. (push (cons "CATEGORY" cat) props))
  3573. (when prio
  3574. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3575. (setq prio (mapcar 'string-to-char prio))
  3576. (org-set-local 'org-highest-priority (nth 0 prio))
  3577. (org-set-local 'org-lowest-priority (nth 1 prio))
  3578. (org-set-local 'org-default-priority (nth 2 prio)))
  3579. (and props (org-set-local 'org-file-properties (nreverse props)))
  3580. (and ftags (org-set-local 'org-file-tags
  3581. (mapcar 'org-add-prop-inherited ftags)))
  3582. (and drawers (org-set-local 'org-drawers drawers))
  3583. (and arch (org-set-local 'org-archive-location arch))
  3584. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3585. ;; Process the TODO keywords
  3586. (unless kwds
  3587. ;; Use the global values as if they had been given locally.
  3588. (setq kwds (default-value 'org-todo-keywords))
  3589. (if (stringp (car kwds))
  3590. (setq kwds (list (cons org-todo-interpretation
  3591. (default-value 'org-todo-keywords)))))
  3592. (setq kwds (reverse kwds)))
  3593. (setq kwds (nreverse kwds))
  3594. (let (inter kws kw)
  3595. (while (setq kws (pop kwds))
  3596. (let ((kws (or
  3597. (run-hook-with-args-until-success
  3598. 'org-todo-setup-filter-hook kws)
  3599. kws)))
  3600. (setq inter (pop kws) sep (member "|" kws)
  3601. kws0 (delete "|" (copy-sequence kws))
  3602. kwsa nil
  3603. kws1 (mapcar
  3604. (lambda (x)
  3605. ;; 1 2
  3606. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3607. (progn
  3608. (setq kw (match-string 1 x)
  3609. key (and (match-end 2) (match-string 2 x))
  3610. log (org-extract-log-state-settings x))
  3611. (push (cons kw (and key (string-to-char key))) kwsa)
  3612. (and log (push log org-todo-log-states))
  3613. kw)
  3614. (error "Invalid TODO keyword %s" x)))
  3615. kws0)
  3616. kwsa (if kwsa (append '((:startgroup))
  3617. (nreverse kwsa)
  3618. '((:endgroup))))
  3619. hw (car kws1)
  3620. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3621. tail (list inter hw (car dws) (org-last dws))))
  3622. (add-to-list 'org-todo-heads hw 'append)
  3623. (push kws1 org-todo-sets)
  3624. (setq org-done-keywords (append org-done-keywords dws nil))
  3625. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3626. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3627. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3628. (setq org-todo-sets (nreverse org-todo-sets)
  3629. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3630. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3631. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3632. ;; Process the constants
  3633. (when const
  3634. (let (e cst)
  3635. (while (setq e (pop const))
  3636. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3637. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3638. (setq org-table-formula-constants-local cst)))
  3639. ;; Process the tags.
  3640. (when tags
  3641. (let (e tgs)
  3642. (while (setq e (pop tags))
  3643. (cond
  3644. ((equal e "{") (push '(:startgroup) tgs))
  3645. ((equal e "}") (push '(:endgroup) tgs))
  3646. ((equal e "\\n") (push '(:newline) tgs))
  3647. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3648. (push (cons (match-string 1 e)
  3649. (string-to-char (match-string 2 e)))
  3650. tgs))
  3651. (t (push (list e) tgs))))
  3652. (org-set-local 'org-tag-alist nil)
  3653. (while (setq e (pop tgs))
  3654. (or (and (stringp (car e))
  3655. (assoc (car e) org-tag-alist))
  3656. (push e org-tag-alist)))))
  3657. ;; Compute the regular expressions and other local variables
  3658. (if (not org-done-keywords)
  3659. (setq org-done-keywords (and org-todo-keywords-1
  3660. (list (org-last org-todo-keywords-1)))))
  3661. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3662. (length org-scheduled-string)
  3663. (length org-clock-string)
  3664. (length org-closed-string)))
  3665. org-drawer-regexp
  3666. (concat "^[ \t]*:\\("
  3667. (mapconcat 'regexp-quote org-drawers "\\|")
  3668. "\\):[ \t]*$")
  3669. org-not-done-keywords
  3670. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3671. org-todo-regexp
  3672. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3673. "\\|") "\\)\\>")
  3674. org-not-done-regexp
  3675. (concat "\\<\\("
  3676. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3677. "\\)\\>")
  3678. org-not-done-heading-regexp
  3679. (concat "^\\(\\*+\\)[ \t]+\\("
  3680. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3681. "\\)\\>")
  3682. org-todo-line-regexp
  3683. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3684. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3685. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3686. org-complex-heading-regexp
  3687. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3688. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3689. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3690. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3691. org-complex-heading-regexp-format
  3692. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3693. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3694. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3695. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3696. org-nl-done-regexp
  3697. (concat "\n\\*+[ \t]+"
  3698. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3699. "\\)" "\\>")
  3700. org-todo-line-tags-regexp
  3701. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3702. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3703. (org-re
  3704. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3705. org-looking-at-done-regexp
  3706. (concat "^" "\\(?:"
  3707. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3708. "\\>")
  3709. org-deadline-regexp (concat "\\<" org-deadline-string)
  3710. org-deadline-time-regexp
  3711. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3712. org-deadline-line-regexp
  3713. (concat "\\<\\(" org-deadline-string "\\).*")
  3714. org-scheduled-regexp
  3715. (concat "\\<" org-scheduled-string)
  3716. org-scheduled-time-regexp
  3717. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3718. org-closed-time-regexp
  3719. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3720. org-keyword-time-regexp
  3721. (concat "\\<\\(" org-scheduled-string
  3722. "\\|" org-deadline-string
  3723. "\\|" org-closed-string
  3724. "\\|" org-clock-string "\\)"
  3725. " *[[<]\\([^]>]+\\)[]>]")
  3726. org-keyword-time-not-clock-regexp
  3727. (concat "\\<\\(" org-scheduled-string
  3728. "\\|" org-deadline-string
  3729. "\\|" org-closed-string
  3730. "\\)"
  3731. " *[[<]\\([^]>]+\\)[]>]")
  3732. org-maybe-keyword-time-regexp
  3733. (concat "\\(\\<\\(" org-scheduled-string
  3734. "\\|" org-deadline-string
  3735. "\\|" org-closed-string
  3736. "\\|" org-clock-string "\\)\\)?"
  3737. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3738. org-planning-or-clock-line-re
  3739. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3740. "\\|" org-deadline-string
  3741. "\\|" org-closed-string "\\|" org-clock-string
  3742. "\\)\\>\\)")
  3743. org-all-time-keywords
  3744. (mapcar (lambda (w) (substring w 0 -1))
  3745. (list org-scheduled-string org-deadline-string
  3746. org-clock-string org-closed-string))
  3747. )
  3748. (org-compute-latex-and-specials-regexp)
  3749. (org-set-font-lock-defaults))))
  3750. (defun org-file-contents (file &optional noerror)
  3751. "Return the contents of FILE, as a string."
  3752. (if (or (not file)
  3753. (not (file-readable-p file)))
  3754. (if noerror
  3755. (progn
  3756. (message "Cannot read file %s" file)
  3757. (ding) (sit-for 2)
  3758. "")
  3759. (error "Cannot read file %s" file))
  3760. (with-temp-buffer
  3761. (insert-file-contents file)
  3762. (buffer-string))))
  3763. (defun org-extract-log-state-settings (x)
  3764. "Extract the log state setting from a TODO keyword string.
  3765. This will extract info from a string like \"WAIT(w@/!)\"."
  3766. (let (kw key log1 log2)
  3767. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3768. (setq kw (match-string 1 x)
  3769. key (and (match-end 2) (match-string 2 x))
  3770. log1 (and (match-end 3) (match-string 3 x))
  3771. log2 (and (match-end 4) (match-string 4 x)))
  3772. (and (or log1 log2)
  3773. (list kw
  3774. (and log1 (if (equal log1 "!") 'time 'note))
  3775. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3776. (defun org-remove-keyword-keys (list)
  3777. "Remove a pair of parenthesis at the end of each string in LIST."
  3778. (mapcar (lambda (x)
  3779. (if (string-match "(.*)$" x)
  3780. (substring x 0 (match-beginning 0))
  3781. x))
  3782. list))
  3783. (defun org-assign-fast-keys (alist)
  3784. "Assign fast keys to a keyword-key alist.
  3785. Respect keys that are already there."
  3786. (let (new e (alt ?0))
  3787. (while (setq e (pop alist))
  3788. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3789. (cdr e)) ;; Key already assigned.
  3790. (push e new)
  3791. (let ((clist (string-to-list (downcase (car e))))
  3792. (used (append new alist)))
  3793. (when (= (car clist) ?@)
  3794. (pop clist))
  3795. (while (and clist (rassoc (car clist) used))
  3796. (pop clist))
  3797. (unless clist
  3798. (while (rassoc alt used)
  3799. (incf alt)))
  3800. (push (cons (car e) (or (car clist) alt)) new))))
  3801. (nreverse new)))
  3802. ;;; Some variables used in various places
  3803. (defvar org-window-configuration nil
  3804. "Used in various places to store a window configuration.")
  3805. (defvar org-selected-window nil
  3806. "Used in various places to store a window configuration.")
  3807. (defvar org-finish-function nil
  3808. "Function to be called when `C-c C-c' is used.
  3809. This is for getting out of special buffers like remember.")
  3810. ;; FIXME: Occasionally check by commenting these, to make sure
  3811. ;; no other functions uses these, forgetting to let-bind them.
  3812. (defvar entry)
  3813. (defvar last-state)
  3814. (defvar date)
  3815. ;; Defined somewhere in this file, but used before definition.
  3816. (defvar org-entities) ;; defined in org-entities.el
  3817. (defvar org-struct-menu)
  3818. (defvar org-org-menu)
  3819. (defvar org-tbl-menu)
  3820. ;;;; Define the Org-mode
  3821. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3822. (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."))
  3823. ;; We use a before-change function to check if a table might need
  3824. ;; an update.
  3825. (defvar org-table-may-need-update t
  3826. "Indicates that a table might need an update.
  3827. This variable is set by `org-before-change-function'.
  3828. `org-table-align' sets it back to nil.")
  3829. (defun org-before-change-function (beg end)
  3830. "Every change indicates that a table might need an update."
  3831. (setq org-table-may-need-update t))
  3832. (defvar org-mode-map)
  3833. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3834. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3835. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3836. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3837. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3838. (defvar org-table-buffer-is-an nil)
  3839. (defconst org-outline-regexp "\\*+ ")
  3840. ;;;###autoload
  3841. (define-derived-mode org-mode outline-mode "Org"
  3842. "Outline-based notes management and organizer, alias
  3843. \"Carsten's outline-mode for keeping track of everything.\"
  3844. Org-mode develops organizational tasks around a NOTES file which
  3845. contains information about projects as plain text. Org-mode is
  3846. implemented on top of outline-mode, which is ideal to keep the content
  3847. of large files well structured. It supports ToDo items, deadlines and
  3848. time stamps, which magically appear in the diary listing of the Emacs
  3849. calendar. Tables are easily created with a built-in table editor.
  3850. Plain text URL-like links connect to websites, emails (VM), Usenet
  3851. messages (Gnus), BBDB entries, and any files related to the project.
  3852. For printing and sharing of notes, an Org-mode file (or a part of it)
  3853. can be exported as a structured ASCII or HTML file.
  3854. The following commands are available:
  3855. \\{org-mode-map}"
  3856. ;; Get rid of Outline menus, they are not needed
  3857. ;; Need to do this here because define-derived-mode sets up
  3858. ;; the keymap so late. Still, it is a waste to call this each time
  3859. ;; we switch another buffer into org-mode.
  3860. (if (featurep 'xemacs)
  3861. (when (boundp 'outline-mode-menu-heading)
  3862. ;; Assume this is Greg's port, it uses easymenu
  3863. (easy-menu-remove outline-mode-menu-heading)
  3864. (easy-menu-remove outline-mode-menu-show)
  3865. (easy-menu-remove outline-mode-menu-hide))
  3866. (define-key org-mode-map [menu-bar headings] 'undefined)
  3867. (define-key org-mode-map [menu-bar hide] 'undefined)
  3868. (define-key org-mode-map [menu-bar show] 'undefined))
  3869. (org-load-modules-maybe)
  3870. (easy-menu-add org-org-menu)
  3871. (easy-menu-add org-tbl-menu)
  3872. (org-install-agenda-files-menu)
  3873. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  3874. (add-to-invisibility-spec '(org-cwidth))
  3875. (add-to-invisibility-spec '(org-hide-block . t))
  3876. (when (featurep 'xemacs)
  3877. (org-set-local 'line-move-ignore-invisible t))
  3878. (org-set-local 'outline-regexp org-outline-regexp)
  3879. (org-set-local 'outline-level 'org-outline-level)
  3880. (when (and org-ellipsis
  3881. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3882. (fboundp 'make-glyph-code))
  3883. (unless org-display-table
  3884. (setq org-display-table (make-display-table)))
  3885. (set-display-table-slot
  3886. org-display-table 4
  3887. (vconcat (mapcar
  3888. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3889. org-ellipsis)))
  3890. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3891. (setq buffer-display-table org-display-table))
  3892. (org-set-regexps-and-options)
  3893. (when (and org-tag-faces (not org-tags-special-faces-re))
  3894. ;; tag faces set outside customize.... force initialization.
  3895. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3896. ;; Calc embedded
  3897. (org-set-local 'calc-embedded-open-mode "# ")
  3898. (modify-syntax-entry ?@ "w")
  3899. (if org-startup-truncated (setq truncate-lines t))
  3900. (org-set-local 'font-lock-unfontify-region-function
  3901. 'org-unfontify-region)
  3902. ;; Activate before-change-function
  3903. (org-set-local 'org-table-may-need-update t)
  3904. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3905. 'local)
  3906. ;; Check for running clock before killing a buffer
  3907. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3908. ;; Paragraphs and auto-filling
  3909. (org-set-autofill-regexps)
  3910. (setq indent-line-function 'org-indent-line-function)
  3911. (org-update-radio-target-regexp)
  3912. ;; Make sure dependence stuff works reliably, even for users who set it
  3913. ;; too late :-(
  3914. (if org-enforce-todo-dependencies
  3915. (add-hook 'org-blocker-hook
  3916. 'org-block-todo-from-children-or-siblings-or-parent)
  3917. (remove-hook 'org-blocker-hook
  3918. 'org-block-todo-from-children-or-siblings-or-parent))
  3919. (if org-enforce-todo-checkbox-dependencies
  3920. (add-hook 'org-blocker-hook
  3921. 'org-block-todo-from-checkboxes)
  3922. (remove-hook 'org-blocker-hook
  3923. 'org-block-todo-from-checkboxes))
  3924. ;; Comment characters
  3925. (org-set-local 'comment-start "#")
  3926. (org-set-local 'comment-padding " ")
  3927. (modify-syntax-entry ?# "<")
  3928. (modify-syntax-entry ?\n ">")
  3929. ;; Align options lines
  3930. (org-set-local
  3931. 'align-mode-rules-list
  3932. '((org-in-buffer-settings
  3933. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3934. (modes . '(org-mode)))))
  3935. ;; Imenu
  3936. (org-set-local 'imenu-create-index-function
  3937. 'org-imenu-get-tree)
  3938. ;; Make isearch reveal context
  3939. (if (or (featurep 'xemacs)
  3940. (not (boundp 'outline-isearch-open-invisible-function)))
  3941. ;; Emacs 21 and XEmacs make use of the hook
  3942. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3943. ;; Emacs 22 deals with this through a special variable
  3944. (org-set-local 'outline-isearch-open-invisible-function
  3945. (lambda (&rest ignore) (org-show-context 'isearch))))
  3946. ;; Turn on org-beamer-mode?
  3947. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3948. ;; If empty file that did not turn on org-mode automatically, make it to.
  3949. (if (and org-insert-mode-line-in-empty-file
  3950. (interactive-p)
  3951. (= (point-min) (point-max)))
  3952. (insert "# -*- mode: org -*-\n\n"))
  3953. (unless org-inhibit-startup
  3954. (when org-startup-align-all-tables
  3955. (let ((bmp (buffer-modified-p)))
  3956. (org-table-map-tables 'org-table-align 'quietly)
  3957. (set-buffer-modified-p bmp)))
  3958. (when org-startup-indented
  3959. (require 'org-indent)
  3960. (org-indent-mode 1))
  3961. (unless org-inhibit-startup-visibility-stuff
  3962. (org-set-startup-visibility))))
  3963. (when (fboundp 'abbrev-table-put)
  3964. (abbrev-table-put org-mode-abbrev-table
  3965. :parents (list text-mode-abbrev-table)))
  3966. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3967. (defun org-current-time ()
  3968. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3969. (if (> (car org-time-stamp-rounding-minutes) 1)
  3970. (let ((r (car org-time-stamp-rounding-minutes))
  3971. (time (decode-time)))
  3972. (apply 'encode-time
  3973. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3974. (nthcdr 2 time))))
  3975. (current-time)))
  3976. ;;;; Font-Lock stuff, including the activators
  3977. (defvar org-mouse-map (make-sparse-keymap))
  3978. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  3979. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  3980. (when org-mouse-1-follows-link
  3981. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3982. (when org-tab-follows-link
  3983. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3984. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3985. (require 'font-lock)
  3986. (defconst org-non-link-chars "]\t\n\r<>")
  3987. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3988. "shell" "elisp" "doi"))
  3989. (defvar org-link-types-re nil
  3990. "Matches a link that has a url-like prefix like \"http:\"")
  3991. (defvar org-link-re-with-space nil
  3992. "Matches a link with spaces, optional angular brackets around it.")
  3993. (defvar org-link-re-with-space2 nil
  3994. "Matches a link with spaces, optional angular brackets around it.")
  3995. (defvar org-link-re-with-space3 nil
  3996. "Matches a link with spaces, only for internal part in bracket links.")
  3997. (defvar org-angle-link-re nil
  3998. "Matches link with angular brackets, spaces are allowed.")
  3999. (defvar org-plain-link-re nil
  4000. "Matches plain link, without spaces.")
  4001. (defvar org-bracket-link-regexp nil
  4002. "Matches a link in double brackets.")
  4003. (defvar org-bracket-link-analytic-regexp nil
  4004. "Regular expression used to analyze links.
  4005. Here is what the match groups contain after a match:
  4006. 1: http:
  4007. 2: http
  4008. 3: path
  4009. 4: [desc]
  4010. 5: desc")
  4011. (defvar org-bracket-link-analytic-regexp++ nil
  4012. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  4013. (defvar org-any-link-re nil
  4014. "Regular expression matching any link.")
  4015. (defun org-make-link-regexps ()
  4016. "Update the link regular expressions.
  4017. This should be called after the variable `org-link-types' has changed."
  4018. (setq org-link-types-re
  4019. (concat
  4020. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4021. org-link-re-with-space
  4022. (concat
  4023. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4024. "\\([^" org-non-link-chars " ]"
  4025. "[^" org-non-link-chars "]*"
  4026. "[^" org-non-link-chars " ]\\)>?")
  4027. org-link-re-with-space2
  4028. (concat
  4029. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4030. "\\([^" org-non-link-chars " ]"
  4031. "[^\t\n\r]*"
  4032. "[^" org-non-link-chars " ]\\)>?")
  4033. org-link-re-with-space3
  4034. (concat
  4035. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4036. "\\([^" org-non-link-chars " ]"
  4037. "[^\t\n\r]*\\)")
  4038. org-angle-link-re
  4039. (concat
  4040. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4041. "\\([^" org-non-link-chars " ]"
  4042. "[^" org-non-link-chars "]*"
  4043. "\\)>")
  4044. org-plain-link-re
  4045. (concat
  4046. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4047. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4048. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4049. org-bracket-link-regexp
  4050. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4051. org-bracket-link-analytic-regexp
  4052. (concat
  4053. "\\[\\["
  4054. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4055. "\\([^]]+\\)"
  4056. "\\]"
  4057. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4058. "\\]")
  4059. org-bracket-link-analytic-regexp++
  4060. (concat
  4061. "\\[\\["
  4062. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4063. "\\([^]]+\\)"
  4064. "\\]"
  4065. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4066. "\\]")
  4067. org-any-link-re
  4068. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4069. org-angle-link-re "\\)\\|\\("
  4070. org-plain-link-re "\\)")))
  4071. (org-make-link-regexps)
  4072. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4073. "Regular expression for fast time stamp matching.")
  4074. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4075. "Regular expression for fast time stamp matching.")
  4076. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4077. "Regular expression matching time strings for analysis.
  4078. This one does not require the space after the date, so it can be used
  4079. on a string that terminates immediately after the date.")
  4080. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4081. "Regular expression matching time strings for analysis.")
  4082. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4083. "Regular expression matching time stamps, with groups.")
  4084. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4085. "Regular expression matching time stamps (also [..]), with groups.")
  4086. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4087. "Regular expression matching a time stamp range.")
  4088. (defconst org-tr-regexp-both
  4089. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4090. "Regular expression matching a time stamp range.")
  4091. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4092. org-ts-regexp "\\)?")
  4093. "Regular expression matching a time stamp or time stamp range.")
  4094. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4095. org-ts-regexp-both "\\)?")
  4096. "Regular expression matching a time stamp or time stamp range.
  4097. The time stamps may be either active or inactive.")
  4098. (defvar org-emph-face nil)
  4099. (defun org-do-emphasis-faces (limit)
  4100. "Run through the buffer and add overlays to links."
  4101. (let (rtn a)
  4102. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4103. (if (not (= (char-after (match-beginning 3))
  4104. (char-after (match-beginning 4))))
  4105. (progn
  4106. (setq rtn t)
  4107. (setq a (assoc (match-string 3) org-emphasis-alist))
  4108. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4109. 'face
  4110. (nth 1 a))
  4111. (and (nth 4 a)
  4112. (org-remove-flyspell-overlays-in
  4113. (match-beginning 0) (match-end 0)))
  4114. (add-text-properties (match-beginning 2) (match-end 2)
  4115. '(font-lock-multiline t))
  4116. (when org-hide-emphasis-markers
  4117. (add-text-properties (match-end 4) (match-beginning 5)
  4118. '(invisible org-link))
  4119. (add-text-properties (match-beginning 3) (match-end 3)
  4120. '(invisible org-link)))))
  4121. (backward-char 1))
  4122. rtn))
  4123. (defun org-emphasize (&optional char)
  4124. "Insert or change an emphasis, i.e. a font like bold or italic.
  4125. If there is an active region, change that region to a new emphasis.
  4126. If there is no region, just insert the marker characters and position
  4127. the cursor between them.
  4128. CHAR should be either the marker character, or the first character of the
  4129. HTML tag associated with that emphasis. If CHAR is a space, the means
  4130. to remove the emphasis of the selected region.
  4131. If char is not given (for example in an interactive call) it
  4132. will be prompted for."
  4133. (interactive)
  4134. (let ((eal org-emphasis-alist) e det
  4135. (erc org-emphasis-regexp-components)
  4136. (prompt "")
  4137. (string "") beg end move tag c s)
  4138. (if (org-region-active-p)
  4139. (setq beg (region-beginning) end (region-end)
  4140. string (buffer-substring beg end))
  4141. (setq move t))
  4142. (while (setq e (pop eal))
  4143. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4144. c (aref tag 0))
  4145. (push (cons c (string-to-char (car e))) det)
  4146. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4147. (substring tag 1)))))
  4148. (setq det (nreverse det))
  4149. (unless char
  4150. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4151. (setq char (read-char-exclusive)))
  4152. (setq char (or (cdr (assoc char det)) char))
  4153. (if (equal char ?\ )
  4154. (setq s "" move nil)
  4155. (unless (assoc (char-to-string char) org-emphasis-alist)
  4156. (error "No such emphasis marker: \"%c\"" char))
  4157. (setq s (char-to-string char)))
  4158. (while (and (> (length string) 1)
  4159. (equal (substring string 0 1) (substring string -1))
  4160. (assoc (substring string 0 1) org-emphasis-alist))
  4161. (setq string (substring string 1 -1)))
  4162. (setq string (concat s string s))
  4163. (if beg (delete-region beg end))
  4164. (unless (or (bolp)
  4165. (string-match (concat "[" (nth 0 erc) "\n]")
  4166. (char-to-string (char-before (point)))))
  4167. (insert " "))
  4168. (unless (or (eobp)
  4169. (string-match (concat "[" (nth 1 erc) "\n]")
  4170. (char-to-string (char-after (point)))))
  4171. (insert " ") (backward-char 1))
  4172. (insert string)
  4173. (and move (backward-char 1))))
  4174. (defconst org-nonsticky-props
  4175. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4176. (defsubst org-rear-nonsticky-at (pos)
  4177. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4178. (defun org-activate-plain-links (limit)
  4179. "Run through the buffer and add overlays to links."
  4180. (catch 'exit
  4181. (let (f)
  4182. (if (re-search-forward org-plain-link-re limit t)
  4183. (progn
  4184. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4185. (setq f (get-text-property (match-beginning 0) 'face))
  4186. (if (or (eq f 'org-tag)
  4187. (and (listp f) (memq 'org-tag f)))
  4188. nil
  4189. (add-text-properties (match-beginning 0) (match-end 0)
  4190. (list 'mouse-face 'highlight
  4191. 'face 'org-link
  4192. 'keymap org-mouse-map))
  4193. (org-rear-nonsticky-at (match-end 0)))
  4194. t)))))
  4195. (defun org-activate-code (limit)
  4196. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4197. (progn
  4198. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4199. (remove-text-properties (match-beginning 0) (match-end 0)
  4200. '(display t invisible t intangible t))
  4201. t)))
  4202. (defun org-fontify-meta-lines-and-blocks (limit)
  4203. "Fontify #+ lines and blocks, in the correct ways."
  4204. (let ((case-fold-search t))
  4205. (if (re-search-forward
  4206. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4207. limit t)
  4208. (let ((beg (match-beginning 0))
  4209. (beg1 (line-beginning-position 2))
  4210. (dc1 (downcase (match-string 2)))
  4211. (dc3 (downcase (match-string 3)))
  4212. end end1 quoting block-type)
  4213. (cond
  4214. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4215. ;; a single line of backend-specific content
  4216. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4217. (remove-text-properties (match-beginning 0) (match-end 0)
  4218. '(display t invisible t intangible t))
  4219. (add-text-properties (match-beginning 1) (match-end 3)
  4220. '(font-lock-fontified t face org-meta-line))
  4221. (add-text-properties (match-beginning 6) (match-end 6)
  4222. '(font-lock-fontified t face org-block))
  4223. t)
  4224. ((and (match-end 4) (equal dc3 "begin"))
  4225. ;; Truly a block
  4226. (setq block-type (downcase (match-string 5))
  4227. quoting (member block-type org-protecting-blocks))
  4228. (when (re-search-forward
  4229. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4230. nil t) ;; on purpose, we look further than LIMIT
  4231. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4232. (when quoting
  4233. (remove-text-properties beg end
  4234. '(display t invisible t intangible t)))
  4235. (add-text-properties
  4236. beg end
  4237. '(font-lock-fontified t font-lock-multiline t))
  4238. (add-text-properties beg beg1 '(face org-meta-line))
  4239. (add-text-properties end1 end '(face org-meta-line))
  4240. (cond
  4241. (quoting
  4242. (add-text-properties beg1 end1 '(face org-block)))
  4243. ((not org-fontify-quote-and-verse-blocks))
  4244. ((string= block-type "quote")
  4245. (add-text-properties beg1 end1 '(face org-quote)))
  4246. ((string= block-type "verse")
  4247. (add-text-properties beg1 end1 '(face org-verse))))
  4248. t))
  4249. ((member dc1 '("title:" "author:" "email:" "date:"))
  4250. (add-text-properties
  4251. beg (match-end 3)
  4252. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4253. '(font-lock-fontified t invisible t)
  4254. '(font-lock-fontified t face org-document-info-keyword)))
  4255. (add-text-properties
  4256. (match-beginning 6) (match-end 6)
  4257. (if (string-equal dc1 "title:")
  4258. '(font-lock-fontified t face org-document-title)
  4259. '(font-lock-fontified t face org-document-info))))
  4260. ((not (member (char-after beg) '(?\ ?\t)))
  4261. ;; just any other in-buffer setting, but not indented
  4262. (add-text-properties
  4263. beg (match-end 0)
  4264. '(font-lock-fontified t face org-meta-line))
  4265. t)
  4266. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4267. "orgtbl:" "tblfm:" "tblname:"))
  4268. (and (match-end 4) (equal dc3 "attr")))
  4269. (add-text-properties
  4270. beg (match-end 0)
  4271. '(font-lock-fontified t face org-meta-line))
  4272. t)
  4273. ((member dc3 '(" " ""))
  4274. (add-text-properties
  4275. beg (match-end 0)
  4276. '(font-lock-fontified t face font-lock-comment-face)))
  4277. (t nil))))))
  4278. (defun org-activate-angle-links (limit)
  4279. "Run through the buffer and add overlays to links."
  4280. (if (re-search-forward org-angle-link-re limit t)
  4281. (progn
  4282. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4283. (add-text-properties (match-beginning 0) (match-end 0)
  4284. (list 'mouse-face 'highlight
  4285. 'keymap org-mouse-map))
  4286. (org-rear-nonsticky-at (match-end 0))
  4287. t)))
  4288. (defun org-activate-footnote-links (limit)
  4289. "Run through the buffer and add overlays to links."
  4290. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4291. limit t)
  4292. (progn
  4293. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4294. (add-text-properties (match-beginning 2) (match-end 2)
  4295. (list 'mouse-face 'highlight
  4296. 'keymap org-mouse-map
  4297. 'help-echo
  4298. (if (= (point-at-bol) (match-beginning 2))
  4299. "Footnote definition"
  4300. "Footnote reference")
  4301. ))
  4302. (org-rear-nonsticky-at (match-end 2))
  4303. t)))
  4304. (defun org-activate-bracket-links (limit)
  4305. "Run through the buffer and add overlays to bracketed links."
  4306. (if (re-search-forward org-bracket-link-regexp limit t)
  4307. (let* ((help (concat "LINK: "
  4308. (org-match-string-no-properties 1)))
  4309. ;; FIXME: above we should remove the escapes.
  4310. ;; but that requires another match, protecting match data,
  4311. ;; a lot of overhead for font-lock.
  4312. (ip (org-maybe-intangible
  4313. (list 'invisible 'org-link
  4314. 'keymap org-mouse-map 'mouse-face 'highlight
  4315. 'font-lock-multiline t 'help-echo help)))
  4316. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4317. 'font-lock-multiline t 'help-echo help)))
  4318. ;; We need to remove the invisible property here. Table narrowing
  4319. ;; may have made some of this invisible.
  4320. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4321. (remove-text-properties (match-beginning 0) (match-end 0)
  4322. '(invisible nil))
  4323. (if (match-end 3)
  4324. (progn
  4325. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4326. (org-rear-nonsticky-at (match-beginning 3))
  4327. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4328. (org-rear-nonsticky-at (match-end 3))
  4329. (add-text-properties (match-end 3) (match-end 0) ip)
  4330. (org-rear-nonsticky-at (match-end 0)))
  4331. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4332. (org-rear-nonsticky-at (match-beginning 1))
  4333. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4334. (org-rear-nonsticky-at (match-end 1))
  4335. (add-text-properties (match-end 1) (match-end 0) ip)
  4336. (org-rear-nonsticky-at (match-end 0)))
  4337. t)))
  4338. (defun org-activate-dates (limit)
  4339. "Run through the buffer and add overlays to dates."
  4340. (if (re-search-forward org-tsr-regexp-both limit t)
  4341. (progn
  4342. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4343. (add-text-properties (match-beginning 0) (match-end 0)
  4344. (list 'mouse-face 'highlight
  4345. 'keymap org-mouse-map))
  4346. (org-rear-nonsticky-at (match-end 0))
  4347. (when org-display-custom-times
  4348. (if (match-end 3)
  4349. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4350. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4351. t)))
  4352. (defvar org-target-link-regexp nil
  4353. "Regular expression matching radio targets in plain text.")
  4354. (make-variable-buffer-local 'org-target-link-regexp)
  4355. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4356. "Regular expression matching a link target.")
  4357. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4358. "Regular expression matching a radio target.")
  4359. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4360. "Regular expression matching any target.")
  4361. (defun org-activate-target-links (limit)
  4362. "Run through the buffer and add overlays to target matches."
  4363. (when org-target-link-regexp
  4364. (let ((case-fold-search t))
  4365. (if (re-search-forward org-target-link-regexp limit t)
  4366. (progn
  4367. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4368. (add-text-properties (match-beginning 0) (match-end 0)
  4369. (list 'mouse-face 'highlight
  4370. 'keymap org-mouse-map
  4371. 'help-echo "Radio target link"
  4372. 'org-linked-text t))
  4373. (org-rear-nonsticky-at (match-end 0))
  4374. t)))))
  4375. (defun org-update-radio-target-regexp ()
  4376. "Find all radio targets in this file and update the regular expression."
  4377. (interactive)
  4378. (when (memq 'radio org-activate-links)
  4379. (setq org-target-link-regexp
  4380. (org-make-target-link-regexp (org-all-targets 'radio)))
  4381. (org-restart-font-lock)))
  4382. (defun org-hide-wide-columns (limit)
  4383. (let (s e)
  4384. (setq s (text-property-any (point) (or limit (point-max))
  4385. 'org-cwidth t))
  4386. (when s
  4387. (setq e (next-single-property-change s 'org-cwidth))
  4388. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4389. (goto-char e)
  4390. t)))
  4391. (defvar org-latex-and-specials-regexp nil
  4392. "Regular expression for highlighting export special stuff.")
  4393. (defvar org-match-substring-regexp)
  4394. (defvar org-match-substring-with-braces-regexp)
  4395. ;; This should be with the exporter code, but we also use if for font-locking
  4396. (defconst org-export-html-special-string-regexps
  4397. '(("\\\\-" . "&shy;")
  4398. ("---\\([^-]\\)" . "&mdash;\\1")
  4399. ("--\\([^-]\\)" . "&ndash;\\1")
  4400. ("\\.\\.\\." . "&hellip;"))
  4401. "Regular expressions for special string conversion.")
  4402. (defun org-compute-latex-and-specials-regexp ()
  4403. "Compute regular expression for stuff treated specially by exporters."
  4404. (if (not org-highlight-latex-fragments-and-specials)
  4405. (org-set-local 'org-latex-and-specials-regexp nil)
  4406. (require 'org-exp)
  4407. (let*
  4408. ((matchers (plist-get org-format-latex-options :matchers))
  4409. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4410. org-latex-regexps)))
  4411. (org-export-allow-BIND nil)
  4412. (options (org-combine-plists (org-default-export-plist)
  4413. (org-infile-export-plist)))
  4414. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4415. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4416. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4417. (org-export-html-expand (plist-get options :expand-quoted-html))
  4418. (org-export-with-special-strings (plist-get options :special-strings))
  4419. (re-sub
  4420. (cond
  4421. ((equal org-export-with-sub-superscripts '{})
  4422. (list org-match-substring-with-braces-regexp))
  4423. (org-export-with-sub-superscripts
  4424. (list org-match-substring-regexp))
  4425. (t nil)))
  4426. (re-latex
  4427. (if org-export-with-LaTeX-fragments
  4428. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4429. (re-macros
  4430. (if org-export-with-TeX-macros
  4431. (list (concat "\\\\"
  4432. (regexp-opt
  4433. (append (mapcar 'car (append org-entities-user
  4434. org-entities))
  4435. (if (boundp 'org-latex-entities)
  4436. (mapcar (lambda (x)
  4437. (or (car-safe x) x))
  4438. org-latex-entities)
  4439. nil))
  4440. 'words))) ; FIXME
  4441. ))
  4442. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4443. (re-special (if org-export-with-special-strings
  4444. (mapcar (lambda (x) (car x))
  4445. org-export-html-special-string-regexps)))
  4446. (re-rest
  4447. (delq nil
  4448. (list
  4449. (if org-export-html-expand "@<[^>\n]+>")
  4450. ))))
  4451. (org-set-local
  4452. 'org-latex-and-specials-regexp
  4453. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4454. re-rest) "\\|")))))
  4455. (defun org-do-latex-and-special-faces (limit)
  4456. "Run through the buffer and add overlays to links."
  4457. (when org-latex-and-specials-regexp
  4458. (let (rtn d)
  4459. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4460. limit t))
  4461. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4462. 'face))
  4463. '(org-code org-verbatim underline)))
  4464. (progn
  4465. (setq rtn t
  4466. d (cond ((member (char-after (1+ (match-beginning 0)))
  4467. '(?_ ?^)) 1)
  4468. (t 0)))
  4469. (font-lock-prepend-text-property
  4470. (+ d (match-beginning 0)) (match-end 0)
  4471. 'face 'org-latex-and-export-specials)
  4472. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4473. '(font-lock-multiline t)))))
  4474. rtn)))
  4475. (defun org-restart-font-lock ()
  4476. "Restart font-lock-mode, to force refontification."
  4477. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4478. (font-lock-mode -1)
  4479. (font-lock-mode 1)))
  4480. (defun org-all-targets (&optional radio)
  4481. "Return a list of all targets in this file.
  4482. With optional argument RADIO, only find radio targets."
  4483. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4484. rtn)
  4485. (save-excursion
  4486. (goto-char (point-min))
  4487. (while (re-search-forward re nil t)
  4488. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4489. rtn)))
  4490. (defun org-make-target-link-regexp (targets)
  4491. "Make regular expression matching all strings in TARGETS.
  4492. The regular expression finds the targets also if there is a line break
  4493. between words."
  4494. (and targets
  4495. (concat
  4496. "\\<\\("
  4497. (mapconcat
  4498. (lambda (x)
  4499. (while (string-match " +" x)
  4500. (setq x (replace-match "\\s-+" t t x)))
  4501. x)
  4502. targets
  4503. "\\|")
  4504. "\\)\\>")))
  4505. (defun org-activate-tags (limit)
  4506. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4507. (progn
  4508. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4509. (add-text-properties (match-beginning 1) (match-end 1)
  4510. (list 'mouse-face 'highlight
  4511. 'keymap org-mouse-map))
  4512. (org-rear-nonsticky-at (match-end 1))
  4513. t)))
  4514. (defun org-outline-level ()
  4515. "Compute the outline level of the heading at point.
  4516. This function assumes that the cursor is at the beginning of a line matched
  4517. by outline-regexp. Otherwise it returns garbage.
  4518. If this is called at a normal headline, the level is the number of stars.
  4519. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4520. For plain list items, if they are matched by `outline-regexp', this returns
  4521. 1000 plus the line indentation."
  4522. (save-excursion
  4523. (looking-at outline-regexp)
  4524. (if (match-beginning 1)
  4525. (+ (org-get-string-indentation (match-string 1)) 1000)
  4526. (1- (- (match-end 0) (match-beginning 0))))))
  4527. (defvar org-font-lock-keywords nil)
  4528. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4529. "Regular expression matching a property line.")
  4530. (defvar org-font-lock-hook nil
  4531. "Functions to be called for special font lock stuff.")
  4532. (defun org-font-lock-hook (limit)
  4533. (run-hook-with-args 'org-font-lock-hook limit))
  4534. (defun org-set-font-lock-defaults ()
  4535. (let* ((em org-fontify-emphasized-text)
  4536. (lk org-activate-links)
  4537. (org-font-lock-extra-keywords
  4538. (list
  4539. ;; Call the hook
  4540. '(org-font-lock-hook)
  4541. ;; Headlines
  4542. `(,(if org-fontify-whole-heading-line
  4543. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4544. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4545. (1 (org-get-level-face 1))
  4546. (2 (org-get-level-face 2))
  4547. (3 (org-get-level-face 3)))
  4548. ;; Table lines
  4549. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4550. (1 'org-table t))
  4551. ;; Table internals
  4552. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4553. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4554. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4555. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4556. ;; Drawers
  4557. (list org-drawer-regexp '(0 'org-special-keyword t))
  4558. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4559. ;; Properties
  4560. (list org-property-re
  4561. '(1 'org-special-keyword t)
  4562. '(3 'org-property-value t))
  4563. ;; Links
  4564. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4565. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4566. (if (memq 'plain lk) '(org-activate-plain-links))
  4567. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4568. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4569. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4570. (if (memq 'footnote lk) '(org-activate-footnote-links
  4571. (2 'org-footnote t)))
  4572. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4573. '(org-hide-wide-columns (0 nil append))
  4574. ;; TODO lines
  4575. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4576. '(1 (org-get-todo-face 1) t))
  4577. ;; DONE
  4578. (if org-fontify-done-headline
  4579. (list (concat "^[*]+ +\\<\\("
  4580. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4581. "\\)\\(.*\\)")
  4582. '(2 'org-headline-done t))
  4583. nil)
  4584. ;; Priorities
  4585. '(org-font-lock-add-priority-faces)
  4586. ;; Tags
  4587. '(org-font-lock-add-tag-faces)
  4588. ;; Special keywords
  4589. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4590. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4591. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4592. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4593. ;; Emphasis
  4594. (if em
  4595. (if (featurep 'xemacs)
  4596. '(org-do-emphasis-faces (0 nil append))
  4597. '(org-do-emphasis-faces)))
  4598. ;; Checkboxes
  4599. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4600. 2 'org-checkbox prepend)
  4601. (if org-provide-checkbox-statistics
  4602. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4603. (0 (org-get-checkbox-statistics-face) t)))
  4604. ;; Description list items
  4605. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4606. 2 'bold prepend)
  4607. ;; ARCHIVEd headings
  4608. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4609. '(1 'org-archived prepend))
  4610. ;; Specials
  4611. '(org-do-latex-and-special-faces)
  4612. ;; Code
  4613. '(org-activate-code (1 'org-code t))
  4614. ;; COMMENT
  4615. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4616. "\\|" org-quote-string "\\)\\>")
  4617. '(1 'org-special-keyword t))
  4618. '("^#.*" (0 'font-lock-comment-face t))
  4619. ;; Blocks and meta lines
  4620. '(org-fontify-meta-lines-and-blocks)
  4621. )))
  4622. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4623. ;; Now set the full font-lock-keywords
  4624. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4625. (org-set-local 'font-lock-defaults
  4626. '(org-font-lock-keywords t nil nil backward-paragraph))
  4627. (kill-local-variable 'font-lock-keywords) nil))
  4628. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4629. "Fontify string S like in Org-mode"
  4630. (with-temp-buffer
  4631. (insert s)
  4632. (let ((org-odd-levels-only odd-levels))
  4633. (org-mode)
  4634. (font-lock-fontify-buffer)
  4635. (buffer-string))))
  4636. (defvar org-m nil)
  4637. (defvar org-l nil)
  4638. (defvar org-f nil)
  4639. (defun org-get-level-face (n)
  4640. "Get the right face for match N in font-lock matching of headlines."
  4641. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4642. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4643. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4644. (cond
  4645. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4646. ((eq n 2) org-f)
  4647. (t (if org-level-color-stars-only nil org-f))))
  4648. (defun org-get-todo-face (kwd)
  4649. "Get the right face for a TODO keyword KWD.
  4650. If KWD is a number, get the corresponding match group."
  4651. (if (numberp kwd) (setq kwd (match-string kwd)))
  4652. (or (org-face-from-face-or-color
  4653. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4654. (and (member kwd org-done-keywords) 'org-done)
  4655. 'org-todo))
  4656. (defun org-face-from-face-or-color (context inherit face-or-color)
  4657. "Create a face list that inherits INHERIT, but sets the foreground color.
  4658. When FACE-OR-COLOR is not a string, just return it."
  4659. (if (stringp face-or-color)
  4660. (list :inherit inherit
  4661. (cdr (assoc context org-faces-easy-properties))
  4662. face-or-color)
  4663. face-or-color))
  4664. (defun org-font-lock-add-tag-faces (limit)
  4665. "Add the special tag faces."
  4666. (when (and org-tag-faces org-tags-special-faces-re)
  4667. (while (re-search-forward org-tags-special-faces-re limit t)
  4668. (add-text-properties (match-beginning 1) (match-end 1)
  4669. (list 'face (org-get-tag-face 1)
  4670. 'font-lock-fontified t))
  4671. (backward-char 1))))
  4672. (defun org-font-lock-add-priority-faces (limit)
  4673. "Add the special priority faces."
  4674. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4675. (add-text-properties
  4676. (match-beginning 0) (match-end 0)
  4677. (list 'face (or (org-face-from-face-or-color
  4678. 'priority 'org-special-keyword
  4679. (cdr (assoc (char-after (match-beginning 1))
  4680. org-priority-faces)))
  4681. 'org-special-keyword)
  4682. 'font-lock-fontified t))))
  4683. (defun org-get-tag-face (kwd)
  4684. "Get the right face for a TODO keyword KWD.
  4685. If KWD is a number, get the corresponding match group."
  4686. (if (numberp kwd) (setq kwd (match-string kwd)))
  4687. (or (org-face-from-face-or-color
  4688. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4689. 'org-tag))
  4690. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4691. "Remove fontification and activation overlays from links."
  4692. (font-lock-default-unfontify-region beg end)
  4693. (let* ((buffer-undo-list t)
  4694. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4695. (inhibit-modification-hooks t)
  4696. deactivate-mark buffer-file-name buffer-file-truename)
  4697. (remove-text-properties
  4698. beg end
  4699. (if org-indent-mode
  4700. ;; also remove line-prefix and wrap-prefix properties
  4701. '(mouse-face t keymap t org-linked-text t
  4702. invisible t intangible t
  4703. line-prefix t wrap-prefix t
  4704. org-no-flyspell t)
  4705. '(mouse-face t keymap t org-linked-text t
  4706. invisible t intangible t
  4707. org-no-flyspell t)))))
  4708. ;;;; Visibility cycling, including org-goto and indirect buffer
  4709. ;;; Cycling
  4710. (defvar org-cycle-global-status nil)
  4711. (make-variable-buffer-local 'org-cycle-global-status)
  4712. (defvar org-cycle-subtree-status nil)
  4713. (make-variable-buffer-local 'org-cycle-subtree-status)
  4714. ;;;###autoload
  4715. (defvar org-inlinetask-min-level)
  4716. (defun org-cycle (&optional arg)
  4717. "TAB-action and visibility cycling for Org-mode.
  4718. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4719. is outline visibility cycling, but it also invokes other actions
  4720. in special contexts.
  4721. - When this function is called with a prefix argument, rotate the entire
  4722. buffer through 3 states (global cycling)
  4723. 1. OVERVIEW: Show only top-level headlines.
  4724. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4725. 3. SHOW ALL: Show everything.
  4726. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4727. determined by the variable `org-startup-folded', and by any VISIBILITY
  4728. properties in the buffer.
  4729. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4730. including any drawers.
  4731. - When inside a table, re-align the table and move to the next field.
  4732. - When point is at the beginning of a headline, rotate the subtree started
  4733. by this line through 3 different states (local cycling)
  4734. 1. FOLDED: Only the main headline is shown.
  4735. 2. CHILDREN: The main headline and the direct children are shown.
  4736. From this state, you can move to one of the children
  4737. and zoom in further.
  4738. 3. SUBTREE: Show the entire subtree, including body text.
  4739. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4740. - When point is at the beginning of an empty headline and the variable
  4741. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4742. of the headline by demoting and promoting it to likely levels. This
  4743. speeds up creation document structure by presing TAB once or several
  4744. times right after creating a new headline.
  4745. - When there is a numeric prefix, go up to a heading with level ARG, do
  4746. a `show-subtree' and return to the previous cursor position. If ARG
  4747. is negative, go up that many levels.
  4748. - When point is not at the beginning of a headline, execute the global
  4749. binding for TAB, which is re-indenting the line. See the option
  4750. `org-cycle-emulate-tab' for details.
  4751. - Special case: if point is at the beginning of the buffer and there is
  4752. no headline in line 1, this function will act as if called with prefix arg.
  4753. But only if also the variable `org-cycle-global-at-bob' is t."
  4754. (interactive "P")
  4755. (org-load-modules-maybe)
  4756. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4757. (and org-cycle-level-after-item/entry-creation
  4758. (or (org-cycle-level)
  4759. (org-cycle-item-indentation))))
  4760. (let* ((limit-level
  4761. (or org-cycle-max-level
  4762. (and (boundp 'org-inlinetask-min-level)
  4763. org-inlinetask-min-level
  4764. (1- org-inlinetask-min-level))))
  4765. (nstars (and limit-level
  4766. (if org-odd-levels-only
  4767. (and limit-level (1- (* limit-level 2)))
  4768. limit-level)))
  4769. (outline-regexp
  4770. (cond
  4771. ((not (org-mode-p)) outline-regexp)
  4772. ((or (eq org-cycle-include-plain-lists 'integrate)
  4773. (and org-cycle-include-plain-lists (org-at-item-p)))
  4774. (concat "\\(?:\\*"
  4775. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4776. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4777. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4778. (bob-special (and org-cycle-global-at-bob (bobp)
  4779. (not (looking-at outline-regexp))))
  4780. (org-cycle-hook
  4781. (if bob-special
  4782. (delq 'org-optimize-window-after-visibility-change
  4783. (copy-sequence org-cycle-hook))
  4784. org-cycle-hook))
  4785. (pos (point)))
  4786. (if (or bob-special (equal arg '(4)))
  4787. ;; special case: use global cycling
  4788. (setq arg t))
  4789. (cond
  4790. ((equal arg '(16))
  4791. (org-set-startup-visibility)
  4792. (message "Startup visibility, plus VISIBILITY properties"))
  4793. ((equal arg '(64))
  4794. (show-all)
  4795. (message "Entire buffer visible, including drawers"))
  4796. ((org-at-table-p 'any)
  4797. ;; Enter the table or move to the next field in the table
  4798. (if (org-at-table.el-p)
  4799. (message "Use C-c ' to edit table.el tables")
  4800. (if arg (org-table-edit-field t)
  4801. (org-table-justify-field-maybe)
  4802. (call-interactively 'org-table-next-field))))
  4803. ((run-hook-with-args-until-success
  4804. 'org-tab-after-check-for-table-hook))
  4805. ((eq arg t) ;; Global cycling
  4806. (org-cycle-internal-global))
  4807. ((and org-drawers org-drawer-regexp
  4808. (save-excursion
  4809. (beginning-of-line 1)
  4810. (looking-at org-drawer-regexp)))
  4811. ;; Toggle block visibility
  4812. (org-flag-drawer
  4813. (not (get-char-property (match-end 0) 'invisible))))
  4814. ((integerp arg)
  4815. ;; Show-subtree, ARG levels up from here.
  4816. (save-excursion
  4817. (org-back-to-heading)
  4818. (outline-up-heading (if (< arg 0) (- arg)
  4819. (- (funcall outline-level) arg)))
  4820. (org-show-subtree)))
  4821. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4822. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4823. (org-cycle-internal-local))
  4824. ;; TAB emulation and template completion
  4825. (buffer-read-only (org-back-to-heading))
  4826. ((run-hook-with-args-until-success
  4827. 'org-tab-after-check-for-cycling-hook))
  4828. ((org-try-structure-completion))
  4829. ((org-try-cdlatex-tab))
  4830. ((run-hook-with-args-until-success
  4831. 'org-tab-before-tab-emulation-hook))
  4832. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4833. (or (not (bolp))
  4834. (not (looking-at outline-regexp))))
  4835. (call-interactively (global-key-binding "\t")))
  4836. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4837. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4838. (or (and (eq org-cycle-emulate-tab 'white)
  4839. (= (match-end 0) (point-at-eol)))
  4840. (and (eq org-cycle-emulate-tab 'whitestart)
  4841. (>= (match-end 0) pos))))
  4842. t
  4843. (eq org-cycle-emulate-tab t))
  4844. (call-interactively (global-key-binding "\t")))
  4845. (t (save-excursion
  4846. (org-back-to-heading)
  4847. (org-cycle)))))))
  4848. (defun org-cycle-internal-global ()
  4849. "Do the global cycling action."
  4850. (cond
  4851. ((and (eq last-command this-command)
  4852. (eq org-cycle-global-status 'overview))
  4853. ;; We just created the overview - now do table of contents
  4854. ;; This can be slow in very large buffers, so indicate action
  4855. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4856. (message "CONTENTS...")
  4857. (org-content)
  4858. (message "CONTENTS...done")
  4859. (setq org-cycle-global-status 'contents)
  4860. (run-hook-with-args 'org-cycle-hook 'contents))
  4861. ((and (eq last-command this-command)
  4862. (eq org-cycle-global-status 'contents))
  4863. ;; We just showed the table of contents - now show everything
  4864. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4865. (show-all)
  4866. (message "SHOW ALL")
  4867. (setq org-cycle-global-status 'all)
  4868. (run-hook-with-args 'org-cycle-hook 'all))
  4869. (t
  4870. ;; Default action: go to overview
  4871. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4872. (org-overview)
  4873. (message "OVERVIEW")
  4874. (setq org-cycle-global-status 'overview)
  4875. (run-hook-with-args 'org-cycle-hook 'overview))))
  4876. (defun org-cycle-internal-local ()
  4877. "Do the local cycling action."
  4878. (org-back-to-heading)
  4879. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4880. ;; First, some boundaries
  4881. (save-excursion
  4882. (org-back-to-heading)
  4883. (setq level (funcall outline-level))
  4884. (save-excursion
  4885. (beginning-of-line 2)
  4886. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4887. ; XEmacs does not have `next-single-char-property-change'
  4888. ; I'm not sure about Emacs 21.
  4889. (while (and (not (eobp)) ;; this is like `next-line'
  4890. (get-char-property (1- (point)) 'invisible))
  4891. (beginning-of-line 2))
  4892. (while (and (not (eobp)) ;; this is like `next-line'
  4893. (get-char-property (1- (point)) 'invisible))
  4894. (goto-char (next-single-char-property-change (point) 'invisible))
  4895. (and (eolp) (beginning-of-line 2))))
  4896. (setq eol (point)))
  4897. (outline-end-of-heading) (setq eoh (point))
  4898. (save-excursion
  4899. (outline-next-heading)
  4900. (setq has-children (and (org-at-heading-p t)
  4901. (> (funcall outline-level) level))))
  4902. (org-end-of-subtree t)
  4903. (unless (eobp)
  4904. (skip-chars-forward " \t\n")
  4905. (beginning-of-line 1) ; in case this is an item
  4906. )
  4907. (setq eos (if (eobp) (point) (1- (point)))))
  4908. ;; Find out what to do next and set `this-command'
  4909. (cond
  4910. ((= eos eoh)
  4911. ;; Nothing is hidden behind this heading
  4912. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4913. (message "EMPTY ENTRY")
  4914. (setq org-cycle-subtree-status nil)
  4915. (save-excursion
  4916. (goto-char eos)
  4917. (outline-next-heading)
  4918. (if (org-invisible-p) (org-flag-heading nil))))
  4919. ((and (or (>= eol eos)
  4920. (not (string-match "\\S-" (buffer-substring eol eos))))
  4921. (or has-children
  4922. (not (setq children-skipped
  4923. org-cycle-skip-children-state-if-no-children))))
  4924. ;; Entire subtree is hidden in one line: children view
  4925. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4926. (org-show-entry)
  4927. (show-children)
  4928. (message "CHILDREN")
  4929. (save-excursion
  4930. (goto-char eos)
  4931. (outline-next-heading)
  4932. (if (org-invisible-p) (org-flag-heading nil)))
  4933. (setq org-cycle-subtree-status 'children)
  4934. (run-hook-with-args 'org-cycle-hook 'children))
  4935. ((or children-skipped
  4936. (and (eq last-command this-command)
  4937. (eq org-cycle-subtree-status 'children)))
  4938. ;; We just showed the children, or no children are there,
  4939. ;; now show everything.
  4940. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4941. (org-show-subtree)
  4942. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4943. (setq org-cycle-subtree-status 'subtree)
  4944. (run-hook-with-args 'org-cycle-hook 'subtree))
  4945. (t
  4946. ;; Default action: hide the subtree.
  4947. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4948. (hide-subtree)
  4949. (message "FOLDED")
  4950. (setq org-cycle-subtree-status 'folded)
  4951. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4952. ;;;###autoload
  4953. (defun org-global-cycle (&optional arg)
  4954. "Cycle the global visibility. For details see `org-cycle'.
  4955. With C-u prefix arg, switch to startup visibility.
  4956. With a numeric prefix, show all headlines up to that level."
  4957. (interactive "P")
  4958. (let ((org-cycle-include-plain-lists
  4959. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4960. (cond
  4961. ((integerp arg)
  4962. (show-all)
  4963. (hide-sublevels arg)
  4964. (setq org-cycle-global-status 'contents))
  4965. ((equal arg '(4))
  4966. (org-set-startup-visibility)
  4967. (message "Startup visibility, plus VISIBILITY properties."))
  4968. (t
  4969. (org-cycle '(4))))))
  4970. (defun org-set-startup-visibility ()
  4971. "Set the visibility required by startup options and properties."
  4972. (cond
  4973. ((eq org-startup-folded t)
  4974. (org-cycle '(4)))
  4975. ((eq org-startup-folded 'content)
  4976. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4977. (org-cycle '(4)) (org-cycle '(4)))))
  4978. (unless (eq org-startup-folded 'showeverything)
  4979. (if org-hide-block-startup (org-hide-block-all))
  4980. (org-set-visibility-according-to-property 'no-cleanup)
  4981. (org-cycle-hide-archived-subtrees 'all)
  4982. (org-cycle-hide-drawers 'all)
  4983. (org-cycle-show-empty-lines 'all)))
  4984. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4985. "Switch subtree visibilities according to :VISIBILITY: property."
  4986. (interactive)
  4987. (let (org-show-entry-below state)
  4988. (save-excursion
  4989. (goto-char (point-min))
  4990. (while (re-search-forward
  4991. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4992. nil t)
  4993. (setq state (match-string 1))
  4994. (save-excursion
  4995. (org-back-to-heading t)
  4996. (hide-subtree)
  4997. (org-reveal)
  4998. (cond
  4999. ((equal state '("fold" "folded"))
  5000. (hide-subtree))
  5001. ((equal state "children")
  5002. (org-show-hidden-entry)
  5003. (show-children))
  5004. ((equal state "content")
  5005. (save-excursion
  5006. (save-restriction
  5007. (org-narrow-to-subtree)
  5008. (org-content))))
  5009. ((member state '("all" "showall"))
  5010. (show-subtree)))))
  5011. (unless no-cleanup
  5012. (org-cycle-hide-archived-subtrees 'all)
  5013. (org-cycle-hide-drawers 'all)
  5014. (org-cycle-show-empty-lines 'all)))))
  5015. (defun org-overview ()
  5016. "Switch to overview mode, showing only top-level headlines.
  5017. Really, this shows all headlines with level equal or greater than the level
  5018. of the first headline in the buffer. This is important, because if the
  5019. first headline is not level one, then (hide-sublevels 1) gives confusing
  5020. results."
  5021. (interactive)
  5022. (let ((level (save-excursion
  5023. (goto-char (point-min))
  5024. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5025. (progn
  5026. (goto-char (match-beginning 0))
  5027. (funcall outline-level))))))
  5028. (and level (hide-sublevels level))))
  5029. (defun org-content (&optional arg)
  5030. "Show all headlines in the buffer, like a table of contents.
  5031. With numerical argument N, show content up to level N."
  5032. (interactive "P")
  5033. (save-excursion
  5034. ;; Visit all headings and show their offspring
  5035. (and (integerp arg) (org-overview))
  5036. (goto-char (point-max))
  5037. (catch 'exit
  5038. (while (and (progn (condition-case nil
  5039. (outline-previous-visible-heading 1)
  5040. (error (goto-char (point-min))))
  5041. t)
  5042. (looking-at outline-regexp))
  5043. (if (integerp arg)
  5044. (show-children (1- arg))
  5045. (show-branches))
  5046. (if (bobp) (throw 'exit nil))))))
  5047. (defun org-optimize-window-after-visibility-change (state)
  5048. "Adjust the window after a change in outline visibility.
  5049. This function is the default value of the hook `org-cycle-hook'."
  5050. (when (get-buffer-window (current-buffer))
  5051. (cond
  5052. ((eq state 'content) nil)
  5053. ((eq state 'all) nil)
  5054. ((eq state 'folded) nil)
  5055. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5056. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5057. (defun org-remove-empty-overlays-at (pos)
  5058. "Remove outline overlays that do not contain non-white stuff."
  5059. (mapc
  5060. (lambda (o)
  5061. (and (eq 'outline (overlay-get o 'invisible))
  5062. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5063. (overlay-end o))))
  5064. (delete-overlay o)))
  5065. (overlays-at pos)))
  5066. (defun org-clean-visibility-after-subtree-move ()
  5067. "Fix visibility issues after moving a subtree."
  5068. ;; First, find a reasonable region to look at:
  5069. ;; Start two siblings above, end three below
  5070. (let* ((beg (save-excursion
  5071. (and (org-get-last-sibling)
  5072. (org-get-last-sibling))
  5073. (point)))
  5074. (end (save-excursion
  5075. (and (org-get-next-sibling)
  5076. (org-get-next-sibling)
  5077. (org-get-next-sibling))
  5078. (if (org-at-heading-p)
  5079. (point-at-eol)
  5080. (point))))
  5081. (level (looking-at "\\*+"))
  5082. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5083. (save-excursion
  5084. (save-restriction
  5085. (narrow-to-region beg end)
  5086. (when re
  5087. ;; Properly fold already folded siblings
  5088. (goto-char (point-min))
  5089. (while (re-search-forward re nil t)
  5090. (if (and (not (org-invisible-p))
  5091. (save-excursion
  5092. (goto-char (point-at-eol)) (org-invisible-p)))
  5093. (hide-entry))))
  5094. (org-cycle-show-empty-lines 'overview)
  5095. (org-cycle-hide-drawers 'overview)))))
  5096. (defun org-cycle-show-empty-lines (state)
  5097. "Show empty lines above all visible headlines.
  5098. The region to be covered depends on STATE when called through
  5099. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5100. entire buffer covered. Note that an empty line is only shown if there
  5101. are at least `org-cycle-separator-lines' empty lines before the headline."
  5102. (when (not (= org-cycle-separator-lines 0))
  5103. (save-excursion
  5104. (let* ((n (abs org-cycle-separator-lines))
  5105. (re (cond
  5106. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5107. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5108. (t (let ((ns (number-to-string (- n 2))))
  5109. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5110. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5111. beg end b e)
  5112. (cond
  5113. ((memq state '(overview contents t))
  5114. (setq beg (point-min) end (point-max)))
  5115. ((memq state '(children folded))
  5116. (setq beg (point) end (progn (org-end-of-subtree t t)
  5117. (beginning-of-line 2)
  5118. (point)))))
  5119. (when beg
  5120. (goto-char beg)
  5121. (while (re-search-forward re end t)
  5122. (unless (get-char-property (match-end 1) 'invisible)
  5123. (setq e (match-end 1))
  5124. (if (< org-cycle-separator-lines 0)
  5125. (setq b (save-excursion
  5126. (goto-char (match-beginning 0))
  5127. (org-back-over-empty-lines)
  5128. (if (save-excursion
  5129. (goto-char (max (point-min) (1- (point))))
  5130. (org-on-heading-p))
  5131. (1- (point))
  5132. (point))))
  5133. (setq b (match-beginning 1)))
  5134. (outline-flag-region b e nil)))))))
  5135. ;; Never hide empty lines at the end of the file.
  5136. (save-excursion
  5137. (goto-char (point-max))
  5138. (outline-previous-heading)
  5139. (outline-end-of-heading)
  5140. (if (and (looking-at "[ \t\n]+")
  5141. (= (match-end 0) (point-max)))
  5142. (outline-flag-region (point) (match-end 0) nil))))
  5143. (defun org-show-empty-lines-in-parent ()
  5144. "Move to the parent and re-show empty lines before visible headlines."
  5145. (save-excursion
  5146. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5147. (org-cycle-show-empty-lines context))))
  5148. (defun org-files-list ()
  5149. "Return `org-agenda-files' list, plus all open org-mode files.
  5150. This is useful for operations that need to scan all of a user's
  5151. open and agenda-wise Org files."
  5152. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5153. (dolist (buf (buffer-list))
  5154. (with-current-buffer buf
  5155. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5156. (let ((file (expand-file-name (buffer-file-name))))
  5157. (unless (member file files)
  5158. (push file files))))))
  5159. files))
  5160. (defsubst org-entry-beginning-position ()
  5161. "Return the beginning position of the current entry."
  5162. (save-excursion (outline-back-to-heading t) (point)))
  5163. (defsubst org-entry-end-position ()
  5164. "Return the end position of the current entry."
  5165. (save-excursion (outline-next-heading) (point)))
  5166. (defun org-cycle-hide-drawers (state)
  5167. "Re-hide all drawers after a visibility state change."
  5168. (when (and (org-mode-p)
  5169. (not (memq state '(overview folded contents))))
  5170. (save-excursion
  5171. (let* ((globalp (memq state '(contents all)))
  5172. (beg (if globalp (point-min) (point)))
  5173. (end (if globalp (point-max)
  5174. (if (eq state 'children)
  5175. (save-excursion (outline-next-heading) (point))
  5176. (org-end-of-subtree t)))))
  5177. (goto-char beg)
  5178. (while (re-search-forward org-drawer-regexp end t)
  5179. (org-flag-drawer t))))))
  5180. (defun org-flag-drawer (flag)
  5181. (save-excursion
  5182. (beginning-of-line 1)
  5183. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5184. (let ((b (match-end 0))
  5185. (outline-regexp org-outline-regexp))
  5186. (if (re-search-forward
  5187. "^[ \t]*:END:"
  5188. (save-excursion (outline-next-heading) (point)) t)
  5189. (outline-flag-region b (point-at-eol) flag)
  5190. (error ":END: line missing at position %s" b))))))
  5191. (defun org-subtree-end-visible-p ()
  5192. "Is the end of the current subtree visible?"
  5193. (pos-visible-in-window-p
  5194. (save-excursion (org-end-of-subtree t) (point))))
  5195. (defun org-first-headline-recenter (&optional N)
  5196. "Move cursor to the first headline and recenter the headline.
  5197. Optional argument N means put the headline into the Nth line of the window."
  5198. (goto-char (point-min))
  5199. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5200. (beginning-of-line)
  5201. (recenter (prefix-numeric-value N))))
  5202. ;;; Saving and restoring visibility
  5203. (defun org-outline-overlay-data (&optional use-markers)
  5204. "Return a list of the locations of all outline overlays.
  5205. The are overlays with the `invisible' property value `outline'.
  5206. The return valus is a list of cons cells, with start and stop
  5207. positions for each overlay.
  5208. If USE-MARKERS is set, return the positions as markers."
  5209. (let (beg end)
  5210. (save-excursion
  5211. (save-restriction
  5212. (widen)
  5213. (delq nil
  5214. (mapcar (lambda (o)
  5215. (when (eq (overlay-get o 'invisible) 'outline)
  5216. (setq beg (overlay-start o)
  5217. end (overlay-end o))
  5218. (and beg end (> end beg)
  5219. (if use-markers
  5220. (cons (move-marker (make-marker) beg)
  5221. (move-marker (make-marker) end))
  5222. (cons beg end)))))
  5223. (overlays-in (point-min) (point-max))))))))
  5224. (defun org-set-outline-overlay-data (data)
  5225. "Create visibility overlays for all positions in DATA.
  5226. DATA should have been made by `org-outline-overlay-data'."
  5227. (let (o)
  5228. (save-excursion
  5229. (save-restriction
  5230. (widen)
  5231. (show-all)
  5232. (mapc (lambda (c)
  5233. (setq o (make-overlay (car c) (cdr c)))
  5234. (overlay-put o 'invisible 'outline))
  5235. data)))))
  5236. (defmacro org-save-outline-visibility (use-markers &rest body)
  5237. "Save and restore outline visibility around BODY.
  5238. If USE-MARKERS is non-nil, use markers for the positions.
  5239. This means that the buffer may change while running BODY,
  5240. but it also means that the buffer should stay alive
  5241. during the operation, because otherwise all these markers will
  5242. point nowhere."
  5243. `(let ((data (org-outline-overlay-data ,use-markers)))
  5244. (unwind-protect
  5245. (progn
  5246. ,@body
  5247. (org-set-outline-overlay-data data))
  5248. (when ,use-markers
  5249. (mapc (lambda (c)
  5250. (and (markerp (car c)) (move-marker (car c) nil))
  5251. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5252. data)))))
  5253. ;;; Folding of blocks
  5254. (defconst org-block-regexp
  5255. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5256. "Regular expression for hiding blocks.")
  5257. (defvar org-hide-block-overlays nil
  5258. "Overlays hiding blocks.")
  5259. (make-variable-buffer-local 'org-hide-block-overlays)
  5260. (defun org-block-map (function &optional start end)
  5261. "Call func at the head of all source blocks in the current
  5262. buffer. Optional arguments START and END can be used to limit
  5263. the range."
  5264. (let ((start (or start (point-min)))
  5265. (end (or end (point-max))))
  5266. (save-excursion
  5267. (goto-char start)
  5268. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5269. (save-excursion
  5270. (save-match-data
  5271. (goto-char (match-beginning 0))
  5272. (funcall function)))))))
  5273. (defun org-hide-block-toggle-all ()
  5274. "Toggle the visibility of all blocks in the current buffer."
  5275. (org-block-map #'org-hide-block-toggle))
  5276. (defun org-hide-block-all ()
  5277. "Fold all blocks in the current buffer."
  5278. (interactive)
  5279. (org-show-block-all)
  5280. (org-block-map #'org-hide-block-toggle-maybe))
  5281. (defun org-show-block-all ()
  5282. "Unfold all blocks in the current buffer."
  5283. (mapc 'delete-overlay org-hide-block-overlays)
  5284. (setq org-hide-block-overlays nil))
  5285. (defun org-hide-block-toggle-maybe ()
  5286. "Toggle visibility of block at point."
  5287. (interactive)
  5288. (let ((case-fold-search t))
  5289. (if (save-excursion
  5290. (beginning-of-line 1)
  5291. (looking-at org-block-regexp))
  5292. (progn (org-hide-block-toggle)
  5293. t) ;; to signal that we took action
  5294. nil))) ;; to signal that we did not
  5295. (defun org-hide-block-toggle (&optional force)
  5296. "Toggle the visibility of the current block."
  5297. (interactive)
  5298. (save-excursion
  5299. (beginning-of-line)
  5300. (if (re-search-forward org-block-regexp nil t)
  5301. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5302. (end (match-end 0)) ;; end of entire body
  5303. ov)
  5304. (if (memq t (mapcar (lambda (overlay)
  5305. (eq (overlay-get overlay 'invisible)
  5306. 'org-hide-block))
  5307. (overlays-at start)))
  5308. (if (or (not force) (eq force 'off))
  5309. (mapc (lambda (ov)
  5310. (when (member ov org-hide-block-overlays)
  5311. (setq org-hide-block-overlays
  5312. (delq ov org-hide-block-overlays)))
  5313. (when (eq (overlay-get ov 'invisible)
  5314. 'org-hide-block)
  5315. (delete-overlay ov)))
  5316. (overlays-at start)))
  5317. (setq ov (make-overlay start end))
  5318. (overlay-put ov 'invisible 'org-hide-block)
  5319. ;; make the block accessible to isearch
  5320. (overlay-put
  5321. ov 'isearch-open-invisible
  5322. (lambda (ov)
  5323. (when (member ov org-hide-block-overlays)
  5324. (setq org-hide-block-overlays
  5325. (delq ov org-hide-block-overlays)))
  5326. (when (eq (overlay-get ov 'invisible)
  5327. 'org-hide-block)
  5328. (delete-overlay ov))))
  5329. (push ov org-hide-block-overlays)))
  5330. (error "Not looking at a source block"))))
  5331. ;; org-tab-after-check-for-cycling-hook
  5332. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5333. ;; Remove overlays when changing major mode
  5334. (add-hook 'org-mode-hook
  5335. (lambda () (org-add-hook 'change-major-mode-hook
  5336. 'org-show-block-all 'append 'local)))
  5337. ;;; Org-goto
  5338. (defvar org-goto-window-configuration nil)
  5339. (defvar org-goto-marker nil)
  5340. (defvar org-goto-map
  5341. (let ((map (make-sparse-keymap)))
  5342. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5343. (while (setq cmd (pop cmds))
  5344. (substitute-key-definition cmd cmd map global-map)))
  5345. (suppress-keymap map)
  5346. (org-defkey map "\C-m" 'org-goto-ret)
  5347. (org-defkey map [(return)] 'org-goto-ret)
  5348. (org-defkey map [(left)] 'org-goto-left)
  5349. (org-defkey map [(right)] 'org-goto-right)
  5350. (org-defkey map [(control ?g)] 'org-goto-quit)
  5351. (org-defkey map "\C-i" 'org-cycle)
  5352. (org-defkey map [(tab)] 'org-cycle)
  5353. (org-defkey map [(down)] 'outline-next-visible-heading)
  5354. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5355. (if org-goto-auto-isearch
  5356. (if (fboundp 'define-key-after)
  5357. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5358. nil)
  5359. (org-defkey map "q" 'org-goto-quit)
  5360. (org-defkey map "n" 'outline-next-visible-heading)
  5361. (org-defkey map "p" 'outline-previous-visible-heading)
  5362. (org-defkey map "f" 'outline-forward-same-level)
  5363. (org-defkey map "b" 'outline-backward-same-level)
  5364. (org-defkey map "u" 'outline-up-heading))
  5365. (org-defkey map "/" 'org-occur)
  5366. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5367. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5368. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5369. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5370. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5371. map))
  5372. (defconst org-goto-help
  5373. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5374. RET=jump to location [Q]uit and return to previous location
  5375. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5376. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5377. ;; FIXME: Docstring does not mention both interfaces
  5378. (defun org-goto (&optional alternative-interface)
  5379. "Look up a different location in the current file, keeping current visibility.
  5380. When you want look-up or go to a different location in a document, the
  5381. fastest way is often to fold the entire buffer and then dive into the tree.
  5382. This method has the disadvantage, that the previous location will be folded,
  5383. which may not be what you want.
  5384. This command works around this by showing a copy of the current buffer
  5385. in an indirect buffer, in overview mode. You can dive into the tree in
  5386. that copy, use org-occur and incremental search to find a location.
  5387. When pressing RET or `Q', the command returns to the original buffer in
  5388. which the visibility is still unchanged. After RET is will also jump to
  5389. the location selected in the indirect buffer and expose the
  5390. the headline hierarchy above."
  5391. (interactive "P")
  5392. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5393. (org-refile-use-outline-path t)
  5394. (org-refile-target-verify-function nil)
  5395. (interface
  5396. (if (not alternative-interface)
  5397. org-goto-interface
  5398. (if (eq org-goto-interface 'outline)
  5399. 'outline-path-completion
  5400. 'outline)))
  5401. (org-goto-start-pos (point))
  5402. (selected-point
  5403. (if (eq interface 'outline)
  5404. (car (org-get-location (current-buffer) org-goto-help))
  5405. (nth 3 (org-refile-get-location "Goto: ")))))
  5406. (if selected-point
  5407. (progn
  5408. (org-mark-ring-push org-goto-start-pos)
  5409. (goto-char selected-point)
  5410. (if (or (org-invisible-p) (org-invisible-p2))
  5411. (org-show-context 'org-goto)))
  5412. (message "Quit"))))
  5413. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5414. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5415. (defvar org-goto-local-auto-isearch-map) ; defined below
  5416. (defun org-get-location (buf help)
  5417. "Let the user select a location in the Org-mode buffer BUF.
  5418. This function uses a recursive edit. It returns the selected position
  5419. or nil."
  5420. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5421. (isearch-hide-immediately nil)
  5422. (isearch-search-fun-function
  5423. (lambda () 'org-goto-local-search-headings))
  5424. (org-goto-selected-point org-goto-exit-command)
  5425. (pop-up-frames nil)
  5426. (special-display-buffer-names nil)
  5427. (special-display-regexps nil)
  5428. (special-display-function nil))
  5429. (save-excursion
  5430. (save-window-excursion
  5431. (delete-other-windows)
  5432. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5433. (switch-to-buffer
  5434. (condition-case nil
  5435. (make-indirect-buffer (current-buffer) "*org-goto*")
  5436. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5437. (with-output-to-temp-buffer "*Help*"
  5438. (princ help))
  5439. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5440. (setq buffer-read-only nil)
  5441. (let ((org-startup-truncated t)
  5442. (org-startup-folded nil)
  5443. (org-startup-align-all-tables nil))
  5444. (org-mode)
  5445. (org-overview))
  5446. (setq buffer-read-only t)
  5447. (if (and (boundp 'org-goto-start-pos)
  5448. (integer-or-marker-p org-goto-start-pos))
  5449. (let ((org-show-hierarchy-above t)
  5450. (org-show-siblings t)
  5451. (org-show-following-heading t))
  5452. (goto-char org-goto-start-pos)
  5453. (and (org-invisible-p) (org-show-context)))
  5454. (goto-char (point-min)))
  5455. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5456. (message "Select location and press RET")
  5457. (use-local-map org-goto-map)
  5458. (recursive-edit)
  5459. ))
  5460. (kill-buffer "*org-goto*")
  5461. (cons org-goto-selected-point org-goto-exit-command)))
  5462. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5463. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5464. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5465. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5466. (defun org-goto-local-search-headings (string bound noerror)
  5467. "Search and make sure that any matches are in headlines."
  5468. (catch 'return
  5469. (while (if isearch-forward
  5470. (search-forward string bound noerror)
  5471. (search-backward string bound noerror))
  5472. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5473. (and (member :headline context)
  5474. (not (member :tags context))))
  5475. (throw 'return (point))))))
  5476. (defun org-goto-local-auto-isearch ()
  5477. "Start isearch."
  5478. (interactive)
  5479. (goto-char (point-min))
  5480. (let ((keys (this-command-keys)))
  5481. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5482. (isearch-mode t)
  5483. (isearch-process-search-char (string-to-char keys)))))
  5484. (defun org-goto-ret (&optional arg)
  5485. "Finish `org-goto' by going to the new location."
  5486. (interactive "P")
  5487. (setq org-goto-selected-point (point)
  5488. org-goto-exit-command 'return)
  5489. (throw 'exit nil))
  5490. (defun org-goto-left ()
  5491. "Finish `org-goto' by going to the new location."
  5492. (interactive)
  5493. (if (org-on-heading-p)
  5494. (progn
  5495. (beginning-of-line 1)
  5496. (setq org-goto-selected-point (point)
  5497. org-goto-exit-command 'left)
  5498. (throw 'exit nil))
  5499. (error "Not on a heading")))
  5500. (defun org-goto-right ()
  5501. "Finish `org-goto' by going to the new location."
  5502. (interactive)
  5503. (if (org-on-heading-p)
  5504. (progn
  5505. (setq org-goto-selected-point (point)
  5506. org-goto-exit-command 'right)
  5507. (throw 'exit nil))
  5508. (error "Not on a heading")))
  5509. (defun org-goto-quit ()
  5510. "Finish `org-goto' without cursor motion."
  5511. (interactive)
  5512. (setq org-goto-selected-point nil)
  5513. (setq org-goto-exit-command 'quit)
  5514. (throw 'exit nil))
  5515. ;;; Indirect buffer display of subtrees
  5516. (defvar org-indirect-dedicated-frame nil
  5517. "This is the frame being used for indirect tree display.")
  5518. (defvar org-last-indirect-buffer nil)
  5519. (defun org-tree-to-indirect-buffer (&optional arg)
  5520. "Create indirect buffer and narrow it to current subtree.
  5521. With numerical prefix ARG, go up to this level and then take that tree.
  5522. If ARG is negative, go up that many levels.
  5523. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5524. indirect buffer previously made with this command, to avoid proliferation of
  5525. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5526. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5527. is kept so that you can work with several indirect buffers at the same time.
  5528. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5529. requests that a new frame be made for the new buffer, so that the dedicated
  5530. frame is not changed."
  5531. (interactive "P")
  5532. (let ((cbuf (current-buffer))
  5533. (cwin (selected-window))
  5534. (pos (point))
  5535. beg end level heading ibuf)
  5536. (save-excursion
  5537. (org-back-to-heading t)
  5538. (when (numberp arg)
  5539. (setq level (org-outline-level))
  5540. (if (< arg 0) (setq arg (+ level arg)))
  5541. (while (> (setq level (org-outline-level)) arg)
  5542. (outline-up-heading 1 t)))
  5543. (setq beg (point)
  5544. heading (org-get-heading))
  5545. (org-end-of-subtree t t)
  5546. (if (org-on-heading-p) (backward-char 1))
  5547. (setq end (point)))
  5548. (if (and (buffer-live-p org-last-indirect-buffer)
  5549. (not (eq org-indirect-buffer-display 'new-frame))
  5550. (not arg))
  5551. (kill-buffer org-last-indirect-buffer))
  5552. (setq ibuf (org-get-indirect-buffer cbuf)
  5553. org-last-indirect-buffer ibuf)
  5554. (cond
  5555. ((or (eq org-indirect-buffer-display 'new-frame)
  5556. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5557. (select-frame (make-frame))
  5558. (delete-other-windows)
  5559. (switch-to-buffer ibuf)
  5560. (org-set-frame-title heading))
  5561. ((eq org-indirect-buffer-display 'dedicated-frame)
  5562. (raise-frame
  5563. (select-frame (or (and org-indirect-dedicated-frame
  5564. (frame-live-p org-indirect-dedicated-frame)
  5565. org-indirect-dedicated-frame)
  5566. (setq org-indirect-dedicated-frame (make-frame)))))
  5567. (delete-other-windows)
  5568. (switch-to-buffer ibuf)
  5569. (org-set-frame-title (concat "Indirect: " heading)))
  5570. ((eq org-indirect-buffer-display 'current-window)
  5571. (switch-to-buffer ibuf))
  5572. ((eq org-indirect-buffer-display 'other-window)
  5573. (pop-to-buffer ibuf))
  5574. (t (error "Invalid value")))
  5575. (if (featurep 'xemacs)
  5576. (save-excursion (org-mode) (turn-on-font-lock)))
  5577. (narrow-to-region beg end)
  5578. (show-all)
  5579. (goto-char pos)
  5580. (and (window-live-p cwin) (select-window cwin))))
  5581. (defun org-get-indirect-buffer (&optional buffer)
  5582. (setq buffer (or buffer (current-buffer)))
  5583. (let ((n 1) (base (buffer-name buffer)) bname)
  5584. (while (buffer-live-p
  5585. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5586. (setq n (1+ n)))
  5587. (condition-case nil
  5588. (make-indirect-buffer buffer bname 'clone)
  5589. (error (make-indirect-buffer buffer bname)))))
  5590. (defun org-set-frame-title (title)
  5591. "Set the title of the current frame to the string TITLE."
  5592. ;; FIXME: how to name a single frame in XEmacs???
  5593. (unless (featurep 'xemacs)
  5594. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5595. ;;;; Structure editing
  5596. ;;; Inserting headlines
  5597. (defun org-previous-line-empty-p ()
  5598. (save-excursion
  5599. (and (not (bobp))
  5600. (or (beginning-of-line 0) t)
  5601. (save-match-data
  5602. (looking-at "[ \t]*$")))))
  5603. (defun org-insert-heading (&optional force-heading invisible-ok)
  5604. "Insert a new heading or item with same depth at point.
  5605. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5606. If point is at the beginning of a headline, insert a sibling before the
  5607. current headline. If point is not at the beginning, do not split the line,
  5608. but create the new headline after the current line.
  5609. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5610. This is important for non-interactive uses of the command."
  5611. (interactive "P")
  5612. (if (or (= (buffer-size) 0)
  5613. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5614. (org-on-heading-p))))
  5615. (not (org-in-item-p))))
  5616. (insert "\n* ")
  5617. (when (or force-heading (not (org-insert-item)))
  5618. (let* ((empty-line-p nil)
  5619. (head (save-excursion
  5620. (condition-case nil
  5621. (progn
  5622. (org-back-to-heading invisible-ok)
  5623. (setq empty-line-p (org-previous-line-empty-p))
  5624. (match-string 0))
  5625. (error "*"))))
  5626. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5627. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5628. pos hide-previous previous-pos)
  5629. (cond
  5630. ((and (org-on-heading-p) (bolp)
  5631. (or (bobp)
  5632. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5633. ;; insert before the current line
  5634. (open-line (if blank 2 1)))
  5635. ((and (bolp)
  5636. (not org-insert-heading-respect-content)
  5637. (or (bobp)
  5638. (save-excursion
  5639. (backward-char 1) (not (org-invisible-p)))))
  5640. ;; insert right here
  5641. nil)
  5642. (t
  5643. ;; somewhere in the line
  5644. (save-excursion
  5645. (setq previous-pos (point-at-bol))
  5646. (end-of-line)
  5647. (setq hide-previous (org-invisible-p)))
  5648. (and org-insert-heading-respect-content (org-show-subtree))
  5649. (let ((split
  5650. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5651. (save-excursion
  5652. (let ((p (point)))
  5653. (goto-char (point-at-bol))
  5654. (and (looking-at org-complex-heading-regexp)
  5655. (> p (match-beginning 4)))))))
  5656. tags pos)
  5657. (cond
  5658. (org-insert-heading-respect-content
  5659. (org-end-of-subtree nil t)
  5660. (or (bolp) (newline))
  5661. (or (org-previous-line-empty-p)
  5662. (and blank (newline)))
  5663. (open-line 1))
  5664. ((org-on-heading-p)
  5665. (when hide-previous
  5666. (show-children)
  5667. (org-show-entry))
  5668. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5669. (setq tags (and (match-end 2) (match-string 2)))
  5670. (and (match-end 1)
  5671. (delete-region (match-beginning 1) (match-end 1)))
  5672. (setq pos (point-at-bol))
  5673. (or split (end-of-line 1))
  5674. (delete-horizontal-space)
  5675. (if (string-match "\\`\\*+\\'"
  5676. (buffer-substring (point-at-bol) (point)))
  5677. (insert " "))
  5678. (newline (if blank 2 1))
  5679. (when tags
  5680. (save-excursion
  5681. (goto-char pos)
  5682. (end-of-line 1)
  5683. (insert " " tags)
  5684. (org-set-tags nil 'align))))
  5685. (t
  5686. (or split (end-of-line 1))
  5687. (newline (if blank 2 1)))))))
  5688. (insert head) (just-one-space)
  5689. (setq pos (point))
  5690. (end-of-line 1)
  5691. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5692. (when (and org-insert-heading-respect-content hide-previous)
  5693. (save-excursion
  5694. (goto-char previous-pos)
  5695. (hide-subtree)))
  5696. (run-hooks 'org-insert-heading-hook)))))
  5697. (defun org-get-heading (&optional no-tags)
  5698. "Return the heading of the current entry, without the stars."
  5699. (save-excursion
  5700. (org-back-to-heading t)
  5701. (if (looking-at
  5702. (if no-tags
  5703. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5704. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5705. (match-string 1) "")))
  5706. (defun org-heading-components ()
  5707. "Return the components of the current heading.
  5708. This is a list with the following elements:
  5709. - the level as an integer
  5710. - the reduced level, different if `org-odd-levels-only' is set.
  5711. - the TODO keyword, or nil
  5712. - the priority character, like ?A, or nil if no priority is given
  5713. - the headline text itself, or the tags string if no headline text
  5714. - the tags string, or nil."
  5715. (save-excursion
  5716. (org-back-to-heading t)
  5717. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5718. (list (length (match-string 1))
  5719. (org-reduced-level (length (match-string 1)))
  5720. (org-match-string-no-properties 2)
  5721. (and (match-end 3) (aref (match-string 3) 2))
  5722. (org-match-string-no-properties 4)
  5723. (org-match-string-no-properties 5)))))
  5724. (defun org-get-entry ()
  5725. "Get the entry text, after heading, entire subtree."
  5726. (save-excursion
  5727. (org-back-to-heading t)
  5728. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5729. (defun org-insert-heading-after-current ()
  5730. "Insert a new heading with same level as current, after current subtree."
  5731. (interactive)
  5732. (org-back-to-heading)
  5733. (org-insert-heading)
  5734. (org-move-subtree-down)
  5735. (end-of-line 1))
  5736. (defun org-insert-heading-respect-content ()
  5737. (interactive)
  5738. (let ((org-insert-heading-respect-content t))
  5739. (org-insert-heading t)))
  5740. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5741. (interactive "P")
  5742. (let ((org-insert-heading-respect-content t))
  5743. (org-insert-todo-heading force-state t)))
  5744. (defun org-insert-todo-heading (arg &optional force-heading)
  5745. "Insert a new heading with the same level and TODO state as current heading.
  5746. If the heading has no TODO state, or if the state is DONE, use the first
  5747. state (TODO by default). Also with prefix arg, force first state."
  5748. (interactive "P")
  5749. (when (or force-heading (not (org-insert-item 'checkbox)))
  5750. (org-insert-heading force-heading)
  5751. (save-excursion
  5752. (org-back-to-heading)
  5753. (outline-previous-heading)
  5754. (looking-at org-todo-line-regexp))
  5755. (let*
  5756. ((new-mark-x
  5757. (if (or arg
  5758. (not (match-beginning 2))
  5759. (member (match-string 2) org-done-keywords))
  5760. (car org-todo-keywords-1)
  5761. (match-string 2)))
  5762. (new-mark
  5763. (or
  5764. (run-hook-with-args-until-success
  5765. 'org-todo-get-default-hook new-mark-x nil)
  5766. new-mark-x)))
  5767. (beginning-of-line 1)
  5768. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5769. (if org-treat-insert-todo-heading-as-state-change
  5770. (org-todo new-mark)
  5771. (insert new-mark " "))))
  5772. (when org-provide-todo-statistics
  5773. (org-update-parent-todo-statistics))))
  5774. (defun org-insert-subheading (arg)
  5775. "Insert a new subheading and demote it.
  5776. Works for outline headings and for plain lists alike."
  5777. (interactive "P")
  5778. (org-insert-heading arg)
  5779. (cond
  5780. ((org-on-heading-p) (org-do-demote))
  5781. ((org-at-item-p) (org-indent-item 1))))
  5782. (defun org-insert-todo-subheading (arg)
  5783. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5784. Works for outline headings and for plain lists alike."
  5785. (interactive "P")
  5786. (org-insert-todo-heading arg)
  5787. (cond
  5788. ((org-on-heading-p) (org-do-demote))
  5789. ((org-at-item-p) (org-indent-item 1))))
  5790. ;;; Promotion and Demotion
  5791. (defvar org-after-demote-entry-hook nil
  5792. "Hook run after an entry has been demoted.
  5793. The cursor will be at the beginning of the entry.
  5794. When a subtree is being demoted, the hook will be called for each node.")
  5795. (defvar org-after-promote-entry-hook nil
  5796. "Hook run after an entry has been promoted.
  5797. The cursor will be at the beginning of the entry.
  5798. When a subtree is being promoted, the hook will be called for each node.")
  5799. (defun org-promote-subtree ()
  5800. "Promote the entire subtree.
  5801. See also `org-promote'."
  5802. (interactive)
  5803. (save-excursion
  5804. (org-map-tree 'org-promote))
  5805. (org-fix-position-after-promote))
  5806. (defun org-demote-subtree ()
  5807. "Demote the entire subtree. See `org-demote'.
  5808. See also `org-promote'."
  5809. (interactive)
  5810. (save-excursion
  5811. (org-map-tree 'org-demote))
  5812. (org-fix-position-after-promote))
  5813. (defun org-do-promote ()
  5814. "Promote the current heading higher up the tree.
  5815. If the region is active in `transient-mark-mode', promote all headings
  5816. in the region."
  5817. (interactive)
  5818. (save-excursion
  5819. (if (org-region-active-p)
  5820. (org-map-region 'org-promote (region-beginning) (region-end))
  5821. (org-promote)))
  5822. (org-fix-position-after-promote))
  5823. (defun org-do-demote ()
  5824. "Demote the current heading lower down the tree.
  5825. If the region is active in `transient-mark-mode', demote all headings
  5826. in the region."
  5827. (interactive)
  5828. (save-excursion
  5829. (if (org-region-active-p)
  5830. (org-map-region 'org-demote (region-beginning) (region-end))
  5831. (org-demote)))
  5832. (org-fix-position-after-promote))
  5833. (defun org-fix-position-after-promote ()
  5834. "Make sure that after pro/demotion cursor position is right."
  5835. (let ((pos (point)))
  5836. (when (save-excursion
  5837. (beginning-of-line 1)
  5838. (looking-at org-todo-line-regexp)
  5839. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5840. (cond ((eobp) (insert " "))
  5841. ((eolp) (insert " "))
  5842. ((equal (char-after) ?\ ) (forward-char 1))))))
  5843. (defun org-current-level ()
  5844. "Return the level of the current entry, or nil if before the first headline.
  5845. The level is the number of stars at the beginning of the headline."
  5846. (save-excursion
  5847. (condition-case nil
  5848. (progn
  5849. (org-back-to-heading t)
  5850. (funcall outline-level))
  5851. (error nil))))
  5852. (defun org-get-previous-line-level ()
  5853. "Return the outline depth of the last headline before the current line.
  5854. Returns 0 for the first headline in the buffer, and nil if before the
  5855. first headline."
  5856. (let ((current-level (org-current-level))
  5857. (prev-level (when (> (line-number-at-pos) 1)
  5858. (save-excursion
  5859. (beginning-of-line 0)
  5860. (org-current-level)))))
  5861. (cond ((null current-level) nil) ; Before first headline
  5862. ((null prev-level) 0) ; At first headline
  5863. (prev-level))))
  5864. (defun org-reduced-level (l)
  5865. "Compute the effective level of a heading.
  5866. This takes into account the setting of `org-odd-levels-only'."
  5867. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5868. (defun org-level-increment ()
  5869. "Return the number of stars that will be added or removed at a
  5870. time to headlines when structure editing, based on the value of
  5871. `org-odd-levels-only'."
  5872. (if org-odd-levels-only 2 1))
  5873. (defun org-get-valid-level (level &optional change)
  5874. "Rectify a level change under the influence of `org-odd-levels-only'
  5875. LEVEL is a current level, CHANGE is by how much the level should be
  5876. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5877. even level numbers will become the next higher odd number."
  5878. (if org-odd-levels-only
  5879. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5880. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5881. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5882. (max 1 (+ level (or change 0)))))
  5883. (if (boundp 'define-obsolete-function-alias)
  5884. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5885. (define-obsolete-function-alias 'org-get-legal-level
  5886. 'org-get-valid-level)
  5887. (define-obsolete-function-alias 'org-get-legal-level
  5888. 'org-get-valid-level "23.1")))
  5889. (defun org-promote ()
  5890. "Promote the current heading higher up the tree.
  5891. If the region is active in `transient-mark-mode', promote all headings
  5892. in the region."
  5893. (org-back-to-heading t)
  5894. (let* ((level (save-match-data (funcall outline-level)))
  5895. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5896. (diff (abs (- level (length up-head) -1))))
  5897. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5898. (replace-match up-head nil t)
  5899. ;; Fixup tag positioning
  5900. (and org-auto-align-tags (org-set-tags nil t))
  5901. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5902. (run-hooks 'org-after-promote-entry-hook)))
  5903. (defun org-demote ()
  5904. "Demote the current heading lower down the tree.
  5905. If the region is active in `transient-mark-mode', demote all headings
  5906. in the region."
  5907. (org-back-to-heading t)
  5908. (let* ((level (save-match-data (funcall outline-level)))
  5909. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5910. (diff (abs (- level (length down-head) -1))))
  5911. (replace-match down-head nil t)
  5912. ;; Fixup tag positioning
  5913. (and org-auto-align-tags (org-set-tags nil t))
  5914. (if org-adapt-indentation (org-fixup-indentation diff))
  5915. (run-hooks 'org-after-demote-entry-hook)))
  5916. (defun org-cycle-level ()
  5917. "Cycle the level of an empty headline through possible states.
  5918. This goes first to child, then to parent, level, then up the hierarchy.
  5919. After top level, it switches back to sibling level."
  5920. (interactive)
  5921. (let ((org-adapt-indentation nil))
  5922. (when (org-point-at-end-of-empty-headline)
  5923. (setq this-command 'org-cycle-level) ; Only needed for caching
  5924. (let ((cur-level (org-current-level))
  5925. (prev-level (org-get-previous-line-level)))
  5926. (cond
  5927. ;; If first headline in file, promote to top-level.
  5928. ((= prev-level 0)
  5929. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5930. do (org-do-promote)))
  5931. ;; If same level as prev, demote one.
  5932. ((= prev-level cur-level)
  5933. (org-do-demote))
  5934. ;; If parent is top-level, promote to top level if not already.
  5935. ((= prev-level 1)
  5936. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5937. do (org-do-promote)))
  5938. ;; If top-level, return to prev-level.
  5939. ((= cur-level 1)
  5940. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5941. do (org-do-demote)))
  5942. ;; If less than prev-level, promote one.
  5943. ((< cur-level prev-level)
  5944. (org-do-promote))
  5945. ;; If deeper than prev-level, promote until higher than
  5946. ;; prev-level.
  5947. ((> cur-level prev-level)
  5948. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5949. do (org-do-promote))))
  5950. t))))
  5951. (defun org-map-tree (fun)
  5952. "Call FUN for every heading underneath the current one."
  5953. (org-back-to-heading)
  5954. (let ((level (funcall outline-level)))
  5955. (save-excursion
  5956. (funcall fun)
  5957. (while (and (progn
  5958. (outline-next-heading)
  5959. (> (funcall outline-level) level))
  5960. (not (eobp)))
  5961. (funcall fun)))))
  5962. (defun org-map-region (fun beg end)
  5963. "Call FUN for every heading between BEG and END."
  5964. (let ((org-ignore-region t))
  5965. (save-excursion
  5966. (setq end (copy-marker end))
  5967. (goto-char beg)
  5968. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5969. (< (point) end))
  5970. (funcall fun))
  5971. (while (and (progn
  5972. (outline-next-heading)
  5973. (< (point) end))
  5974. (not (eobp)))
  5975. (funcall fun)))))
  5976. (defun org-fixup-indentation (diff)
  5977. "Change the indentation in the current entry by DIFF
  5978. However, if any line in the current entry has no indentation, or if it
  5979. would end up with no indentation after the change, nothing at all is done."
  5980. (save-excursion
  5981. (let ((end (save-excursion (outline-next-heading)
  5982. (point-marker)))
  5983. (prohibit (if (> diff 0)
  5984. "^\\S-"
  5985. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5986. col)
  5987. (unless (save-excursion (end-of-line 1)
  5988. (re-search-forward prohibit end t))
  5989. (while (and (< (point) end)
  5990. (re-search-forward "^[ \t]+" end t))
  5991. (goto-char (match-end 0))
  5992. (setq col (current-column))
  5993. (if (< diff 0) (replace-match ""))
  5994. (org-indent-to-column (+ diff col))))
  5995. (move-marker end nil))))
  5996. (defun org-convert-to-odd-levels ()
  5997. "Convert an org-mode file with all levels allowed to one with odd levels.
  5998. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5999. level 5 etc."
  6000. (interactive)
  6001. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6002. (let ((outline-regexp org-outline-regexp)
  6003. (outline-level 'org-outline-level)
  6004. (org-odd-levels-only nil) n)
  6005. (save-excursion
  6006. (goto-char (point-min))
  6007. (while (re-search-forward "^\\*\\*+ " nil t)
  6008. (setq n (- (length (match-string 0)) 2))
  6009. (while (>= (setq n (1- n)) 0)
  6010. (org-demote))
  6011. (end-of-line 1))))))
  6012. (defun org-convert-to-oddeven-levels ()
  6013. "Convert an org-mode file with only odd levels to one with odd and even levels.
  6014. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  6015. section with an even level, conversion would destroy the structure of the file. An error
  6016. is signaled in this case."
  6017. (interactive)
  6018. (goto-char (point-min))
  6019. ;; First check if there are no even levels
  6020. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6021. (org-show-context t)
  6022. (error "Not all levels are odd in this file. Conversion not possible"))
  6023. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6024. (let ((outline-regexp org-outline-regexp)
  6025. (outline-level 'org-outline-level)
  6026. (org-odd-levels-only nil) n)
  6027. (save-excursion
  6028. (goto-char (point-min))
  6029. (while (re-search-forward "^\\*\\*+ " nil t)
  6030. (setq n (/ (1- (length (match-string 0))) 2))
  6031. (while (>= (setq n (1- n)) 0)
  6032. (org-promote))
  6033. (end-of-line 1))))))
  6034. (defun org-tr-level (n)
  6035. "Make N odd if required."
  6036. (if org-odd-levels-only (1+ (/ n 2)) n))
  6037. ;;; Vertical tree motion, cutting and pasting of subtrees
  6038. (defun org-move-subtree-up (&optional arg)
  6039. "Move the current subtree up past ARG headlines of the same level."
  6040. (interactive "p")
  6041. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6042. (defun org-move-subtree-down (&optional arg)
  6043. "Move the current subtree down past ARG headlines of the same level."
  6044. (interactive "p")
  6045. (setq arg (prefix-numeric-value arg))
  6046. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6047. 'org-get-last-sibling))
  6048. (ins-point (make-marker))
  6049. (cnt (abs arg))
  6050. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6051. ;; Select the tree
  6052. (org-back-to-heading)
  6053. (setq beg0 (point))
  6054. (save-excursion
  6055. (setq ne-beg (org-back-over-empty-lines))
  6056. (setq beg (point)))
  6057. (save-match-data
  6058. (save-excursion (outline-end-of-heading)
  6059. (setq folded (org-invisible-p)))
  6060. (outline-end-of-subtree))
  6061. (outline-next-heading)
  6062. (setq ne-end (org-back-over-empty-lines))
  6063. (setq end (point))
  6064. (goto-char beg0)
  6065. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6066. ;; include less whitespace
  6067. (save-excursion
  6068. (goto-char beg)
  6069. (forward-line (- ne-beg ne-end))
  6070. (setq beg (point))))
  6071. ;; Find insertion point, with error handling
  6072. (while (> cnt 0)
  6073. (or (and (funcall movfunc) (looking-at outline-regexp))
  6074. (progn (goto-char beg0)
  6075. (error "Cannot move past superior level or buffer limit")))
  6076. (setq cnt (1- cnt)))
  6077. (if (> arg 0)
  6078. ;; Moving forward - still need to move over subtree
  6079. (progn (org-end-of-subtree t t)
  6080. (save-excursion
  6081. (org-back-over-empty-lines)
  6082. (or (bolp) (newline)))))
  6083. (setq ne-ins (org-back-over-empty-lines))
  6084. (move-marker ins-point (point))
  6085. (setq txt (buffer-substring beg end))
  6086. (org-save-markers-in-region beg end)
  6087. (delete-region beg end)
  6088. (org-remove-empty-overlays-at beg)
  6089. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6090. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6091. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6092. (let ((bbb (point)))
  6093. (insert-before-markers txt)
  6094. (org-reinstall-markers-in-region bbb)
  6095. (move-marker ins-point bbb))
  6096. (or (bolp) (insert "\n"))
  6097. (setq ins-end (point))
  6098. (goto-char ins-point)
  6099. (org-skip-whitespace)
  6100. (when (and (< arg 0)
  6101. (org-first-sibling-p)
  6102. (> ne-ins ne-beg))
  6103. ;; Move whitespace back to beginning
  6104. (save-excursion
  6105. (goto-char ins-end)
  6106. (let ((kill-whole-line t))
  6107. (kill-line (- ne-ins ne-beg)) (point)))
  6108. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6109. (move-marker ins-point nil)
  6110. (if folded
  6111. (hide-subtree)
  6112. (org-show-entry)
  6113. (show-children)
  6114. (org-cycle-hide-drawers 'children))
  6115. (org-clean-visibility-after-subtree-move)))
  6116. (defvar org-subtree-clip ""
  6117. "Clipboard for cut and paste of subtrees.
  6118. This is actually only a copy of the kill, because we use the normal kill
  6119. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6120. (defvar org-subtree-clip-folded nil
  6121. "Was the last copied subtree folded?
  6122. This is used to fold the tree back after pasting.")
  6123. (defun org-cut-subtree (&optional n)
  6124. "Cut the current subtree into the clipboard.
  6125. With prefix arg N, cut this many sequential subtrees.
  6126. This is a short-hand for marking the subtree and then cutting it."
  6127. (interactive "p")
  6128. (org-copy-subtree n 'cut))
  6129. (defun org-copy-subtree (&optional n cut force-store-markers)
  6130. "Cut the current subtree into the clipboard.
  6131. With prefix arg N, cut this many sequential subtrees.
  6132. This is a short-hand for marking the subtree and then copying it.
  6133. If CUT is non-nil, actually cut the subtree.
  6134. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6135. of some markers in the region, even if CUT is non-nil. This is
  6136. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6137. (interactive "p")
  6138. (let (beg end folded (beg0 (point)))
  6139. (if (interactive-p)
  6140. (org-back-to-heading nil) ; take what looks like a subtree
  6141. (org-back-to-heading t)) ; take what is really there
  6142. (org-back-over-empty-lines)
  6143. (setq beg (point))
  6144. (skip-chars-forward " \t\r\n")
  6145. (save-match-data
  6146. (save-excursion (outline-end-of-heading)
  6147. (setq folded (org-invisible-p)))
  6148. (condition-case nil
  6149. (org-forward-same-level (1- n) t)
  6150. (error nil))
  6151. (org-end-of-subtree t t))
  6152. (org-back-over-empty-lines)
  6153. (setq end (point))
  6154. (goto-char beg0)
  6155. (when (> end beg)
  6156. (setq org-subtree-clip-folded folded)
  6157. (when (or cut force-store-markers)
  6158. (org-save-markers-in-region beg end))
  6159. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6160. (setq org-subtree-clip (current-kill 0))
  6161. (message "%s: Subtree(s) with %d characters"
  6162. (if cut "Cut" "Copied")
  6163. (length org-subtree-clip)))))
  6164. (defun org-paste-subtree (&optional level tree for-yank)
  6165. "Paste the clipboard as a subtree, with modification of headline level.
  6166. The entire subtree is promoted or demoted in order to match a new headline
  6167. level.
  6168. If the cursor is at the beginning of a headline, the same level as
  6169. that headline is used to paste the tree
  6170. If not, the new level is derived from the *visible* headings
  6171. before and after the insertion point, and taken to be the inferior headline
  6172. level of the two. So if the previous visible heading is level 3 and the
  6173. next is level 4 (or vice versa), level 4 will be used for insertion.
  6174. This makes sure that the subtree remains an independent subtree and does
  6175. not swallow low level entries.
  6176. You can also force a different level, either by using a numeric prefix
  6177. argument, or by inserting the heading marker by hand. For example, if the
  6178. cursor is after \"*****\", then the tree will be shifted to level 5.
  6179. If optional TREE is given, use this text instead of the kill ring.
  6180. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6181. move back over whitespace before inserting, and move point to the end of
  6182. the inserted text when done."
  6183. (interactive "P")
  6184. (setq tree (or tree (and kill-ring (current-kill 0))))
  6185. (unless (org-kill-is-subtree-p tree)
  6186. (error "%s"
  6187. (substitute-command-keys
  6188. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6189. (let* ((visp (not (org-invisible-p)))
  6190. (txt tree)
  6191. (^re (concat "^\\(" outline-regexp "\\)"))
  6192. (re (concat "\\(" outline-regexp "\\)"))
  6193. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6194. (old-level (if (string-match ^re txt)
  6195. (- (match-end 0) (match-beginning 0) 1)
  6196. -1))
  6197. (force-level (cond (level (prefix-numeric-value level))
  6198. ((and (looking-at "[ \t]*$")
  6199. (string-match
  6200. ^re_ (buffer-substring
  6201. (point-at-bol) (point))))
  6202. (- (match-end 1) (match-beginning 1)))
  6203. ((and (bolp)
  6204. (looking-at org-outline-regexp))
  6205. (- (match-end 0) (point) 1))
  6206. (t nil)))
  6207. (previous-level (save-excursion
  6208. (condition-case nil
  6209. (progn
  6210. (outline-previous-visible-heading 1)
  6211. (if (looking-at re)
  6212. (- (match-end 0) (match-beginning 0) 1)
  6213. 1))
  6214. (error 1))))
  6215. (next-level (save-excursion
  6216. (condition-case nil
  6217. (progn
  6218. (or (looking-at outline-regexp)
  6219. (outline-next-visible-heading 1))
  6220. (if (looking-at re)
  6221. (- (match-end 0) (match-beginning 0) 1)
  6222. 1))
  6223. (error 1))))
  6224. (new-level (or force-level (max previous-level next-level)))
  6225. (shift (if (or (= old-level -1)
  6226. (= new-level -1)
  6227. (= old-level new-level))
  6228. 0
  6229. (- new-level old-level)))
  6230. (delta (if (> shift 0) -1 1))
  6231. (func (if (> shift 0) 'org-demote 'org-promote))
  6232. (org-odd-levels-only nil)
  6233. beg end newend)
  6234. ;; Remove the forced level indicator
  6235. (if force-level
  6236. (delete-region (point-at-bol) (point)))
  6237. ;; Paste
  6238. (beginning-of-line 1)
  6239. (unless for-yank (org-back-over-empty-lines))
  6240. (setq beg (point))
  6241. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6242. (insert-before-markers txt)
  6243. (unless (string-match "\n\\'" txt) (insert "\n"))
  6244. (setq newend (point))
  6245. (org-reinstall-markers-in-region beg)
  6246. (setq end (point))
  6247. (goto-char beg)
  6248. (skip-chars-forward " \t\n\r")
  6249. (setq beg (point))
  6250. (if (and (org-invisible-p) visp)
  6251. (save-excursion (outline-show-heading)))
  6252. ;; Shift if necessary
  6253. (unless (= shift 0)
  6254. (save-restriction
  6255. (narrow-to-region beg end)
  6256. (while (not (= shift 0))
  6257. (org-map-region func (point-min) (point-max))
  6258. (setq shift (+ delta shift)))
  6259. (goto-char (point-min))
  6260. (setq newend (point-max))))
  6261. (when (or (interactive-p) for-yank)
  6262. (message "Clipboard pasted as level %d subtree" new-level))
  6263. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6264. kill-ring
  6265. (eq org-subtree-clip (current-kill 0))
  6266. org-subtree-clip-folded)
  6267. ;; The tree was folded before it was killed/copied
  6268. (hide-subtree))
  6269. (and for-yank (goto-char newend))))
  6270. (defun org-kill-is-subtree-p (&optional txt)
  6271. "Check if the current kill is an outline subtree, or a set of trees.
  6272. Returns nil if kill does not start with a headline, or if the first
  6273. headline level is not the largest headline level in the tree.
  6274. So this will actually accept several entries of equal levels as well,
  6275. which is OK for `org-paste-subtree'.
  6276. If optional TXT is given, check this string instead of the current kill."
  6277. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6278. (start-level (and kill
  6279. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6280. org-outline-regexp "\\)")
  6281. kill)
  6282. (- (match-end 2) (match-beginning 2) 1)))
  6283. (re (concat "^" org-outline-regexp))
  6284. (start (1+ (or (match-beginning 2) -1))))
  6285. (if (not start-level)
  6286. (progn
  6287. nil) ;; does not even start with a heading
  6288. (catch 'exit
  6289. (while (setq start (string-match re kill (1+ start)))
  6290. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6291. (throw 'exit nil)))
  6292. t))))
  6293. (defvar org-markers-to-move nil
  6294. "Markers that should be moved with a cut-and-paste operation.
  6295. Those markers are stored together with their positions relative to
  6296. the start of the region.")
  6297. (defun org-save-markers-in-region (beg end)
  6298. "Check markers in region.
  6299. If these markers are between BEG and END, record their position relative
  6300. to BEG, so that after moving the block of text, we can put the markers back
  6301. into place.
  6302. This function gets called just before an entry or tree gets cut from the
  6303. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6304. called immediately, to move the markers with the entries."
  6305. (setq org-markers-to-move nil)
  6306. (when (featurep 'org-clock)
  6307. (org-clock-save-markers-for-cut-and-paste beg end))
  6308. (when (featurep 'org-agenda)
  6309. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6310. (defun org-check-and-save-marker (marker beg end)
  6311. "Check if MARKER is between BEG and END.
  6312. If yes, remember the marker and the distance to BEG."
  6313. (when (and (marker-buffer marker)
  6314. (equal (marker-buffer marker) (current-buffer)))
  6315. (if (and (>= marker beg) (< marker end))
  6316. (push (cons marker (- marker beg)) org-markers-to-move))))
  6317. (defun org-reinstall-markers-in-region (beg)
  6318. "Move all remembered markers to their position relative to BEG."
  6319. (mapc (lambda (x)
  6320. (move-marker (car x) (+ beg (cdr x))))
  6321. org-markers-to-move)
  6322. (setq org-markers-to-move nil))
  6323. (defun org-narrow-to-subtree ()
  6324. "Narrow buffer to the current subtree."
  6325. (interactive)
  6326. (save-excursion
  6327. (save-match-data
  6328. (narrow-to-region
  6329. (progn (org-back-to-heading t) (point))
  6330. (progn (org-end-of-subtree t t)
  6331. (if (org-on-heading-p) (backward-char 1))
  6332. (point))))))
  6333. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6334. "Clone the task (subtree) at point N times.
  6335. The clones will be inserted as siblings.
  6336. In interactive use, the user will be prompted for the number of clones
  6337. to be produced, and for a time SHIFT, which may be a repeater as used
  6338. in time stamps, for example `+3d'.
  6339. When a valid repeater is given and the entry contains any time stamps,
  6340. the clones will become a sequence in time, with time stamps in the
  6341. subtree shifted for each clone produced. If SHIFT is nil or the
  6342. empty string, time stamps will be left alone.
  6343. If the original subtree did contain time stamps with a repeater,
  6344. the following will happen:
  6345. - the repeater will be removed in each clone
  6346. - an additional clone will be produced, with the current, unshifted
  6347. date(s) in the entry.
  6348. - the original entry will be placed *after* all the clones, with
  6349. repeater intact.
  6350. - the start days in the repeater in the original entry will be shifted
  6351. to past the last clone.
  6352. I this way you can spell out a number of instances of a repeating task,
  6353. and still retain the repeater to cover future instances of the task."
  6354. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6355. (let (beg end template task
  6356. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6357. (if (not (and (integerp n) (> n 0)))
  6358. (error "Invalid number of replications %s" n))
  6359. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6360. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6361. shift)))
  6362. (error "Invalid shift specification %s" shift))
  6363. (when doshift
  6364. (setq shift-n (string-to-number (match-string 1 shift))
  6365. shift-what (cdr (assoc (match-string 2 shift)
  6366. '(("d" . day) ("w" . week)
  6367. ("m" . month) ("y" . year))))))
  6368. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6369. (setq nmin 1 nmax n)
  6370. (org-back-to-heading t)
  6371. (setq beg (point))
  6372. (org-end-of-subtree t t)
  6373. (or (bolp) (insert "\n"))
  6374. (setq end (point))
  6375. (setq template (buffer-substring beg end))
  6376. (when (and doshift
  6377. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6378. (delete-region beg end)
  6379. (setq end beg)
  6380. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6381. (goto-char end)
  6382. (loop for n from nmin to nmax do
  6383. (if (not doshift)
  6384. (setq task template)
  6385. (with-temp-buffer
  6386. (insert template)
  6387. (org-mode)
  6388. (goto-char (point-min))
  6389. (while (re-search-forward org-ts-regexp-both nil t)
  6390. (org-timestamp-change (* n shift-n) shift-what))
  6391. (unless (= n n-no-remove)
  6392. (goto-char (point-min))
  6393. (while (re-search-forward org-ts-regexp nil t)
  6394. (save-excursion
  6395. (goto-char (match-beginning 0))
  6396. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6397. (delete-region (match-beginning 1) (match-end 1))))))
  6398. (setq task (buffer-string))))
  6399. (insert task))
  6400. (goto-char beg)))
  6401. ;;; Outline Sorting
  6402. (defun org-sort (with-case)
  6403. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6404. Optional argument WITH-CASE means sort case-sensitively.
  6405. With a double prefix argument, also remove duplicate entries."
  6406. (interactive "P")
  6407. (if (org-at-table-p)
  6408. (org-call-with-arg 'org-table-sort-lines with-case)
  6409. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6410. (defun org-sort-remove-invisible (s)
  6411. (remove-text-properties 0 (length s) org-rm-props s)
  6412. (while (string-match org-bracket-link-regexp s)
  6413. (setq s (replace-match (if (match-end 2)
  6414. (match-string 3 s)
  6415. (match-string 1 s)) t t s)))
  6416. s)
  6417. (defvar org-priority-regexp) ; defined later in the file
  6418. (defvar org-after-sorting-entries-or-items-hook nil
  6419. "Hook that is run after a bunch of entries or items have been sorted.
  6420. When children are sorted, the cursor is in the parent line when this
  6421. hook gets called. When a region or a plain list is sorted, the cursor
  6422. will be in the first entry of the sorted region/list.")
  6423. (defun org-sort-entries-or-items
  6424. (&optional with-case sorting-type getkey-func compare-func property)
  6425. "Sort entries on a certain level of an outline tree, or plain list items.
  6426. If there is an active region, the entries in the region are sorted.
  6427. Else, if the cursor is before the first entry, sort the top-level items.
  6428. Else, the children of the entry at point are sorted.
  6429. If the cursor is at the first item in a plain list, the list items will be
  6430. sorted.
  6431. Sorting can be alphabetically, numerically, by date/time as given by
  6432. a time stamp, by a property or by priority.
  6433. The command prompts for the sorting type unless it has been given to the
  6434. function through the SORTING-TYPE argument, which needs to be a character,
  6435. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6436. precise meaning of each character:
  6437. n Numerically, by converting the beginning of the entry/item to a number.
  6438. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6439. t By date/time, either the first active time stamp in the entry, or, if
  6440. none exist, by the first inactive one.
  6441. In items, only the first line will be checked.
  6442. s By the scheduled date/time.
  6443. d By deadline date/time.
  6444. c By creation time, which is assumed to be the first inactive time stamp
  6445. at the beginning of a line.
  6446. p By priority according to the cookie.
  6447. r By the value of a property.
  6448. Capital letters will reverse the sort order.
  6449. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6450. called with point at the beginning of the record. It must return either
  6451. a string or a number that should serve as the sorting key for that record.
  6452. Comparing entries ignores case by default. However, with an optional argument
  6453. WITH-CASE, the sorting considers case as well."
  6454. (interactive "P")
  6455. (let ((case-func (if with-case 'identity 'downcase))
  6456. start beg end stars re re2
  6457. txt what tmp plain-list-p)
  6458. ;; Find beginning and end of region to sort
  6459. (cond
  6460. ((org-region-active-p)
  6461. ;; we will sort the region
  6462. (setq end (region-end)
  6463. what "region")
  6464. (goto-char (region-beginning))
  6465. (if (not (org-on-heading-p)) (outline-next-heading))
  6466. (setq start (point)))
  6467. ((org-at-item-p)
  6468. ;; we will sort this plain list
  6469. (org-beginning-of-item-list) (setq start (point))
  6470. (org-end-of-item-list)
  6471. (or (bolp) (insert "\n"))
  6472. (setq end (point))
  6473. (goto-char start)
  6474. (setq plain-list-p t
  6475. what "plain list"))
  6476. ((or (org-on-heading-p)
  6477. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6478. ;; we will sort the children of the current headline
  6479. (org-back-to-heading)
  6480. (setq start (point)
  6481. end (progn (org-end-of-subtree t t)
  6482. (or (bolp) (insert "\n"))
  6483. (org-back-over-empty-lines)
  6484. (point))
  6485. what "children")
  6486. (goto-char start)
  6487. (show-subtree)
  6488. (outline-next-heading))
  6489. (t
  6490. ;; we will sort the top-level entries in this file
  6491. (goto-char (point-min))
  6492. (or (org-on-heading-p) (outline-next-heading))
  6493. (setq start (point))
  6494. (goto-char (point-max))
  6495. (beginning-of-line 1)
  6496. (when (looking-at ".*?\\S-")
  6497. ;; File ends in a non-white line
  6498. (end-of-line 1)
  6499. (insert "\n"))
  6500. (setq end (point-max))
  6501. (setq what "top-level")
  6502. (goto-char start)
  6503. (show-all)))
  6504. (setq beg (point))
  6505. (if (>= beg end) (error "Nothing to sort"))
  6506. (unless plain-list-p
  6507. (looking-at "\\(\\*+\\)")
  6508. (setq stars (match-string 1)
  6509. re (concat "^" (regexp-quote stars) " +")
  6510. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6511. txt (buffer-substring beg end))
  6512. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6513. (if (and (not (equal stars "*")) (string-match re2 txt))
  6514. (error "Region to sort contains a level above the first entry")))
  6515. (unless sorting-type
  6516. (message
  6517. (if plain-list-p
  6518. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6519. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6520. [t]ime [s]cheduled [d]eadline [c]reated
  6521. A/N/T/S/D/C/P/O/F means reversed:")
  6522. what)
  6523. (setq sorting-type (read-char-exclusive))
  6524. (and (= (downcase sorting-type) ?f)
  6525. (setq getkey-func
  6526. (org-icompleting-read "Sort using function: "
  6527. obarray 'fboundp t nil nil))
  6528. (setq getkey-func (intern getkey-func)))
  6529. (and (= (downcase sorting-type) ?r)
  6530. (setq property
  6531. (org-icompleting-read "Property: "
  6532. (mapcar 'list (org-buffer-property-keys t))
  6533. nil t))))
  6534. (message "Sorting entries...")
  6535. (save-restriction
  6536. (narrow-to-region start end)
  6537. (let ((dcst (downcase sorting-type))
  6538. (case-fold-search nil)
  6539. (now (current-time)))
  6540. (sort-subr
  6541. (/= dcst sorting-type)
  6542. ;; This function moves to the beginning character of the "record" to
  6543. ;; be sorted.
  6544. (if plain-list-p
  6545. (lambda nil
  6546. (if (org-at-item-p) t (goto-char (point-max))))
  6547. (lambda nil
  6548. (if (re-search-forward re nil t)
  6549. (goto-char (match-beginning 0))
  6550. (goto-char (point-max)))))
  6551. ;; This function moves to the last character of the "record" being
  6552. ;; sorted.
  6553. (if plain-list-p
  6554. 'org-end-of-item
  6555. (lambda nil
  6556. (save-match-data
  6557. (condition-case nil
  6558. (outline-forward-same-level 1)
  6559. (error
  6560. (goto-char (point-max)))))))
  6561. ;; This function returns the value that gets sorted against.
  6562. (if plain-list-p
  6563. (lambda nil
  6564. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6565. (cond
  6566. ((= dcst ?n)
  6567. (string-to-number (buffer-substring (match-end 0)
  6568. (point-at-eol))))
  6569. ((= dcst ?a)
  6570. (buffer-substring (match-end 0) (point-at-eol)))
  6571. ((= dcst ?t)
  6572. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6573. (re-search-forward org-ts-regexp-both
  6574. (point-at-eol) t))
  6575. (org-time-string-to-seconds (match-string 0))
  6576. (org-float-time now)))
  6577. ((= dcst ?f)
  6578. (if getkey-func
  6579. (progn
  6580. (setq tmp (funcall getkey-func))
  6581. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6582. tmp)
  6583. (error "Invalid key function `%s'" getkey-func)))
  6584. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6585. (lambda nil
  6586. (cond
  6587. ((= dcst ?n)
  6588. (if (looking-at org-complex-heading-regexp)
  6589. (string-to-number (match-string 4))
  6590. nil))
  6591. ((= dcst ?a)
  6592. (if (looking-at org-complex-heading-regexp)
  6593. (funcall case-func (match-string 4))
  6594. nil))
  6595. ((= dcst ?t)
  6596. (let ((end (save-excursion (outline-next-heading) (point))))
  6597. (if (or (re-search-forward org-ts-regexp end t)
  6598. (re-search-forward org-ts-regexp-both end t))
  6599. (org-time-string-to-seconds (match-string 0))
  6600. (org-float-time now))))
  6601. ((= dcst ?c)
  6602. (let ((end (save-excursion (outline-next-heading) (point))))
  6603. (if (re-search-forward
  6604. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6605. end t)
  6606. (org-time-string-to-seconds (match-string 0))
  6607. (org-float-time now))))
  6608. ((= dcst ?s)
  6609. (let ((end (save-excursion (outline-next-heading) (point))))
  6610. (if (re-search-forward org-scheduled-time-regexp end t)
  6611. (org-time-string-to-seconds (match-string 1))
  6612. (org-float-time now))))
  6613. ((= dcst ?d)
  6614. (let ((end (save-excursion (outline-next-heading) (point))))
  6615. (if (re-search-forward org-deadline-time-regexp end t)
  6616. (org-time-string-to-seconds (match-string 1))
  6617. (org-float-time now))))
  6618. ((= dcst ?p)
  6619. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6620. (string-to-char (match-string 2))
  6621. org-default-priority))
  6622. ((= dcst ?r)
  6623. (or (org-entry-get nil property) ""))
  6624. ((= dcst ?o)
  6625. (if (looking-at org-complex-heading-regexp)
  6626. (- 9999 (length (member (match-string 2)
  6627. org-todo-keywords-1)))))
  6628. ((= dcst ?f)
  6629. (if getkey-func
  6630. (progn
  6631. (setq tmp (funcall getkey-func))
  6632. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6633. tmp)
  6634. (error "Invalid key function `%s'" getkey-func)))
  6635. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6636. nil
  6637. (cond
  6638. ((= dcst ?a) 'string<)
  6639. ((= dcst ?f) compare-func)
  6640. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6641. (t nil)))))
  6642. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6643. (message "Sorting entries...done")))
  6644. (defun org-do-sort (table what &optional with-case sorting-type)
  6645. "Sort TABLE of WHAT according to SORTING-TYPE.
  6646. The user will be prompted for the SORTING-TYPE if the call to this
  6647. function does not specify it. WHAT is only for the prompt, to indicate
  6648. what is being sorted. The sorting key will be extracted from
  6649. the car of the elements of the table.
  6650. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6651. (unless sorting-type
  6652. (message
  6653. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6654. what)
  6655. (setq sorting-type (read-char-exclusive)))
  6656. (let ((dcst (downcase sorting-type))
  6657. extractfun comparefun)
  6658. ;; Define the appropriate functions
  6659. (cond
  6660. ((= dcst ?n)
  6661. (setq extractfun 'string-to-number
  6662. comparefun (if (= dcst sorting-type) '< '>)))
  6663. ((= dcst ?a)
  6664. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6665. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6666. comparefun (if (= dcst sorting-type)
  6667. 'string<
  6668. (lambda (a b) (and (not (string< a b))
  6669. (not (string= a b)))))))
  6670. ((= dcst ?t)
  6671. (setq extractfun
  6672. (lambda (x)
  6673. (if (or (string-match org-ts-regexp x)
  6674. (string-match org-ts-regexp-both x))
  6675. (org-float-time
  6676. (org-time-string-to-time (match-string 0 x)))
  6677. 0))
  6678. comparefun (if (= dcst sorting-type) '< '>)))
  6679. (t (error "Invalid sorting type `%c'" sorting-type)))
  6680. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6681. table)
  6682. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6683. ;;; The orgstruct minor mode
  6684. ;; Define a minor mode which can be used in other modes in order to
  6685. ;; integrate the org-mode structure editing commands.
  6686. ;; This is really a hack, because the org-mode structure commands use
  6687. ;; keys which normally belong to the major mode. Here is how it
  6688. ;; works: The minor mode defines all the keys necessary to operate the
  6689. ;; structure commands, but wraps the commands into a function which
  6690. ;; tests if the cursor is currently at a headline or a plain list
  6691. ;; item. If that is the case, the structure command is used,
  6692. ;; temporarily setting many Org-mode variables like regular
  6693. ;; expressions for filling etc. However, when any of those keys is
  6694. ;; used at a different location, function uses `key-binding' to look
  6695. ;; up if the key has an associated command in another currently active
  6696. ;; keymap (minor modes, major mode, global), and executes that
  6697. ;; command. There might be problems if any of the keys is otherwise
  6698. ;; used as a prefix key.
  6699. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6700. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6701. ;; addresses this by checking explicitly for both bindings.
  6702. (defvar orgstruct-mode-map (make-sparse-keymap)
  6703. "Keymap for the minor `orgstruct-mode'.")
  6704. (defvar org-local-vars nil
  6705. "List of local variables, for use by `orgstruct-mode'")
  6706. ;;;###autoload
  6707. (define-minor-mode orgstruct-mode
  6708. "Toggle the minor mode `orgstruct-mode'.
  6709. This mode is for using Org-mode structure commands in other
  6710. modes. The following keys behave as if Org-mode were active, if
  6711. the cursor is on a headline, or on a plain list item (both as
  6712. defined by Org-mode).
  6713. M-up Move entry/item up
  6714. M-down Move entry/item down
  6715. M-left Promote
  6716. M-right Demote
  6717. M-S-up Move entry/item up
  6718. M-S-down Move entry/item down
  6719. M-S-left Promote subtree
  6720. M-S-right Demote subtree
  6721. M-q Fill paragraph and items like in Org-mode
  6722. C-c ^ Sort entries
  6723. C-c - Cycle list bullet
  6724. TAB Cycle item visibility
  6725. M-RET Insert new heading/item
  6726. S-M-RET Insert new TODO heading / Checkbox item
  6727. C-c C-c Set tags / toggle checkbox"
  6728. nil " OrgStruct" nil
  6729. (org-load-modules-maybe)
  6730. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6731. ;;;###autoload
  6732. (defun turn-on-orgstruct ()
  6733. "Unconditionally turn on `orgstruct-mode'."
  6734. (orgstruct-mode 1))
  6735. (defun orgstruct++-mode (&optional arg)
  6736. "Toggle `orgstruct-mode', the enhanced version of it.
  6737. In addition to setting orgstruct-mode, this also exports all indentation
  6738. and autofilling variables from org-mode into the buffer. It will also
  6739. recognize item context in multiline items.
  6740. Note that turning off orgstruct-mode will *not* remove the
  6741. indentation/paragraph settings. This can only be done by refreshing the
  6742. major mode, for example with \\[normal-mode]."
  6743. (interactive "P")
  6744. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6745. (if (< arg 1)
  6746. (orgstruct-mode -1)
  6747. (orgstruct-mode 1)
  6748. (let (var val)
  6749. (mapc
  6750. (lambda (x)
  6751. (when (string-match
  6752. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6753. (symbol-name (car x)))
  6754. (setq var (car x) val (nth 1 x))
  6755. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6756. org-local-vars)
  6757. (org-set-local 'orgstruct-is-++ t))))
  6758. (defvar orgstruct-is-++ nil
  6759. "Is orgstruct-mode in ++ version in the current-buffer?")
  6760. (make-variable-buffer-local 'orgstruct-is-++)
  6761. ;;;###autoload
  6762. (defun turn-on-orgstruct++ ()
  6763. "Unconditionally turn on `orgstruct++-mode'."
  6764. (orgstruct++-mode 1))
  6765. (defun orgstruct-error ()
  6766. "Error when there is no default binding for a structure key."
  6767. (interactive)
  6768. (error "This key has no function outside structure elements"))
  6769. (defun orgstruct-setup ()
  6770. "Setup orgstruct keymaps."
  6771. (let ((nfunc 0)
  6772. (bindings
  6773. (list
  6774. '([(meta up)] org-metaup)
  6775. '([(meta down)] org-metadown)
  6776. '([(meta left)] org-metaleft)
  6777. '([(meta right)] org-metaright)
  6778. '([(meta shift up)] org-shiftmetaup)
  6779. '([(meta shift down)] org-shiftmetadown)
  6780. '([(meta shift left)] org-shiftmetaleft)
  6781. '([(meta shift right)] org-shiftmetaright)
  6782. '([?\e (up)] org-metaup)
  6783. '([?\e (down)] org-metadown)
  6784. '([?\e (left)] org-metaleft)
  6785. '([?\e (right)] org-metaright)
  6786. '([?\e (shift up)] org-shiftmetaup)
  6787. '([?\e (shift down)] org-shiftmetadown)
  6788. '([?\e (shift left)] org-shiftmetaleft)
  6789. '([?\e (shift right)] org-shiftmetaright)
  6790. '([(shift up)] org-shiftup)
  6791. '([(shift down)] org-shiftdown)
  6792. '([(shift left)] org-shiftleft)
  6793. '([(shift right)] org-shiftright)
  6794. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6795. '("\M-q" fill-paragraph)
  6796. '("\C-c^" org-sort)
  6797. '("\C-c-" org-cycle-list-bullet)))
  6798. elt key fun cmd)
  6799. (while (setq elt (pop bindings))
  6800. (setq nfunc (1+ nfunc))
  6801. (setq key (org-key (car elt))
  6802. fun (nth 1 elt)
  6803. cmd (orgstruct-make-binding fun nfunc key))
  6804. (org-defkey orgstruct-mode-map key cmd))
  6805. ;; Special treatment needed for TAB and RET
  6806. (org-defkey orgstruct-mode-map [(tab)]
  6807. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6808. (org-defkey orgstruct-mode-map "\C-i"
  6809. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6810. (org-defkey orgstruct-mode-map "\M-\C-m"
  6811. (orgstruct-make-binding 'org-insert-heading 105
  6812. "\M-\C-m" [(meta return)]))
  6813. (org-defkey orgstruct-mode-map [(meta return)]
  6814. (orgstruct-make-binding 'org-insert-heading 106
  6815. [(meta return)] "\M-\C-m"))
  6816. (org-defkey orgstruct-mode-map [(shift meta return)]
  6817. (orgstruct-make-binding 'org-insert-todo-heading 107
  6818. [(meta return)] "\M-\C-m"))
  6819. (org-defkey orgstruct-mode-map "\e\C-m"
  6820. (orgstruct-make-binding 'org-insert-heading 108
  6821. "\e\C-m" [?\e (return)]))
  6822. (org-defkey orgstruct-mode-map [?\e (return)]
  6823. (orgstruct-make-binding 'org-insert-heading 109
  6824. [?\e (return)] "\e\C-m"))
  6825. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6826. (orgstruct-make-binding 'org-insert-todo-heading 110
  6827. [?\e (return)] "\e\C-m"))
  6828. (unless org-local-vars
  6829. (setq org-local-vars (org-get-local-variables)))
  6830. t))
  6831. (defun orgstruct-make-binding (fun n &rest keys)
  6832. "Create a function for binding in the structure minor mode.
  6833. FUN is the command to call inside a table. N is used to create a unique
  6834. command name. KEYS are keys that should be checked in for a command
  6835. to execute outside of tables."
  6836. (eval
  6837. (list 'defun
  6838. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6839. '(arg)
  6840. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6841. "Outside of structure, run the binding of `"
  6842. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6843. "'.")
  6844. '(interactive "p")
  6845. (list 'if
  6846. `(org-context-p 'headline 'item
  6847. (and orgstruct-is-++
  6848. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6849. 'item-body))
  6850. (list 'org-run-like-in-org-mode (list 'quote fun))
  6851. (list 'let '(orgstruct-mode)
  6852. (list 'call-interactively
  6853. (append '(or)
  6854. (mapcar (lambda (k)
  6855. (list 'key-binding k))
  6856. keys)
  6857. '('orgstruct-error))))))))
  6858. (defun org-context-p (&rest contexts)
  6859. "Check if local context is any of CONTEXTS.
  6860. Possible values in the list of contexts are `table', `headline', and `item'."
  6861. (let ((pos (point)))
  6862. (goto-char (point-at-bol))
  6863. (prog1 (or (and (memq 'table contexts)
  6864. (looking-at "[ \t]*|"))
  6865. (and (memq 'headline contexts)
  6866. ;;????????? (looking-at "\\*+"))
  6867. (looking-at outline-regexp))
  6868. (and (memq 'item contexts)
  6869. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6870. (and (memq 'item-body contexts)
  6871. (org-in-item-p)))
  6872. (goto-char pos))))
  6873. (defun org-get-local-variables ()
  6874. "Return a list of all local variables in an org-mode buffer."
  6875. (let (varlist)
  6876. (with-current-buffer (get-buffer-create "*Org tmp*")
  6877. (erase-buffer)
  6878. (org-mode)
  6879. (setq varlist (buffer-local-variables)))
  6880. (kill-buffer "*Org tmp*")
  6881. (delq nil
  6882. (mapcar
  6883. (lambda (x)
  6884. (setq x
  6885. (if (symbolp x)
  6886. (list x)
  6887. (list (car x) (list 'quote (cdr x)))))
  6888. (if (string-match
  6889. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6890. (symbol-name (car x)))
  6891. x nil))
  6892. varlist))))
  6893. ;;;###autoload
  6894. (defun org-run-like-in-org-mode (cmd)
  6895. "Run a command, pretending that the current buffer is in Org-mode.
  6896. This will temporarily bind local variables that are typically bound in
  6897. Org-mode to the values they have in Org-mode, and then interactively
  6898. call CMD."
  6899. (org-load-modules-maybe)
  6900. (unless org-local-vars
  6901. (setq org-local-vars (org-get-local-variables)))
  6902. (eval (list 'let org-local-vars
  6903. (list 'call-interactively (list 'quote cmd)))))
  6904. ;;;; Archiving
  6905. (defun org-get-category (&optional pos)
  6906. "Get the category applying to position POS."
  6907. (get-text-property (or pos (point)) 'org-category))
  6908. (defun org-refresh-category-properties ()
  6909. "Refresh category text properties in the buffer."
  6910. (let ((def-cat (cond
  6911. ((null org-category)
  6912. (if buffer-file-name
  6913. (file-name-sans-extension
  6914. (file-name-nondirectory buffer-file-name))
  6915. "???"))
  6916. ((symbolp org-category) (symbol-name org-category))
  6917. (t org-category)))
  6918. beg end cat pos optionp)
  6919. (org-unmodified
  6920. (save-excursion
  6921. (save-restriction
  6922. (widen)
  6923. (goto-char (point-min))
  6924. (put-text-property (point) (point-max) 'org-category def-cat)
  6925. (while (re-search-forward
  6926. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6927. (setq pos (match-end 0)
  6928. optionp (equal (char-after (match-beginning 0)) ?#)
  6929. cat (org-trim (match-string 2)))
  6930. (if optionp
  6931. (setq beg (point-at-bol) end (point-max))
  6932. (org-back-to-heading t)
  6933. (setq beg (point) end (org-end-of-subtree t t)))
  6934. (put-text-property beg end 'org-category cat)
  6935. (goto-char pos)))))))
  6936. ;;;; Link Stuff
  6937. ;;; Link abbreviations
  6938. (defun org-link-expand-abbrev (link)
  6939. "Apply replacements as defined in `org-link-abbrev-alist."
  6940. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6941. (let* ((key (match-string 1 link))
  6942. (as (or (assoc key org-link-abbrev-alist-local)
  6943. (assoc key org-link-abbrev-alist)))
  6944. (tag (and (match-end 2) (match-string 3 link)))
  6945. rpl)
  6946. (if (not as)
  6947. link
  6948. (setq rpl (cdr as))
  6949. (cond
  6950. ((symbolp rpl) (funcall rpl tag))
  6951. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6952. ((string-match "%h" rpl)
  6953. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6954. (t (concat rpl tag)))))
  6955. link))
  6956. ;;; Storing and inserting links
  6957. (defvar org-insert-link-history nil
  6958. "Minibuffer history for links inserted with `org-insert-link'.")
  6959. (defvar org-stored-links nil
  6960. "Contains the links stored with `org-store-link'.")
  6961. (defvar org-store-link-plist nil
  6962. "Plist with info about the most recently link created with `org-store-link'.")
  6963. (defvar org-link-protocols nil
  6964. "Link protocols added to Org-mode using `org-add-link-type'.")
  6965. (defvar org-store-link-functions nil
  6966. "List of functions that are called to create and store a link.
  6967. Each function will be called in turn until one returns a non-nil
  6968. value. Each function should check if it is responsible for creating
  6969. this link (for example by looking at the major mode).
  6970. If not, it must exit and return nil.
  6971. If yes, it should return a non-nil value after a calling
  6972. `org-store-link-props' with a list of properties and values.
  6973. Special properties are:
  6974. :type The link prefix. like \"http\". This must be given.
  6975. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6976. This is obligatory as well.
  6977. :description Optional default description for the second pair
  6978. of brackets in an Org-mode link. The user can still change
  6979. this when inserting this link into an Org-mode buffer.
  6980. In addition to these, any additional properties can be specified
  6981. and then used in remember templates.")
  6982. (defun org-add-link-type (type &optional follow export)
  6983. "Add TYPE to the list of `org-link-types'.
  6984. Re-compute all regular expressions depending on `org-link-types'
  6985. FOLLOW and EXPORT are two functions.
  6986. FOLLOW should take the link path as the single argument and do whatever
  6987. is necessary to follow the link, for example find a file or display
  6988. a mail message.
  6989. EXPORT should format the link path for export to one of the export formats.
  6990. It should be a function accepting three arguments:
  6991. path the path of the link, the text after the prefix (like \"http:\")
  6992. desc the description of the link, if any, nil if there was no description
  6993. format the export format, a symbol like `html' or `latex'.
  6994. The function may use the FORMAT information to return different values
  6995. depending on the format. The return value will be put literally into
  6996. the exported file.
  6997. Org-mode has a built-in default for exporting links. If you are happy with
  6998. this default, there is no need to define an export function for the link
  6999. type. For a simple example of an export function, see `org-bbdb.el'."
  7000. (add-to-list 'org-link-types type t)
  7001. (org-make-link-regexps)
  7002. (if (assoc type org-link-protocols)
  7003. (setcdr (assoc type org-link-protocols) (list follow export))
  7004. (push (list type follow export) org-link-protocols)))
  7005. (defvar org-agenda-buffer-name)
  7006. ;;;###autoload
  7007. (defun org-store-link (arg)
  7008. "\\<org-mode-map>Store an org-link to the current location.
  7009. This link is added to `org-stored-links' and can later be inserted
  7010. into an org-buffer with \\[org-insert-link].
  7011. For some link types, a prefix arg is interpreted:
  7012. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7013. For file links, arg negates `org-context-in-file-links'."
  7014. (interactive "P")
  7015. (org-load-modules-maybe)
  7016. (setq org-store-link-plist nil) ; reset
  7017. (let ((outline-regexp (org-get-limited-outline-regexp))
  7018. link cpltxt desc description search txt custom-id)
  7019. (cond
  7020. ((run-hook-with-args-until-success 'org-store-link-functions)
  7021. (setq link (plist-get org-store-link-plist :link)
  7022. desc (or (plist-get org-store-link-plist :description) link)))
  7023. ((equal (buffer-name) "*Org Edit Src Example*")
  7024. (let (label gc)
  7025. (while (or (not label)
  7026. (save-excursion
  7027. (save-restriction
  7028. (widen)
  7029. (goto-char (point-min))
  7030. (re-search-forward
  7031. (regexp-quote (format org-coderef-label-format label))
  7032. nil t))))
  7033. (when label (message "Label exists already") (sit-for 2))
  7034. (setq label (read-string "Code line label: " label)))
  7035. (end-of-line 1)
  7036. (setq link (format org-coderef-label-format label))
  7037. (setq gc (- 79 (length link)))
  7038. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7039. (insert link)
  7040. (setq link (concat "(" label ")") desc nil)))
  7041. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7042. ;; We are in the agenda, link to referenced location
  7043. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7044. (get-text-property (point) 'org-marker))))
  7045. (when m
  7046. (org-with-point-at m
  7047. (call-interactively 'org-store-link)))))
  7048. ((eq major-mode 'calendar-mode)
  7049. (let ((cd (calendar-cursor-to-date)))
  7050. (setq link
  7051. (format-time-string
  7052. (car org-time-stamp-formats)
  7053. (apply 'encode-time
  7054. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7055. nil nil nil))))
  7056. (org-store-link-props :type "calendar" :date cd)))
  7057. ((eq major-mode 'w3-mode)
  7058. (setq cpltxt (if (and (buffer-name)
  7059. (not (string-match "Untitled" (buffer-name))))
  7060. (buffer-name)
  7061. (url-view-url t))
  7062. link (org-make-link (url-view-url t)))
  7063. (org-store-link-props :type "w3" :url (url-view-url t)))
  7064. ((eq major-mode 'w3m-mode)
  7065. (setq cpltxt (or w3m-current-title w3m-current-url)
  7066. link (org-make-link w3m-current-url))
  7067. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7068. ((setq search (run-hook-with-args-until-success
  7069. 'org-create-file-search-functions))
  7070. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7071. "::" search))
  7072. (setq cpltxt (or description link)))
  7073. ((eq major-mode 'image-mode)
  7074. (setq cpltxt (concat "file:"
  7075. (abbreviate-file-name buffer-file-name))
  7076. link (org-make-link cpltxt))
  7077. (org-store-link-props :type "image" :file buffer-file-name))
  7078. ((eq major-mode 'dired-mode)
  7079. ;; link to the file in the current line
  7080. (let ((file (dired-get-filename nil t)))
  7081. (setq file (if file
  7082. (abbreviate-file-name
  7083. (expand-file-name (dired-get-filename nil t)))
  7084. ;; otherwise, no file so use current directory.
  7085. default-directory))
  7086. (setq cpltxt (concat "file:" file)
  7087. link (org-make-link cpltxt))))
  7088. ((and buffer-file-name (org-mode-p))
  7089. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7090. (cond
  7091. ((org-in-regexp "<<\\(.*?\\)>>")
  7092. (setq cpltxt
  7093. (concat "file:"
  7094. (abbreviate-file-name buffer-file-name)
  7095. "::" (match-string 1))
  7096. link (org-make-link cpltxt)))
  7097. ((and (featurep 'org-id)
  7098. (or (eq org-link-to-org-use-id t)
  7099. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7100. (interactive-p))
  7101. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7102. (interactive-p)
  7103. (not custom-id))
  7104. (and org-link-to-org-use-id
  7105. (condition-case nil
  7106. (org-entry-get nil "ID")
  7107. (error nil)))))
  7108. ;; We can make a link using the ID.
  7109. (setq link (condition-case nil
  7110. (prog1 (org-id-store-link)
  7111. (setq desc (plist-get org-store-link-plist
  7112. :description)))
  7113. (error
  7114. ;; probably before first headline, link to file only
  7115. (concat "file:"
  7116. (abbreviate-file-name buffer-file-name))))))
  7117. (t
  7118. ;; Just link to current headline
  7119. (setq cpltxt (concat "file:"
  7120. (abbreviate-file-name buffer-file-name)))
  7121. ;; Add a context search string
  7122. (when (org-xor org-context-in-file-links arg)
  7123. (setq txt (cond
  7124. ((org-on-heading-p) nil)
  7125. ((org-region-active-p)
  7126. (buffer-substring (region-beginning) (region-end)))
  7127. (t nil)))
  7128. (when (or (null txt) (string-match "\\S-" txt))
  7129. (setq cpltxt
  7130. (concat cpltxt "::"
  7131. (condition-case nil
  7132. (org-make-org-heading-search-string txt)
  7133. (error "")))
  7134. desc (or (nth 4 (ignore-errors
  7135. (org-heading-components))) "NONE"))))
  7136. (if (string-match "::\\'" cpltxt)
  7137. (setq cpltxt (substring cpltxt 0 -2)))
  7138. (setq link (org-make-link cpltxt)))))
  7139. ((buffer-file-name (buffer-base-buffer))
  7140. ;; Just link to this file here.
  7141. (setq cpltxt (concat "file:"
  7142. (abbreviate-file-name
  7143. (buffer-file-name (buffer-base-buffer)))))
  7144. ;; Add a context string
  7145. (when (org-xor org-context-in-file-links arg)
  7146. (setq txt (if (org-region-active-p)
  7147. (buffer-substring (region-beginning) (region-end))
  7148. (buffer-substring (point-at-bol) (point-at-eol))))
  7149. ;; Only use search option if there is some text.
  7150. (when (string-match "\\S-" txt)
  7151. (setq cpltxt
  7152. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7153. desc "NONE")))
  7154. (setq link (org-make-link cpltxt)))
  7155. ((interactive-p)
  7156. (error "Cannot link to a buffer which is not visiting a file"))
  7157. (t (setq link nil)))
  7158. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7159. (setq link (or link cpltxt)
  7160. desc (or desc cpltxt))
  7161. (if (equal desc "NONE") (setq desc nil))
  7162. (if (and (or (interactive-p) executing-kbd-macro) link)
  7163. (progn
  7164. (setq org-stored-links
  7165. (cons (list link desc) org-stored-links))
  7166. (message "Stored: %s" (or desc link))
  7167. (when custom-id
  7168. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7169. "::#" custom-id))
  7170. (setq org-stored-links
  7171. (cons (list link desc) org-stored-links))))
  7172. (and link (org-make-link-string link desc)))))
  7173. (defun org-store-link-props (&rest plist)
  7174. "Store link properties, extract names and addresses."
  7175. (let (x adr)
  7176. (when (setq x (plist-get plist :from))
  7177. (setq adr (mail-extract-address-components x))
  7178. (setq plist (plist-put plist :fromname (car adr)))
  7179. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7180. (when (setq x (plist-get plist :to))
  7181. (setq adr (mail-extract-address-components x))
  7182. (setq plist (plist-put plist :toname (car adr)))
  7183. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7184. (let ((from (plist-get plist :from))
  7185. (to (plist-get plist :to)))
  7186. (when (and from to org-from-is-user-regexp)
  7187. (setq plist
  7188. (plist-put plist :fromto
  7189. (if (string-match org-from-is-user-regexp from)
  7190. (concat "to %t")
  7191. (concat "from %f"))))))
  7192. (setq org-store-link-plist plist))
  7193. (defun org-add-link-props (&rest plist)
  7194. "Add these properties to the link property list."
  7195. (let (key value)
  7196. (while plist
  7197. (setq key (pop plist) value (pop plist))
  7198. (setq org-store-link-plist
  7199. (plist-put org-store-link-plist key value)))))
  7200. (defun org-email-link-description (&optional fmt)
  7201. "Return the description part of an email link.
  7202. This takes information from `org-store-link-plist' and formats it
  7203. according to FMT (default from `org-email-link-description-format')."
  7204. (setq fmt (or fmt org-email-link-description-format))
  7205. (let* ((p org-store-link-plist)
  7206. (to (plist-get p :toaddress))
  7207. (from (plist-get p :fromaddress))
  7208. (table
  7209. (list
  7210. (cons "%c" (plist-get p :fromto))
  7211. (cons "%F" (plist-get p :from))
  7212. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7213. (cons "%T" (plist-get p :to))
  7214. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7215. (cons "%s" (plist-get p :subject))
  7216. (cons "%m" (plist-get p :message-id)))))
  7217. (when (string-match "%c" fmt)
  7218. ;; Check if the user wrote this message
  7219. (if (and org-from-is-user-regexp from to
  7220. (save-match-data (string-match org-from-is-user-regexp from)))
  7221. (setq fmt (replace-match "to %t" t t fmt))
  7222. (setq fmt (replace-match "from %f" t t fmt))))
  7223. (org-replace-escapes fmt table)))
  7224. (defun org-make-org-heading-search-string (&optional string heading)
  7225. "Make search string for STRING or current headline."
  7226. (interactive)
  7227. (let ((s (or string (org-get-heading))))
  7228. (unless (and string (not heading))
  7229. ;; We are using a headline, clean up garbage in there.
  7230. (if (string-match org-todo-regexp s)
  7231. (setq s (replace-match "" t t s)))
  7232. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7233. (setq s (replace-match "" t t s)))
  7234. (setq s (org-trim s))
  7235. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7236. org-comment-string "\\)") s)
  7237. (setq s (replace-match "" t t s)))
  7238. (while (string-match org-ts-regexp s)
  7239. (setq s (replace-match "" t t s))))
  7240. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7241. (setq s (replace-match " " t t s)))
  7242. (or string (setq s (concat "*" s))) ; Add * for headlines
  7243. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7244. (defun org-make-link (&rest strings)
  7245. "Concatenate STRINGS."
  7246. (apply 'concat strings))
  7247. (defun org-make-link-string (link &optional description)
  7248. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7249. (unless (string-match "\\S-" link)
  7250. (error "Empty link"))
  7251. (when (and description
  7252. (stringp description)
  7253. (not (string-match "\\S-" description)))
  7254. (setq description nil))
  7255. (when (stringp description)
  7256. ;; Remove brackets from the description, they are fatal.
  7257. (while (string-match "\\[" description)
  7258. (setq description (replace-match "{" t t description)))
  7259. (while (string-match "\\]" description)
  7260. (setq description (replace-match "}" t t description))))
  7261. (when (equal (org-link-escape link) description)
  7262. ;; No description needed, it is identical
  7263. (setq description nil))
  7264. (when (and (not description)
  7265. (not (equal link (org-link-escape link))))
  7266. (setq description (org-extract-attributes link)))
  7267. (concat "[[" (org-link-escape link) "]"
  7268. (if description (concat "[" description "]") "")
  7269. "]"))
  7270. (defconst org-link-escape-chars
  7271. '((?\ . "%20")
  7272. (?\[ . "%5B")
  7273. (?\] . "%5D")
  7274. (?\340 . "%E0") ; `a
  7275. (?\342 . "%E2") ; ^a
  7276. (?\347 . "%E7") ; ,c
  7277. (?\350 . "%E8") ; `e
  7278. (?\351 . "%E9") ; 'e
  7279. (?\352 . "%EA") ; ^e
  7280. (?\356 . "%EE") ; ^i
  7281. (?\364 . "%F4") ; ^o
  7282. (?\371 . "%F9") ; `u
  7283. (?\373 . "%FB") ; ^u
  7284. (?\; . "%3B")
  7285. ;; (?? . "%3F")
  7286. (?= . "%3D")
  7287. (?+ . "%2B")
  7288. )
  7289. "Association list of escapes for some characters problematic in links.
  7290. This is the list that is used for internal purposes.")
  7291. (defvar org-url-encoding-use-url-hexify nil)
  7292. (defconst org-link-escape-chars-browser
  7293. '((?\ . "%20")) ; 32 for the SPC char
  7294. "Association list of escapes for some characters problematic in links.
  7295. This is the list that is used before handing over to the browser.")
  7296. (defun org-link-escape (text &optional table)
  7297. "Escape characters in TEXT that are problematic for links."
  7298. (if (and org-url-encoding-use-url-hexify (not table))
  7299. (url-hexify-string text)
  7300. (setq table (or table org-link-escape-chars))
  7301. (when text
  7302. (let ((re (mapconcat (lambda (x) (regexp-quote
  7303. (char-to-string (car x))))
  7304. table "\\|")))
  7305. (while (string-match re text)
  7306. (setq text
  7307. (replace-match
  7308. (cdr (assoc (string-to-char (match-string 0 text))
  7309. table))
  7310. t t text)))
  7311. text))))
  7312. (defun org-link-unescape (text &optional table)
  7313. "Reverse the action of `org-link-escape'."
  7314. (if (and org-url-encoding-use-url-hexify (not table))
  7315. (url-unhex-string text)
  7316. (setq table (or table org-link-escape-chars))
  7317. (when text
  7318. (let ((case-fold-search t)
  7319. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7320. table "\\|")))
  7321. (while (string-match re text)
  7322. (setq text
  7323. (replace-match
  7324. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7325. table)))
  7326. t t text)))
  7327. text))))
  7328. (defun org-xor (a b)
  7329. "Exclusive or."
  7330. (if a (not b) b))
  7331. (defun org-fixup-message-id-for-http (s)
  7332. "Replace special characters in a message id, so it can be used in an http query."
  7333. (while (string-match "<" s)
  7334. (setq s (replace-match "%3C" t t s)))
  7335. (while (string-match ">" s)
  7336. (setq s (replace-match "%3E" t t s)))
  7337. (while (string-match "@" s)
  7338. (setq s (replace-match "%40" t t s)))
  7339. s)
  7340. ;;;###autoload
  7341. (defun org-insert-link-global ()
  7342. "Insert a link like Org-mode does.
  7343. This command can be called in any mode to insert a link in Org-mode syntax."
  7344. (interactive)
  7345. (org-load-modules-maybe)
  7346. (org-run-like-in-org-mode 'org-insert-link))
  7347. (defun org-insert-link (&optional complete-file link-location)
  7348. "Insert a link. At the prompt, enter the link.
  7349. Completion can be used to insert any of the link protocol prefixes like
  7350. http or ftp in use.
  7351. The history can be used to select a link previously stored with
  7352. `org-store-link'. When the empty string is entered (i.e. if you just
  7353. press RET at the prompt), the link defaults to the most recently
  7354. stored link. As SPC triggers completion in the minibuffer, you need to
  7355. use M-SPC or C-q SPC to force the insertion of a space character.
  7356. You will also be prompted for a description, and if one is given, it will
  7357. be displayed in the buffer instead of the link.
  7358. If there is already a link at point, this command will allow you to edit link
  7359. and description parts.
  7360. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7361. be selected using completion. The path to the file will be relative to the
  7362. current directory if the file is in the current directory or a subdirectory.
  7363. Otherwise, the link will be the absolute path as completed in the minibuffer
  7364. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7365. option `org-link-file-path-type'.
  7366. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7367. the current directory or below.
  7368. With three \\[universal-argument] prefixes, negate the meaning of
  7369. `org-keep-stored-link-after-insertion'.
  7370. If `org-make-link-description-function' is non-nil, this function will be
  7371. called with the link target, and the result will be the default
  7372. link description.
  7373. If the LINK-LOCATION parameter is non-nil, this value will be
  7374. used as the link location instead of reading one interactively."
  7375. (interactive "P")
  7376. (let* ((wcf (current-window-configuration))
  7377. (region (if (org-region-active-p)
  7378. (buffer-substring (region-beginning) (region-end))))
  7379. (remove (and region (list (region-beginning) (region-end))))
  7380. (desc region)
  7381. tmphist ; byte-compile incorrectly complains about this
  7382. (link link-location)
  7383. entry file all-prefixes)
  7384. (cond
  7385. (link-location) ; specified by arg, just use it.
  7386. ((org-in-regexp org-bracket-link-regexp 1)
  7387. ;; We do have a link at point, and we are going to edit it.
  7388. (setq remove (list (match-beginning 0) (match-end 0)))
  7389. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7390. (setq link (read-string "Link: "
  7391. (org-link-unescape
  7392. (org-match-string-no-properties 1)))))
  7393. ((or (org-in-regexp org-angle-link-re)
  7394. (org-in-regexp org-plain-link-re))
  7395. ;; Convert to bracket link
  7396. (setq remove (list (match-beginning 0) (match-end 0))
  7397. link (read-string "Link: "
  7398. (org-remove-angle-brackets (match-string 0)))))
  7399. ((member complete-file '((4) (16)))
  7400. ;; Completing read for file names.
  7401. (setq link (org-file-complete-link complete-file)))
  7402. (t
  7403. ;; Read link, with completion for stored links.
  7404. (with-output-to-temp-buffer "*Org Links*"
  7405. (princ "Insert a link.
  7406. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7407. (when org-stored-links
  7408. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7409. (princ (mapconcat
  7410. (lambda (x)
  7411. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7412. (reverse org-stored-links) "\n"))))
  7413. (let ((cw (selected-window)))
  7414. (select-window (get-buffer-window "*Org Links*" 'visible))
  7415. (setq truncate-lines t)
  7416. (unless (pos-visible-in-window-p (point-max))
  7417. (org-fit-window-to-buffer))
  7418. (and (window-live-p cw) (select-window cw)))
  7419. ;; Fake a link history, containing the stored links.
  7420. (setq tmphist (append (mapcar 'car org-stored-links)
  7421. org-insert-link-history))
  7422. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7423. (mapcar 'car org-link-abbrev-alist)
  7424. org-link-types))
  7425. (unwind-protect
  7426. (progn
  7427. (setq link
  7428. (let ((org-completion-use-ido nil)
  7429. (org-completion-use-iswitchb nil))
  7430. (org-completing-read
  7431. "Link: "
  7432. (append
  7433. (mapcar (lambda (x) (list (concat x ":")))
  7434. all-prefixes)
  7435. (mapcar 'car org-stored-links))
  7436. nil nil nil
  7437. 'tmphist
  7438. (car (car org-stored-links)))))
  7439. (if (not (string-match "\\S-" link))
  7440. (error "No link selected"))
  7441. (if (or (member link all-prefixes)
  7442. (and (equal ":" (substring link -1))
  7443. (member (substring link 0 -1) all-prefixes)
  7444. (setq link (substring link 0 -1))))
  7445. (setq link (org-link-try-special-completion link))))
  7446. (set-window-configuration wcf)
  7447. (kill-buffer "*Org Links*"))
  7448. (setq entry (assoc link org-stored-links))
  7449. (or entry (push link org-insert-link-history))
  7450. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7451. (not org-keep-stored-link-after-insertion))
  7452. (setq org-stored-links (delq (assoc link org-stored-links)
  7453. org-stored-links)))
  7454. (setq desc (or desc (nth 1 entry)))))
  7455. (if (string-match org-plain-link-re link)
  7456. ;; URL-like link, normalize the use of angular brackets.
  7457. (setq link (org-make-link (org-remove-angle-brackets link))))
  7458. ;; Check if we are linking to the current file with a search option
  7459. ;; If yes, simplify the link by using only the search option.
  7460. (when (and buffer-file-name
  7461. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7462. (let* ((path (match-string 1 link))
  7463. (case-fold-search nil)
  7464. (search (match-string 2 link)))
  7465. (save-match-data
  7466. (if (equal (file-truename buffer-file-name) (file-truename path))
  7467. ;; We are linking to this same file, with a search option
  7468. (setq link search)))))
  7469. ;; Check if we can/should use a relative path. If yes, simplify the link
  7470. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7471. (let* ((type (match-string 1 link))
  7472. (path (match-string 2 link))
  7473. (origpath path)
  7474. (case-fold-search nil))
  7475. (cond
  7476. ((or (eq org-link-file-path-type 'absolute)
  7477. (equal complete-file '(16)))
  7478. (setq path (abbreviate-file-name (expand-file-name path))))
  7479. ((eq org-link-file-path-type 'noabbrev)
  7480. (setq path (expand-file-name path)))
  7481. ((eq org-link-file-path-type 'relative)
  7482. (setq path (file-relative-name path)))
  7483. (t
  7484. (save-match-data
  7485. (if (string-match (concat "^" (regexp-quote
  7486. (file-name-as-directory
  7487. (expand-file-name "."))))
  7488. (expand-file-name path))
  7489. ;; We are linking a file with relative path name.
  7490. (setq path (substring (expand-file-name path)
  7491. (match-end 0)))
  7492. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7493. (setq link (concat type path))
  7494. (if (equal desc origpath)
  7495. (setq desc path))))
  7496. (if org-make-link-description-function
  7497. (setq desc (funcall org-make-link-description-function link desc)))
  7498. (setq desc (read-string "Description: " desc))
  7499. (unless (string-match "\\S-" desc) (setq desc nil))
  7500. (if remove (apply 'delete-region remove))
  7501. (insert (org-make-link-string link desc))))
  7502. (defun org-link-try-special-completion (type)
  7503. "If there is completion support for link type TYPE, offer it."
  7504. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7505. (if (functionp fun)
  7506. (funcall fun)
  7507. (read-string "Link (no completion support): " (concat type ":")))))
  7508. (defun org-file-complete-link (&optional arg)
  7509. "Create a file link using completion."
  7510. (let (file link)
  7511. (setq file (read-file-name "File: "))
  7512. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7513. (pwd1 (file-name-as-directory (abbreviate-file-name
  7514. (expand-file-name ".")))))
  7515. (cond
  7516. ((equal arg '(16))
  7517. (setq link (org-make-link
  7518. "file:"
  7519. (abbreviate-file-name (expand-file-name file)))))
  7520. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7521. (setq link (org-make-link "file:" (match-string 1 file))))
  7522. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7523. (expand-file-name file))
  7524. (setq link (org-make-link
  7525. "file:" (match-string 1 (expand-file-name file)))))
  7526. (t (setq link (org-make-link "file:" file)))))
  7527. link))
  7528. (defun org-completing-read (&rest args)
  7529. "Completing-read with SPACE being a normal character."
  7530. (let ((minibuffer-local-completion-map
  7531. (copy-keymap minibuffer-local-completion-map)))
  7532. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7533. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7534. (apply 'org-icompleting-read args)))
  7535. (defun org-completing-read-no-i (&rest args)
  7536. (let (org-completion-use-ido org-completion-use-iswitchb)
  7537. (apply 'org-completing-read args)))
  7538. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7539. "Use iswitch as a completing-read replacement to choose from choices.
  7540. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7541. from."
  7542. (let* ((iswitchb-use-virtual-buffers nil)
  7543. (iswitchb-make-buflist-hook
  7544. (lambda ()
  7545. (setq iswitchb-temp-buflist choices))))
  7546. (iswitchb-read-buffer prompt)))
  7547. (defun org-icompleting-read (&rest args)
  7548. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7549. (org-without-partial-completion
  7550. (if (and org-completion-use-ido
  7551. (fboundp 'ido-completing-read)
  7552. (boundp 'ido-mode) ido-mode
  7553. (listp (second args)))
  7554. (let ((ido-enter-matching-directory nil))
  7555. (apply 'ido-completing-read (concat (car args))
  7556. (if (consp (car (nth 1 args)))
  7557. (mapcar (lambda (x) (car x)) (nth 1 args))
  7558. (nth 1 args))
  7559. (cddr args)))
  7560. (if (and org-completion-use-iswitchb
  7561. (boundp 'iswitchb-mode) iswitchb-mode
  7562. (listp (second args)))
  7563. (apply 'org-iswitchb-completing-read (concat (car args))
  7564. (if (consp (car (nth 1 args)))
  7565. (mapcar (lambda (x) (car x)) (nth 1 args))
  7566. (nth 1 args))
  7567. (cddr args))
  7568. (apply 'completing-read args)))))
  7569. (defun org-extract-attributes (s)
  7570. "Extract the attributes cookie from a string and set as text property."
  7571. (let (a attr (start 0) key value)
  7572. (save-match-data
  7573. (when (string-match "{{\\([^}]+\\)}}$" s)
  7574. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7575. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7576. (setq key (match-string 1 a) value (match-string 2 a)
  7577. start (match-end 0)
  7578. attr (plist-put attr (intern key) value))))
  7579. (org-add-props s nil 'org-attr attr))
  7580. s))
  7581. (defun org-extract-attributes-from-string (tag)
  7582. (let (key value attr)
  7583. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7584. (setq key (match-string 1 tag) value (match-string 2 tag)
  7585. tag (replace-match "" t t tag)
  7586. attr (plist-put attr (intern key) value)))
  7587. (cons tag attr)))
  7588. (defun org-attributes-to-string (plist)
  7589. "Format a property list into an HTML attribute list."
  7590. (let ((s "") key value)
  7591. (while plist
  7592. (setq key (pop plist) value (pop plist))
  7593. (and value
  7594. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7595. s))
  7596. ;;; Opening/following a link
  7597. (defvar org-link-search-failed nil)
  7598. (defvar org-open-link-functions nil
  7599. "Hook for functions finding a plain text link.
  7600. These functions must take a single argument, the link content.
  7601. They will be called for links that look like [[link text][description]]
  7602. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7603. like a filename (e.g. \"./blue.png\").
  7604. These functions will be called *before* Org attempts to resolve the
  7605. link by doing text searches in the current buffer - so if you want a
  7606. link \"[[target]]\" to still find \"<<target>>\", your function should
  7607. handle this as a special case.
  7608. When the function does handle the link, it must return a non-nil value.
  7609. If it decides that it is not responsible for this link, it must return
  7610. nil to indicate that that Org-mode can continue with other options
  7611. like exact and fuzzy text search.")
  7612. (defun org-next-link ()
  7613. "Move forward to the next link.
  7614. If the link is in hidden text, expose it."
  7615. (interactive)
  7616. (when (and org-link-search-failed (eq this-command last-command))
  7617. (goto-char (point-min))
  7618. (message "Link search wrapped back to beginning of buffer"))
  7619. (setq org-link-search-failed nil)
  7620. (let* ((pos (point))
  7621. (ct (org-context))
  7622. (a (assoc :link ct)))
  7623. (if a (goto-char (nth 2 a)))
  7624. (if (re-search-forward org-any-link-re nil t)
  7625. (progn
  7626. (goto-char (match-beginning 0))
  7627. (if (org-invisible-p) (org-show-context)))
  7628. (goto-char pos)
  7629. (setq org-link-search-failed t)
  7630. (error "No further link found"))))
  7631. (defun org-previous-link ()
  7632. "Move backward to the previous link.
  7633. If the link is in hidden text, expose it."
  7634. (interactive)
  7635. (when (and org-link-search-failed (eq this-command last-command))
  7636. (goto-char (point-max))
  7637. (message "Link search wrapped back to end of buffer"))
  7638. (setq org-link-search-failed nil)
  7639. (let* ((pos (point))
  7640. (ct (org-context))
  7641. (a (assoc :link ct)))
  7642. (if a (goto-char (nth 1 a)))
  7643. (if (re-search-backward org-any-link-re nil t)
  7644. (progn
  7645. (goto-char (match-beginning 0))
  7646. (if (org-invisible-p) (org-show-context)))
  7647. (goto-char pos)
  7648. (setq org-link-search-failed t)
  7649. (error "No further link found"))))
  7650. (defun org-translate-link (s)
  7651. "Translate a link string if a translation function has been defined."
  7652. (if (and org-link-translation-function
  7653. (fboundp org-link-translation-function)
  7654. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7655. (progn
  7656. (setq s (funcall org-link-translation-function
  7657. (match-string 1) (match-string 2)))
  7658. (concat (car s) ":" (cdr s)))
  7659. s))
  7660. (defun org-translate-link-from-planner (type path)
  7661. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7662. This is still an experimental function, your mileage may vary."
  7663. (cond
  7664. ((member type '("http" "https" "news" "ftp"))
  7665. ;; standard Internet links are the same.
  7666. nil)
  7667. ((and (equal type "irc") (string-match "^//" path))
  7668. ;; Planner has two / at the beginning of an irc link, we have 1.
  7669. ;; We should have zero, actually....
  7670. (setq path (substring path 1)))
  7671. ((and (equal type "lisp") (string-match "^/" path))
  7672. ;; Planner has a slash, we do not.
  7673. (setq type "elisp" path (substring path 1)))
  7674. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7675. ;; A typical message link. Planner has the id after the final slash,
  7676. ;; we separate it with a hash mark
  7677. (setq path (concat (match-string 1 path) "#"
  7678. (org-remove-angle-brackets (match-string 2 path)))))
  7679. )
  7680. (cons type path))
  7681. (defun org-find-file-at-mouse (ev)
  7682. "Open file link or URL at mouse."
  7683. (interactive "e")
  7684. (mouse-set-point ev)
  7685. (org-open-at-point 'in-emacs))
  7686. (defun org-open-at-mouse (ev)
  7687. "Open file link or URL at mouse."
  7688. (interactive "e")
  7689. (mouse-set-point ev)
  7690. (if (eq major-mode 'org-agenda-mode)
  7691. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7692. (org-open-at-point))
  7693. (defvar org-window-config-before-follow-link nil
  7694. "The window configuration before following a link.
  7695. This is saved in case the need arises to restore it.")
  7696. (defvar org-open-link-marker (make-marker)
  7697. "Marker pointing to the location where `org-open-at-point; was called.")
  7698. ;;;###autoload
  7699. (defun org-open-at-point-global ()
  7700. "Follow a link like Org-mode does.
  7701. This command can be called in any mode to follow a link that has
  7702. Org-mode syntax."
  7703. (interactive)
  7704. (org-run-like-in-org-mode 'org-open-at-point))
  7705. ;;;###autoload
  7706. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7707. "Open a link in the string S, as if it was in Org-mode."
  7708. (interactive "sLink: \nP")
  7709. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7710. (with-temp-buffer
  7711. (let ((org-inhibit-startup t))
  7712. (org-mode)
  7713. (insert s)
  7714. (goto-char (point-min))
  7715. (when reference-buffer
  7716. (setq org-link-abbrev-alist-local
  7717. (with-current-buffer reference-buffer
  7718. org-link-abbrev-alist-local)))
  7719. (org-open-at-point arg reference-buffer)))))
  7720. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7721. "Open link at or after point.
  7722. If there is no link at point, this function will search forward up to
  7723. the end of the current line.
  7724. Normally, files will be opened by an appropriate application. If the
  7725. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7726. With a double prefix argument, try to open outside of Emacs, in the
  7727. application the system uses for this file type."
  7728. (interactive "P")
  7729. (org-load-modules-maybe)
  7730. (move-marker org-open-link-marker (point))
  7731. (setq org-window-config-before-follow-link (current-window-configuration))
  7732. (org-remove-occur-highlights nil nil t)
  7733. (cond
  7734. ((and (org-on-heading-p)
  7735. (not (org-in-regexp
  7736. (concat org-plain-link-re "\\|"
  7737. org-bracket-link-regexp "\\|"
  7738. org-angle-link-re "\\|"
  7739. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7740. (not (get-text-property (point) 'org-linked-text)))
  7741. (or (org-offer-links-in-entry in-emacs)
  7742. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7743. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7744. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7745. (org-footnote-action))
  7746. (t
  7747. (let (type path link line search (pos (point)))
  7748. (catch 'match
  7749. (save-excursion
  7750. (skip-chars-forward "^]\n\r")
  7751. (when (org-in-regexp org-bracket-link-regexp 1)
  7752. (setq link (org-extract-attributes
  7753. (org-link-unescape (org-match-string-no-properties 1))))
  7754. (while (string-match " *\n *" link)
  7755. (setq link (replace-match " " t t link)))
  7756. (setq link (org-link-expand-abbrev link))
  7757. (cond
  7758. ((or (file-name-absolute-p link)
  7759. (string-match "^\\.\\.?/" link))
  7760. (setq type "file" path link))
  7761. ((string-match org-link-re-with-space3 link)
  7762. (setq type (match-string 1 link) path (match-string 2 link)))
  7763. (t (setq type "thisfile" path link)))
  7764. (throw 'match t)))
  7765. (when (get-text-property (point) 'org-linked-text)
  7766. (setq type "thisfile"
  7767. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7768. (1+ (point)) (point))
  7769. path (buffer-substring
  7770. (previous-single-property-change pos 'org-linked-text)
  7771. (next-single-property-change pos 'org-linked-text)))
  7772. (throw 'match t))
  7773. (save-excursion
  7774. (when (or (org-in-regexp org-angle-link-re)
  7775. (org-in-regexp org-plain-link-re))
  7776. (setq type (match-string 1) path (match-string 2))
  7777. (throw 'match t)))
  7778. (save-excursion
  7779. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7780. (setq type "tags"
  7781. path (match-string 1))
  7782. (while (string-match ":" path)
  7783. (setq path (replace-match "+" t t path)))
  7784. (throw 'match t)))
  7785. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7786. (setq type "tree-match"
  7787. path (match-string 1))
  7788. (throw 'match t)))
  7789. (unless path
  7790. (error "No link found"))
  7791. ;; switch back to reference buffer
  7792. ;; needed when if called in a temporary buffer through
  7793. ;; org-open-link-from-string
  7794. (with-current-buffer (or reference-buffer (current-buffer))
  7795. ;; Remove any trailing spaces in path
  7796. (if (string-match " +\\'" path)
  7797. (setq path (replace-match "" t t path)))
  7798. (if (and org-link-translation-function
  7799. (fboundp org-link-translation-function))
  7800. ;; Check if we need to translate the link
  7801. (let ((tmp (funcall org-link-translation-function type path)))
  7802. (setq type (car tmp) path (cdr tmp))))
  7803. (cond
  7804. ((assoc type org-link-protocols)
  7805. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7806. ((equal type "mailto")
  7807. (let ((cmd (car org-link-mailto-program))
  7808. (args (cdr org-link-mailto-program)) args1
  7809. (address path) (subject "") a)
  7810. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7811. (setq address (match-string 1 path)
  7812. subject (org-link-escape (match-string 2 path))))
  7813. (while args
  7814. (cond
  7815. ((not (stringp (car args))) (push (pop args) args1))
  7816. (t (setq a (pop args))
  7817. (if (string-match "%a" a)
  7818. (setq a (replace-match address t t a)))
  7819. (if (string-match "%s" a)
  7820. (setq a (replace-match subject t t a)))
  7821. (push a args1))))
  7822. (apply cmd (nreverse args1))))
  7823. ((member type '("http" "https" "ftp" "news"))
  7824. (browse-url (concat type ":" (org-link-escape
  7825. path org-link-escape-chars-browser))))
  7826. ((string= type "doi")
  7827. (browse-url (concat "http://dx.doi.org/"
  7828. (org-link-escape
  7829. path org-link-escape-chars-browser))))
  7830. ((member type '("message"))
  7831. (browse-url (concat type ":" path)))
  7832. ((string= type "tags")
  7833. (org-tags-view in-emacs path))
  7834. ((string= type "tree-match")
  7835. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7836. ((string= type "file")
  7837. (if (string-match "::\\([0-9]+\\)\\'" path)
  7838. (setq line (string-to-number (match-string 1 path))
  7839. path (substring path 0 (match-beginning 0)))
  7840. (if (string-match "::\\(.+\\)\\'" path)
  7841. (setq search (match-string 1 path)
  7842. path (substring path 0 (match-beginning 0)))))
  7843. (if (string-match "[*?{]" (file-name-nondirectory path))
  7844. (dired path)
  7845. (org-open-file path in-emacs line search)))
  7846. ((string= type "news")
  7847. (require 'org-gnus)
  7848. (org-gnus-follow-link path))
  7849. ((string= type "shell")
  7850. (let ((cmd path))
  7851. (if (or (not org-confirm-shell-link-function)
  7852. (funcall org-confirm-shell-link-function
  7853. (format "Execute \"%s\" in shell? "
  7854. (org-add-props cmd nil
  7855. 'face 'org-warning))))
  7856. (progn
  7857. (message "Executing %s" cmd)
  7858. (shell-command cmd))
  7859. (error "Abort"))))
  7860. ((string= type "elisp")
  7861. (let ((cmd path))
  7862. (if (or (not org-confirm-elisp-link-function)
  7863. (funcall org-confirm-elisp-link-function
  7864. (format "Execute \"%s\" as elisp? "
  7865. (org-add-props cmd nil
  7866. 'face 'org-warning))))
  7867. (message "%s => %s" cmd
  7868. (if (equal (string-to-char cmd) ?\()
  7869. (eval (read cmd))
  7870. (call-interactively (read cmd))))
  7871. (error "Abort"))))
  7872. ((and (string= type "thisfile")
  7873. (run-hook-with-args-until-success
  7874. 'org-open-link-functions path)))
  7875. ((string= type "thisfile")
  7876. (if in-emacs
  7877. (switch-to-buffer-other-window
  7878. (org-get-buffer-for-internal-link (current-buffer)))
  7879. (org-mark-ring-push))
  7880. (let ((cmd `(org-link-search
  7881. ,path
  7882. ,(cond ((equal in-emacs '(4)) 'occur)
  7883. ((equal in-emacs '(16)) 'org-occur)
  7884. (t nil))
  7885. ,pos)))
  7886. (condition-case nil (eval cmd)
  7887. (error (progn (widen) (eval cmd))))))
  7888. (t
  7889. (browse-url-at-point)))))))
  7890. (move-marker org-open-link-marker nil)
  7891. (run-hook-with-args 'org-follow-link-hook))
  7892. (defun org-offer-links-in-entry (&optional nth zero)
  7893. "Offer links in the current entry and follow the selected link.
  7894. If there is only one link, follow it immediately as well.
  7895. If NTH is an integer, immediately pick the NTH link found.
  7896. If ZERO is a string, check also this string for a link, and if
  7897. there is one, offer it as link number zero."
  7898. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7899. "\\(" org-angle-link-re "\\)\\|"
  7900. "\\(" org-plain-link-re "\\)"))
  7901. (cnt ?0)
  7902. (in-emacs (if (integerp nth) nil nth))
  7903. have-zero end links link c)
  7904. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7905. (push (match-string 0 zero) links)
  7906. (setq cnt (1- cnt) have-zero t))
  7907. (save-excursion
  7908. (org-back-to-heading t)
  7909. (setq end (save-excursion (outline-next-heading) (point)))
  7910. (while (re-search-forward re end t)
  7911. (push (match-string 0) links))
  7912. (setq links (org-uniquify (reverse links))))
  7913. (cond
  7914. ((null links)
  7915. (message "No links"))
  7916. ((equal (length links) 1)
  7917. (setq link (list (car links))))
  7918. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7919. (setq link (nth (if have-zero nth (1- nth)) links)))
  7920. (t ; we have to select a link
  7921. (save-excursion
  7922. (save-window-excursion
  7923. (delete-other-windows)
  7924. (with-output-to-temp-buffer "*Select Link*"
  7925. (mapc (lambda (l)
  7926. (if (not (string-match org-bracket-link-regexp l))
  7927. (princ (format "[%c] %s\n" (incf cnt)
  7928. (org-remove-angle-brackets l)))
  7929. (if (match-end 3)
  7930. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7931. (match-string 3 l) (match-string 1 l)))
  7932. (princ (format "[%c] %s\n" (incf cnt)
  7933. (match-string 1 l))))))
  7934. links))
  7935. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7936. (message "Select link to open, RET to open all:")
  7937. (setq c (read-char-exclusive))
  7938. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7939. (when (equal c ?q) (error "Abort"))
  7940. (if (equal c ?\C-m)
  7941. (setq link links)
  7942. (setq nth (- c ?0))
  7943. (if have-zero (setq nth (1+ nth)))
  7944. (unless (and (integerp nth) (>= (length links) nth))
  7945. (error "Invalid link selection"))
  7946. (setq link (list (nth (1- nth) links))))))
  7947. (if link
  7948. (let ((buf (current-buffer)))
  7949. (dolist (l link)
  7950. (org-open-link-from-string l in-emacs buf))
  7951. t)
  7952. nil)))
  7953. ;; Add special file links that specify the way of opening
  7954. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7955. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7956. (defun org-open-file-with-system (path)
  7957. "Open file at PATH using the system way of opeing it."
  7958. (org-open-file path 'system))
  7959. (defun org-open-file-with-emacs (path)
  7960. "Open file at PATH in emacs."
  7961. (org-open-file path 'emacs))
  7962. (defun org-remove-file-link-modifiers ()
  7963. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7964. (goto-char (point-min))
  7965. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7966. (org-if-unprotected
  7967. (replace-match "file:" t t))))
  7968. (eval-after-load "org-exp"
  7969. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7970. 'org-remove-file-link-modifiers))
  7971. ;;;; Time estimates
  7972. (defun org-get-effort (&optional pom)
  7973. "Get the effort estimate for the current entry."
  7974. (org-entry-get pom org-effort-property))
  7975. ;;; File search
  7976. (defvar org-create-file-search-functions nil
  7977. "List of functions to construct the right search string for a file link.
  7978. These functions are called in turn with point at the location to
  7979. which the link should point.
  7980. A function in the hook should first test if it would like to
  7981. handle this file type, for example by checking the major-mode or
  7982. the file extension. If it decides not to handle this file, it
  7983. should just return nil to give other functions a chance. If it
  7984. does handle the file, it must return the search string to be used
  7985. when following the link. The search string will be part of the
  7986. file link, given after a double colon, and `org-open-at-point'
  7987. will automatically search for it. If special measures must be
  7988. taken to make the search successful, another function should be
  7989. added to the companion hook `org-execute-file-search-functions',
  7990. which see.
  7991. A function in this hook may also use `setq' to set the variable
  7992. `description' to provide a suggestion for the descriptive text to
  7993. be used for this link when it gets inserted into an Org-mode
  7994. buffer with \\[org-insert-link].")
  7995. (defvar org-execute-file-search-functions nil
  7996. "List of functions to execute a file search triggered by a link.
  7997. Functions added to this hook must accept a single argument, the
  7998. search string that was part of the file link, the part after the
  7999. double colon. The function must first check if it would like to
  8000. handle this search, for example by checking the major-mode or the
  8001. file extension. If it decides not to handle this search, it
  8002. should just return nil to give other functions a chance. If it
  8003. does handle the search, it must return a non-nil value to keep
  8004. other functions from trying.
  8005. Each function can access the current prefix argument through the
  8006. variable `current-prefix-argument'. Note that a single prefix is
  8007. used to force opening a link in Emacs, so it may be good to only
  8008. use a numeric or double prefix to guide the search function.
  8009. In case this is needed, a function in this hook can also restore
  8010. the window configuration before `org-open-at-point' was called using:
  8011. (set-window-configuration org-window-config-before-follow-link)")
  8012. (defun org-link-search (s &optional type avoid-pos)
  8013. "Search for a link search option.
  8014. If S is surrounded by forward slashes, it is interpreted as a
  8015. regular expression. In org-mode files, this will create an `org-occur'
  8016. sparse tree. In ordinary files, `occur' will be used to list matches.
  8017. If the current buffer is in `dired-mode', grep will be used to search
  8018. in all files. If AVOID-POS is given, ignore matches near that position."
  8019. (let ((case-fold-search t)
  8020. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8021. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8022. (append '(("") (" ") ("\t") ("\n"))
  8023. org-emphasis-alist)
  8024. "\\|") "\\)"))
  8025. (pos (point))
  8026. (pre nil) (post nil)
  8027. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8028. (cond
  8029. ;; First check if there are any special
  8030. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8031. ;; Now try the builtin stuff
  8032. ((and (equal (string-to-char s0) ?#)
  8033. (> (length s0) 1)
  8034. (save-excursion
  8035. (goto-char (point-min))
  8036. (and
  8037. (re-search-forward
  8038. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8039. (setq type 'dedicated
  8040. pos (match-beginning 0))))
  8041. ;; There is an exact target for this
  8042. (goto-char pos)
  8043. (org-back-to-heading t)))
  8044. ((save-excursion
  8045. (goto-char (point-min))
  8046. (and
  8047. (re-search-forward
  8048. (concat "<<" (regexp-quote s0) ">>") nil t)
  8049. (setq type 'dedicated
  8050. pos (match-beginning 0))))
  8051. ;; There is an exact target for this
  8052. (goto-char pos))
  8053. ((and (string-match "^(\\(.*\\))$" s0)
  8054. (save-excursion
  8055. (goto-char (point-min))
  8056. (and
  8057. (re-search-forward
  8058. (concat "[^[]" (regexp-quote
  8059. (format org-coderef-label-format
  8060. (match-string 1 s0))))
  8061. nil t)
  8062. (setq type 'dedicated
  8063. pos (1+ (match-beginning 0))))))
  8064. ;; There is a coderef target for this
  8065. (goto-char pos))
  8066. ((string-match "^/\\(.*\\)/$" s)
  8067. ;; A regular expression
  8068. (cond
  8069. ((org-mode-p)
  8070. (org-occur (match-string 1 s)))
  8071. ;;((eq major-mode 'dired-mode)
  8072. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8073. (t (org-do-occur (match-string 1 s)))))
  8074. (t
  8075. ;; A normal search strings
  8076. (when (equal (string-to-char s) ?*)
  8077. ;; Anchor on headlines, post may include tags.
  8078. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8079. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8080. s (substring s 1)))
  8081. (remove-text-properties
  8082. 0 (length s)
  8083. '(face nil mouse-face nil keymap nil fontified nil) s)
  8084. ;; Make a series of regular expressions to find a match
  8085. (setq words (org-split-string s "[ \n\r\t]+")
  8086. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8087. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8088. "\\)" markers)
  8089. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8090. re2a (concat "[ \t\r\n]" re2a_)
  8091. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8092. re4 (concat "[^a-zA-Z_]" re4_)
  8093. re1 (concat pre re2 post)
  8094. re3 (concat pre (if pre re4_ re4) post)
  8095. re5 (concat pre ".*" re4)
  8096. re2 (concat pre re2)
  8097. re2a (concat pre (if pre re2a_ re2a))
  8098. re4 (concat pre (if pre re4_ re4))
  8099. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8100. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8101. re5 "\\)"
  8102. ))
  8103. (cond
  8104. ((eq type 'org-occur) (org-occur reall))
  8105. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8106. (t (goto-char (point-min))
  8107. (setq type 'fuzzy)
  8108. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8109. (org-search-not-self 1 re1 nil t)
  8110. (org-search-not-self 1 re2 nil t)
  8111. (org-search-not-self 1 re2a nil t)
  8112. (org-search-not-self 1 re3 nil t)
  8113. (org-search-not-self 1 re4 nil t)
  8114. (org-search-not-self 1 re5 nil t)
  8115. )
  8116. (goto-char (match-beginning 1))
  8117. (goto-char pos)
  8118. (error "No match")))))
  8119. (t
  8120. ;; Normal string-search
  8121. (goto-char (point-min))
  8122. (if (search-forward s nil t)
  8123. (goto-char (match-beginning 0))
  8124. (error "No match"))))
  8125. (and (org-mode-p) (org-show-context 'link-search))
  8126. type))
  8127. (defun org-search-not-self (group &rest args)
  8128. "Execute `re-search-forward', but only accept matches that do not
  8129. enclose the position of `org-open-link-marker'."
  8130. (let ((m org-open-link-marker))
  8131. (catch 'exit
  8132. (while (apply 're-search-forward args)
  8133. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8134. (goto-char (match-end group))
  8135. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8136. (> (match-beginning 0) (marker-position m))
  8137. (< (match-end 0) (marker-position m)))
  8138. (save-match-data
  8139. (or (not (org-in-regexp
  8140. org-bracket-link-analytic-regexp 1))
  8141. (not (match-end 4)) ; no description
  8142. (and (<= (match-beginning 4) (point))
  8143. (>= (match-end 4) (point))))))
  8144. (throw 'exit (point))))))))
  8145. (defun org-get-buffer-for-internal-link (buffer)
  8146. "Return a buffer to be used for displaying the link target of internal links."
  8147. (cond
  8148. ((not org-display-internal-link-with-indirect-buffer)
  8149. buffer)
  8150. ((string-match "(Clone)$" (buffer-name buffer))
  8151. (message "Buffer is already a clone, not making another one")
  8152. ;; we also do not modify visibility in this case
  8153. buffer)
  8154. (t ; make a new indirect buffer for displaying the link
  8155. (let* ((bn (buffer-name buffer))
  8156. (ibn (concat bn "(Clone)"))
  8157. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8158. (with-current-buffer ib (org-overview))
  8159. ib))))
  8160. (defun org-do-occur (regexp &optional cleanup)
  8161. "Call the Emacs command `occur'.
  8162. If CLEANUP is non-nil, remove the printout of the regular expression
  8163. in the *Occur* buffer. This is useful if the regex is long and not useful
  8164. to read."
  8165. (occur regexp)
  8166. (when cleanup
  8167. (let ((cwin (selected-window)) win beg end)
  8168. (when (setq win (get-buffer-window "*Occur*"))
  8169. (select-window win))
  8170. (goto-char (point-min))
  8171. (when (re-search-forward "match[a-z]+" nil t)
  8172. (setq beg (match-end 0))
  8173. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8174. (setq end (1- (match-beginning 0)))))
  8175. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8176. (goto-char (point-min))
  8177. (select-window cwin))))
  8178. ;;; The mark ring for links jumps
  8179. (defvar org-mark-ring nil
  8180. "Mark ring for positions before jumps in Org-mode.")
  8181. (defvar org-mark-ring-last-goto nil
  8182. "Last position in the mark ring used to go back.")
  8183. ;; Fill and close the ring
  8184. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8185. (loop for i from 1 to org-mark-ring-length do
  8186. (push (make-marker) org-mark-ring))
  8187. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8188. org-mark-ring)
  8189. (defun org-mark-ring-push (&optional pos buffer)
  8190. "Put the current position or POS into the mark ring and rotate it."
  8191. (interactive)
  8192. (setq pos (or pos (point)))
  8193. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8194. (move-marker (car org-mark-ring)
  8195. (or pos (point))
  8196. (or buffer (current-buffer)))
  8197. (message "%s"
  8198. (substitute-command-keys
  8199. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8200. (defun org-mark-ring-goto (&optional n)
  8201. "Jump to the previous position in the mark ring.
  8202. With prefix arg N, jump back that many stored positions. When
  8203. called several times in succession, walk through the entire ring.
  8204. Org-mode commands jumping to a different position in the current file,
  8205. or to another Org-mode file, automatically push the old position
  8206. onto the ring."
  8207. (interactive "p")
  8208. (let (p m)
  8209. (if (eq last-command this-command)
  8210. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8211. (setq p org-mark-ring))
  8212. (setq org-mark-ring-last-goto p)
  8213. (setq m (car p))
  8214. (switch-to-buffer (marker-buffer m))
  8215. (goto-char m)
  8216. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8217. (defun org-remove-angle-brackets (s)
  8218. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8219. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8220. s)
  8221. (defun org-add-angle-brackets (s)
  8222. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8223. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8224. s)
  8225. (defun org-remove-double-quotes (s)
  8226. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8227. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8228. s)
  8229. ;;; Following specific links
  8230. (defun org-follow-timestamp-link ()
  8231. (cond
  8232. ((org-at-date-range-p t)
  8233. (let ((org-agenda-start-on-weekday)
  8234. (t1 (match-string 1))
  8235. (t2 (match-string 2)))
  8236. (setq t1 (time-to-days (org-time-string-to-time t1))
  8237. t2 (time-to-days (org-time-string-to-time t2)))
  8238. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8239. ((org-at-timestamp-p t)
  8240. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8241. (substring (match-string 1) 0 10)))
  8242. 1))
  8243. (t (error "This should not happen"))))
  8244. ;;; Following file links
  8245. (defvar org-wait nil)
  8246. (defun org-open-file (path &optional in-emacs line search)
  8247. "Open the file at PATH.
  8248. First, this expands any special file name abbreviations. Then the
  8249. configuration variable `org-file-apps' is checked if it contains an
  8250. entry for this file type, and if yes, the corresponding command is launched.
  8251. If no application is found, Emacs simply visits the file.
  8252. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8253. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8254. and to use an external application to visit the file.
  8255. Optional LINE specifies a line to go to, optional SEARCH a string
  8256. to search for. If LINE or SEARCH is given, the file will be
  8257. opened in Emacs, unless an entry from org-file-apps that makes
  8258. use of groups in a regexp matches.
  8259. If the file does not exist, an error is thrown."
  8260. (let* ((file (if (equal path "")
  8261. buffer-file-name
  8262. (substitute-in-file-name (expand-file-name path))))
  8263. (file-apps (append org-file-apps (org-default-apps)))
  8264. (apps (org-remove-if
  8265. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8266. (apps-dlink (org-remove-if-not
  8267. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8268. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8269. (dirp (if remp nil (file-directory-p file)))
  8270. (file (if (and dirp org-open-directory-means-index-dot-org)
  8271. (concat (file-name-as-directory file) "index.org")
  8272. file))
  8273. (a-m-a-p (assq 'auto-mode apps))
  8274. (dfile (downcase file))
  8275. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8276. (link (cond ((and (eq line nil)
  8277. (eq search nil))
  8278. file)
  8279. (line
  8280. (concat file "::" (number-to-string line)))
  8281. (search
  8282. (concat file "::" search))))
  8283. (dlink (downcase link))
  8284. (old-buffer (current-buffer))
  8285. (old-pos (point))
  8286. (old-mode major-mode)
  8287. ext cmd link-match-data)
  8288. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8289. (setq ext (match-string 1 dfile))
  8290. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8291. (setq ext (match-string 1 dfile))))
  8292. (cond
  8293. ((member in-emacs '((16) system))
  8294. (setq cmd (cdr (assoc 'system apps))))
  8295. (in-emacs (setq cmd 'emacs))
  8296. (t
  8297. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8298. (and dirp (cdr (assoc 'directory apps)))
  8299. ; first, try matching against apps-dlink
  8300. ; if we get a match here, store the match data for later
  8301. (let ((match (assoc-default dlink apps-dlink
  8302. 'string-match)))
  8303. (if match
  8304. (progn (setq link-match-data (match-data))
  8305. match)
  8306. (progn (setq in-emacs (or in-emacs line search))
  8307. nil))) ; if we have no match in apps-dlink,
  8308. ; always open the file in emacs if line or search
  8309. ; is given (for backwards compatibility)
  8310. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8311. 'string-match)
  8312. (cdr (assoc ext apps))
  8313. (cdr (assoc t apps))))))
  8314. (when (eq cmd 'system)
  8315. (setq cmd (cdr (assoc 'system apps))))
  8316. (when (eq cmd 'default)
  8317. (setq cmd (cdr (assoc t apps))))
  8318. (when (eq cmd 'mailcap)
  8319. (require 'mailcap)
  8320. (mailcap-parse-mailcaps)
  8321. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8322. (command (mailcap-mime-info mime-type)))
  8323. (if (stringp command)
  8324. (setq cmd command)
  8325. (setq cmd 'emacs))))
  8326. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8327. (not (file-exists-p file))
  8328. (not org-open-non-existing-files))
  8329. (error "No such file: %s" file))
  8330. (cond
  8331. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8332. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8333. (while (string-match "['\"]%s['\"]" cmd)
  8334. (setq cmd (replace-match "%s" t t cmd)))
  8335. (while (string-match "%s" cmd)
  8336. (setq cmd (replace-match
  8337. (save-match-data
  8338. (shell-quote-argument
  8339. (convert-standard-filename file)))
  8340. t t cmd)))
  8341. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8342. (save-match-data
  8343. (let ((match-index 1)
  8344. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8345. (set-match-data link-match-data)
  8346. (while (<= match-index number-of-groups)
  8347. (let ((regex (concat "%" (number-to-string match-index)))
  8348. (replace-with (match-string match-index dlink)))
  8349. (while (string-match regex cmd)
  8350. (setq cmd (replace-match replace-with t t cmd))))
  8351. (setq match-index (+ match-index 1)))))
  8352. (save-window-excursion
  8353. (start-process-shell-command cmd nil cmd)
  8354. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8355. ))
  8356. ((or (stringp cmd)
  8357. (eq cmd 'emacs))
  8358. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8359. (widen)
  8360. (if line (org-goto-line line)
  8361. (if search (org-link-search search))))
  8362. ((consp cmd)
  8363. (let ((file (convert-standard-filename file)))
  8364. (save-match-data
  8365. (set-match-data link-match-data)
  8366. (eval cmd))))
  8367. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8368. (and (org-mode-p) (eq old-mode 'org-mode)
  8369. (or (not (equal old-buffer (current-buffer)))
  8370. (not (equal old-pos (point))))
  8371. (org-mark-ring-push old-pos old-buffer))))
  8372. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8373. "This function returns non-nil if `entry' uses a regular
  8374. expression which should be matched against the whole link by
  8375. org-open-file.
  8376. It assumes that is the case when the entry uses a regular
  8377. expression which has at least one grouping construct and the
  8378. action is either a lisp form or a command string containing
  8379. '%1', i.e. using at least one subexpression match as a
  8380. parameter."
  8381. (let ((selector (car entry))
  8382. (action (cdr entry)))
  8383. (if (stringp selector)
  8384. (and (> (regexp-opt-depth selector) 0)
  8385. (or (and (stringp action)
  8386. (string-match "%[0-9]" action))
  8387. (consp action)))
  8388. nil)))
  8389. (defun org-default-apps ()
  8390. "Return the default applications for this operating system."
  8391. (cond
  8392. ((eq system-type 'darwin)
  8393. org-file-apps-defaults-macosx)
  8394. ((eq system-type 'windows-nt)
  8395. org-file-apps-defaults-windowsnt)
  8396. (t org-file-apps-defaults-gnu)))
  8397. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8398. "Convert extensions to regular expressions in the cars of LIST.
  8399. Also, weed out any non-string entries, because the return value is used
  8400. only for regexp matching.
  8401. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8402. point to the symbol `emacs', indicating that the file should
  8403. be opened in Emacs."
  8404. (append
  8405. (delq nil
  8406. (mapcar (lambda (x)
  8407. (if (not (stringp (car x)))
  8408. nil
  8409. (if (string-match "\\W" (car x))
  8410. x
  8411. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8412. list))
  8413. (if add-auto-mode
  8414. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8415. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8416. (defun org-file-remote-p (file)
  8417. "Test whether FILE specifies a location on a remote system.
  8418. Return non-nil if the location is indeed remote.
  8419. For example, the filename \"/user@host:/foo\" specifies a location
  8420. on the system \"/user@host:\"."
  8421. (cond ((fboundp 'file-remote-p)
  8422. (file-remote-p file))
  8423. ((fboundp 'tramp-handle-file-remote-p)
  8424. (tramp-handle-file-remote-p file))
  8425. ((and (boundp 'ange-ftp-name-format)
  8426. (string-match (car ange-ftp-name-format) file))
  8427. t)
  8428. (t nil)))
  8429. ;;;; Refiling
  8430. (defun org-get-org-file ()
  8431. "Read a filename, with default directory `org-directory'."
  8432. (let ((default (or org-default-notes-file remember-data-file)))
  8433. (read-file-name (format "File name [%s]: " default)
  8434. (file-name-as-directory org-directory)
  8435. default)))
  8436. (defun org-notes-order-reversed-p ()
  8437. "Check if the current file should receive notes in reversed order."
  8438. (cond
  8439. ((not org-reverse-note-order) nil)
  8440. ((eq t org-reverse-note-order) t)
  8441. ((not (listp org-reverse-note-order)) nil)
  8442. (t (catch 'exit
  8443. (let ((all org-reverse-note-order)
  8444. entry)
  8445. (while (setq entry (pop all))
  8446. (if (string-match (car entry) buffer-file-name)
  8447. (throw 'exit (cdr entry))))
  8448. nil)))))
  8449. (defvar org-refile-target-table nil
  8450. "The list of refile targets, created by `org-refile'.")
  8451. (defvar org-agenda-new-buffers nil
  8452. "Buffers created to visit agenda files.")
  8453. (defun org-get-refile-targets (&optional default-buffer)
  8454. "Produce a table with refile targets."
  8455. (let ((case-fold-search nil)
  8456. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8457. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8458. targets txt re files f desc descre fast-path-p level pos0)
  8459. (message "Getting targets...")
  8460. (with-current-buffer (or default-buffer (current-buffer))
  8461. (while (setq entry (pop entries))
  8462. (setq files (car entry) desc (cdr entry))
  8463. (setq fast-path-p nil)
  8464. (cond
  8465. ((null files) (setq files (list (current-buffer))))
  8466. ((eq files 'org-agenda-files)
  8467. (setq files (org-agenda-files 'unrestricted)))
  8468. ((and (symbolp files) (fboundp files))
  8469. (setq files (funcall files)))
  8470. ((and (symbolp files) (boundp files))
  8471. (setq files (symbol-value files))))
  8472. (if (stringp files) (setq files (list files)))
  8473. (cond
  8474. ((eq (car desc) :tag)
  8475. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8476. ((eq (car desc) :todo)
  8477. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8478. ((eq (car desc) :regexp)
  8479. (setq descre (cdr desc)))
  8480. ((eq (car desc) :level)
  8481. (setq descre (concat "^\\*\\{" (number-to-string
  8482. (if org-odd-levels-only
  8483. (1- (* 2 (cdr desc)))
  8484. (cdr desc)))
  8485. "\\}[ \t]")))
  8486. ((eq (car desc) :maxlevel)
  8487. (setq fast-path-p t)
  8488. (setq descre (concat "^\\*\\{1," (number-to-string
  8489. (if org-odd-levels-only
  8490. (1- (* 2 (cdr desc)))
  8491. (cdr desc)))
  8492. "\\}[ \t]")))
  8493. (t (error "Bad refiling target description %s" desc)))
  8494. (while (setq f (pop files))
  8495. (with-current-buffer
  8496. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8497. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8498. (setq f (and f (expand-file-name f)))
  8499. (if (eq org-refile-use-outline-path 'file)
  8500. (push (list (file-name-nondirectory f) f nil nil) targets))
  8501. (save-excursion
  8502. (save-restriction
  8503. (widen)
  8504. (goto-char (point-min))
  8505. (while (re-search-forward descre nil t)
  8506. (goto-char (setq pos0 (point-at-bol)))
  8507. (catch 'next
  8508. (when org-refile-target-verify-function
  8509. (save-match-data
  8510. (or (funcall org-refile-target-verify-function)
  8511. (throw 'next t))))
  8512. (when (looking-at org-complex-heading-regexp)
  8513. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8514. txt (org-link-display-format (match-string 4))
  8515. re (concat "^" (regexp-quote
  8516. (buffer-substring (match-beginning 1)
  8517. (match-end 4)))))
  8518. (if (match-end 5) (setq re (concat re "[ \t]+"
  8519. (regexp-quote
  8520. (match-string 5)))))
  8521. (setq re (concat re "[ \t]*$"))
  8522. (when org-refile-use-outline-path
  8523. (setq txt (mapconcat 'org-protect-slash
  8524. (append
  8525. (if (eq org-refile-use-outline-path 'file)
  8526. (list (file-name-nondirectory
  8527. (buffer-file-name (buffer-base-buffer))))
  8528. (if (eq org-refile-use-outline-path 'full-file-path)
  8529. (list (buffer-file-name (buffer-base-buffer)))))
  8530. (org-get-outline-path fast-path-p level txt)
  8531. (list txt))
  8532. "/")))
  8533. (push (list txt f re (point)) targets)))
  8534. (when (= (point) pos0)
  8535. ;; verification function has not moved point
  8536. (goto-char (point-at-eol))))))))))
  8537. (message "Getting targets...done")
  8538. (nreverse targets)))
  8539. (defun org-protect-slash (s)
  8540. (while (string-match "/" s)
  8541. (setq s (replace-match "\\" t t s)))
  8542. s)
  8543. (defvar org-olpa (make-vector 20 nil))
  8544. (defun org-get-outline-path (&optional fastp level heading)
  8545. "Return the outline path to the current entry, as a list.
  8546. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8547. routine which makes outline path derivations for an entire file,
  8548. avoiding backtracing."
  8549. (if fastp
  8550. (progn
  8551. (if (> level 19)
  8552. (error "Outline path failure, more than 19 levels."))
  8553. (loop for i from level upto 19 do
  8554. (aset org-olpa i nil))
  8555. (prog1
  8556. (delq nil (append org-olpa nil))
  8557. (aset org-olpa level heading)))
  8558. (let (rtn case-fold-search)
  8559. (save-excursion
  8560. (save-restriction
  8561. (widen)
  8562. (while (org-up-heading-safe)
  8563. (when (looking-at org-complex-heading-regexp)
  8564. (push (org-match-string-no-properties 4) rtn)))
  8565. rtn)))))
  8566. (defun org-format-outline-path (path &optional width prefix)
  8567. "Format the outlie path PATH for display.
  8568. Width is the maximum number of characters that is available.
  8569. Prefix is a prefix to be included in the returned string,
  8570. such as the file name."
  8571. (setq width (or width 79))
  8572. (if prefix (setq width (- width (length prefix))))
  8573. (if (not path)
  8574. (or prefix "")
  8575. (let* ((nsteps (length path))
  8576. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8577. (maxwidth (if (<= total-width width)
  8578. 10000 ;; everything fits
  8579. ;; we need to shorten the level headings
  8580. (/ (- width nsteps) nsteps)))
  8581. (org-odd-levels-only nil)
  8582. (n 0)
  8583. (total (1+ (length prefix))))
  8584. (setq maxwidth (max maxwidth 10))
  8585. (concat prefix
  8586. (mapconcat
  8587. (lambda (h)
  8588. (setq n (1+ n))
  8589. (if (and (= n nsteps) (< maxwidth 10000))
  8590. (setq maxwidth (- total-width total)))
  8591. (if (< (length h) maxwidth)
  8592. (progn (setq total (+ total (length h) 1)) h)
  8593. (setq h (substring h 0 (- maxwidth 2))
  8594. total (+ total maxwidth 1))
  8595. (if (string-match "[ \t]+\\'" h)
  8596. (setq h (substring h 0 (match-beginning 0))))
  8597. (setq h (concat h "..")))
  8598. (org-add-props h nil 'face
  8599. (nth (% (1- n) org-n-level-faces)
  8600. org-level-faces))
  8601. h)
  8602. path "/")))))
  8603. (defun org-display-outline-path (&optional file current)
  8604. "Display the current outline path in the echo area."
  8605. (interactive "P")
  8606. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8607. (case-fold-search nil)
  8608. (path (and (org-mode-p) (org-get-outline-path))))
  8609. (if current (setq path (append path
  8610. (save-excursion
  8611. (org-back-to-heading t)
  8612. (if (looking-at org-complex-heading-regexp)
  8613. (list (match-string 4)))))))
  8614. (message "%s"
  8615. (org-format-outline-path
  8616. path
  8617. (1- (frame-width))
  8618. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8619. (defvar org-refile-history nil
  8620. "History for refiling operations.")
  8621. (defvar org-after-refile-insert-hook nil
  8622. "Hook run after `org-refile' has inserted its stuff at the new location.
  8623. Note that this is still *before* the stuff will be removed from
  8624. the *old* location.")
  8625. (defun org-refile (&optional goto default-buffer rfloc)
  8626. "Move the entry at point to another heading.
  8627. The list of target headings is compiled using the information in
  8628. `org-refile-targets', which see. This list is created before each use
  8629. and will therefore always be up-to-date.
  8630. At the target location, the entry is filed as a subitem of the target heading.
  8631. Depending on `org-reverse-note-order', the new subitem will either be the
  8632. first or the last subitem.
  8633. If there is an active region, all entries in that region will be moved.
  8634. However, the region must fulfil the requirement that the first heading
  8635. is the first one sets the top-level of the moved text - at most siblings
  8636. below it are allowed.
  8637. With prefix arg GOTO, the command will only visit the target location,
  8638. not actually move anything.
  8639. With a double prefix `C-u C-u', go to the location where the last refiling
  8640. operation has put the subtree.
  8641. With a prefix argument of `2', refile to the running clock.
  8642. RFLOC can be a refile location obtained in a different way.
  8643. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8644. (interactive "P")
  8645. (let* ((cbuf (current-buffer))
  8646. (regionp (org-region-active-p))
  8647. (region-start (and regionp (region-beginning)))
  8648. (region-end (and regionp (region-end)))
  8649. (region-length (and regionp (- region-end region-start)))
  8650. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8651. pos it nbuf file re level reversed)
  8652. (setq last-command nil)
  8653. (when regionp
  8654. (goto-char region-start)
  8655. (or (bolp) (goto-char (point-at-bol)))
  8656. (setq region-start (point))
  8657. (unless (org-kill-is-subtree-p
  8658. (buffer-substring region-start region-end))
  8659. (error "The region is not a (sequence of) subtree(s)")))
  8660. (if (equal goto '(16))
  8661. (org-refile-goto-last-stored)
  8662. (when (or
  8663. (and (equal goto 2)
  8664. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8665. (prog1
  8666. (setq it (list (or org-clock-heading "running clock")
  8667. (buffer-file-name
  8668. (marker-buffer org-clock-hd-marker))
  8669. ""
  8670. (marker-position org-clock-hd-marker)))
  8671. (setq goto nil)))
  8672. (setq it (or rfloc
  8673. (save-excursion
  8674. (org-refile-get-location
  8675. (if goto "Goto: " "Refile to: ") default-buffer
  8676. org-refile-allow-creating-parent-nodes)))))
  8677. (setq file (nth 1 it)
  8678. re (nth 2 it)
  8679. pos (nth 3 it))
  8680. (if (and (not goto)
  8681. pos
  8682. (equal (buffer-file-name) file)
  8683. (if regionp
  8684. (and (>= pos region-start)
  8685. (<= pos region-end))
  8686. (and (>= pos (point))
  8687. (< pos (save-excursion
  8688. (org-end-of-subtree t t))))))
  8689. (error "Cannot refile to position inside the tree or region"))
  8690. (setq nbuf (or (find-buffer-visiting file)
  8691. (find-file-noselect file)))
  8692. (if goto
  8693. (progn
  8694. (switch-to-buffer nbuf)
  8695. (goto-char pos)
  8696. (org-show-context 'org-goto))
  8697. (if regionp
  8698. (progn
  8699. (org-kill-new (buffer-substring region-start region-end))
  8700. (org-save-markers-in-region region-start region-end))
  8701. (org-copy-subtree 1 nil t))
  8702. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8703. (find-file-noselect file)))
  8704. (setq reversed (org-notes-order-reversed-p))
  8705. (save-excursion
  8706. (save-restriction
  8707. (widen)
  8708. (if pos
  8709. (progn
  8710. (goto-char pos)
  8711. (looking-at outline-regexp)
  8712. (setq level (org-get-valid-level (funcall outline-level) 1))
  8713. (goto-char
  8714. (if reversed
  8715. (or (outline-next-heading) (point-max))
  8716. (or (save-excursion (org-get-next-sibling))
  8717. (org-end-of-subtree t t)
  8718. (point-max)))))
  8719. (setq level 1)
  8720. (if (not reversed)
  8721. (goto-char (point-max))
  8722. (goto-char (point-min))
  8723. (or (outline-next-heading) (goto-char (point-max)))))
  8724. (if (not (bolp)) (newline))
  8725. (org-paste-subtree level)
  8726. (when org-log-refile
  8727. (org-add-log-setup 'refile nil nil 'findpos
  8728. org-log-refile)
  8729. (unless (eq org-log-refile 'note)
  8730. (save-excursion (org-add-log-note))))
  8731. (and org-auto-align-tags (org-set-tags nil t))
  8732. (bookmark-set "org-refile-last-stored")
  8733. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8734. (run-hooks 'org-after-refile-insert-hook))))
  8735. (if regionp
  8736. (delete-region (point) (+ (point) region-length))
  8737. (org-cut-subtree))
  8738. (when (featurep 'org-inlinetask)
  8739. (org-inlinetask-remove-END-maybe))
  8740. (setq org-markers-to-move nil)
  8741. (message "Refiled to \"%s\"" (car it)))))))
  8742. (defun org-refile-goto-last-stored ()
  8743. "Go to the location where the last refile was stored."
  8744. (interactive)
  8745. (bookmark-jump "org-refile-last-stored")
  8746. (message "This is the location of the last refile"))
  8747. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8748. "Prompt the user for a refile location, using PROMPT."
  8749. (let ((org-refile-targets org-refile-targets)
  8750. (org-refile-use-outline-path org-refile-use-outline-path))
  8751. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8752. (unless org-refile-target-table
  8753. (error "No refile targets"))
  8754. (let* ((cbuf (current-buffer))
  8755. (partial-completion-mode nil)
  8756. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8757. (cfunc (if (and org-refile-use-outline-path
  8758. org-outline-path-complete-in-steps)
  8759. 'org-olpath-completing-read
  8760. 'org-icompleting-read))
  8761. (extra (if org-refile-use-outline-path "/" ""))
  8762. (filename (and cfn (expand-file-name cfn)))
  8763. (tbl (mapcar
  8764. (lambda (x)
  8765. (if (and (not (member org-refile-use-outline-path
  8766. '(file full-file-path)))
  8767. (not (equal filename (nth 1 x))))
  8768. (cons (concat (car x) extra " ("
  8769. (file-name-nondirectory (nth 1 x)) ")")
  8770. (cdr x))
  8771. (cons (concat (car x) extra) (cdr x))))
  8772. org-refile-target-table))
  8773. (completion-ignore-case t)
  8774. pa answ parent-target child parent old-hist)
  8775. (setq old-hist org-refile-history)
  8776. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8777. nil 'org-refile-history))
  8778. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8779. (if pa
  8780. (progn
  8781. (when (or (not org-refile-history)
  8782. (not (eq old-hist org-refile-history))
  8783. (not (equal (car pa) (car org-refile-history))))
  8784. (setq org-refile-history
  8785. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8786. org-refile-history
  8787. (cdr org-refile-history))))
  8788. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8789. (pop org-refile-history)))
  8790. pa)
  8791. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8792. (progn
  8793. (setq parent (match-string 1 answ)
  8794. child (match-string 2 answ))
  8795. (setq parent-target (or (assoc parent tbl)
  8796. (assoc (concat parent "/") tbl)))
  8797. (when (and parent-target
  8798. (or (eq new-nodes t)
  8799. (and (eq new-nodes 'confirm)
  8800. (y-or-n-p (format "Create new node \"%s\"? "
  8801. child)))))
  8802. (org-refile-new-child parent-target child)))
  8803. (error "Invalid target location")))))
  8804. (defun org-refile-new-child (parent-target child)
  8805. "Use refile target PARENT-TARGET to add new CHILD below it."
  8806. (unless parent-target
  8807. (error "Cannot find parent for new node"))
  8808. (let ((file (nth 1 parent-target))
  8809. (pos (nth 3 parent-target))
  8810. level)
  8811. (with-current-buffer (or (find-buffer-visiting file)
  8812. (find-file-noselect file))
  8813. (save-excursion
  8814. (save-restriction
  8815. (widen)
  8816. (if pos
  8817. (goto-char pos)
  8818. (goto-char (point-max))
  8819. (if (not (bolp)) (newline)))
  8820. (when (looking-at outline-regexp)
  8821. (setq level (funcall outline-level))
  8822. (org-end-of-subtree t t))
  8823. (org-back-over-empty-lines)
  8824. (insert "\n" (make-string
  8825. (if pos (org-get-valid-level level 1) 1) ?*)
  8826. " " child "\n")
  8827. (beginning-of-line 0)
  8828. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8829. (defun org-olpath-completing-read (prompt collection &rest args)
  8830. "Read an outline path like a file name."
  8831. (let ((thetable collection)
  8832. (org-completion-use-ido nil) ; does not work with ido.
  8833. (org-completion-use-iswitchb nil)) ; or iswitchb
  8834. (apply
  8835. 'org-icompleting-read prompt
  8836. (lambda (string predicate &optional flag)
  8837. (let (rtn r f (l (length string)))
  8838. (cond
  8839. ((eq flag nil)
  8840. ;; try completion
  8841. (try-completion string thetable))
  8842. ((eq flag t)
  8843. ;; all-completions
  8844. (setq rtn (all-completions string thetable predicate))
  8845. (mapcar
  8846. (lambda (x)
  8847. (setq r (substring x l))
  8848. (if (string-match " ([^)]*)$" x)
  8849. (setq f (match-string 0 x))
  8850. (setq f ""))
  8851. (if (string-match "/" r)
  8852. (concat string (substring r 0 (match-end 0)) f)
  8853. x))
  8854. rtn))
  8855. ((eq flag 'lambda)
  8856. ;; exact match?
  8857. (assoc string thetable)))
  8858. ))
  8859. args)))
  8860. ;;;; Dynamic blocks
  8861. (defun org-find-dblock (name)
  8862. "Find the first dynamic block with name NAME in the buffer.
  8863. If not found, stay at current position and return nil."
  8864. (let (pos)
  8865. (save-excursion
  8866. (goto-char (point-min))
  8867. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8868. nil t)
  8869. (match-beginning 0))))
  8870. (if pos (goto-char pos))
  8871. pos))
  8872. (defconst org-dblock-start-re
  8873. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8874. "Matches the start line of a dynamic block, with parameters.")
  8875. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8876. "Matches the end of a dynamic block.")
  8877. (defun org-create-dblock (plist)
  8878. "Create a dynamic block section, with parameters taken from PLIST.
  8879. PLIST must contain a :name entry which is used as name of the block."
  8880. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8881. (end-of-line 1)
  8882. (newline))
  8883. (let ((col (current-column))
  8884. (name (plist-get plist :name)))
  8885. (insert "#+BEGIN: " name)
  8886. (while plist
  8887. (if (eq (car plist) :name)
  8888. (setq plist (cddr plist))
  8889. (insert " " (prin1-to-string (pop plist)))))
  8890. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8891. (beginning-of-line -2)))
  8892. (defun org-prepare-dblock ()
  8893. "Prepare dynamic block for refresh.
  8894. This empties the block, puts the cursor at the insert position and returns
  8895. the property list including an extra property :name with the block name."
  8896. (unless (looking-at org-dblock-start-re)
  8897. (error "Not at a dynamic block"))
  8898. (let* ((begdel (1+ (match-end 0)))
  8899. (name (org-no-properties (match-string 1)))
  8900. (params (append (list :name name)
  8901. (read (concat "(" (match-string 3) ")")))))
  8902. (save-excursion
  8903. (beginning-of-line 1)
  8904. (skip-chars-forward " \t")
  8905. (setq params (plist-put params :indentation-column (current-column))))
  8906. (unless (re-search-forward org-dblock-end-re nil t)
  8907. (error "Dynamic block not terminated"))
  8908. (setq params
  8909. (append params
  8910. (list :content (buffer-substring
  8911. begdel (match-beginning 0)))))
  8912. (delete-region begdel (match-beginning 0))
  8913. (goto-char begdel)
  8914. (open-line 1)
  8915. params))
  8916. (defun org-map-dblocks (&optional command)
  8917. "Apply COMMAND to all dynamic blocks in the current buffer.
  8918. If COMMAND is not given, use `org-update-dblock'."
  8919. (let ((cmd (or command 'org-update-dblock)))
  8920. (save-excursion
  8921. (goto-char (point-min))
  8922. (while (re-search-forward org-dblock-start-re nil t)
  8923. (goto-char (match-beginning 0))
  8924. (save-excursion
  8925. (condition-case nil
  8926. (funcall cmd)
  8927. (error (message "Error during update of dynamic block"))))
  8928. (unless (re-search-forward org-dblock-end-re nil t)
  8929. (error "Dynamic block not terminated"))))))
  8930. (defun org-dblock-update (&optional arg)
  8931. "User command for updating dynamic blocks.
  8932. Update the dynamic block at point. With prefix ARG, update all dynamic
  8933. blocks in the buffer."
  8934. (interactive "P")
  8935. (if arg
  8936. (org-update-all-dblocks)
  8937. (or (looking-at org-dblock-start-re)
  8938. (org-beginning-of-dblock))
  8939. (org-update-dblock)))
  8940. (defun org-update-dblock ()
  8941. "Update the dynamic block at point
  8942. This means to empty the block, parse for parameters and then call
  8943. the correct writing function."
  8944. (save-window-excursion
  8945. (let* ((pos (point))
  8946. (line (org-current-line))
  8947. (params (org-prepare-dblock))
  8948. (name (plist-get params :name))
  8949. (indent (plist-get params :indentation-column))
  8950. (cmd (intern (concat "org-dblock-write:" name))))
  8951. (message "Updating dynamic block `%s' at line %d..." name line)
  8952. (funcall cmd params)
  8953. (message "Updating dynamic block `%s' at line %d...done" name line)
  8954. (goto-char pos)
  8955. (when (and indent (> indent 0))
  8956. (setq indent (make-string indent ?\ ))
  8957. (save-excursion
  8958. (org-beginning-of-dblock)
  8959. (forward-line 1)
  8960. (while (not (looking-at org-dblock-end-re))
  8961. (insert indent)
  8962. (beginning-of-line 2))
  8963. (when (looking-at org-dblock-end-re)
  8964. (and (looking-at "[ \t]+")
  8965. (replace-match ""))
  8966. (insert indent)))))))
  8967. (defun org-beginning-of-dblock ()
  8968. "Find the beginning of the dynamic block at point.
  8969. Error if there is no such block at point."
  8970. (let ((pos (point))
  8971. beg)
  8972. (end-of-line 1)
  8973. (if (and (re-search-backward org-dblock-start-re nil t)
  8974. (setq beg (match-beginning 0))
  8975. (re-search-forward org-dblock-end-re nil t)
  8976. (> (match-end 0) pos))
  8977. (goto-char beg)
  8978. (goto-char pos)
  8979. (error "Not in a dynamic block"))))
  8980. (defun org-update-all-dblocks ()
  8981. "Update all dynamic blocks in the buffer.
  8982. This function can be used in a hook."
  8983. (when (org-mode-p)
  8984. (org-map-dblocks 'org-update-dblock)))
  8985. ;;;; Completion
  8986. (defconst org-additional-option-like-keywords
  8987. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8988. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8989. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8990. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8991. "BEGIN:" "END:"
  8992. "ORGTBL" "TBLFM:" "TBLNAME:"
  8993. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8994. "BEGIN_QUOTE" "END_QUOTE"
  8995. "BEGIN_VERSE" "END_VERSE"
  8996. "BEGIN_CENTER" "END_CENTER"
  8997. "BEGIN_SRC" "END_SRC"
  8998. "CATEGORY" "COLUMNS"
  8999. "CAPTION" "LABEL"
  9000. "SETUPFILE"
  9001. "BIND"
  9002. "MACRO"))
  9003. (defcustom org-structure-template-alist
  9004. '(
  9005. ("s" "#+begin_src ?\n\n#+end_src"
  9006. "<src lang=\"?\">\n\n</src>")
  9007. ("e" "#+begin_example\n?\n#+end_example"
  9008. "<example>\n?\n</example>")
  9009. ("q" "#+begin_quote\n?\n#+end_quote"
  9010. "<quote>\n?\n</quote>")
  9011. ("v" "#+begin_verse\n?\n#+end_verse"
  9012. "<verse>\n?\n/verse>")
  9013. ("c" "#+begin_center\n?\n#+end_center"
  9014. "<center>\n?\n/center>")
  9015. ("l" "#+begin_latex\n?\n#+end_latex"
  9016. "<literal style=\"latex\">\n?\n</literal>")
  9017. ("L" "#+latex: "
  9018. "<literal style=\"latex\">?</literal>")
  9019. ("h" "#+begin_html\n?\n#+end_html"
  9020. "<literal style=\"html\">\n?\n</literal>")
  9021. ("H" "#+html: "
  9022. "<literal style=\"html\">?</literal>")
  9023. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9024. ("A" "#+ascii: ")
  9025. ("i" "#+include %file ?"
  9026. "<include file=%file markup=\"?\">")
  9027. )
  9028. "Structure completion elements.
  9029. This is a list of abbreviation keys and values. The value gets inserted
  9030. if you type `<' followed by the key and then press the completion key,
  9031. usually `M-TAB'. %file will be replaced by a file name after prompting
  9032. for the file using completion.
  9033. There are two templates for each key, the first uses the original Org syntax,
  9034. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9035. the default when the /org-mtags.el/ module has been loaded. See also the
  9036. variable `org-mtags-prefer-muse-templates'.
  9037. This is an experimental feature, it is undecided if it is going to stay in."
  9038. :group 'org-completion
  9039. :type '(repeat
  9040. (string :tag "Key")
  9041. (string :tag "Template")
  9042. (string :tag "Muse Template")))
  9043. (defun org-try-structure-completion ()
  9044. "Try to complete a structure template before point.
  9045. This looks for strings like \"<e\" on an otherwise empty line and
  9046. expands them."
  9047. (let ((l (buffer-substring (point-at-bol) (point)))
  9048. a)
  9049. (when (and (looking-at "[ \t]*$")
  9050. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9051. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9052. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9053. (match-beginning 1)) a)
  9054. t)))
  9055. (defun org-complete-expand-structure-template (start cell)
  9056. "Expand a structure template."
  9057. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9058. (rpl (nth (if musep 2 1) cell))
  9059. (ind ""))
  9060. (delete-region start (point))
  9061. (when (string-match "\\`#\\+" rpl)
  9062. (cond
  9063. ((bolp))
  9064. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9065. (setq ind (buffer-substring (point-at-bol) (point))))
  9066. (t (newline))))
  9067. (setq start (point))
  9068. (if (string-match "%file" rpl)
  9069. (setq rpl (replace-match
  9070. (concat
  9071. "\""
  9072. (save-match-data
  9073. (abbreviate-file-name (read-file-name "Include file: ")))
  9074. "\"")
  9075. t t rpl)))
  9076. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9077. (concat "\n" ind)))
  9078. (insert rpl)
  9079. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9080. (defun org-complete (&optional arg)
  9081. "Perform completion on word at point.
  9082. At the beginning of a headline, this completes TODO keywords as given in
  9083. `org-todo-keywords'.
  9084. If the current word is preceded by a backslash, completes the TeX symbols
  9085. that are supported for HTML support.
  9086. If the current word is preceded by \"#+\", completes special words for
  9087. setting file options.
  9088. In the line after \"#+STARTUP:, complete valid keywords.\"
  9089. At all other locations, this simply calls the value of
  9090. `org-completion-fallback-command'."
  9091. (interactive "P")
  9092. (org-without-partial-completion
  9093. (catch 'exit
  9094. (let* ((a nil)
  9095. (end (point))
  9096. (beg1 (save-excursion
  9097. (skip-chars-backward (org-re "[:alnum:]_@"))
  9098. (point)))
  9099. (beg (save-excursion
  9100. (skip-chars-backward "a-zA-Z0-9_:$")
  9101. (point)))
  9102. (confirm (lambda (x) (stringp (car x))))
  9103. (searchhead (equal (char-before beg) ?*))
  9104. (struct
  9105. (when (and (member (char-before beg1) '(?. ?<))
  9106. (setq a (assoc (buffer-substring beg1 (point))
  9107. org-structure-template-alist)))
  9108. (org-complete-expand-structure-template (1- beg1) a)
  9109. (throw 'exit t)))
  9110. (tag (and (equal (char-before beg1) ?:)
  9111. (equal (char-after (point-at-bol)) ?*)))
  9112. (prop (and (equal (char-before beg1) ?:)
  9113. (not (equal (char-after (point-at-bol)) ?*))))
  9114. (texp (equal (char-before beg) ?\\))
  9115. (link (equal (char-before beg) ?\[))
  9116. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9117. beg)
  9118. "#+"))
  9119. (startup (string-match "^#\\+STARTUP:.*"
  9120. (buffer-substring (point-at-bol) (point))))
  9121. (completion-ignore-case opt)
  9122. (type nil)
  9123. (tbl nil)
  9124. (table (cond
  9125. (opt
  9126. (setq type :opt)
  9127. (require 'org-exp)
  9128. (append
  9129. (delq nil
  9130. (mapcar
  9131. (lambda (x)
  9132. (if (string-match
  9133. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9134. (cons (match-string 2 x)
  9135. (match-string 1 x))))
  9136. (org-split-string (org-get-current-options) "\n")))
  9137. (mapcar 'list org-additional-option-like-keywords)))
  9138. (startup
  9139. (setq type :startup)
  9140. org-startup-options)
  9141. (link (append org-link-abbrev-alist-local
  9142. org-link-abbrev-alist))
  9143. (texp
  9144. (setq type :tex)
  9145. (append org-entities-user org-entities))
  9146. ((string-match "\\`\\*+[ \t]+\\'"
  9147. (buffer-substring (point-at-bol) beg))
  9148. (setq type :todo)
  9149. (mapcar 'list org-todo-keywords-1))
  9150. (searchhead
  9151. (setq type :searchhead)
  9152. (save-excursion
  9153. (goto-char (point-min))
  9154. (while (re-search-forward org-todo-line-regexp nil t)
  9155. (push (list
  9156. (org-make-org-heading-search-string
  9157. (match-string 3) t))
  9158. tbl)))
  9159. tbl)
  9160. (tag (setq type :tag beg beg1)
  9161. (or org-tag-alist (org-get-buffer-tags)))
  9162. (prop (setq type :prop beg beg1)
  9163. (mapcar 'list (org-buffer-property-keys nil t t)))
  9164. (t (progn
  9165. (call-interactively org-completion-fallback-command)
  9166. (throw 'exit nil)))))
  9167. (pattern (buffer-substring-no-properties beg end))
  9168. (completion (try-completion pattern table confirm)))
  9169. (cond ((eq completion t)
  9170. (if (not (assoc (upcase pattern) table))
  9171. (message "Already complete")
  9172. (if (and (equal type :opt)
  9173. (not (member (car (assoc (upcase pattern) table))
  9174. org-additional-option-like-keywords)))
  9175. (insert (substring (cdr (assoc (upcase pattern) table))
  9176. (length pattern)))
  9177. (if (memq type '(:tag :prop)) (insert ":")))))
  9178. ((null completion)
  9179. (message "Can't find completion for \"%s\"" pattern)
  9180. (ding))
  9181. ((not (string= pattern completion))
  9182. (delete-region beg end)
  9183. (if (string-match " +$" completion)
  9184. (setq completion (replace-match "" t t completion)))
  9185. (insert completion)
  9186. (if (get-buffer-window "*Completions*")
  9187. (delete-window (get-buffer-window "*Completions*")))
  9188. (if (assoc completion table)
  9189. (if (eq type :todo) (insert " ")
  9190. (if (memq type '(:tag :prop)) (insert ":"))))
  9191. (if (and (equal type :opt) (assoc completion table))
  9192. (message "%s" (substitute-command-keys
  9193. "Press \\[org-complete] again to insert example settings"))))
  9194. (t
  9195. (message "Making completion list...")
  9196. (let ((list (sort (all-completions pattern table confirm)
  9197. 'string<)))
  9198. (with-output-to-temp-buffer "*Completions*"
  9199. (condition-case nil
  9200. ;; Protection needed for XEmacs and emacs 21
  9201. (display-completion-list list pattern)
  9202. (error (display-completion-list list)))))
  9203. (message "Making completion list...%s" "done")))))))
  9204. ;;;; TODO, DEADLINE, Comments
  9205. (defun org-toggle-comment ()
  9206. "Change the COMMENT state of an entry."
  9207. (interactive)
  9208. (save-excursion
  9209. (org-back-to-heading)
  9210. (let (case-fold-search)
  9211. (if (looking-at (concat outline-regexp
  9212. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9213. (replace-match "" t t nil 1)
  9214. (if (looking-at outline-regexp)
  9215. (progn
  9216. (goto-char (match-end 0))
  9217. (insert org-comment-string " ")))))))
  9218. (defvar org-last-todo-state-is-todo nil
  9219. "This is non-nil when the last TODO state change led to a TODO state.
  9220. If the last change removed the TODO tag or switched to DONE, then
  9221. this is nil.")
  9222. (defvar org-setting-tags nil) ; dynamically skipped
  9223. (defun org-parse-local-options (string var)
  9224. "Parse STRING for startup setting relevant for variable VAR."
  9225. (let ((rtn (symbol-value var))
  9226. e opts)
  9227. (save-match-data
  9228. (if (or (not string) (not (string-match "\\S-" string)))
  9229. rtn
  9230. (setq opts (delq nil (mapcar (lambda (x)
  9231. (setq e (assoc x org-startup-options))
  9232. (if (eq (nth 1 e) var) e nil))
  9233. (org-split-string string "[ \t]+"))))
  9234. (if (not opts)
  9235. rtn
  9236. (setq rtn nil)
  9237. (while (setq e (pop opts))
  9238. (if (not (nth 3 e))
  9239. (setq rtn (nth 2 e))
  9240. (if (not (listp rtn)) (setq rtn nil))
  9241. (push (nth 2 e) rtn)))
  9242. rtn)))))
  9243. (defvar org-todo-setup-filter-hook nil
  9244. "Hook for functions that pre-filter todo specs.
  9245. Each function takes a todo spec and returns either `nil' or the spec
  9246. transformed into canonical form." )
  9247. (defvar org-todo-get-default-hook nil
  9248. "Hook for functions that get a default item for todo.
  9249. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9250. `nil' or a string to be used for the todo mark." )
  9251. (defvar org-agenda-headline-snapshot-before-repeat)
  9252. (defun org-todo (&optional arg)
  9253. "Change the TODO state of an item.
  9254. The state of an item is given by a keyword at the start of the heading,
  9255. like
  9256. *** TODO Write paper
  9257. *** DONE Call mom
  9258. The different keywords are specified in the variable `org-todo-keywords'.
  9259. By default the available states are \"TODO\" and \"DONE\".
  9260. So for this example: when the item starts with TODO, it is changed to DONE.
  9261. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9262. DONE are present, add TODO at the beginning of the heading.
  9263. With C-u prefix arg, use completion to determine the new state.
  9264. With numeric prefix arg, switch to that state.
  9265. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9266. With a triple C-u prefix, circumvent any state blocking.
  9267. For calling through lisp, arg is also interpreted in the following way:
  9268. 'none -> empty state
  9269. \"\"(empty string) -> switch to empty state
  9270. 'done -> switch to DONE
  9271. 'nextset -> switch to the next set of keywords
  9272. 'previousset -> switch to the previous set of keywords
  9273. \"WAITING\" -> switch to the specified keyword, but only if it
  9274. really is a member of `org-todo-keywords'."
  9275. (interactive "P")
  9276. (if (equal arg '(16)) (setq arg 'nextset))
  9277. (let ((org-blocker-hook org-blocker-hook)
  9278. (case-fold-search nil))
  9279. (when (equal arg '(64))
  9280. (setq arg nil org-blocker-hook nil))
  9281. (when (and org-blocker-hook
  9282. (or org-inhibit-blocking
  9283. (org-entry-get nil "NOBLOCKING")))
  9284. (setq org-blocker-hook nil))
  9285. (save-excursion
  9286. (catch 'exit
  9287. (org-back-to-heading t)
  9288. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9289. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9290. (looking-at " *"))
  9291. (let* ((match-data (match-data))
  9292. (startpos (point-at-bol))
  9293. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9294. (org-log-done org-log-done)
  9295. (org-log-repeat org-log-repeat)
  9296. (org-todo-log-states org-todo-log-states)
  9297. (this (match-string 1))
  9298. (hl-pos (match-beginning 0))
  9299. (head (org-get-todo-sequence-head this))
  9300. (ass (assoc head org-todo-kwd-alist))
  9301. (interpret (nth 1 ass))
  9302. (done-word (nth 3 ass))
  9303. (final-done-word (nth 4 ass))
  9304. (last-state (or this ""))
  9305. (completion-ignore-case t)
  9306. (member (member this org-todo-keywords-1))
  9307. (tail (cdr member))
  9308. (state (cond
  9309. ((and org-todo-key-trigger
  9310. (or (and (equal arg '(4))
  9311. (eq org-use-fast-todo-selection 'prefix))
  9312. (and (not arg) org-use-fast-todo-selection
  9313. (not (eq org-use-fast-todo-selection
  9314. 'prefix)))))
  9315. ;; Use fast selection
  9316. (org-fast-todo-selection))
  9317. ((and (equal arg '(4))
  9318. (or (not org-use-fast-todo-selection)
  9319. (not org-todo-key-trigger)))
  9320. ;; Read a state with completion
  9321. (org-icompleting-read
  9322. "State: " (mapcar (lambda(x) (list x))
  9323. org-todo-keywords-1)
  9324. nil t))
  9325. ((eq arg 'right)
  9326. (if this
  9327. (if tail (car tail) nil)
  9328. (car org-todo-keywords-1)))
  9329. ((eq arg 'left)
  9330. (if (equal member org-todo-keywords-1)
  9331. nil
  9332. (if this
  9333. (nth (- (length org-todo-keywords-1)
  9334. (length tail) 2)
  9335. org-todo-keywords-1)
  9336. (org-last org-todo-keywords-1))))
  9337. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9338. (setq arg nil))) ; hack to fall back to cycling
  9339. (arg
  9340. ;; user or caller requests a specific state
  9341. (cond
  9342. ((equal arg "") nil)
  9343. ((eq arg 'none) nil)
  9344. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9345. ((eq arg 'nextset)
  9346. (or (car (cdr (member head org-todo-heads)))
  9347. (car org-todo-heads)))
  9348. ((eq arg 'previousset)
  9349. (let ((org-todo-heads (reverse org-todo-heads)))
  9350. (or (car (cdr (member head org-todo-heads)))
  9351. (car org-todo-heads))))
  9352. ((car (member arg org-todo-keywords-1)))
  9353. ((stringp arg)
  9354. (error "State `%s' not valid in this file" arg))
  9355. ((nth (1- (prefix-numeric-value arg))
  9356. org-todo-keywords-1))))
  9357. ((null member) (or head (car org-todo-keywords-1)))
  9358. ((equal this final-done-word) nil) ;; -> make empty
  9359. ((null tail) nil) ;; -> first entry
  9360. ((memq interpret '(type priority))
  9361. (if (eq this-command last-command)
  9362. (car tail)
  9363. (if (> (length tail) 0)
  9364. (or done-word (car org-done-keywords))
  9365. nil)))
  9366. (t
  9367. (car tail))))
  9368. (state (or
  9369. (run-hook-with-args-until-success
  9370. 'org-todo-get-default-hook state last-state)
  9371. state))
  9372. (next (if state (concat " " state " ") " "))
  9373. (change-plist (list :type 'todo-state-change :from this :to state
  9374. :position startpos))
  9375. dolog now-done-p)
  9376. (when org-blocker-hook
  9377. (setq org-last-todo-state-is-todo
  9378. (not (member this org-done-keywords)))
  9379. (unless (save-excursion
  9380. (save-match-data
  9381. (run-hook-with-args-until-failure
  9382. 'org-blocker-hook change-plist)))
  9383. (if (interactive-p)
  9384. (error "TODO state change from %s to %s blocked" this state)
  9385. ;; fail silently
  9386. (message "TODO state change from %s to %s blocked" this state)
  9387. (throw 'exit nil))))
  9388. (store-match-data match-data)
  9389. (replace-match next t t)
  9390. (unless (pos-visible-in-window-p hl-pos)
  9391. (message "TODO state changed to %s" (org-trim next)))
  9392. (unless head
  9393. (setq head (org-get-todo-sequence-head state)
  9394. ass (assoc head org-todo-kwd-alist)
  9395. interpret (nth 1 ass)
  9396. done-word (nth 3 ass)
  9397. final-done-word (nth 4 ass)))
  9398. (when (memq arg '(nextset previousset))
  9399. (message "Keyword-Set %d/%d: %s"
  9400. (- (length org-todo-sets) -1
  9401. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9402. (length org-todo-sets)
  9403. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9404. (setq org-last-todo-state-is-todo
  9405. (not (member state org-done-keywords)))
  9406. (setq now-done-p (and (member state org-done-keywords)
  9407. (not (member this org-done-keywords))))
  9408. (and logging (org-local-logging logging))
  9409. (when (and (or org-todo-log-states org-log-done)
  9410. (not (eq org-inhibit-logging t))
  9411. (not (memq arg '(nextset previousset))))
  9412. ;; we need to look at recording a time and note
  9413. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9414. (nth 2 (assoc this org-todo-log-states))))
  9415. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9416. (setq dolog 'time))
  9417. (when (and state
  9418. (member state org-not-done-keywords)
  9419. (not (member this org-not-done-keywords)))
  9420. ;; This is now a todo state and was not one before
  9421. ;; If there was a CLOSED time stamp, get rid of it.
  9422. (org-add-planning-info nil nil 'closed))
  9423. (when (and now-done-p org-log-done)
  9424. ;; It is now done, and it was not done before
  9425. (org-add-planning-info 'closed (org-current-time))
  9426. (if (and (not dolog) (eq 'note org-log-done))
  9427. (org-add-log-setup 'done state this 'findpos 'note)))
  9428. (when (and state dolog)
  9429. ;; This is a non-nil state, and we need to log it
  9430. (org-add-log-setup 'state state this 'findpos dolog)))
  9431. ;; Fixup tag positioning
  9432. (org-todo-trigger-tag-changes state)
  9433. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9434. (when org-provide-todo-statistics
  9435. (org-update-parent-todo-statistics))
  9436. (run-hooks 'org-after-todo-state-change-hook)
  9437. (if (and arg (not (member state org-done-keywords)))
  9438. (setq head (org-get-todo-sequence-head state)))
  9439. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9440. ;; Do we need to trigger a repeat?
  9441. (when now-done-p
  9442. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9443. ;; This is for the agenda, take a snapshot of the headline.
  9444. (save-match-data
  9445. (setq org-agenda-headline-snapshot-before-repeat
  9446. (org-get-heading))))
  9447. (org-auto-repeat-maybe state))
  9448. ;; Fixup cursor location if close to the keyword
  9449. (if (and (outline-on-heading-p)
  9450. (not (bolp))
  9451. (save-excursion (beginning-of-line 1)
  9452. (looking-at org-todo-line-regexp))
  9453. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9454. (progn
  9455. (goto-char (or (match-end 2) (match-end 1)))
  9456. (and (looking-at " ") (just-one-space))))
  9457. (when org-trigger-hook
  9458. (save-excursion
  9459. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9460. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9461. "Block turning an entry into a TODO, using the hierarchy.
  9462. This checks whether the current task should be blocked from state
  9463. changes. Such blocking occurs when:
  9464. 1. The task has children which are not all in a completed state.
  9465. 2. A task has a parent with the property :ORDERED:, and there
  9466. are siblings prior to the current task with incomplete
  9467. status.
  9468. 3. The parent of the task is blocked because it has siblings that should
  9469. be done first, or is child of a block grandparent TODO entry."
  9470. (if (not org-enforce-todo-dependencies)
  9471. t ; if locally turned off don't block
  9472. (catch 'dont-block
  9473. ;; If this is not a todo state change, or if this entry is already DONE,
  9474. ;; do not block
  9475. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9476. (member (plist-get change-plist :from)
  9477. (cons 'done org-done-keywords))
  9478. (member (plist-get change-plist :to)
  9479. (cons 'todo org-not-done-keywords))
  9480. (not (plist-get change-plist :to)))
  9481. (throw 'dont-block t))
  9482. ;; If this task has children, and any are undone, it's blocked
  9483. (save-excursion
  9484. (org-back-to-heading t)
  9485. (let ((this-level (funcall outline-level)))
  9486. (outline-next-heading)
  9487. (let ((child-level (funcall outline-level)))
  9488. (while (and (not (eobp))
  9489. (> child-level this-level))
  9490. ;; this todo has children, check whether they are all
  9491. ;; completed
  9492. (if (and (not (org-entry-is-done-p))
  9493. (org-entry-is-todo-p))
  9494. (throw 'dont-block nil))
  9495. (outline-next-heading)
  9496. (setq child-level (funcall outline-level))))))
  9497. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9498. ;; any previous siblings are undone, it's blocked
  9499. (save-excursion
  9500. (org-back-to-heading t)
  9501. (let* ((pos (point))
  9502. (parent-pos (and (org-up-heading-safe) (point))))
  9503. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9504. (when (and (org-entry-get (point) "ORDERED")
  9505. (forward-line 1)
  9506. (re-search-forward org-not-done-heading-regexp pos t))
  9507. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9508. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9509. (while t
  9510. (goto-char parent-pos)
  9511. (if (not (looking-at org-not-done-heading-regexp))
  9512. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9513. (setq pos (point))
  9514. (setq parent-pos (and (org-up-heading-safe) (point)))
  9515. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9516. (when (and (org-entry-get (point) "ORDERED")
  9517. (forward-line 1)
  9518. (re-search-forward org-not-done-heading-regexp pos t))
  9519. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9520. (defcustom org-track-ordered-property-with-tag nil
  9521. "Should the ORDERED property also be shown as a tag?
  9522. The ORDERED property decides if an entry should require subtasks to be
  9523. completed in sequence. Since a property is not very visible, setting
  9524. this option means that toggling the ORDERED property with the command
  9525. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9526. not relevant for the behavior, but it makes things more visible.
  9527. Note that toggling the tag with tags commands will not change the property
  9528. and therefore not influence behavior!
  9529. This can be t, meaning the tag ORDERED should be used, It can also be a
  9530. string to select a different tag for this task."
  9531. :group 'org-todo
  9532. :type '(choice
  9533. (const :tag "No tracking" nil)
  9534. (const :tag "Track with ORDERED tag" t)
  9535. (string :tag "Use other tag")))
  9536. (defun org-toggle-ordered-property ()
  9537. "Toggle the ORDERED property of the current entry.
  9538. For better visibility, you can track the value of this property with a tag.
  9539. See variable `org-track-ordered-property-with-tag'."
  9540. (interactive)
  9541. (let* ((t1 org-track-ordered-property-with-tag)
  9542. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9543. (save-excursion
  9544. (org-back-to-heading)
  9545. (if (org-entry-get nil "ORDERED")
  9546. (progn
  9547. (org-delete-property "ORDERED")
  9548. (and tag (org-toggle-tag tag 'off))
  9549. (message "Subtasks can be completed in arbitrary order"))
  9550. (org-entry-put nil "ORDERED" "t")
  9551. (and tag (org-toggle-tag tag 'on))
  9552. (message "Subtasks must be completed in sequence")))))
  9553. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9554. (defun org-block-todo-from-checkboxes (change-plist)
  9555. "Block turning an entry into a TODO, using checkboxes.
  9556. This checks whether the current task should be blocked from state
  9557. changes because there are unchecked boxes in this entry."
  9558. (if (not org-enforce-todo-checkbox-dependencies)
  9559. t ; if locally turned off don't block
  9560. (catch 'dont-block
  9561. ;; If this is not a todo state change, or if this entry is already DONE,
  9562. ;; do not block
  9563. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9564. (member (plist-get change-plist :from)
  9565. (cons 'done org-done-keywords))
  9566. (member (plist-get change-plist :to)
  9567. (cons 'todo org-not-done-keywords))
  9568. (not (plist-get change-plist :to)))
  9569. (throw 'dont-block t))
  9570. ;; If this task has checkboxes that are not checked, it's blocked
  9571. (save-excursion
  9572. (org-back-to-heading t)
  9573. (let ((beg (point)) end)
  9574. (outline-next-heading)
  9575. (setq end (point))
  9576. (goto-char beg)
  9577. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9578. end t)
  9579. (progn
  9580. (if (boundp 'org-blocked-by-checkboxes)
  9581. (setq org-blocked-by-checkboxes t))
  9582. (throw 'dont-block nil)))))
  9583. t))) ; do not block
  9584. (defun org-entry-blocked-p ()
  9585. "Is the current entry blocked?"
  9586. (if (org-entry-get nil "NOBLOCKING")
  9587. nil ;; Never block this entry
  9588. (not
  9589. (run-hook-with-args-until-failure
  9590. 'org-blocker-hook
  9591. (list :type 'todo-state-change
  9592. :position (point)
  9593. :from 'todo
  9594. :to 'done)))))
  9595. (defun org-update-statistics-cookies (all)
  9596. "Update the statistics cookie, either from TODO or from checkboxes.
  9597. This should be called with the cursor in a line with a statistics cookie."
  9598. (interactive "P")
  9599. (if all
  9600. (progn
  9601. (org-update-checkbox-count 'all)
  9602. (org-map-entries 'org-update-parent-todo-statistics))
  9603. (if (not (org-on-heading-p))
  9604. (org-update-checkbox-count)
  9605. (let ((pos (move-marker (make-marker) (point)))
  9606. end l1 l2)
  9607. (ignore-errors (org-back-to-heading t))
  9608. (if (not (org-on-heading-p))
  9609. (org-update-checkbox-count)
  9610. (setq l1 (org-outline-level))
  9611. (setq end (save-excursion
  9612. (outline-next-heading)
  9613. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9614. (point)))
  9615. (if (and (save-excursion
  9616. (re-search-forward
  9617. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9618. (not (save-excursion (re-search-forward
  9619. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9620. (org-update-checkbox-count)
  9621. (if (and l2 (> l2 l1))
  9622. (progn
  9623. (goto-char end)
  9624. (org-update-parent-todo-statistics))
  9625. (goto-char pos)
  9626. (beginning-of-line 1)
  9627. (while (re-search-forward
  9628. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9629. (point-at-eol) t)
  9630. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9631. (goto-char pos)
  9632. (move-marker pos nil)))))
  9633. (defvar org-entry-property-inherited-from) ;; defined below
  9634. (defun org-update-parent-todo-statistics ()
  9635. "Update any statistics cookie in the parent of the current headline.
  9636. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9637. the entire subtree and this will travel up the hierarchy and update
  9638. statistics everywhere."
  9639. (interactive)
  9640. (let* ((lim 0) prop
  9641. (recursive (or (not org-hierarchical-todo-statistics)
  9642. (string-match
  9643. "\\<recursive\\>"
  9644. (or (setq prop (org-entry-get
  9645. nil "COOKIE_DATA" 'inherit)) ""))))
  9646. (lim (or (and prop (marker-position
  9647. org-entry-property-inherited-from))
  9648. lim))
  9649. (first t)
  9650. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9651. level ltoggle l1 new ndel
  9652. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9653. (catch 'exit
  9654. (save-excursion
  9655. (beginning-of-line 1)
  9656. (if (org-at-heading-p)
  9657. (setq ltoggle (funcall outline-level))
  9658. (error "This should not happen"))
  9659. (while (and (setq level (org-up-heading-safe))
  9660. (or recursive first)
  9661. (>= (point) lim))
  9662. (setq first nil cookie-present nil)
  9663. (unless (and level
  9664. (not (string-match
  9665. "\\<checkbox\\>"
  9666. (downcase
  9667. (or (org-entry-get
  9668. nil "COOKIE_DATA")
  9669. "")))))
  9670. (throw 'exit nil))
  9671. (while (re-search-forward box-re (point-at-eol) t)
  9672. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9673. (setq is-percent (match-end 2))
  9674. (save-match-data
  9675. (unless (outline-next-heading) (throw 'exit nil))
  9676. (while (and (looking-at org-complex-heading-regexp)
  9677. (> (setq l1 (length (match-string 1))) level))
  9678. (setq kwd (and (or recursive (= l1 ltoggle))
  9679. (match-string 2)))
  9680. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9681. (and (listp org-provide-todo-statistics)
  9682. (or (member kwd org-provide-todo-statistics)
  9683. (member kwd org-done-keywords))))
  9684. (setq cnt-all (1+ cnt-all))
  9685. (if (eq org-provide-todo-statistics t)
  9686. (and kwd (setq cnt-all (1+ cnt-all)))))
  9687. (and (member kwd org-done-keywords)
  9688. (setq cnt-done (1+ cnt-done)))
  9689. (outline-next-heading)))
  9690. (setq new
  9691. (if is-percent
  9692. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9693. (format "[%d/%d]" cnt-done cnt-all))
  9694. ndel (- (match-end 0) (match-beginning 0)))
  9695. (goto-char (match-beginning 0))
  9696. (insert new)
  9697. (delete-region (point) (+ (point) ndel)))
  9698. (when cookie-present
  9699. (run-hook-with-args 'org-after-todo-statistics-hook
  9700. cnt-done (- cnt-all cnt-done))))))
  9701. (run-hooks 'org-todo-statistics-hook)))
  9702. (defvar org-after-todo-statistics-hook nil
  9703. "Hook that is called after a TODO statistics cookie has been updated.
  9704. Each function is called with two arguments: the number of not-done entries
  9705. and the number of done entries.
  9706. For example, the following function, when added to this hook, will switch
  9707. an entry to DONE when all children are done, and back to TODO when new
  9708. entries are set to a TODO status. Note that this hook is only called
  9709. when there is a statistics cookie in the headline!
  9710. (defun org-summary-todo (n-done n-not-done)
  9711. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9712. (let (org-log-done org-log-states) ; turn off logging
  9713. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9714. ")
  9715. (defvar org-todo-statistics-hook nil
  9716. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9717. This hook runs even if there is no statistics cookie present, in which case
  9718. `org-after-todo-statistics-hook' would not run.")
  9719. (defun org-todo-trigger-tag-changes (state)
  9720. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9721. (let ((l org-todo-state-tags-triggers)
  9722. changes)
  9723. (when (or (not state) (equal state ""))
  9724. (setq changes (append changes (cdr (assoc "" l)))))
  9725. (when (and (stringp state) (> (length state) 0))
  9726. (setq changes (append changes (cdr (assoc state l)))))
  9727. (when (member state org-not-done-keywords)
  9728. (setq changes (append changes (cdr (assoc 'todo l)))))
  9729. (when (member state org-done-keywords)
  9730. (setq changes (append changes (cdr (assoc 'done l)))))
  9731. (dolist (c changes)
  9732. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9733. (defun org-local-logging (value)
  9734. "Get logging settings from a property VALUE."
  9735. (let* (words w a)
  9736. ;; directly set the variables, they are already local.
  9737. (setq org-log-done nil
  9738. org-log-repeat nil
  9739. org-todo-log-states nil)
  9740. (setq words (org-split-string value))
  9741. (while (setq w (pop words))
  9742. (cond
  9743. ((setq a (assoc w org-startup-options))
  9744. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9745. (set (nth 1 a) (nth 2 a))))
  9746. ((setq a (org-extract-log-state-settings w))
  9747. (and (member (car a) org-todo-keywords-1)
  9748. (push a org-todo-log-states)))))))
  9749. (defun org-get-todo-sequence-head (kwd)
  9750. "Return the head of the TODO sequence to which KWD belongs.
  9751. If KWD is not set, check if there is a text property remembering the
  9752. right sequence."
  9753. (let (p)
  9754. (cond
  9755. ((not kwd)
  9756. (or (get-text-property (point-at-bol) 'org-todo-head)
  9757. (progn
  9758. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9759. nil (point-at-eol)))
  9760. (get-text-property p 'org-todo-head))))
  9761. ((not (member kwd org-todo-keywords-1))
  9762. (car org-todo-keywords-1))
  9763. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9764. (defun org-fast-todo-selection ()
  9765. "Fast TODO keyword selection with single keys.
  9766. Returns the new TODO keyword, or nil if no state change should occur."
  9767. (let* ((fulltable org-todo-key-alist)
  9768. (done-keywords org-done-keywords) ;; needed for the faces.
  9769. (maxlen (apply 'max (mapcar
  9770. (lambda (x)
  9771. (if (stringp (car x)) (string-width (car x)) 0))
  9772. fulltable)))
  9773. (expert nil)
  9774. (fwidth (+ maxlen 3 1 3))
  9775. (ncol (/ (- (window-width) 4) fwidth))
  9776. tg cnt e c tbl
  9777. groups ingroup)
  9778. (save-excursion
  9779. (save-window-excursion
  9780. (if expert
  9781. (set-buffer (get-buffer-create " *Org todo*"))
  9782. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9783. (erase-buffer)
  9784. (org-set-local 'org-done-keywords done-keywords)
  9785. (setq tbl fulltable cnt 0)
  9786. (while (setq e (pop tbl))
  9787. (cond
  9788. ((equal e '(:startgroup))
  9789. (push '() groups) (setq ingroup t)
  9790. (when (not (= cnt 0))
  9791. (setq cnt 0)
  9792. (insert "\n"))
  9793. (insert "{ "))
  9794. ((equal e '(:endgroup))
  9795. (setq ingroup nil cnt 0)
  9796. (insert "}\n"))
  9797. ((equal e '(:newline))
  9798. (when (not (= cnt 0))
  9799. (setq cnt 0)
  9800. (insert "\n")
  9801. (setq e (car tbl))
  9802. (while (equal (car tbl) '(:newline))
  9803. (insert "\n")
  9804. (setq tbl (cdr tbl)))))
  9805. (t
  9806. (setq tg (car e) c (cdr e))
  9807. (if ingroup (push tg (car groups)))
  9808. (setq tg (org-add-props tg nil 'face
  9809. (org-get-todo-face tg)))
  9810. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9811. (insert "[" c "] " tg (make-string
  9812. (- fwidth 4 (length tg)) ?\ ))
  9813. (when (= (setq cnt (1+ cnt)) ncol)
  9814. (insert "\n")
  9815. (if ingroup (insert " "))
  9816. (setq cnt 0)))))
  9817. (insert "\n")
  9818. (goto-char (point-min))
  9819. (if (not expert) (org-fit-window-to-buffer))
  9820. (message "[a-z..]:Set [SPC]:clear")
  9821. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9822. (cond
  9823. ((or (= c ?\C-g)
  9824. (and (= c ?q) (not (rassoc c fulltable))))
  9825. (setq quit-flag t))
  9826. ((= c ?\ ) nil)
  9827. ((setq e (rassoc c fulltable) tg (car e))
  9828. tg)
  9829. (t (setq quit-flag t)))))))
  9830. (defun org-entry-is-todo-p ()
  9831. (member (org-get-todo-state) org-not-done-keywords))
  9832. (defun org-entry-is-done-p ()
  9833. (member (org-get-todo-state) org-done-keywords))
  9834. (defun org-get-todo-state ()
  9835. (save-excursion
  9836. (org-back-to-heading t)
  9837. (and (looking-at org-todo-line-regexp)
  9838. (match-end 2)
  9839. (match-string 2))))
  9840. (defun org-at-date-range-p (&optional inactive-ok)
  9841. "Is the cursor inside a date range?"
  9842. (interactive)
  9843. (save-excursion
  9844. (catch 'exit
  9845. (let ((pos (point)))
  9846. (skip-chars-backward "^[<\r\n")
  9847. (skip-chars-backward "<[")
  9848. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9849. (>= (match-end 0) pos)
  9850. (throw 'exit t))
  9851. (skip-chars-backward "^<[\r\n")
  9852. (skip-chars-backward "<[")
  9853. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9854. (>= (match-end 0) pos)
  9855. (throw 'exit t)))
  9856. nil)))
  9857. (defun org-get-repeat (&optional tagline)
  9858. "Check if there is a deadline/schedule with repeater in this entry."
  9859. (save-match-data
  9860. (save-excursion
  9861. (org-back-to-heading t)
  9862. (and (re-search-forward (if tagline
  9863. (concat tagline "\\s-*" org-repeat-re)
  9864. org-repeat-re)
  9865. (org-entry-end-position) t)
  9866. (match-string-no-properties 1)))))
  9867. (defvar org-last-changed-timestamp)
  9868. (defvar org-last-inserted-timestamp)
  9869. (defvar org-log-post-message)
  9870. (defvar org-log-note-purpose)
  9871. (defvar org-log-note-how)
  9872. (defvar org-log-note-extra)
  9873. (defun org-auto-repeat-maybe (done-word)
  9874. "Check if the current headline contains a repeated deadline/schedule.
  9875. If yes, set TODO state back to what it was and change the base date
  9876. of repeating deadline/scheduled time stamps to new date.
  9877. This function is run automatically after each state change to a DONE state."
  9878. ;; last-state is dynamically scoped into this function
  9879. (let* ((repeat (org-get-repeat))
  9880. (aa (assoc last-state org-todo-kwd-alist))
  9881. (interpret (nth 1 aa))
  9882. (head (nth 2 aa))
  9883. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9884. (msg "Entry repeats: ")
  9885. (org-log-done nil)
  9886. (org-todo-log-states nil)
  9887. (nshiftmax 10) (nshift 0)
  9888. re type n what ts time to-state)
  9889. (when repeat
  9890. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9891. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  9892. org-todo-repeat-to-state))
  9893. (unless (and to-state (member to-state org-todo-keywords-1))
  9894. (setq to-state (if (eq interpret 'type) last-state head)))
  9895. (org-todo to-state)
  9896. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  9897. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9898. (org-time-stamp-format t t))))
  9899. (when org-log-repeat
  9900. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9901. (memq 'org-add-log-note post-command-hook))
  9902. ;; OK, we are already setup for some record
  9903. (if (eq org-log-repeat 'note)
  9904. ;; make sure we take a note, not only a time stamp
  9905. (setq org-log-note-how 'note))
  9906. ;; Set up for taking a record
  9907. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9908. last-state
  9909. 'findpos org-log-repeat)))
  9910. (org-back-to-heading t)
  9911. (org-add-planning-info nil nil 'closed)
  9912. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9913. org-deadline-time-regexp "\\)\\|\\("
  9914. org-ts-regexp "\\)"))
  9915. (while (re-search-forward
  9916. re (save-excursion (outline-next-heading) (point)) t)
  9917. (setq type (if (match-end 1) org-scheduled-string
  9918. (if (match-end 3) org-deadline-string "Plain:"))
  9919. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9920. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9921. (setq n (string-to-number (match-string 2 ts))
  9922. what (match-string 3 ts))
  9923. (if (equal what "w") (setq n (* n 7) what "d"))
  9924. ;; Preparation, see if we need to modify the start date for the change
  9925. (when (match-end 1)
  9926. (setq time (save-match-data (org-time-string-to-time ts)))
  9927. (cond
  9928. ((equal (match-string 1 ts) ".")
  9929. ;; Shift starting date to today
  9930. (org-timestamp-change
  9931. (- (time-to-days (current-time)) (time-to-days time))
  9932. 'day))
  9933. ((equal (match-string 1 ts) "+")
  9934. (while (or (= nshift 0)
  9935. (<= (time-to-days time) (time-to-days (current-time))))
  9936. (when (= (incf nshift) nshiftmax)
  9937. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9938. (error "Abort")))
  9939. (org-timestamp-change n (cdr (assoc what whata)))
  9940. (org-at-timestamp-p t)
  9941. (setq ts (match-string 1))
  9942. (setq time (save-match-data (org-time-string-to-time ts))))
  9943. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9944. ;; rematch, so that we have everything in place for the real shift
  9945. (org-at-timestamp-p t)
  9946. (setq ts (match-string 1))
  9947. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9948. (org-timestamp-change n (cdr (assoc what whata)))
  9949. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9950. (setq org-log-post-message msg)
  9951. (message "%s" msg))))
  9952. (defun org-show-todo-tree (arg)
  9953. "Make a compact tree which shows all headlines marked with TODO.
  9954. The tree will show the lines where the regexp matches, and all higher
  9955. headlines above the match.
  9956. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9957. With a numeric prefix N, construct a sparse tree for the Nth element
  9958. of `org-todo-keywords-1'."
  9959. (interactive "P")
  9960. (let ((case-fold-search nil)
  9961. (kwd-re
  9962. (cond ((null arg) org-not-done-regexp)
  9963. ((equal arg '(4))
  9964. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9965. (mapcar 'list org-todo-keywords-1))))
  9966. (concat "\\("
  9967. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9968. "\\)\\>")))
  9969. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9970. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9971. org-todo-keywords-1)))
  9972. (t (error "Invalid prefix argument: %s" arg)))))
  9973. (message "%d TODO entries found"
  9974. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9975. (defun org-deadline (&optional remove time)
  9976. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9977. With argument REMOVE, remove any deadline from the item.
  9978. When TIME is set, it should be an internal time specification, and the
  9979. scheduling will use the corresponding date."
  9980. (interactive "P")
  9981. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9982. (repeater (and old-date
  9983. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9984. (match-string 1 old-date))))
  9985. (if remove
  9986. (progn
  9987. (when (and old-date org-log-redeadline)
  9988. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9989. org-log-redeadline))
  9990. (org-remove-timestamp-with-keyword org-deadline-string)
  9991. (message "Item no longer has a deadline."))
  9992. (org-add-planning-info 'deadline time 'closed)
  9993. (when (and old-date org-log-redeadline
  9994. (not (equal old-date
  9995. (substring org-last-inserted-timestamp 1 -1))))
  9996. (org-add-log-setup 'redeadline nil old-date 'findpos
  9997. org-log-redeadline))
  9998. (when repeater
  9999. (save-excursion
  10000. (org-back-to-heading t)
  10001. (when (re-search-forward (concat org-deadline-string " "
  10002. org-last-inserted-timestamp)
  10003. (save-excursion
  10004. (outline-next-heading) (point)) t)
  10005. (goto-char (1- (match-end 0)))
  10006. (insert " " repeater)
  10007. (setq org-last-inserted-timestamp
  10008. (concat (substring org-last-inserted-timestamp 0 -1)
  10009. " " repeater
  10010. (substring org-last-inserted-timestamp -1))))))
  10011. (message "Deadline on %s" org-last-inserted-timestamp))))
  10012. (defun org-schedule (&optional remove time)
  10013. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10014. With argument REMOVE, remove any scheduling date from the item.
  10015. When TIME is set, it should be an internal time specification, and the
  10016. scheduling will use the corresponding date."
  10017. (interactive "P")
  10018. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10019. (repeater (and old-date
  10020. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  10021. (match-string 1 old-date))))
  10022. (if remove
  10023. (progn
  10024. (when (and old-date org-log-reschedule)
  10025. (org-add-log-setup 'delschedule nil old-date 'findpos
  10026. org-log-reschedule))
  10027. (org-remove-timestamp-with-keyword org-scheduled-string)
  10028. (message "Item is no longer scheduled."))
  10029. (org-add-planning-info 'scheduled time 'closed)
  10030. (when (and old-date org-log-reschedule
  10031. (not (equal old-date
  10032. (substring org-last-inserted-timestamp 1 -1))))
  10033. (org-add-log-setup 'reschedule nil old-date 'findpos
  10034. org-log-reschedule))
  10035. (when repeater
  10036. (save-excursion
  10037. (org-back-to-heading t)
  10038. (when (re-search-forward (concat org-scheduled-string " "
  10039. org-last-inserted-timestamp)
  10040. (save-excursion
  10041. (outline-next-heading) (point)) t)
  10042. (goto-char (1- (match-end 0)))
  10043. (insert " " repeater)
  10044. (setq org-last-inserted-timestamp
  10045. (concat (substring org-last-inserted-timestamp 0 -1)
  10046. " " repeater
  10047. (substring org-last-inserted-timestamp -1))))))
  10048. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10049. (defun org-get-scheduled-time (pom &optional inherit)
  10050. "Get the scheduled time as a time tuple, of a format suitable
  10051. for calling org-schedule with, or if there is no scheduling,
  10052. returns nil."
  10053. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10054. (when time
  10055. (apply 'encode-time (org-parse-time-string time)))))
  10056. (defun org-get-deadline-time (pom &optional inherit)
  10057. "Get the deadine as a time tuple, of a format suitable for
  10058. calling org-deadline with, or if there is no scheduling, returns
  10059. nil."
  10060. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10061. (when time
  10062. (apply 'encode-time (org-parse-time-string time)))))
  10063. (defun org-remove-timestamp-with-keyword (keyword)
  10064. "Remove all time stamps with KEYWORD in the current entry."
  10065. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10066. beg)
  10067. (save-excursion
  10068. (org-back-to-heading t)
  10069. (setq beg (point))
  10070. (outline-next-heading)
  10071. (while (re-search-backward re beg t)
  10072. (replace-match "")
  10073. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10074. (equal (char-before) ?\ ))
  10075. (backward-delete-char 1)
  10076. (if (string-match "^[ \t]*$" (buffer-substring
  10077. (point-at-bol) (point-at-eol)))
  10078. (delete-region (point-at-bol)
  10079. (min (point-max) (1+ (point-at-eol))))))))))
  10080. (defun org-add-planning-info (what &optional time &rest remove)
  10081. "Insert new timestamp with keyword in the line directly after the headline.
  10082. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10083. If non is given, the user is prompted for a date.
  10084. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10085. be removed."
  10086. (interactive)
  10087. (let (org-time-was-given org-end-time-was-given ts
  10088. end default-time default-input)
  10089. (catch 'exit
  10090. (when (and (not time) (memq what '(scheduled deadline)))
  10091. ;; Try to get a default date/time from existing timestamp
  10092. (save-excursion
  10093. (org-back-to-heading t)
  10094. (setq end (save-excursion (outline-next-heading) (point)))
  10095. (when (re-search-forward (if (eq what 'scheduled)
  10096. org-scheduled-time-regexp
  10097. org-deadline-time-regexp)
  10098. end t)
  10099. (setq ts (match-string 1)
  10100. default-time
  10101. (apply 'encode-time (org-parse-time-string ts))
  10102. default-input (and ts (org-get-compact-tod ts))))))
  10103. (when what
  10104. ;; If necessary, get the time from the user
  10105. (setq time (or time (org-read-date nil 'to-time nil nil
  10106. default-time default-input))))
  10107. (when (and org-insert-labeled-timestamps-at-point
  10108. (member what '(scheduled deadline)))
  10109. (insert
  10110. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10111. (org-insert-time-stamp time org-time-was-given
  10112. nil nil nil (list org-end-time-was-given))
  10113. (setq what nil))
  10114. (save-excursion
  10115. (save-restriction
  10116. (let (col list elt ts buffer-invisibility-spec)
  10117. (org-back-to-heading t)
  10118. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10119. (goto-char (match-end 1))
  10120. (setq col (current-column))
  10121. (goto-char (match-end 0))
  10122. (if (eobp) (insert "\n") (forward-char 1))
  10123. (when (and (not what)
  10124. (not (looking-at
  10125. (concat "[ \t]*"
  10126. org-keyword-time-not-clock-regexp))))
  10127. ;; Nothing to add, nothing to remove...... :-)
  10128. (throw 'exit nil))
  10129. (if (and (not (looking-at outline-regexp))
  10130. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10131. "[^\r\n]*"))
  10132. (not (equal (match-string 1) org-clock-string)))
  10133. (narrow-to-region (match-beginning 0) (match-end 0))
  10134. (insert-before-markers "\n")
  10135. (backward-char 1)
  10136. (narrow-to-region (point) (point))
  10137. (and org-adapt-indentation (org-indent-to-column col)))
  10138. ;; Check if we have to remove something.
  10139. (setq list (cons what remove))
  10140. (while list
  10141. (setq elt (pop list))
  10142. (goto-char (point-min))
  10143. (when (or (and (eq elt 'scheduled)
  10144. (re-search-forward org-scheduled-time-regexp nil t))
  10145. (and (eq elt 'deadline)
  10146. (re-search-forward org-deadline-time-regexp nil t))
  10147. (and (eq elt 'closed)
  10148. (re-search-forward org-closed-time-regexp nil t)))
  10149. (replace-match "")
  10150. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10151. (skip-chars-backward " ")
  10152. (if (looking-at " +") (replace-match ""))))
  10153. (goto-char (point-max))
  10154. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10155. (when what
  10156. (insert
  10157. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10158. (cond ((eq what 'scheduled) org-scheduled-string)
  10159. ((eq what 'deadline) org-deadline-string)
  10160. ((eq what 'closed) org-closed-string))
  10161. " ")
  10162. (setq ts (org-insert-time-stamp
  10163. time
  10164. (or org-time-was-given
  10165. (and (eq what 'closed) org-log-done-with-time))
  10166. (eq what 'closed)
  10167. nil nil (list org-end-time-was-given)))
  10168. (end-of-line 1))
  10169. (goto-char (point-min))
  10170. (widen)
  10171. (if (and (looking-at "[ \t]+\n")
  10172. (equal (char-before) ?\n))
  10173. (delete-region (1- (point)) (point-at-eol)))
  10174. ts))))))
  10175. (defvar org-log-note-marker (make-marker))
  10176. (defvar org-log-note-purpose nil)
  10177. (defvar org-log-note-state nil)
  10178. (defvar org-log-note-previous-state nil)
  10179. (defvar org-log-note-how nil)
  10180. (defvar org-log-note-extra nil)
  10181. (defvar org-log-note-window-configuration nil)
  10182. (defvar org-log-note-return-to (make-marker))
  10183. (defvar org-log-post-message nil
  10184. "Message to be displayed after a log note has been stored.
  10185. The auto-repeater uses this.")
  10186. (defun org-add-note ()
  10187. "Add a note to the current entry.
  10188. This is done in the same way as adding a state change note."
  10189. (interactive)
  10190. (org-add-log-setup 'note nil nil 'findpos nil))
  10191. (defvar org-property-end-re)
  10192. (defun org-add-log-setup (&optional purpose state prev-state
  10193. findpos how &optional extra)
  10194. "Set up the post command hook to take a note.
  10195. If this is about to TODO state change, the new state is expected in STATE.
  10196. When FINDPOS is non-nil, find the correct position for the note in
  10197. the current entry. If not, assume that it can be inserted at point.
  10198. HOW is an indicator what kind of note should be created.
  10199. EXTRA is additional text that will be inserted into the notes buffer."
  10200. (let* ((org-log-into-drawer (org-log-into-drawer))
  10201. (drawer (cond ((stringp org-log-into-drawer)
  10202. org-log-into-drawer)
  10203. (org-log-into-drawer "LOGBOOK")
  10204. (t nil))))
  10205. (save-restriction
  10206. (save-excursion
  10207. (when findpos
  10208. (org-back-to-heading t)
  10209. (narrow-to-region (point) (save-excursion
  10210. (outline-next-heading) (point)))
  10211. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10212. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10213. "[^\r\n]*\\)?"))
  10214. (goto-char (match-end 0))
  10215. (cond
  10216. (drawer
  10217. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10218. nil t)
  10219. (progn
  10220. (goto-char (match-end 0))
  10221. (or org-log-states-order-reversed
  10222. (and (re-search-forward org-property-end-re nil t)
  10223. (goto-char (1- (match-beginning 0))))))
  10224. (insert "\n:" drawer ":\n:END:")
  10225. (beginning-of-line 0)
  10226. (org-indent-line-function)
  10227. (beginning-of-line 2)
  10228. (org-indent-line-function)
  10229. (end-of-line 0)))
  10230. ((and org-log-state-notes-insert-after-drawers
  10231. (save-excursion
  10232. (forward-line) (looking-at org-drawer-regexp)))
  10233. (forward-line)
  10234. (while (looking-at org-drawer-regexp)
  10235. (goto-char (match-end 0))
  10236. (re-search-forward org-property-end-re (point-max) t)
  10237. (forward-line))
  10238. (forward-line -1)))
  10239. (unless org-log-states-order-reversed
  10240. (and (= (char-after) ?\n) (forward-char 1))
  10241. (org-skip-over-state-notes)
  10242. (skip-chars-backward " \t\n\r")))
  10243. (move-marker org-log-note-marker (point))
  10244. (setq org-log-note-purpose purpose
  10245. org-log-note-state state
  10246. org-log-note-previous-state prev-state
  10247. org-log-note-how how
  10248. org-log-note-extra extra)
  10249. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10250. (defun org-skip-over-state-notes ()
  10251. "Skip past the list of State notes in an entry."
  10252. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10253. (while (looking-at "[ \t]*- State")
  10254. (condition-case nil
  10255. (org-next-item)
  10256. (error (org-end-of-item)))))
  10257. (defun org-add-log-note (&optional purpose)
  10258. "Pop up a window for taking a note, and add this note later at point."
  10259. (remove-hook 'post-command-hook 'org-add-log-note)
  10260. (setq org-log-note-window-configuration (current-window-configuration))
  10261. (delete-other-windows)
  10262. (move-marker org-log-note-return-to (point))
  10263. (switch-to-buffer (marker-buffer org-log-note-marker))
  10264. (goto-char org-log-note-marker)
  10265. (org-switch-to-buffer-other-window "*Org Note*")
  10266. (erase-buffer)
  10267. (if (memq org-log-note-how '(time state))
  10268. (let (current-prefix-arg) (org-store-log-note))
  10269. (let ((org-inhibit-startup t)) (org-mode))
  10270. (insert (format "# Insert note for %s.
  10271. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10272. (cond
  10273. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10274. ((eq org-log-note-purpose 'done) "closed todo item")
  10275. ((eq org-log-note-purpose 'state)
  10276. (format "state change from \"%s\" to \"%s\""
  10277. (or org-log-note-previous-state "")
  10278. (or org-log-note-state "")))
  10279. ((eq org-log-note-purpose 'reschedule)
  10280. "rescheduling")
  10281. ((eq org-log-note-purpose 'delschedule)
  10282. "no longer scheduled")
  10283. ((eq org-log-note-purpose 'redeadline)
  10284. "changing deadline")
  10285. ((eq org-log-note-purpose 'deldeadline)
  10286. "removing deadline")
  10287. ((eq org-log-note-purpose 'refile)
  10288. "refiling")
  10289. ((eq org-log-note-purpose 'note)
  10290. "this entry")
  10291. (t (error "This should not happen")))))
  10292. (if org-log-note-extra (insert org-log-note-extra))
  10293. (org-set-local 'org-finish-function 'org-store-log-note)))
  10294. (defvar org-note-abort nil) ; dynamically scoped
  10295. (defun org-store-log-note ()
  10296. "Finish taking a log note, and insert it to where it belongs."
  10297. (let ((txt (buffer-string))
  10298. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10299. lines ind)
  10300. (kill-buffer (current-buffer))
  10301. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10302. (setq txt (replace-match "" t t txt)))
  10303. (if (string-match "\\s-+\\'" txt)
  10304. (setq txt (replace-match "" t t txt)))
  10305. (setq lines (org-split-string txt "\n"))
  10306. (when (and note (string-match "\\S-" note))
  10307. (setq note
  10308. (org-replace-escapes
  10309. note
  10310. (list (cons "%u" (user-login-name))
  10311. (cons "%U" user-full-name)
  10312. (cons "%t" (format-time-string
  10313. (org-time-stamp-format 'long 'inactive)
  10314. (current-time)))
  10315. (cons "%s" (if org-log-note-state
  10316. (concat "\"" org-log-note-state "\"")
  10317. ""))
  10318. (cons "%S" (if org-log-note-previous-state
  10319. (concat "\"" org-log-note-previous-state "\"")
  10320. "\"\"")))))
  10321. (if lines (setq note (concat note " \\\\")))
  10322. (push note lines))
  10323. (when (or current-prefix-arg org-note-abort)
  10324. (when org-log-into-drawer
  10325. (org-remove-empty-drawer-at
  10326. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10327. org-log-note-marker))
  10328. (setq lines nil))
  10329. (when lines
  10330. (with-current-buffer (marker-buffer org-log-note-marker)
  10331. (save-excursion
  10332. (goto-char org-log-note-marker)
  10333. (move-marker org-log-note-marker nil)
  10334. (end-of-line 1)
  10335. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10336. (insert "- " (pop lines))
  10337. (org-indent-line-function)
  10338. (beginning-of-line 1)
  10339. (looking-at "[ \t]*")
  10340. (setq ind (concat (match-string 0) " "))
  10341. (end-of-line 1)
  10342. (while lines (insert "\n" ind (pop lines)))
  10343. (message "Note stored")
  10344. (org-back-to-heading t)
  10345. (org-cycle-hide-drawers 'children)))))
  10346. (set-window-configuration org-log-note-window-configuration)
  10347. (with-current-buffer (marker-buffer org-log-note-return-to)
  10348. (goto-char org-log-note-return-to))
  10349. (move-marker org-log-note-return-to nil)
  10350. (and org-log-post-message (message "%s" org-log-post-message)))
  10351. (defun org-remove-empty-drawer-at (drawer pos)
  10352. "Remove an empty drawer DRAWER at position POS.
  10353. POS may also be a marker."
  10354. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10355. (save-excursion
  10356. (save-restriction
  10357. (widen)
  10358. (goto-char pos)
  10359. (if (org-in-regexp
  10360. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10361. (replace-match ""))))))
  10362. (defun org-sparse-tree (&optional arg)
  10363. "Create a sparse tree, prompt for the details.
  10364. This command can create sparse trees. You first need to select the type
  10365. of match used to create the tree:
  10366. t Show entries with a specific TODO keyword.
  10367. m Show entries selected by a tags/property match.
  10368. p Enter a property name and its value (both with completion on existing
  10369. names/values) and show entries with that property.
  10370. / Show entries matching a regular expression (`r' can be used as well)
  10371. d Show deadlines due within `org-deadline-warning-days'.
  10372. b Show deadlines and scheduled items before a date.
  10373. a Show deadlines and scheduled items after a date."
  10374. (interactive "P")
  10375. (let (ans kwd value)
  10376. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10377. (setq ans (read-char-exclusive))
  10378. (cond
  10379. ((equal ans ?d)
  10380. (call-interactively 'org-check-deadlines))
  10381. ((equal ans ?b)
  10382. (call-interactively 'org-check-before-date))
  10383. ((equal ans ?a)
  10384. (call-interactively 'org-check-after-date))
  10385. ((equal ans ?t)
  10386. (org-show-todo-tree '(4)))
  10387. ((member ans '(?T ?m))
  10388. (call-interactively 'org-match-sparse-tree))
  10389. ((member ans '(?p ?P))
  10390. (setq kwd (org-icompleting-read "Property: "
  10391. (mapcar 'list (org-buffer-property-keys))))
  10392. (setq value (org-icompleting-read "Value: "
  10393. (mapcar 'list (org-property-values kwd))))
  10394. (unless (string-match "\\`{.*}\\'" value)
  10395. (setq value (concat "\"" value "\"")))
  10396. (org-match-sparse-tree arg (concat kwd "=" value)))
  10397. ((member ans '(?r ?R ?/))
  10398. (call-interactively 'org-occur))
  10399. (t (error "No such sparse tree command \"%c\"" ans)))))
  10400. (defvar org-occur-highlights nil
  10401. "List of overlays used for occur matches.")
  10402. (make-variable-buffer-local 'org-occur-highlights)
  10403. (defvar org-occur-parameters nil
  10404. "Parameters of the active org-occur calls.
  10405. This is a list, each call to org-occur pushes as cons cell,
  10406. containing the regular expression and the callback, onto the list.
  10407. The list can contain several entries if `org-occur' has been called
  10408. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10409. will only contain one set of parameters. When the highlights are
  10410. removed (for example with `C-c C-c', or with the next edit (depending
  10411. on `org-remove-highlights-with-change'), this variable is emptied
  10412. as well.")
  10413. (make-variable-buffer-local 'org-occur-parameters)
  10414. (defun org-occur (regexp &optional keep-previous callback)
  10415. "Make a compact tree which shows all matches of REGEXP.
  10416. The tree will show the lines where the regexp matches, and all higher
  10417. headlines above the match. It will also show the heading after the match,
  10418. to make sure editing the matching entry is easy.
  10419. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10420. call to `org-occur' will be kept, to allow stacking of calls to this
  10421. command.
  10422. If CALLBACK is non-nil, it is a function which is called to confirm
  10423. that the match should indeed be shown."
  10424. (interactive "sRegexp: \nP")
  10425. (when (equal regexp "")
  10426. (error "Regexp cannot be empty"))
  10427. (unless keep-previous
  10428. (org-remove-occur-highlights nil nil t))
  10429. (push (cons regexp callback) org-occur-parameters)
  10430. (let ((cnt 0))
  10431. (save-excursion
  10432. (goto-char (point-min))
  10433. (if (or (not keep-previous) ; do not want to keep
  10434. (not org-occur-highlights)) ; no previous matches
  10435. ;; hide everything
  10436. (org-overview))
  10437. (while (re-search-forward regexp nil t)
  10438. (when (or (not callback)
  10439. (save-match-data (funcall callback)))
  10440. (setq cnt (1+ cnt))
  10441. (when org-highlight-sparse-tree-matches
  10442. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10443. (org-show-context 'occur-tree))))
  10444. (when org-remove-highlights-with-change
  10445. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10446. nil 'local))
  10447. (unless org-sparse-tree-open-archived-trees
  10448. (org-hide-archived-subtrees (point-min) (point-max)))
  10449. (run-hooks 'org-occur-hook)
  10450. (if (interactive-p)
  10451. (message "%d match(es) for regexp %s" cnt regexp))
  10452. cnt))
  10453. (defun org-show-context (&optional key)
  10454. "Make sure point and context and visible.
  10455. How much context is shown depends upon the variables
  10456. `org-show-hierarchy-above', `org-show-following-heading'. and
  10457. `org-show-siblings'."
  10458. (let ((heading-p (org-on-heading-p t))
  10459. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10460. (following-p (org-get-alist-option org-show-following-heading key))
  10461. (entry-p (org-get-alist-option org-show-entry-below key))
  10462. (siblings-p (org-get-alist-option org-show-siblings key)))
  10463. (catch 'exit
  10464. ;; Show heading or entry text
  10465. (if (and heading-p (not entry-p))
  10466. (org-flag-heading nil) ; only show the heading
  10467. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10468. (org-show-hidden-entry))) ; show entire entry
  10469. (when following-p
  10470. ;; Show next sibling, or heading below text
  10471. (save-excursion
  10472. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10473. (org-flag-heading nil))))
  10474. (when siblings-p (org-show-siblings))
  10475. (when hierarchy-p
  10476. ;; show all higher headings, possibly with siblings
  10477. (save-excursion
  10478. (while (and (condition-case nil
  10479. (progn (org-up-heading-all 1) t)
  10480. (error nil))
  10481. (not (bobp)))
  10482. (org-flag-heading nil)
  10483. (when siblings-p (org-show-siblings))))))))
  10484. (defvar org-reveal-start-hook nil
  10485. "Hook run before revealing a location.")
  10486. (defun org-reveal (&optional siblings)
  10487. "Show current entry, hierarchy above it, and the following headline.
  10488. This can be used to show a consistent set of context around locations
  10489. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10490. not t for the search context.
  10491. With optional argument SIBLINGS, on each level of the hierarchy all
  10492. siblings are shown. This repairs the tree structure to what it would
  10493. look like when opened with hierarchical calls to `org-cycle'.
  10494. With double optional argument `C-u C-u', go to the parent and show the
  10495. entire tree."
  10496. (interactive "P")
  10497. (run-hooks 'org-reveal-start-hook)
  10498. (let ((org-show-hierarchy-above t)
  10499. (org-show-following-heading t)
  10500. (org-show-siblings (if siblings t org-show-siblings)))
  10501. (org-show-context nil))
  10502. (when (equal siblings '(16))
  10503. (save-excursion
  10504. (when (org-up-heading-safe)
  10505. (org-show-subtree)
  10506. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10507. (defun org-highlight-new-match (beg end)
  10508. "Highlight from BEG to END and mark the highlight is an occur headline."
  10509. (let ((ov (make-overlay beg end)))
  10510. (overlay-put ov 'face 'secondary-selection)
  10511. (push ov org-occur-highlights)))
  10512. (defun org-remove-occur-highlights (&optional beg end noremove)
  10513. "Remove the occur highlights from the buffer.
  10514. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10515. from the `before-change-functions' in the current buffer."
  10516. (interactive)
  10517. (unless org-inhibit-highlight-removal
  10518. (mapc 'delete-overlay org-occur-highlights)
  10519. (setq org-occur-highlights nil)
  10520. (setq org-occur-parameters nil)
  10521. (unless noremove
  10522. (remove-hook 'before-change-functions
  10523. 'org-remove-occur-highlights 'local))))
  10524. ;;;; Priorities
  10525. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10526. "Regular expression matching the priority indicator.")
  10527. (defvar org-remove-priority-next-time nil)
  10528. (defun org-priority-up ()
  10529. "Increase the priority of the current item."
  10530. (interactive)
  10531. (org-priority 'up))
  10532. (defun org-priority-down ()
  10533. "Decrease the priority of the current item."
  10534. (interactive)
  10535. (org-priority 'down))
  10536. (defun org-priority (&optional action)
  10537. "Change the priority of an item by ARG.
  10538. ACTION can be `set', `up', `down', or a character."
  10539. (interactive)
  10540. (unless org-enable-priority-commands
  10541. (error "Priority commands are disabled"))
  10542. (setq action (or action 'set))
  10543. (let (current new news have remove)
  10544. (save-excursion
  10545. (org-back-to-heading t)
  10546. (if (looking-at org-priority-regexp)
  10547. (setq current (string-to-char (match-string 2))
  10548. have t)
  10549. (setq current org-default-priority))
  10550. (cond
  10551. ((eq action 'remove)
  10552. (setq remove t new ?\ ))
  10553. ((or (eq action 'set)
  10554. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10555. (if (not (eq action 'set))
  10556. (setq new action)
  10557. (message "Priority %c-%c, SPC to remove: "
  10558. org-highest-priority org-lowest-priority)
  10559. (setq new (read-char-exclusive)))
  10560. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10561. (= (upcase org-lowest-priority) org-lowest-priority))
  10562. (setq new (upcase new)))
  10563. (cond ((equal new ?\ ) (setq remove t))
  10564. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10565. (error "Priority must be between `%c' and `%c'"
  10566. org-highest-priority org-lowest-priority))))
  10567. ((eq action 'up)
  10568. (if (and (not have) (eq last-command this-command))
  10569. (setq new org-lowest-priority)
  10570. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10571. org-default-priority (1- current)))))
  10572. ((eq action 'down)
  10573. (if (and (not have) (eq last-command this-command))
  10574. (setq new org-highest-priority)
  10575. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10576. org-default-priority (1+ current)))))
  10577. (t (error "Invalid action")))
  10578. (if (or (< (upcase new) org-highest-priority)
  10579. (> (upcase new) org-lowest-priority))
  10580. (setq remove t))
  10581. (setq news (format "%c" new))
  10582. (if have
  10583. (if remove
  10584. (replace-match "" t t nil 1)
  10585. (replace-match news t t nil 2))
  10586. (if remove
  10587. (error "No priority cookie found in line")
  10588. (let ((case-fold-search nil))
  10589. (looking-at org-todo-line-regexp))
  10590. (if (match-end 2)
  10591. (progn
  10592. (goto-char (match-end 2))
  10593. (insert " [#" news "]"))
  10594. (goto-char (match-beginning 3))
  10595. (insert "[#" news "] "))))
  10596. (org-preserve-lc (org-set-tags nil 'align)))
  10597. (if remove
  10598. (message "Priority removed")
  10599. (message "Priority of current item set to %s" news))))
  10600. (defun org-get-priority (s)
  10601. "Find priority cookie and return priority."
  10602. (save-match-data
  10603. (if (not (string-match org-priority-regexp s))
  10604. (* 1000 (- org-lowest-priority org-default-priority))
  10605. (* 1000 (- org-lowest-priority
  10606. (string-to-char (match-string 2 s)))))))
  10607. ;;;; Tags
  10608. (defvar org-agenda-archives-mode)
  10609. (defvar org-map-continue-from nil
  10610. "Position from where mapping should continue.
  10611. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10612. (defvar org-scanner-tags nil
  10613. "The current tag list while the tags scanner is running.")
  10614. (defvar org-trust-scanner-tags nil
  10615. "Should `org-get-tags-at' use the tags fro the scanner.
  10616. This is for internal dynamical scoping only.
  10617. When this is non-nil, the function `org-get-tags-at' will return the value
  10618. of `org-scanner-tags' instead of building the list by itself. This
  10619. can lead to large speed-ups when the tags scanner is used in a file with
  10620. many entries, and when the list of tags is retrieved, for example to
  10621. obtain a list of properties. Building the tags list for each entry in such
  10622. a file becomes an N^2 operation - but with this variable set, it scales
  10623. as N.")
  10624. (defun org-scan-tags (action matcher &optional todo-only)
  10625. "Scan headline tags with inheritance and produce output ACTION.
  10626. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10627. or `agenda' to produce an entry list for an agenda view. It can also be
  10628. a Lisp form or a function that should be called at each matched headline, in
  10629. this case the return value is a list of all return values from these calls.
  10630. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10631. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10632. only lines with a TODO keyword are included in the output."
  10633. (require 'org-agenda)
  10634. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10635. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10636. (org-re
  10637. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10638. (props (list 'face 'default
  10639. 'done-face 'org-agenda-done
  10640. 'undone-face 'default
  10641. 'mouse-face 'highlight
  10642. 'org-not-done-regexp org-not-done-regexp
  10643. 'org-todo-regexp org-todo-regexp
  10644. 'help-echo
  10645. (format "mouse-2 or RET jump to org file %s"
  10646. (abbreviate-file-name
  10647. (or (buffer-file-name (buffer-base-buffer))
  10648. (buffer-name (buffer-base-buffer)))))))
  10649. (case-fold-search nil)
  10650. (org-map-continue-from nil)
  10651. lspos tags tags-list
  10652. (tags-alist (list (cons 0 org-file-tags)))
  10653. (llast 0) rtn rtn1 level category i txt
  10654. todo marker entry priority)
  10655. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10656. (setq action (list 'lambda nil action)))
  10657. (save-excursion
  10658. (goto-char (point-min))
  10659. (when (eq action 'sparse-tree)
  10660. (org-overview)
  10661. (org-remove-occur-highlights))
  10662. (while (re-search-forward re nil t)
  10663. (catch :skip
  10664. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10665. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10666. (goto-char (setq lspos (match-beginning 0)))
  10667. (setq level (org-reduced-level (funcall outline-level))
  10668. category (org-get-category))
  10669. (setq i llast llast level)
  10670. ;; remove tag lists from same and sublevels
  10671. (while (>= i level)
  10672. (when (setq entry (assoc i tags-alist))
  10673. (setq tags-alist (delete entry tags-alist)))
  10674. (setq i (1- i)))
  10675. ;; add the next tags
  10676. (when tags
  10677. (setq tags (org-split-string tags ":")
  10678. tags-alist
  10679. (cons (cons level tags) tags-alist)))
  10680. ;; compile tags for current headline
  10681. (setq tags-list
  10682. (if org-use-tag-inheritance
  10683. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10684. tags)
  10685. org-scanner-tags tags-list)
  10686. (when org-use-tag-inheritance
  10687. (setcdr (car tags-alist)
  10688. (mapcar (lambda (x)
  10689. (setq x (copy-sequence x))
  10690. (org-add-prop-inherited x))
  10691. (cdar tags-alist))))
  10692. (when (and tags org-use-tag-inheritance
  10693. (or (not (eq t org-use-tag-inheritance))
  10694. org-tags-exclude-from-inheritance))
  10695. ;; selective inheritance, remove uninherited ones
  10696. (setcdr (car tags-alist)
  10697. (org-remove-uniherited-tags (cdar tags-alist))))
  10698. (when (and (or (not todo-only)
  10699. (and (member todo org-not-done-keywords)
  10700. (or (not org-agenda-tags-todo-honor-ignore-options)
  10701. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10702. (let ((case-fold-search t)) (eval matcher))
  10703. (or
  10704. (not (member org-archive-tag tags-list))
  10705. ;; we have an archive tag, should we use this anyway?
  10706. (or (not org-agenda-skip-archived-trees)
  10707. (and (eq action 'agenda) org-agenda-archives-mode))))
  10708. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10709. ;; select this headline
  10710. (cond
  10711. ((eq action 'sparse-tree)
  10712. (and org-highlight-sparse-tree-matches
  10713. (org-get-heading) (match-end 0)
  10714. (org-highlight-new-match
  10715. (match-beginning 0) (match-beginning 1)))
  10716. (org-show-context 'tags-tree))
  10717. ((eq action 'agenda)
  10718. (setq txt (org-format-agenda-item
  10719. ""
  10720. (concat
  10721. (if (eq org-tags-match-list-sublevels 'indented)
  10722. (make-string (1- level) ?.) "")
  10723. (org-get-heading))
  10724. category
  10725. tags-list
  10726. )
  10727. priority (org-get-priority txt))
  10728. (goto-char lspos)
  10729. (setq marker (org-agenda-new-marker))
  10730. (org-add-props txt props
  10731. 'org-marker marker 'org-hd-marker marker 'org-category category
  10732. 'todo-state todo
  10733. 'priority priority 'type "tagsmatch")
  10734. (push txt rtn))
  10735. ((functionp action)
  10736. (setq org-map-continue-from nil)
  10737. (save-excursion
  10738. (setq rtn1 (funcall action))
  10739. (push rtn1 rtn)))
  10740. (t (error "Invalid action")))
  10741. ;; if we are to skip sublevels, jump to end of subtree
  10742. (unless org-tags-match-list-sublevels
  10743. (org-end-of-subtree t)
  10744. (backward-char 1))))
  10745. ;; Get the correct position from where to continue
  10746. (if org-map-continue-from
  10747. (goto-char org-map-continue-from)
  10748. (and (= (point) lspos) (end-of-line 1)))))
  10749. (when (and (eq action 'sparse-tree)
  10750. (not org-sparse-tree-open-archived-trees))
  10751. (org-hide-archived-subtrees (point-min) (point-max)))
  10752. (nreverse rtn)))
  10753. (defun org-remove-uniherited-tags (tags)
  10754. "Remove all tags that are not inherited from the list TAGS."
  10755. (cond
  10756. ((eq org-use-tag-inheritance t)
  10757. (if org-tags-exclude-from-inheritance
  10758. (org-delete-all org-tags-exclude-from-inheritance tags)
  10759. tags))
  10760. ((not org-use-tag-inheritance) nil)
  10761. ((stringp org-use-tag-inheritance)
  10762. (delq nil (mapcar
  10763. (lambda (x)
  10764. (if (and (string-match org-use-tag-inheritance x)
  10765. (not (member x org-tags-exclude-from-inheritance)))
  10766. x nil))
  10767. tags)))
  10768. ((listp org-use-tag-inheritance)
  10769. (delq nil (mapcar
  10770. (lambda (x)
  10771. (if (member x org-use-tag-inheritance) x nil))
  10772. tags)))))
  10773. (defvar todo-only) ;; dynamically scoped
  10774. (defun org-match-sparse-tree (&optional todo-only match)
  10775. "Create a sparse tree according to tags string MATCH.
  10776. MATCH can contain positive and negative selection of tags, like
  10777. \"+WORK+URGENT-WITHBOSS\".
  10778. If optional argument TODO-ONLY is non-nil, only select lines that are
  10779. also TODO lines."
  10780. (interactive "P")
  10781. (org-prepare-agenda-buffers (list (current-buffer)))
  10782. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10783. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10784. (defvar org-cached-props nil)
  10785. (defun org-cached-entry-get (pom property)
  10786. (if (or (eq t org-use-property-inheritance)
  10787. (and (stringp org-use-property-inheritance)
  10788. (string-match org-use-property-inheritance property))
  10789. (and (listp org-use-property-inheritance)
  10790. (member property org-use-property-inheritance)))
  10791. ;; Caching is not possible, check it directly
  10792. (org-entry-get pom property 'inherit)
  10793. ;; Get all properties, so that we can do complicated checks easily
  10794. (cdr (assoc property (or org-cached-props
  10795. (setq org-cached-props
  10796. (org-entry-properties pom)))))))
  10797. (defun org-global-tags-completion-table (&optional files)
  10798. "Return the list of all tags in all agenda buffer/files."
  10799. (save-excursion
  10800. (org-uniquify
  10801. (delq nil
  10802. (apply 'append
  10803. (mapcar
  10804. (lambda (file)
  10805. (set-buffer (find-file-noselect file))
  10806. (append (org-get-buffer-tags)
  10807. (mapcar (lambda (x) (if (stringp (car-safe x))
  10808. (list (car-safe x)) nil))
  10809. org-tag-alist)))
  10810. (if (and files (car files))
  10811. files
  10812. (org-agenda-files))))))))
  10813. (defun org-make-tags-matcher (match)
  10814. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10815. ;; todo-only is scoped dynamically into this function, and the function
  10816. ;; may change it if the matcher asks for it.
  10817. (unless match
  10818. ;; Get a new match request, with completion
  10819. (let ((org-last-tags-completion-table
  10820. (org-global-tags-completion-table)))
  10821. (setq match (org-completing-read-no-i
  10822. "Match: " 'org-tags-completion-function nil nil nil
  10823. 'org-tags-history))))
  10824. ;; Parse the string and create a lisp form
  10825. (let ((match0 match)
  10826. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10827. minus tag mm
  10828. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10829. orterms term orlist re-p str-p level-p level-op time-p
  10830. prop-p pn pv po cat-p gv rest)
  10831. (if (string-match "/+" match)
  10832. ;; match contains also a todo-matching request
  10833. (progn
  10834. (setq tagsmatch (substring match 0 (match-beginning 0))
  10835. todomatch (substring match (match-end 0)))
  10836. (if (string-match "^!" todomatch)
  10837. (setq todo-only t todomatch (substring todomatch 1)))
  10838. (if (string-match "^\\s-*$" todomatch)
  10839. (setq todomatch nil)))
  10840. ;; only matching tags
  10841. (setq tagsmatch match todomatch nil))
  10842. ;; Make the tags matcher
  10843. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10844. (setq tagsmatcher t)
  10845. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10846. (while (setq term (pop orterms))
  10847. (while (and (equal (substring term -1) "\\") orterms)
  10848. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10849. (while (string-match re term)
  10850. (setq rest (substring term (match-end 0))
  10851. minus (and (match-end 1)
  10852. (equal (match-string 1 term) "-"))
  10853. tag (match-string 2 term)
  10854. re-p (equal (string-to-char tag) ?{)
  10855. level-p (match-end 4)
  10856. prop-p (match-end 5)
  10857. mm (cond
  10858. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10859. (level-p
  10860. (setq level-op (org-op-to-function (match-string 3 term)))
  10861. `(,level-op level ,(string-to-number
  10862. (match-string 4 term))))
  10863. (prop-p
  10864. (setq pn (match-string 5 term)
  10865. po (match-string 6 term)
  10866. pv (match-string 7 term)
  10867. cat-p (equal pn "CATEGORY")
  10868. re-p (equal (string-to-char pv) ?{)
  10869. str-p (equal (string-to-char pv) ?\")
  10870. time-p (save-match-data
  10871. (string-match "^\"[[<].*[]>]\"$" pv))
  10872. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10873. (if time-p (setq pv (org-matcher-time pv)))
  10874. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10875. (cond
  10876. ((equal pn "CATEGORY")
  10877. (setq gv '(get-text-property (point) 'org-category)))
  10878. ((equal pn "TODO")
  10879. (setq gv 'todo))
  10880. (t
  10881. (setq gv `(org-cached-entry-get nil ,pn))))
  10882. (if re-p
  10883. (if (eq po 'org<>)
  10884. `(not (string-match ,pv (or ,gv "")))
  10885. `(string-match ,pv (or ,gv "")))
  10886. (if str-p
  10887. `(,po (or ,gv "") ,pv)
  10888. `(,po (string-to-number (or ,gv ""))
  10889. ,(string-to-number pv) ))))
  10890. (t `(member ,tag tags-list)))
  10891. mm (if minus (list 'not mm) mm)
  10892. term rest)
  10893. (push mm tagsmatcher))
  10894. (push (if (> (length tagsmatcher) 1)
  10895. (cons 'and tagsmatcher)
  10896. (car tagsmatcher))
  10897. orlist)
  10898. (setq tagsmatcher nil))
  10899. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10900. (setq tagsmatcher
  10901. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10902. ;; Make the todo matcher
  10903. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10904. (setq todomatcher t)
  10905. (setq orterms (org-split-string todomatch "|") orlist nil)
  10906. (while (setq term (pop orterms))
  10907. (while (string-match re term)
  10908. (setq minus (and (match-end 1)
  10909. (equal (match-string 1 term) "-"))
  10910. kwd (match-string 2 term)
  10911. re-p (equal (string-to-char kwd) ?{)
  10912. term (substring term (match-end 0))
  10913. mm (if re-p
  10914. `(string-match ,(substring kwd 1 -1) todo)
  10915. (list 'equal 'todo kwd))
  10916. mm (if minus (list 'not mm) mm))
  10917. (push mm todomatcher))
  10918. (push (if (> (length todomatcher) 1)
  10919. (cons 'and todomatcher)
  10920. (car todomatcher))
  10921. orlist)
  10922. (setq todomatcher nil))
  10923. (setq todomatcher (if (> (length orlist) 1)
  10924. (cons 'or orlist) (car orlist))))
  10925. ;; Return the string and lisp forms of the matcher
  10926. (setq matcher (if todomatcher
  10927. (list 'and tagsmatcher todomatcher)
  10928. tagsmatcher))
  10929. (cons match0 matcher)))
  10930. (defun org-op-to-function (op &optional stringp)
  10931. "Turn an operator into the appropriate function."
  10932. (setq op
  10933. (cond
  10934. ((equal op "<" ) '(< string< org-time<))
  10935. ((equal op ">" ) '(> org-string> org-time>))
  10936. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10937. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10938. ((member op '("=" "==")) '(= string= org-time=))
  10939. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10940. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10941. (defun org<> (a b) (not (= a b)))
  10942. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10943. (defun org-string>= (a b) (not (string< a b)))
  10944. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10945. (defun org-string<> (a b) (not (string= a b)))
  10946. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10947. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10948. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10949. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10950. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10951. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10952. (defun org-2ft (s)
  10953. "Convert S to a floating point time.
  10954. If S is already a number, just return it. If it is a string, parse
  10955. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10956. (cond
  10957. ((numberp s) s)
  10958. ((stringp s)
  10959. (condition-case nil
  10960. (float-time (apply 'encode-time (org-parse-time-string s)))
  10961. (error 0.)))
  10962. (t 0.)))
  10963. (defun org-time-today ()
  10964. "Time in seconds today at 0:00.
  10965. Returns the float number of seconds since the beginning of the
  10966. epoch to the beginning of today (00:00)."
  10967. (float-time (apply 'encode-time
  10968. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10969. (defun org-matcher-time (s)
  10970. "Interpret a time comparison value."
  10971. (save-match-data
  10972. (cond
  10973. ((string= s "<now>") (float-time))
  10974. ((string= s "<today>") (org-time-today))
  10975. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10976. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10977. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10978. (+ (org-time-today)
  10979. (* (string-to-number (match-string 1 s))
  10980. (cdr (assoc (match-string 2 s)
  10981. '(("d" . 86400.0) ("w" . 604800.0)
  10982. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10983. (t (org-2ft s)))))
  10984. (defun org-match-any-p (re list)
  10985. "Does re match any element of list?"
  10986. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10987. (delq nil list))
  10988. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10989. (defvar org-tags-overlay (make-overlay 1 1))
  10990. (org-detach-overlay org-tags-overlay)
  10991. (defun org-get-local-tags-at (&optional pos)
  10992. "Get a list of tags defined in the current headline."
  10993. (org-get-tags-at pos 'local))
  10994. (defun org-get-local-tags ()
  10995. "Get a list of tags defined in the current headline."
  10996. (org-get-tags-at nil 'local))
  10997. (defun org-get-tags-at (&optional pos local)
  10998. "Get a list of all headline tags applicable at POS.
  10999. POS defaults to point. If tags are inherited, the list contains
  11000. the targets in the same sequence as the headlines appear, i.e.
  11001. the tags of the current headline come last.
  11002. When LOCAL is non-nil, only return tags from the current headline,
  11003. ignore inherited ones."
  11004. (interactive)
  11005. (if (and org-trust-scanner-tags
  11006. (or (not pos) (equal pos (point)))
  11007. (not local))
  11008. org-scanner-tags
  11009. (let (tags ltags lastpos parent)
  11010. (save-excursion
  11011. (save-restriction
  11012. (widen)
  11013. (goto-char (or pos (point)))
  11014. (save-match-data
  11015. (catch 'done
  11016. (condition-case nil
  11017. (progn
  11018. (org-back-to-heading t)
  11019. (while (not (equal lastpos (point)))
  11020. (setq lastpos (point))
  11021. (when (looking-at
  11022. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  11023. (setq ltags (org-split-string
  11024. (org-match-string-no-properties 1) ":"))
  11025. (when parent
  11026. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11027. (setq tags (append
  11028. (if parent
  11029. (org-remove-uniherited-tags ltags)
  11030. ltags)
  11031. tags)))
  11032. (or org-use-tag-inheritance (throw 'done t))
  11033. (if local (throw 'done t))
  11034. (or (org-up-heading-safe) (error nil))
  11035. (setq parent t)))
  11036. (error nil)))))
  11037. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11038. (defun org-add-prop-inherited (s)
  11039. (add-text-properties 0 (length s) '(inherited t) s)
  11040. s)
  11041. (defun org-toggle-tag (tag &optional onoff)
  11042. "Toggle the tag TAG for the current line.
  11043. If ONOFF is `on' or `off', don't toggle but set to this state."
  11044. (let (res current)
  11045. (save-excursion
  11046. (org-back-to-heading t)
  11047. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11048. (point-at-eol) t)
  11049. (progn
  11050. (setq current (match-string 1))
  11051. (replace-match ""))
  11052. (setq current ""))
  11053. (setq current (nreverse (org-split-string current ":")))
  11054. (cond
  11055. ((eq onoff 'on)
  11056. (setq res t)
  11057. (or (member tag current) (push tag current)))
  11058. ((eq onoff 'off)
  11059. (or (not (member tag current)) (setq current (delete tag current))))
  11060. (t (if (member tag current)
  11061. (setq current (delete tag current))
  11062. (setq res t)
  11063. (push tag current))))
  11064. (end-of-line 1)
  11065. (if current
  11066. (progn
  11067. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11068. (org-set-tags nil t))
  11069. (delete-horizontal-space))
  11070. (run-hooks 'org-after-tags-change-hook))
  11071. res))
  11072. (defun org-align-tags-here (to-col)
  11073. ;; Assumes that this is a headline
  11074. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11075. (beginning-of-line 1)
  11076. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11077. (< pos (match-beginning 2)))
  11078. (progn
  11079. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11080. (goto-char (match-beginning 1))
  11081. (insert " ")
  11082. (delete-region (point) (1+ (match-beginning 2)))
  11083. (setq ncol (max (1+ (current-column))
  11084. (1+ col)
  11085. (if (> to-col 0)
  11086. to-col
  11087. (- (abs to-col) tags-l))))
  11088. (setq p (point))
  11089. (insert (make-string (- ncol (current-column)) ?\ ))
  11090. (setq ncol (current-column))
  11091. (when indent-tabs-mode (tabify p (point-at-eol)))
  11092. (org-move-to-column (min ncol col) t))
  11093. (goto-char pos))))
  11094. (defun org-set-tags-command (&optional arg just-align)
  11095. "Call the set-tags command for the current entry."
  11096. (interactive "P")
  11097. (if (org-on-heading-p)
  11098. (org-set-tags arg just-align)
  11099. (save-excursion
  11100. (org-back-to-heading t)
  11101. (org-set-tags arg just-align))))
  11102. (defun org-set-tags-to (data)
  11103. "Set the tags of the current entry to DATA, replacing the current tags.
  11104. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11105. If DATA is nil or the empty string, any tags will be removed."
  11106. (interactive "sTags: ")
  11107. (setq data
  11108. (cond
  11109. ((eq data nil) "")
  11110. ((equal data "") "")
  11111. ((stringp data)
  11112. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11113. ":"))
  11114. ((listp data)
  11115. (concat ":" (mapconcat 'identity data ":") ":"))
  11116. (t nil)))
  11117. (when data
  11118. (save-excursion
  11119. (org-back-to-heading t)
  11120. (when (looking-at org-complex-heading-regexp)
  11121. (if (match-end 5)
  11122. (progn
  11123. (goto-char (match-beginning 5))
  11124. (insert data)
  11125. (delete-region (point) (point-at-eol))
  11126. (org-set-tags nil 'align))
  11127. (goto-char (point-at-eol))
  11128. (insert " " data)
  11129. (org-set-tags nil 'align)))
  11130. (beginning-of-line 1)
  11131. (if (looking-at ".*?\\([ \t]+\\)$")
  11132. (delete-region (match-beginning 1) (match-end 1))))))
  11133. (defun org-align-all-tags ()
  11134. "Align the tags i all headings."
  11135. (interactive)
  11136. (save-excursion
  11137. (or (ignore-errors (org-back-to-heading t))
  11138. (outline-next-heading))
  11139. (if (org-on-heading-p)
  11140. (org-set-tags t)
  11141. (message "No headings"))))
  11142. (defun org-set-tags (&optional arg just-align)
  11143. "Set the tags for the current headline.
  11144. With prefix ARG, realign all tags in headings in the current buffer."
  11145. (interactive "P")
  11146. (let* ((re (concat "^" outline-regexp))
  11147. (current (org-get-tags-string))
  11148. (col (current-column))
  11149. (org-setting-tags t)
  11150. table current-tags inherited-tags ; computed below when needed
  11151. tags p0 c0 c1 rpl)
  11152. (if arg
  11153. (save-excursion
  11154. (goto-char (point-min))
  11155. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11156. (while (re-search-forward re nil t)
  11157. (org-set-tags nil t)
  11158. (end-of-line 1)))
  11159. (message "All tags realigned to column %d" org-tags-column))
  11160. (if just-align
  11161. (setq tags current)
  11162. ;; Get a new set of tags from the user
  11163. (save-excursion
  11164. (setq table (append org-tag-persistent-alist
  11165. (or org-tag-alist (org-get-buffer-tags))
  11166. (and org-complete-tags-always-offer-all-agenda-tags
  11167. (org-global-tags-completion-table (org-agenda-files))))
  11168. org-last-tags-completion-table table
  11169. current-tags (org-split-string current ":")
  11170. inherited-tags (nreverse
  11171. (nthcdr (length current-tags)
  11172. (nreverse (org-get-tags-at))))
  11173. tags
  11174. (if (or (eq t org-use-fast-tag-selection)
  11175. (and org-use-fast-tag-selection
  11176. (delq nil (mapcar 'cdr table))))
  11177. (org-fast-tag-selection
  11178. current-tags inherited-tags table
  11179. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11180. (let ((org-add-colon-after-tag-completion t))
  11181. (org-trim
  11182. (org-without-partial-completion
  11183. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11184. nil nil current 'org-tags-history)))))))
  11185. (while (string-match "[-+&]+" tags)
  11186. ;; No boolean logic, just a list
  11187. (setq tags (replace-match ":" t t tags))))
  11188. (if org-tags-sort-function
  11189. (setq tags (mapconcat 'identity
  11190. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11191. org-tags-sort-function) ":")))
  11192. (if (string-match "\\`[\t ]*\\'" tags)
  11193. (setq tags "")
  11194. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11195. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11196. ;; Insert new tags at the correct column
  11197. (beginning-of-line 1)
  11198. (cond
  11199. ((and (equal current "") (equal tags "")))
  11200. ((re-search-forward
  11201. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11202. (point-at-eol) t)
  11203. (if (equal tags "")
  11204. (setq rpl "")
  11205. (goto-char (match-beginning 0))
  11206. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11207. (1+ (point)) (point))
  11208. c1 (max (1+ c0) (if (> org-tags-column 0)
  11209. org-tags-column
  11210. (- (- org-tags-column) (length tags))))
  11211. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11212. (replace-match rpl t t)
  11213. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11214. tags)
  11215. (t (error "Tags alignment failed")))
  11216. (org-move-to-column col)
  11217. (unless just-align
  11218. (run-hooks 'org-after-tags-change-hook)))))
  11219. (defun org-change-tag-in-region (beg end tag off)
  11220. "Add or remove TAG for each entry in the region.
  11221. This works in the agenda, and also in an org-mode buffer."
  11222. (interactive
  11223. (list (region-beginning) (region-end)
  11224. (let ((org-last-tags-completion-table
  11225. (if (org-mode-p)
  11226. (org-get-buffer-tags)
  11227. (org-global-tags-completion-table))))
  11228. (org-icompleting-read
  11229. "Tag: " 'org-tags-completion-function nil nil nil
  11230. 'org-tags-history))
  11231. (progn
  11232. (message "[s]et or [r]emove? ")
  11233. (equal (read-char-exclusive) ?r))))
  11234. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11235. (let ((agendap (equal major-mode 'org-agenda-mode))
  11236. l1 l2 m buf pos newhead (cnt 0))
  11237. (goto-char end)
  11238. (setq l2 (1- (org-current-line)))
  11239. (goto-char beg)
  11240. (setq l1 (org-current-line))
  11241. (loop for l from l1 to l2 do
  11242. (org-goto-line l)
  11243. (setq m (get-text-property (point) 'org-hd-marker))
  11244. (when (or (and (org-mode-p) (org-on-heading-p))
  11245. (and agendap m))
  11246. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11247. pos (if agendap m (point)))
  11248. (with-current-buffer buf
  11249. (save-excursion
  11250. (save-restriction
  11251. (goto-char pos)
  11252. (setq cnt (1+ cnt))
  11253. (org-toggle-tag tag (if off 'off 'on))
  11254. (setq newhead (org-get-heading)))))
  11255. (and agendap (org-agenda-change-all-lines newhead m))))
  11256. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11257. (defun org-tags-completion-function (string predicate &optional flag)
  11258. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11259. (confirm (lambda (x) (stringp (car x)))))
  11260. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11261. (setq s1 (match-string 1 string)
  11262. s2 (match-string 2 string))
  11263. (setq s1 "" s2 string))
  11264. (cond
  11265. ((eq flag nil)
  11266. ;; try completion
  11267. (setq rtn (try-completion s2 ctable confirm))
  11268. (if (stringp rtn)
  11269. (setq rtn
  11270. (concat s1 s2 (substring rtn (length s2))
  11271. (if (and org-add-colon-after-tag-completion
  11272. (assoc rtn ctable))
  11273. ":" ""))))
  11274. rtn)
  11275. ((eq flag t)
  11276. ;; all-completions
  11277. (all-completions s2 ctable confirm)
  11278. )
  11279. ((eq flag 'lambda)
  11280. ;; exact match?
  11281. (assoc s2 ctable)))
  11282. ))
  11283. (defun org-fast-tag-insert (kwd tags face &optional end)
  11284. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11285. (insert (format "%-12s" (concat kwd ":"))
  11286. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11287. (or end "")))
  11288. (defun org-fast-tag-show-exit (flag)
  11289. (save-excursion
  11290. (org-goto-line 3)
  11291. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11292. (replace-match ""))
  11293. (when flag
  11294. (end-of-line 1)
  11295. (org-move-to-column (- (window-width) 19) t)
  11296. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11297. (defun org-set-current-tags-overlay (current prefix)
  11298. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11299. (if (featurep 'xemacs)
  11300. (org-overlay-display org-tags-overlay (concat prefix s)
  11301. 'secondary-selection)
  11302. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11303. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11304. (defvar org-last-tag-selection-key nil)
  11305. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11306. "Fast tag selection with single keys.
  11307. CURRENT is the current list of tags in the headline, INHERITED is the
  11308. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11309. possibly with grouping information. TODO-TABLE is a similar table with
  11310. TODO keywords, should these have keys assigned to them.
  11311. If the keys are nil, a-z are automatically assigned.
  11312. Returns the new tags string, or nil to not change the current settings."
  11313. (let* ((fulltable (append table todo-table))
  11314. (maxlen (apply 'max (mapcar
  11315. (lambda (x)
  11316. (if (stringp (car x)) (string-width (car x)) 0))
  11317. fulltable)))
  11318. (buf (current-buffer))
  11319. (expert (eq org-fast-tag-selection-single-key 'expert))
  11320. (buffer-tags nil)
  11321. (fwidth (+ maxlen 3 1 3))
  11322. (ncol (/ (- (window-width) 4) fwidth))
  11323. (i-face 'org-done)
  11324. (c-face 'org-todo)
  11325. tg cnt e c char c1 c2 ntable tbl rtn
  11326. ov-start ov-end ov-prefix
  11327. (exit-after-next org-fast-tag-selection-single-key)
  11328. (done-keywords org-done-keywords)
  11329. groups ingroup)
  11330. (save-excursion
  11331. (beginning-of-line 1)
  11332. (if (looking-at
  11333. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11334. (setq ov-start (match-beginning 1)
  11335. ov-end (match-end 1)
  11336. ov-prefix "")
  11337. (setq ov-start (1- (point-at-eol))
  11338. ov-end (1+ ov-start))
  11339. (skip-chars-forward "^\n\r")
  11340. (setq ov-prefix
  11341. (concat
  11342. (buffer-substring (1- (point)) (point))
  11343. (if (> (current-column) org-tags-column)
  11344. " "
  11345. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11346. (move-overlay org-tags-overlay ov-start ov-end)
  11347. (save-window-excursion
  11348. (if expert
  11349. (set-buffer (get-buffer-create " *Org tags*"))
  11350. (delete-other-windows)
  11351. (split-window-vertically)
  11352. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11353. (erase-buffer)
  11354. (org-set-local 'org-done-keywords done-keywords)
  11355. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11356. (org-fast-tag-insert "Current" current c-face "\n\n")
  11357. (org-fast-tag-show-exit exit-after-next)
  11358. (org-set-current-tags-overlay current ov-prefix)
  11359. (setq tbl fulltable char ?a cnt 0)
  11360. (while (setq e (pop tbl))
  11361. (cond
  11362. ((equal (car e) :startgroup)
  11363. (push '() groups) (setq ingroup t)
  11364. (when (not (= cnt 0))
  11365. (setq cnt 0)
  11366. (insert "\n"))
  11367. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11368. ((equal (car e) :endgroup)
  11369. (setq ingroup nil cnt 0)
  11370. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11371. ((equal e '(:newline))
  11372. (when (not (= cnt 0))
  11373. (setq cnt 0)
  11374. (insert "\n")
  11375. (setq e (car tbl))
  11376. (while (equal (car tbl) '(:newline))
  11377. (insert "\n")
  11378. (setq tbl (cdr tbl)))))
  11379. (t
  11380. (setq tg (copy-sequence (car e)) c2 nil)
  11381. (if (cdr e)
  11382. (setq c (cdr e))
  11383. ;; automatically assign a character.
  11384. (setq c1 (string-to-char
  11385. (downcase (substring
  11386. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11387. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11388. (while (or (rassoc char ntable) (rassoc char table))
  11389. (setq char (1+ char)))
  11390. (setq c2 c1))
  11391. (setq c (or c2 char)))
  11392. (if ingroup (push tg (car groups)))
  11393. (setq tg (org-add-props tg nil 'face
  11394. (cond
  11395. ((not (assoc tg table))
  11396. (org-get-todo-face tg))
  11397. ((member tg current) c-face)
  11398. ((member tg inherited) i-face)
  11399. (t nil))))
  11400. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11401. (insert "[" c "] " tg (make-string
  11402. (- fwidth 4 (length tg)) ?\ ))
  11403. (push (cons tg c) ntable)
  11404. (when (= (setq cnt (1+ cnt)) ncol)
  11405. (insert "\n")
  11406. (if ingroup (insert " "))
  11407. (setq cnt 0)))))
  11408. (setq ntable (nreverse ntable))
  11409. (insert "\n")
  11410. (goto-char (point-min))
  11411. (if (not expert) (org-fit-window-to-buffer))
  11412. (setq rtn
  11413. (catch 'exit
  11414. (while t
  11415. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11416. (if (not groups) "no " "")
  11417. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11418. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11419. (setq org-last-tag-selection-key c)
  11420. (cond
  11421. ((= c ?\r) (throw 'exit t))
  11422. ((= c ?!)
  11423. (setq groups (not groups))
  11424. (goto-char (point-min))
  11425. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11426. ((= c ?\C-c)
  11427. (if (not expert)
  11428. (org-fast-tag-show-exit
  11429. (setq exit-after-next (not exit-after-next)))
  11430. (setq expert nil)
  11431. (delete-other-windows)
  11432. (split-window-vertically)
  11433. (org-switch-to-buffer-other-window " *Org tags*")
  11434. (org-fit-window-to-buffer)))
  11435. ((or (= c ?\C-g)
  11436. (and (= c ?q) (not (rassoc c ntable))))
  11437. (org-detach-overlay org-tags-overlay)
  11438. (setq quit-flag t))
  11439. ((= c ?\ )
  11440. (setq current nil)
  11441. (if exit-after-next (setq exit-after-next 'now)))
  11442. ((= c ?\t)
  11443. (condition-case nil
  11444. (setq tg (org-icompleting-read
  11445. "Tag: "
  11446. (or buffer-tags
  11447. (with-current-buffer buf
  11448. (org-get-buffer-tags)))))
  11449. (quit (setq tg "")))
  11450. (when (string-match "\\S-" tg)
  11451. (add-to-list 'buffer-tags (list tg))
  11452. (if (member tg current)
  11453. (setq current (delete tg current))
  11454. (push tg current)))
  11455. (if exit-after-next (setq exit-after-next 'now)))
  11456. ((setq e (rassoc c todo-table) tg (car e))
  11457. (with-current-buffer buf
  11458. (save-excursion (org-todo tg)))
  11459. (if exit-after-next (setq exit-after-next 'now)))
  11460. ((setq e (rassoc c ntable) tg (car e))
  11461. (if (member tg current)
  11462. (setq current (delete tg current))
  11463. (loop for g in groups do
  11464. (if (member tg g)
  11465. (mapc (lambda (x)
  11466. (setq current (delete x current)))
  11467. g)))
  11468. (push tg current))
  11469. (if exit-after-next (setq exit-after-next 'now))))
  11470. ;; Create a sorted list
  11471. (setq current
  11472. (sort current
  11473. (lambda (a b)
  11474. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11475. (if (eq exit-after-next 'now) (throw 'exit t))
  11476. (goto-char (point-min))
  11477. (beginning-of-line 2)
  11478. (delete-region (point) (point-at-eol))
  11479. (org-fast-tag-insert "Current" current c-face)
  11480. (org-set-current-tags-overlay current ov-prefix)
  11481. (while (re-search-forward
  11482. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11483. (setq tg (match-string 1))
  11484. (add-text-properties
  11485. (match-beginning 1) (match-end 1)
  11486. (list 'face
  11487. (cond
  11488. ((member tg current) c-face)
  11489. ((member tg inherited) i-face)
  11490. (t (get-text-property (match-beginning 1) 'face))))))
  11491. (goto-char (point-min)))))
  11492. (org-detach-overlay org-tags-overlay)
  11493. (if rtn
  11494. (mapconcat 'identity current ":")
  11495. nil))))
  11496. (defun org-get-tags-string ()
  11497. "Get the TAGS string in the current headline."
  11498. (unless (org-on-heading-p t)
  11499. (error "Not on a heading"))
  11500. (save-excursion
  11501. (beginning-of-line 1)
  11502. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11503. (org-match-string-no-properties 1)
  11504. "")))
  11505. (defun org-get-tags ()
  11506. "Get the list of tags specified in the current headline."
  11507. (org-split-string (org-get-tags-string) ":"))
  11508. (defun org-get-buffer-tags ()
  11509. "Get a table of all tags used in the buffer, for completion."
  11510. (let (tags)
  11511. (save-excursion
  11512. (goto-char (point-min))
  11513. (while (re-search-forward
  11514. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11515. (when (equal (char-after (point-at-bol 0)) ?*)
  11516. (mapc (lambda (x) (add-to-list 'tags x))
  11517. (org-split-string (org-match-string-no-properties 1) ":")))))
  11518. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11519. (mapcar 'list tags)))
  11520. ;;;; The mapping API
  11521. ;;;###autoload
  11522. (defun org-map-entries (func &optional match scope &rest skip)
  11523. "Call FUNC at each headline selected by MATCH in SCOPE.
  11524. FUNC is a function or a lisp form. The function will be called without
  11525. arguments, with the cursor positioned at the beginning of the headline.
  11526. The return values of all calls to the function will be collected and
  11527. returned as a list.
  11528. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11529. does not need to preserve point. After evaluation, the cursor will be
  11530. moved to the end of the line (presumably of the headline of the
  11531. processed entry) and search continues from there. Under some
  11532. circumstances, this may not produce the wanted results. For example,
  11533. if you have removed (e.g. archived) the current (sub)tree it could
  11534. mean that the next entry will be skipped entirely. In such cases, you
  11535. can specify the position from where search should continue by making
  11536. FUNC set the variable `org-map-continue-from' to the desired buffer
  11537. position.
  11538. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11539. Only headlines that are matched by this query will be considered during
  11540. the iteration. When MATCH is nil or t, all headlines will be
  11541. visited by the iteration.
  11542. SCOPE determines the scope of this command. It can be any of:
  11543. nil The current buffer, respecting the restriction if any
  11544. tree The subtree started with the entry at point
  11545. file The current buffer, without restriction
  11546. file-with-archives
  11547. The current buffer, and any archives associated with it
  11548. agenda All agenda files
  11549. agenda-with-archives
  11550. All agenda files with any archive files associated with them
  11551. \(file1 file2 ...)
  11552. If this is a list, all files in the list will be scanned
  11553. The remaining args are treated as settings for the skipping facilities of
  11554. the scanner. The following items can be given here:
  11555. archive skip trees with the archive tag.
  11556. comment skip trees with the COMMENT keyword
  11557. function or Emacs Lisp form:
  11558. will be used as value for `org-agenda-skip-function', so whenever
  11559. the function returns t, FUNC will not be called for that
  11560. entry and search will continue from the point where the
  11561. function leaves it.
  11562. If your function needs to retrieve the tags including inherited tags
  11563. at the *current* entry, you can use the value of the variable
  11564. `org-scanner-tags' which will be much faster than getting the value
  11565. with `org-get-tags-at'. If your function gets properties with
  11566. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11567. to t around the call to `org-entry-properties' to get the same speedup.
  11568. Note that if your function moves around to retrieve tags and properties at
  11569. a *different* entry, you cannot use these techniques."
  11570. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11571. (org-agenda-skip-archived-trees (memq 'archive skip))
  11572. (org-agenda-skip-comment-trees (memq 'comment skip))
  11573. (org-agenda-skip-function
  11574. (car (org-delete-all '(comment archive) skip)))
  11575. (org-tags-match-list-sublevels t)
  11576. matcher file res
  11577. org-todo-keywords-for-agenda
  11578. org-done-keywords-for-agenda
  11579. org-todo-keyword-alist-for-agenda
  11580. org-drawers-for-agenda
  11581. org-tag-alist-for-agenda)
  11582. (cond
  11583. ((eq match t) (setq matcher t))
  11584. ((eq match nil) (setq matcher t))
  11585. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11586. (save-excursion
  11587. (save-restriction
  11588. (when (eq scope 'tree)
  11589. (org-back-to-heading t)
  11590. (org-narrow-to-subtree)
  11591. (setq scope nil))
  11592. (if (not scope)
  11593. (progn
  11594. (org-prepare-agenda-buffers
  11595. (list (buffer-file-name (current-buffer))))
  11596. (setq res (org-scan-tags func matcher)))
  11597. ;; Get the right scope
  11598. (cond
  11599. ((and scope (listp scope) (symbolp (car scope)))
  11600. (setq scope (eval scope)))
  11601. ((eq scope 'agenda)
  11602. (setq scope (org-agenda-files t)))
  11603. ((eq scope 'agenda-with-archives)
  11604. (setq scope (org-agenda-files t))
  11605. (setq scope (org-add-archive-files scope)))
  11606. ((eq scope 'file)
  11607. (setq scope (list (buffer-file-name))))
  11608. ((eq scope 'file-with-archives)
  11609. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11610. (org-prepare-agenda-buffers scope)
  11611. (while (setq file (pop scope))
  11612. (with-current-buffer (org-find-base-buffer-visiting file)
  11613. (save-excursion
  11614. (save-restriction
  11615. (widen)
  11616. (goto-char (point-min))
  11617. (setq res (append res (org-scan-tags func matcher))))))))))
  11618. res))
  11619. ;;;; Properties
  11620. ;;; Setting and retrieving properties
  11621. (defconst org-special-properties
  11622. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11623. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11624. "The special properties valid in Org-mode.
  11625. These are properties that are not defined in the property drawer,
  11626. but in some other way.")
  11627. (defconst org-default-properties
  11628. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11629. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11630. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11631. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11632. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  11633. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11634. "Some properties that are used by Org-mode for various purposes.
  11635. Being in this list makes sure that they are offered for completion.")
  11636. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11637. "Regular expression matching the first line of a property drawer.")
  11638. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11639. "Regular expression matching the last line of a property drawer.")
  11640. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11641. "Regular expression matching the first line of a property drawer.")
  11642. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11643. "Regular expression matching the first line of a property drawer.")
  11644. (defconst org-property-drawer-re
  11645. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11646. org-property-end-re "\\)\n?")
  11647. "Matches an entire property drawer.")
  11648. (defconst org-clock-drawer-re
  11649. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11650. org-property-end-re "\\)\n?")
  11651. "Matches an entire clock drawer.")
  11652. (defun org-property-action ()
  11653. "Do an action on properties."
  11654. (interactive)
  11655. (let (c)
  11656. (org-at-property-p)
  11657. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11658. (setq c (read-char-exclusive))
  11659. (cond
  11660. ((equal c ?s)
  11661. (call-interactively 'org-set-property))
  11662. ((equal c ?d)
  11663. (call-interactively 'org-delete-property))
  11664. ((equal c ?D)
  11665. (call-interactively 'org-delete-property-globally))
  11666. ((equal c ?c)
  11667. (call-interactively 'org-compute-property-at-point))
  11668. (t (error "No such property action %c" c)))))
  11669. (defun org-set-effort (&optional value)
  11670. "Set the effort property of the current entry.
  11671. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11672. allowed value."
  11673. (interactive "P")
  11674. (if (equal value 0) (setq value 10))
  11675. (let* ((completion-ignore-case t)
  11676. (prop org-effort-property)
  11677. (cur (org-entry-get nil prop))
  11678. (allowed (org-property-get-allowed-values nil prop 'table))
  11679. (existing (mapcar 'list (org-property-values prop)))
  11680. rpl
  11681. (val (cond
  11682. ((stringp value) value)
  11683. ((and allowed (integerp value))
  11684. (or (car (nth (1- value) allowed))
  11685. (car (org-last allowed))))
  11686. (allowed
  11687. (message "Select 1-9,0, [RET%s]: %s"
  11688. (if cur (concat "=" cur) "")
  11689. (mapconcat 'car allowed " "))
  11690. (setq rpl (read-char-exclusive))
  11691. (if (equal rpl ?\r)
  11692. cur
  11693. (setq rpl (- rpl ?0))
  11694. (if (equal rpl 0) (setq rpl 10))
  11695. (if (and (> rpl 0) (<= rpl (length allowed)))
  11696. (car (nth (1- rpl) allowed))
  11697. (org-completing-read "Effort: " allowed nil))))
  11698. (t
  11699. (let (org-completion-use-ido org-completion-use-iswitchb)
  11700. (org-completing-read
  11701. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11702. (concat "[" cur "]") "")
  11703. ": ")
  11704. existing nil nil "" nil cur))))))
  11705. (unless (equal (org-entry-get nil prop) val)
  11706. (org-entry-put nil prop val))
  11707. (message "%s is now %s" prop val)))
  11708. (defun org-at-property-p ()
  11709. "Is cursor inside a property drawer?"
  11710. (save-excursion
  11711. (beginning-of-line 1)
  11712. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11713. (let ((match (match-data)) ;; Keep match-data for use by calling
  11714. (p (point)) ;; procedures.
  11715. (range (unless (org-before-first-heading-p)
  11716. (org-get-property-block))))
  11717. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11718. (set-match-data match))))))
  11719. (defun org-get-property-block (&optional beg end force)
  11720. "Return the (beg . end) range of the body of the property drawer.
  11721. BEG and END can be beginning and end of subtree, if not given
  11722. they will be found.
  11723. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11724. (catch 'exit
  11725. (save-excursion
  11726. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11727. (end (or end (progn (outline-next-heading) (point)))))
  11728. (goto-char beg)
  11729. (if (re-search-forward org-property-start-re end t)
  11730. (setq beg (1+ (match-end 0)))
  11731. (if force
  11732. (save-excursion
  11733. (org-insert-property-drawer)
  11734. (setq end (progn (outline-next-heading) (point))))
  11735. (throw 'exit nil))
  11736. (goto-char beg)
  11737. (if (re-search-forward org-property-start-re end t)
  11738. (setq beg (1+ (match-end 0)))))
  11739. (if (re-search-forward org-property-end-re end t)
  11740. (setq end (match-beginning 0))
  11741. (or force (throw 'exit nil))
  11742. (goto-char beg)
  11743. (setq end beg)
  11744. (org-indent-line-function)
  11745. (insert ":END:\n"))
  11746. (cons beg end)))))
  11747. (defun org-entry-properties (&optional pom which specific)
  11748. "Get all properties of the entry at point-or-marker POM.
  11749. This includes the TODO keyword, the tags, time strings for deadline,
  11750. scheduled, and clocking, and any additional properties defined in the
  11751. entry. The return value is an alist, keys may occur multiple times
  11752. if the property key was used several times.
  11753. POM may also be nil, in which case the current entry is used.
  11754. If WHICH is nil or `all', get all properties. If WHICH is
  11755. `special' or `standard', only get that subclass. If WHICH
  11756. is a string only get exactly this property. Specific can be a string, the
  11757. specific property we are interested in. Specifying it can speed
  11758. things up because then unnecessary parsing is avoided."
  11759. (setq which (or which 'all))
  11760. (org-with-point-at pom
  11761. (let ((clockstr (substring org-clock-string 0 -1))
  11762. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11763. (case-fold-search nil)
  11764. beg end range props sum-props key value string clocksum)
  11765. (save-excursion
  11766. (when (condition-case nil
  11767. (and (org-mode-p) (org-back-to-heading t))
  11768. (error nil))
  11769. (setq beg (point))
  11770. (setq sum-props (get-text-property (point) 'org-summaries))
  11771. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11772. (outline-next-heading)
  11773. (setq end (point))
  11774. (when (memq which '(all special))
  11775. ;; Get the special properties, like TODO and tags
  11776. (goto-char beg)
  11777. (when (and (or (not specific) (string= specific "TODO"))
  11778. (looking-at org-todo-line-regexp) (match-end 2))
  11779. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11780. (when (and (or (not specific) (string= specific "PRIORITY"))
  11781. (looking-at org-priority-regexp))
  11782. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11783. (when (and (or (not specific) (string= specific "TAGS"))
  11784. (setq value (org-get-tags-string))
  11785. (string-match "\\S-" value))
  11786. (push (cons "TAGS" value) props))
  11787. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11788. (setq value (org-get-tags-at)))
  11789. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11790. ":"))
  11791. props))
  11792. (when (or (not specific) (string= specific "BLOCKED"))
  11793. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11794. (when (or (not specific)
  11795. (member specific org-all-time-keywords)
  11796. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11797. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11798. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11799. string (if (equal key clockstr)
  11800. (org-no-properties
  11801. (org-trim
  11802. (buffer-substring
  11803. (match-beginning 3) (goto-char (point-at-eol)))))
  11804. (substring (org-match-string-no-properties 3) 1 -1)))
  11805. (unless key
  11806. (if (= (char-after (match-beginning 3)) ?\[)
  11807. (setq key "TIMESTAMP_IA")
  11808. (setq key "TIMESTAMP")))
  11809. (when (or (equal key clockstr) (not (assoc key props)))
  11810. (push (cons key string) props))))
  11811. )
  11812. (when (memq which '(all standard))
  11813. ;; Get the standard properties, like :PROP: ...
  11814. (setq range (org-get-property-block beg end))
  11815. (when range
  11816. (goto-char (car range))
  11817. (while (re-search-forward
  11818. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11819. (cdr range) t)
  11820. (setq key (org-match-string-no-properties 1)
  11821. value (org-trim (or (org-match-string-no-properties 2) "")))
  11822. (unless (member key excluded)
  11823. (push (cons key (or value "")) props)))))
  11824. (if clocksum
  11825. (push (cons "CLOCKSUM"
  11826. (org-columns-number-to-string (/ (float clocksum) 60.)
  11827. 'add_times))
  11828. props))
  11829. (unless (assoc "CATEGORY" props)
  11830. (setq value (or (org-get-category)
  11831. (progn (org-refresh-category-properties)
  11832. (org-get-category))))
  11833. (push (cons "CATEGORY" value) props))
  11834. (append sum-props (nreverse props)))))))
  11835. (defun org-entry-get (pom property &optional inherit)
  11836. "Get value of PROPERTY for entry at point-or-marker POM.
  11837. If INHERIT is non-nil and the entry does not have the property,
  11838. then also check higher levels of the hierarchy.
  11839. If INHERIT is the symbol `selective', use inheritance only if the setting
  11840. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11841. If the property is present but empty, the return value is the empty string.
  11842. If the property is not present at all, nil is returned."
  11843. (org-with-point-at pom
  11844. (if (and inherit (if (eq inherit 'selective)
  11845. (org-property-inherit-p property)
  11846. t))
  11847. (org-entry-get-with-inheritance property)
  11848. (if (member property org-special-properties)
  11849. ;; We need a special property. Use `org-entry-properties' to
  11850. ;; retrieve it, but specify the wanted property
  11851. (cdr (assoc property (org-entry-properties nil 'special property)))
  11852. (let ((range (org-get-property-block)))
  11853. (if (and range
  11854. (goto-char (car range))
  11855. (re-search-forward
  11856. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11857. (cdr range) t))
  11858. ;; Found the property, return it.
  11859. (if (match-end 1)
  11860. (org-match-string-no-properties 1)
  11861. "")))))))
  11862. (defun org-property-or-variable-value (var &optional inherit)
  11863. "Check if there is a property fixing the value of VAR.
  11864. If yes, return this value. If not, return the current value of the variable."
  11865. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11866. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11867. (read prop)
  11868. (symbol-value var))))
  11869. (defun org-entry-delete (pom property)
  11870. "Delete the property PROPERTY from entry at point-or-marker POM."
  11871. (org-with-point-at pom
  11872. (if (member property org-special-properties)
  11873. nil ; cannot delete these properties.
  11874. (let ((range (org-get-property-block)))
  11875. (if (and range
  11876. (goto-char (car range))
  11877. (re-search-forward
  11878. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11879. (cdr range) t))
  11880. (progn
  11881. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11882. t)
  11883. nil)))))
  11884. ;; Multi-values properties are properties that contain multiple values
  11885. ;; These values are assumed to be single words, separated by whitespace.
  11886. (defun org-entry-add-to-multivalued-property (pom property value)
  11887. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11888. (let* ((old (org-entry-get pom property))
  11889. (values (and old (org-split-string old "[ \t]"))))
  11890. (setq value (org-entry-protect-space value))
  11891. (unless (member value values)
  11892. (setq values (cons value values))
  11893. (org-entry-put pom property
  11894. (mapconcat 'identity values " ")))))
  11895. (defun org-entry-remove-from-multivalued-property (pom property value)
  11896. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11897. (let* ((old (org-entry-get pom property))
  11898. (values (and old (org-split-string old "[ \t]"))))
  11899. (setq value (org-entry-protect-space value))
  11900. (when (member value values)
  11901. (setq values (delete value values))
  11902. (org-entry-put pom property
  11903. (mapconcat 'identity values " ")))))
  11904. (defun org-entry-member-in-multivalued-property (pom property value)
  11905. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11906. (let* ((old (org-entry-get pom property))
  11907. (values (and old (org-split-string old "[ \t]"))))
  11908. (setq value (org-entry-protect-space value))
  11909. (member value values)))
  11910. (defun org-entry-get-multivalued-property (pom property)
  11911. "Return a list of values in a multivalued property."
  11912. (let* ((value (org-entry-get pom property))
  11913. (values (and value (org-split-string value "[ \t]"))))
  11914. (mapcar 'org-entry-restore-space values)))
  11915. (defun org-entry-put-multivalued-property (pom property &rest values)
  11916. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11917. VALUES should be a list of strings. Spaces will be protected."
  11918. (org-entry-put pom property
  11919. (mapconcat 'org-entry-protect-space values " "))
  11920. (let* ((value (org-entry-get pom property))
  11921. (values (and value (org-split-string value "[ \t]"))))
  11922. (mapcar 'org-entry-restore-space values)))
  11923. (defun org-entry-protect-space (s)
  11924. "Protect spaces and newline in string S."
  11925. (while (string-match " " s)
  11926. (setq s (replace-match "%20" t t s)))
  11927. (while (string-match "\n" s)
  11928. (setq s (replace-match "%0A" t t s)))
  11929. s)
  11930. (defun org-entry-restore-space (s)
  11931. "Restore spaces and newline in string S."
  11932. (while (string-match "%20" s)
  11933. (setq s (replace-match " " t t s)))
  11934. (while (string-match "%0A" s)
  11935. (setq s (replace-match "\n" t t s)))
  11936. s)
  11937. (defvar org-entry-property-inherited-from (make-marker)
  11938. "Marker pointing to the entry from where a property was inherited.
  11939. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11940. location of the entry where the inheritance search matched. If there was
  11941. no match, the marker will point nowhere.
  11942. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11943. is set.")
  11944. (defun org-entry-get-with-inheritance (property)
  11945. "Get entry property, and search higher levels if not present."
  11946. (move-marker org-entry-property-inherited-from nil)
  11947. (let (tmp)
  11948. (save-excursion
  11949. (save-restriction
  11950. (widen)
  11951. (catch 'ex
  11952. (while t
  11953. (when (setq tmp (org-entry-get nil property))
  11954. (org-back-to-heading t)
  11955. (move-marker org-entry-property-inherited-from (point))
  11956. (throw 'ex tmp))
  11957. (or (org-up-heading-safe) (throw 'ex nil)))))
  11958. (or tmp
  11959. (cdr (assoc property org-file-properties))
  11960. (cdr (assoc property org-global-properties))
  11961. (cdr (assoc property org-global-properties-fixed))))))
  11962. (defvar org-property-changed-functions nil
  11963. "Hook called when the value of a property has changed.
  11964. Each hook function should accept two arguments, the name of the property
  11965. and the new value.")
  11966. (defun org-entry-put (pom property value)
  11967. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11968. (org-with-point-at pom
  11969. (org-back-to-heading t)
  11970. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11971. range)
  11972. (cond
  11973. ((equal property "TODO")
  11974. (when (and (stringp value) (string-match "\\S-" value)
  11975. (not (member value org-todo-keywords-1)))
  11976. (error "\"%s\" is not a valid TODO state" value))
  11977. (if (or (not value)
  11978. (not (string-match "\\S-" value)))
  11979. (setq value 'none))
  11980. (org-todo value)
  11981. (org-set-tags nil 'align))
  11982. ((equal property "PRIORITY")
  11983. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11984. (string-to-char value) ?\ ))
  11985. (org-set-tags nil 'align))
  11986. ((equal property "SCHEDULED")
  11987. (if (re-search-forward org-scheduled-time-regexp end t)
  11988. (cond
  11989. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11990. ((eq value 'later) (org-timestamp-change 1 'day))
  11991. (t (call-interactively 'org-schedule)))
  11992. (call-interactively 'org-schedule)))
  11993. ((equal property "DEADLINE")
  11994. (if (re-search-forward org-deadline-time-regexp end t)
  11995. (cond
  11996. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11997. ((eq value 'later) (org-timestamp-change 1 'day))
  11998. (t (call-interactively 'org-deadline)))
  11999. (call-interactively 'org-deadline)))
  12000. ((member property org-special-properties)
  12001. (error "The %s property can not yet be set with `org-entry-put'"
  12002. property))
  12003. (t ; a non-special property
  12004. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12005. (setq range (org-get-property-block beg end 'force))
  12006. (goto-char (car range))
  12007. (if (re-search-forward
  12008. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  12009. (progn
  12010. (delete-region (match-beginning 1) (match-end 1))
  12011. (goto-char (match-beginning 1)))
  12012. (goto-char (cdr range))
  12013. (insert "\n")
  12014. (backward-char 1)
  12015. (org-indent-line-function)
  12016. (insert ":" property ":"))
  12017. (and value (insert " " value))
  12018. (org-indent-line-function)))))
  12019. (run-hook-with-args 'org-property-changed-functions property value)))
  12020. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12021. "Get all property keys in the current buffer.
  12022. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12023. like tags and TODO state.
  12024. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12025. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  12026. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12027. formats in the current buffer."
  12028. (let (rtn range cfmt s p)
  12029. (save-excursion
  12030. (save-restriction
  12031. (widen)
  12032. (goto-char (point-min))
  12033. (while (re-search-forward org-property-start-re nil t)
  12034. (setq range (org-get-property-block))
  12035. (goto-char (car range))
  12036. (while (re-search-forward
  12037. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12038. (cdr range) t)
  12039. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12040. (outline-next-heading))))
  12041. (when include-specials
  12042. (setq rtn (append org-special-properties rtn)))
  12043. (when include-defaults
  12044. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12045. (add-to-list 'rtn org-effort-property))
  12046. (when include-columns
  12047. (save-excursion
  12048. (save-restriction
  12049. (widen)
  12050. (goto-char (point-min))
  12051. (while (re-search-forward
  12052. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12053. nil t)
  12054. (setq cfmt (match-string 2) s 0)
  12055. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12056. cfmt s)
  12057. (setq s (match-end 0)
  12058. p (match-string 1 cfmt))
  12059. (unless (or (equal p "ITEM")
  12060. (member p org-special-properties))
  12061. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12062. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12063. (defun org-property-values (key)
  12064. "Return a list of all values of property KEY."
  12065. (save-excursion
  12066. (save-restriction
  12067. (widen)
  12068. (goto-char (point-min))
  12069. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12070. values)
  12071. (while (re-search-forward re nil t)
  12072. (add-to-list 'values (org-trim (match-string 1))))
  12073. (delete "" values)))))
  12074. (defun org-insert-property-drawer ()
  12075. "Insert a property drawer into the current entry."
  12076. (interactive)
  12077. (org-back-to-heading t)
  12078. (looking-at outline-regexp)
  12079. (let ((indent (if org-adapt-indentation
  12080. (- (match-end 0)(match-beginning 0))
  12081. 0))
  12082. (beg (point))
  12083. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12084. end hiddenp)
  12085. (outline-next-heading)
  12086. (setq end (point))
  12087. (goto-char beg)
  12088. (while (re-search-forward re end t))
  12089. (setq hiddenp (org-invisible-p))
  12090. (end-of-line 1)
  12091. (and (equal (char-after) ?\n) (forward-char 1))
  12092. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12093. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12094. ;; just skip this line
  12095. (beginning-of-line 2)
  12096. ;; Drawer start, find the end
  12097. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12098. (beginning-of-line 1)))
  12099. (org-skip-over-state-notes)
  12100. (skip-chars-backward " \t\n\r")
  12101. (if (eq (char-before) ?*) (forward-char 1))
  12102. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12103. (beginning-of-line 0)
  12104. (org-indent-to-column indent)
  12105. (beginning-of-line 2)
  12106. (org-indent-to-column indent)
  12107. (beginning-of-line 0)
  12108. (if hiddenp
  12109. (save-excursion
  12110. (org-back-to-heading t)
  12111. (hide-entry))
  12112. (org-flag-drawer t))))
  12113. (defun org-set-property (property value)
  12114. "In the current entry, set PROPERTY to VALUE.
  12115. When called interactively, this will prompt for a property name, offering
  12116. completion on existing and default properties. And then it will prompt
  12117. for a value, offering completion either on allowed values (via an inherited
  12118. xxx_ALL property) or on existing values in other instances of this property
  12119. in the current file."
  12120. (interactive
  12121. (let* ((completion-ignore-case t)
  12122. (keys (org-buffer-property-keys nil t t))
  12123. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12124. (prop (if (member prop0 keys)
  12125. prop0
  12126. (or (cdr (assoc (downcase prop0)
  12127. (mapcar (lambda (x) (cons (downcase x) x))
  12128. keys)))
  12129. prop0)))
  12130. (cur (org-entry-get nil prop))
  12131. (prompt (concat prop " value"
  12132. (if (and cur (string-match "\\S-" cur))
  12133. (concat " [" cur "]") "") ": "))
  12134. (allowed (org-property-get-allowed-values nil prop 'table))
  12135. (existing (mapcar 'list (org-property-values prop)))
  12136. (val (if allowed
  12137. (org-completing-read prompt allowed nil
  12138. (not (get-text-property 0 'org-unrestricted
  12139. (caar allowed))))
  12140. (let (org-completion-use-ido org-completion-use-iswitchb)
  12141. (org-completing-read prompt existing nil nil "" nil cur)))))
  12142. (list prop (if (equal val "") cur val))))
  12143. (unless (equal (org-entry-get nil property) value)
  12144. (org-entry-put nil property value)))
  12145. (defun org-delete-property (property)
  12146. "In the current entry, delete PROPERTY."
  12147. (interactive
  12148. (let* ((completion-ignore-case t)
  12149. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12150. (list prop)))
  12151. (message "Property %s %s" property
  12152. (if (org-entry-delete nil property)
  12153. "deleted"
  12154. "was not present in the entry")))
  12155. (defun org-delete-property-globally (property)
  12156. "Remove PROPERTY globally, from all entries."
  12157. (interactive
  12158. (let* ((completion-ignore-case t)
  12159. (prop (org-icompleting-read
  12160. "Globally remove property: "
  12161. (mapcar 'list (org-buffer-property-keys)))))
  12162. (list prop)))
  12163. (save-excursion
  12164. (save-restriction
  12165. (widen)
  12166. (goto-char (point-min))
  12167. (let ((cnt 0))
  12168. (while (re-search-forward
  12169. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12170. nil t)
  12171. (setq cnt (1+ cnt))
  12172. (replace-match ""))
  12173. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12174. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12175. (defun org-compute-property-at-point ()
  12176. "Compute the property at point.
  12177. This looks for an enclosing column format, extracts the operator and
  12178. then applies it to the property in the column format's scope."
  12179. (interactive)
  12180. (unless (org-at-property-p)
  12181. (error "Not at a property"))
  12182. (let ((prop (org-match-string-no-properties 2)))
  12183. (org-columns-get-format-and-top-level)
  12184. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12185. (error "No operator defined for property %s" prop))
  12186. (org-columns-compute prop)))
  12187. (defvar org-property-allowed-value-functions nil
  12188. "Hook for functions supplying allowed values for a specific property.
  12189. The functions must take a single argument, the name of the property, and
  12190. return a flat list of allowed values. If \":ETC\" is one of
  12191. the values, this means that these values are intended as defaults for
  12192. completion, but that other values should be allowed too.
  12193. The functions must return nil if they are not responsible for this
  12194. property.")
  12195. (defun org-property-get-allowed-values (pom property &optional table)
  12196. "Get allowed values for the property PROPERTY.
  12197. When TABLE is non-nil, return an alist that can directly be used for
  12198. completion."
  12199. (let (vals)
  12200. (cond
  12201. ((equal property "TODO")
  12202. (setq vals (org-with-point-at pom
  12203. (append org-todo-keywords-1 '("")))))
  12204. ((equal property "PRIORITY")
  12205. (let ((n org-lowest-priority))
  12206. (while (>= n org-highest-priority)
  12207. (push (char-to-string n) vals)
  12208. (setq n (1- n)))))
  12209. ((member property org-special-properties))
  12210. ((setq vals (run-hook-with-args-until-success
  12211. 'org-property-allowed-value-functions property)))
  12212. (t
  12213. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12214. (when (and vals (string-match "\\S-" vals))
  12215. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12216. (setq vals (mapcar (lambda (x)
  12217. (cond ((stringp x) x)
  12218. ((numberp x) (number-to-string x))
  12219. ((symbolp x) (symbol-name x))
  12220. (t "???")))
  12221. vals)))))
  12222. (when (member ":ETC" vals)
  12223. (setq vals (remove ":ETC" vals))
  12224. (org-add-props (car vals) '(org-unrestricted t)))
  12225. (if table (mapcar 'list vals) vals)))
  12226. (defun org-property-previous-allowed-value (&optional previous)
  12227. "Switch to the next allowed value for this property."
  12228. (interactive)
  12229. (org-property-next-allowed-value t))
  12230. (defun org-property-next-allowed-value (&optional previous)
  12231. "Switch to the next allowed value for this property."
  12232. (interactive)
  12233. (unless (org-at-property-p)
  12234. (error "Not at a property"))
  12235. (let* ((key (match-string 2))
  12236. (value (match-string 3))
  12237. (allowed (or (org-property-get-allowed-values (point) key)
  12238. (and (member value '("[ ]" "[-]" "[X]"))
  12239. '("[ ]" "[X]"))))
  12240. nval)
  12241. (unless allowed
  12242. (error "Allowed values for this property have not been defined"))
  12243. (if previous (setq allowed (reverse allowed)))
  12244. (if (member value allowed)
  12245. (setq nval (car (cdr (member value allowed)))))
  12246. (setq nval (or nval (car allowed)))
  12247. (if (equal nval value)
  12248. (error "Only one allowed value for this property"))
  12249. (org-at-property-p)
  12250. (replace-match (concat " :" key ": " nval) t t)
  12251. (org-indent-line-function)
  12252. (beginning-of-line 1)
  12253. (skip-chars-forward " \t")
  12254. (run-hook-with-args 'org-property-changed-functions key nval)))
  12255. (defun org-find-entry-with-id (ident)
  12256. "Locate the entry that contains the ID property with exact value IDENT.
  12257. IDENT can be a string, a symbol or a number, this function will search for
  12258. the string representation of it.
  12259. Return the position where this entry starts, or nil if there is no such entry."
  12260. (interactive "sID: ")
  12261. (let ((id (cond
  12262. ((stringp ident) ident)
  12263. ((symbol-name ident) (symbol-name ident))
  12264. ((numberp ident) (number-to-string ident))
  12265. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12266. (case-fold-search nil))
  12267. (save-excursion
  12268. (save-restriction
  12269. (widen)
  12270. (goto-char (point-min))
  12271. (when (re-search-forward
  12272. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12273. nil t)
  12274. (org-back-to-heading t)
  12275. (point))))))
  12276. ;;;; Timestamps
  12277. (defvar org-last-changed-timestamp nil)
  12278. (defvar org-last-inserted-timestamp nil
  12279. "The last time stamp inserted with `org-insert-time-stamp'.")
  12280. (defvar org-time-was-given) ; dynamically scoped parameter
  12281. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12282. (defvar org-ts-what) ; dynamically scoped parameter
  12283. (defun org-time-stamp (arg &optional inactive)
  12284. "Prompt for a date/time and insert a time stamp.
  12285. If the user specifies a time like HH:MM, or if this command is called
  12286. with a prefix argument, the time stamp will contain date and time.
  12287. Otherwise, only the date will be included. All parts of a date not
  12288. specified by the user will be filled in from the current date/time.
  12289. So if you press just return without typing anything, the time stamp
  12290. will represent the current date/time. If there is already a timestamp
  12291. at the cursor, it will be modified."
  12292. (interactive "P")
  12293. (let* ((ts nil)
  12294. (default-time
  12295. ;; Default time is either today, or, when entering a range,
  12296. ;; the range start.
  12297. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12298. (save-excursion
  12299. (re-search-backward
  12300. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12301. (- (point) 20) t)))
  12302. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12303. (current-time)))
  12304. (default-input (and ts (org-get-compact-tod ts)))
  12305. org-time-was-given org-end-time-was-given time)
  12306. (cond
  12307. ((and (org-at-timestamp-p t)
  12308. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12309. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12310. (insert "--")
  12311. (setq time (let ((this-command this-command))
  12312. (org-read-date arg 'totime nil nil
  12313. default-time default-input)))
  12314. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12315. ((org-at-timestamp-p t)
  12316. (setq time (let ((this-command this-command))
  12317. (org-read-date arg 'totime nil nil default-time default-input)))
  12318. (when (org-at-timestamp-p t) ; just to get the match data
  12319. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12320. (replace-match "")
  12321. (setq org-last-changed-timestamp
  12322. (org-insert-time-stamp
  12323. time (or org-time-was-given arg)
  12324. inactive nil nil (list org-end-time-was-given))))
  12325. (message "Timestamp updated"))
  12326. (t
  12327. (setq time (let ((this-command this-command))
  12328. (org-read-date arg 'totime nil nil default-time default-input)))
  12329. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12330. nil nil (list org-end-time-was-given))))))
  12331. ;; FIXME: can we use this for something else, like computing time differences?
  12332. (defun org-get-compact-tod (s)
  12333. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12334. (let* ((t1 (match-string 1 s))
  12335. (h1 (string-to-number (match-string 2 s)))
  12336. (m1 (string-to-number (match-string 3 s)))
  12337. (t2 (and (match-end 4) (match-string 5 s)))
  12338. (h2 (and t2 (string-to-number (match-string 6 s))))
  12339. (m2 (and t2 (string-to-number (match-string 7 s))))
  12340. dh dm)
  12341. (if (not t2)
  12342. t1
  12343. (setq dh (- h2 h1) dm (- m2 m1))
  12344. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12345. (concat t1 "+" (number-to-string dh)
  12346. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12347. (defun org-time-stamp-inactive (&optional arg)
  12348. "Insert an inactive time stamp.
  12349. An inactive time stamp is enclosed in square brackets instead of angle
  12350. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12351. does not link to the calendar and cannot be changed with the S-cursor keys.
  12352. So these are more for recording a certain time/date."
  12353. (interactive "P")
  12354. (org-time-stamp arg 'inactive))
  12355. (defvar org-date-ovl (make-overlay 1 1))
  12356. (overlay-put org-date-ovl 'face 'org-warning)
  12357. (org-detach-overlay org-date-ovl)
  12358. (defvar org-ans1) ; dynamically scoped parameter
  12359. (defvar org-ans2) ; dynamically scoped parameter
  12360. (defvar org-plain-time-of-day-regexp) ; defined below
  12361. (defvar org-overriding-default-time nil) ; dynamically scoped
  12362. (defvar org-read-date-overlay nil)
  12363. (defvar org-dcst nil) ; dynamically scoped
  12364. (defvar org-read-date-history nil)
  12365. (defvar org-read-date-final-answer nil)
  12366. (defun org-read-date (&optional with-time to-time from-string prompt
  12367. default-time default-input)
  12368. "Read a date, possibly a time, and make things smooth for the user.
  12369. The prompt will suggest to enter an ISO date, but you can also enter anything
  12370. which will at least partially be understood by `parse-time-string'.
  12371. Unrecognized parts of the date will default to the current day, month, year,
  12372. hour and minute. If this command is called to replace a timestamp at point,
  12373. of to enter the second timestamp of a range, the default time is taken from the
  12374. existing stamp. For example,
  12375. 3-2-5 --> 2003-02-05
  12376. feb 15 --> currentyear-02-15
  12377. sep 12 9 --> 2009-09-12
  12378. 12:45 --> today 12:45
  12379. 22 sept 0:34 --> currentyear-09-22 0:34
  12380. 12 --> currentyear-currentmonth-12
  12381. Fri --> nearest Friday (today or later)
  12382. etc.
  12383. Furthermore you can specify a relative date by giving, as the *first* thing
  12384. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12385. change in days weeks, months, years.
  12386. With a single plus or minus, the date is relative to today. With a double
  12387. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12388. +4d --> four days from today
  12389. +4 --> same as above
  12390. +2w --> two weeks from today
  12391. ++5 --> five days from default date
  12392. The function understands only English month and weekday abbreviations,
  12393. but this can be configured with the variables `parse-time-months' and
  12394. `parse-time-weekdays'.
  12395. While prompting, a calendar is popped up - you can also select the
  12396. date with the mouse (button 1). The calendar shows a period of three
  12397. months. To scroll it to other months, use the keys `>' and `<'.
  12398. If you don't like the calendar, turn it off with
  12399. \(setq org-read-date-popup-calendar nil)
  12400. With optional argument TO-TIME, the date will immediately be converted
  12401. to an internal time.
  12402. With an optional argument WITH-TIME, the prompt will suggest to also
  12403. insert a time. Note that when WITH-TIME is not set, you can still
  12404. enter a time, and this function will inform the calling routine about
  12405. this change. The calling routine may then choose to change the format
  12406. used to insert the time stamp into the buffer to include the time.
  12407. With optional argument FROM-STRING, read from this string instead from
  12408. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12409. the time/date that is used for everything that is not specified by the
  12410. user."
  12411. (require 'parse-time)
  12412. (let* ((org-time-stamp-rounding-minutes
  12413. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12414. (org-dcst org-display-custom-times)
  12415. (ct (org-current-time))
  12416. (def (or org-overriding-default-time default-time ct))
  12417. (defdecode (decode-time def))
  12418. (dummy (progn
  12419. (when (< (nth 2 defdecode) org-extend-today-until)
  12420. (setcar (nthcdr 2 defdecode) -1)
  12421. (setcar (nthcdr 1 defdecode) 59)
  12422. (setq def (apply 'encode-time defdecode)
  12423. defdecode (decode-time def)))))
  12424. (calendar-frame-setup nil)
  12425. (calendar-move-hook nil)
  12426. (calendar-view-diary-initially-flag nil)
  12427. (calendar-view-holidays-initially-flag nil)
  12428. (timestr (format-time-string
  12429. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12430. (prompt (concat (if prompt (concat prompt " ") "")
  12431. (format "Date+time [%s]: " timestr)))
  12432. ans (org-ans0 "") org-ans1 org-ans2 final)
  12433. (cond
  12434. (from-string (setq ans from-string))
  12435. (org-read-date-popup-calendar
  12436. (save-excursion
  12437. (save-window-excursion
  12438. (calendar)
  12439. (calendar-forward-day (- (time-to-days def)
  12440. (calendar-absolute-from-gregorian
  12441. (calendar-current-date))))
  12442. (org-eval-in-calendar nil t)
  12443. (let* ((old-map (current-local-map))
  12444. (map (copy-keymap calendar-mode-map))
  12445. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12446. (org-defkey map (kbd "RET") 'org-calendar-select)
  12447. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  12448. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  12449. (org-defkey minibuffer-local-map [(meta shift left)]
  12450. (lambda () (interactive)
  12451. (org-eval-in-calendar '(calendar-backward-month 1))))
  12452. (org-defkey minibuffer-local-map [(meta shift right)]
  12453. (lambda () (interactive)
  12454. (org-eval-in-calendar '(calendar-forward-month 1))))
  12455. (org-defkey minibuffer-local-map [(meta shift up)]
  12456. (lambda () (interactive)
  12457. (org-eval-in-calendar '(calendar-backward-year 1))))
  12458. (org-defkey minibuffer-local-map [(meta shift down)]
  12459. (lambda () (interactive)
  12460. (org-eval-in-calendar '(calendar-forward-year 1))))
  12461. (org-defkey minibuffer-local-map [?\e (shift left)]
  12462. (lambda () (interactive)
  12463. (org-eval-in-calendar '(calendar-backward-month 1))))
  12464. (org-defkey minibuffer-local-map [?\e (shift right)]
  12465. (lambda () (interactive)
  12466. (org-eval-in-calendar '(calendar-forward-month 1))))
  12467. (org-defkey minibuffer-local-map [?\e (shift up)]
  12468. (lambda () (interactive)
  12469. (org-eval-in-calendar '(calendar-backward-year 1))))
  12470. (org-defkey minibuffer-local-map [?\e (shift down)]
  12471. (lambda () (interactive)
  12472. (org-eval-in-calendar '(calendar-forward-year 1))))
  12473. (org-defkey minibuffer-local-map [(shift up)]
  12474. (lambda () (interactive)
  12475. (org-eval-in-calendar '(calendar-backward-week 1))))
  12476. (org-defkey minibuffer-local-map [(shift down)]
  12477. (lambda () (interactive)
  12478. (org-eval-in-calendar '(calendar-forward-week 1))))
  12479. (org-defkey minibuffer-local-map [(shift left)]
  12480. (lambda () (interactive)
  12481. (org-eval-in-calendar '(calendar-backward-day 1))))
  12482. (org-defkey minibuffer-local-map [(shift right)]
  12483. (lambda () (interactive)
  12484. (org-eval-in-calendar '(calendar-forward-day 1))))
  12485. (org-defkey minibuffer-local-map ">"
  12486. (lambda () (interactive)
  12487. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12488. (org-defkey minibuffer-local-map "<"
  12489. (lambda () (interactive)
  12490. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12491. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12492. (unwind-protect
  12493. (progn
  12494. (use-local-map map)
  12495. (add-hook 'post-command-hook 'org-read-date-display)
  12496. (setq org-ans0 (read-string prompt default-input
  12497. 'org-read-date-history nil))
  12498. ;; org-ans0: from prompt
  12499. ;; org-ans1: from mouse click
  12500. ;; org-ans2: from calendar motion
  12501. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12502. (remove-hook 'post-command-hook 'org-read-date-display)
  12503. (use-local-map old-map)
  12504. (when org-read-date-overlay
  12505. (delete-overlay org-read-date-overlay)
  12506. (setq org-read-date-overlay nil)))))))
  12507. (t ; Naked prompt only
  12508. (unwind-protect
  12509. (setq ans (read-string prompt default-input
  12510. 'org-read-date-history timestr))
  12511. (when org-read-date-overlay
  12512. (delete-overlay org-read-date-overlay)
  12513. (setq org-read-date-overlay nil)))))
  12514. (setq final (org-read-date-analyze ans def defdecode))
  12515. (setq org-read-date-final-answer ans)
  12516. (if to-time
  12517. (apply 'encode-time final)
  12518. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12519. (format "%04d-%02d-%02d %02d:%02d"
  12520. (nth 5 final) (nth 4 final) (nth 3 final)
  12521. (nth 2 final) (nth 1 final))
  12522. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12523. (defvar def)
  12524. (defvar defdecode)
  12525. (defvar with-time)
  12526. (defvar org-read-date-analyze-futurep nil)
  12527. (defun org-read-date-display ()
  12528. "Display the current date prompt interpretation in the minibuffer."
  12529. (when org-read-date-display-live
  12530. (when org-read-date-overlay
  12531. (delete-overlay org-read-date-overlay))
  12532. (let ((p (point)))
  12533. (end-of-line 1)
  12534. (while (not (equal (buffer-substring
  12535. (max (point-min) (- (point) 4)) (point))
  12536. " "))
  12537. (insert " "))
  12538. (goto-char p))
  12539. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12540. " " (or org-ans1 org-ans2)))
  12541. (org-end-time-was-given nil)
  12542. (f (org-read-date-analyze ans def defdecode))
  12543. (fmts (if org-dcst
  12544. org-time-stamp-custom-formats
  12545. org-time-stamp-formats))
  12546. (fmt (if (or with-time
  12547. (and (boundp 'org-time-was-given) org-time-was-given))
  12548. (cdr fmts)
  12549. (car fmts)))
  12550. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12551. (when (and org-end-time-was-given
  12552. (string-match org-plain-time-of-day-regexp txt))
  12553. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12554. org-end-time-was-given
  12555. (substring txt (match-end 0)))))
  12556. (when org-read-date-analyze-futurep
  12557. (setq txt (concat txt " (=>F)")))
  12558. (setq org-read-date-overlay
  12559. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  12560. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12561. (defun org-read-date-analyze (ans def defdecode)
  12562. "Analyse the combined answer of the date prompt."
  12563. ;; FIXME: cleanup and comment
  12564. (let ((nowdecode (decode-time (current-time)))
  12565. delta deltan deltaw deltadef year month day
  12566. hour minute second wday pm h2 m2 tl wday1
  12567. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12568. (setq org-read-date-analyze-futurep nil)
  12569. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12570. (setq ans "+0"))
  12571. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12572. (setq ans (replace-match "" t t ans)
  12573. deltan (car delta)
  12574. deltaw (nth 1 delta)
  12575. deltadef (nth 2 delta)))
  12576. ;; Check if there is an iso week date in there
  12577. ;; If yes, store the info and postpone interpreting it until the rest
  12578. ;; of the parsing is done
  12579. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12580. (setq iso-year (if (match-end 1)
  12581. (org-small-year-to-year
  12582. (string-to-number (match-string 1 ans))))
  12583. iso-weekday (if (match-end 3)
  12584. (string-to-number (match-string 3 ans)))
  12585. iso-week (string-to-number (match-string 2 ans)))
  12586. (setq ans (replace-match "" t t ans)))
  12587. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12588. (when (string-match
  12589. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12590. (setq year (if (match-end 2)
  12591. (string-to-number (match-string 2 ans))
  12592. (progn (setq kill-year t)
  12593. (string-to-number (format-time-string "%Y"))))
  12594. month (string-to-number (match-string 3 ans))
  12595. day (string-to-number (match-string 4 ans)))
  12596. (if (< year 100) (setq year (+ 2000 year)))
  12597. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12598. t nil ans)))
  12599. ;; Help matching american dates, like 5/30 or 5/30/7
  12600. (when (string-match
  12601. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12602. (setq year (if (match-end 4)
  12603. (string-to-number (match-string 4 ans))
  12604. (progn (setq kill-year t)
  12605. (string-to-number (format-time-string "%Y"))))
  12606. month (string-to-number (match-string 1 ans))
  12607. day (string-to-number (match-string 2 ans)))
  12608. (if (< year 100) (setq year (+ 2000 year)))
  12609. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12610. t nil ans)))
  12611. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12612. ;; If there is a time with am/pm, and *no* time without it, we convert
  12613. ;; so that matching will be successful.
  12614. (loop for i from 1 to 2 do ; twice, for end time as well
  12615. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12616. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12617. (setq hour (string-to-number (match-string 1 ans))
  12618. minute (if (match-end 3)
  12619. (string-to-number (match-string 3 ans))
  12620. 0)
  12621. pm (equal ?p
  12622. (string-to-char (downcase (match-string 4 ans)))))
  12623. (if (and (= hour 12) (not pm))
  12624. (setq hour 0)
  12625. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12626. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12627. t t ans))))
  12628. ;; Check if a time range is given as a duration
  12629. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12630. (setq hour (string-to-number (match-string 1 ans))
  12631. h2 (+ hour (string-to-number (match-string 3 ans)))
  12632. minute (string-to-number (match-string 2 ans))
  12633. m2 (+ minute (if (match-end 5) (string-to-number
  12634. (match-string 5 ans))0)))
  12635. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12636. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12637. t t ans)))
  12638. ;; Check if there is a time range
  12639. (when (boundp 'org-end-time-was-given)
  12640. (setq org-time-was-given nil)
  12641. (when (and (string-match org-plain-time-of-day-regexp ans)
  12642. (match-end 8))
  12643. (setq org-end-time-was-given (match-string 8 ans))
  12644. (setq ans (concat (substring ans 0 (match-beginning 7))
  12645. (substring ans (match-end 7))))))
  12646. (setq tl (parse-time-string ans)
  12647. day (or (nth 3 tl) (nth 3 defdecode))
  12648. month (or (nth 4 tl)
  12649. (if (and org-read-date-prefer-future
  12650. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12651. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12652. (nth 4 defdecode)))
  12653. year (or (and (not kill-year) (nth 5 tl))
  12654. (if (and org-read-date-prefer-future
  12655. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12656. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12657. (nth 5 defdecode)))
  12658. hour (or (nth 2 tl) (nth 2 defdecode))
  12659. minute (or (nth 1 tl) (nth 1 defdecode))
  12660. second (or (nth 0 tl) 0)
  12661. wday (nth 6 tl))
  12662. (when (and (eq org-read-date-prefer-future 'time)
  12663. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12664. (equal day (nth 3 nowdecode))
  12665. (equal month (nth 4 nowdecode))
  12666. (equal year (nth 5 nowdecode))
  12667. (nth 2 tl)
  12668. (or (< (nth 2 tl) (nth 2 nowdecode))
  12669. (and (= (nth 2 tl) (nth 2 nowdecode))
  12670. (nth 1 tl)
  12671. (< (nth 1 tl) (nth 1 nowdecode)))))
  12672. (setq day (1+ day)
  12673. futurep t))
  12674. ;; Special date definitions below
  12675. (cond
  12676. (iso-week
  12677. ;; There was an iso week
  12678. (require 'cal-iso)
  12679. (setq futurep nil)
  12680. (setq year (or iso-year year)
  12681. day (or iso-weekday wday 1)
  12682. wday nil ; to make sure that the trigger below does not match
  12683. iso-date (calendar-gregorian-from-absolute
  12684. (calendar-absolute-from-iso
  12685. (list iso-week day year))))
  12686. ; FIXME: Should we also push ISO weeks into the future?
  12687. ; (when (and org-read-date-prefer-future
  12688. ; (not iso-year)
  12689. ; (< (calendar-absolute-from-gregorian iso-date)
  12690. ; (time-to-days (current-time))))
  12691. ; (setq year (1+ year)
  12692. ; iso-date (calendar-gregorian-from-absolute
  12693. ; (calendar-absolute-from-iso
  12694. ; (list iso-week day year)))))
  12695. (setq month (car iso-date)
  12696. year (nth 2 iso-date)
  12697. day (nth 1 iso-date)))
  12698. (deltan
  12699. (setq futurep nil)
  12700. (unless deltadef
  12701. (let ((now (decode-time (current-time))))
  12702. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12703. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12704. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12705. ((equal deltaw "m") (setq month (+ month deltan)))
  12706. ((equal deltaw "y") (setq year (+ year deltan)))))
  12707. ((and wday (not (nth 3 tl)))
  12708. (setq futurep nil)
  12709. ;; Weekday was given, but no day, so pick that day in the week
  12710. ;; on or after the derived date.
  12711. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12712. (unless (equal wday wday1)
  12713. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12714. (if (and (boundp 'org-time-was-given)
  12715. (nth 2 tl))
  12716. (setq org-time-was-given t))
  12717. (if (< year 100) (setq year (+ 2000 year)))
  12718. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12719. (setq org-read-date-analyze-futurep futurep)
  12720. (list second minute hour day month year)))
  12721. (defvar parse-time-weekdays)
  12722. (defun org-read-date-get-relative (s today default)
  12723. "Check string S for special relative date string.
  12724. TODAY and DEFAULT are internal times, for today and for a default.
  12725. Return shift list (N what def-flag)
  12726. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12727. N is the number of WHATs to shift.
  12728. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12729. the DEFAULT date rather than TODAY."
  12730. (when (and
  12731. (string-match
  12732. (concat
  12733. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12734. "\\([0-9]+\\)?"
  12735. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12736. "\\([ \t]\\|$\\)") s)
  12737. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12738. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12739. (string-to-char (substring (match-string 1 s) -1))
  12740. ?+))
  12741. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12742. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12743. (what (if (match-end 3) (match-string 3 s) "d"))
  12744. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12745. (date (if rel default today))
  12746. (wday (nth 6 (decode-time date)))
  12747. delta)
  12748. (if wday1
  12749. (progn
  12750. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12751. (if (= dir ?-) (setq delta (- delta 7)))
  12752. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12753. (list delta "d" rel))
  12754. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12755. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12756. "Turn a user-specified date into the internal representation.
  12757. The internal representation needed by the calendar is (month day year).
  12758. This is a wrapper to handle the brain-dead convention in calendar that
  12759. user function argument order change dependent on argument order."
  12760. (if (boundp 'calendar-date-style)
  12761. (cond
  12762. ((eq calendar-date-style 'american)
  12763. (list arg1 arg2 arg3))
  12764. ((eq calendar-date-style 'european)
  12765. (list arg2 arg1 arg3))
  12766. ((eq calendar-date-style 'iso)
  12767. (list arg2 arg3 arg1)))
  12768. (if (org-bound-and-true-p european-calendar-style)
  12769. (list arg2 arg1 arg3)
  12770. (list arg1 arg2 arg3))))
  12771. (defun org-eval-in-calendar (form &optional keepdate)
  12772. "Eval FORM in the calendar window and return to current window.
  12773. Also, store the cursor date in variable org-ans2."
  12774. (let ((sf (selected-frame))
  12775. (sw (selected-window)))
  12776. (select-window (get-buffer-window "*Calendar*" t))
  12777. (eval form)
  12778. (when (and (not keepdate) (calendar-cursor-to-date))
  12779. (let* ((date (calendar-cursor-to-date))
  12780. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12781. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12782. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12783. (select-window sw)
  12784. (org-select-frame-set-input-focus sf)))
  12785. (defun org-calendar-select ()
  12786. "Return to `org-read-date' with the date currently selected.
  12787. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12788. (interactive)
  12789. (when (calendar-cursor-to-date)
  12790. (let* ((date (calendar-cursor-to-date))
  12791. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12792. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12793. (if (active-minibuffer-window) (exit-minibuffer))))
  12794. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12795. "Insert a date stamp for the date given by the internal TIME.
  12796. WITH-HM means use the stamp format that includes the time of the day.
  12797. INACTIVE means use square brackets instead of angular ones, so that the
  12798. stamp will not contribute to the agenda.
  12799. PRE and POST are optional strings to be inserted before and after the
  12800. stamp.
  12801. The command returns the inserted time stamp."
  12802. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12803. stamp)
  12804. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12805. (insert-before-markers (or pre ""))
  12806. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12807. (when (listp extra)
  12808. (setq extra (car extra))
  12809. (if (and (stringp extra)
  12810. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12811. (setq extra (format "-%02d:%02d"
  12812. (string-to-number (match-string 1 extra))
  12813. (string-to-number (match-string 2 extra))))
  12814. (setq extra nil)))
  12815. (when extra
  12816. (backward-char 1)
  12817. (insert-before-markers extra)
  12818. (forward-char 1))
  12819. (insert-before-markers (or post ""))
  12820. (setq org-last-inserted-timestamp stamp)))
  12821. (defun org-toggle-time-stamp-overlays ()
  12822. "Toggle the use of custom time stamp formats."
  12823. (interactive)
  12824. (setq org-display-custom-times (not org-display-custom-times))
  12825. (unless org-display-custom-times
  12826. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12827. (while (setq p (next-single-property-change p 'display))
  12828. (if (and (get-text-property p 'display)
  12829. (eq (get-text-property p 'face) 'org-date))
  12830. (remove-text-properties
  12831. p (setq p (next-single-property-change p 'display))
  12832. '(display t))))
  12833. (set-buffer-modified-p bmp)))
  12834. (if (featurep 'xemacs)
  12835. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12836. (org-restart-font-lock)
  12837. (setq org-table-may-need-update t)
  12838. (if org-display-custom-times
  12839. (message "Time stamps are overlayed with custom format")
  12840. (message "Time stamp overlays removed")))
  12841. (defun org-display-custom-time (beg end)
  12842. "Overlay modified time stamp format over timestamp between BEG and END."
  12843. (let* ((ts (buffer-substring beg end))
  12844. t1 w1 with-hm tf time str w2 (off 0))
  12845. (save-match-data
  12846. (setq t1 (org-parse-time-string ts t))
  12847. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12848. (setq off (- (match-end 0) (match-beginning 0)))))
  12849. (setq end (- end off))
  12850. (setq w1 (- end beg)
  12851. with-hm (and (nth 1 t1) (nth 2 t1))
  12852. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12853. time (org-fix-decoded-time t1)
  12854. str (org-add-props
  12855. (format-time-string
  12856. (substring tf 1 -1) (apply 'encode-time time))
  12857. nil 'mouse-face 'highlight)
  12858. w2 (length str))
  12859. (if (not (= w2 w1))
  12860. (add-text-properties (1+ beg) (+ 2 beg)
  12861. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12862. (if (featurep 'xemacs)
  12863. (progn
  12864. (put-text-property beg end 'invisible t)
  12865. (put-text-property beg end 'end-glyph (make-glyph str)))
  12866. (put-text-property beg end 'display str))))
  12867. (defun org-translate-time (string)
  12868. "Translate all timestamps in STRING to custom format.
  12869. But do this only if the variable `org-display-custom-times' is set."
  12870. (when org-display-custom-times
  12871. (save-match-data
  12872. (let* ((start 0)
  12873. (re org-ts-regexp-both)
  12874. t1 with-hm inactive tf time str beg end)
  12875. (while (setq start (string-match re string start))
  12876. (setq beg (match-beginning 0)
  12877. end (match-end 0)
  12878. t1 (save-match-data
  12879. (org-parse-time-string (substring string beg end) t))
  12880. with-hm (and (nth 1 t1) (nth 2 t1))
  12881. inactive (equal (substring string beg (1+ beg)) "[")
  12882. tf (funcall (if with-hm 'cdr 'car)
  12883. org-time-stamp-custom-formats)
  12884. time (org-fix-decoded-time t1)
  12885. str (format-time-string
  12886. (concat
  12887. (if inactive "[" "<") (substring tf 1 -1)
  12888. (if inactive "]" ">"))
  12889. (apply 'encode-time time))
  12890. string (replace-match str t t string)
  12891. start (+ start (length str)))))))
  12892. string)
  12893. (defun org-fix-decoded-time (time)
  12894. "Set 0 instead of nil for the first 6 elements of time.
  12895. Don't touch the rest."
  12896. (let ((n 0))
  12897. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12898. (defun org-days-to-time (timestamp-string)
  12899. "Difference between TIMESTAMP-STRING and now in days."
  12900. (- (time-to-days (org-time-string-to-time timestamp-string))
  12901. (time-to-days (current-time))))
  12902. (defun org-deadline-close (timestamp-string &optional ndays)
  12903. "Is the time in TIMESTAMP-STRING close to the current date?"
  12904. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12905. (and (< (org-days-to-time timestamp-string) ndays)
  12906. (not (org-entry-is-done-p))))
  12907. (defun org-get-wdays (ts)
  12908. "Get the deadline lead time appropriate for timestring TS."
  12909. (cond
  12910. ((<= org-deadline-warning-days 0)
  12911. ;; 0 or negative, enforce this value no matter what
  12912. (- org-deadline-warning-days))
  12913. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12914. ;; lead time is specified.
  12915. (floor (* (string-to-number (match-string 1 ts))
  12916. (cdr (assoc (match-string 2 ts)
  12917. '(("d" . 1) ("w" . 7)
  12918. ("m" . 30.4) ("y" . 365.25)))))))
  12919. ;; go for the default.
  12920. (t org-deadline-warning-days)))
  12921. (defun org-calendar-select-mouse (ev)
  12922. "Return to `org-read-date' with the date currently selected.
  12923. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12924. (interactive "e")
  12925. (mouse-set-point ev)
  12926. (when (calendar-cursor-to-date)
  12927. (let* ((date (calendar-cursor-to-date))
  12928. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12929. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12930. (if (active-minibuffer-window) (exit-minibuffer))))
  12931. (defun org-check-deadlines (ndays)
  12932. "Check if there are any deadlines due or past due.
  12933. A deadline is considered due if it happens within `org-deadline-warning-days'
  12934. days from today's date. If the deadline appears in an entry marked DONE,
  12935. it is not shown. The prefix arg NDAYS can be used to test that many
  12936. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12937. (interactive "P")
  12938. (let* ((org-warn-days
  12939. (cond
  12940. ((equal ndays '(4)) 100000)
  12941. (ndays (prefix-numeric-value ndays))
  12942. (t (abs org-deadline-warning-days))))
  12943. (case-fold-search nil)
  12944. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12945. (callback
  12946. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12947. (message "%d deadlines past-due or due within %d days"
  12948. (org-occur regexp nil callback)
  12949. org-warn-days)))
  12950. (defun org-check-before-date (date)
  12951. "Check if there are deadlines or scheduled entries before DATE."
  12952. (interactive (list (org-read-date)))
  12953. (let ((case-fold-search nil)
  12954. (regexp (concat "\\<\\(" org-deadline-string
  12955. "\\|" org-scheduled-string
  12956. "\\) *<\\([^>]+\\)>"))
  12957. (callback
  12958. (lambda () (time-less-p
  12959. (org-time-string-to-time (match-string 2))
  12960. (org-time-string-to-time date)))))
  12961. (message "%d entries before %s"
  12962. (org-occur regexp nil callback) date)))
  12963. (defun org-check-after-date (date)
  12964. "Check if there are deadlines or scheduled entries after DATE."
  12965. (interactive (list (org-read-date)))
  12966. (let ((case-fold-search nil)
  12967. (regexp (concat "\\<\\(" org-deadline-string
  12968. "\\|" org-scheduled-string
  12969. "\\) *<\\([^>]+\\)>"))
  12970. (callback
  12971. (lambda () (not
  12972. (time-less-p
  12973. (org-time-string-to-time (match-string 2))
  12974. (org-time-string-to-time date))))))
  12975. (message "%d entries after %s"
  12976. (org-occur regexp nil callback) date)))
  12977. (defun org-evaluate-time-range (&optional to-buffer)
  12978. "Evaluate a time range by computing the difference between start and end.
  12979. Normally the result is just printed in the echo area, but with prefix arg
  12980. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12981. If the time range is actually in a table, the result is inserted into the
  12982. next column.
  12983. For time difference computation, a year is assumed to be exactly 365
  12984. days in order to avoid rounding problems."
  12985. (interactive "P")
  12986. (or
  12987. (org-clock-update-time-maybe)
  12988. (save-excursion
  12989. (unless (org-at-date-range-p t)
  12990. (goto-char (point-at-bol))
  12991. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12992. (if (not (org-at-date-range-p t))
  12993. (error "Not at a time-stamp range, and none found in current line")))
  12994. (let* ((ts1 (match-string 1))
  12995. (ts2 (match-string 2))
  12996. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12997. (match-end (match-end 0))
  12998. (time1 (org-time-string-to-time ts1))
  12999. (time2 (org-time-string-to-time ts2))
  13000. (t1 (org-float-time time1))
  13001. (t2 (org-float-time time2))
  13002. (diff (abs (- t2 t1)))
  13003. (negative (< (- t2 t1) 0))
  13004. ;; (ys (floor (* 365 24 60 60)))
  13005. (ds (* 24 60 60))
  13006. (hs (* 60 60))
  13007. (fy "%dy %dd %02d:%02d")
  13008. (fy1 "%dy %dd")
  13009. (fd "%dd %02d:%02d")
  13010. (fd1 "%dd")
  13011. (fh "%02d:%02d")
  13012. y d h m align)
  13013. (if havetime
  13014. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13015. y 0
  13016. d (floor (/ diff ds)) diff (mod diff ds)
  13017. h (floor (/ diff hs)) diff (mod diff hs)
  13018. m (floor (/ diff 60)))
  13019. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13020. y 0
  13021. d (floor (+ (/ diff ds) 0.5))
  13022. h 0 m 0))
  13023. (if (not to-buffer)
  13024. (message "%s" (org-make-tdiff-string y d h m))
  13025. (if (org-at-table-p)
  13026. (progn
  13027. (goto-char match-end)
  13028. (setq align t)
  13029. (and (looking-at " *|") (goto-char (match-end 0))))
  13030. (goto-char match-end))
  13031. (if (looking-at
  13032. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13033. (replace-match ""))
  13034. (if negative (insert " -"))
  13035. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13036. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13037. (insert " " (format fh h m))))
  13038. (if align (org-table-align))
  13039. (message "Time difference inserted")))))
  13040. (defun org-make-tdiff-string (y d h m)
  13041. (let ((fmt "")
  13042. (l nil))
  13043. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13044. l (push y l)))
  13045. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13046. l (push d l)))
  13047. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13048. l (push h l)))
  13049. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13050. l (push m l)))
  13051. (apply 'format fmt (nreverse l))))
  13052. (defun org-time-string-to-time (s)
  13053. (apply 'encode-time (org-parse-time-string s)))
  13054. (defun org-time-string-to-seconds (s)
  13055. (org-float-time (org-time-string-to-time s)))
  13056. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13057. "Convert a time stamp to an absolute day number.
  13058. If there is a specifyer for a cyclic time stamp, get the closest date to
  13059. DAYNR.
  13060. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13061. the variable date is bound by the calendar when this is called."
  13062. (cond
  13063. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13064. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13065. daynr
  13066. (+ daynr 1000)))
  13067. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13068. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13069. (time-to-days (current-time))) (match-string 0 s)
  13070. prefer show-all))
  13071. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13072. (defun org-days-to-iso-week (days)
  13073. "Return the iso week number."
  13074. (require 'cal-iso)
  13075. (car (calendar-iso-from-absolute days)))
  13076. (defun org-small-year-to-year (year)
  13077. "Convert 2-digit years into 4-digit years.
  13078. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13079. The year 2000 cannot be abbreviated. Any year larger than 99
  13080. is returned unchanged."
  13081. (if (< year 38)
  13082. (setq year (+ 2000 year))
  13083. (if (< year 100)
  13084. (setq year (+ 1900 year))))
  13085. year)
  13086. (defun org-time-from-absolute (d)
  13087. "Return the time corresponding to date D.
  13088. D may be an absolute day number, or a calendar-type list (month day year)."
  13089. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13090. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13091. (defun org-calendar-holiday ()
  13092. "List of holidays, for Diary display in Org-mode."
  13093. (require 'holidays)
  13094. (let ((hl (funcall
  13095. (if (fboundp 'calendar-check-holidays)
  13096. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13097. (if hl (mapconcat 'identity hl "; "))))
  13098. (defun org-diary-sexp-entry (sexp entry date)
  13099. "Process a SEXP diary ENTRY for DATE."
  13100. (require 'diary-lib)
  13101. (let ((result (if calendar-debug-sexp
  13102. (let ((stack-trace-on-error t))
  13103. (eval (car (read-from-string sexp))))
  13104. (condition-case nil
  13105. (eval (car (read-from-string sexp)))
  13106. (error
  13107. (beep)
  13108. (message "Bad sexp at line %d in %s: %s"
  13109. (org-current-line)
  13110. (buffer-file-name) sexp)
  13111. (sleep-for 2))))))
  13112. (cond ((stringp result) result)
  13113. ((and (consp result)
  13114. (stringp (cdr result))) (cdr result))
  13115. (result entry)
  13116. (t nil))))
  13117. (defun org-diary-to-ical-string (frombuf)
  13118. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13119. This uses the icalendar.el library."
  13120. (let* ((tmpdir (if (featurep 'xemacs)
  13121. (temp-directory)
  13122. temporary-file-directory))
  13123. (tmpfile (make-temp-name
  13124. (expand-file-name "orgics" tmpdir)))
  13125. buf rtn b e)
  13126. (with-current-buffer frombuf
  13127. (icalendar-export-region (point-min) (point-max) tmpfile)
  13128. (setq buf (find-buffer-visiting tmpfile))
  13129. (set-buffer buf)
  13130. (goto-char (point-min))
  13131. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13132. (setq b (match-beginning 0)))
  13133. (goto-char (point-max))
  13134. (if (re-search-backward "^END:VEVENT" nil t)
  13135. (setq e (match-end 0)))
  13136. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13137. (kill-buffer buf)
  13138. (delete-file tmpfile)
  13139. rtn))
  13140. (defun org-closest-date (start current change prefer show-all)
  13141. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13142. When PREFER is `past' return a date that is either CURRENT or past.
  13143. When PREFER is `future', return a date that is either CURRENT or future.
  13144. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13145. ;; Make the proper lists from the dates
  13146. (catch 'exit
  13147. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13148. dn dw sday cday n1 n2 n0
  13149. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13150. (setq start (org-date-to-gregorian start)
  13151. current (org-date-to-gregorian
  13152. (if show-all
  13153. current
  13154. (time-to-days (current-time))))
  13155. sday (calendar-absolute-from-gregorian start)
  13156. cday (calendar-absolute-from-gregorian current))
  13157. (if (<= cday sday) (throw 'exit sday))
  13158. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13159. (setq dn (string-to-number (match-string 1 change))
  13160. dw (cdr (assoc (match-string 2 change) a1)))
  13161. (error "Invalid change specifyer: %s" change))
  13162. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13163. (cond
  13164. ((eq dw 'day)
  13165. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13166. n2 (+ n1 dn)))
  13167. ((eq dw 'year)
  13168. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13169. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13170. (setq date1 (list m d y1)
  13171. n1 (calendar-absolute-from-gregorian date1)
  13172. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13173. n2 (calendar-absolute-from-gregorian date2)))
  13174. ((eq dw 'month)
  13175. ;; approx number of month between the two dates
  13176. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13177. ;; How often does dn fit in there?
  13178. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13179. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13180. m (+ m nm)
  13181. ny (floor (/ m 12))
  13182. y (+ y ny)
  13183. m (- m (* ny 12)))
  13184. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13185. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13186. (setq m2 (+ m dn) y2 y)
  13187. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13188. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13189. (while (<= n2 cday)
  13190. (setq n1 n2 m m2 y y2)
  13191. (setq m2 (+ m dn) y2 y)
  13192. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13193. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13194. ;; Make sure n1 is the earlier date
  13195. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13196. (if show-all
  13197. (cond
  13198. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13199. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13200. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13201. (cond
  13202. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13203. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13204. (t (if (= cday n1) n1 n2)))))))
  13205. (defun org-date-to-gregorian (date)
  13206. "Turn any specification of DATE into a gregorian date for the calendar."
  13207. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13208. ((and (listp date) (= (length date) 3)) date)
  13209. ((stringp date)
  13210. (setq date (org-parse-time-string date))
  13211. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13212. ((listp date)
  13213. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13214. (defun org-parse-time-string (s &optional nodefault)
  13215. "Parse the standard Org-mode time string.
  13216. This should be a lot faster than the normal `parse-time-string'.
  13217. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13218. hour and minute fields will be nil if not given."
  13219. (if (string-match org-ts-regexp0 s)
  13220. (list 0
  13221. (if (or (match-beginning 8) (not nodefault))
  13222. (string-to-number (or (match-string 8 s) "0")))
  13223. (if (or (match-beginning 7) (not nodefault))
  13224. (string-to-number (or (match-string 7 s) "0")))
  13225. (string-to-number (match-string 4 s))
  13226. (string-to-number (match-string 3 s))
  13227. (string-to-number (match-string 2 s))
  13228. nil nil nil)
  13229. (error "Not a standard Org-mode time string: %s" s)))
  13230. (defun org-timestamp-up (&optional arg)
  13231. "Increase the date item at the cursor by one.
  13232. If the cursor is on the year, change the year. If it is on the month or
  13233. the day, change that.
  13234. With prefix ARG, change by that many units."
  13235. (interactive "p")
  13236. (org-timestamp-change (prefix-numeric-value arg)))
  13237. (defun org-timestamp-down (&optional arg)
  13238. "Decrease the date item at the cursor by one.
  13239. If the cursor is on the year, change the year. If it is on the month or
  13240. the day, change that.
  13241. With prefix ARG, change by that many units."
  13242. (interactive "p")
  13243. (org-timestamp-change (- (prefix-numeric-value arg))))
  13244. (defun org-timestamp-up-day (&optional arg)
  13245. "Increase the date in the time stamp by one day.
  13246. With prefix ARG, change that many days."
  13247. (interactive "p")
  13248. (if (and (not (org-at-timestamp-p t))
  13249. (org-on-heading-p))
  13250. (org-todo 'up)
  13251. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13252. (defun org-timestamp-down-day (&optional arg)
  13253. "Decrease the date in the time stamp by one day.
  13254. With prefix ARG, change that many days."
  13255. (interactive "p")
  13256. (if (and (not (org-at-timestamp-p t))
  13257. (org-on-heading-p))
  13258. (org-todo 'down)
  13259. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13260. (defun org-at-timestamp-p (&optional inactive-ok)
  13261. "Determine if the cursor is in or at a timestamp."
  13262. (interactive)
  13263. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13264. (pos (point))
  13265. (ans (or (looking-at tsr)
  13266. (save-excursion
  13267. (skip-chars-backward "^[<\n\r\t")
  13268. (if (> (point) (point-min)) (backward-char 1))
  13269. (and (looking-at tsr)
  13270. (> (- (match-end 0) pos) -1))))))
  13271. (and ans
  13272. (boundp 'org-ts-what)
  13273. (setq org-ts-what
  13274. (cond
  13275. ((= pos (match-beginning 0)) 'bracket)
  13276. ((= pos (1- (match-end 0))) 'bracket)
  13277. ((org-pos-in-match-range pos 2) 'year)
  13278. ((org-pos-in-match-range pos 3) 'month)
  13279. ((org-pos-in-match-range pos 7) 'hour)
  13280. ((org-pos-in-match-range pos 8) 'minute)
  13281. ((or (org-pos-in-match-range pos 4)
  13282. (org-pos-in-match-range pos 5)) 'day)
  13283. ((and (> pos (or (match-end 8) (match-end 5)))
  13284. (< pos (match-end 0)))
  13285. (- pos (or (match-end 8) (match-end 5))))
  13286. (t 'day))))
  13287. ans))
  13288. (defun org-toggle-timestamp-type ()
  13289. "Toggle the type (<active> or [inactive]) of a time stamp."
  13290. (interactive)
  13291. (when (org-at-timestamp-p t)
  13292. (let ((beg (match-beginning 0)) (end (match-end 0))
  13293. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13294. (save-excursion
  13295. (goto-char beg)
  13296. (while (re-search-forward "[][<>]" end t)
  13297. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13298. t t)))
  13299. (message "Timestamp is now %sactive"
  13300. (if (equal (char-after beg) ?<) "" "in")))))
  13301. (defun org-timestamp-change (n &optional what)
  13302. "Change the date in the time stamp at point.
  13303. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13304. `year', `minute', `second'. If WHAT is not given, the cursor position
  13305. in the timestamp determines what will be changed."
  13306. (let ((pos (point))
  13307. with-hm inactive
  13308. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13309. org-ts-what
  13310. extra rem
  13311. ts time time0)
  13312. (if (not (org-at-timestamp-p t))
  13313. (error "Not at a timestamp"))
  13314. (if (and (not what) (eq org-ts-what 'bracket))
  13315. (org-toggle-timestamp-type)
  13316. (if (and (not what) (not (eq org-ts-what 'day))
  13317. org-display-custom-times
  13318. (get-text-property (point) 'display)
  13319. (not (get-text-property (1- (point)) 'display)))
  13320. (setq org-ts-what 'day))
  13321. (setq org-ts-what (or what org-ts-what)
  13322. inactive (= (char-after (match-beginning 0)) ?\[)
  13323. ts (match-string 0))
  13324. (replace-match "")
  13325. (if (string-match
  13326. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13327. ts)
  13328. (setq extra (match-string 1 ts)))
  13329. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13330. (setq with-hm t))
  13331. (setq time0 (org-parse-time-string ts))
  13332. (when (and (eq org-ts-what 'minute)
  13333. (eq current-prefix-arg nil))
  13334. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13335. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13336. (setcar (cdr time0) (+ (nth 1 time0)
  13337. (if (> n 0) (- rem) (- dm rem))))))
  13338. (setq time
  13339. (encode-time (or (car time0) 0)
  13340. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13341. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13342. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13343. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13344. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13345. (nthcdr 6 time0)))
  13346. (when (and (member org-ts-what '(hour minute))
  13347. extra
  13348. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13349. (setq extra (org-modify-ts-extra
  13350. extra
  13351. (if (eq org-ts-what 'hour) 2 5)
  13352. n dm)))
  13353. (when (integerp org-ts-what)
  13354. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13355. (if (eq what 'calendar)
  13356. (let ((cal-date (org-get-date-from-calendar)))
  13357. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13358. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13359. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13360. (setcar time0 (or (car time0) 0))
  13361. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13362. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13363. (setq time (apply 'encode-time time0))))
  13364. (setq org-last-changed-timestamp
  13365. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13366. (org-clock-update-time-maybe)
  13367. (goto-char pos)
  13368. ;; Try to recenter the calendar window, if any
  13369. (if (and org-calendar-follow-timestamp-change
  13370. (get-buffer-window "*Calendar*" t)
  13371. (memq org-ts-what '(day month year)))
  13372. (org-recenter-calendar (time-to-days time))))))
  13373. (defun org-modify-ts-extra (s pos n dm)
  13374. "Change the different parts of the lead-time and repeat fields in timestamp."
  13375. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13376. ng h m new rem)
  13377. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13378. (cond
  13379. ((or (org-pos-in-match-range pos 2)
  13380. (org-pos-in-match-range pos 3))
  13381. (setq m (string-to-number (match-string 3 s))
  13382. h (string-to-number (match-string 2 s)))
  13383. (if (org-pos-in-match-range pos 2)
  13384. (setq h (+ h n))
  13385. (setq n (* dm (org-no-warnings (signum n))))
  13386. (when (not (= 0 (setq rem (% m dm))))
  13387. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13388. (setq m (+ m n)))
  13389. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13390. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13391. (setq h (min 24 (max 0 h)))
  13392. (setq ng 1 new (format "-%02d:%02d" h m)))
  13393. ((org-pos-in-match-range pos 6)
  13394. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13395. ((org-pos-in-match-range pos 5)
  13396. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13397. ((org-pos-in-match-range pos 9)
  13398. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13399. ((org-pos-in-match-range pos 8)
  13400. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13401. (when ng
  13402. (setq s (concat
  13403. (substring s 0 (match-beginning ng))
  13404. new
  13405. (substring s (match-end ng))))))
  13406. s))
  13407. (defun org-recenter-calendar (date)
  13408. "If the calendar is visible, recenter it to DATE."
  13409. (let* ((win (selected-window))
  13410. (cwin (get-buffer-window "*Calendar*" t))
  13411. (calendar-move-hook nil))
  13412. (when cwin
  13413. (select-window cwin)
  13414. (calendar-goto-date (if (listp date) date
  13415. (calendar-gregorian-from-absolute date)))
  13416. (select-window win))))
  13417. (defun org-goto-calendar (&optional arg)
  13418. "Go to the Emacs calendar at the current date.
  13419. If there is a time stamp in the current line, go to that date.
  13420. A prefix ARG can be used to force the current date."
  13421. (interactive "P")
  13422. (let ((tsr org-ts-regexp) diff
  13423. (calendar-move-hook nil)
  13424. (calendar-view-holidays-initially-flag nil)
  13425. (calendar-view-diary-initially-flag nil))
  13426. (if (or (org-at-timestamp-p)
  13427. (save-excursion
  13428. (beginning-of-line 1)
  13429. (looking-at (concat ".*" tsr))))
  13430. (let ((d1 (time-to-days (current-time)))
  13431. (d2 (time-to-days
  13432. (org-time-string-to-time (match-string 1)))))
  13433. (setq diff (- d2 d1))))
  13434. (calendar)
  13435. (calendar-goto-today)
  13436. (if (and diff (not arg)) (calendar-forward-day diff))))
  13437. (defun org-get-date-from-calendar ()
  13438. "Return a list (month day year) of date at point in calendar."
  13439. (with-current-buffer "*Calendar*"
  13440. (save-match-data
  13441. (calendar-cursor-to-date))))
  13442. (defun org-date-from-calendar ()
  13443. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13444. If there is already a time stamp at the cursor position, update it."
  13445. (interactive)
  13446. (if (org-at-timestamp-p t)
  13447. (org-timestamp-change 0 'calendar)
  13448. (let ((cal-date (org-get-date-from-calendar)))
  13449. (org-insert-time-stamp
  13450. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13451. (defun org-minutes-to-hh:mm-string (m)
  13452. "Compute H:MM from a number of minutes."
  13453. (let ((h (/ m 60)))
  13454. (setq m (- m (* 60 h)))
  13455. (format org-time-clocksum-format h m)))
  13456. (defun org-hh:mm-string-to-minutes (s)
  13457. "Convert a string H:MM to a number of minutes.
  13458. If the string is just a number, interpret it as minutes.
  13459. In fact, the first hh:mm or number in the string will be taken,
  13460. there can be extra stuff in the string.
  13461. If no number is found, the return value is 0."
  13462. (cond
  13463. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13464. (+ (* (string-to-number (match-string 1 s)) 60)
  13465. (string-to-number (match-string 2 s))))
  13466. ((string-match "\\([0-9]+\\)" s)
  13467. (string-to-number (match-string 1 s)))
  13468. (t 0)))
  13469. ;;;; Files
  13470. (defun org-save-all-org-buffers ()
  13471. "Save all Org-mode buffers without user confirmation."
  13472. (interactive)
  13473. (message "Saving all Org-mode buffers...")
  13474. (save-some-buffers t 'org-mode-p)
  13475. (when (featurep 'org-id) (org-id-locations-save))
  13476. (message "Saving all Org-mode buffers... done"))
  13477. (defun org-revert-all-org-buffers ()
  13478. "Revert all Org-mode buffers.
  13479. Prompt for confirmation when there are unsaved changes.
  13480. Be sure you know what you are doing before letting this function
  13481. overwrite your changes.
  13482. This function is useful in a setup where one tracks org files
  13483. with a version control system, to revert on one machine after pulling
  13484. changes from another. I believe the procedure must be like this:
  13485. 1. M-x org-save-all-org-buffers
  13486. 2. Pull changes from the other machine, resolve conflicts
  13487. 3. M-x org-revert-all-org-buffers"
  13488. (interactive)
  13489. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13490. (error "Abort"))
  13491. (save-excursion
  13492. (save-window-excursion
  13493. (mapc
  13494. (lambda (b)
  13495. (when (and (with-current-buffer b (org-mode-p))
  13496. (with-current-buffer b buffer-file-name))
  13497. (switch-to-buffer b)
  13498. (revert-buffer t 'no-confirm)))
  13499. (buffer-list))
  13500. (when (and (featurep 'org-id) org-id-track-globally)
  13501. (org-id-locations-load)))))
  13502. ;;;; Agenda files
  13503. ;;;###autoload
  13504. (defun org-iswitchb (&optional arg)
  13505. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13506. With a prefix argument, restrict available to files.
  13507. With two prefix arguments, restrict available buffers to agenda files."
  13508. (interactive "P")
  13509. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13510. ((equal arg '(16)) (org-buffer-list 'agenda))
  13511. (t (org-buffer-list)))))
  13512. (switch-to-buffer
  13513. (org-icompleting-read "Org buffer: "
  13514. (mapcar 'list (mapcar 'buffer-name blist))
  13515. nil t))))
  13516. ;;;###autoload
  13517. (defalias 'org-ido-switchb 'org-iswitchb)
  13518. (defun org-buffer-list (&optional predicate exclude-tmp)
  13519. "Return a list of Org buffers.
  13520. PREDICATE can be `export', `files' or `agenda'.
  13521. export restrict the list to Export buffers.
  13522. files restrict the list to buffers visiting Org files.
  13523. agenda restrict the list to buffers visiting agenda files.
  13524. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13525. (let* ((bfn nil)
  13526. (agenda-files (and (eq predicate 'agenda)
  13527. (mapcar 'file-truename (org-agenda-files t))))
  13528. (filter
  13529. (cond
  13530. ((eq predicate 'files)
  13531. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13532. ((eq predicate 'export)
  13533. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13534. ((eq predicate 'agenda)
  13535. (lambda (b)
  13536. (with-current-buffer b
  13537. (and (eq major-mode 'org-mode)
  13538. (setq bfn (buffer-file-name b))
  13539. (member (file-truename bfn) agenda-files)))))
  13540. (t (lambda (b) (with-current-buffer b
  13541. (or (eq major-mode 'org-mode)
  13542. (string-match "\*Org .*Export"
  13543. (buffer-name b)))))))))
  13544. (delq nil
  13545. (mapcar
  13546. (lambda(b)
  13547. (if (and (funcall filter b)
  13548. (or (not exclude-tmp)
  13549. (not (string-match "tmp" (buffer-name b)))))
  13550. b
  13551. nil))
  13552. (buffer-list)))))
  13553. (defun org-agenda-files (&optional unrestricted archives)
  13554. "Get the list of agenda files.
  13555. Optional UNRESTRICTED means return the full list even if a restriction
  13556. is currently in place.
  13557. When ARCHIVES is t, include all archive files that are really being
  13558. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13559. `org-agenda-archives-mode' is t."
  13560. (let ((files
  13561. (cond
  13562. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13563. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13564. ((listp org-agenda-files) org-agenda-files)
  13565. (t (error "Invalid value of `org-agenda-files'")))))
  13566. (setq files (apply 'append
  13567. (mapcar (lambda (f)
  13568. (if (file-directory-p f)
  13569. (directory-files
  13570. f t org-agenda-file-regexp)
  13571. (list f)))
  13572. files)))
  13573. (when org-agenda-skip-unavailable-files
  13574. (setq files (delq nil
  13575. (mapcar (function
  13576. (lambda (file)
  13577. (and (file-readable-p file) file)))
  13578. files))))
  13579. (when (or (eq archives t)
  13580. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13581. (setq files (org-add-archive-files files)))
  13582. files))
  13583. (defun org-edit-agenda-file-list ()
  13584. "Edit the list of agenda files.
  13585. Depending on setup, this either uses customize to edit the variable
  13586. `org-agenda-files', or it visits the file that is holding the list. In the
  13587. latter case, the buffer is set up in a way that saving it automatically kills
  13588. the buffer and restores the previous window configuration."
  13589. (interactive)
  13590. (if (stringp org-agenda-files)
  13591. (let ((cw (current-window-configuration)))
  13592. (find-file org-agenda-files)
  13593. (org-set-local 'org-window-configuration cw)
  13594. (org-add-hook 'after-save-hook
  13595. (lambda ()
  13596. (set-window-configuration
  13597. (prog1 org-window-configuration
  13598. (kill-buffer (current-buffer))))
  13599. (org-install-agenda-files-menu)
  13600. (message "New agenda file list installed"))
  13601. nil 'local)
  13602. (message "%s" (substitute-command-keys
  13603. "Edit list and finish with \\[save-buffer]")))
  13604. (customize-variable 'org-agenda-files)))
  13605. (defun org-store-new-agenda-file-list (list)
  13606. "Set new value for the agenda file list and save it correctly."
  13607. (if (stringp org-agenda-files)
  13608. (let ((fe (org-read-agenda-file-list t)) b u)
  13609. (while (setq b (find-buffer-visiting org-agenda-files))
  13610. (kill-buffer b))
  13611. (with-temp-file org-agenda-files
  13612. (insert
  13613. (mapconcat
  13614. (lambda (f) ;; Keep un-expanded entries.
  13615. (if (setq u (assoc f fe))
  13616. (cdr u)
  13617. f))
  13618. list "\n")
  13619. "\n")))
  13620. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13621. (org-insert-mode-line-in-empty-file nil))
  13622. (setq org-agenda-files list)
  13623. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13624. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13625. "Read the list of agenda files from a file.
  13626. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13627. filenames, used by `org-store-new-agenda-file-list' to write back
  13628. un-expanded file names."
  13629. (when (file-directory-p org-agenda-files)
  13630. (error "`org-agenda-files' cannot be a single directory"))
  13631. (when (stringp org-agenda-files)
  13632. (with-temp-buffer
  13633. (insert-file-contents org-agenda-files)
  13634. (mapcar
  13635. (lambda (f)
  13636. (let ((e (expand-file-name (substitute-in-file-name f)
  13637. org-directory)))
  13638. (if pair-with-expansion
  13639. (cons e f)
  13640. e)))
  13641. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13642. ;;;###autoload
  13643. (defun org-cycle-agenda-files ()
  13644. "Cycle through the files in `org-agenda-files'.
  13645. If the current buffer visits an agenda file, find the next one in the list.
  13646. If the current buffer does not, find the first agenda file."
  13647. (interactive)
  13648. (let* ((fs (org-agenda-files t))
  13649. (files (append fs (list (car fs))))
  13650. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13651. file)
  13652. (unless files (error "No agenda files"))
  13653. (catch 'exit
  13654. (while (setq file (pop files))
  13655. (if (equal (file-truename file) tcf)
  13656. (when (car files)
  13657. (find-file (car files))
  13658. (throw 'exit t))))
  13659. (find-file (car fs)))
  13660. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13661. (defun org-agenda-file-to-front (&optional to-end)
  13662. "Move/add the current file to the top of the agenda file list.
  13663. If the file is not present in the list, it is added to the front. If it is
  13664. present, it is moved there. With optional argument TO-END, add/move to the
  13665. end of the list."
  13666. (interactive "P")
  13667. (let ((org-agenda-skip-unavailable-files nil)
  13668. (file-alist (mapcar (lambda (x)
  13669. (cons (file-truename x) x))
  13670. (org-agenda-files t)))
  13671. (ctf (file-truename buffer-file-name))
  13672. x had)
  13673. (setq x (assoc ctf file-alist) had x)
  13674. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13675. (if to-end
  13676. (setq file-alist (append (delq x file-alist) (list x)))
  13677. (setq file-alist (cons x (delq x file-alist))))
  13678. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13679. (org-install-agenda-files-menu)
  13680. (message "File %s to %s of agenda file list"
  13681. (if had "moved" "added") (if to-end "end" "front"))))
  13682. (defun org-remove-file (&optional file)
  13683. "Remove current file from the list of files in variable `org-agenda-files'.
  13684. These are the files which are being checked for agenda entries.
  13685. Optional argument FILE means use this file instead of the current."
  13686. (interactive)
  13687. (let* ((org-agenda-skip-unavailable-files nil)
  13688. (file (or file buffer-file-name))
  13689. (true-file (file-truename file))
  13690. (afile (abbreviate-file-name file))
  13691. (files (delq nil (mapcar
  13692. (lambda (x)
  13693. (if (equal true-file
  13694. (file-truename x))
  13695. nil x))
  13696. (org-agenda-files t)))))
  13697. (if (not (= (length files) (length (org-agenda-files t))))
  13698. (progn
  13699. (org-store-new-agenda-file-list files)
  13700. (org-install-agenda-files-menu)
  13701. (message "Removed file: %s" afile))
  13702. (message "File was not in list: %s (not removed)" afile))))
  13703. (defun org-file-menu-entry (file)
  13704. (vector file (list 'find-file file) t))
  13705. (defun org-check-agenda-file (file)
  13706. "Make sure FILE exists. If not, ask user what to do."
  13707. (when (not (file-exists-p file))
  13708. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13709. (abbreviate-file-name file))
  13710. (let ((r (downcase (read-char-exclusive))))
  13711. (cond
  13712. ((equal r ?r)
  13713. (org-remove-file file)
  13714. (throw 'nextfile t))
  13715. (t (error "Abort"))))))
  13716. (defun org-get-agenda-file-buffer (file)
  13717. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13718. it to the list of buffers which might be released later."
  13719. (let ((buf (org-find-base-buffer-visiting file)))
  13720. (if buf
  13721. buf ; just return it
  13722. ;; Make a new buffer and remember it
  13723. (setq buf (find-file-noselect file))
  13724. (if buf (push buf org-agenda-new-buffers))
  13725. buf)))
  13726. (defun org-release-buffers (blist)
  13727. "Release all buffers in list, asking the user for confirmation when needed.
  13728. When a buffer is unmodified, it is just killed. When modified, it is saved
  13729. \(if the user agrees) and then killed."
  13730. (let (buf file)
  13731. (while (setq buf (pop blist))
  13732. (setq file (buffer-file-name buf))
  13733. (when (and (buffer-modified-p buf)
  13734. file
  13735. (y-or-n-p (format "Save file %s? " file)))
  13736. (with-current-buffer buf (save-buffer)))
  13737. (kill-buffer buf))))
  13738. (defun org-prepare-agenda-buffers (files)
  13739. "Create buffers for all agenda files, protect archived trees and comments."
  13740. (interactive)
  13741. (let ((pa '(:org-archived t))
  13742. (pc '(:org-comment t))
  13743. (pall '(:org-archived t :org-comment t))
  13744. (inhibit-read-only t)
  13745. (rea (concat ":" org-archive-tag ":"))
  13746. bmp file re)
  13747. (save-excursion
  13748. (save-restriction
  13749. (while (setq file (pop files))
  13750. (catch 'nextfile
  13751. (if (bufferp file)
  13752. (set-buffer file)
  13753. (org-check-agenda-file file)
  13754. (set-buffer (org-get-agenda-file-buffer file)))
  13755. (widen)
  13756. (setq bmp (buffer-modified-p))
  13757. (org-refresh-category-properties)
  13758. (setq org-todo-keywords-for-agenda
  13759. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13760. (setq org-done-keywords-for-agenda
  13761. (append org-done-keywords-for-agenda org-done-keywords))
  13762. (setq org-todo-keyword-alist-for-agenda
  13763. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13764. (setq org-drawers-for-agenda
  13765. (append org-drawers-for-agenda org-drawers))
  13766. (setq org-tag-alist-for-agenda
  13767. (append org-tag-alist-for-agenda org-tag-alist))
  13768. (save-excursion
  13769. (remove-text-properties (point-min) (point-max) pall)
  13770. (when org-agenda-skip-archived-trees
  13771. (goto-char (point-min))
  13772. (while (re-search-forward rea nil t)
  13773. (if (org-on-heading-p t)
  13774. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13775. (goto-char (point-min))
  13776. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13777. (while (re-search-forward re nil t)
  13778. (add-text-properties
  13779. (match-beginning 0) (org-end-of-subtree t) pc)))
  13780. (set-buffer-modified-p bmp)))))
  13781. (setq org-todo-keywords-for-agenda
  13782. (org-uniquify org-todo-keywords-for-agenda))
  13783. (setq org-todo-keyword-alist-for-agenda
  13784. (org-uniquify org-todo-keyword-alist-for-agenda)
  13785. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13786. ;;;; Embedded LaTeX
  13787. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13788. "Keymap for the minor `org-cdlatex-mode'.")
  13789. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13790. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13791. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13792. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13793. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13794. (defvar org-cdlatex-texmathp-advice-is-done nil
  13795. "Flag remembering if we have applied the advice to texmathp already.")
  13796. (define-minor-mode org-cdlatex-mode
  13797. "Toggle the minor `org-cdlatex-mode'.
  13798. This mode supports entering LaTeX environment and math in LaTeX fragments
  13799. in Org-mode.
  13800. \\{org-cdlatex-mode-map}"
  13801. nil " OCDL" nil
  13802. (when org-cdlatex-mode (require 'cdlatex))
  13803. (unless org-cdlatex-texmathp-advice-is-done
  13804. (setq org-cdlatex-texmathp-advice-is-done t)
  13805. (defadvice texmathp (around org-math-always-on activate)
  13806. "Always return t in org-mode buffers.
  13807. This is because we want to insert math symbols without dollars even outside
  13808. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13809. an embedded LaTeX fragment, let texmathp do its job.
  13810. \\[org-cdlatex-mode-map]"
  13811. (interactive)
  13812. (let (p)
  13813. (cond
  13814. ((not (org-mode-p)) ad-do-it)
  13815. ((eq this-command 'cdlatex-math-symbol)
  13816. (setq ad-return-value t
  13817. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13818. (t
  13819. (let ((p (org-inside-LaTeX-fragment-p)))
  13820. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13821. (setq ad-return-value t
  13822. texmathp-why '("Org-mode embedded math" . 0))
  13823. (if p ad-do-it)))))))))
  13824. (defun turn-on-org-cdlatex ()
  13825. "Unconditionally turn on `org-cdlatex-mode'."
  13826. (org-cdlatex-mode 1))
  13827. (defun org-inside-LaTeX-fragment-p ()
  13828. "Test if point is inside a LaTeX fragment.
  13829. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13830. sequence appearing also before point.
  13831. Even though the matchers for math are configurable, this function assumes
  13832. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13833. delimiters are skipped when they have been removed by customization.
  13834. The return value is nil, or a cons cell with the delimiter and
  13835. and the position of this delimiter.
  13836. This function does a reasonably good job, but can locally be fooled by
  13837. for example currency specifications. For example it will assume being in
  13838. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13839. fragments that are properly closed, but during editing, we have to live
  13840. with the uncertainty caused by missing closing delimiters. This function
  13841. looks only before point, not after."
  13842. (catch 'exit
  13843. (let ((pos (point))
  13844. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13845. (lim (progn
  13846. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13847. (point)))
  13848. dd-on str (start 0) m re)
  13849. (goto-char pos)
  13850. (when dodollar
  13851. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13852. re (nth 1 (assoc "$" org-latex-regexps)))
  13853. (while (string-match re str start)
  13854. (cond
  13855. ((= (match-end 0) (length str))
  13856. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13857. ((= (match-end 0) (- (length str) 5))
  13858. (throw 'exit nil))
  13859. (t (setq start (match-end 0))))))
  13860. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13861. (goto-char pos)
  13862. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13863. (and (match-beginning 2) (throw 'exit nil))
  13864. ;; count $$
  13865. (while (re-search-backward "\\$\\$" lim t)
  13866. (setq dd-on (not dd-on)))
  13867. (goto-char pos)
  13868. (if dd-on (cons "$$" m))))))
  13869. (defun org-inside-latex-macro-p ()
  13870. "Is point inside a LaTeX macro or its arguments?"
  13871. (save-match-data
  13872. (org-in-regexp
  13873. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13874. (defun test ()
  13875. (interactive)
  13876. (message "%s" (org-inside-latex-macro-p)))
  13877. (defun org-try-cdlatex-tab ()
  13878. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13879. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13880. - inside a LaTeX fragment, or
  13881. - after the first word in a line, where an abbreviation expansion could
  13882. insert a LaTeX environment."
  13883. (when org-cdlatex-mode
  13884. (cond
  13885. ((save-excursion
  13886. (skip-chars-backward "a-zA-Z0-9*")
  13887. (skip-chars-backward " \t")
  13888. (bolp))
  13889. (cdlatex-tab) t)
  13890. ((org-inside-LaTeX-fragment-p)
  13891. (cdlatex-tab) t)
  13892. (t nil))))
  13893. (defun org-cdlatex-underscore-caret (&optional arg)
  13894. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13895. Revert to the normal definition outside of these fragments."
  13896. (interactive "P")
  13897. (if (org-inside-LaTeX-fragment-p)
  13898. (call-interactively 'cdlatex-sub-superscript)
  13899. (let (org-cdlatex-mode)
  13900. (call-interactively (key-binding (vector last-input-event))))))
  13901. (defun org-cdlatex-math-modify (&optional arg)
  13902. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13903. Revert to the normal definition outside of these fragments."
  13904. (interactive "P")
  13905. (if (org-inside-LaTeX-fragment-p)
  13906. (call-interactively 'cdlatex-math-modify)
  13907. (let (org-cdlatex-mode)
  13908. (call-interactively (key-binding (vector last-input-event))))))
  13909. (defvar org-latex-fragment-image-overlays nil
  13910. "List of overlays carrying the images of latex fragments.")
  13911. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13912. (defun org-remove-latex-fragment-image-overlays ()
  13913. "Remove all overlays with LaTeX fragment images in current buffer."
  13914. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  13915. (setq org-latex-fragment-image-overlays nil))
  13916. (defun org-preview-latex-fragment (&optional subtree)
  13917. "Preview the LaTeX fragment at point, or all locally or globally.
  13918. If the cursor is in a LaTeX fragment, create the image and overlay
  13919. it over the source code. If there is no fragment at point, display
  13920. all fragments in the current text, from one headline to the next. With
  13921. prefix SUBTREE, display all fragments in the current subtree. With a
  13922. double prefix `C-u C-u', or when the cursor is before the first headline,
  13923. display all fragments in the buffer.
  13924. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13925. (interactive "P")
  13926. (org-remove-latex-fragment-image-overlays)
  13927. (save-excursion
  13928. (save-restriction
  13929. (let (beg end at msg)
  13930. (cond
  13931. ((or (equal subtree '(16))
  13932. (not (save-excursion
  13933. (re-search-backward (concat "^" outline-regexp) nil t))))
  13934. (setq beg (point-min) end (point-max)
  13935. msg "Creating images for buffer...%s"))
  13936. ((equal subtree '(4))
  13937. (org-back-to-heading)
  13938. (setq beg (point) end (org-end-of-subtree t)
  13939. msg "Creating images for subtree...%s"))
  13940. (t
  13941. (if (setq at (org-inside-LaTeX-fragment-p))
  13942. (goto-char (max (point-min) (- (cdr at) 2)))
  13943. (org-back-to-heading))
  13944. (setq beg (point) end (progn (outline-next-heading) (point))
  13945. msg (if at "Creating image...%s"
  13946. "Creating images for entry...%s"))))
  13947. (message msg "")
  13948. (narrow-to-region beg end)
  13949. (goto-char beg)
  13950. (org-format-latex
  13951. (concat "ltxpng/" (file-name-sans-extension
  13952. (file-name-nondirectory
  13953. buffer-file-name)))
  13954. default-directory 'overlays msg at 'forbuffer)
  13955. (message msg "done. Use `C-c C-c' to remove images.")))))
  13956. (defvar org-latex-regexps
  13957. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13958. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13959. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13960. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13961. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13962. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13963. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13964. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13965. "Regular expressions for matching embedded LaTeX.")
  13966. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13967. "Replace LaTeX fragments with links to an image, and produce images.
  13968. Some of the options can be changed using the variable
  13969. `org-format-latex-options'."
  13970. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13971. (let* ((prefixnodir (file-name-nondirectory prefix))
  13972. (absprefix (expand-file-name prefix dir))
  13973. (todir (file-name-directory absprefix))
  13974. (opt org-format-latex-options)
  13975. (matchers (plist-get opt :matchers))
  13976. (re-list org-latex-regexps)
  13977. (org-format-latex-header-extra
  13978. (plist-get (org-infile-export-plist) :latex-header-extra))
  13979. (cnt 0) txt hash link beg end re e checkdir
  13980. executables-checked
  13981. m n block linkfile movefile ov)
  13982. ;; Check the different regular expressions
  13983. (while (setq e (pop re-list))
  13984. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13985. block (if (nth 3 e) "\n\n" ""))
  13986. (when (member m matchers)
  13987. (goto-char (point-min))
  13988. (while (re-search-forward re nil t)
  13989. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13990. (not (get-text-property (match-beginning n)
  13991. 'org-protected))
  13992. (or (not overlays)
  13993. (not (eq (get-char-property (match-beginning n)
  13994. 'org-overlay-type)
  13995. 'org-latex-overlay))))
  13996. (setq txt (match-string n)
  13997. beg (match-beginning n) end (match-end n)
  13998. cnt (1+ cnt))
  13999. (let (print-length print-level) ; make sure full list is printed
  14000. (setq hash (sha1 (prin1-to-string
  14001. (list org-format-latex-header
  14002. org-format-latex-header-extra
  14003. org-export-latex-default-packages-alist
  14004. org-export-latex-packages-alist
  14005. org-format-latex-options
  14006. forbuffer txt)))
  14007. linkfile (format "%s_%s.png" prefix hash)
  14008. movefile (format "%s_%s.png" absprefix hash)))
  14009. (setq link (concat block "[[file:" linkfile "]]" block))
  14010. (if msg (message msg cnt))
  14011. (goto-char beg)
  14012. (unless checkdir ; make sure the directory exists
  14013. (setq checkdir t)
  14014. (or (file-directory-p todir) (make-directory todir)))
  14015. (unless executables-checked
  14016. (org-check-external-command
  14017. "latex" "needed to convert LaTeX fragments to images")
  14018. (org-check-external-command
  14019. "dvipng" "needed to convert LaTeX fragments to images")
  14020. (setq executables-checked t))
  14021. (unless (file-exists-p movefile)
  14022. (org-create-formula-image
  14023. txt movefile opt forbuffer))
  14024. (if overlays
  14025. (progn
  14026. (mapc (lambda (o)
  14027. (if (eq (overlay-get o 'org-overlay-type)
  14028. 'org-latex-overlay)
  14029. (delete-overlay o)))
  14030. (overlays-in beg end))
  14031. (setq ov (make-overlay beg end))
  14032. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14033. (if (featurep 'xemacs)
  14034. (progn
  14035. (overlay-put ov 'invisible t)
  14036. (overlay-put
  14037. ov 'end-glyph
  14038. (make-glyph (vector 'png :file movefile))))
  14039. (overlay-put
  14040. ov 'display
  14041. (list 'image :type 'png :file movefile :ascent 'center)))
  14042. (push ov org-latex-fragment-image-overlays)
  14043. (goto-char end))
  14044. (delete-region beg end)
  14045. (insert (org-add-props link
  14046. (list 'org-latex-src
  14047. (replace-regexp-in-string "\"" "" txt)))))))))))
  14048. ;; This function borrows from Ganesh Swami's latex2png.el
  14049. (defun org-create-formula-image (string tofile options buffer)
  14050. "This calls dvipng."
  14051. (require 'org-latex)
  14052. (let* ((tmpdir (if (featurep 'xemacs)
  14053. (temp-directory)
  14054. temporary-file-directory))
  14055. (texfilebase (make-temp-name
  14056. (expand-file-name "orgtex" tmpdir)))
  14057. (texfile (concat texfilebase ".tex"))
  14058. (dvifile (concat texfilebase ".dvi"))
  14059. (pngfile (concat texfilebase ".png"))
  14060. (fnh (if (featurep 'xemacs)
  14061. (font-height (get-face-font 'default))
  14062. (face-attribute 'default :height nil)))
  14063. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14064. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14065. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14066. "Black"))
  14067. (bg (or (plist-get options (if buffer :background :html-background))
  14068. "Transparent")))
  14069. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14070. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14071. (with-temp-file texfile
  14072. (insert (org-splice-latex-header
  14073. org-format-latex-header
  14074. org-export-latex-default-packages-alist
  14075. org-export-latex-packages-alist t
  14076. org-format-latex-header-extra))
  14077. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14078. (require 'org-latex)
  14079. (org-export-latex-fix-inputenc))
  14080. (let ((dir default-directory))
  14081. (condition-case nil
  14082. (progn
  14083. (cd tmpdir)
  14084. (call-process "latex" nil nil nil texfile))
  14085. (error nil))
  14086. (cd dir))
  14087. (if (not (file-exists-p dvifile))
  14088. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14089. (condition-case nil
  14090. (call-process "dvipng" nil nil nil
  14091. "-fg" fg "-bg" bg
  14092. "-D" dpi
  14093. ;;"-x" scale "-y" scale
  14094. "-T" "tight"
  14095. "-o" pngfile
  14096. dvifile)
  14097. (error nil))
  14098. (if (not (file-exists-p pngfile))
  14099. (if org-format-latex-signal-error
  14100. (error "Failed to create png file from %s" texfile)
  14101. (message "Failed to create png file from %s" texfile)
  14102. nil)
  14103. ;; Use the requested file name and clean up
  14104. (copy-file pngfile tofile 'replace)
  14105. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14106. (delete-file (concat texfilebase e)))
  14107. pngfile))))
  14108. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14109. "Fill a LaTeX header template TPL.
  14110. In the template, the following place holders will be recognized:
  14111. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14112. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14113. [PACKAGES] \\usepackage statements for PKG
  14114. [NO-PACKAGES] do not include PKG
  14115. [EXTRA] the string EXTRA
  14116. [NO-EXTRA] do not include EXTRA
  14117. For backward compatibility, if both the positive and the negative place
  14118. holder is missing, the positive one (without the \"NO-\") will be
  14119. assumed to be present at the end of the template.
  14120. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14121. EXTRA is a string.
  14122. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  14123. (let (rpl (end ""))
  14124. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14125. (setq rpl (if (or (match-end 1) (not def-pkg))
  14126. "" (org-latex-packages-to-string def-pkg snippets-p t))
  14127. tpl (replace-match rpl t t tpl))
  14128. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  14129. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14130. (setq rpl (if (or (match-end 1) (not pkg))
  14131. "" (org-latex-packages-to-string pkg snippets-p t))
  14132. tpl (replace-match rpl t t tpl))
  14133. (if pkg (setq end
  14134. (concat end "\n"
  14135. (org-latex-packages-to-string pkg snippets-p)))))
  14136. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14137. (setq rpl (if (or (match-end 1) (not extra))
  14138. "" (concat extra "\n"))
  14139. tpl (replace-match rpl t t tpl))
  14140. (if (and extra (string-match "\\S-" extra))
  14141. (setq end (concat end "\n" extra))))
  14142. (if (string-match "\\S-" end)
  14143. (concat tpl "\n" end)
  14144. tpl)))
  14145. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  14146. "Turn an alist of packages into a string with the \\usepackage macros."
  14147. (setq pkg (mapconcat (lambda(p)
  14148. (cond
  14149. ((stringp p) p)
  14150. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  14151. (format "%% Package %s omitted" (cadr p)))
  14152. ((equal "" (car p))
  14153. (format "\\usepackage{%s}" (cadr p)))
  14154. (t
  14155. (format "\\usepackage[%s]{%s}"
  14156. (car p) (cadr p)))))
  14157. pkg
  14158. "\n"))
  14159. (if newline (concat pkg "\n") pkg))
  14160. (defun org-dvipng-color (attr)
  14161. "Return an rgb color specification for dvipng."
  14162. (apply 'format "rgb %s %s %s"
  14163. (mapcar 'org-normalize-color
  14164. (color-values (face-attribute 'default attr nil)))))
  14165. (defun org-normalize-color (value)
  14166. "Return string to be used as color value for an RGB component."
  14167. (format "%g" (/ value 65535.0)))
  14168. ;; Image display
  14169. (defvar org-inline-image-overlays nil)
  14170. (make-variable-buffer-local 'org-inline-image-overlays)
  14171. (defun org-toggle-inline-images (&optional include-linked)
  14172. "Toggle the display of inline images.
  14173. INCLUDE-LINKED is passed to `org-display-inline-images'."
  14174. (interactive "P")
  14175. (if org-inline-image-overlays
  14176. (progn
  14177. (org-remove-inline-images)
  14178. (message "Inline image display turned off"))
  14179. (org-display-inline-images include-linked)
  14180. (if org-inline-image-overlays
  14181. (message "%d images displayed inline"
  14182. (length org-inline-image-overlays))
  14183. (message "No images to display inline"))))
  14184. (defun org-display-inline-images (&optional include-linked)
  14185. "Display inline images.
  14186. Normally only links without a description part are inlined, because this
  14187. is how it will work for export. When INCLUDE-LINKED is set, also links
  14188. with a description part will be inlined."
  14189. (interactive "P")
  14190. (org-remove-inline-images)
  14191. (goto-char (point-min))
  14192. (let ((re (concat "\\[\\[\\(file:\\|\\./\\)\\(~?" "[-+./_0-9a-zA-Z]+"
  14193. (substring (org-image-file-name-regexp) 0 -2)
  14194. "\\)\\]" (if include-linked "" "\\]")))
  14195. file ov)
  14196. (while (re-search-forward re nil t)
  14197. (setq file (expand-file-name (match-string 2)))
  14198. (when (file-exists-p file)
  14199. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  14200. (overlay-put ov 'display (create-image file))
  14201. (overlay-put ov 'face 'default)
  14202. (push ov org-inline-image-overlays)))))
  14203. (defun org-remove-inline-images ()
  14204. "Remove inline display of images."
  14205. (interactive)
  14206. (mapc 'delete-overlay org-inline-image-overlays)
  14207. (setq org-inline-image-overlays nil))
  14208. ;;;; Key bindings
  14209. ;; Make `C-c C-x' a prefix key
  14210. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14211. ;; TAB key with modifiers
  14212. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14213. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14214. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14215. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14216. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14217. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14218. ;; The following line is necessary under Suse GNU/Linux
  14219. (unless (featurep 'xemacs)
  14220. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14221. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14222. (define-key org-mode-map [backtab] 'org-shifttab)
  14223. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14224. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14225. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14226. ;; Cursor keys with modifiers
  14227. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14228. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14229. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14230. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14231. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14232. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14233. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14234. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14235. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14236. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14237. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14238. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14239. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14240. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14241. ;;; Extra keys for tty access.
  14242. ;; We only set them when really needed because otherwise the
  14243. ;; menus don't show the simple keys
  14244. (when (or org-use-extra-keys
  14245. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14246. (not window-system))
  14247. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14248. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14249. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14250. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14251. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14252. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14253. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14254. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14255. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14256. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14257. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14258. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14259. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14260. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14261. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14262. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14263. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14264. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14265. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14266. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14267. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14268. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14269. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14270. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14271. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14272. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14273. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14274. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14275. ;; All the other keys
  14276. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14277. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14278. (if (boundp 'narrow-map)
  14279. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14280. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14281. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14282. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14283. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14284. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14285. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14286. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14287. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14288. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14289. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14290. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14291. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14292. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14293. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14294. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14295. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14296. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14297. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14298. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14299. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14300. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14301. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14302. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14303. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14304. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14305. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14306. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14307. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14308. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14309. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14310. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14311. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14312. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14313. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14314. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14315. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14316. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14317. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14318. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14319. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14320. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14321. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14322. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14323. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14324. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14325. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14326. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14327. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14328. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14329. (org-defkey org-mode-map "\C-m" 'org-return)
  14330. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14331. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14332. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14333. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14334. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14335. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14336. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14337. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14338. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14339. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14340. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14341. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14342. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14343. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14344. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14345. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14346. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14347. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14348. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14349. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14350. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14351. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14352. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14353. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14354. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14355. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14356. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14357. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14358. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14359. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14360. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14361. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14362. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14363. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14364. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  14365. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14366. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14367. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14368. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14369. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14370. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14371. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14372. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14373. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14374. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14375. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14376. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14377. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14378. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14379. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14380. (when (featurep 'xemacs)
  14381. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14382. (defconst org-speed-commands-default
  14383. '(
  14384. ("Outline Navigation")
  14385. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14386. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14387. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14388. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14389. ("u" . (org-speed-move-safe 'outline-up-heading))
  14390. ("j" . org-goto)
  14391. ("g" . (org-refile t))
  14392. ("Outline Visibility")
  14393. ("c" . org-cycle)
  14394. ("C" . org-shifttab)
  14395. (" " . org-display-outline-path)
  14396. ("Outline Structure Editing")
  14397. ("U" . org-shiftmetaup)
  14398. ("D" . org-shiftmetadown)
  14399. ("r" . org-metaright)
  14400. ("l" . org-metaleft)
  14401. ("R" . org-shiftmetaright)
  14402. ("L" . org-shiftmetaleft)
  14403. ("i" . (progn (forward-char 1) (call-interactively
  14404. 'org-insert-heading-respect-content)))
  14405. ("^" . org-sort)
  14406. ("w" . org-refile)
  14407. ("a" . org-archive-subtree-default-with-confirmation)
  14408. ("." . outline-mark-subtree)
  14409. ("Clock Commands")
  14410. ("I" . org-clock-in)
  14411. ("O" . org-clock-out)
  14412. ("Meta Data Editing")
  14413. ("t" . org-todo)
  14414. ("0" . (org-priority ?\ ))
  14415. ("1" . (org-priority ?A))
  14416. ("2" . (org-priority ?B))
  14417. ("3" . (org-priority ?C))
  14418. (";" . org-set-tags-command)
  14419. ("e" . org-set-effort)
  14420. ("Agenda Views etc")
  14421. ("v" . org-agenda)
  14422. ("/" . org-sparse-tree)
  14423. ("Misc")
  14424. ("o" . org-open-at-point)
  14425. ("?" . org-speed-command-help)
  14426. )
  14427. "The default speed commands.")
  14428. (defun org-print-speed-command (e)
  14429. (if (> (length (car e)) 1)
  14430. (progn
  14431. (princ "\n")
  14432. (princ (car e))
  14433. (princ "\n")
  14434. (princ (make-string (length (car e)) ?-))
  14435. (princ "\n"))
  14436. (princ (car e))
  14437. (princ " ")
  14438. (if (symbolp (cdr e))
  14439. (princ (symbol-name (cdr e)))
  14440. (prin1 (cdr e)))
  14441. (princ "\n")))
  14442. (defun org-speed-command-help ()
  14443. "Show the available speed commands."
  14444. (interactive)
  14445. (if (not org-use-speed-commands)
  14446. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14447. (with-output-to-temp-buffer "*Help*"
  14448. (princ "User-defined Speed commands\n===========================\n")
  14449. (mapc 'org-print-speed-command org-speed-commands-user)
  14450. (princ "\n")
  14451. (princ "Built-in Speed commands\n=======================\n")
  14452. (mapc 'org-print-speed-command org-speed-commands-default))
  14453. (with-current-buffer "*Help*"
  14454. (setq truncate-lines t))))
  14455. (defun org-speed-move-safe (cmd)
  14456. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14457. If not, return to the original position and throw an error."
  14458. (interactive)
  14459. (let ((pos (point)))
  14460. (call-interactively cmd)
  14461. (unless (and (bolp) (org-on-heading-p))
  14462. (goto-char pos)
  14463. (error "Boundary reached while executing %s" cmd))))
  14464. (defvar org-self-insert-command-undo-counter 0)
  14465. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14466. (defvar org-speed-command nil)
  14467. (defun org-self-insert-command (N)
  14468. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14469. If the cursor is in a table looking at whitespace, the whitespace is
  14470. overwritten, and the table is not marked as requiring realignment."
  14471. (interactive "p")
  14472. (cond
  14473. ((and org-use-speed-commands
  14474. (or (and (bolp) (looking-at outline-regexp))
  14475. (and (functionp org-use-speed-commands)
  14476. (funcall org-use-speed-commands)))
  14477. (setq
  14478. org-speed-command
  14479. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14480. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14481. (cond
  14482. ((commandp org-speed-command)
  14483. (setq this-command org-speed-command)
  14484. (call-interactively org-speed-command))
  14485. ((functionp org-speed-command)
  14486. (funcall org-speed-command))
  14487. ((and org-speed-command (listp org-speed-command))
  14488. (eval org-speed-command))
  14489. (t (let (org-use-speed-commands)
  14490. (call-interactively 'org-self-insert-command)))))
  14491. ((and
  14492. (org-table-p)
  14493. (progn
  14494. ;; check if we blank the field, and if that triggers align
  14495. (and (featurep 'org-table) org-table-auto-blank-field
  14496. (member last-command
  14497. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14498. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14499. ;; got extra space, this field does not determine column width
  14500. (let (org-table-may-need-update) (org-table-blank-field))
  14501. ;; no extra space, this field may determine column width
  14502. (org-table-blank-field)))
  14503. t)
  14504. (eq N 1)
  14505. (looking-at "[^|\n]* |"))
  14506. (let (org-table-may-need-update)
  14507. (goto-char (1- (match-end 0)))
  14508. (delete-backward-char 1)
  14509. (goto-char (match-beginning 0))
  14510. (self-insert-command N)))
  14511. (t
  14512. (setq org-table-may-need-update t)
  14513. (self-insert-command N)
  14514. (org-fix-tags-on-the-fly)
  14515. (if org-self-insert-cluster-for-undo
  14516. (if (not (eq last-command 'org-self-insert-command))
  14517. (setq org-self-insert-command-undo-counter 1)
  14518. (if (>= org-self-insert-command-undo-counter 20)
  14519. (setq org-self-insert-command-undo-counter 1)
  14520. (and (> org-self-insert-command-undo-counter 0)
  14521. buffer-undo-list
  14522. (not (cadr buffer-undo-list)) ; remove nil entry
  14523. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14524. (setq org-self-insert-command-undo-counter
  14525. (1+ org-self-insert-command-undo-counter))))))))
  14526. (defun org-fix-tags-on-the-fly ()
  14527. (when (and (equal (char-after (point-at-bol)) ?*)
  14528. (org-on-heading-p))
  14529. (org-align-tags-here org-tags-column)))
  14530. (defun org-delete-backward-char (N)
  14531. "Like `delete-backward-char', insert whitespace at field end in tables.
  14532. When deleting backwards, in tables this function will insert whitespace in
  14533. front of the next \"|\" separator, to keep the table aligned. The table will
  14534. still be marked for re-alignment if the field did fill the entire column,
  14535. because, in this case the deletion might narrow the column."
  14536. (interactive "p")
  14537. (if (and (org-table-p)
  14538. (eq N 1)
  14539. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14540. (looking-at ".*?|"))
  14541. (let ((pos (point))
  14542. (noalign (looking-at "[^|\n\r]* |"))
  14543. (c org-table-may-need-update))
  14544. (backward-delete-char N)
  14545. (skip-chars-forward "^|")
  14546. (insert " ")
  14547. (goto-char (1- pos))
  14548. ;; noalign: if there were two spaces at the end, this field
  14549. ;; does not determine the width of the column.
  14550. (if noalign (setq org-table-may-need-update c)))
  14551. (backward-delete-char N)
  14552. (org-fix-tags-on-the-fly)))
  14553. (defun org-delete-char (N)
  14554. "Like `delete-char', but insert whitespace at field end in tables.
  14555. When deleting characters, in tables this function will insert whitespace in
  14556. front of the next \"|\" separator, to keep the table aligned. The table will
  14557. still be marked for re-alignment if the field did fill the entire column,
  14558. because, in this case the deletion might narrow the column."
  14559. (interactive "p")
  14560. (if (and (org-table-p)
  14561. (not (bolp))
  14562. (not (= (char-after) ?|))
  14563. (eq N 1))
  14564. (if (looking-at ".*?|")
  14565. (let ((pos (point))
  14566. (noalign (looking-at "[^|\n\r]* |"))
  14567. (c org-table-may-need-update))
  14568. (replace-match (concat
  14569. (substring (match-string 0) 1 -1)
  14570. " |"))
  14571. (goto-char pos)
  14572. ;; noalign: if there were two spaces at the end, this field
  14573. ;; does not determine the width of the column.
  14574. (if noalign (setq org-table-may-need-update c)))
  14575. (delete-char N))
  14576. (delete-char N)
  14577. (org-fix-tags-on-the-fly)))
  14578. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14579. (put 'org-self-insert-command 'delete-selection t)
  14580. (put 'orgtbl-self-insert-command 'delete-selection t)
  14581. (put 'org-delete-char 'delete-selection 'supersede)
  14582. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14583. (put 'org-yank 'delete-selection 'yank)
  14584. ;; Make `flyspell-mode' delay after some commands
  14585. (put 'org-self-insert-command 'flyspell-delayed t)
  14586. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14587. (put 'org-delete-char 'flyspell-delayed t)
  14588. (put 'org-delete-backward-char 'flyspell-delayed t)
  14589. ;; Make pabbrev-mode expand after org-mode commands
  14590. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14591. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14592. ;; How to do this: Measure non-white length of current string
  14593. ;; If equal to column width, we should realign.
  14594. (defun org-remap (map &rest commands)
  14595. "In MAP, remap the functions given in COMMANDS.
  14596. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14597. (let (new old)
  14598. (while commands
  14599. (setq old (pop commands) new (pop commands))
  14600. (if (fboundp 'command-remapping)
  14601. (org-defkey map (vector 'remap old) new)
  14602. (substitute-key-definition old new map global-map)))))
  14603. (when (eq org-enable-table-editor 'optimized)
  14604. ;; If the user wants maximum table support, we need to hijack
  14605. ;; some standard editing functions
  14606. (org-remap org-mode-map
  14607. 'self-insert-command 'org-self-insert-command
  14608. 'delete-char 'org-delete-char
  14609. 'delete-backward-char 'org-delete-backward-char)
  14610. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14611. (defvar org-ctrl-c-ctrl-c-hook nil
  14612. "Hook for functions attaching themselves to `C-c C-c'.
  14613. This can be used to add additional functionality to the C-c C-c key which
  14614. executes context-dependent commands.
  14615. Each function will be called with no arguments. The function must check
  14616. if the context is appropriate for it to act. If yes, it should do its
  14617. thing and then return a non-nil value. If the context is wrong,
  14618. just do nothing and return nil.")
  14619. (defvar org-tab-first-hook nil
  14620. "Hook for functions to attach themselves to TAB.
  14621. See `org-ctrl-c-ctrl-c-hook' for more information.
  14622. This hook runs as the first action when TAB is pressed, even before
  14623. `org-cycle' messes around with the `outline-regexp' to cater for
  14624. inline tasks and plain list item folding.
  14625. If any function in this hook returns t, any other actions that
  14626. would have been caused by TAB (such as table field motion or visibility
  14627. cycling) will not occur.")
  14628. (defvar org-tab-after-check-for-table-hook nil
  14629. "Hook for functions to attach themselves to TAB.
  14630. See `org-ctrl-c-ctrl-c-hook' for more information.
  14631. This hook runs after it has been established that the cursor is not in a
  14632. table, but before checking if the cursor is in a headline or if global cycling
  14633. should be done.
  14634. If any function in this hook returns t, not other actions like visibility
  14635. cycling will be done.")
  14636. (defvar org-tab-after-check-for-cycling-hook nil
  14637. "Hook for functions to attach themselves to TAB.
  14638. See `org-ctrl-c-ctrl-c-hook' for more information.
  14639. This hook runs after it has been established that not table field motion and
  14640. not visibility should be done because of current context. This is probably
  14641. the place where a package like yasnippets can hook in.")
  14642. (defvar org-tab-before-tab-emulation-hook nil
  14643. "Hook for functions to attach themselves to TAB.
  14644. See `org-ctrl-c-ctrl-c-hook' for more information.
  14645. This hook runs after every other options for TAB have been exhausted, but
  14646. before indentation and \t insertion takes place.")
  14647. (defvar org-metaleft-hook nil
  14648. "Hook for functions attaching themselves to `M-left'.
  14649. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14650. (defvar org-metaright-hook nil
  14651. "Hook for functions attaching themselves to `M-right'.
  14652. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14653. (defvar org-metaup-hook nil
  14654. "Hook for functions attaching themselves to `M-up'.
  14655. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14656. (defvar org-metadown-hook nil
  14657. "Hook for functions attaching themselves to `M-down'.
  14658. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14659. (defvar org-shiftmetaleft-hook nil
  14660. "Hook for functions attaching themselves to `M-S-left'.
  14661. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14662. (defvar org-shiftmetaright-hook nil
  14663. "Hook for functions attaching themselves to `M-S-right'.
  14664. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14665. (defvar org-shiftmetaup-hook nil
  14666. "Hook for functions attaching themselves to `M-S-up'.
  14667. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14668. (defvar org-shiftmetadown-hook nil
  14669. "Hook for functions attaching themselves to `M-S-down'.
  14670. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14671. (defvar org-metareturn-hook nil
  14672. "Hook for functions attaching themselves to `M-RET'.
  14673. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14674. (defvar org-shiftup-hook nil
  14675. "Hook for functions attaching themselves to `S-up'.
  14676. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14677. (defvar org-shiftup-final-hook nil
  14678. "Hook for functions attaching themselves to `S-up'.
  14679. This one runs after all other options except shift-select have been excluded.
  14680. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14681. (defvar org-shiftdown-hook nil
  14682. "Hook for functions attaching themselves to `S-down'.
  14683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14684. (defvar org-shiftdown-final-hook nil
  14685. "Hook for functions attaching themselves to `S-down'.
  14686. This one runs after all other options except shift-select have been excluded.
  14687. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14688. (defvar org-shiftleft-hook nil
  14689. "Hook for functions attaching themselves to `S-left'.
  14690. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14691. (defvar org-shiftleft-final-hook nil
  14692. "Hook for functions attaching themselves to `S-left'.
  14693. This one runs after all other options except shift-select have been excluded.
  14694. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14695. (defvar org-shiftright-hook nil
  14696. "Hook for functions attaching themselves to `S-right'.
  14697. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14698. (defvar org-shiftright-final-hook nil
  14699. "Hook for functions attaching themselves to `S-right'.
  14700. This one runs after all other options except shift-select have been excluded.
  14701. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14702. (defun org-modifier-cursor-error ()
  14703. "Throw an error, a modified cursor command was applied in wrong context."
  14704. (error "This command is active in special context like tables, headlines or items"))
  14705. (defun org-shiftselect-error ()
  14706. "Throw an error because Shift-Cursor command was applied in wrong context."
  14707. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14708. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14709. (error "This command works only in special context like headlines or timestamps")))
  14710. (defun org-call-for-shift-select (cmd)
  14711. (let ((this-command-keys-shift-translated t))
  14712. (call-interactively cmd)))
  14713. (defun org-shifttab (&optional arg)
  14714. "Global visibility cycling or move to previous table field.
  14715. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14716. on context.
  14717. See the individual commands for more information."
  14718. (interactive "P")
  14719. (cond
  14720. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14721. ((integerp arg)
  14722. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14723. (message "Content view to level: %d" arg)
  14724. (org-content (prefix-numeric-value arg2))
  14725. (setq org-cycle-global-status 'overview)))
  14726. (t (call-interactively 'org-global-cycle))))
  14727. (defun org-shiftmetaleft ()
  14728. "Promote subtree or delete table column.
  14729. Calls `org-promote-subtree', `org-outdent-item',
  14730. or `org-table-delete-column', depending on context.
  14731. See the individual commands for more information."
  14732. (interactive)
  14733. (cond
  14734. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14735. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14736. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14737. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  14738. (t (org-modifier-cursor-error))))
  14739. (defun org-shiftmetaright ()
  14740. "Demote subtree or insert table column.
  14741. Calls `org-demote-subtree', `org-indent-item',
  14742. or `org-table-insert-column', depending on context.
  14743. See the individual commands for more information."
  14744. (interactive)
  14745. (cond
  14746. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14747. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14748. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14749. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  14750. (t (org-modifier-cursor-error))))
  14751. (defun org-shiftmetaup (&optional arg)
  14752. "Move subtree up or kill table row.
  14753. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14754. `org-move-item-up' depending on context. See the individual commands
  14755. for more information."
  14756. (interactive "P")
  14757. (cond
  14758. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14759. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14760. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14761. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14762. (t (org-modifier-cursor-error))))
  14763. (defun org-shiftmetadown (&optional arg)
  14764. "Move subtree down or insert table row.
  14765. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14766. `org-move-item-down', depending on context. See the individual
  14767. commands for more information."
  14768. (interactive "P")
  14769. (cond
  14770. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14771. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14772. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14773. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14774. (t (org-modifier-cursor-error))))
  14775. (defsubst org-hidden-tree-error ()
  14776. (error
  14777. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  14778. (defun org-metaleft (&optional arg)
  14779. "Promote heading or move table column to left.
  14780. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14781. With no specific context, calls the Emacs default `backward-word'.
  14782. See the individual commands for more information."
  14783. (interactive "P")
  14784. (cond
  14785. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14786. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14787. ((or (org-on-heading-p)
  14788. (and (org-region-active-p)
  14789. (save-excursion
  14790. (goto-char (region-beginning))
  14791. (org-on-heading-p))))
  14792. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  14793. (call-interactively 'org-do-promote))
  14794. ((or (org-at-item-p)
  14795. (and (org-region-active-p)
  14796. (save-excursion
  14797. (goto-char (region-beginning))
  14798. (org-at-item-p))))
  14799. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  14800. (call-interactively 'org-outdent-item))
  14801. (t (call-interactively 'backward-word))))
  14802. (defun org-metaright (&optional arg)
  14803. "Demote subtree or move table column to right.
  14804. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14805. With no specific context, calls the Emacs default `forward-word'.
  14806. See the individual commands for more information."
  14807. (interactive "P")
  14808. (cond
  14809. ((run-hook-with-args-until-success 'org-metaright-hook))
  14810. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14811. ((or (org-on-heading-p)
  14812. (and (org-region-active-p)
  14813. (save-excursion
  14814. (goto-char (region-beginning))
  14815. (org-on-heading-p))))
  14816. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  14817. (call-interactively 'org-do-demote))
  14818. ((or (org-at-item-p)
  14819. (and (org-region-active-p)
  14820. (save-excursion
  14821. (goto-char (region-beginning))
  14822. (org-at-item-p))))
  14823. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  14824. (call-interactively 'org-indent-item))
  14825. (t (call-interactively 'forward-word))))
  14826. (defun org-check-for-hidden (what)
  14827. "Check if there are hidden headlines/items in the current visual line.
  14828. WHAT can be either `headlines' or `items'. If the current line is
  14829. an outline or item heading and it has a folded subtree below it,
  14830. this fucntion returns t, nil otherwise."
  14831. (let ((re (cond
  14832. ((eq what 'headlines) (concat "^" org-outline-regexp))
  14833. ((eq what 'items) (concat "^" (org-item-re t)))
  14834. (t (error "This should not happen"))))
  14835. beg end)
  14836. (save-excursion
  14837. (catch 'exit
  14838. (unless (org-region-active-p)
  14839. (setq beg (point-at-bol))
  14840. (beginning-of-line 2)
  14841. (while (and (not (eobp)) ;; this is like `next-line'
  14842. (get-char-property (1- (point)) 'invisible))
  14843. (beginning-of-line 2))
  14844. (setq end (point))
  14845. (goto-char beg)
  14846. (goto-char (point-at-eol))
  14847. (setq end (max end (point)))
  14848. (while (re-search-forward re end t)
  14849. (if (get-char-property (match-beginning 0) 'invisible)
  14850. (throw 'exit t))))
  14851. nil))))
  14852. (defun org-metaup (&optional arg)
  14853. "Move subtree up or move table row up.
  14854. Calls `org-move-subtree-up' or `org-table-move-row' or
  14855. `org-move-item-up', depending on context. See the individual commands
  14856. for more information."
  14857. (interactive "P")
  14858. (cond
  14859. ((run-hook-with-args-until-success 'org-metaup-hook))
  14860. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14861. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14862. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14863. (t (transpose-lines 1) (beginning-of-line -1))))
  14864. (defun org-metadown (&optional arg)
  14865. "Move subtree down or move table row down.
  14866. Calls `org-move-subtree-down' or `org-table-move-row' or
  14867. `org-move-item-down', depending on context. See the individual
  14868. commands for more information."
  14869. (interactive "P")
  14870. (cond
  14871. ((run-hook-with-args-until-success 'org-metadown-hook))
  14872. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14873. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14874. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14875. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14876. (defun org-shiftup (&optional arg)
  14877. "Increase item in timestamp or increase priority of current headline.
  14878. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14879. depending on context. See the individual commands for more information."
  14880. (interactive "P")
  14881. (cond
  14882. ((run-hook-with-args-until-success 'org-shiftup-hook))
  14883. ((and org-support-shift-select (org-region-active-p))
  14884. (org-call-for-shift-select 'previous-line))
  14885. ((org-at-timestamp-p t)
  14886. (call-interactively (if org-edit-timestamp-down-means-later
  14887. 'org-timestamp-down 'org-timestamp-up)))
  14888. ((and (not (eq org-support-shift-select 'always))
  14889. org-enable-priority-commands
  14890. (org-on-heading-p))
  14891. (call-interactively 'org-priority-up))
  14892. ((and (not org-support-shift-select) (org-at-item-p))
  14893. (call-interactively 'org-previous-item))
  14894. ((org-clocktable-try-shift 'up arg))
  14895. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  14896. (org-support-shift-select
  14897. (org-call-for-shift-select 'previous-line))
  14898. (t (org-shiftselect-error))))
  14899. (defun org-shiftdown (&optional arg)
  14900. "Decrease item in timestamp or decrease priority of current headline.
  14901. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14902. depending on context. See the individual commands for more information."
  14903. (interactive "P")
  14904. (cond
  14905. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  14906. ((and org-support-shift-select (org-region-active-p))
  14907. (org-call-for-shift-select 'next-line))
  14908. ((org-at-timestamp-p t)
  14909. (call-interactively (if org-edit-timestamp-down-means-later
  14910. 'org-timestamp-up 'org-timestamp-down)))
  14911. ((and (not (eq org-support-shift-select 'always))
  14912. org-enable-priority-commands
  14913. (org-on-heading-p))
  14914. (call-interactively 'org-priority-down))
  14915. ((and (not org-support-shift-select) (org-at-item-p))
  14916. (call-interactively 'org-next-item))
  14917. ((org-clocktable-try-shift 'down arg))
  14918. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  14919. (org-support-shift-select
  14920. (org-call-for-shift-select 'next-line))
  14921. (t (org-shiftselect-error))))
  14922. (defun org-shiftright (&optional arg)
  14923. "Cycle the thing at point or in the current line, depending on context.
  14924. Depending on context, this does one of the following:
  14925. - switch a timestamp at point one day into the future
  14926. - on a headline, switch to the next TODO keyword.
  14927. - on an item, switch entire list to the next bullet type
  14928. - on a property line, switch to the next allowed value
  14929. - on a clocktable definition line, move time block into the future"
  14930. (interactive "P")
  14931. (cond
  14932. ((run-hook-with-args-until-success 'org-shiftright-hook))
  14933. ((and org-support-shift-select (org-region-active-p))
  14934. (org-call-for-shift-select 'forward-char))
  14935. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14936. ((and (not (eq org-support-shift-select 'always))
  14937. (org-on-heading-p))
  14938. (let ((org-inhibit-logging
  14939. (not org-treat-S-cursor-todo-selection-as-state-change))
  14940. (org-inhibit-blocking
  14941. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14942. (org-call-with-arg 'org-todo 'right)))
  14943. ((or (and org-support-shift-select
  14944. (not (eq org-support-shift-select 'always))
  14945. (org-at-item-bullet-p))
  14946. (and (not org-support-shift-select) (org-at-item-p)))
  14947. (org-call-with-arg 'org-cycle-list-bullet nil))
  14948. ((and (not (eq org-support-shift-select 'always))
  14949. (org-at-property-p))
  14950. (call-interactively 'org-property-next-allowed-value))
  14951. ((org-clocktable-try-shift 'right arg))
  14952. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  14953. (org-support-shift-select
  14954. (org-call-for-shift-select 'forward-char))
  14955. (t (org-shiftselect-error))))
  14956. (defun org-shiftleft (&optional arg)
  14957. "Cycle the thing at point or in the current line, depending on context.
  14958. Depending on context, this does one of the following:
  14959. - switch a timestamp at point one day into the past
  14960. - on a headline, switch to the previous TODO keyword.
  14961. - on an item, switch entire list to the previous bullet type
  14962. - on a property line, switch to the previous allowed value
  14963. - on a clocktable definition line, move time block into the past"
  14964. (interactive "P")
  14965. (cond
  14966. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  14967. ((and org-support-shift-select (org-region-active-p))
  14968. (org-call-for-shift-select 'backward-char))
  14969. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14970. ((and (not (eq org-support-shift-select 'always))
  14971. (org-on-heading-p))
  14972. (let ((org-inhibit-logging
  14973. (not org-treat-S-cursor-todo-selection-as-state-change))
  14974. (org-inhibit-blocking
  14975. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14976. (org-call-with-arg 'org-todo 'left)))
  14977. ((or (and org-support-shift-select
  14978. (not (eq org-support-shift-select 'always))
  14979. (org-at-item-bullet-p))
  14980. (and (not org-support-shift-select) (org-at-item-p)))
  14981. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14982. ((and (not (eq org-support-shift-select 'always))
  14983. (org-at-property-p))
  14984. (call-interactively 'org-property-previous-allowed-value))
  14985. ((org-clocktable-try-shift 'left arg))
  14986. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  14987. (org-support-shift-select
  14988. (org-call-for-shift-select 'backward-char))
  14989. (t (org-shiftselect-error))))
  14990. (defun org-shiftcontrolright ()
  14991. "Switch to next TODO set."
  14992. (interactive)
  14993. (cond
  14994. ((and org-support-shift-select (org-region-active-p))
  14995. (org-call-for-shift-select 'forward-word))
  14996. ((and (not (eq org-support-shift-select 'always))
  14997. (org-on-heading-p))
  14998. (org-call-with-arg 'org-todo 'nextset))
  14999. (org-support-shift-select
  15000. (org-call-for-shift-select 'forward-word))
  15001. (t (org-shiftselect-error))))
  15002. (defun org-shiftcontrolleft ()
  15003. "Switch to previous TODO set."
  15004. (interactive)
  15005. (cond
  15006. ((and org-support-shift-select (org-region-active-p))
  15007. (org-call-for-shift-select 'backward-word))
  15008. ((and (not (eq org-support-shift-select 'always))
  15009. (org-on-heading-p))
  15010. (org-call-with-arg 'org-todo 'previousset))
  15011. (org-support-shift-select
  15012. (org-call-for-shift-select 'backward-word))
  15013. (t (org-shiftselect-error))))
  15014. (defun org-ctrl-c-ret ()
  15015. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15016. (interactive)
  15017. (cond
  15018. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15019. (t (call-interactively 'org-insert-heading))))
  15020. (defun org-copy-special ()
  15021. "Copy region in table or copy current subtree.
  15022. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15023. See the individual commands for more information."
  15024. (interactive)
  15025. (call-interactively
  15026. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15027. (defun org-cut-special ()
  15028. "Cut region in table or cut current subtree.
  15029. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15030. See the individual commands for more information."
  15031. (interactive)
  15032. (call-interactively
  15033. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15034. (defun org-paste-special (arg)
  15035. "Paste rectangular region into table, or past subtree relative to level.
  15036. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15037. See the individual commands for more information."
  15038. (interactive "P")
  15039. (if (org-at-table-p)
  15040. (org-table-paste-rectangle)
  15041. (org-paste-subtree arg)))
  15042. (defun org-edit-special ()
  15043. "Call a special editor for the stuff at point.
  15044. When at a table, call the formula editor with `org-table-edit-formulas'.
  15045. When at the first line of an src example, call `org-edit-src-code'.
  15046. When in an #+include line, visit the include file. Otherwise call
  15047. `ffap' to visit the file at point."
  15048. (interactive)
  15049. (cond
  15050. ((org-at-table.el-p)
  15051. (org-edit-src-code))
  15052. ((org-at-table-p)
  15053. (call-interactively 'org-table-edit-formulas))
  15054. ((save-excursion
  15055. (beginning-of-line 1)
  15056. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  15057. (find-file (org-trim (match-string 1))))
  15058. ((org-edit-src-code))
  15059. ((org-edit-fixed-width-region))
  15060. (t (call-interactively 'ffap))))
  15061. (defun org-ctrl-c-ctrl-c (&optional arg)
  15062. "Set tags in headline, or update according to changed information at point.
  15063. This command does many different things, depending on context:
  15064. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  15065. this is what we do.
  15066. - If the cursor is on a statistics cookie, update it.
  15067. - If the cursor is in a headline, prompt for tags and insert them
  15068. into the current line, aligned to `org-tags-column'. When called
  15069. with prefix arg, realign all tags in the current buffer.
  15070. - If the cursor is in one of the special #+KEYWORD lines, this
  15071. triggers scanning the buffer for these lines and updating the
  15072. information.
  15073. - If the cursor is inside a table, realign the table. This command
  15074. works even if the automatic table editor has been turned off.
  15075. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  15076. the entire table.
  15077. - If the cursor is at a footnote reference or definition, jump to
  15078. the corresponding definition or references, respectively.
  15079. - If the cursor is a the beginning of a dynamic block, update it.
  15080. - If the current buffer is a remember buffer, close note and file
  15081. it. A prefix argument of 1 files to the default location
  15082. without further interaction. A prefix argument of 2 files to
  15083. the currently clocking task.
  15084. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  15085. links in this buffer.
  15086. - If the cursor is on a numbered item in a plain list, renumber the
  15087. ordered list.
  15088. - If the cursor is on a checkbox, toggle it."
  15089. (interactive "P")
  15090. (let ((org-enable-table-editor t))
  15091. (cond
  15092. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  15093. org-occur-highlights
  15094. org-latex-fragment-image-overlays)
  15095. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  15096. (org-remove-occur-highlights)
  15097. (org-remove-latex-fragment-image-overlays)
  15098. (message "Temporary highlights/overlays removed from current buffer"))
  15099. ((and (local-variable-p 'org-finish-function (current-buffer))
  15100. (fboundp org-finish-function))
  15101. (funcall org-finish-function))
  15102. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  15103. ((or (looking-at org-property-start-re)
  15104. (org-at-property-p))
  15105. (call-interactively 'org-property-action))
  15106. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  15107. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  15108. (or (org-on-heading-p) (org-at-item-p)))
  15109. (call-interactively 'org-update-statistics-cookies))
  15110. ((org-on-heading-p) (call-interactively 'org-set-tags))
  15111. ((org-at-table.el-p)
  15112. (message "Use C-c ' to edit table.el tables"))
  15113. ((org-at-table-p)
  15114. (org-table-maybe-eval-formula)
  15115. (if arg
  15116. (call-interactively 'org-table-recalculate)
  15117. (org-table-maybe-recalculate-line))
  15118. (call-interactively 'org-table-align))
  15119. ((or (org-footnote-at-reference-p)
  15120. (org-footnote-at-definition-p))
  15121. (call-interactively 'org-footnote-action))
  15122. ((org-at-item-checkbox-p)
  15123. (call-interactively 'org-toggle-checkbox))
  15124. ((org-at-item-p)
  15125. (if arg
  15126. (call-interactively 'org-toggle-checkbox)
  15127. (call-interactively 'org-maybe-renumber-ordered-list)))
  15128. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  15129. ;; Dynamic block
  15130. (beginning-of-line 1)
  15131. (save-excursion (org-update-dblock)))
  15132. ((save-excursion
  15133. (beginning-of-line 1)
  15134. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  15135. (cond
  15136. ((equal (match-string 1) "TBLFM")
  15137. ;; Recalculate the table before this line
  15138. (save-excursion
  15139. (beginning-of-line 1)
  15140. (skip-chars-backward " \r\n\t")
  15141. (if (org-at-table-p)
  15142. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  15143. (t
  15144. (let ((org-inhibit-startup-visibility-stuff t)
  15145. (org-startup-align-all-tables nil))
  15146. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  15147. (message "Local setup has been refreshed"))))
  15148. ((org-clock-update-time-maybe))
  15149. (t (error "C-c C-c can do nothing useful at this location")))))
  15150. (defun org-mode-restart ()
  15151. "Restart Org-mode, to scan again for special lines.
  15152. Also updates the keyword regular expressions."
  15153. (interactive)
  15154. (org-mode)
  15155. (message "Org-mode restarted"))
  15156. (defun org-kill-note-or-show-branches ()
  15157. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15158. (interactive)
  15159. (if (not org-finish-function)
  15160. (call-interactively 'show-branches)
  15161. (let ((org-note-abort t))
  15162. (funcall org-finish-function))))
  15163. (defun org-return (&optional indent)
  15164. "Goto next table row or insert a newline.
  15165. Calls `org-table-next-row' or `newline', depending on context.
  15166. See the individual commands for more information."
  15167. (interactive)
  15168. (cond
  15169. ((bobp) (if indent (newline-and-indent) (newline)))
  15170. ((org-at-table-p)
  15171. (org-table-justify-field-maybe)
  15172. (call-interactively 'org-table-next-row))
  15173. ((and org-return-follows-link
  15174. (eq (get-text-property (point) 'face) 'org-link))
  15175. (call-interactively 'org-open-at-point))
  15176. ((and (org-at-heading-p)
  15177. (looking-at
  15178. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15179. (org-show-entry)
  15180. (end-of-line 1)
  15181. (newline))
  15182. (t (if indent (newline-and-indent) (newline)))))
  15183. (defun org-return-indent ()
  15184. "Goto next table row or insert a newline and indent.
  15185. Calls `org-table-next-row' or `newline-and-indent', depending on
  15186. context. See the individual commands for more information."
  15187. (interactive)
  15188. (org-return t))
  15189. (defun org-ctrl-c-star ()
  15190. "Compute table, or change heading status of lines.
  15191. Calls `org-table-recalculate' or `org-toggle-heading',
  15192. depending on context."
  15193. (interactive)
  15194. (cond
  15195. ((org-at-table-p)
  15196. (call-interactively 'org-table-recalculate))
  15197. (t
  15198. ;; Convert all lines in region to list items
  15199. (call-interactively 'org-toggle-heading))))
  15200. (defun org-ctrl-c-minus ()
  15201. "Insert separator line in table or modify bullet status of line.
  15202. Also turns a plain line or a region of lines into list items.
  15203. Calls `org-table-insert-hline', `org-toggle-item', or
  15204. `org-cycle-list-bullet', depending on context."
  15205. (interactive)
  15206. (cond
  15207. ((org-at-table-p)
  15208. (call-interactively 'org-table-insert-hline))
  15209. ((org-region-active-p)
  15210. (call-interactively 'org-toggle-item))
  15211. ((org-in-item-p)
  15212. (call-interactively 'org-cycle-list-bullet))
  15213. (t
  15214. (call-interactively 'org-toggle-item))))
  15215. (defun org-toggle-item ()
  15216. "Convert headings or normal lines to items, items to normal lines.
  15217. If there is no active region, only the current line is considered.
  15218. If the first line in the region is a headline, convert all headlines to items.
  15219. If the first line in the region is an item, convert all items to normal lines.
  15220. If the first line is normal text, add an item bullet to each line."
  15221. (interactive)
  15222. (let (l2 l beg end)
  15223. (if (org-region-active-p)
  15224. (setq beg (region-beginning) end (region-end))
  15225. (setq beg (point-at-bol)
  15226. end (min (1+ (point-at-eol)) (point-max))))
  15227. (save-excursion
  15228. (goto-char end)
  15229. (setq l2 (org-current-line))
  15230. (goto-char beg)
  15231. (beginning-of-line 1)
  15232. (setq l (1- (org-current-line)))
  15233. (if (org-at-item-p)
  15234. ;; We already have items, de-itemize
  15235. (while (< (setq l (1+ l)) l2)
  15236. (when (org-at-item-p)
  15237. (goto-char (match-beginning 2))
  15238. (delete-region (match-beginning 2) (match-end 2))
  15239. (and (looking-at "[ \t]+") (replace-match "")))
  15240. (beginning-of-line 2))
  15241. (if (org-on-heading-p)
  15242. ;; Headings, convert to items
  15243. (while (< (setq l (1+ l)) l2)
  15244. (if (looking-at org-outline-regexp)
  15245. (replace-match "- " t t))
  15246. (beginning-of-line 2))
  15247. ;; normal lines, turn them into items
  15248. (while (< (setq l (1+ l)) l2)
  15249. (unless (org-at-item-p)
  15250. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15251. (replace-match "\\1- \\2")))
  15252. (beginning-of-line 2)))))))
  15253. (defun org-toggle-heading (&optional nstars)
  15254. "Convert headings to normal text, or items or text to headings.
  15255. If there is no active region, only the current line is considered.
  15256. If the first line is a heading, remove the stars from all headlines
  15257. in the region.
  15258. If the first line is a plain list item, turn all plain list items
  15259. into headings.
  15260. If the first line is a normal line, turn each and every line in the
  15261. region into a heading.
  15262. When converting a line into a heading, the number of stars is chosen
  15263. such that the lines become children of the current entry. However,
  15264. when a prefix argument is given, its value determines the number of
  15265. stars to add."
  15266. (interactive "P")
  15267. (let (l2 l itemp beg end)
  15268. (if (org-region-active-p)
  15269. (setq beg (region-beginning) end (region-end))
  15270. (setq beg (point-at-bol)
  15271. end (min (1+ (point-at-eol)) (point-max))))
  15272. (save-excursion
  15273. (goto-char end)
  15274. (setq l2 (org-current-line))
  15275. (goto-char beg)
  15276. (beginning-of-line 1)
  15277. (setq l (1- (org-current-line)))
  15278. (if (org-on-heading-p)
  15279. ;; We already have headlines, de-star them
  15280. (while (< (setq l (1+ l)) l2)
  15281. (when (org-on-heading-p t)
  15282. (and (looking-at outline-regexp) (replace-match "")))
  15283. (beginning-of-line 2))
  15284. (setq itemp (org-at-item-p))
  15285. (let* ((stars
  15286. (if nstars
  15287. (make-string (prefix-numeric-value current-prefix-arg)
  15288. ?*)
  15289. (save-excursion
  15290. (if (re-search-backward org-complex-heading-regexp nil t)
  15291. (match-string 1) ""))))
  15292. (add-stars (cond (nstars "")
  15293. ((equal stars "") "*")
  15294. (org-odd-levels-only "**")
  15295. (t "*")))
  15296. (rpl (concat stars add-stars " ")))
  15297. (while (< (setq l (1+ l)) l2)
  15298. (if itemp
  15299. (and (org-at-item-p) (replace-match rpl t t))
  15300. (unless (org-on-heading-p)
  15301. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15302. (replace-match (concat rpl (match-string 2))))))
  15303. (beginning-of-line 2)))))))
  15304. (defun org-meta-return (&optional arg)
  15305. "Insert a new heading or wrap a region in a table.
  15306. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15307. See the individual commands for more information."
  15308. (interactive "P")
  15309. (cond
  15310. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15311. ((org-at-table-p)
  15312. (call-interactively 'org-table-wrap-region))
  15313. (t (call-interactively 'org-insert-heading))))
  15314. ;;; Menu entries
  15315. ;; Define the Org-mode menus
  15316. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15317. '("Tbl"
  15318. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15319. ["Next Field" org-cycle (org-at-table-p)]
  15320. ["Previous Field" org-shifttab (org-at-table-p)]
  15321. ["Next Row" org-return (org-at-table-p)]
  15322. "--"
  15323. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15324. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15325. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15326. "--"
  15327. ("Column"
  15328. ["Move Column Left" org-metaleft (org-at-table-p)]
  15329. ["Move Column Right" org-metaright (org-at-table-p)]
  15330. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15331. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15332. ("Row"
  15333. ["Move Row Up" org-metaup (org-at-table-p)]
  15334. ["Move Row Down" org-metadown (org-at-table-p)]
  15335. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15336. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15337. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15338. "--"
  15339. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15340. ("Rectangle"
  15341. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15342. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15343. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15344. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15345. "--"
  15346. ("Calculate"
  15347. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15348. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15349. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15350. "--"
  15351. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15352. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15353. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15354. "--"
  15355. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15356. "--"
  15357. ["Sum Column/Rectangle" org-table-sum
  15358. (or (org-at-table-p) (org-region-active-p))]
  15359. ["Which Column?" org-table-current-column (org-at-table-p)])
  15360. ["Debug Formulas"
  15361. org-table-toggle-formula-debugger
  15362. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15363. ["Show Col/Row Numbers"
  15364. org-table-toggle-coordinate-overlays
  15365. :style toggle
  15366. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15367. "--"
  15368. ["Create" org-table-create (and (not (org-at-table-p))
  15369. org-enable-table-editor)]
  15370. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15371. ["Import from File" org-table-import (not (org-at-table-p))]
  15372. ["Export to File" org-table-export (org-at-table-p)]
  15373. "--"
  15374. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15375. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15376. '("Org"
  15377. ("Show/Hide"
  15378. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15379. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15380. ["Sparse Tree..." org-sparse-tree t]
  15381. ["Reveal Context" org-reveal t]
  15382. ["Show All" show-all t]
  15383. "--"
  15384. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15385. "--"
  15386. ["New Heading" org-insert-heading t]
  15387. ("Navigate Headings"
  15388. ["Up" outline-up-heading t]
  15389. ["Next" outline-next-visible-heading t]
  15390. ["Previous" outline-previous-visible-heading t]
  15391. ["Next Same Level" outline-forward-same-level t]
  15392. ["Previous Same Level" outline-backward-same-level t]
  15393. "--"
  15394. ["Jump" org-goto t])
  15395. ("Edit Structure"
  15396. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15397. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15398. "--"
  15399. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15400. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15401. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15402. "--"
  15403. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15404. "--"
  15405. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15406. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15407. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15408. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15409. "--"
  15410. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15411. "--"
  15412. ["Convert to odd levels" org-convert-to-odd-levels t]
  15413. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15414. ("Editing"
  15415. ["Emphasis..." org-emphasize t]
  15416. ["Edit Source Example" org-edit-special t]
  15417. "--"
  15418. ["Footnote new/jump" org-footnote-action t]
  15419. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15420. ("Archive"
  15421. ["Archive (default method)" org-archive-subtree-default t]
  15422. "--"
  15423. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15424. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15425. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15426. )
  15427. "--"
  15428. ("Hyperlinks"
  15429. ["Store Link (Global)" org-store-link t]
  15430. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15431. ["Insert Link" org-insert-link t]
  15432. ["Follow Link" org-open-at-point t]
  15433. "--"
  15434. ["Next link" org-next-link t]
  15435. ["Previous link" org-previous-link t]
  15436. "--"
  15437. ["Descriptive Links"
  15438. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15439. :style radio
  15440. :selected (member '(org-link) buffer-invisibility-spec)]
  15441. ["Literal Links"
  15442. (progn
  15443. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15444. :style radio
  15445. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15446. "--"
  15447. ("TODO Lists"
  15448. ["TODO/DONE/-" org-todo t]
  15449. ("Select keyword"
  15450. ["Next keyword" org-shiftright (org-on-heading-p)]
  15451. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15452. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15453. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15454. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15455. ["Show TODO Tree" org-show-todo-tree t]
  15456. ["Global TODO list" org-todo-list t]
  15457. "--"
  15458. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15459. :selected org-enforce-todo-dependencies :style toggle :active t]
  15460. "Settings for tree at point"
  15461. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15462. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15463. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15464. ["Do Children parallel" org-toggle-ordered-property :style radio
  15465. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15466. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15467. "--"
  15468. ["Set Priority" org-priority t]
  15469. ["Priority Up" org-shiftup t]
  15470. ["Priority Down" org-shiftdown t]
  15471. "--"
  15472. ["Get news from all feeds" org-feed-update-all t]
  15473. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15474. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15475. ("TAGS and Properties"
  15476. ["Set Tags" org-set-tags-command t]
  15477. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15478. "--"
  15479. ["Set property" org-set-property t]
  15480. ["Column view of properties" org-columns t]
  15481. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15482. ("Dates and Scheduling"
  15483. ["Timestamp" org-time-stamp t]
  15484. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15485. ("Change Date"
  15486. ["1 Day Later" org-shiftright t]
  15487. ["1 Day Earlier" org-shiftleft t]
  15488. ["1 ... Later" org-shiftup t]
  15489. ["1 ... Earlier" org-shiftdown t])
  15490. ["Compute Time Range" org-evaluate-time-range t]
  15491. ["Schedule Item" org-schedule t]
  15492. ["Deadline" org-deadline t]
  15493. "--"
  15494. ["Custom time format" org-toggle-time-stamp-overlays
  15495. :style radio :selected org-display-custom-times]
  15496. "--"
  15497. ["Goto Calendar" org-goto-calendar t]
  15498. ["Date from Calendar" org-date-from-calendar t]
  15499. "--"
  15500. ["Start/Restart Timer" org-timer-start t]
  15501. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15502. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15503. ["Insert Timer String" org-timer t]
  15504. ["Insert Timer Item" org-timer-item t])
  15505. ("Logging work"
  15506. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15507. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15508. ["Clock out" org-clock-out t]
  15509. ["Clock cancel" org-clock-cancel t]
  15510. "--"
  15511. ["Mark as default task" org-clock-mark-default-task t]
  15512. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15513. ["Goto running clock" org-clock-goto t]
  15514. "--"
  15515. ["Display times" org-clock-display t]
  15516. ["Create clock table" org-clock-report t]
  15517. "--"
  15518. ["Record DONE time"
  15519. (progn (setq org-log-done (not org-log-done))
  15520. (message "Switching to %s will %s record a timestamp"
  15521. (car org-done-keywords)
  15522. (if org-log-done "automatically" "not")))
  15523. :style toggle :selected org-log-done])
  15524. "--"
  15525. ["Agenda Command..." org-agenda t]
  15526. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15527. ("File List for Agenda")
  15528. ("Special views current file"
  15529. ["TODO Tree" org-show-todo-tree t]
  15530. ["Check Deadlines" org-check-deadlines t]
  15531. ["Timeline" org-timeline t]
  15532. ["Tags/Property tree" org-match-sparse-tree t])
  15533. "--"
  15534. ["Export/Publish..." org-export t]
  15535. ("LaTeX"
  15536. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15537. :selected org-cdlatex-mode]
  15538. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15539. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15540. ["Modify math symbol" org-cdlatex-math-modify
  15541. (org-inside-LaTeX-fragment-p)]
  15542. ["Insert citation" org-reftex-citation t]
  15543. "--"
  15544. ["Export LaTeX fragments as images"
  15545. (if (featurep 'org-exp)
  15546. (setq org-export-with-LaTeX-fragments
  15547. (not org-export-with-LaTeX-fragments))
  15548. (require 'org-exp))
  15549. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15550. org-export-with-LaTeX-fragments)]
  15551. "--"
  15552. ["Template for BEAMER" org-beamer-settings-template t])
  15553. "--"
  15554. ("MobileOrg"
  15555. ["Push Files and Views" org-mobile-push t]
  15556. ["Get Captured and Flagged" org-mobile-pull t]
  15557. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15558. "--"
  15559. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15560. "--"
  15561. ("Documentation"
  15562. ["Show Version" org-version t]
  15563. ["Info Documentation" org-info t])
  15564. ("Customize"
  15565. ["Browse Org Group" org-customize t]
  15566. "--"
  15567. ["Expand This Menu" org-create-customize-menu
  15568. (fboundp 'customize-menu-create)])
  15569. ["Send bug report" org-submit-bug-report t]
  15570. "--"
  15571. ("Refresh/Reload"
  15572. ["Refresh setup current buffer" org-mode-restart t]
  15573. ["Reload Org (after update)" org-reload t]
  15574. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15575. ))
  15576. (defun org-info (&optional node)
  15577. "Read documentation for Org-mode in the info system.
  15578. With optional NODE, go directly to that node."
  15579. (interactive)
  15580. (info (format "(org)%s" (or node ""))))
  15581. ;;;###autoload
  15582. (defun org-submit-bug-report ()
  15583. "Submit a bug report on Org-mode via mail.
  15584. Don't hesitate to report any problems or inaccurate documentation.
  15585. If you don't have setup sending mail from (X)Emacs, please copy the
  15586. output buffer into your mail program, as it gives us important
  15587. information about your Org-mode version and configuration."
  15588. (interactive)
  15589. (require 'reporter)
  15590. (org-load-modules-maybe)
  15591. (org-require-autoloaded-modules)
  15592. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15593. (reporter-submit-bug-report
  15594. "emacs-orgmode@gnu.org"
  15595. (org-version)
  15596. (let (list)
  15597. (save-window-excursion
  15598. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15599. (delete-other-windows)
  15600. (erase-buffer)
  15601. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15602. We would like to add your full Org-mode and Outline configuration to the
  15603. bug report. This greatly simplifies the work of the maintainer and
  15604. other experts on the mailing list.
  15605. HOWEVER, some variables you have customized may contain private
  15606. information. The names of customers, colleagues, or friends, might
  15607. appear in the form of file names, tags, todo states, or search strings.
  15608. If you answer yes to the prompt, you might want to check and remove
  15609. such private information before sending the email.")
  15610. (add-text-properties (point-min) (point-max) '(face org-warning))
  15611. (when (yes-or-no-p "Include your Org-mode configuration ")
  15612. (mapatoms
  15613. (lambda (v)
  15614. (and (boundp v)
  15615. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15616. (or (and (symbol-value v)
  15617. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15618. (and
  15619. (get v 'custom-type) (get v 'standard-value)
  15620. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15621. (push v list)))))
  15622. (kill-buffer (get-buffer "*Warn about privacy*"))
  15623. list))
  15624. nil nil
  15625. "Remember to cover the basics, that is, what you expected to happen and
  15626. what in fact did happen. You don't know how to make a good report? See
  15627. http://orgmode.org/manual/Feedback.html#Feedback
  15628. Your bug report will be posted to the Org-mode mailing list.
  15629. ------------------------------------------------------------------------")
  15630. (save-excursion
  15631. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15632. (replace-match "\\1Bug: \\3 [\\2]")))))
  15633. (defun org-install-agenda-files-menu ()
  15634. (let ((bl (buffer-list)))
  15635. (save-excursion
  15636. (while bl
  15637. (set-buffer (pop bl))
  15638. (if (org-mode-p) (setq bl nil)))
  15639. (when (org-mode-p)
  15640. (easy-menu-change
  15641. '("Org") "File List for Agenda"
  15642. (append
  15643. (list
  15644. ["Edit File List" (org-edit-agenda-file-list) t]
  15645. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15646. ["Remove Current File from List" org-remove-file t]
  15647. ["Cycle through agenda files" org-cycle-agenda-files t]
  15648. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15649. "--")
  15650. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15651. ;;;; Documentation
  15652. ;;;###autoload
  15653. (defun org-require-autoloaded-modules ()
  15654. (interactive)
  15655. (mapc 'require
  15656. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15657. org-docbook org-exp org-html org-icalendar
  15658. org-id org-latex
  15659. org-publish org-remember org-table
  15660. org-timer org-xoxo)))
  15661. ;;;###autoload
  15662. (defun org-reload (&optional uncompiled)
  15663. "Reload all org lisp files.
  15664. With prefix arg UNCOMPILED, load the uncompiled versions."
  15665. (interactive "P")
  15666. (require 'find-func)
  15667. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15668. (dir-org (file-name-directory (org-find-library-name "org")))
  15669. (dir-org-contrib (ignore-errors
  15670. (file-name-directory
  15671. (org-find-library-name "org-contribdir"))))
  15672. (files
  15673. (append (directory-files dir-org t file-re)
  15674. (and dir-org-contrib
  15675. (directory-files dir-org-contrib t file-re))))
  15676. (remove-re (concat (if (featurep 'xemacs)
  15677. "org-colview" "org-colview-xemacs")
  15678. "\\'")))
  15679. (setq files (mapcar 'file-name-sans-extension files))
  15680. (setq files (mapcar
  15681. (lambda (x) (if (string-match remove-re x) nil x))
  15682. files))
  15683. (setq files (delq nil files))
  15684. (mapc
  15685. (lambda (f)
  15686. (when (featurep (intern (file-name-nondirectory f)))
  15687. (if (and (not uncompiled)
  15688. (file-exists-p (concat f ".elc")))
  15689. (load (concat f ".elc") nil nil t)
  15690. (load (concat f ".el") nil nil t))))
  15691. files))
  15692. (org-version))
  15693. ;;;###autoload
  15694. (defun org-customize ()
  15695. "Call the customize function with org as argument."
  15696. (interactive)
  15697. (org-load-modules-maybe)
  15698. (org-require-autoloaded-modules)
  15699. (customize-browse 'org))
  15700. (defun org-create-customize-menu ()
  15701. "Create a full customization menu for Org-mode, insert it into the menu."
  15702. (interactive)
  15703. (org-load-modules-maybe)
  15704. (org-require-autoloaded-modules)
  15705. (if (fboundp 'customize-menu-create)
  15706. (progn
  15707. (easy-menu-change
  15708. '("Org") "Customize"
  15709. `(["Browse Org group" org-customize t]
  15710. "--"
  15711. ,(customize-menu-create 'org)
  15712. ["Set" Custom-set t]
  15713. ["Save" Custom-save t]
  15714. ["Reset to Current" Custom-reset-current t]
  15715. ["Reset to Saved" Custom-reset-saved t]
  15716. ["Reset to Standard Settings" Custom-reset-standard t]))
  15717. (message "\"Org\"-menu now contains full customization menu"))
  15718. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15719. ;;;; Miscellaneous stuff
  15720. ;;; Generally useful functions
  15721. (defun org-get-at-bol (property)
  15722. "Get text property PROPERTY at beginning of line."
  15723. (get-text-property (point-at-bol) property))
  15724. (defun org-find-text-property-in-string (prop s)
  15725. "Return the first non-nil value of property PROP in string S."
  15726. (or (get-text-property 0 prop s)
  15727. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15728. prop s)))
  15729. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15730. "Display the given MESSAGE as a warning."
  15731. (if (fboundp 'display-warning)
  15732. (display-warning 'org message
  15733. (if (featurep 'xemacs) 'warning :warning))
  15734. (let ((buf (get-buffer-create "*Org warnings*")))
  15735. (with-current-buffer buf
  15736. (goto-char (point-max))
  15737. (insert "Warning (Org): " message)
  15738. (unless (bolp)
  15739. (newline)))
  15740. (display-buffer buf)
  15741. (sit-for 0))))
  15742. (defun org-in-commented-line ()
  15743. "Is point in a line starting with `#'?"
  15744. (equal (char-after (point-at-bol)) ?#))
  15745. (defun org-in-verbatim-emphasis ()
  15746. (save-match-data
  15747. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15748. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15749. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15750. (if (and marker (marker-buffer marker)
  15751. (buffer-live-p (marker-buffer marker)))
  15752. (progn
  15753. (switch-to-buffer (marker-buffer marker))
  15754. (if (or (> marker (point-max)) (< marker (point-min)))
  15755. (widen))
  15756. (goto-char marker)
  15757. (org-show-context 'org-goto))
  15758. (if bookmark
  15759. (bookmark-jump bookmark)
  15760. (error "Cannot find location"))))
  15761. (defun org-quote-csv-field (s)
  15762. "Quote field for inclusion in CSV material."
  15763. (if (string-match "[\",]" s)
  15764. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15765. s))
  15766. (defun org-plist-delete (plist property)
  15767. "Delete PROPERTY from PLIST.
  15768. This is in contrast to merely setting it to 0."
  15769. (let (p)
  15770. (while plist
  15771. (if (not (eq property (car plist)))
  15772. (setq p (plist-put p (car plist) (nth 1 plist))))
  15773. (setq plist (cddr plist)))
  15774. p))
  15775. (defun org-force-self-insert (N)
  15776. "Needed to enforce self-insert under remapping."
  15777. (interactive "p")
  15778. (self-insert-command N))
  15779. (defun org-string-width (s)
  15780. "Compute width of string, ignoring invisible characters.
  15781. This ignores character with invisibility property `org-link', and also
  15782. characters with property `org-cwidth', because these will become invisible
  15783. upon the next fontification round."
  15784. (let (b l)
  15785. (when (or (eq t buffer-invisibility-spec)
  15786. (assq 'org-link buffer-invisibility-spec))
  15787. (while (setq b (text-property-any 0 (length s)
  15788. 'invisible 'org-link s))
  15789. (setq s (concat (substring s 0 b)
  15790. (substring s (or (next-single-property-change
  15791. b 'invisible s) (length s)))))))
  15792. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15793. (setq s (concat (substring s 0 b)
  15794. (substring s (or (next-single-property-change
  15795. b 'org-cwidth s) (length s))))))
  15796. (setq l (string-width s) b -1)
  15797. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15798. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15799. l))
  15800. (defun org-get-indentation (&optional line)
  15801. "Get the indentation of the current line, interpreting tabs.
  15802. When LINE is given, assume it represents a line and compute its indentation."
  15803. (if line
  15804. (if (string-match "^ *" (org-remove-tabs line))
  15805. (match-end 0))
  15806. (save-excursion
  15807. (beginning-of-line 1)
  15808. (skip-chars-forward " \t")
  15809. (current-column))))
  15810. (defun org-remove-tabs (s &optional width)
  15811. "Replace tabulators in S with spaces.
  15812. Assumes that s is a single line, starting in column 0."
  15813. (setq width (or width tab-width))
  15814. (while (string-match "\t" s)
  15815. (setq s (replace-match
  15816. (make-string
  15817. (- (* width (/ (+ (match-beginning 0) width) width))
  15818. (match-beginning 0)) ?\ )
  15819. t t s)))
  15820. s)
  15821. (defun org-fix-indentation (line ind)
  15822. "Fix indentation in LINE.
  15823. IND is a cons cell with target and minimum indentation.
  15824. If the current indentation in LINE is smaller than the minimum,
  15825. leave it alone. If it is larger than ind, set it to the target."
  15826. (let* ((l (org-remove-tabs line))
  15827. (i (org-get-indentation l))
  15828. (i1 (car ind)) (i2 (cdr ind)))
  15829. (if (>= i i2) (setq l (substring line i2)))
  15830. (if (> i1 0)
  15831. (concat (make-string i1 ?\ ) l)
  15832. l)))
  15833. (defun org-remove-indentation (code &optional n)
  15834. "Remove the maximum common indentation from the lines in CODE.
  15835. N may optionally be the number of spaces to remove."
  15836. (with-temp-buffer
  15837. (insert code)
  15838. (org-do-remove-indentation n)
  15839. (buffer-string)))
  15840. (defun org-do-remove-indentation (&optional n)
  15841. "Remove the maximum common indentation from the buffer."
  15842. (untabify (point-min) (point-max))
  15843. (let ((min 10000) re)
  15844. (if n
  15845. (setq min n)
  15846. (goto-char (point-min))
  15847. (while (re-search-forward "^ *[^ \n]" nil t)
  15848. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15849. (unless (or (= min 0) (= min 10000))
  15850. (setq re (format "^ \\{%d\\}" min))
  15851. (goto-char (point-min))
  15852. (while (re-search-forward re nil t)
  15853. (replace-match "")
  15854. (end-of-line 1))
  15855. min)))
  15856. (defun org-fill-template (template alist)
  15857. "Find each %key of ALIST in TEMPLATE and replace it."
  15858. (let ((case-fold-search nil)
  15859. entry key value)
  15860. (setq alist (sort (copy-sequence alist)
  15861. (lambda (a b) (< (length (car a)) (length (car b))))))
  15862. (while (setq entry (pop alist))
  15863. (setq template
  15864. (replace-regexp-in-string
  15865. (concat "%" (regexp-quote (car entry)))
  15866. (cdr entry) template t t)))
  15867. template))
  15868. (defun org-base-buffer (buffer)
  15869. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15870. (if (not buffer)
  15871. buffer
  15872. (or (buffer-base-buffer buffer)
  15873. buffer)))
  15874. (defun org-trim (s)
  15875. "Remove whitespace at beginning and end of string."
  15876. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15877. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15878. s)
  15879. (defun org-wrap (string &optional width lines)
  15880. "Wrap string to either a number of lines, or a width in characters.
  15881. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15882. that costs. If there is a word longer than WIDTH, the text is actually
  15883. wrapped to the length of that word.
  15884. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15885. many lines, whatever width that takes.
  15886. The return value is a list of lines, without newlines at the end."
  15887. (let* ((words (org-split-string string "[ \t\n]+"))
  15888. (maxword (apply 'max (mapcar 'org-string-width words)))
  15889. w ll)
  15890. (cond (width
  15891. (org-do-wrap words (max maxword width)))
  15892. (lines
  15893. (setq w maxword)
  15894. (setq ll (org-do-wrap words maxword))
  15895. (if (<= (length ll) lines)
  15896. ll
  15897. (setq ll words)
  15898. (while (> (length ll) lines)
  15899. (setq w (1+ w))
  15900. (setq ll (org-do-wrap words w)))
  15901. ll))
  15902. (t (error "Cannot wrap this")))))
  15903. (defun org-do-wrap (words width)
  15904. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15905. (let (lines line)
  15906. (while words
  15907. (setq line (pop words))
  15908. (while (and words (< (+ (length line) (length (car words))) width))
  15909. (setq line (concat line " " (pop words))))
  15910. (setq lines (push line lines)))
  15911. (nreverse lines)))
  15912. (defun org-split-string (string &optional separators)
  15913. "Splits STRING into substrings at SEPARATORS.
  15914. No empty strings are returned if there are matches at the beginning
  15915. and end of string."
  15916. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15917. (start 0)
  15918. notfirst
  15919. (list nil))
  15920. (while (and (string-match rexp string
  15921. (if (and notfirst
  15922. (= start (match-beginning 0))
  15923. (< start (length string)))
  15924. (1+ start) start))
  15925. (< (match-beginning 0) (length string)))
  15926. (setq notfirst t)
  15927. (or (eq (match-beginning 0) 0)
  15928. (and (eq (match-beginning 0) (match-end 0))
  15929. (eq (match-beginning 0) start))
  15930. (setq list
  15931. (cons (substring string start (match-beginning 0))
  15932. list)))
  15933. (setq start (match-end 0)))
  15934. (or (eq start (length string))
  15935. (setq list
  15936. (cons (substring string start)
  15937. list)))
  15938. (nreverse list)))
  15939. (defun org-quote-vert (s)
  15940. "Replace \"|\" with \"\\vert\"."
  15941. (while (string-match "|" s)
  15942. (setq s (replace-match "\\vert" t t s)))
  15943. s)
  15944. (defun org-uuidgen-p (s)
  15945. "Is S an ID created by UUIDGEN?"
  15946. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15947. (defun org-context ()
  15948. "Return a list of contexts of the current cursor position.
  15949. If several contexts apply, all are returned.
  15950. Each context entry is a list with a symbol naming the context, and
  15951. two positions indicating start and end of the context. Possible
  15952. contexts are:
  15953. :headline anywhere in a headline
  15954. :headline-stars on the leading stars in a headline
  15955. :todo-keyword on a TODO keyword (including DONE) in a headline
  15956. :tags on the TAGS in a headline
  15957. :priority on the priority cookie in a headline
  15958. :item on the first line of a plain list item
  15959. :item-bullet on the bullet/number of a plain list item
  15960. :checkbox on the checkbox in a plain list item
  15961. :table in an org-mode table
  15962. :table-special on a special filed in a table
  15963. :table-table in a table.el table
  15964. :link on a hyperlink
  15965. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15966. :target on a <<target>>
  15967. :radio-target on a <<<radio-target>>>
  15968. :latex-fragment on a LaTeX fragment
  15969. :latex-preview on a LaTeX fragment with overlayed preview image
  15970. This function expects the position to be visible because it uses font-lock
  15971. faces as a help to recognize the following contexts: :table-special, :link,
  15972. and :keyword."
  15973. (let* ((f (get-text-property (point) 'face))
  15974. (faces (if (listp f) f (list f)))
  15975. (p (point)) clist o)
  15976. ;; First the large context
  15977. (cond
  15978. ((org-on-heading-p t)
  15979. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15980. (when (progn
  15981. (beginning-of-line 1)
  15982. (looking-at org-todo-line-tags-regexp))
  15983. (push (org-point-in-group p 1 :headline-stars) clist)
  15984. (push (org-point-in-group p 2 :todo-keyword) clist)
  15985. (push (org-point-in-group p 4 :tags) clist))
  15986. (goto-char p)
  15987. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15988. (if (looking-at "\\[#[A-Z0-9]\\]")
  15989. (push (org-point-in-group p 0 :priority) clist)))
  15990. ((org-at-item-p)
  15991. (push (org-point-in-group p 2 :item-bullet) clist)
  15992. (push (list :item (point-at-bol)
  15993. (save-excursion (org-end-of-item) (point)))
  15994. clist)
  15995. (and (org-at-item-checkbox-p)
  15996. (push (org-point-in-group p 0 :checkbox) clist)))
  15997. ((org-at-table-p)
  15998. (push (list :table (org-table-begin) (org-table-end)) clist)
  15999. (if (memq 'org-formula faces)
  16000. (push (list :table-special
  16001. (previous-single-property-change p 'face)
  16002. (next-single-property-change p 'face)) clist)))
  16003. ((org-at-table-p 'any)
  16004. (push (list :table-table) clist)))
  16005. (goto-char p)
  16006. ;; Now the small context
  16007. (cond
  16008. ((org-at-timestamp-p)
  16009. (push (org-point-in-group p 0 :timestamp) clist))
  16010. ((memq 'org-link faces)
  16011. (push (list :link
  16012. (previous-single-property-change p 'face)
  16013. (next-single-property-change p 'face)) clist))
  16014. ((memq 'org-special-keyword faces)
  16015. (push (list :keyword
  16016. (previous-single-property-change p 'face)
  16017. (next-single-property-change p 'face)) clist))
  16018. ((org-on-target-p)
  16019. (push (org-point-in-group p 0 :target) clist)
  16020. (goto-char (1- (match-beginning 0)))
  16021. (if (looking-at org-radio-target-regexp)
  16022. (push (org-point-in-group p 0 :radio-target) clist))
  16023. (goto-char p))
  16024. ((setq o (car (delq nil
  16025. (mapcar
  16026. (lambda (x)
  16027. (if (memq x org-latex-fragment-image-overlays) x))
  16028. (overlays-at (point))))))
  16029. (push (list :latex-fragment
  16030. (overlay-start o) (overlay-end o)) clist)
  16031. (push (list :latex-preview
  16032. (overlay-start o) (overlay-end o)) clist))
  16033. ((org-inside-LaTeX-fragment-p)
  16034. ;; FIXME: positions wrong.
  16035. (push (list :latex-fragment (point) (point)) clist)))
  16036. (setq clist (nreverse (delq nil clist)))
  16037. clist))
  16038. ;; FIXME: Compare with at-regexp-p Do we need both?
  16039. (defun org-in-regexp (re &optional nlines visually)
  16040. "Check if point is inside a match of regexp.
  16041. Normally only the current line is checked, but you can include NLINES extra
  16042. lines both before and after point into the search.
  16043. If VISUALLY is set, require that the cursor is not after the match but
  16044. really on, so that the block visually is on the match."
  16045. (catch 'exit
  16046. (let ((pos (point))
  16047. (eol (point-at-eol (+ 1 (or nlines 0))))
  16048. (inc (if visually 1 0)))
  16049. (save-excursion
  16050. (beginning-of-line (- 1 (or nlines 0)))
  16051. (while (re-search-forward re eol t)
  16052. (if (and (<= (match-beginning 0) pos)
  16053. (>= (+ inc (match-end 0)) pos))
  16054. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  16055. (defun org-at-regexp-p (regexp)
  16056. "Is point inside a match of REGEXP in the current line?"
  16057. (catch 'exit
  16058. (save-excursion
  16059. (let ((pos (point)) (end (point-at-eol)))
  16060. (beginning-of-line 1)
  16061. (while (re-search-forward regexp end t)
  16062. (if (and (<= (match-beginning 0) pos)
  16063. (>= (match-end 0) pos))
  16064. (throw 'exit t)))
  16065. nil))))
  16066. (defun org-in-regexps-block-p (start-re end-re)
  16067. "Returns t if the current point is between matches of START-RE and END-RE.
  16068. This will also return to if point is on one of the two matches."
  16069. (interactive)
  16070. (let ((p (point)))
  16071. (save-excursion
  16072. (and (or (org-at-regexp-p start-re)
  16073. (re-search-backward start-re nil t))
  16074. (re-search-forward end-re nil t)
  16075. (>= (point) p)))))
  16076. (defun org-occur-in-agenda-files (regexp &optional nlines)
  16077. "Call `multi-occur' with buffers for all agenda files."
  16078. (interactive "sOrg-files matching: \np")
  16079. (let* ((files (org-agenda-files))
  16080. (tnames (mapcar 'file-truename files))
  16081. (extra org-agenda-text-search-extra-files)
  16082. f)
  16083. (when (eq (car extra) 'agenda-archives)
  16084. (setq extra (cdr extra))
  16085. (setq files (org-add-archive-files files)))
  16086. (while (setq f (pop extra))
  16087. (unless (member (file-truename f) tnames)
  16088. (add-to-list 'files f 'append)
  16089. (add-to-list 'tnames (file-truename f) 'append)))
  16090. (multi-occur
  16091. (mapcar (lambda (x)
  16092. (with-current-buffer
  16093. (or (get-file-buffer x) (find-file-noselect x))
  16094. (widen)
  16095. (current-buffer)))
  16096. files)
  16097. regexp)))
  16098. (if (boundp 'occur-mode-find-occurrence-hook)
  16099. ;; Emacs 23
  16100. (add-hook 'occur-mode-find-occurrence-hook
  16101. (lambda ()
  16102. (when (org-mode-p)
  16103. (org-reveal))))
  16104. ;; Emacs 22
  16105. (defadvice occur-mode-goto-occurrence
  16106. (after org-occur-reveal activate)
  16107. (and (org-mode-p) (org-reveal)))
  16108. (defadvice occur-mode-goto-occurrence-other-window
  16109. (after org-occur-reveal activate)
  16110. (and (org-mode-p) (org-reveal)))
  16111. (defadvice occur-mode-display-occurrence
  16112. (after org-occur-reveal activate)
  16113. (when (org-mode-p)
  16114. (let ((pos (occur-mode-find-occurrence)))
  16115. (with-current-buffer (marker-buffer pos)
  16116. (save-excursion
  16117. (goto-char pos)
  16118. (org-reveal)))))))
  16119. (defun org-occur-link-in-agenda-files ()
  16120. "Create a link and search for it in the agendas.
  16121. The link is not stored in `org-stored-links', it is just created
  16122. for the search purpose."
  16123. (interactive)
  16124. (let ((link (condition-case nil
  16125. (org-store-link nil)
  16126. (error "Unable to create a link to here"))))
  16127. (org-occur-in-agenda-files (regexp-quote link))))
  16128. (defun org-uniquify (list)
  16129. "Remove duplicate elements from LIST."
  16130. (let (res)
  16131. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  16132. res))
  16133. (defun org-delete-all (elts list)
  16134. "Remove all elements in ELTS from LIST."
  16135. (while elts
  16136. (setq list (delete (pop elts) list)))
  16137. list)
  16138. (defun org-remove-if (predicate seq)
  16139. "Remove everything from SEQ that fulfills PREDICATE."
  16140. (let (res e)
  16141. (while seq
  16142. (setq e (pop seq))
  16143. (if (not (funcall predicate e)) (push e res)))
  16144. (nreverse res)))
  16145. (defun org-remove-if-not (predicate seq)
  16146. "Remove everything from SEQ that does not fulfill PREDICATE."
  16147. (let (res e)
  16148. (while seq
  16149. (setq e (pop seq))
  16150. (if (funcall predicate e) (push e res)))
  16151. (nreverse res)))
  16152. (defun org-back-over-empty-lines ()
  16153. "Move backwards over whitespace, to the beginning of the first empty line.
  16154. Returns the number of empty lines passed."
  16155. (let ((pos (point)))
  16156. (skip-chars-backward " \t\n\r")
  16157. (beginning-of-line 2)
  16158. (goto-char (min (point) pos))
  16159. (count-lines (point) pos)))
  16160. (defun org-skip-whitespace ()
  16161. (skip-chars-forward " \t\n\r"))
  16162. (defun org-point-in-group (point group &optional context)
  16163. "Check if POINT is in match-group GROUP.
  16164. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16165. match. If the match group does ot exist or point is not inside it,
  16166. return nil."
  16167. (and (match-beginning group)
  16168. (>= point (match-beginning group))
  16169. (<= point (match-end group))
  16170. (if context
  16171. (list context (match-beginning group) (match-end group))
  16172. t)))
  16173. (defun org-switch-to-buffer-other-window (&rest args)
  16174. "Switch to buffer in a second window on the current frame.
  16175. In particular, do not allow pop-up frames."
  16176. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16177. special-display-function)
  16178. (apply 'switch-to-buffer-other-window args)))
  16179. (defun org-combine-plists (&rest plists)
  16180. "Create a single property list from all plists in PLISTS.
  16181. The process starts by copying the first list, and then setting properties
  16182. from the other lists. Settings in the last list are the most significant
  16183. ones and overrule settings in the other lists."
  16184. (let ((rtn (copy-sequence (pop plists)))
  16185. p v ls)
  16186. (while plists
  16187. (setq ls (pop plists))
  16188. (while ls
  16189. (setq p (pop ls) v (pop ls))
  16190. (setq rtn (plist-put rtn p v))))
  16191. rtn))
  16192. (defun org-move-line-down (arg)
  16193. "Move the current line down. With prefix argument, move it past ARG lines."
  16194. (interactive "p")
  16195. (let ((col (current-column))
  16196. beg end pos)
  16197. (beginning-of-line 1) (setq beg (point))
  16198. (beginning-of-line 2) (setq end (point))
  16199. (beginning-of-line (+ 1 arg))
  16200. (setq pos (move-marker (make-marker) (point)))
  16201. (insert (delete-and-extract-region beg end))
  16202. (goto-char pos)
  16203. (org-move-to-column col)))
  16204. (defun org-move-line-up (arg)
  16205. "Move the current line up. With prefix argument, move it past ARG lines."
  16206. (interactive "p")
  16207. (let ((col (current-column))
  16208. beg end pos)
  16209. (beginning-of-line 1) (setq beg (point))
  16210. (beginning-of-line 2) (setq end (point))
  16211. (beginning-of-line (- arg))
  16212. (setq pos (move-marker (make-marker) (point)))
  16213. (insert (delete-and-extract-region beg end))
  16214. (goto-char pos)
  16215. (org-move-to-column col)))
  16216. (defun org-replace-escapes (string table)
  16217. "Replace %-escapes in STRING with values in TABLE.
  16218. TABLE is an association list with keys like \"%a\" and string values.
  16219. The sequences in STRING may contain normal field width and padding information,
  16220. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16221. so values can contain further %-escapes if they are define later in TABLE."
  16222. (let ((tbl (copy-alist table))
  16223. (case-fold-search nil)
  16224. (pchg 0)
  16225. e re rpl)
  16226. (while (setq e (pop tbl))
  16227. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16228. (when (string-match re (cdr e))
  16229. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  16230. (safe "SREF"))
  16231. (add-text-properties 0 3 (list 'sref sref) safe)
  16232. (setcdr e (replace-match safe t t (cdr e)))))
  16233. (while (string-match re string)
  16234. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16235. (cdr e)))
  16236. (setq string (replace-match rpl t t string))))
  16237. (while (setq pchg (next-property-change pchg string))
  16238. (let ((sref (get-text-property pchg 'sref string)))
  16239. (when (and sref (string-match "SREF" string pchg))
  16240. (setq string (replace-match sref t t string)))))
  16241. string))
  16242. (defun org-sublist (list start end)
  16243. "Return a section of LIST, from START to END.
  16244. Counting starts at 1."
  16245. (let (rtn (c start))
  16246. (setq list (nthcdr (1- start) list))
  16247. (while (and list (<= c end))
  16248. (push (pop list) rtn)
  16249. (setq c (1+ c)))
  16250. (nreverse rtn)))
  16251. (defun org-find-base-buffer-visiting (file)
  16252. "Like `find-buffer-visiting' but always return the base buffer and
  16253. not an indirect buffer."
  16254. (let ((buf (or (get-file-buffer file)
  16255. (find-buffer-visiting file))))
  16256. (if buf
  16257. (or (buffer-base-buffer buf) buf)
  16258. nil)))
  16259. (defun org-image-file-name-regexp (&optional extensions)
  16260. "Return regexp matching the file names of images.
  16261. If EXTENSIONS is given, only match these."
  16262. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16263. (image-file-name-regexp)
  16264. (let ((image-file-name-extensions
  16265. (or extensions
  16266. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16267. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16268. (concat "\\."
  16269. (regexp-opt (nconc (mapcar 'upcase
  16270. image-file-name-extensions)
  16271. image-file-name-extensions)
  16272. t)
  16273. "\\'"))))
  16274. (defun org-file-image-p (file &optional extensions)
  16275. "Return non-nil if FILE is an image."
  16276. (save-match-data
  16277. (string-match (org-image-file-name-regexp extensions) file)))
  16278. (defun org-get-cursor-date ()
  16279. "Return the date at cursor in as a time.
  16280. This works in the calendar and in the agenda, anywhere else it just
  16281. returns the current time."
  16282. (let (date day defd)
  16283. (cond
  16284. ((eq major-mode 'calendar-mode)
  16285. (setq date (calendar-cursor-to-date)
  16286. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16287. ((eq major-mode 'org-agenda-mode)
  16288. (setq day (get-text-property (point) 'day))
  16289. (if day
  16290. (setq date (calendar-gregorian-from-absolute day)
  16291. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16292. (nth 2 date))))))
  16293. (or defd (current-time))))
  16294. (defvar org-agenda-action-marker (make-marker)
  16295. "Marker pointing to the entry for the next agenda action.")
  16296. (defun org-mark-entry-for-agenda-action ()
  16297. "Mark the current entry as target of an agenda action.
  16298. Agenda actions are actions executed from the agenda with the key `k',
  16299. which make use of the date at the cursor."
  16300. (interactive)
  16301. (move-marker org-agenda-action-marker
  16302. (save-excursion (org-back-to-heading t) (point))
  16303. (current-buffer))
  16304. (message
  16305. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16306. ;;; Paragraph filling stuff.
  16307. ;; We want this to be just right, so use the full arsenal.
  16308. (defun org-indent-line-function ()
  16309. "Indent line like previous, but further if previous was headline or item."
  16310. (interactive)
  16311. (let* ((pos (point))
  16312. (itemp (org-at-item-p))
  16313. (case-fold-search t)
  16314. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16315. column bpos bcol tpos tcol bullet btype bullet-type)
  16316. ;; Find the previous relevant line
  16317. (beginning-of-line 1)
  16318. (cond
  16319. ((looking-at "#") (setq column 0))
  16320. ((looking-at "\\*+ ") (setq column 0))
  16321. ((and (looking-at "[ \t]*:END:")
  16322. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16323. (save-excursion
  16324. (goto-char (1- (match-beginning 1)))
  16325. (setq column (current-column))))
  16326. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16327. (save-excursion
  16328. (re-search-backward
  16329. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16330. (setq column (org-get-indentation (match-string 0))))
  16331. (t
  16332. (beginning-of-line 0)
  16333. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16334. (not (looking-at "[ \t]*:END:"))
  16335. (not (looking-at org-drawer-regexp)))
  16336. (beginning-of-line 0))
  16337. (cond
  16338. ((looking-at "\\*+[ \t]+")
  16339. (if (not org-adapt-indentation)
  16340. (setq column 0)
  16341. (goto-char (match-end 0))
  16342. (setq column (current-column))))
  16343. ((looking-at org-drawer-regexp)
  16344. (goto-char (1- (match-beginning 1)))
  16345. (setq column (current-column)))
  16346. ((looking-at "\\([ \t]*\\):END:")
  16347. (goto-char (match-end 1))
  16348. (setq column (current-column)))
  16349. ((org-in-item-p)
  16350. (org-beginning-of-item)
  16351. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16352. (setq bpos (match-beginning 1) tpos (match-end 0)
  16353. bcol (progn (goto-char bpos) (current-column))
  16354. tcol (progn (goto-char tpos) (current-column))
  16355. bullet (match-string 1)
  16356. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16357. (if (> tcol (+ bcol org-description-max-indent))
  16358. (setq tcol (+ bcol 5)))
  16359. (if (not itemp)
  16360. (setq column tcol)
  16361. (goto-char pos)
  16362. (beginning-of-line 1)
  16363. (if (looking-at "\\S-")
  16364. (progn
  16365. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16366. (setq bullet (match-string 1)
  16367. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16368. (setq column (if (equal btype bullet-type) bcol tcol)))
  16369. (setq column (org-get-indentation)))))
  16370. (t (setq column (org-get-indentation))))))
  16371. (goto-char pos)
  16372. (if (<= (current-column) (current-indentation))
  16373. (org-indent-line-to column)
  16374. (save-excursion (org-indent-line-to column)))
  16375. (setq column (current-column))
  16376. (beginning-of-line 1)
  16377. (if (looking-at
  16378. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16379. (replace-match (concat (match-string 1)
  16380. (format org-property-format
  16381. (match-string 2) (match-string 3)))
  16382. t t))
  16383. (org-move-to-column column)))
  16384. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16385. "Variable to store copy of `adaptive-fill-regexp'.
  16386. Since `adaptive-fill-regexp' is set to never match, we need to
  16387. store a backup of its value before entering `org-mode' so that
  16388. the functionality can be provided as a fall-back.")
  16389. (defun org-set-autofill-regexps ()
  16390. (interactive)
  16391. ;; In the paragraph separator we include headlines, because filling
  16392. ;; text in a line directly attached to a headline would otherwise
  16393. ;; fill the headline as well.
  16394. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16395. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16396. ;; The paragraph starter includes hand-formatted lists.
  16397. (org-set-local
  16398. 'paragraph-start
  16399. (concat
  16400. "\f" "\\|"
  16401. "[ ]*$" "\\|"
  16402. "\\*+ " "\\|"
  16403. "[ \t]*#" "\\|"
  16404. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16405. "[ \t]*[:|]" "\\|"
  16406. "\\$\\$" "\\|"
  16407. "\\\\\\(begin\\|end\\|[][]\\)"))
  16408. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16409. ;; But only if the user has not turned off tables or fixed-width regions
  16410. (org-set-local
  16411. 'auto-fill-inhibit-regexp
  16412. (concat "\\*+ \\|#\\+"
  16413. "\\|[ \t]*" org-keyword-time-regexp
  16414. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16415. (concat
  16416. "\\|[ \t]*["
  16417. (if org-enable-table-editor "|" "")
  16418. (if org-enable-fixed-width-editor ":" "")
  16419. "]"))))
  16420. ;; We use our own fill-paragraph function, to make sure that tables
  16421. ;; and fixed-width regions are not wrapped. That function will pass
  16422. ;; through to `fill-paragraph' when appropriate.
  16423. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16424. ;; Adaptive filling: To get full control, first make sure that
  16425. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16426. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  16427. (org-set-local 'org-adaptive-fill-regexp-backup
  16428. adaptive-fill-regexp))
  16429. (org-set-local 'adaptive-fill-regexp "\000")
  16430. (org-set-local 'adaptive-fill-function
  16431. 'org-adaptive-fill-function)
  16432. (org-set-local
  16433. 'align-mode-rules-list
  16434. '((org-in-buffer-settings
  16435. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16436. (modes . '(org-mode))))))
  16437. (defun org-fill-paragraph (&optional justify)
  16438. "Re-align a table, pass through to fill-paragraph if no table."
  16439. (let ((table-p (org-at-table-p))
  16440. (table.el-p (org-at-table.el-p)))
  16441. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16442. (save-excursion (goto-char (point-at-bol))
  16443. (looking-at outline-regexp)))
  16444. t) ; skip headlines
  16445. (table.el-p t) ; skip table.el tables
  16446. (table-p (org-table-align) t) ; align org-mode tables
  16447. (t nil)))) ; call paragraph-fill
  16448. ;; For reference, this is the default value of adaptive-fill-regexp
  16449. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16450. (defun org-adaptive-fill-function ()
  16451. "Return a fill prefix for org-mode files.
  16452. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16453. work correctly."
  16454. (cond
  16455. ;; Comment line
  16456. ((looking-at "#[ \t]+")
  16457. (match-string-no-properties 0))
  16458. ;; Description list
  16459. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16460. (save-excursion
  16461. (if (> (match-end 1) (+ (match-beginning 1)
  16462. org-description-max-indent))
  16463. (goto-char (+ (match-beginning 1) 5))
  16464. (goto-char (match-end 0)))
  16465. (make-string (current-column) ?\ )))
  16466. ;; Ordered or unordered list
  16467. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16468. (save-excursion
  16469. (goto-char (match-end 0))
  16470. (make-string (current-column) ?\ )))
  16471. ;; Other text
  16472. ((looking-at org-adaptive-fill-regexp-backup)
  16473. (match-string-no-properties 0))))
  16474. ;;; Other stuff.
  16475. (defun org-toggle-fixed-width-section (arg)
  16476. "Toggle the fixed-width export.
  16477. If there is no active region, the QUOTE keyword at the current headline is
  16478. inserted or removed. When present, it causes the text between this headline
  16479. and the next to be exported as fixed-width text, and unmodified.
  16480. If there is an active region, this command adds or removes a colon as the
  16481. first character of this line. If the first character of a line is a colon,
  16482. this line is also exported in fixed-width font."
  16483. (interactive "P")
  16484. (let* ((cc 0)
  16485. (regionp (org-region-active-p))
  16486. (beg (if regionp (region-beginning) (point)))
  16487. (end (if regionp (region-end)))
  16488. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16489. (case-fold-search nil)
  16490. (re "[ \t]*\\(: \\)")
  16491. off)
  16492. (if regionp
  16493. (save-excursion
  16494. (goto-char beg)
  16495. (setq cc (current-column))
  16496. (beginning-of-line 1)
  16497. (setq off (looking-at re))
  16498. (while (> nlines 0)
  16499. (setq nlines (1- nlines))
  16500. (beginning-of-line 1)
  16501. (cond
  16502. (arg
  16503. (org-move-to-column cc t)
  16504. (insert ": \n")
  16505. (forward-line -1))
  16506. ((and off (looking-at re))
  16507. (replace-match "" t t nil 1))
  16508. ((not off) (org-move-to-column cc t) (insert ": ")))
  16509. (forward-line 1)))
  16510. (save-excursion
  16511. (org-back-to-heading)
  16512. (if (looking-at (concat outline-regexp
  16513. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16514. (replace-match "" t t nil 1)
  16515. (if (looking-at outline-regexp)
  16516. (progn
  16517. (goto-char (match-end 0))
  16518. (insert org-quote-string " "))))))))
  16519. (defun org-reftex-citation ()
  16520. "Use reftex-citation to insert a citation into the buffer.
  16521. This looks for a line like
  16522. #+BIBLIOGRAPHY: foo plain option:-d
  16523. and derives from it that foo.bib is the bibliography file relevant
  16524. for this document. It then installs the necessary environment for RefTeX
  16525. to work in this buffer and calls `reftex-citation' to insert a citation
  16526. into the buffer.
  16527. Export of such citations to both LaTeX and HTML is handled by the contributed
  16528. package org-exp-bibtex by Taru Karttunen."
  16529. (interactive)
  16530. (let ((reftex-docstruct-symbol 'rds)
  16531. (reftex-cite-format "\\cite{%l}")
  16532. rds bib)
  16533. (save-excursion
  16534. (save-restriction
  16535. (widen)
  16536. (let ((case-fold-search t)
  16537. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16538. (if (not (save-excursion
  16539. (or (re-search-forward re nil t)
  16540. (re-search-backward re nil t))))
  16541. (error "No bibliography defined in file")
  16542. (setq bib (concat (match-string 1) ".bib")
  16543. rds (list (list 'bib bib)))))))
  16544. (call-interactively 'reftex-citation)))
  16545. ;;;; Functions extending outline functionality
  16546. (defun org-beginning-of-line (&optional arg)
  16547. "Go to the beginning of the current line. If that is invisible, continue
  16548. to a visible line beginning. This makes the function of C-a more intuitive.
  16549. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16550. first attempt, and only move to after the tags when the cursor is already
  16551. beyond the end of the headline."
  16552. (interactive "P")
  16553. (let ((pos (point))
  16554. (special (if (consp org-special-ctrl-a/e)
  16555. (car org-special-ctrl-a/e)
  16556. org-special-ctrl-a/e))
  16557. refpos)
  16558. (if (org-bound-and-true-p line-move-visual)
  16559. (beginning-of-visual-line 1)
  16560. (beginning-of-line 1))
  16561. (if (and arg (fboundp 'move-beginning-of-line))
  16562. (call-interactively 'move-beginning-of-line)
  16563. (if (bobp)
  16564. nil
  16565. (backward-char 1)
  16566. (if (org-invisible-p)
  16567. (while (and (not (bobp)) (org-invisible-p))
  16568. (backward-char 1)
  16569. (beginning-of-line 1))
  16570. (forward-char 1))))
  16571. (when special
  16572. (cond
  16573. ((and (looking-at org-complex-heading-regexp)
  16574. (= (char-after (match-end 1)) ?\ ))
  16575. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16576. (point-at-eol)))
  16577. (goto-char
  16578. (if (eq special t)
  16579. (cond ((> pos refpos) refpos)
  16580. ((= pos (point)) refpos)
  16581. (t (point)))
  16582. (cond ((> pos (point)) (point))
  16583. ((not (eq last-command this-command)) (point))
  16584. (t refpos)))))
  16585. ((org-at-item-p)
  16586. (goto-char
  16587. (if (eq special t)
  16588. (cond ((> pos (match-end 4)) (match-end 4))
  16589. ((= pos (point)) (match-end 4))
  16590. (t (point)))
  16591. (cond ((> pos (point)) (point))
  16592. ((not (eq last-command this-command)) (point))
  16593. (t (match-end 4))))))))
  16594. (org-no-warnings
  16595. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16596. (defun org-end-of-line (&optional arg)
  16597. "Go to the end of the line.
  16598. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16599. first attempt, and only move to after the tags when the cursor is already
  16600. beyond the end of the headline."
  16601. (interactive "P")
  16602. (let ((special (if (consp org-special-ctrl-a/e)
  16603. (cdr org-special-ctrl-a/e)
  16604. org-special-ctrl-a/e)))
  16605. (if (or (not special)
  16606. (not (org-on-heading-p))
  16607. arg)
  16608. (call-interactively
  16609. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16610. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16611. (t 'end-of-line)))
  16612. (let ((pos (point)))
  16613. (beginning-of-line 1)
  16614. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16615. (if (eq special t)
  16616. (if (or (< pos (match-beginning 1))
  16617. (= pos (match-end 0)))
  16618. (goto-char (match-beginning 1))
  16619. (goto-char (match-end 0)))
  16620. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16621. (goto-char (match-end 0))
  16622. (goto-char (match-beginning 1))))
  16623. (call-interactively (if (fboundp 'move-end-of-line)
  16624. 'move-end-of-line
  16625. 'end-of-line)))))
  16626. (org-no-warnings
  16627. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16628. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16629. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16630. (define-key org-mode-map [home] 'org-beginning-of-line)
  16631. (define-key org-mode-map [end] 'org-end-of-line)
  16632. (defun org-backward-sentence (&optional arg)
  16633. "Go to beginning of sentence, or beginning of table field.
  16634. This will call `backward-sentence' or `org-table-beginning-of-field',
  16635. depending on context."
  16636. (interactive "P")
  16637. (cond
  16638. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16639. (t (call-interactively 'backward-sentence))))
  16640. (defun org-forward-sentence (&optional arg)
  16641. "Go to end of sentence, or end of table field.
  16642. This will call `forward-sentence' or `org-table-end-of-field',
  16643. depending on context."
  16644. (interactive "P")
  16645. (cond
  16646. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16647. (t (call-interactively 'forward-sentence))))
  16648. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16649. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16650. (defun org-kill-line (&optional arg)
  16651. "Kill line, to tags or end of line."
  16652. (interactive "P")
  16653. (cond
  16654. ((or (not org-special-ctrl-k)
  16655. (bolp)
  16656. (not (org-on-heading-p)))
  16657. (call-interactively 'kill-line))
  16658. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16659. (kill-region (point) (match-beginning 1))
  16660. (org-set-tags nil t))
  16661. (t (kill-region (point) (point-at-eol)))))
  16662. (define-key org-mode-map "\C-k" 'org-kill-line)
  16663. (defun org-yank (&optional arg)
  16664. "Yank. If the kill is a subtree, treat it specially.
  16665. This command will look at the current kill and check if is a single
  16666. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16667. cursor is at the beginning of a line or after the stars of a currently
  16668. empty headline, then the yank is handled specially. How exactly depends
  16669. on the value of the following variables, both set by default.
  16670. org-yank-folded-subtrees
  16671. When set, the subtree(s) will be folded after insertion, but only
  16672. if doing so would now swallow text after the yanked text.
  16673. org-yank-adjusted-subtrees
  16674. When set, the subtree will be promoted or demoted in order to
  16675. fit into the local outline tree structure, which means that the level
  16676. will be adjusted so that it becomes the smaller one of the two
  16677. *visible* surrounding headings.
  16678. Any prefix to this command will cause `yank' to be called directly with
  16679. no special treatment. In particular, a simple `C-u' prefix will just
  16680. plainly yank the text as it is.
  16681. \[1] The test checks if the first non-white line is a heading
  16682. and if there are no other headings with fewer stars."
  16683. (interactive "P")
  16684. (org-yank-generic 'yank arg))
  16685. (defun org-yank-generic (command arg)
  16686. "Perform some yank-like command.
  16687. This function implements the behavior described in the `org-yank'
  16688. documentation. However, it has been generalized to work for any
  16689. interactive command with similar behavior."
  16690. ;; pretend to be command COMMAND
  16691. (setq this-command command)
  16692. (if arg
  16693. (call-interactively command)
  16694. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16695. (and (org-kill-is-subtree-p)
  16696. (or (bolp)
  16697. (and (looking-at "[ \t]*$")
  16698. (string-match
  16699. "\\`\\*+\\'"
  16700. (buffer-substring (point-at-bol) (point)))))))
  16701. swallowp)
  16702. (cond
  16703. ((and subtreep org-yank-folded-subtrees)
  16704. (let ((beg (point))
  16705. end)
  16706. (if (and subtreep org-yank-adjusted-subtrees)
  16707. (org-paste-subtree nil nil 'for-yank)
  16708. (call-interactively command))
  16709. (setq end (point))
  16710. (goto-char beg)
  16711. (when (and (bolp) subtreep
  16712. (not (setq swallowp
  16713. (org-yank-folding-would-swallow-text beg end))))
  16714. (or (looking-at outline-regexp)
  16715. (re-search-forward (concat "^" outline-regexp) end t))
  16716. (while (and (< (point) end) (looking-at outline-regexp))
  16717. (hide-subtree)
  16718. (org-cycle-show-empty-lines 'folded)
  16719. (condition-case nil
  16720. (outline-forward-same-level 1)
  16721. (error (goto-char end)))))
  16722. (when swallowp
  16723. (message
  16724. "Inserted text not folded because that would swallow text"))
  16725. (goto-char end)
  16726. (skip-chars-forward " \t\n\r")
  16727. (beginning-of-line 1)
  16728. (push-mark beg 'nomsg)))
  16729. ((and subtreep org-yank-adjusted-subtrees)
  16730. (let ((beg (point-at-bol)))
  16731. (org-paste-subtree nil nil 'for-yank)
  16732. (push-mark beg 'nomsg)))
  16733. (t
  16734. (call-interactively command))))))
  16735. (defun org-yank-folding-would-swallow-text (beg end)
  16736. "Would hide-subtree at BEG swallow any text after END?"
  16737. (let (level)
  16738. (save-excursion
  16739. (goto-char beg)
  16740. (when (or (looking-at outline-regexp)
  16741. (re-search-forward (concat "^" outline-regexp) end t))
  16742. (setq level (org-outline-level)))
  16743. (goto-char end)
  16744. (skip-chars-forward " \t\r\n\v\f")
  16745. (if (or (eobp)
  16746. (and (bolp) (looking-at org-outline-regexp)
  16747. (<= (org-outline-level) level)))
  16748. nil ; Nothing would be swallowed
  16749. t)))) ; something would swallow
  16750. (define-key org-mode-map "\C-y" 'org-yank)
  16751. (defun org-invisible-p ()
  16752. "Check if point is at a character currently not visible."
  16753. ;; Early versions of noutline don't have `outline-invisible-p'.
  16754. (if (fboundp 'outline-invisible-p)
  16755. (outline-invisible-p)
  16756. (get-char-property (point) 'invisible)))
  16757. (defun org-invisible-p2 ()
  16758. "Check if point is at a character currently not visible."
  16759. (save-excursion
  16760. (if (and (eolp) (not (bobp))) (backward-char 1))
  16761. ;; Early versions of noutline don't have `outline-invisible-p'.
  16762. (if (fboundp 'outline-invisible-p)
  16763. (outline-invisible-p)
  16764. (get-char-property (point) 'invisible))))
  16765. (defun org-back-to-heading (&optional invisible-ok)
  16766. "Call `outline-back-to-heading', but provide a better error message."
  16767. (condition-case nil
  16768. (outline-back-to-heading invisible-ok)
  16769. (error (error "Before first headline at position %d in buffer %s"
  16770. (point) (current-buffer)))))
  16771. (defun org-before-first-heading-p ()
  16772. "Before first heading?"
  16773. (save-excursion
  16774. (null (re-search-backward "^\\*+ " nil t))))
  16775. (defun org-on-heading-p (&optional ignored)
  16776. (outline-on-heading-p t))
  16777. (defun org-at-heading-p (&optional ignored)
  16778. (outline-on-heading-p t))
  16779. (defun org-point-at-end-of-empty-headline ()
  16780. "If point is at the end of an empty headline, return t, else nil.
  16781. If the heading only contains a TODO keyword, it is still still considered
  16782. empty."
  16783. (and (looking-at "[ \t]*$")
  16784. (save-excursion
  16785. (beginning-of-line 1)
  16786. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16787. "\\)?[ \t]*$")))))
  16788. (defun org-at-heading-or-item-p ()
  16789. (or (org-on-heading-p) (org-at-item-p)))
  16790. (defun org-on-target-p ()
  16791. (or (org-in-regexp org-radio-target-regexp)
  16792. (org-in-regexp org-target-regexp)))
  16793. (defun org-up-heading-all (arg)
  16794. "Move to the heading line of which the present line is a subheading.
  16795. This function considers both visible and invisible heading lines.
  16796. With argument, move up ARG levels."
  16797. (if (fboundp 'outline-up-heading-all)
  16798. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16799. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16800. (defun org-up-heading-safe ()
  16801. "Move to the heading line of which the present line is a subheading.
  16802. This version will not throw an error. It will return the level of the
  16803. headline found, or nil if no higher level is found.
  16804. Also, this function will be a lot faster than `outline-up-heading',
  16805. because it relies on stars being the outline starters. This can really
  16806. make a significant difference in outlines with very many siblings."
  16807. (let (start-level re)
  16808. (org-back-to-heading t)
  16809. (setq start-level (funcall outline-level))
  16810. (if (equal start-level 1)
  16811. nil
  16812. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16813. (if (re-search-backward re nil t)
  16814. (funcall outline-level)))))
  16815. (defun org-first-sibling-p ()
  16816. "Is this heading the first child of its parents?"
  16817. (interactive)
  16818. (let ((re (concat "^" outline-regexp))
  16819. level l)
  16820. (unless (org-at-heading-p t)
  16821. (error "Not at a heading"))
  16822. (setq level (funcall outline-level))
  16823. (save-excursion
  16824. (if (not (re-search-backward re nil t))
  16825. t
  16826. (setq l (funcall outline-level))
  16827. (< l level)))))
  16828. (defun org-goto-sibling (&optional previous)
  16829. "Goto the next sibling, even if it is invisible.
  16830. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16831. when a sibling was found. When none is found, return nil and don't
  16832. move point."
  16833. (let ((fun (if previous 're-search-backward 're-search-forward))
  16834. (pos (point))
  16835. (re (concat "^" outline-regexp))
  16836. level l)
  16837. (when (condition-case nil (org-back-to-heading t) (error nil))
  16838. (setq level (funcall outline-level))
  16839. (catch 'exit
  16840. (or previous (forward-char 1))
  16841. (while (funcall fun re nil t)
  16842. (setq l (funcall outline-level))
  16843. (when (< l level) (goto-char pos) (throw 'exit nil))
  16844. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16845. (goto-char pos)
  16846. nil))))
  16847. (defun org-show-siblings ()
  16848. "Show all siblings of the current headline."
  16849. (save-excursion
  16850. (while (org-goto-sibling) (org-flag-heading nil)))
  16851. (save-excursion
  16852. (while (org-goto-sibling 'previous)
  16853. (org-flag-heading nil))))
  16854. (defun org-show-hidden-entry ()
  16855. "Show an entry where even the heading is hidden."
  16856. (save-excursion
  16857. (org-show-entry)))
  16858. (defun org-flag-heading (flag &optional entry)
  16859. "Flag the current heading. FLAG non-nil means make invisible.
  16860. When ENTRY is non-nil, show the entire entry."
  16861. (save-excursion
  16862. (org-back-to-heading t)
  16863. ;; Check if we should show the entire entry
  16864. (if entry
  16865. (progn
  16866. (org-show-entry)
  16867. (save-excursion
  16868. (and (outline-next-heading)
  16869. (org-flag-heading nil))))
  16870. (outline-flag-region (max (point-min) (1- (point)))
  16871. (save-excursion (outline-end-of-heading) (point))
  16872. flag))))
  16873. (defun org-get-next-sibling ()
  16874. "Move to next heading of the same level, and return point.
  16875. If there is no such heading, return nil.
  16876. This is like outline-next-sibling, but invisible headings are ok."
  16877. (let ((level (funcall outline-level)))
  16878. (outline-next-heading)
  16879. (while (and (not (eobp)) (> (funcall outline-level) level))
  16880. (outline-next-heading))
  16881. (if (or (eobp) (< (funcall outline-level) level))
  16882. nil
  16883. (point))))
  16884. (defun org-get-last-sibling ()
  16885. "Move to previous heading of the same level, and return point.
  16886. If there is no such heading, return nil."
  16887. (let ((opoint (point))
  16888. (level (funcall outline-level)))
  16889. (outline-previous-heading)
  16890. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16891. (while (and (> (funcall outline-level) level)
  16892. (not (bobp)))
  16893. (outline-previous-heading))
  16894. (if (< (funcall outline-level) level)
  16895. nil
  16896. (point)))))
  16897. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16898. ;; This contains an exact copy of the original function, but it uses
  16899. ;; `org-back-to-heading', to make it work also in invisible
  16900. ;; trees. And is uses an invisible-OK argument.
  16901. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16902. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16903. ;; with many children and grandchildren etc, this can be much faster
  16904. ;; than the outline version.
  16905. (org-back-to-heading invisible-OK)
  16906. (let ((first t)
  16907. (level (funcall outline-level)))
  16908. (if (and (org-mode-p) (< level 1000))
  16909. ;; A true heading (not a plain list item), in Org-mode
  16910. ;; This means we can easily find the end by looking
  16911. ;; only for the right number of stars. Using a regexp to do
  16912. ;; this is so much faster than using a Lisp loop.
  16913. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16914. (forward-char 1)
  16915. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16916. ;; something else, do it the slow way
  16917. (while (and (not (eobp))
  16918. (or first (> (funcall outline-level) level)))
  16919. (setq first nil)
  16920. (outline-next-heading)))
  16921. (unless to-heading
  16922. (if (memq (preceding-char) '(?\n ?\^M))
  16923. (progn
  16924. ;; Go to end of line before heading
  16925. (forward-char -1)
  16926. (if (memq (preceding-char) '(?\n ?\^M))
  16927. ;; leave blank line before heading
  16928. (forward-char -1))))))
  16929. (point))
  16930. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16931. "Use Org version in org-mode, for dramatic speed-up."
  16932. (if (eq major-mode 'org-mode)
  16933. (progn
  16934. (org-end-of-subtree nil t)
  16935. (unless (eobp) (backward-char 1)))
  16936. ad-do-it))
  16937. (defun org-forward-same-level (arg &optional invisible-ok)
  16938. "Move forward to the arg'th subheading at same level as this one.
  16939. Stop at the first and last subheadings of a superior heading."
  16940. (interactive "p")
  16941. (org-back-to-heading invisible-ok)
  16942. (org-on-heading-p)
  16943. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16944. (re (format "^\\*\\{1,%d\\} " level))
  16945. l)
  16946. (forward-char 1)
  16947. (while (> arg 0)
  16948. (while (and (re-search-forward re nil 'move)
  16949. (setq l (- (match-end 0) (match-beginning 0) 1))
  16950. (= l level)
  16951. (not invisible-ok)
  16952. (progn (backward-char 1) (org-invisible-p)))
  16953. (if (< l level) (setq arg 1)))
  16954. (setq arg (1- arg)))
  16955. (beginning-of-line 1)))
  16956. (defun org-backward-same-level (arg &optional invisible-ok)
  16957. "Move backward to the arg'th subheading at same level as this one.
  16958. Stop at the first and last subheadings of a superior heading."
  16959. (interactive "p")
  16960. (org-back-to-heading)
  16961. (org-on-heading-p)
  16962. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16963. (re (format "^\\*\\{1,%d\\} " level))
  16964. l)
  16965. (while (> arg 0)
  16966. (while (and (re-search-backward re nil 'move)
  16967. (setq l (- (match-end 0) (match-beginning 0) 1))
  16968. (= l level)
  16969. (not invisible-ok)
  16970. (org-invisible-p))
  16971. (if (< l level) (setq arg 1)))
  16972. (setq arg (1- arg)))))
  16973. (defun org-show-subtree ()
  16974. "Show everything after this heading at deeper levels."
  16975. (outline-flag-region
  16976. (point)
  16977. (save-excursion
  16978. (org-end-of-subtree t t))
  16979. nil))
  16980. (defun org-show-entry ()
  16981. "Show the body directly following this heading.
  16982. Show the heading too, if it is currently invisible."
  16983. (interactive)
  16984. (save-excursion
  16985. (condition-case nil
  16986. (progn
  16987. (org-back-to-heading t)
  16988. (outline-flag-region
  16989. (max (point-min) (1- (point)))
  16990. (save-excursion
  16991. (if (re-search-forward
  16992. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16993. (match-beginning 1)
  16994. (point-max)))
  16995. nil)
  16996. (org-cycle-hide-drawers 'children))
  16997. (error nil))))
  16998. (defun org-make-options-regexp (kwds &optional extra)
  16999. "Make a regular expression for keyword lines."
  17000. (concat
  17001. "^"
  17002. "#?[ \t]*\\+\\("
  17003. (mapconcat 'regexp-quote kwds "\\|")
  17004. (if extra (concat "\\|" extra))
  17005. "\\):[ \t]*"
  17006. "\\(.*\\)"))
  17007. ;; Make isearch reveal the necessary context
  17008. (defun org-isearch-end ()
  17009. "Reveal context after isearch exits."
  17010. (when isearch-success ; only if search was successful
  17011. (if (featurep 'xemacs)
  17012. ;; Under XEmacs, the hook is run in the correct place,
  17013. ;; we directly show the context.
  17014. (org-show-context 'isearch)
  17015. ;; In Emacs the hook runs *before* restoring the overlays.
  17016. ;; So we have to use a one-time post-command-hook to do this.
  17017. ;; (Emacs 22 has a special variable, see function `org-mode')
  17018. (unless (and (boundp 'isearch-mode-end-hook-quit)
  17019. isearch-mode-end-hook-quit)
  17020. ;; Only when the isearch was not quitted.
  17021. (org-add-hook 'post-command-hook 'org-isearch-post-command
  17022. 'append 'local)))))
  17023. (defun org-isearch-post-command ()
  17024. "Remove self from hook, and show context."
  17025. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  17026. (org-show-context 'isearch))
  17027. ;;;; Integration with and fixes for other packages
  17028. ;;; Imenu support
  17029. (defvar org-imenu-markers nil
  17030. "All markers currently used by Imenu.")
  17031. (make-variable-buffer-local 'org-imenu-markers)
  17032. (defun org-imenu-new-marker (&optional pos)
  17033. "Return a new marker for use by Imenu, and remember the marker."
  17034. (let ((m (make-marker)))
  17035. (move-marker m (or pos (point)))
  17036. (push m org-imenu-markers)
  17037. m))
  17038. (defun org-imenu-get-tree ()
  17039. "Produce the index for Imenu."
  17040. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  17041. (setq org-imenu-markers nil)
  17042. (let* ((n org-imenu-depth)
  17043. (re (concat "^" outline-regexp))
  17044. (subs (make-vector (1+ n) nil))
  17045. (last-level 0)
  17046. m level head)
  17047. (save-excursion
  17048. (save-restriction
  17049. (widen)
  17050. (goto-char (point-max))
  17051. (while (re-search-backward re nil t)
  17052. (setq level (org-reduced-level (funcall outline-level)))
  17053. (when (<= level n)
  17054. (looking-at org-complex-heading-regexp)
  17055. (setq head (org-link-display-format
  17056. (org-match-string-no-properties 4))
  17057. m (org-imenu-new-marker))
  17058. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  17059. (if (>= level last-level)
  17060. (push (cons head m) (aref subs level))
  17061. (push (cons head (aref subs (1+ level))) (aref subs level))
  17062. (loop for i from (1+ level) to n do (aset subs i nil)))
  17063. (setq last-level level)))))
  17064. (aref subs 1)))
  17065. (eval-after-load "imenu"
  17066. '(progn
  17067. (add-hook 'imenu-after-jump-hook
  17068. (lambda ()
  17069. (if (eq major-mode 'org-mode)
  17070. (org-show-context 'org-goto))))))
  17071. (defun org-link-display-format (link)
  17072. "Replace a link with either the description, or the link target
  17073. if no description is present"
  17074. (save-match-data
  17075. (if (string-match org-bracket-link-analytic-regexp link)
  17076. (replace-match (if (match-end 5)
  17077. (match-string 5 link)
  17078. (concat (match-string 1 link)
  17079. (match-string 3 link)))
  17080. nil t link)
  17081. link)))
  17082. ;; Speedbar support
  17083. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  17084. "Overlay marking the agenda restriction line in speedbar.")
  17085. (overlay-put org-speedbar-restriction-lock-overlay
  17086. 'face 'org-agenda-restriction-lock)
  17087. (overlay-put org-speedbar-restriction-lock-overlay
  17088. 'help-echo "Agendas are currently limited to this item.")
  17089. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  17090. (defun org-speedbar-set-agenda-restriction ()
  17091. "Restrict future agenda commands to the location at point in speedbar.
  17092. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  17093. (interactive)
  17094. (require 'org-agenda)
  17095. (let (p m tp np dir txt)
  17096. (cond
  17097. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17098. 'org-imenu t))
  17099. (setq m (get-text-property p 'org-imenu-marker))
  17100. (with-current-buffer (marker-buffer m)
  17101. (goto-char m)
  17102. (org-agenda-set-restriction-lock 'subtree)))
  17103. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  17104. 'speedbar-function 'speedbar-find-file))
  17105. (setq tp (previous-single-property-change
  17106. (1+ p) 'speedbar-function)
  17107. np (next-single-property-change
  17108. tp 'speedbar-function)
  17109. dir (speedbar-line-directory)
  17110. txt (buffer-substring-no-properties (or tp (point-min))
  17111. (or np (point-max))))
  17112. (with-current-buffer (find-file-noselect
  17113. (let ((default-directory dir))
  17114. (expand-file-name txt)))
  17115. (unless (org-mode-p)
  17116. (error "Cannot restrict to non-Org-mode file"))
  17117. (org-agenda-set-restriction-lock 'file)))
  17118. (t (error "Don't know how to restrict Org-mode's agenda")))
  17119. (move-overlay org-speedbar-restriction-lock-overlay
  17120. (point-at-bol) (point-at-eol))
  17121. (setq current-prefix-arg nil)
  17122. (org-agenda-maybe-redo)))
  17123. (eval-after-load "speedbar"
  17124. '(progn
  17125. (speedbar-add-supported-extension ".org")
  17126. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  17127. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  17128. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  17129. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17130. (add-hook 'speedbar-visiting-tag-hook
  17131. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  17132. ;;; Fixes and Hacks for problems with other packages
  17133. ;; Make flyspell not check words in links, to not mess up our keymap
  17134. (defun org-mode-flyspell-verify ()
  17135. "Don't let flyspell put overlays at active buttons."
  17136. (and (not (get-text-property (point) 'keymap))
  17137. (not (get-text-property (point) 'org-no-flyspell))))
  17138. (defun org-remove-flyspell-overlays-in (beg end)
  17139. "Remove flyspell overlays in region."
  17140. (and (org-bound-and-true-p flyspell-mode)
  17141. (fboundp 'flyspell-delete-region-overlays)
  17142. (flyspell-delete-region-overlays beg end))
  17143. (add-text-properties beg end '(org-no-flyspell t)))
  17144. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  17145. (eval-after-load "bookmark"
  17146. '(if (boundp 'bookmark-after-jump-hook)
  17147. ;; We can use the hook
  17148. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  17149. ;; Hook not available, use advice
  17150. (defadvice bookmark-jump (after org-make-visible activate)
  17151. "Make the position visible."
  17152. (org-bookmark-jump-unhide))))
  17153. ;; Make sure saveplace shows the location if it was hidden
  17154. (eval-after-load "saveplace"
  17155. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  17156. "Make the position visible."
  17157. (org-bookmark-jump-unhide)))
  17158. ;; Make sure ecb shows the location if it was hidden
  17159. (eval-after-load "ecb"
  17160. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17161. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17162. (if (eq major-mode 'org-mode)
  17163. (org-show-context))))
  17164. (defun org-bookmark-jump-unhide ()
  17165. "Unhide the current position, to show the bookmark location."
  17166. (and (org-mode-p)
  17167. (or (org-invisible-p)
  17168. (save-excursion (goto-char (max (point-min) (1- (point))))
  17169. (org-invisible-p)))
  17170. (org-show-context 'bookmark-jump)))
  17171. ;; Make session.el ignore our circular variable
  17172. (eval-after-load "session"
  17173. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17174. ;;;; Experimental code
  17175. (defun org-closed-in-range ()
  17176. "Sparse tree of items closed in a certain time range.
  17177. Still experimental, may disappear in the future."
  17178. (interactive)
  17179. ;; Get the time interval from the user.
  17180. (let* ((time1 (org-float-time
  17181. (org-read-date nil 'to-time nil "Starting date: ")))
  17182. (time2 (org-float-time
  17183. (org-read-date nil 'to-time nil "End date:")))
  17184. ;; callback function
  17185. (callback (lambda ()
  17186. (let ((time
  17187. (org-float-time
  17188. (apply 'encode-time
  17189. (org-parse-time-string
  17190. (match-string 1))))))
  17191. ;; check if time in interval
  17192. (and (>= time time1) (<= time time2))))))
  17193. ;; make tree, check each match with the callback
  17194. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17195. ;;;; Finish up
  17196. (provide 'org)
  17197. (run-hooks 'org-load-hook)
  17198. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17199. ;;; org.el ends here