org.el 693 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596
  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.35trans
  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. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'overlay)
  81. (require 'org-macs)
  82. (require 'org-entities)
  83. (require 'org-compat)
  84. (require 'org-faces)
  85. (require 'org-list)
  86. (require 'org-src)
  87. (require 'org-footnote)
  88. ;;;; Customization variables
  89. ;;; Version
  90. (defconst org-version "6.35trans"
  91. "The version number of the file org.el.")
  92. (defun org-version (&optional here)
  93. "Show the org-mode version in the echo area.
  94. With prefix arg HERE, insert it at point."
  95. (interactive "P")
  96. (let* ((origin default-directory)
  97. (version org-version)
  98. (git-version)
  99. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  100. (when (and (file-exists-p (expand-file-name ".git" dir))
  101. (executable-find "git"))
  102. (unwind-protect
  103. (progn
  104. (cd dir)
  105. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  106. (with-current-buffer "*Shell Command Output*"
  107. (goto-char (point-min))
  108. (setq git-version (buffer-substring (point) (point-at-eol))))
  109. (subst-char-in-string ?- ?. git-version t)
  110. (when (string-match "\\S-"
  111. (shell-command-to-string
  112. "git diff-index --name-only HEAD --"))
  113. (setq git-version (concat git-version ".dirty")))
  114. (setq version (concat version " (" git-version ")"))))
  115. (cd origin)))
  116. (setq version (format "Org-mode version %s" version))
  117. (if here (insert version))
  118. (message version)))
  119. ;;; Compatibility constants
  120. ;;; The custom variables
  121. (defgroup org nil
  122. "Outline-based notes management and organizer."
  123. :tag "Org"
  124. :group 'outlines
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (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)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  171. (const :tag " docview: Links to doc-view buffers" org-docview)
  172. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  173. (const :tag " id: Global IDs for identifying entries" org-id)
  174. (const :tag " info: Links to Info nodes" org-info)
  175. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  176. (const :tag " habit: Track your consistency with habits" org-habit)
  177. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  178. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  179. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  180. (const :tag " mew Links to Mew folders/messages" org-mew)
  181. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  182. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  183. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  184. (const :tag " vm: Links to VM folders/messages" org-vm)
  185. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  186. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  187. (const :tag " mouse: Additional mouse support" org-mouse)
  188. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  189. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  190. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  191. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  192. (const :tag "C collector: Collect properties into tables" org-collector)
  193. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  194. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  195. (const :tag "C eval: Include command output as text" org-eval)
  196. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  197. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  198. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  199. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  200. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  201. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  202. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  203. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  204. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  205. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  206. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  207. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  208. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  209. (const :tag "C registry: A registry for Org-mode links" org-registry)
  210. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  211. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  212. (const :tag "C secretary: Team management with org-mode" org-secretary)
  213. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  214. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  215. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  216. (const :tag "C track: Keep up with Org-mode development" org-track)
  217. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  218. (defcustom org-support-shift-select nil
  219. "Non-nil means make shift-cursor commands select text when possible.
  220. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  221. selecting a region, or enlarge thusly regions started in this way.
  222. In Org-mode, in special contexts, these same keys are used for other
  223. purposes, important enough to compete with shift selection. Org tries
  224. to balance these needs by supporting `shift-select-mode' outside these
  225. special contexts, under control of this variable.
  226. The default of this variable is nil, to avoid confusing behavior. Shifted
  227. cursor keys will then execute Org commands in the following contexts:
  228. - on a headline, changing TODO state (left/right) and priority (up/down)
  229. - on a time stamp, changing the time
  230. - in a plain list item, changing the bullet type
  231. - in a property definition line, switching between allowed values
  232. - in the BEGIN line of a clock table (changing the time block).
  233. Outside these contexts, the commands will throw an error.
  234. When this variable is t and the cursor is not in a special context,
  235. Org-mode will support shift-selection for making and enlarging regions.
  236. To make this more effective, the bullet cycling will no longer happen
  237. anywhere in an item line, but only if the cursor is exactly on the bullet.
  238. If you set this variable to the symbol `always', then the keys
  239. will not be special in headlines, property lines, and item lines, to make
  240. shift selection work there as well. If this is what you want, you can
  241. use the following alternative commands: `C-c C-t' and `C-c ,' to
  242. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  243. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  244. edited by hand or in column view.
  245. However, when the cursor is on a timestamp, shift-cursor commands
  246. will still edit the time stamp - this is just too good to give up.
  247. XEmacs user should have this variable set to nil, because shift-select-mode
  248. is Emacs 23 only."
  249. :group 'org
  250. :type '(choice
  251. (const :tag "Never" nil)
  252. (const :tag "When outside special context" t)
  253. (const :tag "Everywhere except timestamps" always)))
  254. (defgroup org-startup nil
  255. "Options concerning startup of Org-mode."
  256. :tag "Org Startup"
  257. :group 'org)
  258. (defcustom org-startup-folded t
  259. "Non-nil means entering Org-mode will switch to OVERVIEW.
  260. This can also be configured on a per-file basis by adding one of
  261. the following lines anywhere in the buffer:
  262. #+STARTUP: fold (or `overview', this is equivalent)
  263. #+STARTUP: nofold (or `showall', this is equivalent)
  264. #+STARTUP: content
  265. #+STARTUP: showeverything"
  266. :group 'org-startup
  267. :type '(choice
  268. (const :tag "nofold: show all" nil)
  269. (const :tag "fold: overview" t)
  270. (const :tag "content: all headlines" content)
  271. (const :tag "show everything, even drawers" showeverything)))
  272. (defcustom org-startup-truncated t
  273. "Non-nil means entering Org-mode will set `truncate-lines'.
  274. This is useful since some lines containing links can be very long and
  275. uninteresting. Also tables look terrible when wrapped."
  276. :group 'org-startup
  277. :type 'boolean)
  278. (defcustom org-startup-indented nil
  279. "Non-nil means turn on `org-indent-mode' on startup.
  280. This can also be configured on a per-file basis by adding one of
  281. the following lines anywhere in the buffer:
  282. #+STARTUP: indent
  283. #+STARTUP: noindent"
  284. :group 'org-structure
  285. :type '(choice
  286. (const :tag "Not" nil)
  287. (const :tag "Globally (slow on startup in large files)" t)))
  288. (defcustom org-startup-with-beamer-mode nil
  289. "Non-nil means turn on `org-beamer-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: beamer"
  293. :group 'org-startup
  294. :type 'boolean)
  295. (defcustom org-startup-align-all-tables nil
  296. "Non-nil means align all tables when visiting a file.
  297. This is useful when the column width in tables is forced with <N> cookies
  298. in table fields. Such tables will look correct only after the first re-align.
  299. This can also be configured on a per-file basis by adding one of
  300. the following lines anywhere in the buffer:
  301. #+STARTUP: align
  302. #+STARTUP: noalign"
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-insert-mode-line-in-empty-file nil
  306. "Non-nil means insert the first line setting Org-mode in empty files.
  307. When the function `org-mode' is called interactively in an empty file, this
  308. normally means that the file name does not automatically trigger Org-mode.
  309. To ensure that the file will always be in Org-mode in the future, a
  310. line enforcing Org-mode will be inserted into the buffer, if this option
  311. has been set."
  312. :group 'org-startup
  313. :type 'boolean)
  314. (defcustom org-replace-disputed-keys nil
  315. "Non-nil means use alternative key bindings for some keys.
  316. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  317. These keys are also used by other packages like shift-selection-mode'
  318. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  319. If you want to use Org-mode together with one of these other modes,
  320. or more generally if you would like to move some Org-mode commands to
  321. other keys, set this variable and configure the keys with the variable
  322. `org-disputed-keys'.
  323. This option is only relevant at load-time of Org-mode, and must be set
  324. *before* org.el is loaded. Changing it requires a restart of Emacs to
  325. become effective."
  326. :group 'org-startup
  327. :type 'boolean)
  328. (defcustom org-use-extra-keys nil
  329. "Non-nil means use extra key sequence definitions for certain
  330. commands. This happens automatically if you run XEmacs or if
  331. window-system is nil. This variable lets you do the same
  332. manually. You must set it before loading org.
  333. Example: on Carbon Emacs 22 running graphically, with an external
  334. keyboard on a Powerbook, the default way of setting M-left might
  335. not work for either Alt or ESC. Setting this variable will make
  336. it work for ESC."
  337. :group 'org-startup
  338. :type 'boolean)
  339. (if (fboundp 'defvaralias)
  340. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  341. (defcustom org-disputed-keys
  342. '(([(shift up)] . [(meta p)])
  343. ([(shift down)] . [(meta n)])
  344. ([(shift left)] . [(meta -)])
  345. ([(shift right)] . [(meta +)])
  346. ([(control shift right)] . [(meta shift +)])
  347. ([(control shift left)] . [(meta shift -)]))
  348. "Keys for which Org-mode and other modes compete.
  349. This is an alist, cars are the default keys, second element specifies
  350. the alternative to use when `org-replace-disputed-keys' is t.
  351. Keys can be specified in any syntax supported by `define-key'.
  352. The value of this option takes effect only at Org-mode's startup,
  353. therefore you'll have to restart Emacs to apply it after changing."
  354. :group 'org-startup
  355. :type 'alist)
  356. (defun org-key (key)
  357. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  358. Or return the original if not disputed."
  359. (if org-replace-disputed-keys
  360. (let* ((nkey (key-description key))
  361. (x (org-find-if (lambda (x)
  362. (equal (key-description (car x)) nkey))
  363. org-disputed-keys)))
  364. (if x (cdr x) key))
  365. key))
  366. (defun org-find-if (predicate seq)
  367. (catch 'exit
  368. (while seq
  369. (if (funcall predicate (car seq))
  370. (throw 'exit (car seq))
  371. (pop seq)))))
  372. (defun org-defkey (keymap key def)
  373. "Define a key, possibly translated, as returned by `org-key'."
  374. (define-key keymap (org-key key) def))
  375. (defcustom org-ellipsis nil
  376. "The ellipsis to use in the Org-mode outline.
  377. When nil, just use the standard three dots. When a string, use that instead,
  378. When a face, use the standard 3 dots, but with the specified face.
  379. The change affects only Org-mode (which will then use its own display table).
  380. Changing this requires executing `M-x org-mode' in a buffer to become
  381. effective."
  382. :group 'org-startup
  383. :type '(choice (const :tag "Default" nil)
  384. (face :tag "Face" :value org-warning)
  385. (string :tag "String" :value "...#")))
  386. (defvar org-display-table nil
  387. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  388. (defgroup org-keywords nil
  389. "Keywords in Org-mode."
  390. :tag "Org Keywords"
  391. :group 'org)
  392. (defcustom org-deadline-string "DEADLINE:"
  393. "String to mark deadline entries.
  394. A deadline is this string, followed by a time stamp. Should be a word,
  395. terminated by a colon. You can insert a schedule keyword and
  396. a timestamp with \\[org-deadline].
  397. Changes become only effective after restarting Emacs."
  398. :group 'org-keywords
  399. :type 'string)
  400. (defcustom org-scheduled-string "SCHEDULED:"
  401. "String to mark scheduled TODO entries.
  402. A schedule is this string, followed by a time stamp. Should be a word,
  403. terminated by a colon. You can insert a schedule keyword and
  404. a timestamp with \\[org-schedule].
  405. Changes become only effective after restarting Emacs."
  406. :group 'org-keywords
  407. :type 'string)
  408. (defcustom org-closed-string "CLOSED:"
  409. "String used as the prefix for timestamps logging closing a TODO entry."
  410. :group 'org-keywords
  411. :type 'string)
  412. (defcustom org-clock-string "CLOCK:"
  413. "String used as prefix for timestamps clocking work hours on an item."
  414. :group 'org-keywords
  415. :type 'string)
  416. (defcustom org-comment-string "COMMENT"
  417. "Entries starting with this keyword will never be exported.
  418. An entry can be toggled between COMMENT and normal with
  419. \\[org-toggle-comment].
  420. Changes become only effective after restarting Emacs."
  421. :group 'org-keywords
  422. :type 'string)
  423. (defcustom org-quote-string "QUOTE"
  424. "Entries starting with this keyword will be exported in fixed-width font.
  425. Quoting applies only to the text in the entry following the headline, and does
  426. not extend beyond the next headline, even if that is lower level.
  427. An entry can be toggled between QUOTE and normal with
  428. \\[org-toggle-fixed-width-section]."
  429. :group 'org-keywords
  430. :type 'string)
  431. (defconst org-repeat-re
  432. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  433. "Regular expression for specifying repeated events.
  434. After a match, group 1 contains the repeat expression.")
  435. (defgroup org-structure nil
  436. "Options concerning the general structure of Org-mode files."
  437. :tag "Org Structure"
  438. :group 'org)
  439. (defgroup org-reveal-location nil
  440. "Options about how to make context of a location visible."
  441. :tag "Org Reveal Location"
  442. :group 'org-structure)
  443. (defconst org-context-choice
  444. '(choice
  445. (const :tag "Always" t)
  446. (const :tag "Never" nil)
  447. (repeat :greedy t :tag "Individual contexts"
  448. (cons
  449. (choice :tag "Context"
  450. (const agenda)
  451. (const org-goto)
  452. (const occur-tree)
  453. (const tags-tree)
  454. (const link-search)
  455. (const mark-goto)
  456. (const bookmark-jump)
  457. (const isearch)
  458. (const default))
  459. (boolean))))
  460. "Contexts for the reveal options.")
  461. (defcustom org-show-hierarchy-above '((default . t))
  462. "Non-nil means show full hierarchy when revealing a location.
  463. Org-mode often shows locations in an org-mode file which might have
  464. been invisible before. When this is set, the hierarchy of headings
  465. above the exposed location is shown.
  466. Turning this off for example for sparse trees makes them very compact.
  467. Instead of t, this can also be an alist specifying this option for different
  468. contexts. Valid contexts are
  469. agenda when exposing an entry from the agenda
  470. org-goto when using the command `org-goto' on key C-c C-j
  471. occur-tree when using the command `org-occur' on key C-c /
  472. tags-tree when constructing a sparse tree based on tags matches
  473. link-search when exposing search matches associated with a link
  474. mark-goto when exposing the jump goal of a mark
  475. bookmark-jump when exposing a bookmark location
  476. isearch when exiting from an incremental search
  477. default default for all contexts not set explicitly"
  478. :group 'org-reveal-location
  479. :type org-context-choice)
  480. (defcustom org-show-following-heading '((default . nil))
  481. "Non-nil means show following heading when revealing a location.
  482. Org-mode often shows locations in an org-mode file which might have
  483. been invisible before. When this is set, the heading following the
  484. match is shown.
  485. Turning this off for example for sparse trees makes them very compact,
  486. but makes it harder to edit the location of the match. In such a case,
  487. use the command \\[org-reveal] to show more context.
  488. Instead of t, this can also be an alist specifying this option for different
  489. contexts. See `org-show-hierarchy-above' for valid contexts."
  490. :group 'org-reveal-location
  491. :type org-context-choice)
  492. (defcustom org-show-siblings '((default . nil) (isearch t))
  493. "Non-nil means show all sibling heading when revealing a location.
  494. Org-mode often shows locations in an org-mode file which might have
  495. been invisible before. When this is set, the sibling of the current entry
  496. heading are all made visible. If `org-show-hierarchy-above' is t,
  497. the same happens on each level of the hierarchy above the current entry.
  498. By default this is on for the isearch context, off for all other contexts.
  499. Turning this off for example for sparse trees makes them very compact,
  500. but makes it harder to edit the location of the match. In such a case,
  501. use the command \\[org-reveal] to show more context.
  502. Instead of t, this can also be an alist specifying this option for different
  503. contexts. See `org-show-hierarchy-above' for valid contexts."
  504. :group 'org-reveal-location
  505. :type org-context-choice)
  506. (defcustom org-show-entry-below '((default . nil))
  507. "Non-nil means show the entry below a headline when revealing a location.
  508. Org-mode often shows locations in an org-mode file which might have
  509. been invisible before. When this is set, the text below the headline that is
  510. exposed is also shown.
  511. By default this is off for all contexts.
  512. Instead of t, this can also be an alist specifying this option for different
  513. contexts. See `org-show-hierarchy-above' for valid contexts."
  514. :group 'org-reveal-location
  515. :type org-context-choice)
  516. (defcustom org-indirect-buffer-display 'other-window
  517. "How should indirect tree buffers be displayed?
  518. This applies to indirect buffers created with the commands
  519. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  520. Valid values are:
  521. current-window Display in the current window
  522. other-window Just display in another window.
  523. dedicated-frame Create one new frame, and re-use it each time.
  524. new-frame Make a new frame each time. Note that in this case
  525. previously-made indirect buffers are kept, and you need to
  526. kill these buffers yourself."
  527. :group 'org-structure
  528. :group 'org-agenda-windows
  529. :type '(choice
  530. (const :tag "In current window" current-window)
  531. (const :tag "In current frame, other window" other-window)
  532. (const :tag "Each time a new frame" new-frame)
  533. (const :tag "One dedicated frame" dedicated-frame)))
  534. (defcustom org-use-speed-commands nil
  535. "Non-nil means activate single letter commands at beginning of a headline.
  536. This may also be a function to test for appropriate locations where speed
  537. commands should be active."
  538. :group 'org-structure
  539. :type '(choice
  540. (const :tag "Never" nil)
  541. (const :tag "At beginning of headline stars" t)
  542. (function)))
  543. (defcustom org-speed-commands-user nil
  544. "Alist of additional speed commands.
  545. This list will be checked before `org-speed-commands-default'
  546. when the variable `org-use-speed-commands' is non-nil
  547. and when the cursor is at the beginning of a headline.
  548. The car if each entry is a string with a single letter, which must
  549. be assigned to `self-insert-command' in the global map.
  550. The cdr is either a command to be called interactively, a function
  551. to be called, or a form to be evaluated.
  552. An entry that is just a list with a single string will be interpreted
  553. as a descriptive headline that will be added when listing the speed
  554. copmmands in the Help buffer using the `?' speed command."
  555. :group 'org-structure
  556. :type '(repeat :value ("k" . ignore)
  557. (choice :value ("k" . ignore)
  558. (list :tag "Descriptive Headline" (string :tag "Headline"))
  559. (cons :tag "Letter and Command"
  560. (string :tag "Command letter")
  561. (choice
  562. (function)
  563. (sexp))))))
  564. (defgroup org-cycle nil
  565. "Options concerning visibility cycling in Org-mode."
  566. :tag "Org Cycle"
  567. :group 'org-structure)
  568. (defcustom org-cycle-skip-children-state-if-no-children t
  569. "Non-nil means skip CHILDREN state in entries that don't have any."
  570. :group 'org-cycle
  571. :type 'boolean)
  572. (defcustom org-cycle-max-level nil
  573. "Maximum level which should still be subject to visibility cycling.
  574. Levels higher than this will, for cycling, be treated as text, not a headline.
  575. When `org-odd-levels-only' is set, a value of N in this variable actually
  576. means 2N-1 stars as the limiting headline.
  577. When nil, cycle all levels.
  578. Note that the limiting level of cycling is also influenced by
  579. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  580. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  581. than its value."
  582. :group 'org-cycle
  583. :type '(choice
  584. (const :tag "No limit" nil)
  585. (integer :tag "Maximum level")))
  586. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  587. "Names of drawers. Drawers are not opened by cycling on the headline above.
  588. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  589. this:
  590. :DRAWERNAME:
  591. .....
  592. :END:
  593. The drawer \"PROPERTIES\" is special for capturing properties through
  594. the property API.
  595. Drawers can be defined on the per-file basis with a line like:
  596. #+DRAWERS: HIDDEN STATE PROPERTIES"
  597. :group 'org-structure
  598. :group 'org-cycle
  599. :type '(repeat (string :tag "Drawer Name")))
  600. (defcustom org-hide-block-startup nil
  601. "Non-nil means entering Org-mode will fold all blocks.
  602. This can also be set in on a per-file basis with
  603. #+STARTUP: hideblocks
  604. #+STARTUP: showblocks"
  605. :group 'org-startup
  606. :group 'org-cycle
  607. :type 'boolean)
  608. (defcustom org-cycle-global-at-bob nil
  609. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  610. This makes it possible to do global cycling without having to use S-TAB or
  611. C-u TAB. For this special case to work, the first line of the buffer
  612. must not be a headline - it may be empty or some other text. When used in
  613. this way, `org-cycle-hook' is disables temporarily, to make sure the
  614. cursor stays at the beginning of the buffer.
  615. When this option is nil, don't do anything special at the beginning
  616. of the buffer."
  617. :group 'org-cycle
  618. :type 'boolean)
  619. (defcustom org-cycle-level-after-item/entry-creation t
  620. "Non-nil means cycle entry level or item indentation in new empty entries.
  621. When the cursor is at the end of an empty headline, i.e with only stars
  622. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  623. and then all possible anchestor states, before returning to the original state.
  624. This makes data entry extremely fast: M-RET to create a new headline,
  625. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  626. When the cursor is at the end of an empty plain list item, one TAB will
  627. make it a subitem, two or more tabs will back up to make this an item
  628. higher up in the item hierarchy."
  629. :group 'org-cycle
  630. :type 'boolean)
  631. (defcustom org-cycle-emulate-tab t
  632. "Where should `org-cycle' emulate TAB.
  633. nil Never
  634. white Only in completely white lines
  635. whitestart Only at the beginning of lines, before the first non-white char
  636. t Everywhere except in headlines
  637. exc-hl-bol Everywhere except at the start of a headline
  638. If TAB is used in a place where it does not emulate TAB, the current subtree
  639. visibility is cycled."
  640. :group 'org-cycle
  641. :type '(choice (const :tag "Never" nil)
  642. (const :tag "Only in completely white lines" white)
  643. (const :tag "Before first char in a line" whitestart)
  644. (const :tag "Everywhere except in headlines" t)
  645. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  646. ))
  647. (defcustom org-cycle-separator-lines 2
  648. "Number of empty lines needed to keep an empty line between collapsed trees.
  649. If you leave an empty line between the end of a subtree and the following
  650. headline, this empty line is hidden when the subtree is folded.
  651. Org-mode will leave (exactly) one empty line visible if the number of
  652. empty lines is equal or larger to the number given in this variable.
  653. So the default 2 means at least 2 empty lines after the end of a subtree
  654. are needed to produce free space between a collapsed subtree and the
  655. following headline.
  656. If the number is negative, and the number of empty lines is at least -N,
  657. all empty lines are shown.
  658. Special case: when 0, never leave empty lines in collapsed view."
  659. :group 'org-cycle
  660. :type 'integer)
  661. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  662. (defcustom org-pre-cycle-hook nil
  663. "Hook that is run before visibility cycling is happening.
  664. The function(s) in this hook must accept a single argument which indicates
  665. the new state that will be set right after running this hook. The
  666. argument is a symbol. Before a global state change, it can have the values
  667. `overview', `content', or `all'. Before a local state change, it can have
  668. the values `folded', `children', or `subtree'."
  669. :group 'org-cycle
  670. :type 'hook)
  671. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  672. org-cycle-hide-drawers
  673. org-cycle-show-empty-lines
  674. org-optimize-window-after-visibility-change)
  675. "Hook that is run after `org-cycle' has changed the buffer visibility.
  676. The function(s) in this hook must accept a single argument which indicates
  677. the new state that was set by the most recent `org-cycle' command. The
  678. argument is a symbol. After a global state change, it can have the values
  679. `overview', `content', or `all'. After a local state change, it can have
  680. the values `folded', `children', or `subtree'."
  681. :group 'org-cycle
  682. :type 'hook)
  683. (defgroup org-edit-structure nil
  684. "Options concerning structure editing in Org-mode."
  685. :tag "Org Edit Structure"
  686. :group 'org-structure)
  687. (defcustom org-odd-levels-only nil
  688. "Non-nil means skip even levels and only use odd levels for the outline.
  689. This has the effect that two stars are being added/taken away in
  690. promotion/demotion commands. It also influences how levels are
  691. handled by the exporters.
  692. Changing it requires restart of `font-lock-mode' to become effective
  693. for fontification also in regions already fontified.
  694. You may also set this on a per-file basis by adding one of the following
  695. lines to the buffer:
  696. #+STARTUP: odd
  697. #+STARTUP: oddeven"
  698. :group 'org-edit-structure
  699. :group 'org-appearance
  700. :type 'boolean)
  701. (defcustom org-adapt-indentation t
  702. "Non-nil means adapt indentation to outline node level.
  703. When this variable is set, Org assumes that you write outlines by
  704. indenting text in each node to align with the headline (after the stars).
  705. The following issues are influenced by this variable:
  706. - When this is set and the *entire* text in an entry is indented, the
  707. indentation is increased by one space in a demotion command, and
  708. decreased by one in a promotion command. If any line in the entry
  709. body starts with text at column 0, indentation is not changed at all.
  710. - Property drawers and planning information is inserted indented when
  711. this variable s set. When nil, they will not be indented.
  712. - TAB indents a line relative to context. The lines below a headline
  713. will be indented when this variable is set.
  714. Note that this is all about true indentation, by adding and removing
  715. space characters. See also `org-indent.el' which does level-dependent
  716. indentation in a virtual way, i.e. at display time in Emacs."
  717. :group 'org-edit-structure
  718. :type 'boolean)
  719. (defcustom org-special-ctrl-a/e nil
  720. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  721. When t, `C-a' will bring back the cursor to the beginning of the
  722. headline text, i.e. after the stars and after a possible TODO keyword.
  723. In an item, this will be the position after the bullet.
  724. When the cursor is already at that position, another `C-a' will bring
  725. it to the beginning of the line.
  726. `C-e' will jump to the end of the headline, ignoring the presence of tags
  727. in the headline. A second `C-e' will then jump to the true end of the
  728. line, after any tags. This also means that, when this variable is
  729. non-nil, `C-e' also will never jump beyond the end of the heading of a
  730. folded section, i.e. not after the ellipses.
  731. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  732. going to the true line boundary first. Only a directly following, identical
  733. keypress will bring the cursor to the special positions.
  734. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  735. set separately."
  736. :group 'org-edit-structure
  737. :type '(choice
  738. (const :tag "off" nil)
  739. (const :tag "on: after stars/bullet and before tags first" t)
  740. (const :tag "reversed: true line boundary first" reversed)
  741. (cons :tag "Set C-a and C-e separately"
  742. (choice :tag "Special C-a"
  743. (const :tag "off" nil)
  744. (const :tag "on: after stars/bullet first" t)
  745. (const :tag "reversed: before stars/bullet first" reversed))
  746. (choice :tag "Special C-e"
  747. (const :tag "off" nil)
  748. (const :tag "on: before tags first" t)
  749. (const :tag "reversed: after tags first" reversed)))))
  750. (if (fboundp 'defvaralias)
  751. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  752. (defcustom org-special-ctrl-k nil
  753. "Non-nil means `C-k' will behave specially in headlines.
  754. When nil, `C-k' will call the default `kill-line' command.
  755. When t, the following will happen while the cursor is in the headline:
  756. - When the cursor is at the beginning of a headline, kill the entire
  757. line and possible the folded subtree below the line.
  758. - When in the middle of the headline text, kill the headline up to the tags.
  759. - When after the headline text, kill the tags."
  760. :group 'org-edit-structure
  761. :type 'boolean)
  762. (defcustom org-yank-folded-subtrees t
  763. "Non-nil means when yanking subtrees, fold them.
  764. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  765. it starts with a heading and all other headings in it are either children
  766. or siblings, then fold all the subtrees. However, do this only if no
  767. text after the yank would be swallowed into a folded tree by this action."
  768. :group 'org-edit-structure
  769. :type 'boolean)
  770. (defcustom org-yank-adjusted-subtrees nil
  771. "Non-nil means when yanking subtrees, adjust the level.
  772. With this setting, `org-paste-subtree' is used to insert the subtree, see
  773. this function for details."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defcustom org-M-RET-may-split-line '((default . t))
  777. "Non-nil means M-RET will split the line at the cursor position.
  778. When nil, it will go to the end of the line before making a
  779. new line.
  780. You may also set this option in a different way for different
  781. contexts. Valid contexts are:
  782. headline when creating a new headline
  783. item when creating a new item
  784. table in a table field
  785. default the value to be used for all contexts not explicitly
  786. customized"
  787. :group 'org-structure
  788. :group 'org-table
  789. :type '(choice
  790. (const :tag "Always" t)
  791. (const :tag "Never" nil)
  792. (repeat :greedy t :tag "Individual contexts"
  793. (cons
  794. (choice :tag "Context"
  795. (const headline)
  796. (const item)
  797. (const table)
  798. (const default))
  799. (boolean)))))
  800. (defcustom org-insert-heading-respect-content nil
  801. "Non-nil means insert new headings after the current subtree.
  802. When nil, the new heading is created directly after the current line.
  803. The commands \\[org-insert-heading-respect-content] and
  804. \\[org-insert-todo-heading-respect-content] turn this variable on
  805. for the duration of the command."
  806. :group 'org-structure
  807. :type 'boolean)
  808. (defcustom org-blank-before-new-entry '((heading . auto)
  809. (plain-list-item . auto))
  810. "Should `org-insert-heading' leave a blank line before new heading/item?
  811. The value is an alist, with `heading' and `plain-list-item' as car,
  812. and a boolean flag as cdr. For plain lists, if the variable
  813. `org-empty-line-terminates-plain-lists' is set, the setting here
  814. is ignored and no empty line is inserted, to keep the list in tact."
  815. :group 'org-edit-structure
  816. :type '(list
  817. (cons (const heading)
  818. (choice (const :tag "Never" nil)
  819. (const :tag "Always" t)
  820. (const :tag "Auto" auto)))
  821. (cons (const plain-list-item)
  822. (choice (const :tag "Never" nil)
  823. (const :tag "Always" t)
  824. (const :tag "Auto" auto)))))
  825. (defcustom org-insert-heading-hook nil
  826. "Hook being run after inserting a new heading."
  827. :group 'org-edit-structure
  828. :type 'hook)
  829. (defcustom org-enable-fixed-width-editor t
  830. "Non-nil means lines starting with \":\" are treated as fixed-width.
  831. This currently only means they are never auto-wrapped.
  832. When nil, such lines will be treated like ordinary lines.
  833. See also the QUOTE keyword."
  834. :group 'org-edit-structure
  835. :type 'boolean)
  836. (defcustom org-goto-auto-isearch t
  837. "Non-nil means typing characters in org-goto starts incremental search."
  838. :group 'org-edit-structure
  839. :type 'boolean)
  840. (defgroup org-sparse-trees nil
  841. "Options concerning sparse trees in Org-mode."
  842. :tag "Org Sparse Trees"
  843. :group 'org-structure)
  844. (defcustom org-highlight-sparse-tree-matches t
  845. "Non-nil means highlight all matches that define a sparse tree.
  846. The highlights will automatically disappear the next time the buffer is
  847. changed by an edit command."
  848. :group 'org-sparse-trees
  849. :type 'boolean)
  850. (defcustom org-remove-highlights-with-change t
  851. "Non-nil means any change to the buffer will remove temporary highlights.
  852. Such highlights are created by `org-occur' and `org-clock-display'.
  853. When nil, `C-c C-c needs to be used to get rid of the highlights.
  854. The highlights created by `org-preview-latex-fragment' always need
  855. `C-c C-c' to be removed."
  856. :group 'org-sparse-trees
  857. :group 'org-time
  858. :type 'boolean)
  859. (defcustom org-occur-hook '(org-first-headline-recenter)
  860. "Hook that is run after `org-occur' has constructed a sparse tree.
  861. This can be used to recenter the window to show as much of the structure
  862. as possible."
  863. :group 'org-sparse-trees
  864. :type 'hook)
  865. (defgroup org-imenu-and-speedbar nil
  866. "Options concerning imenu and speedbar in Org-mode."
  867. :tag "Org Imenu and Speedbar"
  868. :group 'org-structure)
  869. (defcustom org-imenu-depth 2
  870. "The maximum level for Imenu access to Org-mode headlines.
  871. This also applied for speedbar access."
  872. :group 'org-imenu-and-speedbar
  873. :type 'integer)
  874. (defgroup org-table nil
  875. "Options concerning tables in Org-mode."
  876. :tag "Org Table"
  877. :group 'org)
  878. (defcustom org-enable-table-editor 'optimized
  879. "Non-nil means lines starting with \"|\" are handled by the table editor.
  880. When nil, such lines will be treated like ordinary lines.
  881. When equal to the symbol `optimized', the table editor will be optimized to
  882. do the following:
  883. - Automatic overwrite mode in front of whitespace in table fields.
  884. This makes the structure of the table stay in tact as long as the edited
  885. field does not exceed the column width.
  886. - Minimize the number of realigns. Normally, the table is aligned each time
  887. TAB or RET are pressed to move to another field. With optimization this
  888. happens only if changes to a field might have changed the column width.
  889. Optimization requires replacing the functions `self-insert-command',
  890. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  891. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  892. very good at guessing when a re-align will be necessary, but you can always
  893. force one with \\[org-ctrl-c-ctrl-c].
  894. If you would like to use the optimized version in Org-mode, but the
  895. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  896. This variable can be used to turn on and off the table editor during a session,
  897. but in order to toggle optimization, a restart is required.
  898. See also the variable `org-table-auto-blank-field'."
  899. :group 'org-table
  900. :type '(choice
  901. (const :tag "off" nil)
  902. (const :tag "on" t)
  903. (const :tag "on, optimized" optimized)))
  904. (defcustom org-self-insert-cluster-for-undo t
  905. "Non-nil means cluster self-insert commands for undo when possible.
  906. If this is set, then, like in the Emacs command loop, 20 consecutive
  907. characters will be undone together.
  908. This is configurable, because there is some impact on typing performance."
  909. :group 'org-table
  910. :type 'boolean)
  911. (defcustom org-table-tab-recognizes-table.el t
  912. "Non-nil means TAB will automatically notice a table.el table.
  913. When it sees such a table, it moves point into it and - if necessary -
  914. calls `table-recognize-table'."
  915. :group 'org-table-editing
  916. :type 'boolean)
  917. (defgroup org-link nil
  918. "Options concerning links in Org-mode."
  919. :tag "Org Link"
  920. :group 'org)
  921. (defvar org-link-abbrev-alist-local nil
  922. "Buffer-local version of `org-link-abbrev-alist', which see.
  923. The value of this is taken from the #+LINK lines.")
  924. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  925. (defcustom org-link-abbrev-alist nil
  926. "Alist of link abbreviations.
  927. The car of each element is a string, to be replaced at the start of a link.
  928. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  929. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  930. [[linkkey:tag][description]]
  931. The 'linkkey' must be a word word, starting with a letter, followed
  932. by letters, numbers, '-' or '_'.
  933. If REPLACE is a string, the tag will simply be appended to create the link.
  934. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  935. the placeholder \"%h\" will cause a url-encoded version of the tag to
  936. be inserted at that point (see the function `url-hexify-string').
  937. REPLACE may also be a function that will be called with the tag as the
  938. only argument to create the link, which should be returned as a string.
  939. See the manual for examples."
  940. :group 'org-link
  941. :type '(repeat
  942. (cons
  943. (string :tag "Protocol")
  944. (choice
  945. (string :tag "Format")
  946. (function)))))
  947. (defcustom org-descriptive-links t
  948. "Non-nil means hide link part and only show description of bracket links.
  949. Bracket links are like [[link][description]]. This variable sets the initial
  950. state in new org-mode buffers. The setting can then be toggled on a
  951. per-buffer basis from the Org->Hyperlinks menu."
  952. :group 'org-link
  953. :type 'boolean)
  954. (defcustom org-link-file-path-type 'adaptive
  955. "How the path name in file links should be stored.
  956. Valid values are:
  957. relative Relative to the current directory, i.e. the directory of the file
  958. into which the link is being inserted.
  959. absolute Absolute path, if possible with ~ for home directory.
  960. noabbrev Absolute path, no abbreviation of home directory.
  961. adaptive Use relative path for files in the current directory and sub-
  962. directories of it. For other files, use an absolute path."
  963. :group 'org-link
  964. :type '(choice
  965. (const relative)
  966. (const absolute)
  967. (const noabbrev)
  968. (const adaptive)))
  969. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  970. "Types of links that should be activated in Org-mode files.
  971. This is a list of symbols, each leading to the activation of a certain link
  972. type. In principle, it does not hurt to turn on most link types - there may
  973. be a small gain when turning off unused link types. The types are:
  974. bracket The recommended [[link][description]] or [[link]] links with hiding.
  975. angular Links in angular brackets that may contain whitespace like
  976. <bbdb:Carsten Dominik>.
  977. plain Plain links in normal text, no whitespace, like http://google.com.
  978. radio Text that is matched by a radio target, see manual for details.
  979. tag Tag settings in a headline (link to tag search).
  980. date Time stamps (link to calendar).
  981. footnote Footnote labels.
  982. Changing this variable requires a restart of Emacs to become effective."
  983. :group 'org-link
  984. :type '(set :greedy t
  985. (const :tag "Double bracket links (new style)" bracket)
  986. (const :tag "Angular bracket links (old style)" angular)
  987. (const :tag "Plain text links" plain)
  988. (const :tag "Radio target matches" radio)
  989. (const :tag "Tags" tag)
  990. (const :tag "Timestamps" date)
  991. (const :tag "Footnotes" footnote)))
  992. (defcustom org-make-link-description-function nil
  993. "Function to use to generate link descriptions from links. If
  994. nil the link location will be used. This function must take two
  995. parameters; the first is the link and the second the description
  996. org-insert-link has generated, and should return the description
  997. to use."
  998. :group 'org-link
  999. :type 'function)
  1000. (defgroup org-link-store nil
  1001. "Options concerning storing links in Org-mode."
  1002. :tag "Org Store Link"
  1003. :group 'org-link)
  1004. (defcustom org-email-link-description-format "Email %c: %.30s"
  1005. "Format of the description part of a link to an email or usenet message.
  1006. The following %-escapes will be replaced by corresponding information:
  1007. %F full \"From\" field
  1008. %f name, taken from \"From\" field, address if no name
  1009. %T full \"To\" field
  1010. %t first name in \"To\" field, address if no name
  1011. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1012. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1013. %s subject
  1014. %m message-id.
  1015. You may use normal field width specification between the % and the letter.
  1016. This is for example useful to limit the length of the subject.
  1017. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1018. :group 'org-link-store
  1019. :type 'string)
  1020. (defcustom org-from-is-user-regexp
  1021. (let (r1 r2)
  1022. (when (and user-mail-address (not (string= user-mail-address "")))
  1023. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1024. (when (and user-full-name (not (string= user-full-name "")))
  1025. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1026. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1027. "Regexp matched against the \"From:\" header of an email or usenet message.
  1028. It should match if the message is from the user him/herself."
  1029. :group 'org-link-store
  1030. :type 'regexp)
  1031. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1032. "Non-nil means storing a link to an Org file will use entry IDs.
  1033. Note that before this variable is even considered, org-id must be loaded,
  1034. so please customize `org-modules' and turn it on.
  1035. The variable can have the following values:
  1036. t Create an ID if needed to make a link to the current entry.
  1037. create-if-interactive
  1038. If `org-store-link' is called directly (interactively, as a user
  1039. command), do create an ID to support the link. But when doing the
  1040. job for remember, only use the ID if it already exists. The
  1041. purpose of this setting is to avoid proliferation of unwanted
  1042. IDs, just because you happen to be in an Org file when you
  1043. call `org-remember' that automatically and preemptively
  1044. creates a link. If you do want to get an ID link in a remember
  1045. template to an entry not having an ID, create it first by
  1046. explicitly creating a link to it, using `C-c C-l' first.
  1047. create-if-interactive-and-no-custom-id
  1048. Like create-if-interactive, but do not create an ID if there is
  1049. a CUSTOM_ID property defined in the entry. This is the default.
  1050. use-existing
  1051. Use existing ID, do not create one.
  1052. nil Never use an ID to make a link, instead link using a text search for
  1053. the headline text."
  1054. :group 'org-link-store
  1055. :type '(choice
  1056. (const :tag "Create ID to make link" t)
  1057. (const :tag "Create if storing link interactively"
  1058. create-if-interactive)
  1059. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1060. create-if-interactive-and-no-custom-id)
  1061. (const :tag "Only use existing" use-existing)
  1062. (const :tag "Do not use ID to create link" nil)))
  1063. (defcustom org-context-in-file-links t
  1064. "Non-nil means file links from `org-store-link' contain context.
  1065. A search string will be added to the file name with :: as separator and
  1066. used to find the context when the link is activated by the command
  1067. `org-open-at-point'.
  1068. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1069. negates this setting for the duration of the command."
  1070. :group 'org-link-store
  1071. :type 'boolean)
  1072. (defcustom org-keep-stored-link-after-insertion nil
  1073. "Non-nil means keep link in list for entire session.
  1074. The command `org-store-link' adds a link pointing to the current
  1075. location to an internal list. These links accumulate during a session.
  1076. The command `org-insert-link' can be used to insert links into any
  1077. Org-mode file (offering completion for all stored links). When this
  1078. option is nil, every link which has been inserted once using \\[org-insert-link]
  1079. will be removed from the list, to make completing the unused links
  1080. more efficient."
  1081. :group 'org-link-store
  1082. :type 'boolean)
  1083. (defgroup org-link-follow nil
  1084. "Options concerning following links in Org-mode."
  1085. :tag "Org Follow Link"
  1086. :group 'org-link)
  1087. (defcustom org-link-translation-function nil
  1088. "Function to translate links with different syntax to Org syntax.
  1089. This can be used to translate links created for example by the Planner
  1090. or emacs-wiki packages to Org syntax.
  1091. The function must accept two parameters, a TYPE containing the link
  1092. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1093. which is everything after the link protocol. It should return a cons
  1094. with possibly modified values of type and path.
  1095. Org contains a function for this, so if you set this variable to
  1096. `org-translate-link-from-planner', you should be able follow many
  1097. links created by planner."
  1098. :group 'org-link-follow
  1099. :type 'function)
  1100. (defcustom org-follow-link-hook nil
  1101. "Hook that is run after a link has been followed."
  1102. :group 'org-link-follow
  1103. :type 'hook)
  1104. (defcustom org-tab-follows-link nil
  1105. "Non-nil means on links TAB will follow the link.
  1106. Needs to be set before org.el is loaded.
  1107. This really should not be used, it does not make sense, and the
  1108. implementation is bad."
  1109. :group 'org-link-follow
  1110. :type 'boolean)
  1111. (defcustom org-return-follows-link nil
  1112. "Non-nil means on links RET will follow the link.
  1113. Needs to be set before org.el is loaded."
  1114. :group 'org-link-follow
  1115. :type 'boolean)
  1116. (defcustom org-mouse-1-follows-link
  1117. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1118. "Non-nil means mouse-1 on a link will follow the link.
  1119. A longer mouse click will still set point. Does not work on XEmacs.
  1120. Needs to be set before org.el is loaded."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-mark-ring-length 4
  1124. "Number of different positions to be recorded in the ring
  1125. Changing this requires a restart of Emacs to work correctly."
  1126. :group 'org-link-follow
  1127. :type 'integer)
  1128. (defcustom org-link-frame-setup
  1129. '((vm . vm-visit-folder-other-frame)
  1130. (gnus . gnus-other-frame)
  1131. (file . find-file-other-window))
  1132. "Setup the frame configuration for following links.
  1133. When following a link with Emacs, it may often be useful to display
  1134. this link in another window or frame. This variable can be used to
  1135. set this up for the different types of links.
  1136. For VM, use any of
  1137. `vm-visit-folder'
  1138. `vm-visit-folder-other-frame'
  1139. For Gnus, use any of
  1140. `gnus'
  1141. `gnus-other-frame'
  1142. `org-gnus-no-new-news'
  1143. For FILE, use any of
  1144. `find-file'
  1145. `find-file-other-window'
  1146. `find-file-other-frame'
  1147. For the calendar, use the variable `calendar-setup'.
  1148. For BBDB, it is currently only possible to display the matches in
  1149. another window."
  1150. :group 'org-link-follow
  1151. :type '(list
  1152. (cons (const vm)
  1153. (choice
  1154. (const vm-visit-folder)
  1155. (const vm-visit-folder-other-window)
  1156. (const vm-visit-folder-other-frame)))
  1157. (cons (const gnus)
  1158. (choice
  1159. (const gnus)
  1160. (const gnus-other-frame)
  1161. (const org-gnus-no-new-news)))
  1162. (cons (const file)
  1163. (choice
  1164. (const find-file)
  1165. (const find-file-other-window)
  1166. (const find-file-other-frame)))))
  1167. (defcustom org-display-internal-link-with-indirect-buffer nil
  1168. "Non-nil means use indirect buffer to display infile links.
  1169. Activating internal links (from one location in a file to another location
  1170. in the same file) normally just jumps to the location. When the link is
  1171. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1172. another window. When this option is set, the other window actually displays
  1173. an indirect buffer clone of the current buffer, to avoid any visibility
  1174. changes to the current buffer."
  1175. :group 'org-link-follow
  1176. :type 'boolean)
  1177. (defcustom org-open-non-existing-files nil
  1178. "Non-nil means `org-open-file' will open non-existing files.
  1179. When nil, an error will be generated.
  1180. This variable applies only to external applications because they
  1181. might choke on non-existing files. If the link is to a file that
  1182. will be opened in Emacs, the variable is ignored."
  1183. :group 'org-link-follow
  1184. :type 'boolean)
  1185. (defcustom org-open-directory-means-index-dot-org nil
  1186. "Non-nil means a link to a directory really means to index.org.
  1187. When nil, following a directory link will run dired or open a finder/explorer
  1188. window on that directory."
  1189. :group 'org-link-follow
  1190. :type 'boolean)
  1191. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1192. "Function and arguments to call for following mailto links.
  1193. This is a list with the first element being a lisp function, and the
  1194. remaining elements being arguments to the function. In string arguments,
  1195. %a will be replaced by the address, and %s will be replaced by the subject
  1196. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1197. :group 'org-link-follow
  1198. :type '(choice
  1199. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1200. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1201. (const :tag "message-mail" (message-mail "%a" "%s"))
  1202. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1203. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1204. "Non-nil means ask for confirmation before executing shell links.
  1205. Shell links can be dangerous: just think about a link
  1206. [[shell:rm -rf ~/*][Google Search]]
  1207. This link would show up in your Org-mode document as \"Google Search\",
  1208. but really it would remove your entire home directory.
  1209. Therefore we advise against setting this variable to nil.
  1210. Just change it to `y-or-n-p' if you want to confirm with a
  1211. single keystroke rather than having to type \"yes\"."
  1212. :group 'org-link-follow
  1213. :type '(choice
  1214. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1215. (const :tag "with y-or-n (faster)" y-or-n-p)
  1216. (const :tag "no confirmation (dangerous)" nil)))
  1217. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1218. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1219. Elisp links can be dangerous: just think about a link
  1220. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1221. This link would show up in your Org-mode document as \"Google Search\",
  1222. but really it would remove your entire home directory.
  1223. Therefore we advise against setting this variable to nil.
  1224. Just change it to `y-or-n-p' if you want to confirm with a
  1225. single keystroke rather than having to type \"yes\"."
  1226. :group 'org-link-follow
  1227. :type '(choice
  1228. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1229. (const :tag "with y-or-n (faster)" y-or-n-p)
  1230. (const :tag "no confirmation (dangerous)" nil)))
  1231. (defconst org-file-apps-defaults-gnu
  1232. '((remote . emacs)
  1233. (system . mailcap)
  1234. (t . mailcap))
  1235. "Default file applications on a UNIX or GNU/Linux system.
  1236. See `org-file-apps'.")
  1237. (defconst org-file-apps-defaults-macosx
  1238. '((remote . emacs)
  1239. (t . "open %s")
  1240. (system . "open %s")
  1241. ("ps.gz" . "gv %s")
  1242. ("eps.gz" . "gv %s")
  1243. ("dvi" . "xdvi %s")
  1244. ("fig" . "xfig %s"))
  1245. "Default file applications on a MacOS X system.
  1246. The system \"open\" is known as a default, but we use X11 applications
  1247. for some files for which the OS does not have a good default.
  1248. See `org-file-apps'.")
  1249. (defconst org-file-apps-defaults-windowsnt
  1250. (list
  1251. '(remote . emacs)
  1252. (cons t
  1253. (list (if (featurep 'xemacs)
  1254. 'mswindows-shell-execute
  1255. 'w32-shell-execute)
  1256. "open" 'file))
  1257. (cons 'system
  1258. (list (if (featurep 'xemacs)
  1259. 'mswindows-shell-execute
  1260. 'w32-shell-execute)
  1261. "open" 'file)))
  1262. "Default file applications on a Windows NT system.
  1263. The system \"open\" is used for most files.
  1264. See `org-file-apps'.")
  1265. (defcustom org-file-apps
  1266. '(
  1267. (auto-mode . emacs)
  1268. ("\\.mm\\'" . default)
  1269. ("\\.x?html?\\'" . default)
  1270. ("\\.pdf\\'" . default)
  1271. )
  1272. "External applications for opening `file:path' items in a document.
  1273. Org-mode uses system defaults for different file types, but
  1274. you can use this variable to set the application for a given file
  1275. extension. The entries in this list are cons cells where the car identifies
  1276. files and the cdr the corresponding command. Possible values for the
  1277. file identifier are
  1278. \"regex\" Regular expression matched against the file name. For backward
  1279. compatibility, this can also be a string with only alphanumeric
  1280. characters, which is then interpreted as an extension.
  1281. `directory' Matches a directory
  1282. `remote' Matches a remote file, accessible through tramp or efs.
  1283. Remote files most likely should be visited through Emacs
  1284. because external applications cannot handle such paths.
  1285. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1286. so all files Emacs knows how to handle. Using this with
  1287. command `emacs' will open most files in Emacs. Beware that this
  1288. will also open html files inside Emacs, unless you add
  1289. (\"html\" . default) to the list as well.
  1290. t Default for files not matched by any of the other options.
  1291. `system' The system command to open files, like `open' on Windows
  1292. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1293. that will be selected if you call `C-c C-o' with a double
  1294. `C-u C-u' prefix.
  1295. Possible values for the command are:
  1296. `emacs' The file will be visited by the current Emacs process.
  1297. `default' Use the default application for this file type, which is the
  1298. association for t in the list, most likely in the system-specific
  1299. part.
  1300. This can be used to overrule an unwanted setting in the
  1301. system-specific variable.
  1302. `system' Use the system command for opening files, like \"open\".
  1303. This command is specified by the entry whose car is `system'.
  1304. Most likely, the system-specific version of this variable
  1305. does define this command, but you can overrule/replace it
  1306. here.
  1307. string A command to be executed by a shell; %s will be replaced
  1308. by the path to the file.
  1309. sexp A Lisp form which will be evaluated. The file path will
  1310. be available in the Lisp variable `file'.
  1311. For more examples, see the system specific constants
  1312. `org-file-apps-defaults-macosx'
  1313. `org-file-apps-defaults-windowsnt'
  1314. `org-file-apps-defaults-gnu'."
  1315. :group 'org-link-follow
  1316. :type '(repeat
  1317. (cons (choice :value ""
  1318. (string :tag "Extension")
  1319. (const :tag "System command to open files" system)
  1320. (const :tag "Default for unrecognized files" t)
  1321. (const :tag "Remote file" remote)
  1322. (const :tag "Links to a directory" directory)
  1323. (const :tag "Any files that have Emacs modes"
  1324. auto-mode))
  1325. (choice :value ""
  1326. (const :tag "Visit with Emacs" emacs)
  1327. (const :tag "Use default" default)
  1328. (const :tag "Use the system command" system)
  1329. (string :tag "Command")
  1330. (sexp :tag "Lisp form")))))
  1331. (defgroup org-refile nil
  1332. "Options concerning refiling entries in Org-mode."
  1333. :tag "Org Refile"
  1334. :group 'org)
  1335. (defcustom org-directory "~/org"
  1336. "Directory with org files.
  1337. This is just a default location to look for Org files. There is no need
  1338. at all to put your files into this directory. It is only used in the
  1339. following situations:
  1340. 1. When a remember template specifies a target file that is not an
  1341. absolute path. The path will then be interpreted relative to
  1342. `org-directory'
  1343. 2. When a remember note is filed away in an interactive way (when exiting the
  1344. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1345. with `org-directory' as the default path."
  1346. :group 'org-refile
  1347. :group 'org-remember
  1348. :type 'directory)
  1349. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1350. "Default target for storing notes.
  1351. Used by the hooks for remember.el. This can be a string, or nil to mean
  1352. the value of `remember-data-file'.
  1353. You can set this on a per-template basis with the variable
  1354. `org-remember-templates'."
  1355. :group 'org-refile
  1356. :group 'org-remember
  1357. :type '(choice
  1358. (const :tag "Default from remember-data-file" nil)
  1359. file))
  1360. (defcustom org-goto-interface 'outline
  1361. "The default interface to be used for `org-goto'.
  1362. Allowed values are:
  1363. outline The interface shows an outline of the relevant file
  1364. and the correct heading is found by moving through
  1365. the outline or by searching with incremental search.
  1366. outline-path-completion Headlines in the current buffer are offered via
  1367. completion. This is the interface also used by
  1368. the refile command."
  1369. :group 'org-refile
  1370. :type '(choice
  1371. (const :tag "Outline" outline)
  1372. (const :tag "Outline-path-completion" outline-path-completion)))
  1373. (defcustom org-goto-max-level 5
  1374. "Maximum level to be considered when running org-goto with refile interface."
  1375. :group 'org-refile
  1376. :type 'integer)
  1377. (defcustom org-reverse-note-order nil
  1378. "Non-nil means store new notes at the beginning of a file or entry.
  1379. When nil, new notes will be filed to the end of a file or entry.
  1380. This can also be a list with cons cells of regular expressions that
  1381. are matched against file names, and values."
  1382. :group 'org-remember
  1383. :group 'org-refile
  1384. :type '(choice
  1385. (const :tag "Reverse always" t)
  1386. (const :tag "Reverse never" nil)
  1387. (repeat :tag "By file name regexp"
  1388. (cons regexp boolean))))
  1389. (defcustom org-log-refile nil
  1390. "Information to record when a task is refiled.
  1391. Possible values are:
  1392. nil Don't add anything
  1393. time Add a time stamp to the task
  1394. note Prompt for a note and add it with template `org-log-note-headings'
  1395. This option can also be set with on a per-file-basis with
  1396. #+STARTUP: nologrefile
  1397. #+STARTUP: logrefile
  1398. #+STARTUP: lognoterefile
  1399. You can have local logging settings for a subtree by setting the LOGGING
  1400. property to one or more of these keywords.
  1401. When bulk-refiling from the agenda, the value `note' is forbidden and
  1402. will temporarily be changed to `time'."
  1403. :group 'org-refile
  1404. :group 'org-progress
  1405. :type '(choice
  1406. (const :tag "No logging" nil)
  1407. (const :tag "Record timestamp" time)
  1408. (const :tag "Record timestamp with note." note)))
  1409. (defcustom org-refile-targets nil
  1410. "Targets for refiling entries with \\[org-refile].
  1411. This is list of cons cells. Each cell contains:
  1412. - a specification of the files to be considered, either a list of files,
  1413. or a symbol whose function or variable value will be used to retrieve
  1414. a file name or a list of file names. If you use `org-agenda-files' for
  1415. that, all agenda files will be scanned for targets. Nil means consider
  1416. headings in the current buffer.
  1417. - A specification of how to find candidate refile targets. This may be
  1418. any of:
  1419. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1420. This tag has to be present in all target headlines, inheritance will
  1421. not be considered.
  1422. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1423. todo keyword.
  1424. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1425. headlines that are refiling targets.
  1426. - a cons cell (:level . N). Any headline of level N is considered a target.
  1427. Note that, when `org-odd-levels-only' is set, level corresponds to
  1428. order in hierarchy, not to the number of stars.
  1429. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1430. Note that, when `org-odd-levels-only' is set, level corresponds to
  1431. order in hierarchy, not to the number of stars.
  1432. You can set the variable `org-refile-target-verify-function' to a function
  1433. to verify each headline found by the simple critery above.
  1434. When this variable is nil, all top-level headlines in the current buffer
  1435. are used, equivalent to the value `((nil . (:level . 1))'."
  1436. :group 'org-refile
  1437. :type '(repeat
  1438. (cons
  1439. (choice :value org-agenda-files
  1440. (const :tag "All agenda files" org-agenda-files)
  1441. (const :tag "Current buffer" nil)
  1442. (function) (variable) (file))
  1443. (choice :tag "Identify target headline by"
  1444. (cons :tag "Specific tag" (const :value :tag) (string))
  1445. (cons :tag "TODO keyword" (const :value :todo) (string))
  1446. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1447. (cons :tag "Level number" (const :value :level) (integer))
  1448. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1449. (defcustom org-refile-target-verify-function nil
  1450. "Function to verify if the headline at point should be a refile target.
  1451. The function will be called without arguments, with point at the
  1452. beginning of the headline. It should return t and leave point
  1453. where it is if the headline is a valid target for refiling.
  1454. If the target should not be selected, the function must return nil.
  1455. In addition to this, it may move point to a place from where the search
  1456. should be continued. For example, the function may decide that the entire
  1457. subtree of the current entry should be excluded and move point to the end
  1458. of the subtree."
  1459. :group 'org-refile
  1460. :type 'function)
  1461. (defcustom org-refile-use-outline-path nil
  1462. "Non-nil means provide refile targets as paths.
  1463. So a level 3 headline will be available as level1/level2/level3.
  1464. When the value is `file', also include the file name (without directory)
  1465. into the path. In this case, you can also stop the completion after
  1466. the file name, to get entries inserted as top level in the file.
  1467. When `full-file-path', include the full file path."
  1468. :group 'org-refile
  1469. :type '(choice
  1470. (const :tag "Not" nil)
  1471. (const :tag "Yes" t)
  1472. (const :tag "Start with file name" file)
  1473. (const :tag "Start with full file path" full-file-path)))
  1474. (defcustom org-outline-path-complete-in-steps t
  1475. "Non-nil means complete the outline path in hierarchical steps.
  1476. When Org-mode uses the refile interface to select an outline path
  1477. \(see variable `org-refile-use-outline-path'), the completion of
  1478. the path can be done is a single go, or if can be done in steps down
  1479. the headline hierarchy. Going in steps is probably the best if you
  1480. do not use a special completion package like `ido' or `icicles'.
  1481. However, when using these packages, going in one step can be very
  1482. fast, while still showing the whole path to the entry."
  1483. :group 'org-refile
  1484. :type 'boolean)
  1485. (defcustom org-refile-allow-creating-parent-nodes nil
  1486. "Non-nil means allow to create new nodes as refile targets.
  1487. New nodes are then created by adding \"/new node name\" to the completion
  1488. of an existing node. When the value of this variable is `confirm',
  1489. new node creation must be confirmed by the user (recommended)
  1490. When nil, the completion must match an existing entry.
  1491. Note that, if the new heading is not seen by the criteria
  1492. listed in `org-refile-targets', multiple instances of the same
  1493. heading would be created by trying again to file under the new
  1494. heading."
  1495. :group 'org-refile
  1496. :type '(choice
  1497. (const :tag "Never" nil)
  1498. (const :tag "Always" t)
  1499. (const :tag "Prompt for confirmation" confirm)))
  1500. (defgroup org-todo nil
  1501. "Options concerning TODO items in Org-mode."
  1502. :tag "Org TODO"
  1503. :group 'org)
  1504. (defgroup org-progress nil
  1505. "Options concerning Progress logging in Org-mode."
  1506. :tag "Org Progress"
  1507. :group 'org-time)
  1508. (defvar org-todo-interpretation-widgets
  1509. '(
  1510. (:tag "Sequence (cycling hits every state)" sequence)
  1511. (:tag "Type (cycling directly to DONE)" type))
  1512. "The available interpretation symbols for customizing
  1513. `org-todo-keywords'.
  1514. Interested libraries should add to this list.")
  1515. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1516. "List of TODO entry keyword sequences and their interpretation.
  1517. \\<org-mode-map>This is a list of sequences.
  1518. Each sequence starts with a symbol, either `sequence' or `type',
  1519. indicating if the keywords should be interpreted as a sequence of
  1520. action steps, or as different types of TODO items. The first
  1521. keywords are states requiring action - these states will select a headline
  1522. for inclusion into the global TODO list Org-mode produces. If one of
  1523. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1524. signify that no further action is necessary. If \"|\" is not found,
  1525. the last keyword is treated as the only DONE state of the sequence.
  1526. The command \\[org-todo] cycles an entry through these states, and one
  1527. additional state where no keyword is present. For details about this
  1528. cycling, see the manual.
  1529. TODO keywords and interpretation can also be set on a per-file basis with
  1530. the special #+SEQ_TODO and #+TYP_TODO lines.
  1531. Each keyword can optionally specify a character for fast state selection
  1532. \(in combination with the variable `org-use-fast-todo-selection')
  1533. and specifiers for state change logging, using the same syntax
  1534. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1535. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1536. indicates to record a time stamp each time this state is selected.
  1537. Each keyword may also specify if a timestamp or a note should be
  1538. recorded when entering or leaving the state, by adding additional
  1539. characters in the parenthesis after the keyword. This looks like this:
  1540. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1541. record only the time of the state change. With X and Y being either
  1542. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1543. Y when leaving the state if and only if the *target* state does not
  1544. define X. You may omit any of the fast-selection key or X or /Y,
  1545. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1546. For backward compatibility, this variable may also be just a list
  1547. of keywords - in this case the interpretation (sequence or type) will be
  1548. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1549. :group 'org-todo
  1550. :group 'org-keywords
  1551. :type '(choice
  1552. (repeat :tag "Old syntax, just keywords"
  1553. (string :tag "Keyword"))
  1554. (repeat :tag "New syntax"
  1555. (cons
  1556. (choice
  1557. :tag "Interpretation"
  1558. ;;Quick and dirty way to see
  1559. ;;`org-todo-interpretations'. This takes the
  1560. ;;place of item arguments
  1561. :convert-widget
  1562. (lambda (widget)
  1563. (widget-put widget
  1564. :args (mapcar
  1565. #'(lambda (x)
  1566. (widget-convert
  1567. (cons 'const x)))
  1568. org-todo-interpretation-widgets))
  1569. widget))
  1570. (repeat
  1571. (string :tag "Keyword"))))))
  1572. (defvar org-todo-keywords-1 nil
  1573. "All TODO and DONE keywords active in a buffer.")
  1574. (make-variable-buffer-local 'org-todo-keywords-1)
  1575. (defvar org-todo-keywords-for-agenda nil)
  1576. (defvar org-done-keywords-for-agenda nil)
  1577. (defvar org-drawers-for-agenda nil)
  1578. (defvar org-todo-keyword-alist-for-agenda nil)
  1579. (defvar org-tag-alist-for-agenda nil)
  1580. (defvar org-agenda-contributing-files nil)
  1581. (defvar org-not-done-keywords nil)
  1582. (make-variable-buffer-local 'org-not-done-keywords)
  1583. (defvar org-done-keywords nil)
  1584. (make-variable-buffer-local 'org-done-keywords)
  1585. (defvar org-todo-heads nil)
  1586. (make-variable-buffer-local 'org-todo-heads)
  1587. (defvar org-todo-sets nil)
  1588. (make-variable-buffer-local 'org-todo-sets)
  1589. (defvar org-todo-log-states nil)
  1590. (make-variable-buffer-local 'org-todo-log-states)
  1591. (defvar org-todo-kwd-alist nil)
  1592. (make-variable-buffer-local 'org-todo-kwd-alist)
  1593. (defvar org-todo-key-alist nil)
  1594. (make-variable-buffer-local 'org-todo-key-alist)
  1595. (defvar org-todo-key-trigger nil)
  1596. (make-variable-buffer-local 'org-todo-key-trigger)
  1597. (defcustom org-todo-interpretation 'sequence
  1598. "Controls how TODO keywords are interpreted.
  1599. This variable is in principle obsolete and is only used for
  1600. backward compatibility, if the interpretation of todo keywords is
  1601. not given already in `org-todo-keywords'. See that variable for
  1602. more information."
  1603. :group 'org-todo
  1604. :group 'org-keywords
  1605. :type '(choice (const sequence)
  1606. (const type)))
  1607. (defcustom org-use-fast-todo-selection t
  1608. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1609. This variable describes if and under what circumstances the cycling
  1610. mechanism for TODO keywords will be replaced by a single-key, direct
  1611. selection scheme.
  1612. When nil, fast selection is never used.
  1613. When the symbol `prefix', it will be used when `org-todo' is called with
  1614. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1615. in an agenda buffer.
  1616. When t, fast selection is used by default. In this case, the prefix
  1617. argument forces cycling instead.
  1618. In all cases, the special interface is only used if access keys have actually
  1619. been assigned by the user, i.e. if keywords in the configuration are followed
  1620. by a letter in parenthesis, like TODO(t)."
  1621. :group 'org-todo
  1622. :type '(choice
  1623. (const :tag "Never" nil)
  1624. (const :tag "By default" t)
  1625. (const :tag "Only with C-u C-c C-t" prefix)))
  1626. (defcustom org-provide-todo-statistics t
  1627. "Non-nil means update todo statistics after insert and toggle.
  1628. ALL-HEADLINES means update todo statistics by including headlines
  1629. with no TODO keyword as well, counting them as not done.
  1630. A list of TODO keywords means the same, but skip keywords that are
  1631. not in this list.
  1632. When this is set, todo statistics is updated in the parent of the
  1633. current entry each time a todo state is changed."
  1634. :group 'org-todo
  1635. :type '(choice
  1636. (const :tag "Yes, only for TODO entries" t)
  1637. (const :tag "Yes, including all entries" 'all-headlines)
  1638. (repeat :tag "Yes, for TODOs in this list"
  1639. (string :tag "TODO keyword"))
  1640. (other :tag "No TODO statistics" nil)))
  1641. (defcustom org-hierarchical-todo-statistics t
  1642. "Non-nil means TODO statistics covers just direct children.
  1643. When nil, all entries in the subtree are considered.
  1644. This has only an effect if `org-provide-todo-statistics' is set.
  1645. To set this to nil for only a single subtree, use a COOKIE_DATA
  1646. property and include the word \"recursive\" into the value."
  1647. :group 'org-todo
  1648. :type 'boolean)
  1649. (defcustom org-after-todo-state-change-hook nil
  1650. "Hook which is run after the state of a TODO item was changed.
  1651. The new state (a string with a TODO keyword, or nil) is available in the
  1652. Lisp variable `state'."
  1653. :group 'org-todo
  1654. :type 'hook)
  1655. (defvar org-blocker-hook nil
  1656. "Hook for functions that are allowed to block a state change.
  1657. Each function gets as its single argument a property list, see
  1658. `org-trigger-hook' for more information about this list.
  1659. If any of the functions in this hook returns nil, the state change
  1660. is blocked.")
  1661. (defvar org-trigger-hook nil
  1662. "Hook for functions that are triggered by a state change.
  1663. Each function gets as its single argument a property list with at least
  1664. the following elements:
  1665. (:type type-of-change :position pos-at-entry-start
  1666. :from old-state :to new-state)
  1667. Depending on the type, more properties may be present.
  1668. This mechanism is currently implemented for:
  1669. TODO state changes
  1670. ------------------
  1671. :type todo-state-change
  1672. :from previous state (keyword as a string), or nil, or a symbol
  1673. 'todo' or 'done', to indicate the general type of state.
  1674. :to new state, like in :from")
  1675. (defcustom org-enforce-todo-dependencies nil
  1676. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1677. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1678. be blocked if any prior sibling is not yet done.
  1679. Finally, if the parent is blocked because of ordered siblings of its own,
  1680. the child will also be blocked.
  1681. This variable needs to be set before org.el is loaded, and you need to
  1682. restart Emacs after a change to make the change effective. The only way
  1683. to change is while Emacs is running is through the customize interface."
  1684. :set (lambda (var val)
  1685. (set var val)
  1686. (if val
  1687. (add-hook 'org-blocker-hook
  1688. 'org-block-todo-from-children-or-siblings-or-parent)
  1689. (remove-hook 'org-blocker-hook
  1690. 'org-block-todo-from-children-or-siblings-or-parent)))
  1691. :group 'org-todo
  1692. :type 'boolean)
  1693. (defcustom org-enforce-todo-checkbox-dependencies nil
  1694. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1695. When this is nil, checkboxes have no influence on switching TODO states.
  1696. When non-nil, you first need to check off all check boxes before the TODO
  1697. entry can be switched to DONE.
  1698. This variable needs to be set before org.el is loaded, and you need to
  1699. restart Emacs after a change to make the change effective. The only way
  1700. to change is while Emacs is running is through the customize interface."
  1701. :set (lambda (var val)
  1702. (set var val)
  1703. (if val
  1704. (add-hook 'org-blocker-hook
  1705. 'org-block-todo-from-checkboxes)
  1706. (remove-hook 'org-blocker-hook
  1707. 'org-block-todo-from-checkboxes)))
  1708. :group 'org-todo
  1709. :type 'boolean)
  1710. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1711. "Non-nil means inserting a TODO heading is treated as state change.
  1712. So when the command \\[org-insert-todo-heading] is used, state change
  1713. logging will apply if appropriate. When nil, the new TODO item will
  1714. be inserted directly, and no logging will take place."
  1715. :group 'org-todo
  1716. :type 'boolean)
  1717. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1718. "Non-nil means switching TODO states with S-cursor counts as state change.
  1719. This is the default behavior. However, setting this to nil allows a
  1720. convenient way to select a TODO state and bypass any logging associated
  1721. with that."
  1722. :group 'org-todo
  1723. :type 'boolean)
  1724. (defcustom org-todo-state-tags-triggers nil
  1725. "Tag changes that should be triggered by TODO state changes.
  1726. This is a list. Each entry is
  1727. (state-change (tag . flag) .......)
  1728. State-change can be a string with a state, and empty string to indicate the
  1729. state that has no TODO keyword, or it can be one of the symbols `todo'
  1730. or `done', meaning any not-done or done state, respectively."
  1731. :group 'org-todo
  1732. :group 'org-tags
  1733. :type '(repeat
  1734. (cons (choice :tag "When changing to"
  1735. (const :tag "Not-done state" todo)
  1736. (const :tag "Done state" done)
  1737. (string :tag "State"))
  1738. (repeat
  1739. (cons :tag "Tag action"
  1740. (string :tag "Tag")
  1741. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1742. (defcustom org-log-done nil
  1743. "Information to record when a task moves to the DONE state.
  1744. Possible values are:
  1745. nil Don't add anything, just change the keyword
  1746. time Add a time stamp to the task
  1747. note Prompt for a note and add it with template `org-log-note-headings'
  1748. This option can also be set with on a per-file-basis with
  1749. #+STARTUP: nologdone
  1750. #+STARTUP: logdone
  1751. #+STARTUP: lognotedone
  1752. You can have local logging settings for a subtree by setting the LOGGING
  1753. property to one or more of these keywords."
  1754. :group 'org-todo
  1755. :group 'org-progress
  1756. :type '(choice
  1757. (const :tag "No logging" nil)
  1758. (const :tag "Record CLOSED timestamp" time)
  1759. (const :tag "Record CLOSED timestamp with note." note)))
  1760. ;; Normalize old uses of org-log-done.
  1761. (cond
  1762. ((eq org-log-done t) (setq org-log-done 'time))
  1763. ((and (listp org-log-done) (memq 'done org-log-done))
  1764. (setq org-log-done 'note)))
  1765. (defcustom org-log-reschedule nil
  1766. "Information to record when the scheduling date of a tasks is modified.
  1767. Possible values are:
  1768. nil Don't add anything, just change the date
  1769. time Add a time stamp to the task
  1770. note Prompt for a note and add it with template `org-log-note-headings'
  1771. This option can also be set with on a per-file-basis with
  1772. #+STARTUP: nologreschedule
  1773. #+STARTUP: logreschedule
  1774. #+STARTUP: lognotereschedule"
  1775. :group 'org-todo
  1776. :group 'org-progress
  1777. :type '(choice
  1778. (const :tag "No logging" nil)
  1779. (const :tag "Record timestamp" time)
  1780. (const :tag "Record timestamp with note." note)))
  1781. (defcustom org-log-redeadline nil
  1782. "Information to record when the deadline date of a tasks is modified.
  1783. Possible values are:
  1784. nil Don't add anything, just change the date
  1785. time Add a time stamp to the task
  1786. note Prompt for a note and add it with template `org-log-note-headings'
  1787. This option can also be set with on a per-file-basis with
  1788. #+STARTUP: nologredeadline
  1789. #+STARTUP: logredeadline
  1790. #+STARTUP: lognoteredeadline
  1791. You can have local logging settings for a subtree by setting the LOGGING
  1792. property to one or more of these keywords."
  1793. :group 'org-todo
  1794. :group 'org-progress
  1795. :type '(choice
  1796. (const :tag "No logging" nil)
  1797. (const :tag "Record timestamp" time)
  1798. (const :tag "Record timestamp with note." note)))
  1799. (defcustom org-log-note-clock-out nil
  1800. "Non-nil means record a note when clocking out of an item.
  1801. This can also be configured on a per-file basis by adding one of
  1802. the following lines anywhere in the buffer:
  1803. #+STARTUP: lognoteclock-out
  1804. #+STARTUP: nolognoteclock-out"
  1805. :group 'org-todo
  1806. :group 'org-progress
  1807. :type 'boolean)
  1808. (defcustom org-log-done-with-time t
  1809. "Non-nil means the CLOSED time stamp will contain date and time.
  1810. When nil, only the date will be recorded."
  1811. :group 'org-progress
  1812. :type 'boolean)
  1813. (defcustom org-log-note-headings
  1814. '((done . "CLOSING NOTE %t")
  1815. (state . "State %-12s from %-12S %t")
  1816. (note . "Note taken on %t")
  1817. (reschedule . "Rescheduled from %S on %t")
  1818. (delschedule . "Not scheduled, was %S on %t")
  1819. (redeadline . "New deadline from %S on %t")
  1820. (deldeadline . "Removed deadline, was %S on %t")
  1821. (refile . "Refiled on %t")
  1822. (clock-out . ""))
  1823. "Headings for notes added to entries.
  1824. The value is an alist, with the car being a symbol indicating the note
  1825. context, and the cdr is the heading to be used. The heading may also be the
  1826. empty string.
  1827. %t in the heading will be replaced by a time stamp.
  1828. %s will be replaced by the new TODO state, in double quotes.
  1829. %S will be replaced by the old TODO state, in double quotes.
  1830. %u will be replaced by the user name.
  1831. %U will be replaced by the full user name.
  1832. In fact, it is not a good idea to change the `state' entry, because
  1833. agenda log mode depends on the format of these entries."
  1834. :group 'org-todo
  1835. :group 'org-progress
  1836. :type '(list :greedy t
  1837. (cons (const :tag "Heading when closing an item" done) string)
  1838. (cons (const :tag
  1839. "Heading when changing todo state (todo sequence only)"
  1840. state) string)
  1841. (cons (const :tag "Heading when just taking a note" note) string)
  1842. (cons (const :tag "Heading when clocking out" clock-out) string)
  1843. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1844. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1845. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1846. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1847. (cons (const :tag "Heading when refiling" refile) string)))
  1848. (unless (assq 'note org-log-note-headings)
  1849. (push '(note . "%t") org-log-note-headings))
  1850. (defcustom org-log-into-drawer nil
  1851. "Non-nil means insert state change notes and time stamps into a drawer.
  1852. When nil, state changes notes will be inserted after the headline and
  1853. any scheduling and clock lines, but not inside a drawer.
  1854. The value of this variable should be the name of the drawer to use.
  1855. LOGBOOK is proposed at the default drawer for this purpose, you can
  1856. also set this to a string to define the drawer of your choice.
  1857. A value of t is also allowed, representing \"LOGBOOK\".
  1858. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1859. will be ignored.
  1860. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1861. a subtree."
  1862. :group 'org-todo
  1863. :group 'org-progress
  1864. :type '(choice
  1865. (const :tag "Not into a drawer" nil)
  1866. (const :tag "LOGBOOK" t)
  1867. (string :tag "Other")))
  1868. (if (fboundp 'defvaralias)
  1869. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1870. (defun org-log-into-drawer ()
  1871. "Return the value of `org-log-into-drawer', but let properties overrule.
  1872. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1873. used instead of the default value."
  1874. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1875. (cond
  1876. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1877. ((equal p "t") "LOGBOOK")
  1878. (t p))))
  1879. (defcustom org-log-state-notes-insert-after-drawers nil
  1880. "Non-nil means insert state change notes after any drawers in entry.
  1881. Only the drawers that *immediately* follow the headline and the
  1882. deadline/scheduled line are skipped.
  1883. When nil, insert notes right after the heading and perhaps the line
  1884. with deadline/scheduling if present.
  1885. This variable will have no effect if `org-log-into-drawer' is
  1886. set."
  1887. :group 'org-todo
  1888. :group 'org-progress
  1889. :type 'boolean)
  1890. (defcustom org-log-states-order-reversed t
  1891. "Non-nil means the latest state note will be directly after heading.
  1892. When nil, the state change notes will be ordered according to time."
  1893. :group 'org-todo
  1894. :group 'org-progress
  1895. :type 'boolean)
  1896. (defcustom org-todo-repeat-to-state nil
  1897. "The TODO state to which a repeater should return the repeating task.
  1898. By default this is the first task in a TODO sequence, or the previous state
  1899. in a TODO_TYP set. But you can specify another task here.
  1900. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  1901. :group 'org-todo
  1902. :type '(choice (const :tag "Head of sequence" nil)
  1903. (string :tag "Specific state")))
  1904. (defcustom org-log-repeat 'time
  1905. "Non-nil means record moving through the DONE state when triggering repeat.
  1906. An auto-repeating task is immediately switched back to TODO when
  1907. marked DONE. If you are not logging state changes (by adding \"@\"
  1908. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1909. record a closing note, there will be no record of the task moving
  1910. through DONE. This variable forces taking a note anyway.
  1911. nil Don't force a record
  1912. time Record a time stamp
  1913. note Record a note
  1914. This option can also be set with on a per-file-basis with
  1915. #+STARTUP: logrepeat
  1916. #+STARTUP: lognoterepeat
  1917. #+STARTUP: nologrepeat
  1918. You can have local logging settings for a subtree by setting the LOGGING
  1919. property to one or more of these keywords."
  1920. :group 'org-todo
  1921. :group 'org-progress
  1922. :type '(choice
  1923. (const :tag "Don't force a record" nil)
  1924. (const :tag "Force recording the DONE state" time)
  1925. (const :tag "Force recording a note with the DONE state" note)))
  1926. (defgroup org-priorities nil
  1927. "Priorities in Org-mode."
  1928. :tag "Org Priorities"
  1929. :group 'org-todo)
  1930. (defcustom org-enable-priority-commands t
  1931. "Non-nil means priority commands are active.
  1932. When nil, these commands will be disabled, so that you never accidentally
  1933. set a priority."
  1934. :group 'org-priorities
  1935. :type 'boolean)
  1936. (defcustom org-highest-priority ?A
  1937. "The highest priority of TODO items. A character like ?A, ?B etc.
  1938. Must have a smaller ASCII number than `org-lowest-priority'."
  1939. :group 'org-priorities
  1940. :type 'character)
  1941. (defcustom org-lowest-priority ?C
  1942. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1943. Must have a larger ASCII number than `org-highest-priority'."
  1944. :group 'org-priorities
  1945. :type 'character)
  1946. (defcustom org-default-priority ?B
  1947. "The default priority of TODO items.
  1948. This is the priority an item get if no explicit priority is given."
  1949. :group 'org-priorities
  1950. :type 'character)
  1951. (defcustom org-priority-start-cycle-with-default t
  1952. "Non-nil means start with default priority when starting to cycle.
  1953. When this is nil, the first step in the cycle will be (depending on the
  1954. command used) one higher or lower that the default priority."
  1955. :group 'org-priorities
  1956. :type 'boolean)
  1957. (defgroup org-time nil
  1958. "Options concerning time stamps and deadlines in Org-mode."
  1959. :tag "Org Time"
  1960. :group 'org)
  1961. (defcustom org-insert-labeled-timestamps-at-point nil
  1962. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  1963. When nil, these labeled time stamps are forces into the second line of an
  1964. entry, just after the headline. When scheduling from the global TODO list,
  1965. the time stamp will always be forced into the second line."
  1966. :group 'org-time
  1967. :type 'boolean)
  1968. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1969. "Formats for `format-time-string' which are used for time stamps.
  1970. It is not recommended to change this constant.")
  1971. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1972. "Number of minutes to round time stamps to.
  1973. These are two values, the first applies when first creating a time stamp.
  1974. The second applies when changing it with the commands `S-up' and `S-down'.
  1975. When changing the time stamp, this means that it will change in steps
  1976. of N minutes, as given by the second value.
  1977. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1978. numbers should be factors of 60, so for example 5, 10, 15.
  1979. When this is larger than 1, you can still force an exact time-stamp by using
  1980. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1981. and by using a prefix arg to `S-up/down' to specify the exact number
  1982. of minutes to shift."
  1983. :group 'org-time
  1984. :get '(lambda (var) ; Make sure all entries have 5 elements
  1985. (if (integerp (default-value var))
  1986. (list (default-value var) 5)
  1987. (default-value var)))
  1988. :type '(list
  1989. (integer :tag "when inserting times")
  1990. (integer :tag "when modifying times")))
  1991. ;; Normalize old customizations of this variable.
  1992. (when (integerp org-time-stamp-rounding-minutes)
  1993. (setq org-time-stamp-rounding-minutes
  1994. (list org-time-stamp-rounding-minutes
  1995. org-time-stamp-rounding-minutes)))
  1996. (defcustom org-display-custom-times nil
  1997. "Non-nil means overlay custom formats over all time stamps.
  1998. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1999. To turn this on on a per-file basis, insert anywhere in the file:
  2000. #+STARTUP: customtime"
  2001. :group 'org-time
  2002. :set 'set-default
  2003. :type 'sexp)
  2004. (make-variable-buffer-local 'org-display-custom-times)
  2005. (defcustom org-time-stamp-custom-formats
  2006. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2007. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2008. These are overlayed over the default ISO format if the variable
  2009. `org-display-custom-times' is set. Time like %H:%M should be at the
  2010. end of the second format. The custom formats are also honored by export
  2011. commands, if custom time display is turned on at the time of export."
  2012. :group 'org-time
  2013. :type 'sexp)
  2014. (defun org-time-stamp-format (&optional long inactive)
  2015. "Get the right format for a time string."
  2016. (let ((f (if long (cdr org-time-stamp-formats)
  2017. (car org-time-stamp-formats))))
  2018. (if inactive
  2019. (concat "[" (substring f 1 -1) "]")
  2020. f)))
  2021. (defcustom org-time-clocksum-format "%d:%02d"
  2022. "The format string used when creating CLOCKSUM lines, or when
  2023. org-mode generates a time duration."
  2024. :group 'org-time
  2025. :type 'string)
  2026. (defcustom org-time-clocksum-use-fractional nil
  2027. "If non-nil, \\[org-clock-display] uses fractional times.
  2028. org-mode generates a time duration."
  2029. :group 'org-time
  2030. :type 'boolean)
  2031. (defcustom org-time-clocksum-fractional-format "%.2f"
  2032. "The format string used when creating CLOCKSUM lines, or when
  2033. org-mode generates a time duration."
  2034. :group 'org-time
  2035. :type 'string)
  2036. (defcustom org-deadline-warning-days 14
  2037. "No. of days before expiration during which a deadline becomes active.
  2038. This variable governs the display in sparse trees and in the agenda.
  2039. When 0 or negative, it means use this number (the absolute value of it)
  2040. even if a deadline has a different individual lead time specified.
  2041. Custom commands can set this variable in the options section."
  2042. :group 'org-time
  2043. :group 'org-agenda-daily/weekly
  2044. :type 'integer)
  2045. (defcustom org-read-date-prefer-future t
  2046. "Non-nil means assume future for incomplete date input from user.
  2047. This affects the following situations:
  2048. 1. The user gives a month but not a year.
  2049. For example, if it is april and you enter \"feb 2\", this will be read
  2050. as feb 2, *next* year. \"May 5\", however, will be this year.
  2051. 2. The user gives a day, but no month.
  2052. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2053. read this as the third of *next* month. However, if you enter \"17\",
  2054. it will be considered as *this* month.
  2055. If you set this variable to the symbol `time', then also the following
  2056. will work:
  2057. 3. If the user gives a time, but no day. If the time is before now,
  2058. to will be interpreted as tomorrow.
  2059. Currently none of this works for ISO week specifications.
  2060. When this option is nil, the current day, month and year will always be
  2061. used as defaults."
  2062. :group 'org-time
  2063. :type '(choice
  2064. (const :tag "Never" nil)
  2065. (const :tag "Check month and day" t)
  2066. (const :tag "Check month, day, and time" time)))
  2067. (defcustom org-read-date-display-live t
  2068. "Non-nil means display current interpretation of date prompt live.
  2069. This display will be in an overlay, in the minibuffer."
  2070. :group 'org-time
  2071. :type 'boolean)
  2072. (defcustom org-read-date-popup-calendar t
  2073. "Non-nil means pop up a calendar when prompting for a date.
  2074. In the calendar, the date can be selected with mouse-1. However, the
  2075. minibuffer will also be active, and you can simply enter the date as well.
  2076. When nil, only the minibuffer will be available."
  2077. :group 'org-time
  2078. :type 'boolean)
  2079. (if (fboundp 'defvaralias)
  2080. (defvaralias 'org-popup-calendar-for-date-prompt
  2081. 'org-read-date-popup-calendar))
  2082. (defcustom org-read-date-minibuffer-setup-hook nil
  2083. "Hook to be used to set up keys for the date/time interface.
  2084. Add key definitions to `minibuffer-local-map', which will be a temporary
  2085. copy."
  2086. :group 'org-time
  2087. :type 'hook)
  2088. (defcustom org-extend-today-until 0
  2089. "The hour when your day really ends. Must be an integer.
  2090. This has influence for the following applications:
  2091. - When switching the agenda to \"today\". It it is still earlier than
  2092. the time given here, the day recognized as TODAY is actually yesterday.
  2093. - When a date is read from the user and it is still before the time given
  2094. here, the current date and time will be assumed to be yesterday, 23:59.
  2095. Also, timestamps inserted in remember templates follow this rule.
  2096. IMPORTANT: This is a feature whose implementation is and likely will
  2097. remain incomplete. Really, it is only here because past midnight seems to
  2098. be the favorite working time of John Wiegley :-)"
  2099. :group 'org-time
  2100. :type 'integer)
  2101. (defcustom org-edit-timestamp-down-means-later nil
  2102. "Non-nil means S-down will increase the time in a time stamp.
  2103. When nil, S-up will increase."
  2104. :group 'org-time
  2105. :type 'boolean)
  2106. (defcustom org-calendar-follow-timestamp-change t
  2107. "Non-nil means make the calendar window follow timestamp changes.
  2108. When a timestamp is modified and the calendar window is visible, it will be
  2109. moved to the new date."
  2110. :group 'org-time
  2111. :type 'boolean)
  2112. (defgroup org-tags nil
  2113. "Options concerning tags in Org-mode."
  2114. :tag "Org Tags"
  2115. :group 'org)
  2116. (defcustom org-tag-alist nil
  2117. "List of tags allowed in Org-mode files.
  2118. When this list is nil, Org-mode will base TAG input on what is already in the
  2119. buffer.
  2120. The value of this variable is an alist, the car of each entry must be a
  2121. keyword as a string, the cdr may be a character that is used to select
  2122. that tag through the fast-tag-selection interface.
  2123. See the manual for details."
  2124. :group 'org-tags
  2125. :type '(repeat
  2126. (choice
  2127. (cons (string :tag "Tag name")
  2128. (character :tag "Access char"))
  2129. (list :tag "Start radio group"
  2130. (const :startgroup)
  2131. (option (string :tag "Group description")))
  2132. (list :tag "End radio group"
  2133. (const :endgroup)
  2134. (option (string :tag "Group description")))
  2135. (const :tag "New line" (:newline)))))
  2136. (defcustom org-tag-persistent-alist nil
  2137. "List of tags that will always appear in all Org-mode files.
  2138. This is in addition to any in buffer settings or customizations
  2139. of `org-tag-alist'.
  2140. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2141. The value of this variable is an alist, the car of each entry must be a
  2142. keyword as a string, the cdr may be a character that is used to select
  2143. that tag through the fast-tag-selection interface.
  2144. See the manual for details.
  2145. To disable these tags on a per-file basis, insert anywhere in the file:
  2146. #+STARTUP: noptag"
  2147. :group 'org-tags
  2148. :type '(repeat
  2149. (choice
  2150. (cons (string :tag "Tag name")
  2151. (character :tag "Access char"))
  2152. (const :tag "Start radio group" (:startgroup))
  2153. (const :tag "End radio group" (:endgroup))
  2154. (const :tag "New line" (:newline)))))
  2155. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2156. "If non-nil, always offer completion for all tags of all agenda files.
  2157. Instead of customizing this variable directly, you might want to
  2158. set it locally for remember buffers, because there no list of
  2159. tags in that file can be created dynamically (there are none).
  2160. (add-hook 'org-remember-mode-hook
  2161. (lambda ()
  2162. (set (make-local-variable
  2163. 'org-complete-tags-always-offer-all-agenda-tags)
  2164. t)))"
  2165. :group 'org-tags
  2166. :type 'boolean)
  2167. (defvar org-file-tags nil
  2168. "List of tags that can be inherited by all entries in the file.
  2169. The tags will be inherited if the variable `org-use-tag-inheritance'
  2170. says they should be.
  2171. This variable is populated from #+FILETAGS lines.")
  2172. (defcustom org-use-fast-tag-selection 'auto
  2173. "Non-nil means use fast tag selection scheme.
  2174. This is a special interface to select and deselect tags with single keys.
  2175. When nil, fast selection is never used.
  2176. When the symbol `auto', fast selection is used if and only if selection
  2177. characters for tags have been configured, either through the variable
  2178. `org-tag-alist' or through a #+TAGS line in the buffer.
  2179. When t, fast selection is always used and selection keys are assigned
  2180. automatically if necessary."
  2181. :group 'org-tags
  2182. :type '(choice
  2183. (const :tag "Always" t)
  2184. (const :tag "Never" nil)
  2185. (const :tag "When selection characters are configured" 'auto)))
  2186. (defcustom org-fast-tag-selection-single-key nil
  2187. "Non-nil means fast tag selection exits after first change.
  2188. When nil, you have to press RET to exit it.
  2189. During fast tag selection, you can toggle this flag with `C-c'.
  2190. This variable can also have the value `expert'. In this case, the window
  2191. displaying the tags menu is not even shown, until you press C-c again."
  2192. :group 'org-tags
  2193. :type '(choice
  2194. (const :tag "No" nil)
  2195. (const :tag "Yes" t)
  2196. (const :tag "Expert" expert)))
  2197. (defvar org-fast-tag-selection-include-todo nil
  2198. "Non-nil means fast tags selection interface will also offer TODO states.
  2199. This is an undocumented feature, you should not rely on it.")
  2200. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2201. "The column to which tags should be indented in a headline.
  2202. If this number is positive, it specifies the column. If it is negative,
  2203. it means that the tags should be flushright to that column. For example,
  2204. -80 works well for a normal 80 character screen."
  2205. :group 'org-tags
  2206. :type 'integer)
  2207. (defcustom org-auto-align-tags t
  2208. "Non-nil means realign tags after pro/demotion of TODO state change.
  2209. These operations change the length of a headline and therefore shift
  2210. the tags around. With this options turned on, after each such operation
  2211. the tags are again aligned to `org-tags-column'."
  2212. :group 'org-tags
  2213. :type 'boolean)
  2214. (defcustom org-use-tag-inheritance t
  2215. "Non-nil means tags in levels apply also for sublevels.
  2216. When nil, only the tags directly given in a specific line apply there.
  2217. This may also be a list of tags that should be inherited, or a regexp that
  2218. matches tags that should be inherited. Additional control is possible
  2219. with the variable `org-tags-exclude-from-inheritance' which gives an
  2220. explicit list of tags to be excluded from inheritance., even if the value of
  2221. `org-use-tag-inheritance' would select it for inheritance.
  2222. If this option is t, a match early-on in a tree can lead to a large
  2223. number of matches in the subtree when constructing the agenda or creating
  2224. a sparse tree. If you only want to see the first match in a tree during
  2225. a search, check out the variable `org-tags-match-list-sublevels'."
  2226. :group 'org-tags
  2227. :type '(choice
  2228. (const :tag "Not" nil)
  2229. (const :tag "Always" t)
  2230. (repeat :tag "Specific tags" (string :tag "Tag"))
  2231. (regexp :tag "Tags matched by regexp")))
  2232. (defcustom org-tags-exclude-from-inheritance nil
  2233. "List of tags that should never be inherited.
  2234. This is a way to exclude a few tags from inheritance. For way to do
  2235. the opposite, to actively allow inheritance for selected tags,
  2236. see the variable `org-use-tag-inheritance'."
  2237. :group 'org-tags
  2238. :type '(repeat (string :tag "Tag")))
  2239. (defun org-tag-inherit-p (tag)
  2240. "Check if TAG is one that should be inherited."
  2241. (cond
  2242. ((member tag org-tags-exclude-from-inheritance) nil)
  2243. ((eq org-use-tag-inheritance t) t)
  2244. ((not org-use-tag-inheritance) nil)
  2245. ((stringp org-use-tag-inheritance)
  2246. (string-match org-use-tag-inheritance tag))
  2247. ((listp org-use-tag-inheritance)
  2248. (member tag org-use-tag-inheritance))
  2249. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2250. (defcustom org-tags-match-list-sublevels t
  2251. "Non-nil means list also sublevels of headlines matching a search.
  2252. This variable applies to tags/property searches, and also to stuck
  2253. projects because this search is based on a tags match as well.
  2254. When set to the symbol `indented', sublevels are indented with
  2255. leading dots.
  2256. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2257. the sublevels of a headline matching a tag search often also match
  2258. the same search. Listing all of them can create very long lists.
  2259. Setting this variable to nil causes subtrees of a match to be skipped.
  2260. This variable is semi-obsolete and probably should always be true. It
  2261. is better to limit inheritance to certain tags using the variables
  2262. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2263. :group 'org-tags
  2264. :type '(choice
  2265. (const :tag "No, don't list them" nil)
  2266. (const :tag "Yes, do list them" t)
  2267. (const :tag "List them, indented with leading dots" indented)))
  2268. (defcustom org-tags-sort-function nil
  2269. "When set, tags are sorted using this function as a comparator"
  2270. :group 'org-tags
  2271. :type '(choice
  2272. (const :tag "No sorting" nil)
  2273. (const :tag "Alphabetical" string<)
  2274. (const :tag "Reverse alphabetical" string>)
  2275. (function :tag "Custom function" nil)))
  2276. (defvar org-tags-history nil
  2277. "History of minibuffer reads for tags.")
  2278. (defvar org-last-tags-completion-table nil
  2279. "The last used completion table for tags.")
  2280. (defvar org-after-tags-change-hook nil
  2281. "Hook that is run after the tags in a line have changed.")
  2282. (defgroup org-properties nil
  2283. "Options concerning properties in Org-mode."
  2284. :tag "Org Properties"
  2285. :group 'org)
  2286. (defcustom org-property-format "%-10s %s"
  2287. "How property key/value pairs should be formatted by `indent-line'.
  2288. When `indent-line' hits a property definition, it will format the line
  2289. according to this format, mainly to make sure that the values are
  2290. lined-up with respect to each other."
  2291. :group 'org-properties
  2292. :type 'string)
  2293. (defcustom org-use-property-inheritance nil
  2294. "Non-nil means properties apply also for sublevels.
  2295. This setting is chiefly used during property searches. Turning it on can
  2296. cause significant overhead when doing a search, which is why it is not
  2297. on by default.
  2298. When nil, only the properties directly given in the current entry count.
  2299. When t, every property is inherited. The value may also be a list of
  2300. properties that should have inheritance, or a regular expression matching
  2301. properties that should be inherited.
  2302. However, note that some special properties use inheritance under special
  2303. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2304. and the properties ending in \"_ALL\" when they are used as descriptor
  2305. for valid values of a property.
  2306. Note for programmers:
  2307. When querying an entry with `org-entry-get', you can control if inheritance
  2308. should be used. By default, `org-entry-get' looks only at the local
  2309. properties. You can request inheritance by setting the inherit argument
  2310. to t (to force inheritance) or to `selective' (to respect the setting
  2311. in this variable)."
  2312. :group 'org-properties
  2313. :type '(choice
  2314. (const :tag "Not" nil)
  2315. (const :tag "Always" t)
  2316. (repeat :tag "Specific properties" (string :tag "Property"))
  2317. (regexp :tag "Properties matched by regexp")))
  2318. (defun org-property-inherit-p (property)
  2319. "Check if PROPERTY is one that should be inherited."
  2320. (cond
  2321. ((eq org-use-property-inheritance t) t)
  2322. ((not org-use-property-inheritance) nil)
  2323. ((stringp org-use-property-inheritance)
  2324. (string-match org-use-property-inheritance property))
  2325. ((listp org-use-property-inheritance)
  2326. (member property org-use-property-inheritance))
  2327. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2328. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2329. "The default column format, if no other format has been defined.
  2330. This variable can be set on the per-file basis by inserting a line
  2331. #+COLUMNS: %25ITEM ....."
  2332. :group 'org-properties
  2333. :type 'string)
  2334. (defcustom org-columns-ellipses ".."
  2335. "The ellipses to be used when a field in column view is truncated.
  2336. When this is the empty string, as many characters as possible are shown,
  2337. but then there will be no visual indication that the field has been truncated.
  2338. When this is a string of length N, the last N characters of a truncated
  2339. field are replaced by this string. If the column is narrower than the
  2340. ellipses string, only part of the ellipses string will be shown."
  2341. :group 'org-properties
  2342. :type 'string)
  2343. (defcustom org-columns-modify-value-for-display-function nil
  2344. "Function that modifies values for display in column view.
  2345. For example, it can be used to cut out a certain part from a time stamp.
  2346. The function must take 2 arguments:
  2347. column-title The title of the column (*not* the property name)
  2348. value The value that should be modified.
  2349. The function should return the value that should be displayed,
  2350. or nil if the normal value should be used."
  2351. :group 'org-properties
  2352. :type 'function)
  2353. (defcustom org-effort-property "Effort"
  2354. "The property that is being used to keep track of effort estimates.
  2355. Effort estimates given in this property need to have the format H:MM."
  2356. :group 'org-properties
  2357. :group 'org-progress
  2358. :type '(string :tag "Property"))
  2359. (defconst org-global-properties-fixed
  2360. '(("VISIBILITY_ALL" . "folded children content all")
  2361. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2362. "List of property/value pairs that can be inherited by any entry.
  2363. These are fixed values, for the preset properties. The user variable
  2364. that can be used to add to this list is `org-global-properties'.
  2365. The entries in this list are cons cells where the car is a property
  2366. name and cdr is a string with the value. If the value represents
  2367. multiple items like an \"_ALL\" property, separate the items by
  2368. spaces.")
  2369. (defcustom org-global-properties nil
  2370. "List of property/value pairs that can be inherited by any entry.
  2371. This list will be combined with the constant `org-global-properties-fixed'.
  2372. The entries in this list are cons cells where the car is a property
  2373. name and cdr is a string with the value.
  2374. You can set buffer-local values for the same purpose in the variable
  2375. `org-file-properties' this by adding lines like
  2376. #+PROPERTY: NAME VALUE"
  2377. :group 'org-properties
  2378. :type '(repeat
  2379. (cons (string :tag "Property")
  2380. (string :tag "Value"))))
  2381. (defvar org-file-properties nil
  2382. "List of property/value pairs that can be inherited by any entry.
  2383. Valid for the current buffer.
  2384. This variable is populated from #+PROPERTY lines.")
  2385. (make-variable-buffer-local 'org-file-properties)
  2386. (defgroup org-agenda nil
  2387. "Options concerning agenda views in Org-mode."
  2388. :tag "Org Agenda"
  2389. :group 'org)
  2390. (defvar org-category nil
  2391. "Variable used by org files to set a category for agenda display.
  2392. Such files should use a file variable to set it, for example
  2393. # -*- mode: org; org-category: \"ELisp\"
  2394. or contain a special line
  2395. #+CATEGORY: ELisp
  2396. If the file does not specify a category, then file's base name
  2397. is used instead.")
  2398. (make-variable-buffer-local 'org-category)
  2399. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2400. (defcustom org-agenda-files nil
  2401. "The files to be used for agenda display.
  2402. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2403. \\[org-remove-file]. You can also use customize to edit the list.
  2404. If an entry is a directory, all files in that directory that are matched by
  2405. `org-agenda-file-regexp' will be part of the file list.
  2406. If the value of the variable is not a list but a single file name, then
  2407. the list of agenda files is actually stored and maintained in that file, one
  2408. agenda file per line. In this file paths can be given relative to
  2409. `org-directory'. Tilde expansion and environment variable substitution
  2410. are also made."
  2411. :group 'org-agenda
  2412. :type '(choice
  2413. (repeat :tag "List of files and directories" file)
  2414. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2415. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2416. "Regular expression to match files for `org-agenda-files'.
  2417. If any element in the list in that variable contains a directory instead
  2418. of a normal file, all files in that directory that are matched by this
  2419. regular expression will be included."
  2420. :group 'org-agenda
  2421. :type 'regexp)
  2422. (defcustom org-agenda-text-search-extra-files nil
  2423. "List of extra files to be searched by text search commands.
  2424. These files will be search in addition to the agenda files by the
  2425. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2426. Note that these files will only be searched for text search commands,
  2427. not for the other agenda views like todo lists, tag searches or the weekly
  2428. agenda. This variable is intended to list notes and possibly archive files
  2429. that should also be searched by these two commands.
  2430. In fact, if the first element in the list is the symbol `agenda-archives',
  2431. than all archive files of all agenda files will be added to the search
  2432. scope."
  2433. :group 'org-agenda
  2434. :type '(set :greedy t
  2435. (const :tag "Agenda Archives" agenda-archives)
  2436. (repeat :inline t (file))))
  2437. (if (fboundp 'defvaralias)
  2438. (defvaralias 'org-agenda-multi-occur-extra-files
  2439. 'org-agenda-text-search-extra-files))
  2440. (defcustom org-agenda-skip-unavailable-files nil
  2441. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2442. A nil value means to remove them, after a query, from the list."
  2443. :group 'org-agenda
  2444. :type 'boolean)
  2445. (defcustom org-calendar-to-agenda-key [?c]
  2446. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2447. The command `org-calendar-goto-agenda' will be bound to this key. The
  2448. default is the character `c' because then `c' can be used to switch back and
  2449. forth between agenda and calendar."
  2450. :group 'org-agenda
  2451. :type 'sexp)
  2452. (defcustom org-calendar-agenda-action-key [?k]
  2453. "The key to be installed in `calendar-mode-map' for agenda-action.
  2454. The command `org-agenda-action' will be bound to this key. The
  2455. default is the character `k' because we use the same key in the agenda."
  2456. :group 'org-agenda
  2457. :type 'sexp)
  2458. (defcustom org-calendar-insert-diary-entry-key [?i]
  2459. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2460. This option is irrelevant until `org-agenda-diary-file' has been configured
  2461. to point to an Org-mode file. When that is the case, the command
  2462. `org-agenda-diary-entry' will be bound to the key given here, by default
  2463. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2464. if you want to continue doing this, you need to change this to a different
  2465. key."
  2466. :group 'org-agenda
  2467. :type 'sexp)
  2468. (defcustom org-agenda-diary-file 'diary-file
  2469. "File to which to add new entries with the `i' key in agenda and calendar.
  2470. When this is the symbol `diary-file', the functionality in the Emacs
  2471. calendar will be used to add entries to the `diary-file'. But when this
  2472. points to a file, `org-agenda-diary-entry' will be used instead."
  2473. :group 'org-agenda
  2474. :type '(choice
  2475. (const :tag "The standard Emacs diary file" diary-file)
  2476. (file :tag "Special Org file diary entries")))
  2477. (eval-after-load "calendar"
  2478. '(progn
  2479. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2480. 'org-calendar-goto-agenda)
  2481. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2482. 'org-agenda-action)
  2483. (add-hook 'calendar-mode-hook
  2484. (lambda ()
  2485. (unless (eq org-agenda-diary-file 'diary-file)
  2486. (define-key calendar-mode-map
  2487. org-calendar-insert-diary-entry-key
  2488. 'org-agenda-diary-entry))))))
  2489. (defgroup org-latex nil
  2490. "Options for embedding LaTeX code into Org-mode."
  2491. :tag "Org LaTeX"
  2492. :group 'org)
  2493. (defcustom org-format-latex-options
  2494. '(:foreground default :background default :scale 1.0
  2495. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2496. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2497. "Options for creating images from LaTeX fragments.
  2498. This is a property list with the following properties:
  2499. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2500. `default' means use the foreground of the default face.
  2501. :background the background color, or \"Transparent\".
  2502. `default' means use the background of the default face.
  2503. :scale a scaling factor for the size of the images.
  2504. :html-foreground, :html-background, :html-scale
  2505. the same numbers for HTML export.
  2506. :matchers a list indicating which matchers should be used to
  2507. find LaTeX fragments. Valid members of this list are:
  2508. \"begin\" find environments
  2509. \"$1\" find single characters surrounded by $.$
  2510. \"$\" find math expressions surrounded by $...$
  2511. \"$$\" find math expressions surrounded by $$....$$
  2512. \"\\(\" find math expressions surrounded by \\(...\\)
  2513. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2514. :group 'org-latex
  2515. :type 'plist)
  2516. (defcustom org-format-latex-signal-error t
  2517. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2518. When nil, just push out a message."
  2519. :group 'org-latex
  2520. :type 'boolean)
  2521. (defcustom org-format-latex-header "\\documentclass{article}
  2522. \\usepackage[usenames]{color}
  2523. \\usepackage{amsmath}
  2524. \\usepackage[mathscr]{eucal}
  2525. \\pagestyle{empty} % do not remove
  2526. \[PACKAGES]
  2527. \[DEFAULT-PACKAGES]
  2528. % The settings below are copied from fullpage.sty
  2529. \\setlength{\\textwidth}{\\paperwidth}
  2530. \\addtolength{\\textwidth}{-3cm}
  2531. \\setlength{\\oddsidemargin}{1.5cm}
  2532. \\addtolength{\\oddsidemargin}{-2.54cm}
  2533. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2534. \\setlength{\\textheight}{\\paperheight}
  2535. \\addtolength{\\textheight}{-\\headheight}
  2536. \\addtolength{\\textheight}{-\\headsep}
  2537. \\addtolength{\\textheight}{-\\footskip}
  2538. \\addtolength{\\textheight}{-3cm}
  2539. \\setlength{\\topmargin}{1.5cm}
  2540. \\addtolength{\\topmargin}{-2.54cm}"
  2541. "The document header used for processing LaTeX fragments.
  2542. It is imperative that this header make sure that no page number
  2543. appears on the page. The package defined in the variables
  2544. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2545. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2546. will be appended."
  2547. :group 'org-latex
  2548. :type 'string)
  2549. (defvar org-format-latex-header-extra nil)
  2550. ;; The following variables are defined here because is it also used
  2551. ;; when formatting latex fragments. Originally it was part of the
  2552. ;; LaTeX exporter, which is why the name includes "export".
  2553. (defcustom org-export-latex-default-packages-alist
  2554. '(("AUTO" "inputenc")
  2555. ("T1" "fontenc")
  2556. ("" "fixltx2e")
  2557. ("" "graphicx")
  2558. ("" "longtable")
  2559. ("" "float")
  2560. ("" "wrapfig")
  2561. ("" "soul")
  2562. ("" "t1enc")
  2563. ("" "textcomp")
  2564. ("" "marvosym")
  2565. ("" "wasysym")
  2566. ("" "latexsym")
  2567. ("" "amssymb")
  2568. ("" "hyperref")
  2569. "\\tolerance=1000"
  2570. )
  2571. "Alist of default packages to be inserted in the header.
  2572. Change this only if one of the packages here causes an incompatibility
  2573. with another package you are using.
  2574. The packages in this list are needed by one part or another of Org-mode
  2575. to function properly.
  2576. - inputenc, fontenc, t1enc: for basic font and character selection
  2577. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2578. for interpreting the entities in `org-entities'. You can skip some of these
  2579. packages if you don't use any of the symbols in it.
  2580. - graphicx: for including images
  2581. - float, wrapfig: for figure placement
  2582. - longtable: for long tables
  2583. - hyperref: for cross references
  2584. Therefore you should not modify this variable unless you know what you
  2585. are doing. The one reason to change it anyway is that you might be loading
  2586. some other package that conflicts with one of the default packages.
  2587. Each cell is of the format \( \"options\" \"package\" \)."
  2588. :group 'org-export-latex
  2589. :type '(repeat
  2590. (choice
  2591. (string :tag "A line of LaTeX")
  2592. (list :tag "options/package pair"
  2593. (string :tag "options")
  2594. (string :tag "package")))))
  2595. (defcustom org-export-latex-packages-alist nil
  2596. "Alist of packages to be inserted in every LaTeX the header.
  2597. These will be inserted after `org-export-latex-default-packages-alist'.
  2598. Each cell is of the format \( \"options\" \"package\" \).
  2599. Make sure that you only lis packages here which:
  2600. - you want in every file
  2601. - do not conflict with the default packages in
  2602. `org-export-latex-default-packages-alist'
  2603. - do not conflict with the setup in `org-format-latex-header'."
  2604. :group 'org-export-latex
  2605. :type '(repeat
  2606. (choice
  2607. (string :tag "A line of LaTeX")
  2608. (list :tag "options/package pair"
  2609. (string :tag "options")
  2610. (string :tag "package")))))
  2611. (defgroup org-appearance nil
  2612. "Settings for Org-mode appearance."
  2613. :tag "Org Appearance"
  2614. :group 'org)
  2615. (defcustom org-level-color-stars-only nil
  2616. "Non-nil means fontify only the stars in each headline.
  2617. When nil, the entire headline is fontified.
  2618. Changing it requires restart of `font-lock-mode' to become effective
  2619. also in regions already fontified."
  2620. :group 'org-appearance
  2621. :type 'boolean)
  2622. (defcustom org-hide-leading-stars nil
  2623. "Non-nil means hide the first N-1 stars in a headline.
  2624. This works by using the face `org-hide' for these stars. This
  2625. face is white for a light background, and black for a dark
  2626. background. You may have to customize the face `org-hide' to
  2627. make this work.
  2628. Changing it requires restart of `font-lock-mode' to become effective
  2629. also in regions already fontified.
  2630. You may also set this on a per-file basis by adding one of the following
  2631. lines to the buffer:
  2632. #+STARTUP: hidestars
  2633. #+STARTUP: showstars"
  2634. :group 'org-appearance
  2635. :type 'boolean)
  2636. (defcustom org-hidden-keywords nil
  2637. "List of keywords that should be hidden when typed in the org buffer.
  2638. For example, add #+TITLE to this list in order to make the
  2639. document title appear in the buffer without the initial #+TITLE:
  2640. keyword."
  2641. :group 'org-appearance
  2642. :type '(set (const :tag "#+AUTHOR" author)
  2643. (const :tag "#+DATE" date)
  2644. (const :tag "#+EMAIL" email)
  2645. (const :tag "#+TITLE" title)))
  2646. (defcustom org-fontify-done-headline nil
  2647. "Non-nil means change the face of a headline if it is marked DONE.
  2648. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2649. When this is non-nil, the headline after the keyword is set to the
  2650. `org-headline-done' as an additional indication."
  2651. :group 'org-appearance
  2652. :type 'boolean)
  2653. (defcustom org-fontify-emphasized-text t
  2654. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2655. Changing this variable requires a restart of Emacs to take effect."
  2656. :group 'org-appearance
  2657. :type 'boolean)
  2658. (defcustom org-fontify-whole-heading-line nil
  2659. "Non-nil means fontify the whole line for headings.
  2660. This is useful when setting a background color for the
  2661. org-level-* faces."
  2662. :group 'org-appearance
  2663. :type 'boolean)
  2664. (defcustom org-highlight-latex-fragments-and-specials nil
  2665. "Non-nil means fontify what is treated specially by the exporters."
  2666. :group 'org-appearance
  2667. :type 'boolean)
  2668. (defcustom org-hide-emphasis-markers nil
  2669. "Non-nil mean font-lock should hide the emphasis marker characters."
  2670. :group 'org-appearance
  2671. :type 'boolean)
  2672. (defvar org-emph-re nil
  2673. "Regular expression for matching emphasis.")
  2674. (defvar org-verbatim-re nil
  2675. "Regular expression for matching verbatim text.")
  2676. (defvar org-emphasis-regexp-components) ; defined just below
  2677. (defvar org-emphasis-alist) ; defined just below
  2678. (defun org-set-emph-re (var val)
  2679. "Set variable and compute the emphasis regular expression."
  2680. (set var val)
  2681. (when (and (boundp 'org-emphasis-alist)
  2682. (boundp 'org-emphasis-regexp-components)
  2683. org-emphasis-alist org-emphasis-regexp-components)
  2684. (let* ((e org-emphasis-regexp-components)
  2685. (pre (car e))
  2686. (post (nth 1 e))
  2687. (border (nth 2 e))
  2688. (body (nth 3 e))
  2689. (nl (nth 4 e))
  2690. (body1 (concat body "*?"))
  2691. (markers (mapconcat 'car org-emphasis-alist ""))
  2692. (vmarkers (mapconcat
  2693. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2694. org-emphasis-alist "")))
  2695. ;; make sure special characters appear at the right position in the class
  2696. (if (string-match "\\^" markers)
  2697. (setq markers (concat (replace-match "" t t markers) "^")))
  2698. (if (string-match "-" markers)
  2699. (setq markers (concat (replace-match "" t t markers) "-")))
  2700. (if (string-match "\\^" vmarkers)
  2701. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2702. (if (string-match "-" vmarkers)
  2703. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2704. (if (> nl 0)
  2705. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2706. (int-to-string nl) "\\}")))
  2707. ;; Make the regexp
  2708. (setq org-emph-re
  2709. (concat "\\([" pre "]\\|^\\)"
  2710. "\\("
  2711. "\\([" markers "]\\)"
  2712. "\\("
  2713. "[^" border "]\\|"
  2714. "[^" border "]"
  2715. body1
  2716. "[^" border "]"
  2717. "\\)"
  2718. "\\3\\)"
  2719. "\\([" post "]\\|$\\)"))
  2720. (setq org-verbatim-re
  2721. (concat "\\([" pre "]\\|^\\)"
  2722. "\\("
  2723. "\\([" vmarkers "]\\)"
  2724. "\\("
  2725. "[^" border "]\\|"
  2726. "[^" border "]"
  2727. body1
  2728. "[^" border "]"
  2729. "\\)"
  2730. "\\3\\)"
  2731. "\\([" post "]\\|$\\)")))))
  2732. (defcustom org-emphasis-regexp-components
  2733. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2734. "Components used to build the regular expression for emphasis.
  2735. This is a list with 6 entries. Terminology: In an emphasis string
  2736. like \" *strong word* \", we call the initial space PREMATCH, the final
  2737. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2738. and \"trong wor\" is the body. The different components in this variable
  2739. specify what is allowed/forbidden in each part:
  2740. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2741. post Chars allowed as postmatch. End of line will be allowed too.
  2742. border The chars *forbidden* as border characters.
  2743. body-regexp A regexp like \".\" to match a body character. Don't use
  2744. non-shy groups here, and don't allow newline here.
  2745. newline The maximum number of newlines allowed in an emphasis exp.
  2746. Use customize to modify this, or restart Emacs after changing it."
  2747. :group 'org-appearance
  2748. :set 'org-set-emph-re
  2749. :type '(list
  2750. (sexp :tag "Allowed chars in pre ")
  2751. (sexp :tag "Allowed chars in post ")
  2752. (sexp :tag "Forbidden chars in border ")
  2753. (sexp :tag "Regexp for body ")
  2754. (integer :tag "number of newlines allowed")
  2755. (option (boolean :tag "Please ignore this button"))))
  2756. (defcustom org-emphasis-alist
  2757. `(("*" bold "<b>" "</b>")
  2758. ("/" italic "<i>" "</i>")
  2759. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2760. ("=" org-code "<code>" "</code>" verbatim)
  2761. ("~" org-verbatim "<code>" "</code>" verbatim)
  2762. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2763. "<del>" "</del>")
  2764. )
  2765. "Special syntax for emphasized text.
  2766. Text starting and ending with a special character will be emphasized, for
  2767. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2768. characters, the face to be used by font-lock for highlighting in Org-mode
  2769. Emacs buffers, and the HTML tags to be used for this.
  2770. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2771. Use customize to modify this, or restart Emacs after changing it."
  2772. :group 'org-appearance
  2773. :set 'org-set-emph-re
  2774. :type '(repeat
  2775. (list
  2776. (string :tag "Marker character")
  2777. (choice
  2778. (face :tag "Font-lock-face")
  2779. (plist :tag "Face property list"))
  2780. (string :tag "HTML start tag")
  2781. (string :tag "HTML end tag")
  2782. (option (const verbatim)))))
  2783. (defvar org-protecting-blocks
  2784. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2785. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2786. This is needed for font-lock setup.")
  2787. ;;; Miscellaneous options
  2788. (defgroup org-completion nil
  2789. "Completion in Org-mode."
  2790. :tag "Org Completion"
  2791. :group 'org)
  2792. (defcustom org-completion-use-ido nil
  2793. "Non-nil means use ido completion wherever possible.
  2794. Note that `ido-mode' must be active for this variable to be relevant.
  2795. If you decide to turn this variable on, you might well want to turn off
  2796. `org-outline-path-complete-in-steps'.
  2797. See also `org-completion-use-iswitchb'."
  2798. :group 'org-completion
  2799. :type 'boolean)
  2800. (defcustom org-completion-use-iswitchb nil
  2801. "Non-nil means use iswitchb completion wherever possible.
  2802. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2803. If you decide to turn this variable on, you might well want to turn off
  2804. `org-outline-path-complete-in-steps'.
  2805. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2806. :group 'org-completion
  2807. :type 'boolean)
  2808. (defcustom org-completion-fallback-command 'hippie-expand
  2809. "The expansion command called by \\[org-complete] in normal context.
  2810. Normal means no org-mode-specific context."
  2811. :group 'org-completion
  2812. :type 'function)
  2813. ;;; Functions and variables from their packages
  2814. ;; Declared here to avoid compiler warnings
  2815. ;; XEmacs only
  2816. (defvar outline-mode-menu-heading)
  2817. (defvar outline-mode-menu-show)
  2818. (defvar outline-mode-menu-hide)
  2819. (defvar zmacs-regions) ; XEmacs regions
  2820. ;; Emacs only
  2821. (defvar mark-active)
  2822. ;; Various packages
  2823. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2824. (declare-function calendar-forward-day "cal-move" (arg))
  2825. (declare-function calendar-goto-date "cal-move" (date))
  2826. (declare-function calendar-goto-today "cal-move" ())
  2827. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2828. (defvar calc-embedded-close-formula)
  2829. (defvar calc-embedded-open-formula)
  2830. (declare-function cdlatex-tab "ext:cdlatex" ())
  2831. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2832. (defvar font-lock-unfontify-region-function)
  2833. (declare-function iswitchb-read-buffer "iswitchb"
  2834. (prompt &optional default require-match start matches-set))
  2835. (defvar iswitchb-temp-buflist)
  2836. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2837. (defvar org-agenda-tags-todo-honor-ignore-options)
  2838. (declare-function org-agenda-skip "org-agenda" ())
  2839. (declare-function
  2840. org-format-agenda-item "org-agenda"
  2841. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2842. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2843. (declare-function org-agenda-change-all-lines "org-agenda"
  2844. (newhead hdmarker &optional fixface just-this))
  2845. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2846. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2847. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2848. (beg end))
  2849. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2850. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2851. "org-agenda" (&optional end))
  2852. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2853. (declare-function org-indent-mode "org-indent" (&optional arg))
  2854. (declare-function parse-time-string "parse-time" (string))
  2855. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2856. (defvar remember-data-file)
  2857. (defvar texmathp-why)
  2858. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2859. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2860. (defvar w3m-current-url)
  2861. (defvar w3m-current-title)
  2862. (defvar org-latex-regexps)
  2863. ;;; Autoload and prepare some org modules
  2864. ;; Some table stuff that needs to be defined here, because it is used
  2865. ;; by the functions setting up org-mode or checking for table context.
  2866. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2867. "Detects an org-type or table-type table.")
  2868. (defconst org-table-line-regexp "^[ \t]*|"
  2869. "Detects an org-type table line.")
  2870. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2871. "Detects an org-type table line.")
  2872. (defconst org-table-hline-regexp "^[ \t]*|-"
  2873. "Detects an org-type table hline.")
  2874. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2875. "Detects a table-type table hline.")
  2876. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2877. "Searching from within a table (any type) this finds the first line
  2878. outside the table.")
  2879. ;; Autoload the functions in org-table.el that are needed by functions here.
  2880. (eval-and-compile
  2881. (org-autoload "org-table"
  2882. '(org-table-align org-table-begin org-table-blank-field
  2883. org-table-convert org-table-convert-region org-table-copy-down
  2884. org-table-copy-region org-table-create
  2885. org-table-create-or-convert-from-region
  2886. org-table-create-with-table.el org-table-current-dline
  2887. org-table-cut-region org-table-delete-column org-table-edit-field
  2888. org-table-edit-formulas org-table-end org-table-eval-formula
  2889. org-table-export org-table-field-info
  2890. org-table-get-stored-formulas org-table-goto-column
  2891. org-table-hline-and-move org-table-import org-table-insert-column
  2892. org-table-insert-hline org-table-insert-row org-table-iterate
  2893. org-table-justify-field-maybe org-table-kill-row
  2894. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2895. org-table-move-column org-table-move-column-left
  2896. org-table-move-column-right org-table-move-row
  2897. org-table-move-row-down org-table-move-row-up
  2898. org-table-next-field org-table-next-row org-table-paste-rectangle
  2899. org-table-previous-field org-table-recalculate
  2900. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2901. org-table-toggle-coordinate-overlays
  2902. org-table-toggle-formula-debugger org-table-wrap-region
  2903. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2904. (defun org-at-table-p (&optional table-type)
  2905. "Return t if the cursor is inside an org-type table.
  2906. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2907. (if org-enable-table-editor
  2908. (save-excursion
  2909. (beginning-of-line 1)
  2910. (looking-at (if table-type org-table-any-line-regexp
  2911. org-table-line-regexp)))
  2912. nil))
  2913. (defsubst org-table-p () (org-at-table-p))
  2914. (defun org-at-table.el-p ()
  2915. "Return t if and only if we are at a table.el table."
  2916. (and (org-at-table-p 'any)
  2917. (save-excursion
  2918. (goto-char (org-table-begin 'any))
  2919. (looking-at org-table1-hline-regexp))))
  2920. (defun org-table-recognize-table.el ()
  2921. "If there is a table.el table nearby, recognize it and move into it."
  2922. (if org-table-tab-recognizes-table.el
  2923. (if (org-at-table.el-p)
  2924. (progn
  2925. (beginning-of-line 1)
  2926. (if (looking-at org-table-dataline-regexp)
  2927. nil
  2928. (if (looking-at org-table1-hline-regexp)
  2929. (progn
  2930. (beginning-of-line 2)
  2931. (if (looking-at org-table-any-border-regexp)
  2932. (beginning-of-line -1)))))
  2933. (if (re-search-forward "|" (org-table-end t) t)
  2934. (progn
  2935. (require 'table)
  2936. (if (table--at-cell-p (point))
  2937. t
  2938. (message "recognizing table.el table...")
  2939. (table-recognize-table)
  2940. (message "recognizing table.el table...done")))
  2941. (error "This should not happen..."))
  2942. t)
  2943. nil)
  2944. nil))
  2945. (defun org-at-table-hline-p ()
  2946. "Return t if the cursor is inside a hline in a table."
  2947. (if org-enable-table-editor
  2948. (save-excursion
  2949. (beginning-of-line 1)
  2950. (looking-at org-table-hline-regexp))
  2951. nil))
  2952. (defvar org-table-clean-did-remove-column nil)
  2953. (defun org-table-map-tables (function)
  2954. "Apply FUNCTION to the start of all tables in the buffer."
  2955. (save-excursion
  2956. (save-restriction
  2957. (widen)
  2958. (goto-char (point-min))
  2959. (while (re-search-forward org-table-any-line-regexp nil t)
  2960. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2961. (beginning-of-line 1)
  2962. (when (looking-at org-table-line-regexp)
  2963. (save-excursion (funcall function))
  2964. (or (looking-at org-table-line-regexp)
  2965. (forward-char 1)))
  2966. (re-search-forward org-table-any-border-regexp nil 1))))
  2967. (message "Mapping tables: done"))
  2968. ;; Declare and autoload functions from org-exp.el & Co
  2969. (declare-function org-default-export-plist "org-exp")
  2970. (declare-function org-infile-export-plist "org-exp")
  2971. (declare-function org-get-current-options "org-exp")
  2972. (eval-and-compile
  2973. (org-autoload "org-exp"
  2974. '(org-export org-export-visible
  2975. org-insert-export-options-template
  2976. org-table-clean-before-export))
  2977. (org-autoload "org-ascii"
  2978. '(org-export-as-ascii org-export-ascii-preprocess
  2979. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2980. org-export-region-as-ascii))
  2981. (org-autoload "org-latex"
  2982. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2983. org-replace-region-by-latex org-export-region-as-latex
  2984. org-export-as-latex org-export-as-pdf
  2985. org-export-as-pdf-and-open))
  2986. (org-autoload "org-html"
  2987. '(org-export-as-html-and-open
  2988. org-export-as-html-batch org-export-as-html-to-buffer
  2989. org-replace-region-by-html org-export-region-as-html
  2990. org-export-as-html))
  2991. (org-autoload "org-docbook"
  2992. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  2993. org-replace-region-by-docbook org-export-region-as-docbook
  2994. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  2995. org-export-as-docbook))
  2996. (org-autoload "org-icalendar"
  2997. '(org-export-icalendar-this-file
  2998. org-export-icalendar-all-agenda-files
  2999. org-export-icalendar-combine-agenda-files))
  3000. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3001. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3002. ;; Declare and autoload functions from org-agenda.el
  3003. (eval-and-compile
  3004. (org-autoload "org-agenda"
  3005. '(org-agenda org-agenda-list org-search-view
  3006. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3007. org-diary org-agenda-to-appt
  3008. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3009. ;; Autoload org-remember
  3010. (eval-and-compile
  3011. (org-autoload "org-remember"
  3012. '(org-remember-insinuate org-remember-annotation
  3013. org-remember-apply-template org-remember org-remember-handler)))
  3014. ;; Autoload org-clock.el
  3015. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3016. (beg end))
  3017. (declare-function org-clock-update-mode-line "org-clock" ())
  3018. (declare-function org-resolve-clocks "org-clock"
  3019. (&optional also-non-dangling-p prompt last-valid))
  3020. (defvar org-clock-start-time)
  3021. (defvar org-clock-marker (make-marker)
  3022. "Marker recording the last clock-in.")
  3023. (defvar org-clock-hd-marker (make-marker)
  3024. "Marker recording the last clock-in, but the headline position.")
  3025. (defvar org-clock-heading ""
  3026. "The heading of the current clock entry.")
  3027. (defun org-clock-is-active ()
  3028. "Return non-nil if clock is currently running.
  3029. The return value is actually the clock marker."
  3030. (marker-buffer org-clock-marker))
  3031. (eval-and-compile
  3032. (org-autoload
  3033. "org-clock"
  3034. '(org-clock-in org-clock-out org-clock-cancel
  3035. org-clock-goto org-clock-sum org-clock-display
  3036. org-clock-remove-overlays org-clock-report
  3037. org-clocktable-shift org-dblock-write:clocktable
  3038. org-get-clocktable org-resolve-clocks)))
  3039. (defun org-clock-update-time-maybe ()
  3040. "If this is a CLOCK line, update it and return t.
  3041. Otherwise, return nil."
  3042. (interactive)
  3043. (save-excursion
  3044. (beginning-of-line 1)
  3045. (skip-chars-forward " \t")
  3046. (when (looking-at org-clock-string)
  3047. (let ((re (concat "[ \t]*" org-clock-string
  3048. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3049. "\\([ \t]*=>.*\\)?\\)?"))
  3050. ts te h m s neg)
  3051. (cond
  3052. ((not (looking-at re))
  3053. nil)
  3054. ((not (match-end 2))
  3055. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3056. (> org-clock-marker (point))
  3057. (<= org-clock-marker (point-at-eol)))
  3058. ;; The clock is running here
  3059. (setq org-clock-start-time
  3060. (apply 'encode-time
  3061. (org-parse-time-string (match-string 1))))
  3062. (org-clock-update-mode-line)))
  3063. (t
  3064. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3065. (end-of-line 1)
  3066. (setq ts (match-string 1)
  3067. te (match-string 3))
  3068. (setq s (- (org-float-time
  3069. (apply 'encode-time (org-parse-time-string te)))
  3070. (org-float-time
  3071. (apply 'encode-time (org-parse-time-string ts))))
  3072. neg (< s 0)
  3073. s (abs s)
  3074. h (floor (/ s 3600))
  3075. s (- s (* 3600 h))
  3076. m (floor (/ s 60))
  3077. s (- s (* 60 s)))
  3078. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3079. t))))))
  3080. (defun org-check-running-clock ()
  3081. "Check if the current buffer contains the running clock.
  3082. If yes, offer to stop it and to save the buffer with the changes."
  3083. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3084. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3085. (buffer-name))))
  3086. (org-clock-out)
  3087. (when (y-or-n-p "Save changed buffer?")
  3088. (save-buffer))))
  3089. (defun org-clocktable-try-shift (dir n)
  3090. "Check if this line starts a clock table, if yes, shift the time block."
  3091. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3092. (org-clocktable-shift dir n)))
  3093. ;; Autoload org-timer.el
  3094. (eval-and-compile
  3095. (org-autoload
  3096. "org-timer"
  3097. '(org-timer-start org-timer org-timer-item
  3098. org-timer-change-times-in-region
  3099. org-timer-set-timer
  3100. org-timer-reset-timers
  3101. org-timer-show-remaining-time)))
  3102. ;; Autoload org-feed.el
  3103. (eval-and-compile
  3104. (org-autoload
  3105. "org-feed"
  3106. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3107. ;; Autoload org-indent.el
  3108. ;; Define the variable already here, to make sure we have it.
  3109. (defvar org-indent-mode nil
  3110. "Non-nil if Org-Indent mode is enabled.
  3111. Use the command `org-indent-mode' to change this variable.")
  3112. (eval-and-compile
  3113. (org-autoload
  3114. "org-indent"
  3115. '(org-indent-mode)))
  3116. ;; Autoload org-mobile.el
  3117. (eval-and-compile
  3118. (org-autoload
  3119. "org-mobile"
  3120. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3121. ;; Autoload archiving code
  3122. ;; The stuff that is needed for cycling and tags has to be defined here.
  3123. (defgroup org-archive nil
  3124. "Options concerning archiving in Org-mode."
  3125. :tag "Org Archive"
  3126. :group 'org-structure)
  3127. (defcustom org-archive-location "%s_archive::"
  3128. "The location where subtrees should be archived.
  3129. The value of this variable is a string, consisting of two parts,
  3130. separated by a double-colon. The first part is a filename and
  3131. the second part is a headline.
  3132. When the filename is omitted, archiving happens in the same file.
  3133. %s in the filename will be replaced by the current file
  3134. name (without the directory part). Archiving to a different file
  3135. is useful to keep archived entries from contributing to the
  3136. Org-mode Agenda.
  3137. The archived entries will be filed as subtrees of the specified
  3138. headline. When the headline is omitted, the subtrees are simply
  3139. filed away at the end of the file, as top-level entries. Also in
  3140. the heading you can use %s to represent the file name, this can be
  3141. useful when using the same archive for a number of different files.
  3142. Here are a few examples:
  3143. \"%s_archive::\"
  3144. If the current file is Projects.org, archive in file
  3145. Projects.org_archive, as top-level trees. This is the default.
  3146. \"::* Archived Tasks\"
  3147. Archive in the current file, under the top-level headline
  3148. \"* Archived Tasks\".
  3149. \"~/org/archive.org::\"
  3150. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3151. \"~/org/archive.org::From %s\"
  3152. Archive in file ~/org/archive.org (absolute path), under headlines
  3153. \"From FILENAME\" where file name is the current file name.
  3154. \"basement::** Finished Tasks\"
  3155. Archive in file ./basement (relative path), as level 3 trees
  3156. below the level 2 heading \"** Finished Tasks\".
  3157. You may set this option on a per-file basis by adding to the buffer a
  3158. line like
  3159. #+ARCHIVE: basement::** Finished Tasks
  3160. You may also define it locally for a subtree by setting an ARCHIVE property
  3161. in the entry. If such a property is found in an entry, or anywhere up
  3162. the hierarchy, it will be used."
  3163. :group 'org-archive
  3164. :type 'string)
  3165. (defcustom org-archive-tag "ARCHIVE"
  3166. "The tag that marks a subtree as archived.
  3167. An archived subtree does not open during visibility cycling, and does
  3168. not contribute to the agenda listings.
  3169. After changing this, font-lock must be restarted in the relevant buffers to
  3170. get the proper fontification."
  3171. :group 'org-archive
  3172. :group 'org-keywords
  3173. :type 'string)
  3174. (defcustom org-agenda-skip-archived-trees t
  3175. "Non-nil means the agenda will skip any items located in archived trees.
  3176. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3177. variable is no longer recommended, you should leave it at the value t.
  3178. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3179. :group 'org-archive
  3180. :group 'org-agenda-skip
  3181. :type 'boolean)
  3182. (defcustom org-columns-skip-archived-trees t
  3183. "Non-nil means ignore archived trees when creating column view."
  3184. :group 'org-archive
  3185. :group 'org-properties
  3186. :type 'boolean)
  3187. (defcustom org-cycle-open-archived-trees nil
  3188. "Non-nil means `org-cycle' will open archived trees.
  3189. An archived tree is a tree marked with the tag ARCHIVE.
  3190. When nil, archived trees will stay folded. You can still open them with
  3191. normal outline commands like `show-all', but not with the cycling commands."
  3192. :group 'org-archive
  3193. :group 'org-cycle
  3194. :type 'boolean)
  3195. (defcustom org-sparse-tree-open-archived-trees nil
  3196. "Non-nil means sparse tree construction shows matches in archived trees.
  3197. When nil, matches in these trees are highlighted, but the trees are kept in
  3198. collapsed state."
  3199. :group 'org-archive
  3200. :group 'org-sparse-trees
  3201. :type 'boolean)
  3202. (defun org-cycle-hide-archived-subtrees (state)
  3203. "Re-hide all archived subtrees after a visibility state change."
  3204. (when (and (not org-cycle-open-archived-trees)
  3205. (not (memq state '(overview folded))))
  3206. (save-excursion
  3207. (let* ((globalp (memq state '(contents all)))
  3208. (beg (if globalp (point-min) (point)))
  3209. (end (if globalp (point-max) (org-end-of-subtree t))))
  3210. (org-hide-archived-subtrees beg end)
  3211. (goto-char beg)
  3212. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3213. (message "%s" (substitute-command-keys
  3214. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3215. (defun org-force-cycle-archived ()
  3216. "Cycle subtree even if it is archived."
  3217. (interactive)
  3218. (setq this-command 'org-cycle)
  3219. (let ((org-cycle-open-archived-trees t))
  3220. (call-interactively 'org-cycle)))
  3221. (defun org-hide-archived-subtrees (beg end)
  3222. "Re-hide all archived subtrees after a visibility state change."
  3223. (save-excursion
  3224. (let* ((re (concat ":" org-archive-tag ":")))
  3225. (goto-char beg)
  3226. (while (re-search-forward re end t)
  3227. (when (org-on-heading-p)
  3228. (org-flag-subtree t)
  3229. (org-end-of-subtree t))))))
  3230. (defun org-flag-subtree (flag)
  3231. (save-excursion
  3232. (org-back-to-heading t)
  3233. (outline-end-of-heading)
  3234. (outline-flag-region (point)
  3235. (progn (org-end-of-subtree t) (point))
  3236. flag)))
  3237. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3238. (eval-and-compile
  3239. (org-autoload "org-archive"
  3240. '(org-add-archive-files org-archive-subtree
  3241. org-archive-to-archive-sibling org-toggle-archive-tag
  3242. org-archive-subtree-default
  3243. org-archive-subtree-default-with-confirmation)))
  3244. ;; Autoload Column View Code
  3245. (declare-function org-columns-number-to-string "org-colview")
  3246. (declare-function org-columns-get-format-and-top-level "org-colview")
  3247. (declare-function org-columns-compute "org-colview")
  3248. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3249. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3250. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3251. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3252. ;; Autoload ID code
  3253. (declare-function org-id-store-link "org-id")
  3254. (declare-function org-id-locations-load "org-id")
  3255. (declare-function org-id-locations-save "org-id")
  3256. (defvar org-id-track-globally)
  3257. (org-autoload "org-id"
  3258. '(org-id-get-create org-id-new org-id-copy org-id-get
  3259. org-id-get-with-outline-path-completion
  3260. org-id-get-with-outline-drilling
  3261. org-id-goto org-id-find org-id-store-link))
  3262. ;; Autoload Plotting Code
  3263. (org-autoload "org-plot"
  3264. '(org-plot/gnuplot))
  3265. ;;; Variables for pre-computed regular expressions, all buffer local
  3266. (defvar org-drawer-regexp nil
  3267. "Matches first line of a hidden block.")
  3268. (make-variable-buffer-local 'org-drawer-regexp)
  3269. (defvar org-todo-regexp nil
  3270. "Matches any of the TODO state keywords.")
  3271. (make-variable-buffer-local 'org-todo-regexp)
  3272. (defvar org-not-done-regexp nil
  3273. "Matches any of the TODO state keywords except the last one.")
  3274. (make-variable-buffer-local 'org-not-done-regexp)
  3275. (defvar org-not-done-heading-regexp nil
  3276. "Matches a TODO headline that is not done.")
  3277. (make-variable-buffer-local 'org-not-done-regexp)
  3278. (defvar org-todo-line-regexp nil
  3279. "Matches a headline and puts TODO state into group 2 if present.")
  3280. (make-variable-buffer-local 'org-todo-line-regexp)
  3281. (defvar org-complex-heading-regexp nil
  3282. "Matches a headline and puts everything into groups:
  3283. group 1: the stars
  3284. group 2: The todo keyword, maybe
  3285. group 3: Priority cookie
  3286. group 4: True headline
  3287. group 5: Tags")
  3288. (make-variable-buffer-local 'org-complex-heading-regexp)
  3289. (defvar org-complex-heading-regexp-format nil)
  3290. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3291. (defvar org-todo-line-tags-regexp nil
  3292. "Matches a headline and puts TODO state into group 2 if present.
  3293. Also put tags into group 4 if tags are present.")
  3294. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3295. (defvar org-nl-done-regexp nil
  3296. "Matches newline followed by a headline with the DONE keyword.")
  3297. (make-variable-buffer-local 'org-nl-done-regexp)
  3298. (defvar org-looking-at-done-regexp nil
  3299. "Matches the DONE keyword a point.")
  3300. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3301. (defvar org-ds-keyword-length 12
  3302. "Maximum length of the Deadline and SCHEDULED keywords.")
  3303. (make-variable-buffer-local 'org-ds-keyword-length)
  3304. (defvar org-deadline-regexp nil
  3305. "Matches the DEADLINE keyword.")
  3306. (make-variable-buffer-local 'org-deadline-regexp)
  3307. (defvar org-deadline-time-regexp nil
  3308. "Matches the DEADLINE keyword together with a time stamp.")
  3309. (make-variable-buffer-local 'org-deadline-time-regexp)
  3310. (defvar org-deadline-line-regexp nil
  3311. "Matches the DEADLINE keyword and the rest of the line.")
  3312. (make-variable-buffer-local 'org-deadline-line-regexp)
  3313. (defvar org-scheduled-regexp nil
  3314. "Matches the SCHEDULED keyword.")
  3315. (make-variable-buffer-local 'org-scheduled-regexp)
  3316. (defvar org-scheduled-time-regexp nil
  3317. "Matches the SCHEDULED keyword together with a time stamp.")
  3318. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3319. (defvar org-closed-time-regexp nil
  3320. "Matches the CLOSED keyword together with a time stamp.")
  3321. (make-variable-buffer-local 'org-closed-time-regexp)
  3322. (defvar org-keyword-time-regexp nil
  3323. "Matches any of the 4 keywords, together with the time stamp.")
  3324. (make-variable-buffer-local 'org-keyword-time-regexp)
  3325. (defvar org-keyword-time-not-clock-regexp nil
  3326. "Matches any of the 3 keywords, together with the time stamp.")
  3327. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3328. (defvar org-maybe-keyword-time-regexp nil
  3329. "Matches a timestamp, possibly preceeded by a keyword.")
  3330. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3331. (defvar org-planning-or-clock-line-re nil
  3332. "Matches a line with planning or clock info.")
  3333. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3334. (defvar org-all-time-keywords nil
  3335. "List of time keywords.")
  3336. (make-variable-buffer-local 'org-all-time-keywords)
  3337. (defconst org-plain-time-of-day-regexp
  3338. (concat
  3339. "\\(\\<[012]?[0-9]"
  3340. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3341. "\\(--?"
  3342. "\\(\\<[012]?[0-9]"
  3343. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3344. "\\)?")
  3345. "Regular expression to match a plain time or time range.
  3346. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3347. groups carry important information:
  3348. 0 the full match
  3349. 1 the first time, range or not
  3350. 8 the second time, if it is a range.")
  3351. (defconst org-plain-time-extension-regexp
  3352. (concat
  3353. "\\(\\<[012]?[0-9]"
  3354. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3355. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3356. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3357. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3358. groups carry important information:
  3359. 0 the full match
  3360. 7 hours of duration
  3361. 9 minutes of duration")
  3362. (defconst org-stamp-time-of-day-regexp
  3363. (concat
  3364. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3365. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3366. "\\(--?"
  3367. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3368. "Regular expression to match a timestamp time or time range.
  3369. After a match, the following groups carry important information:
  3370. 0 the full match
  3371. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3372. 2 the first time, range or not
  3373. 4 the second time, if it is a range.")
  3374. (defconst org-startup-options
  3375. '(("fold" org-startup-folded t)
  3376. ("overview" org-startup-folded t)
  3377. ("nofold" org-startup-folded nil)
  3378. ("showall" org-startup-folded nil)
  3379. ("showeverything" org-startup-folded showeverything)
  3380. ("content" org-startup-folded content)
  3381. ("indent" org-startup-indented t)
  3382. ("noindent" org-startup-indented nil)
  3383. ("hidestars" org-hide-leading-stars t)
  3384. ("showstars" org-hide-leading-stars nil)
  3385. ("odd" org-odd-levels-only t)
  3386. ("oddeven" org-odd-levels-only nil)
  3387. ("align" org-startup-align-all-tables t)
  3388. ("noalign" org-startup-align-all-tables nil)
  3389. ("customtime" org-display-custom-times t)
  3390. ("logdone" org-log-done time)
  3391. ("lognotedone" org-log-done note)
  3392. ("nologdone" org-log-done nil)
  3393. ("lognoteclock-out" org-log-note-clock-out t)
  3394. ("nolognoteclock-out" org-log-note-clock-out nil)
  3395. ("logrepeat" org-log-repeat state)
  3396. ("lognoterepeat" org-log-repeat note)
  3397. ("nologrepeat" org-log-repeat nil)
  3398. ("logreschedule" org-log-reschedule time)
  3399. ("lognotereschedule" org-log-reschedule note)
  3400. ("nologreschedule" org-log-reschedule nil)
  3401. ("logredeadline" org-log-redeadline time)
  3402. ("lognoteredeadline" org-log-redeadline note)
  3403. ("nologredeadline" org-log-redeadline nil)
  3404. ("logrefile" org-log-refile time)
  3405. ("lognoterefile" org-log-refile note)
  3406. ("nologrefile" org-log-refile nil)
  3407. ("fninline" org-footnote-define-inline t)
  3408. ("nofninline" org-footnote-define-inline nil)
  3409. ("fnlocal" org-footnote-section nil)
  3410. ("fnauto" org-footnote-auto-label t)
  3411. ("fnprompt" org-footnote-auto-label nil)
  3412. ("fnconfirm" org-footnote-auto-label confirm)
  3413. ("fnplain" org-footnote-auto-label plain)
  3414. ("fnadjust" org-footnote-auto-adjust t)
  3415. ("nofnadjust" org-footnote-auto-adjust nil)
  3416. ("constcgs" constants-unit-system cgs)
  3417. ("constSI" constants-unit-system SI)
  3418. ("noptag" org-tag-persistent-alist nil)
  3419. ("hideblocks" org-hide-block-startup t)
  3420. ("nohideblocks" org-hide-block-startup nil)
  3421. ("beamer" org-startup-with-beamer-mode t))
  3422. "Variable associated with STARTUP options for org-mode.
  3423. Each element is a list of three items: The startup options as written
  3424. in the #+STARTUP line, the corresponding variable, and the value to
  3425. set this variable to if the option is found. An optional forth element PUSH
  3426. means to push this value onto the list in the variable.")
  3427. (defun org-set-regexps-and-options ()
  3428. "Precompute regular expressions for current buffer."
  3429. (when (org-mode-p)
  3430. (org-set-local 'org-todo-kwd-alist nil)
  3431. (org-set-local 'org-todo-key-alist nil)
  3432. (org-set-local 'org-todo-key-trigger nil)
  3433. (org-set-local 'org-todo-keywords-1 nil)
  3434. (org-set-local 'org-done-keywords nil)
  3435. (org-set-local 'org-todo-heads nil)
  3436. (org-set-local 'org-todo-sets nil)
  3437. (org-set-local 'org-todo-log-states nil)
  3438. (org-set-local 'org-file-properties nil)
  3439. (org-set-local 'org-file-tags nil)
  3440. (let ((re (org-make-options-regexp
  3441. '("CATEGORY" "TODO" "COLUMNS"
  3442. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3443. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3444. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3445. (splitre "[ \t]+")
  3446. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3447. tail sep kws1 prio props ftags drawers beamer-p
  3448. ext-setup-or-nil setup-contents (start 0))
  3449. (save-excursion
  3450. (save-restriction
  3451. (widen)
  3452. (goto-char (point-min))
  3453. (while (or (and ext-setup-or-nil
  3454. (string-match re ext-setup-or-nil start)
  3455. (setq start (match-end 0)))
  3456. (and (setq ext-setup-or-nil nil start 0)
  3457. (re-search-forward re nil t)))
  3458. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3459. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3460. (cond
  3461. ((equal key "CATEGORY")
  3462. (if (string-match "[ \t]+$" value)
  3463. (setq value (replace-match "" t t value)))
  3464. (setq cat value))
  3465. ((member key '("SEQ_TODO" "TODO"))
  3466. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3467. ((equal key "TYP_TODO")
  3468. (push (cons 'type (org-split-string value splitre)) kwds))
  3469. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3470. ;; general TODO-like setup
  3471. (push (cons (intern (downcase (match-string 1 key)))
  3472. (org-split-string value splitre)) kwds))
  3473. ((equal key "TAGS")
  3474. (setq tags (append tags (if tags '("\\n") nil)
  3475. (org-split-string value splitre))))
  3476. ((equal key "COLUMNS")
  3477. (org-set-local 'org-columns-default-format value))
  3478. ((equal key "LINK")
  3479. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3480. (push (cons (match-string 1 value)
  3481. (org-trim (match-string 2 value)))
  3482. links)))
  3483. ((equal key "PRIORITIES")
  3484. (setq prio (org-split-string value " +")))
  3485. ((equal key "PROPERTY")
  3486. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3487. (push (cons (match-string 1 value) (match-string 2 value))
  3488. props)))
  3489. ((equal key "FILETAGS")
  3490. (when (string-match "\\S-" value)
  3491. (setq ftags
  3492. (append
  3493. ftags
  3494. (apply 'append
  3495. (mapcar (lambda (x) (org-split-string x ":"))
  3496. (org-split-string value)))))))
  3497. ((equal key "DRAWERS")
  3498. (setq drawers (org-split-string value splitre)))
  3499. ((equal key "CONSTANTS")
  3500. (setq const (append const (org-split-string value splitre))))
  3501. ((equal key "STARTUP")
  3502. (let ((opts (org-split-string value splitre))
  3503. l var val)
  3504. (while (setq l (pop opts))
  3505. (when (setq l (assoc l org-startup-options))
  3506. (setq var (nth 1 l) val (nth 2 l))
  3507. (if (not (nth 3 l))
  3508. (set (make-local-variable var) val)
  3509. (if (not (listp (symbol-value var)))
  3510. (set (make-local-variable var) nil))
  3511. (set (make-local-variable var) (symbol-value var))
  3512. (add-to-list var val))))))
  3513. ((equal key "ARCHIVE")
  3514. (string-match " *$" value)
  3515. (setq arch (replace-match "" t t value))
  3516. (remove-text-properties 0 (length arch)
  3517. '(face t fontified t) arch))
  3518. ((equal key "LATEX_CLASS")
  3519. (setq beamer-p (equal value "beamer")))
  3520. ((equal key "SETUPFILE")
  3521. (setq setup-contents (org-file-contents
  3522. (expand-file-name
  3523. (org-remove-double-quotes value))
  3524. 'noerror))
  3525. (if (not ext-setup-or-nil)
  3526. (setq ext-setup-or-nil setup-contents start 0)
  3527. (setq ext-setup-or-nil
  3528. (concat (substring ext-setup-or-nil 0 start)
  3529. "\n" setup-contents "\n"
  3530. (substring ext-setup-or-nil start)))))
  3531. ))))
  3532. (when cat
  3533. (org-set-local 'org-category (intern cat))
  3534. (push (cons "CATEGORY" cat) props))
  3535. (when prio
  3536. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3537. (setq prio (mapcar 'string-to-char prio))
  3538. (org-set-local 'org-highest-priority (nth 0 prio))
  3539. (org-set-local 'org-lowest-priority (nth 1 prio))
  3540. (org-set-local 'org-default-priority (nth 2 prio)))
  3541. (and props (org-set-local 'org-file-properties (nreverse props)))
  3542. (and ftags (org-set-local 'org-file-tags
  3543. (mapcar 'org-add-prop-inherited ftags)))
  3544. (and drawers (org-set-local 'org-drawers drawers))
  3545. (and arch (org-set-local 'org-archive-location arch))
  3546. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3547. ;; Process the TODO keywords
  3548. (unless kwds
  3549. ;; Use the global values as if they had been given locally.
  3550. (setq kwds (default-value 'org-todo-keywords))
  3551. (if (stringp (car kwds))
  3552. (setq kwds (list (cons org-todo-interpretation
  3553. (default-value 'org-todo-keywords)))))
  3554. (setq kwds (reverse kwds)))
  3555. (setq kwds (nreverse kwds))
  3556. (let (inter kws kw)
  3557. (while (setq kws (pop kwds))
  3558. (let ((kws (or
  3559. (run-hook-with-args-until-success
  3560. 'org-todo-setup-filter-hook kws)
  3561. kws)))
  3562. (setq inter (pop kws) sep (member "|" kws)
  3563. kws0 (delete "|" (copy-sequence kws))
  3564. kwsa nil
  3565. kws1 (mapcar
  3566. (lambda (x)
  3567. ;; 1 2
  3568. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3569. (progn
  3570. (setq kw (match-string 1 x)
  3571. key (and (match-end 2) (match-string 2 x))
  3572. log (org-extract-log-state-settings x))
  3573. (push (cons kw (and key (string-to-char key))) kwsa)
  3574. (and log (push log org-todo-log-states))
  3575. kw)
  3576. (error "Invalid TODO keyword %s" x)))
  3577. kws0)
  3578. kwsa (if kwsa (append '((:startgroup))
  3579. (nreverse kwsa)
  3580. '((:endgroup))))
  3581. hw (car kws1)
  3582. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3583. tail (list inter hw (car dws) (org-last dws))))
  3584. (add-to-list 'org-todo-heads hw 'append)
  3585. (push kws1 org-todo-sets)
  3586. (setq org-done-keywords (append org-done-keywords dws nil))
  3587. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3588. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3589. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3590. (setq org-todo-sets (nreverse org-todo-sets)
  3591. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3592. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3593. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3594. ;; Process the constants
  3595. (when const
  3596. (let (e cst)
  3597. (while (setq e (pop const))
  3598. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3599. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3600. (setq org-table-formula-constants-local cst)))
  3601. ;; Process the tags.
  3602. (when tags
  3603. (let (e tgs)
  3604. (while (setq e (pop tags))
  3605. (cond
  3606. ((equal e "{") (push '(:startgroup) tgs))
  3607. ((equal e "}") (push '(:endgroup) tgs))
  3608. ((equal e "\\n") (push '(:newline) tgs))
  3609. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3610. (push (cons (match-string 1 e)
  3611. (string-to-char (match-string 2 e)))
  3612. tgs))
  3613. (t (push (list e) tgs))))
  3614. (org-set-local 'org-tag-alist nil)
  3615. (while (setq e (pop tgs))
  3616. (or (and (stringp (car e))
  3617. (assoc (car e) org-tag-alist))
  3618. (push e org-tag-alist)))))
  3619. ;; Compute the regular expressions and other local variables
  3620. (if (not org-done-keywords)
  3621. (setq org-done-keywords (and org-todo-keywords-1
  3622. (list (org-last org-todo-keywords-1)))))
  3623. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3624. (length org-scheduled-string)
  3625. (length org-clock-string)
  3626. (length org-closed-string)))
  3627. org-drawer-regexp
  3628. (concat "^[ \t]*:\\("
  3629. (mapconcat 'regexp-quote org-drawers "\\|")
  3630. "\\):[ \t]*$")
  3631. org-not-done-keywords
  3632. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3633. org-todo-regexp
  3634. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3635. "\\|") "\\)\\>")
  3636. org-not-done-regexp
  3637. (concat "\\<\\("
  3638. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3639. "\\)\\>")
  3640. org-not-done-heading-regexp
  3641. (concat "^\\(\\*+\\)[ \t]+\\("
  3642. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3643. "\\)\\>")
  3644. org-todo-line-regexp
  3645. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3646. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3647. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3648. org-complex-heading-regexp
  3649. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3650. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3651. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3652. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3653. org-complex-heading-regexp-format
  3654. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3655. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3656. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3657. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3658. org-nl-done-regexp
  3659. (concat "\n\\*+[ \t]+"
  3660. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3661. "\\)" "\\>")
  3662. org-todo-line-tags-regexp
  3663. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3664. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3665. (org-re
  3666. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3667. org-looking-at-done-regexp
  3668. (concat "^" "\\(?:"
  3669. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3670. "\\>")
  3671. org-deadline-regexp (concat "\\<" org-deadline-string)
  3672. org-deadline-time-regexp
  3673. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3674. org-deadline-line-regexp
  3675. (concat "\\<\\(" org-deadline-string "\\).*")
  3676. org-scheduled-regexp
  3677. (concat "\\<" org-scheduled-string)
  3678. org-scheduled-time-regexp
  3679. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3680. org-closed-time-regexp
  3681. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3682. org-keyword-time-regexp
  3683. (concat "\\<\\(" org-scheduled-string
  3684. "\\|" org-deadline-string
  3685. "\\|" org-closed-string
  3686. "\\|" org-clock-string "\\)"
  3687. " *[[<]\\([^]>]+\\)[]>]")
  3688. org-keyword-time-not-clock-regexp
  3689. (concat "\\<\\(" org-scheduled-string
  3690. "\\|" org-deadline-string
  3691. "\\|" org-closed-string
  3692. "\\)"
  3693. " *[[<]\\([^]>]+\\)[]>]")
  3694. org-maybe-keyword-time-regexp
  3695. (concat "\\(\\<\\(" org-scheduled-string
  3696. "\\|" org-deadline-string
  3697. "\\|" org-closed-string
  3698. "\\|" org-clock-string "\\)\\)?"
  3699. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3700. org-planning-or-clock-line-re
  3701. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3702. "\\|" org-deadline-string
  3703. "\\|" org-closed-string "\\|" org-clock-string
  3704. "\\)\\>\\)")
  3705. org-all-time-keywords
  3706. (mapcar (lambda (w) (substring w 0 -1))
  3707. (list org-scheduled-string org-deadline-string
  3708. org-clock-string org-closed-string))
  3709. )
  3710. (org-compute-latex-and-specials-regexp)
  3711. (org-set-font-lock-defaults))))
  3712. (defun org-file-contents (file &optional noerror)
  3713. "Return the contents of FILE, as a string."
  3714. (if (or (not file)
  3715. (not (file-readable-p file)))
  3716. (if noerror
  3717. (progn
  3718. (message "Cannot read file %s" file)
  3719. (ding) (sit-for 2)
  3720. "")
  3721. (error "Cannot read file %s" file))
  3722. (with-temp-buffer
  3723. (insert-file-contents file)
  3724. (buffer-string))))
  3725. (defun org-extract-log-state-settings (x)
  3726. "Extract the log state setting from a TODO keyword string.
  3727. This will extract info from a string like \"WAIT(w@/!)\"."
  3728. (let (kw key log1 log2)
  3729. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3730. (setq kw (match-string 1 x)
  3731. key (and (match-end 2) (match-string 2 x))
  3732. log1 (and (match-end 3) (match-string 3 x))
  3733. log2 (and (match-end 4) (match-string 4 x)))
  3734. (and (or log1 log2)
  3735. (list kw
  3736. (and log1 (if (equal log1 "!") 'time 'note))
  3737. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3738. (defun org-remove-keyword-keys (list)
  3739. "Remove a pair of parenthesis at the end of each string in LIST."
  3740. (mapcar (lambda (x)
  3741. (if (string-match "(.*)$" x)
  3742. (substring x 0 (match-beginning 0))
  3743. x))
  3744. list))
  3745. (defun org-assign-fast-keys (alist)
  3746. "Assign fast keys to a keyword-key alist.
  3747. Respect keys that are already there."
  3748. (let (new e (alt ?0))
  3749. (while (setq e (pop alist))
  3750. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3751. (cdr e)) ;; Key already assigned.
  3752. (push e new)
  3753. (let ((clist (string-to-list (downcase (car e))))
  3754. (used (append new alist)))
  3755. (when (= (car clist) ?@)
  3756. (pop clist))
  3757. (while (and clist (rassoc (car clist) used))
  3758. (pop clist))
  3759. (unless clist
  3760. (while (rassoc alt used)
  3761. (incf alt)))
  3762. (push (cons (car e) (or (car clist) alt)) new))))
  3763. (nreverse new)))
  3764. ;;; Some variables used in various places
  3765. (defvar org-window-configuration nil
  3766. "Used in various places to store a window configuration.")
  3767. (defvar org-selected-window nil
  3768. "Used in various places to store a window configuration.")
  3769. (defvar org-finish-function nil
  3770. "Function to be called when `C-c C-c' is used.
  3771. This is for getting out of special buffers like remember.")
  3772. ;; FIXME: Occasionally check by commenting these, to make sure
  3773. ;; no other functions uses these, forgetting to let-bind them.
  3774. (defvar entry)
  3775. (defvar last-state)
  3776. (defvar date)
  3777. ;; Defined somewhere in this file, but used before definition.
  3778. (defvar org-entities) ;; defined in org-entities.el
  3779. (defvar org-struct-menu)
  3780. (defvar org-org-menu)
  3781. (defvar org-tbl-menu)
  3782. ;;;; Define the Org-mode
  3783. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3784. (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."))
  3785. ;; We use a before-change function to check if a table might need
  3786. ;; an update.
  3787. (defvar org-table-may-need-update t
  3788. "Indicates that a table might need an update.
  3789. This variable is set by `org-before-change-function'.
  3790. `org-table-align' sets it back to nil.")
  3791. (defun org-before-change-function (beg end)
  3792. "Every change indicates that a table might need an update."
  3793. (setq org-table-may-need-update t))
  3794. (defvar org-mode-map)
  3795. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3796. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3797. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3798. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3799. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3800. (defvar org-table-buffer-is-an nil)
  3801. (defconst org-outline-regexp "\\*+ ")
  3802. ;;;###autoload
  3803. (define-derived-mode org-mode outline-mode "Org"
  3804. "Outline-based notes management and organizer, alias
  3805. \"Carsten's outline-mode for keeping track of everything.\"
  3806. Org-mode develops organizational tasks around a NOTES file which
  3807. contains information about projects as plain text. Org-mode is
  3808. implemented on top of outline-mode, which is ideal to keep the content
  3809. of large files well structured. It supports ToDo items, deadlines and
  3810. time stamps, which magically appear in the diary listing of the Emacs
  3811. calendar. Tables are easily created with a built-in table editor.
  3812. Plain text URL-like links connect to websites, emails (VM), Usenet
  3813. messages (Gnus), BBDB entries, and any files related to the project.
  3814. For printing and sharing of notes, an Org-mode file (or a part of it)
  3815. can be exported as a structured ASCII or HTML file.
  3816. The following commands are available:
  3817. \\{org-mode-map}"
  3818. ;; Get rid of Outline menus, they are not needed
  3819. ;; Need to do this here because define-derived-mode sets up
  3820. ;; the keymap so late. Still, it is a waste to call this each time
  3821. ;; we switch another buffer into org-mode.
  3822. (if (featurep 'xemacs)
  3823. (when (boundp 'outline-mode-menu-heading)
  3824. ;; Assume this is Greg's port, it used easymenu
  3825. (easy-menu-remove outline-mode-menu-heading)
  3826. (easy-menu-remove outline-mode-menu-show)
  3827. (easy-menu-remove outline-mode-menu-hide))
  3828. (define-key org-mode-map [menu-bar headings] 'undefined)
  3829. (define-key org-mode-map [menu-bar hide] 'undefined)
  3830. (define-key org-mode-map [menu-bar show] 'undefined))
  3831. (org-load-modules-maybe)
  3832. (easy-menu-add org-org-menu)
  3833. (easy-menu-add org-tbl-menu)
  3834. (org-install-agenda-files-menu)
  3835. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3836. (org-add-to-invisibility-spec '(org-cwidth))
  3837. (org-add-to-invisibility-spec '(org-hide-block . t))
  3838. (when (featurep 'xemacs)
  3839. (org-set-local 'line-move-ignore-invisible t))
  3840. (org-set-local 'outline-regexp org-outline-regexp)
  3841. (org-set-local 'outline-level 'org-outline-level)
  3842. (when (and org-ellipsis
  3843. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3844. (fboundp 'make-glyph-code))
  3845. (unless org-display-table
  3846. (setq org-display-table (make-display-table)))
  3847. (set-display-table-slot
  3848. org-display-table 4
  3849. (vconcat (mapcar
  3850. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3851. org-ellipsis)))
  3852. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3853. (setq buffer-display-table org-display-table))
  3854. (org-set-regexps-and-options)
  3855. (when (and org-tag-faces (not org-tags-special-faces-re))
  3856. ;; tag faces set outside customize.... force initialization.
  3857. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3858. ;; Calc embedded
  3859. (org-set-local 'calc-embedded-open-mode "# ")
  3860. (modify-syntax-entry ?# "<")
  3861. (modify-syntax-entry ?@ "w")
  3862. (if org-startup-truncated (setq truncate-lines t))
  3863. (org-set-local 'font-lock-unfontify-region-function
  3864. 'org-unfontify-region)
  3865. ;; Activate before-change-function
  3866. (org-set-local 'org-table-may-need-update t)
  3867. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3868. 'local)
  3869. ;; Check for running clock before killing a buffer
  3870. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3871. ;; Paragraphs and auto-filling
  3872. (org-set-autofill-regexps)
  3873. (setq indent-line-function 'org-indent-line-function)
  3874. (org-update-radio-target-regexp)
  3875. ;; Make sure dependence stuff works reliably, even for users who set it
  3876. ;; too late :-(
  3877. (if org-enforce-todo-dependencies
  3878. (add-hook 'org-blocker-hook
  3879. 'org-block-todo-from-children-or-siblings-or-parent)
  3880. (remove-hook 'org-blocker-hook
  3881. 'org-block-todo-from-children-or-siblings-or-parent))
  3882. (if org-enforce-todo-checkbox-dependencies
  3883. (add-hook 'org-blocker-hook
  3884. 'org-block-todo-from-checkboxes)
  3885. (remove-hook 'org-blocker-hook
  3886. 'org-block-todo-from-checkboxes))
  3887. ;; Comment characters
  3888. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3889. (org-set-local 'comment-padding " ")
  3890. ;; Align options lines
  3891. (org-set-local
  3892. 'align-mode-rules-list
  3893. '((org-in-buffer-settings
  3894. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3895. (modes . '(org-mode)))))
  3896. ;; Imenu
  3897. (org-set-local 'imenu-create-index-function
  3898. 'org-imenu-get-tree)
  3899. ;; Make isearch reveal context
  3900. (if (or (featurep 'xemacs)
  3901. (not (boundp 'outline-isearch-open-invisible-function)))
  3902. ;; Emacs 21 and XEmacs make use of the hook
  3903. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3904. ;; Emacs 22 deals with this through a special variable
  3905. (org-set-local 'outline-isearch-open-invisible-function
  3906. (lambda (&rest ignore) (org-show-context 'isearch))))
  3907. ;; Turn on org-beamer-mode?
  3908. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3909. ;; If empty file that did not turn on org-mode automatically, make it to.
  3910. (if (and org-insert-mode-line-in-empty-file
  3911. (interactive-p)
  3912. (= (point-min) (point-max)))
  3913. (insert "# -*- mode: org -*-\n\n"))
  3914. (unless org-inhibit-startup
  3915. (when org-startup-align-all-tables
  3916. (let ((bmp (buffer-modified-p)))
  3917. (org-table-map-tables 'org-table-align)
  3918. (set-buffer-modified-p bmp)))
  3919. (when org-startup-indented
  3920. (require 'org-indent)
  3921. (org-indent-mode 1))
  3922. (unless org-inhibit-startup-visibility-stuff
  3923. (org-set-startup-visibility))))
  3924. (when (fboundp 'abbrev-table-put)
  3925. (abbrev-table-put org-mode-abbrev-table
  3926. :parents (list text-mode-abbrev-table)))
  3927. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3928. (defun org-current-time ()
  3929. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3930. (if (> (car org-time-stamp-rounding-minutes) 1)
  3931. (let ((r (car org-time-stamp-rounding-minutes))
  3932. (time (decode-time)))
  3933. (apply 'encode-time
  3934. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3935. (nthcdr 2 time))))
  3936. (current-time)))
  3937. ;;;; Font-Lock stuff, including the activators
  3938. (defvar org-mouse-map (make-sparse-keymap))
  3939. (org-defkey org-mouse-map
  3940. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3941. (org-defkey org-mouse-map
  3942. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3943. (when org-mouse-1-follows-link
  3944. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3945. (when org-tab-follows-link
  3946. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3947. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3948. (require 'font-lock)
  3949. (defconst org-non-link-chars "]\t\n\r<>")
  3950. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3951. "shell" "elisp"))
  3952. (defvar org-link-types-re nil
  3953. "Matches a link that has a url-like prefix like \"http:\"")
  3954. (defvar org-link-re-with-space nil
  3955. "Matches a link with spaces, optional angular brackets around it.")
  3956. (defvar org-link-re-with-space2 nil
  3957. "Matches a link with spaces, optional angular brackets around it.")
  3958. (defvar org-link-re-with-space3 nil
  3959. "Matches a link with spaces, only for internal part in bracket links.")
  3960. (defvar org-angle-link-re nil
  3961. "Matches link with angular brackets, spaces are allowed.")
  3962. (defvar org-plain-link-re nil
  3963. "Matches plain link, without spaces.")
  3964. (defvar org-bracket-link-regexp nil
  3965. "Matches a link in double brackets.")
  3966. (defvar org-bracket-link-analytic-regexp nil
  3967. "Regular expression used to analyze links.
  3968. Here is what the match groups contain after a match:
  3969. 1: http:
  3970. 2: http
  3971. 3: path
  3972. 4: [desc]
  3973. 5: desc")
  3974. (defvar org-bracket-link-analytic-regexp++ nil
  3975. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3976. (defvar org-any-link-re nil
  3977. "Regular expression matching any link.")
  3978. (defun org-make-link-regexps ()
  3979. "Update the link regular expressions.
  3980. This should be called after the variable `org-link-types' has changed."
  3981. (setq org-link-types-re
  3982. (concat
  3983. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3984. org-link-re-with-space
  3985. (concat
  3986. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3987. "\\([^" org-non-link-chars " ]"
  3988. "[^" org-non-link-chars "]*"
  3989. "[^" org-non-link-chars " ]\\)>?")
  3990. org-link-re-with-space2
  3991. (concat
  3992. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3993. "\\([^" org-non-link-chars " ]"
  3994. "[^\t\n\r]*"
  3995. "[^" org-non-link-chars " ]\\)>?")
  3996. org-link-re-with-space3
  3997. (concat
  3998. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3999. "\\([^" org-non-link-chars " ]"
  4000. "[^\t\n\r]*\\)")
  4001. org-angle-link-re
  4002. (concat
  4003. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4004. "\\([^" org-non-link-chars " ]"
  4005. "[^" org-non-link-chars "]*"
  4006. "\\)>")
  4007. org-plain-link-re
  4008. (concat
  4009. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4010. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4011. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4012. org-bracket-link-regexp
  4013. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4014. org-bracket-link-analytic-regexp
  4015. (concat
  4016. "\\[\\["
  4017. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4018. "\\([^]]+\\)"
  4019. "\\]"
  4020. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4021. "\\]")
  4022. org-bracket-link-analytic-regexp++
  4023. (concat
  4024. "\\[\\["
  4025. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4026. "\\([^]]+\\)"
  4027. "\\]"
  4028. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4029. "\\]")
  4030. org-any-link-re
  4031. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4032. org-angle-link-re "\\)\\|\\("
  4033. org-plain-link-re "\\)")))
  4034. (org-make-link-regexps)
  4035. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4036. "Regular expression for fast time stamp matching.")
  4037. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4038. "Regular expression for fast time stamp matching.")
  4039. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4040. "Regular expression matching time strings for analysis.
  4041. This one does not require the space after the date, so it can be used
  4042. on a string that terminates immediately after the date.")
  4043. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4044. "Regular expression matching time strings for analysis.")
  4045. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4046. "Regular expression matching time stamps, with groups.")
  4047. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4048. "Regular expression matching time stamps (also [..]), with groups.")
  4049. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4050. "Regular expression matching a time stamp range.")
  4051. (defconst org-tr-regexp-both
  4052. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4053. "Regular expression matching a time stamp range.")
  4054. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4055. org-ts-regexp "\\)?")
  4056. "Regular expression matching a time stamp or time stamp range.")
  4057. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4058. org-ts-regexp-both "\\)?")
  4059. "Regular expression matching a time stamp or time stamp range.
  4060. The time stamps may be either active or inactive.")
  4061. (defvar org-emph-face nil)
  4062. (defun org-do-emphasis-faces (limit)
  4063. "Run through the buffer and add overlays to links."
  4064. (let (rtn a)
  4065. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4066. (if (not (= (char-after (match-beginning 3))
  4067. (char-after (match-beginning 4))))
  4068. (progn
  4069. (setq rtn t)
  4070. (setq a (assoc (match-string 3) org-emphasis-alist))
  4071. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4072. 'face
  4073. (nth 1 a))
  4074. (and (nth 4 a)
  4075. (org-remove-flyspell-overlays-in
  4076. (match-beginning 0) (match-end 0)))
  4077. (add-text-properties (match-beginning 2) (match-end 2)
  4078. '(font-lock-multiline t))
  4079. (when org-hide-emphasis-markers
  4080. (add-text-properties (match-end 4) (match-beginning 5)
  4081. '(invisible org-link))
  4082. (add-text-properties (match-beginning 3) (match-end 3)
  4083. '(invisible org-link)))))
  4084. (backward-char 1))
  4085. rtn))
  4086. (defun org-emphasize (&optional char)
  4087. "Insert or change an emphasis, i.e. a font like bold or italic.
  4088. If there is an active region, change that region to a new emphasis.
  4089. If there is no region, just insert the marker characters and position
  4090. the cursor between them.
  4091. CHAR should be either the marker character, or the first character of the
  4092. HTML tag associated with that emphasis. If CHAR is a space, the means
  4093. to remove the emphasis of the selected region.
  4094. If char is not given (for example in an interactive call) it
  4095. will be prompted for."
  4096. (interactive)
  4097. (let ((eal org-emphasis-alist) e det
  4098. (erc org-emphasis-regexp-components)
  4099. (prompt "")
  4100. (string "") beg end move tag c s)
  4101. (if (org-region-active-p)
  4102. (setq beg (region-beginning) end (region-end)
  4103. string (buffer-substring beg end))
  4104. (setq move t))
  4105. (while (setq e (pop eal))
  4106. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4107. c (aref tag 0))
  4108. (push (cons c (string-to-char (car e))) det)
  4109. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4110. (substring tag 1)))))
  4111. (setq det (nreverse det))
  4112. (unless char
  4113. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4114. (setq char (read-char-exclusive)))
  4115. (setq char (or (cdr (assoc char det)) char))
  4116. (if (equal char ?\ )
  4117. (setq s "" move nil)
  4118. (unless (assoc (char-to-string char) org-emphasis-alist)
  4119. (error "No such emphasis marker: \"%c\"" char))
  4120. (setq s (char-to-string char)))
  4121. (while (and (> (length string) 1)
  4122. (equal (substring string 0 1) (substring string -1))
  4123. (assoc (substring string 0 1) org-emphasis-alist))
  4124. (setq string (substring string 1 -1)))
  4125. (setq string (concat s string s))
  4126. (if beg (delete-region beg end))
  4127. (unless (or (bolp)
  4128. (string-match (concat "[" (nth 0 erc) "\n]")
  4129. (char-to-string (char-before (point)))))
  4130. (insert " "))
  4131. (unless (or (eobp)
  4132. (string-match (concat "[" (nth 1 erc) "\n]")
  4133. (char-to-string (char-after (point)))))
  4134. (insert " ") (backward-char 1))
  4135. (insert string)
  4136. (and move (backward-char 1))))
  4137. (defconst org-nonsticky-props
  4138. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4139. (defsubst org-rear-nonsticky-at (pos)
  4140. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4141. (defun org-activate-plain-links (limit)
  4142. "Run through the buffer and add overlays to links."
  4143. (catch 'exit
  4144. (let (f)
  4145. (if (re-search-forward org-plain-link-re limit t)
  4146. (progn
  4147. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4148. (setq f (get-text-property (match-beginning 0) 'face))
  4149. (if (or (eq f 'org-tag)
  4150. (and (listp f) (memq 'org-tag f)))
  4151. nil
  4152. (add-text-properties (match-beginning 0) (match-end 0)
  4153. (list 'mouse-face 'highlight
  4154. 'face 'org-link
  4155. 'keymap org-mouse-map))
  4156. (org-rear-nonsticky-at (match-end 0)))
  4157. t)))))
  4158. (defun org-activate-code (limit)
  4159. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4160. (progn
  4161. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4162. (remove-text-properties (match-beginning 0) (match-end 0)
  4163. '(display t invisible t intangible t))
  4164. t)))
  4165. (defun org-fontify-meta-lines-and-blocks (limit)
  4166. "Fontify #+ lines and blocks, in the correct ways."
  4167. (let ((case-fold-search t))
  4168. (if (re-search-forward
  4169. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4170. limit t)
  4171. (let ((beg (match-beginning 0))
  4172. (beg1 (line-beginning-position 2))
  4173. (dc1 (downcase (match-string 2)))
  4174. (dc3 (downcase (match-string 3)))
  4175. end end1 quoting block-type)
  4176. (cond
  4177. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4178. ;; a single line of backend-specific content
  4179. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4180. (remove-text-properties (match-beginning 0) (match-end 0)
  4181. '(display t invisible t intangible t))
  4182. (add-text-properties (match-beginning 1) (match-end 3)
  4183. '(font-lock-fontified t face org-meta-line))
  4184. (add-text-properties (match-beginning 6) (match-end 6)
  4185. '(font-lock-fontified t face org-block))
  4186. t)
  4187. ((and (match-end 4) (equal dc3 "begin"))
  4188. ;; Truely a block
  4189. (setq block-type (downcase (match-string 5))
  4190. quoting (member block-type org-protecting-blocks))
  4191. (when (re-search-forward
  4192. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4193. nil t) ;; on purpose, we look further than LIMIT
  4194. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4195. (when quoting
  4196. (remove-text-properties beg end
  4197. '(display t invisible t intangible t)))
  4198. (add-text-properties
  4199. beg end
  4200. '(font-lock-fontified t font-lock-multiline t))
  4201. (add-text-properties beg beg1 '(face org-meta-line))
  4202. (add-text-properties end1 end '(face org-meta-line))
  4203. (cond
  4204. (quoting
  4205. (add-text-properties beg1 end1 '(face org-block)))
  4206. ((not org-fontify-quote-and-verse-blocks))
  4207. ((string= block-type "quote")
  4208. (add-text-properties beg1 end1 '(face org-quote)))
  4209. ((string= block-type "verse")
  4210. (add-text-properties beg1 end1 '(face org-verse))))
  4211. t))
  4212. ((member dc1 '("title:" "author:" "email:" "date:"))
  4213. (add-text-properties
  4214. beg (match-end 3)
  4215. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4216. '(font-lock-fontified t invisible t)
  4217. '(font-lock-fontified t face org-document-info-keyword)))
  4218. (add-text-properties
  4219. (match-beginning 6) (match-end 6)
  4220. (if (string-equal dc1 "title:")
  4221. '(font-lock-fontified t face org-document-title)
  4222. '(font-lock-fontified t face org-document-info))))
  4223. ((not (member (char-after beg) '(?\ ?\t)))
  4224. ;; just any other in-buffer setting, but not indented
  4225. (add-text-properties
  4226. beg (match-end 0)
  4227. '(font-lock-fontified t face org-meta-line))
  4228. t)
  4229. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4230. "orgtbl:" "tblfm:" "tblname:"))
  4231. (and (match-end 4) (equal dc3 "attr")))
  4232. (add-text-properties
  4233. beg (match-end 0)
  4234. '(font-lock-fontified t face org-meta-line))
  4235. t)
  4236. ((member dc3 '(" " ""))
  4237. (add-text-properties
  4238. beg (match-end 0)
  4239. '(font-lock-fontified t face font-lock-comment-face)))
  4240. (t nil))))))
  4241. (defun org-activate-angle-links (limit)
  4242. "Run through the buffer and add overlays to links."
  4243. (if (re-search-forward org-angle-link-re limit t)
  4244. (progn
  4245. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4246. (add-text-properties (match-beginning 0) (match-end 0)
  4247. (list 'mouse-face 'highlight
  4248. 'keymap org-mouse-map))
  4249. (org-rear-nonsticky-at (match-end 0))
  4250. t)))
  4251. (defun org-activate-footnote-links (limit)
  4252. "Run through the buffer and add overlays to links."
  4253. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4254. limit t)
  4255. (progn
  4256. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4257. (add-text-properties (match-beginning 2) (match-end 2)
  4258. (list 'mouse-face 'highlight
  4259. 'keymap org-mouse-map
  4260. 'help-echo
  4261. (if (= (point-at-bol) (match-beginning 2))
  4262. "Footnote definition"
  4263. "Footnote reference")
  4264. ))
  4265. (org-rear-nonsticky-at (match-end 2))
  4266. t)))
  4267. (defun org-activate-bracket-links (limit)
  4268. "Run through the buffer and add overlays to bracketed links."
  4269. (if (re-search-forward org-bracket-link-regexp limit t)
  4270. (let* ((help (concat "LINK: "
  4271. (org-match-string-no-properties 1)))
  4272. ;; FIXME: above we should remove the escapes.
  4273. ;; but that requires another match, protecting match data,
  4274. ;; a lot of overhead for font-lock.
  4275. (ip (org-maybe-intangible
  4276. (list 'invisible 'org-link
  4277. 'keymap org-mouse-map 'mouse-face 'highlight
  4278. 'font-lock-multiline t 'help-echo help)))
  4279. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4280. 'font-lock-multiline t 'help-echo help)))
  4281. ;; We need to remove the invisible property here. Table narrowing
  4282. ;; may have made some of this invisible.
  4283. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4284. (remove-text-properties (match-beginning 0) (match-end 0)
  4285. '(invisible nil))
  4286. (if (match-end 3)
  4287. (progn
  4288. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4289. (org-rear-nonsticky-at (match-beginning 3))
  4290. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4291. (org-rear-nonsticky-at (match-end 3))
  4292. (add-text-properties (match-end 3) (match-end 0) ip)
  4293. (org-rear-nonsticky-at (match-end 0)))
  4294. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4295. (org-rear-nonsticky-at (match-beginning 1))
  4296. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4297. (org-rear-nonsticky-at (match-end 1))
  4298. (add-text-properties (match-end 1) (match-end 0) ip)
  4299. (org-rear-nonsticky-at (match-end 0)))
  4300. t)))
  4301. (defun org-activate-dates (limit)
  4302. "Run through the buffer and add overlays to dates."
  4303. (if (re-search-forward org-tsr-regexp-both limit t)
  4304. (progn
  4305. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4306. (add-text-properties (match-beginning 0) (match-end 0)
  4307. (list 'mouse-face 'highlight
  4308. 'keymap org-mouse-map))
  4309. (org-rear-nonsticky-at (match-end 0))
  4310. (when org-display-custom-times
  4311. (if (match-end 3)
  4312. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4313. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4314. t)))
  4315. (defvar org-target-link-regexp nil
  4316. "Regular expression matching radio targets in plain text.")
  4317. (make-variable-buffer-local 'org-target-link-regexp)
  4318. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4319. "Regular expression matching a link target.")
  4320. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4321. "Regular expression matching a radio target.")
  4322. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4323. "Regular expression matching any target.")
  4324. (defun org-activate-target-links (limit)
  4325. "Run through the buffer and add overlays to target matches."
  4326. (when org-target-link-regexp
  4327. (let ((case-fold-search t))
  4328. (if (re-search-forward org-target-link-regexp limit t)
  4329. (progn
  4330. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4331. (add-text-properties (match-beginning 0) (match-end 0)
  4332. (list 'mouse-face 'highlight
  4333. 'keymap org-mouse-map
  4334. 'help-echo "Radio target link"
  4335. 'org-linked-text t))
  4336. (org-rear-nonsticky-at (match-end 0))
  4337. t)))))
  4338. (defun org-update-radio-target-regexp ()
  4339. "Find all radio targets in this file and update the regular expression."
  4340. (interactive)
  4341. (when (memq 'radio org-activate-links)
  4342. (setq org-target-link-regexp
  4343. (org-make-target-link-regexp (org-all-targets 'radio)))
  4344. (org-restart-font-lock)))
  4345. (defun org-hide-wide-columns (limit)
  4346. (let (s e)
  4347. (setq s (text-property-any (point) (or limit (point-max))
  4348. 'org-cwidth t))
  4349. (when s
  4350. (setq e (next-single-property-change s 'org-cwidth))
  4351. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4352. (goto-char e)
  4353. t)))
  4354. (defvar org-latex-and-specials-regexp nil
  4355. "Regular expression for highlighting export special stuff.")
  4356. (defvar org-match-substring-regexp)
  4357. (defvar org-match-substring-with-braces-regexp)
  4358. ;; This should be with the exporter code, but we also use if for font-locking
  4359. (defconst org-export-html-special-string-regexps
  4360. '(("\\\\-" . "&shy;")
  4361. ("---\\([^-]\\)" . "&mdash;\\1")
  4362. ("--\\([^-]\\)" . "&ndash;\\1")
  4363. ("\\.\\.\\." . "&hellip;"))
  4364. "Regular expressions for special string conversion.")
  4365. (defun org-compute-latex-and-specials-regexp ()
  4366. "Compute regular expression for stuff treated specially by exporters."
  4367. (if (not org-highlight-latex-fragments-and-specials)
  4368. (org-set-local 'org-latex-and-specials-regexp nil)
  4369. (require 'org-exp)
  4370. (let*
  4371. ((matchers (plist-get org-format-latex-options :matchers))
  4372. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4373. org-latex-regexps)))
  4374. (org-export-allow-BIND nil)
  4375. (options (org-combine-plists (org-default-export-plist)
  4376. (org-infile-export-plist)))
  4377. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4378. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4379. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4380. (org-export-html-expand (plist-get options :expand-quoted-html))
  4381. (org-export-with-special-strings (plist-get options :special-strings))
  4382. (re-sub
  4383. (cond
  4384. ((equal org-export-with-sub-superscripts '{})
  4385. (list org-match-substring-with-braces-regexp))
  4386. (org-export-with-sub-superscripts
  4387. (list org-match-substring-regexp))
  4388. (t nil)))
  4389. (re-latex
  4390. (if org-export-with-LaTeX-fragments
  4391. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4392. (re-macros
  4393. (if org-export-with-TeX-macros
  4394. (list (concat "\\\\"
  4395. (regexp-opt
  4396. (append (mapcar 'car (append org-entities-user
  4397. org-entities))
  4398. (if (boundp 'org-latex-entities)
  4399. (mapcar (lambda (x)
  4400. (or (car-safe x) x))
  4401. org-latex-entities)
  4402. nil))
  4403. 'words))) ; FIXME
  4404. ))
  4405. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4406. (re-special (if org-export-with-special-strings
  4407. (mapcar (lambda (x) (car x))
  4408. org-export-html-special-string-regexps)))
  4409. (re-rest
  4410. (delq nil
  4411. (list
  4412. (if org-export-html-expand "@<[^>\n]+>")
  4413. ))))
  4414. (org-set-local
  4415. 'org-latex-and-specials-regexp
  4416. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4417. re-rest) "\\|")))))
  4418. (defun org-do-latex-and-special-faces (limit)
  4419. "Run through the buffer and add overlays to links."
  4420. (when org-latex-and-specials-regexp
  4421. (let (rtn d)
  4422. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4423. limit t))
  4424. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4425. 'face))
  4426. '(org-code org-verbatim underline)))
  4427. (progn
  4428. (setq rtn t
  4429. d (cond ((member (char-after (1+ (match-beginning 0)))
  4430. '(?_ ?^)) 1)
  4431. (t 0)))
  4432. (font-lock-prepend-text-property
  4433. (+ d (match-beginning 0)) (match-end 0)
  4434. 'face 'org-latex-and-export-specials)
  4435. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4436. '(font-lock-multiline t)))))
  4437. rtn)))
  4438. (defun org-restart-font-lock ()
  4439. "Restart font-lock-mode, to force refontification."
  4440. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4441. (font-lock-mode -1)
  4442. (font-lock-mode 1)))
  4443. (defun org-all-targets (&optional radio)
  4444. "Return a list of all targets in this file.
  4445. With optional argument RADIO, only find radio targets."
  4446. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4447. rtn)
  4448. (save-excursion
  4449. (goto-char (point-min))
  4450. (while (re-search-forward re nil t)
  4451. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4452. rtn)))
  4453. (defun org-make-target-link-regexp (targets)
  4454. "Make regular expression matching all strings in TARGETS.
  4455. The regular expression finds the targets also if there is a line break
  4456. between words."
  4457. (and targets
  4458. (concat
  4459. "\\<\\("
  4460. (mapconcat
  4461. (lambda (x)
  4462. (while (string-match " +" x)
  4463. (setq x (replace-match "\\s-+" t t x)))
  4464. x)
  4465. targets
  4466. "\\|")
  4467. "\\)\\>")))
  4468. (defun org-activate-tags (limit)
  4469. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4470. (progn
  4471. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4472. (add-text-properties (match-beginning 1) (match-end 1)
  4473. (list 'mouse-face 'highlight
  4474. 'keymap org-mouse-map))
  4475. (org-rear-nonsticky-at (match-end 1))
  4476. t)))
  4477. (defun org-outline-level ()
  4478. "Compute the outline level of the heading at point.
  4479. This function assumes that the cursor is at the beginning of a line matched
  4480. by outline-regexp. Otherwise it returns garbage.
  4481. If this is called at a normal headline, the level is the number of stars.
  4482. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4483. For plain list items, if they are matched by `outline-regexp', this returns
  4484. 1000 plus the line indentation."
  4485. (save-excursion
  4486. (looking-at outline-regexp)
  4487. (if (match-beginning 1)
  4488. (+ (org-get-string-indentation (match-string 1)) 1000)
  4489. (1- (- (match-end 0) (match-beginning 0))))))
  4490. (defvar org-font-lock-keywords nil)
  4491. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4492. "Regular expression matching a property line.")
  4493. (defvar org-font-lock-hook nil
  4494. "Functions to be called for special font lock stuff.")
  4495. (defun org-font-lock-hook (limit)
  4496. (run-hook-with-args 'org-font-lock-hook limit))
  4497. (defun org-set-font-lock-defaults ()
  4498. (let* ((em org-fontify-emphasized-text)
  4499. (lk org-activate-links)
  4500. (org-font-lock-extra-keywords
  4501. (list
  4502. ;; Call the hook
  4503. '(org-font-lock-hook)
  4504. ;; Headlines
  4505. `(,(if org-fontify-whole-heading-line
  4506. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4507. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4508. (1 (org-get-level-face 1))
  4509. (2 (org-get-level-face 2))
  4510. (3 (org-get-level-face 3)))
  4511. ;; Table lines
  4512. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4513. (1 'org-table t))
  4514. ;; Table internals
  4515. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4516. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4517. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4518. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4519. ;; Drawers
  4520. (list org-drawer-regexp '(0 'org-special-keyword t))
  4521. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4522. ;; Properties
  4523. (list org-property-re
  4524. '(1 'org-special-keyword t)
  4525. '(3 'org-property-value t))
  4526. ;; Links
  4527. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4528. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4529. (if (memq 'plain lk) '(org-activate-plain-links))
  4530. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4531. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4532. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4533. (if (memq 'footnote lk) '(org-activate-footnote-links
  4534. (2 'org-footnote t)))
  4535. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4536. '(org-hide-wide-columns (0 nil append))
  4537. ;; TODO lines
  4538. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4539. '(1 (org-get-todo-face 1) t))
  4540. ;; DONE
  4541. (if org-fontify-done-headline
  4542. (list (concat "^[*]+ +\\<\\("
  4543. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4544. "\\)\\(.*\\)")
  4545. '(2 'org-headline-done t))
  4546. nil)
  4547. ;; Priorities
  4548. '(org-font-lock-add-priority-faces)
  4549. ;; Tags
  4550. '(org-font-lock-add-tag-faces)
  4551. ;; Special keywords
  4552. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4553. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4554. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4555. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4556. ;; Emphasis
  4557. (if em
  4558. (if (featurep 'xemacs)
  4559. '(org-do-emphasis-faces (0 nil append))
  4560. '(org-do-emphasis-faces)))
  4561. ;; Checkboxes
  4562. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4563. 2 'org-checkbox prepend)
  4564. (if org-provide-checkbox-statistics
  4565. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4566. (0 (org-get-checkbox-statistics-face) t)))
  4567. ;; Description list items
  4568. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4569. 2 'bold prepend)
  4570. ;; ARCHIVEd headings
  4571. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4572. '(1 'org-archived prepend))
  4573. ;; Specials
  4574. '(org-do-latex-and-special-faces)
  4575. ;; Code
  4576. '(org-activate-code (1 'org-code t))
  4577. ;; COMMENT
  4578. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4579. "\\|" org-quote-string "\\)\\>")
  4580. '(1 'org-special-keyword t))
  4581. '("^#.*" (0 'font-lock-comment-face t))
  4582. ;; Blocks and meta lines
  4583. '(org-fontify-meta-lines-and-blocks)
  4584. )))
  4585. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4586. ;; Now set the full font-lock-keywords
  4587. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4588. (org-set-local 'font-lock-defaults
  4589. '(org-font-lock-keywords t nil nil backward-paragraph))
  4590. (kill-local-variable 'font-lock-keywords) nil))
  4591. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4592. "Fontify string S like in Org-mode"
  4593. (with-temp-buffer
  4594. (insert s)
  4595. (let ((org-odd-levels-only odd-levels))
  4596. (org-mode)
  4597. (font-lock-fontify-buffer)
  4598. (buffer-string))))
  4599. (defvar org-m nil)
  4600. (defvar org-l nil)
  4601. (defvar org-f nil)
  4602. (defun org-get-level-face (n)
  4603. "Get the right face for match N in font-lock matching of headlines."
  4604. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4605. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4606. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4607. (cond
  4608. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4609. ((eq n 2) org-f)
  4610. (t (if org-level-color-stars-only nil org-f))))
  4611. (defun org-get-todo-face (kwd)
  4612. "Get the right face for a TODO keyword KWD.
  4613. If KWD is a number, get the corresponding match group."
  4614. (if (numberp kwd) (setq kwd (match-string kwd)))
  4615. (or (org-face-from-face-or-color
  4616. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4617. (and (member kwd org-done-keywords) 'org-done)
  4618. 'org-todo))
  4619. (defun org-face-from-face-or-color (context inherit face-or-color)
  4620. "Create a face list that inherits INHERIT, but sets the foreground color.
  4621. When FACE-OR-COLOR is not a string, just return it."
  4622. (if (stringp face-or-color)
  4623. (list :inherit inherit
  4624. (cdr (assoc context org-faces-easy-properties))
  4625. face-or-color)
  4626. face-or-color))
  4627. (defun org-font-lock-add-tag-faces (limit)
  4628. "Add the special tag faces."
  4629. (when (and org-tag-faces org-tags-special-faces-re)
  4630. (while (re-search-forward org-tags-special-faces-re limit t)
  4631. (add-text-properties (match-beginning 1) (match-end 1)
  4632. (list 'face (org-get-tag-face 1)
  4633. 'font-lock-fontified t))
  4634. (backward-char 1))))
  4635. (defun org-font-lock-add-priority-faces (limit)
  4636. "Add the special priority faces."
  4637. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4638. (add-text-properties
  4639. (match-beginning 0) (match-end 0)
  4640. (list 'face (or (org-face-from-face-or-color
  4641. 'priority 'org-special-keyword
  4642. (cdr (assoc (char-after (match-beginning 1))
  4643. org-priority-faces)))
  4644. 'org-special-keyword)
  4645. 'font-lock-fontified t))))
  4646. (defun org-get-tag-face (kwd)
  4647. "Get the right face for a TODO keyword KWD.
  4648. If KWD is a number, get the corresponding match group."
  4649. (if (numberp kwd) (setq kwd (match-string kwd)))
  4650. (or (org-face-from-face-or-color
  4651. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4652. 'org-tag))
  4653. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4654. "Remove fontification and activation overlays from links."
  4655. (font-lock-default-unfontify-region beg end)
  4656. (let* ((buffer-undo-list t)
  4657. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4658. (inhibit-modification-hooks t)
  4659. deactivate-mark buffer-file-name buffer-file-truename)
  4660. (remove-text-properties
  4661. beg end
  4662. (if org-indent-mode
  4663. ;; also remove line-prefix and wrap-prefix properties
  4664. '(mouse-face t keymap t org-linked-text t
  4665. invisible t intangible t
  4666. line-prefix t wrap-prefix t
  4667. org-no-flyspell t)
  4668. '(mouse-face t keymap t org-linked-text t
  4669. invisible t intangible t
  4670. org-no-flyspell t)))))
  4671. ;;;; Visibility cycling, including org-goto and indirect buffer
  4672. ;;; Cycling
  4673. (defvar org-cycle-global-status nil)
  4674. (make-variable-buffer-local 'org-cycle-global-status)
  4675. (defvar org-cycle-subtree-status nil)
  4676. (make-variable-buffer-local 'org-cycle-subtree-status)
  4677. ;;;###autoload
  4678. (defvar org-inlinetask-min-level)
  4679. (defun org-cycle (&optional arg)
  4680. "TAB-action and visibility cycling for Org-mode.
  4681. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4682. is outline visibility cycling, but it also invokes other actions
  4683. in special contexts.
  4684. - When this function is called with a prefix argument, rotate the entire
  4685. buffer through 3 states (global cycling)
  4686. 1. OVERVIEW: Show only top-level headlines.
  4687. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4688. 3. SHOW ALL: Show everything.
  4689. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4690. determined by the variable `org-startup-folded', and by any VISIBILITY
  4691. properties in the buffer.
  4692. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4693. including any drawers.
  4694. - When inside a table, re-align the table and move to the next field.
  4695. - When point is at the beginning of a headline, rotate the subtree started
  4696. by this line through 3 different states (local cycling)
  4697. 1. FOLDED: Only the main headline is shown.
  4698. 2. CHILDREN: The main headline and the direct children are shown.
  4699. From this state, you can move to one of the children
  4700. and zoom in further.
  4701. 3. SUBTREE: Show the entire subtree, including body text.
  4702. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4703. - When point is at the beginning of an empty headline and the variable
  4704. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4705. of the headline by demoting and promoting it to likely levels. This
  4706. speeds up creation document structure by presing TAB once or several
  4707. times right after creating a new headline.
  4708. - When there is a numeric prefix, go up to a heading with level ARG, do
  4709. a `show-subtree' and return to the previous cursor position. If ARG
  4710. is negative, go up that many levels.
  4711. - When point is not at the beginning of a headline, execute the global
  4712. binding for TAB, which is re-indenting the line. See the option
  4713. `org-cycle-emulate-tab' for details.
  4714. - Special case: if point is at the beginning of the buffer and there is
  4715. no headline in line 1, this function will act as if called with prefix arg.
  4716. But only if also the variable `org-cycle-global-at-bob' is t."
  4717. (interactive "P")
  4718. (org-load-modules-maybe)
  4719. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4720. (and org-cycle-level-after-item/entry-creation
  4721. (or (org-cycle-level)
  4722. (org-cycle-item-indentation))))
  4723. (let* ((limit-level
  4724. (or org-cycle-max-level
  4725. (and (boundp 'org-inlinetask-min-level)
  4726. org-inlinetask-min-level
  4727. (1- org-inlinetask-min-level))))
  4728. (nstars (and limit-level
  4729. (if org-odd-levels-only
  4730. (and limit-level (1- (* limit-level 2)))
  4731. limit-level)))
  4732. (outline-regexp
  4733. (cond
  4734. ((not (org-mode-p)) outline-regexp)
  4735. ((or (eq org-cycle-include-plain-lists 'integrate)
  4736. (and org-cycle-include-plain-lists (org-at-item-p)))
  4737. (concat "\\(?:\\*"
  4738. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4739. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4740. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4741. (bob-special (and org-cycle-global-at-bob (bobp)
  4742. (not (looking-at outline-regexp))))
  4743. (org-cycle-hook
  4744. (if bob-special
  4745. (delq 'org-optimize-window-after-visibility-change
  4746. (copy-sequence org-cycle-hook))
  4747. org-cycle-hook))
  4748. (pos (point)))
  4749. (if (or bob-special (equal arg '(4)))
  4750. ;; special case: use global cycling
  4751. (setq arg t))
  4752. (cond
  4753. ((equal arg '(16))
  4754. (org-set-startup-visibility)
  4755. (message "Startup visibility, plus VISIBILITY properties"))
  4756. ((equal arg '(64))
  4757. (show-all)
  4758. (message "Entire buffer visible, including drawers"))
  4759. ((org-at-table-p 'any)
  4760. ;; Enter the table or move to the next field in the table
  4761. (if (org-at-table.el-p)
  4762. (message "Use C-c ' to edit table.el tables")
  4763. (if arg (org-table-edit-field t)
  4764. (org-table-justify-field-maybe)
  4765. (call-interactively 'org-table-next-field))))
  4766. ((run-hook-with-args-until-success
  4767. 'org-tab-after-check-for-table-hook))
  4768. ((eq arg t) ;; Global cycling
  4769. (org-cycle-internal-global))
  4770. ((and org-drawers org-drawer-regexp
  4771. (save-excursion
  4772. (beginning-of-line 1)
  4773. (looking-at org-drawer-regexp)))
  4774. ;; Toggle block visibility
  4775. (org-flag-drawer
  4776. (not (get-char-property (match-end 0) 'invisible))))
  4777. ((integerp arg)
  4778. ;; Show-subtree, ARG levels up from here.
  4779. (save-excursion
  4780. (org-back-to-heading)
  4781. (outline-up-heading (if (< arg 0) (- arg)
  4782. (- (funcall outline-level) arg)))
  4783. (org-show-subtree)))
  4784. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4785. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4786. (org-cycle-internal-local))
  4787. ;; TAB emulation and template completion
  4788. (buffer-read-only (org-back-to-heading))
  4789. ((run-hook-with-args-until-success
  4790. 'org-tab-after-check-for-cycling-hook))
  4791. ((org-try-structure-completion))
  4792. ((org-try-cdlatex-tab))
  4793. ((run-hook-with-args-until-success
  4794. 'org-tab-before-tab-emulation-hook))
  4795. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4796. (or (not (bolp))
  4797. (not (looking-at outline-regexp))))
  4798. (call-interactively (global-key-binding "\t")))
  4799. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4800. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4801. (or (and (eq org-cycle-emulate-tab 'white)
  4802. (= (match-end 0) (point-at-eol)))
  4803. (and (eq org-cycle-emulate-tab 'whitestart)
  4804. (>= (match-end 0) pos))))
  4805. t
  4806. (eq org-cycle-emulate-tab t))
  4807. (call-interactively (global-key-binding "\t")))
  4808. (t (save-excursion
  4809. (org-back-to-heading)
  4810. (org-cycle)))))))
  4811. (defun org-cycle-internal-global ()
  4812. "Do the global cycling action."
  4813. (cond
  4814. ((and (eq last-command this-command)
  4815. (eq org-cycle-global-status 'overview))
  4816. ;; We just created the overview - now do table of contents
  4817. ;; This can be slow in very large buffers, so indicate action
  4818. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4819. (message "CONTENTS...")
  4820. (org-content)
  4821. (message "CONTENTS...done")
  4822. (setq org-cycle-global-status 'contents)
  4823. (run-hook-with-args 'org-cycle-hook 'contents))
  4824. ((and (eq last-command this-command)
  4825. (eq org-cycle-global-status 'contents))
  4826. ;; We just showed the table of contents - now show everything
  4827. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4828. (show-all)
  4829. (message "SHOW ALL")
  4830. (setq org-cycle-global-status 'all)
  4831. (run-hook-with-args 'org-cycle-hook 'all))
  4832. (t
  4833. ;; Default action: go to overview
  4834. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4835. (org-overview)
  4836. (message "OVERVIEW")
  4837. (setq org-cycle-global-status 'overview)
  4838. (run-hook-with-args 'org-cycle-hook 'overview))))
  4839. (defun org-cycle-internal-local ()
  4840. "Do the local cycling action."
  4841. (org-back-to-heading)
  4842. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4843. ;; First, some boundaries
  4844. (save-excursion
  4845. (org-back-to-heading)
  4846. (setq level (funcall outline-level))
  4847. (save-excursion
  4848. (beginning-of-line 2)
  4849. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4850. ; XEmacs does not have `next-single-char-property-change'
  4851. ; I'm not sure about Emacs 21.
  4852. (while (and (not (eobp)) ;; this is like `next-line'
  4853. (get-char-property (1- (point)) 'invisible))
  4854. (beginning-of-line 2))
  4855. (while (and (not (eobp)) ;; this is like `next-line'
  4856. (get-char-property (1- (point)) 'invisible))
  4857. (goto-char (next-single-char-property-change (point) 'invisible))
  4858. ;;;??? (or (bolp) (beginning-of-line 2))))
  4859. (and (eolp) (beginning-of-line 2))))
  4860. (setq eol (point)))
  4861. (outline-end-of-heading) (setq eoh (point))
  4862. (save-excursion
  4863. (outline-next-heading)
  4864. (setq has-children (and (org-at-heading-p t)
  4865. (> (funcall outline-level) level))))
  4866. (org-end-of-subtree t)
  4867. (unless (eobp)
  4868. (skip-chars-forward " \t\n")
  4869. (beginning-of-line 1) ; in case this is an item
  4870. )
  4871. (setq eos (if (eobp) (point) (1- (point)))))
  4872. ;; Find out what to do next and set `this-command'
  4873. (cond
  4874. ((= eos eoh)
  4875. ;; Nothing is hidden behind this heading
  4876. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4877. (message "EMPTY ENTRY")
  4878. (setq org-cycle-subtree-status nil)
  4879. (save-excursion
  4880. (goto-char eos)
  4881. (outline-next-heading)
  4882. (if (org-invisible-p) (org-flag-heading nil))))
  4883. ((and (or (>= eol eos)
  4884. (not (string-match "\\S-" (buffer-substring eol eos))))
  4885. (or has-children
  4886. (not (setq children-skipped
  4887. org-cycle-skip-children-state-if-no-children))))
  4888. ;; Entire subtree is hidden in one line: children view
  4889. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4890. (org-show-entry)
  4891. (show-children)
  4892. (message "CHILDREN")
  4893. (save-excursion
  4894. (goto-char eos)
  4895. (outline-next-heading)
  4896. (if (org-invisible-p) (org-flag-heading nil)))
  4897. (setq org-cycle-subtree-status 'children)
  4898. (run-hook-with-args 'org-cycle-hook 'children))
  4899. ((or children-skipped
  4900. (and (eq last-command this-command)
  4901. (eq org-cycle-subtree-status 'children)))
  4902. ;; We just showed the children, or no children are there,
  4903. ;; now show everything.
  4904. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4905. (org-show-subtree)
  4906. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4907. (setq org-cycle-subtree-status 'subtree)
  4908. (run-hook-with-args 'org-cycle-hook 'subtree))
  4909. (t
  4910. ;; Default action: hide the subtree.
  4911. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4912. (hide-subtree)
  4913. (message "FOLDED")
  4914. (setq org-cycle-subtree-status 'folded)
  4915. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4916. ;;;###autoload
  4917. (defun org-global-cycle (&optional arg)
  4918. "Cycle the global visibility. For details see `org-cycle'.
  4919. With C-u prefix arg, switch to startup visibility.
  4920. With a numeric prefix, show all headlines up to that level."
  4921. (interactive "P")
  4922. (let ((org-cycle-include-plain-lists
  4923. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4924. (cond
  4925. ((integerp arg)
  4926. (show-all)
  4927. (hide-sublevels arg)
  4928. (setq org-cycle-global-status 'contents))
  4929. ((equal arg '(4))
  4930. (org-set-startup-visibility)
  4931. (message "Startup visibility, plus VISIBILITY properties."))
  4932. (t
  4933. (org-cycle '(4))))))
  4934. (defun org-set-startup-visibility ()
  4935. "Set the visibility required by startup options and properties."
  4936. (cond
  4937. ((eq org-startup-folded t)
  4938. (org-cycle '(4)))
  4939. ((eq org-startup-folded 'content)
  4940. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4941. (org-cycle '(4)) (org-cycle '(4)))))
  4942. (unless (eq org-startup-folded 'showeverything)
  4943. (if org-hide-block-startup (org-hide-block-all))
  4944. (org-set-visibility-according-to-property 'no-cleanup)
  4945. (org-cycle-hide-archived-subtrees 'all)
  4946. (org-cycle-hide-drawers 'all)
  4947. (org-cycle-show-empty-lines 'all)))
  4948. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4949. "Switch subtree visibilities according to :VISIBILITY: property."
  4950. (interactive)
  4951. (let (org-show-entry-below state)
  4952. (save-excursion
  4953. (goto-char (point-min))
  4954. (while (re-search-forward
  4955. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4956. nil t)
  4957. (setq state (match-string 1))
  4958. (save-excursion
  4959. (org-back-to-heading t)
  4960. (hide-subtree)
  4961. (org-reveal)
  4962. (cond
  4963. ((equal state '("fold" "folded"))
  4964. (hide-subtree))
  4965. ((equal state "children")
  4966. (org-show-hidden-entry)
  4967. (show-children))
  4968. ((equal state "content")
  4969. (save-excursion
  4970. (save-restriction
  4971. (org-narrow-to-subtree)
  4972. (org-content))))
  4973. ((member state '("all" "showall"))
  4974. (show-subtree)))))
  4975. (unless no-cleanup
  4976. (org-cycle-hide-archived-subtrees 'all)
  4977. (org-cycle-hide-drawers 'all)
  4978. (org-cycle-show-empty-lines 'all)))))
  4979. (defun org-overview ()
  4980. "Switch to overview mode, showing only top-level headlines.
  4981. Really, this shows all headlines with level equal or greater than the level
  4982. of the first headline in the buffer. This is important, because if the
  4983. first headline is not level one, then (hide-sublevels 1) gives confusing
  4984. results."
  4985. (interactive)
  4986. (let ((level (save-excursion
  4987. (goto-char (point-min))
  4988. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4989. (progn
  4990. (goto-char (match-beginning 0))
  4991. (funcall outline-level))))))
  4992. (and level (hide-sublevels level))))
  4993. (defun org-content (&optional arg)
  4994. "Show all headlines in the buffer, like a table of contents.
  4995. With numerical argument N, show content up to level N."
  4996. (interactive "P")
  4997. (save-excursion
  4998. ;; Visit all headings and show their offspring
  4999. (and (integerp arg) (org-overview))
  5000. (goto-char (point-max))
  5001. (catch 'exit
  5002. (while (and (progn (condition-case nil
  5003. (outline-previous-visible-heading 1)
  5004. (error (goto-char (point-min))))
  5005. t)
  5006. (looking-at outline-regexp))
  5007. (if (integerp arg)
  5008. (show-children (1- arg))
  5009. (show-branches))
  5010. (if (bobp) (throw 'exit nil))))))
  5011. (defun org-optimize-window-after-visibility-change (state)
  5012. "Adjust the window after a change in outline visibility.
  5013. This function is the default value of the hook `org-cycle-hook'."
  5014. (when (get-buffer-window (current-buffer))
  5015. (cond
  5016. ((eq state 'content) nil)
  5017. ((eq state 'all) nil)
  5018. ((eq state 'folded) nil)
  5019. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5020. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5021. (defun org-remove-empty-overlays-at (pos)
  5022. "Remove outline overlays that do not contain non-white stuff."
  5023. (mapc
  5024. (lambda (o)
  5025. (and (eq 'outline (overlay-get o 'invisible))
  5026. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5027. (overlay-end o))))
  5028. (delete-overlay o)))
  5029. (org-overlays-at pos)))
  5030. (defun org-clean-visibility-after-subtree-move ()
  5031. "Fix visibility issues after moving a subtree."
  5032. ;; First, find a reasonable region to look at:
  5033. ;; Start two siblings above, end three below
  5034. (let* ((beg (save-excursion
  5035. (and (org-get-last-sibling)
  5036. (org-get-last-sibling))
  5037. (point)))
  5038. (end (save-excursion
  5039. (and (org-get-next-sibling)
  5040. (org-get-next-sibling)
  5041. (org-get-next-sibling))
  5042. (if (org-at-heading-p)
  5043. (point-at-eol)
  5044. (point))))
  5045. (level (looking-at "\\*+"))
  5046. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5047. (save-excursion
  5048. (save-restriction
  5049. (narrow-to-region beg end)
  5050. (when re
  5051. ;; Properly fold already folded siblings
  5052. (goto-char (point-min))
  5053. (while (re-search-forward re nil t)
  5054. (if (and (not (org-invisible-p))
  5055. (save-excursion
  5056. (goto-char (point-at-eol)) (org-invisible-p)))
  5057. (hide-entry))))
  5058. (org-cycle-show-empty-lines 'overview)
  5059. (org-cycle-hide-drawers 'overview)))))
  5060. (defun org-cycle-show-empty-lines (state)
  5061. "Show empty lines above all visible headlines.
  5062. The region to be covered depends on STATE when called through
  5063. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5064. entire buffer covered. Note that an empty line is only shown if there
  5065. are at least `org-cycle-separator-lines' empty lines before the headline."
  5066. (when (not (= org-cycle-separator-lines 0))
  5067. (save-excursion
  5068. (let* ((n (abs org-cycle-separator-lines))
  5069. (re (cond
  5070. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5071. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5072. (t (let ((ns (number-to-string (- n 2))))
  5073. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5074. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5075. beg end b e)
  5076. (cond
  5077. ((memq state '(overview contents t))
  5078. (setq beg (point-min) end (point-max)))
  5079. ((memq state '(children folded))
  5080. (setq beg (point) end (progn (org-end-of-subtree t t)
  5081. (beginning-of-line 2)
  5082. (point)))))
  5083. (when beg
  5084. (goto-char beg)
  5085. (while (re-search-forward re end t)
  5086. (unless (get-char-property (match-end 1) 'invisible)
  5087. (setq e (match-end 1))
  5088. (if (< org-cycle-separator-lines 0)
  5089. (setq b (save-excursion
  5090. (goto-char (match-beginning 0))
  5091. (org-back-over-empty-lines)
  5092. (if (save-excursion
  5093. (goto-char (max (point-min) (1- (point))))
  5094. (org-on-heading-p))
  5095. (1- (point))
  5096. (point))))
  5097. (setq b (match-beginning 1)))
  5098. (outline-flag-region b e nil)))))))
  5099. ;; Never hide empty lines at the end of the file.
  5100. (save-excursion
  5101. (goto-char (point-max))
  5102. (outline-previous-heading)
  5103. (outline-end-of-heading)
  5104. (if (and (looking-at "[ \t\n]+")
  5105. (= (match-end 0) (point-max)))
  5106. (outline-flag-region (point) (match-end 0) nil))))
  5107. (defun org-show-empty-lines-in-parent ()
  5108. "Move to the parent and re-show empty lines before visible headlines."
  5109. (save-excursion
  5110. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5111. (org-cycle-show-empty-lines context))))
  5112. (defun org-files-list ()
  5113. "Return `org-agenda-files' list, plus all open org-mode files.
  5114. This is useful for operations that need to scan all of a user's
  5115. open and agenda-wise Org files."
  5116. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5117. (dolist (buf (buffer-list))
  5118. (with-current-buffer buf
  5119. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5120. (let ((file (expand-file-name (buffer-file-name))))
  5121. (unless (member file files)
  5122. (push file files))))))
  5123. files))
  5124. (defsubst org-entry-beginning-position ()
  5125. "Return the beginning position of the current entry."
  5126. (save-excursion (outline-back-to-heading t) (point)))
  5127. (defsubst org-entry-end-position ()
  5128. "Return the end position of the current entry."
  5129. (save-excursion (outline-next-heading) (point)))
  5130. (defun org-cycle-hide-drawers (state)
  5131. "Re-hide all drawers after a visibility state change."
  5132. (when (and (org-mode-p)
  5133. (not (memq state '(overview folded contents))))
  5134. (save-excursion
  5135. (let* ((globalp (memq state '(contents all)))
  5136. (beg (if globalp (point-min) (point)))
  5137. (end (if globalp (point-max)
  5138. (if (eq state 'children)
  5139. (save-excursion (outline-next-heading) (point))
  5140. (org-end-of-subtree t)))))
  5141. (goto-char beg)
  5142. (while (re-search-forward org-drawer-regexp end t)
  5143. (org-flag-drawer t))))))
  5144. (defun org-flag-drawer (flag)
  5145. (save-excursion
  5146. (beginning-of-line 1)
  5147. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5148. (let ((b (match-end 0))
  5149. (outline-regexp org-outline-regexp))
  5150. (if (re-search-forward
  5151. "^[ \t]*:END:"
  5152. (save-excursion (outline-next-heading) (point)) t)
  5153. (outline-flag-region b (point-at-eol) flag)
  5154. (error ":END: line missing at position %s" b))))))
  5155. (defun org-subtree-end-visible-p ()
  5156. "Is the end of the current subtree visible?"
  5157. (pos-visible-in-window-p
  5158. (save-excursion (org-end-of-subtree t) (point))))
  5159. (defun org-first-headline-recenter (&optional N)
  5160. "Move cursor to the first headline and recenter the headline.
  5161. Optional argument N means put the headline into the Nth line of the window."
  5162. (goto-char (point-min))
  5163. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5164. (beginning-of-line)
  5165. (recenter (prefix-numeric-value N))))
  5166. ;;; Saving and restoring visibility
  5167. (defun org-outline-overlay-data (&optional use-markers)
  5168. "Return a list of the locations of all outline overlays.
  5169. The are overlays with the `invisible' property value `outline'.
  5170. The return valus is a list of cons cells, with start and stop
  5171. positions for each overlay.
  5172. If USE-MARKERS is set, return the positions as markers."
  5173. (let (beg end)
  5174. (save-excursion
  5175. (save-restriction
  5176. (widen)
  5177. (delq nil
  5178. (mapcar (lambda (o)
  5179. (when (eq (overlay-get o 'invisible) 'outline)
  5180. (setq beg (overlay-start o)
  5181. end (overlay-end o))
  5182. (and beg end (> end beg)
  5183. (if use-markers
  5184. (cons (move-marker (make-marker) beg)
  5185. (move-marker (make-marker) end))
  5186. (cons beg end)))))
  5187. (org-overlays-in (point-min) (point-max))))))))
  5188. (defun org-set-outline-overlay-data (data)
  5189. "Create visibility overlays for all positions in DATA.
  5190. DATA should have been made by `org-outline-overlay-data'."
  5191. (let (o)
  5192. (save-excursion
  5193. (save-restriction
  5194. (widen)
  5195. (show-all)
  5196. (mapc (lambda (c)
  5197. (setq o (make-overlay (car c) (cdr c)))
  5198. (overlay-put o 'invisible 'outline))
  5199. data)))))
  5200. (defmacro org-save-outline-visibility (use-markers &rest body)
  5201. "Save and restore outline visibility around BODY.
  5202. If USE-MARKERS is non-nil, use markers for the positions.
  5203. This means that the buffer may change while running BODY,
  5204. but it also means that the buffer should stay alive
  5205. during the operation, because otherwise all these markers will
  5206. point nowhere."
  5207. `(let ((data (org-outline-overlay-data ,use-markers)))
  5208. (unwind-protect
  5209. (progn
  5210. ,@body
  5211. (org-set-outline-overlay-data data))
  5212. (when ,use-markers
  5213. (mapc (lambda (c)
  5214. (and (markerp (car c)) (move-marker (car c) nil))
  5215. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5216. data)))))
  5217. ;;; Folding of blocks
  5218. (defconst org-block-regexp
  5219. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5220. "Regular expression for hiding blocks.")
  5221. (defvar org-hide-block-overlays nil
  5222. "Overlays hiding blocks.")
  5223. (make-variable-buffer-local 'org-hide-block-overlays)
  5224. (defun org-block-map (function &optional start end)
  5225. "Call func at the head of all source blocks in the current
  5226. buffer. Optional arguments START and END can be used to limit
  5227. the range."
  5228. (let ((start (or start (point-min)))
  5229. (end (or end (point-max))))
  5230. (save-excursion
  5231. (goto-char start)
  5232. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5233. (save-excursion
  5234. (save-match-data
  5235. (goto-char (match-beginning 0))
  5236. (funcall function)))))))
  5237. (defun org-hide-block-toggle-all ()
  5238. "Toggle the visibility of all blocks in the current buffer."
  5239. (org-block-map #'org-hide-block-toggle))
  5240. (defun org-hide-block-all ()
  5241. "Fold all blocks in the current buffer."
  5242. (interactive)
  5243. (org-show-block-all)
  5244. (org-block-map #'org-hide-block-toggle-maybe))
  5245. (defun org-show-block-all ()
  5246. "Unfold all blocks in the current buffer."
  5247. (mapc 'delete-overlay org-hide-block-overlays)
  5248. (setq org-hide-block-overlays nil))
  5249. (defun org-hide-block-toggle-maybe ()
  5250. "Toggle visibility of block at point."
  5251. (interactive)
  5252. (let ((case-fold-search t))
  5253. (if (save-excursion
  5254. (beginning-of-line 1)
  5255. (looking-at org-block-regexp))
  5256. (progn (org-hide-block-toggle)
  5257. t) ;; to signal that we took action
  5258. nil))) ;; to signal that we did not
  5259. (defun org-hide-block-toggle (&optional force)
  5260. "Toggle the visibility of the current block."
  5261. (interactive)
  5262. (save-excursion
  5263. (beginning-of-line)
  5264. (if (re-search-forward org-block-regexp nil t)
  5265. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5266. (end (match-end 0)) ;; end of entire body
  5267. ov)
  5268. (if (memq t (mapcar (lambda (overlay)
  5269. (eq (overlay-get overlay 'invisible)
  5270. 'org-hide-block))
  5271. (org-overlays-at start)))
  5272. (if (or (not force) (eq force 'off))
  5273. (mapc (lambda (ov)
  5274. (when (member ov org-hide-block-overlays)
  5275. (setq org-hide-block-overlays
  5276. (delq ov org-hide-block-overlays)))
  5277. (when (eq (overlay-get ov 'invisible)
  5278. 'org-hide-block)
  5279. (delete-overlay ov)))
  5280. (org-overlays-at start)))
  5281. (setq ov (make-overlay start end))
  5282. (overlay-put ov 'invisible 'org-hide-block)
  5283. ;; make the block accessible to isearch
  5284. (overlay-put
  5285. ov 'isearch-open-invisible
  5286. (lambda (ov)
  5287. (when (member ov org-hide-block-overlays)
  5288. (setq org-hide-block-overlays
  5289. (delq ov org-hide-block-overlays)))
  5290. (when (eq (overlay-get ov 'invisible)
  5291. 'org-hide-block)
  5292. (delete-overlay ov))))
  5293. (push ov org-hide-block-overlays)))
  5294. (error "Not looking at a source block"))))
  5295. ;; org-tab-after-check-for-cycling-hook
  5296. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5297. ;; Remove overlays when changing major mode
  5298. (add-hook 'org-mode-hook
  5299. (lambda () (org-add-hook 'change-major-mode-hook
  5300. 'org-show-block-all 'append 'local)))
  5301. ;;; Org-goto
  5302. (defvar org-goto-window-configuration nil)
  5303. (defvar org-goto-marker nil)
  5304. (defvar org-goto-map
  5305. (let ((map (make-sparse-keymap)))
  5306. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5307. (while (setq cmd (pop cmds))
  5308. (substitute-key-definition cmd cmd map global-map)))
  5309. (suppress-keymap map)
  5310. (org-defkey map "\C-m" 'org-goto-ret)
  5311. (org-defkey map [(return)] 'org-goto-ret)
  5312. (org-defkey map [(left)] 'org-goto-left)
  5313. (org-defkey map [(right)] 'org-goto-right)
  5314. (org-defkey map [(control ?g)] 'org-goto-quit)
  5315. (org-defkey map "\C-i" 'org-cycle)
  5316. (org-defkey map [(tab)] 'org-cycle)
  5317. (org-defkey map [(down)] 'outline-next-visible-heading)
  5318. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5319. (if org-goto-auto-isearch
  5320. (if (fboundp 'define-key-after)
  5321. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5322. nil)
  5323. (org-defkey map "q" 'org-goto-quit)
  5324. (org-defkey map "n" 'outline-next-visible-heading)
  5325. (org-defkey map "p" 'outline-previous-visible-heading)
  5326. (org-defkey map "f" 'outline-forward-same-level)
  5327. (org-defkey map "b" 'outline-backward-same-level)
  5328. (org-defkey map "u" 'outline-up-heading))
  5329. (org-defkey map "/" 'org-occur)
  5330. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5331. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5332. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5333. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5334. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5335. map))
  5336. (defconst org-goto-help
  5337. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5338. RET=jump to location [Q]uit and return to previous location
  5339. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5340. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5341. ;; FIXME: Docstring does not mention both interfaces
  5342. (defun org-goto (&optional alternative-interface)
  5343. "Look up a different location in the current file, keeping current visibility.
  5344. When you want look-up or go to a different location in a document, the
  5345. fastest way is often to fold the entire buffer and then dive into the tree.
  5346. This method has the disadvantage, that the previous location will be folded,
  5347. which may not be what you want.
  5348. This command works around this by showing a copy of the current buffer
  5349. in an indirect buffer, in overview mode. You can dive into the tree in
  5350. that copy, use org-occur and incremental search to find a location.
  5351. When pressing RET or `Q', the command returns to the original buffer in
  5352. which the visibility is still unchanged. After RET is will also jump to
  5353. the location selected in the indirect buffer and expose the
  5354. the headline hierarchy above."
  5355. (interactive "P")
  5356. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5357. (org-refile-use-outline-path t)
  5358. (org-refile-target-verify-function nil)
  5359. (interface
  5360. (if (not alternative-interface)
  5361. org-goto-interface
  5362. (if (eq org-goto-interface 'outline)
  5363. 'outline-path-completion
  5364. 'outline)))
  5365. (org-goto-start-pos (point))
  5366. (selected-point
  5367. (if (eq interface 'outline)
  5368. (car (org-get-location (current-buffer) org-goto-help))
  5369. (nth 3 (org-refile-get-location "Goto: ")))))
  5370. (if selected-point
  5371. (progn
  5372. (org-mark-ring-push org-goto-start-pos)
  5373. (goto-char selected-point)
  5374. (if (or (org-invisible-p) (org-invisible-p2))
  5375. (org-show-context 'org-goto)))
  5376. (message "Quit"))))
  5377. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5378. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5379. (defvar org-goto-local-auto-isearch-map) ; defined below
  5380. (defun org-get-location (buf help)
  5381. "Let the user select a location in the Org-mode buffer BUF.
  5382. This function uses a recursive edit. It returns the selected position
  5383. or nil."
  5384. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5385. (isearch-hide-immediately nil)
  5386. (isearch-search-fun-function
  5387. (lambda () 'org-goto-local-search-headings))
  5388. (org-goto-selected-point org-goto-exit-command)
  5389. (pop-up-frames nil)
  5390. (special-display-buffer-names nil)
  5391. (special-display-regexps nil)
  5392. (special-display-function nil))
  5393. (save-excursion
  5394. (save-window-excursion
  5395. (delete-other-windows)
  5396. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5397. (switch-to-buffer
  5398. (condition-case nil
  5399. (make-indirect-buffer (current-buffer) "*org-goto*")
  5400. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5401. (with-output-to-temp-buffer "*Help*"
  5402. (princ help))
  5403. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5404. (setq buffer-read-only nil)
  5405. (let ((org-startup-truncated t)
  5406. (org-startup-folded nil)
  5407. (org-startup-align-all-tables nil))
  5408. (org-mode)
  5409. (org-overview))
  5410. (setq buffer-read-only t)
  5411. (if (and (boundp 'org-goto-start-pos)
  5412. (integer-or-marker-p org-goto-start-pos))
  5413. (let ((org-show-hierarchy-above t)
  5414. (org-show-siblings t)
  5415. (org-show-following-heading t))
  5416. (goto-char org-goto-start-pos)
  5417. (and (org-invisible-p) (org-show-context)))
  5418. (goto-char (point-min)))
  5419. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5420. (message "Select location and press RET")
  5421. (use-local-map org-goto-map)
  5422. (recursive-edit)
  5423. ))
  5424. (kill-buffer "*org-goto*")
  5425. (cons org-goto-selected-point org-goto-exit-command)))
  5426. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5427. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5428. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5429. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5430. (defun org-goto-local-search-headings (string bound noerror)
  5431. "Search and make sure that any matches are in headlines."
  5432. (catch 'return
  5433. (while (if isearch-forward
  5434. (search-forward string bound noerror)
  5435. (search-backward string bound noerror))
  5436. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5437. (and (member :headline context)
  5438. (not (member :tags context))))
  5439. (throw 'return (point))))))
  5440. (defun org-goto-local-auto-isearch ()
  5441. "Start isearch."
  5442. (interactive)
  5443. (goto-char (point-min))
  5444. (let ((keys (this-command-keys)))
  5445. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5446. (isearch-mode t)
  5447. (isearch-process-search-char (string-to-char keys)))))
  5448. (defun org-goto-ret (&optional arg)
  5449. "Finish `org-goto' by going to the new location."
  5450. (interactive "P")
  5451. (setq org-goto-selected-point (point)
  5452. org-goto-exit-command 'return)
  5453. (throw 'exit nil))
  5454. (defun org-goto-left ()
  5455. "Finish `org-goto' by going to the new location."
  5456. (interactive)
  5457. (if (org-on-heading-p)
  5458. (progn
  5459. (beginning-of-line 1)
  5460. (setq org-goto-selected-point (point)
  5461. org-goto-exit-command 'left)
  5462. (throw 'exit nil))
  5463. (error "Not on a heading")))
  5464. (defun org-goto-right ()
  5465. "Finish `org-goto' by going to the new location."
  5466. (interactive)
  5467. (if (org-on-heading-p)
  5468. (progn
  5469. (setq org-goto-selected-point (point)
  5470. org-goto-exit-command 'right)
  5471. (throw 'exit nil))
  5472. (error "Not on a heading")))
  5473. (defun org-goto-quit ()
  5474. "Finish `org-goto' without cursor motion."
  5475. (interactive)
  5476. (setq org-goto-selected-point nil)
  5477. (setq org-goto-exit-command 'quit)
  5478. (throw 'exit nil))
  5479. ;;; Indirect buffer display of subtrees
  5480. (defvar org-indirect-dedicated-frame nil
  5481. "This is the frame being used for indirect tree display.")
  5482. (defvar org-last-indirect-buffer nil)
  5483. (defun org-tree-to-indirect-buffer (&optional arg)
  5484. "Create indirect buffer and narrow it to current subtree.
  5485. With numerical prefix ARG, go up to this level and then take that tree.
  5486. If ARG is negative, go up that many levels.
  5487. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5488. indirect buffer previously made with this command, to avoid proliferation of
  5489. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5490. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5491. is kept so that you can work with several indirect buffers at the same time.
  5492. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5493. requests that a new frame be made for the new buffer, so that the dedicated
  5494. frame is not changed."
  5495. (interactive "P")
  5496. (let ((cbuf (current-buffer))
  5497. (cwin (selected-window))
  5498. (pos (point))
  5499. beg end level heading ibuf)
  5500. (save-excursion
  5501. (org-back-to-heading t)
  5502. (when (numberp arg)
  5503. (setq level (org-outline-level))
  5504. (if (< arg 0) (setq arg (+ level arg)))
  5505. (while (> (setq level (org-outline-level)) arg)
  5506. (outline-up-heading 1 t)))
  5507. (setq beg (point)
  5508. heading (org-get-heading))
  5509. (org-end-of-subtree t t)
  5510. (if (org-on-heading-p) (backward-char 1))
  5511. (setq end (point)))
  5512. (if (and (buffer-live-p org-last-indirect-buffer)
  5513. (not (eq org-indirect-buffer-display 'new-frame))
  5514. (not arg))
  5515. (kill-buffer org-last-indirect-buffer))
  5516. (setq ibuf (org-get-indirect-buffer cbuf)
  5517. org-last-indirect-buffer ibuf)
  5518. (cond
  5519. ((or (eq org-indirect-buffer-display 'new-frame)
  5520. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5521. (select-frame (make-frame))
  5522. (delete-other-windows)
  5523. (switch-to-buffer ibuf)
  5524. (org-set-frame-title heading))
  5525. ((eq org-indirect-buffer-display 'dedicated-frame)
  5526. (raise-frame
  5527. (select-frame (or (and org-indirect-dedicated-frame
  5528. (frame-live-p org-indirect-dedicated-frame)
  5529. org-indirect-dedicated-frame)
  5530. (setq org-indirect-dedicated-frame (make-frame)))))
  5531. (delete-other-windows)
  5532. (switch-to-buffer ibuf)
  5533. (org-set-frame-title (concat "Indirect: " heading)))
  5534. ((eq org-indirect-buffer-display 'current-window)
  5535. (switch-to-buffer ibuf))
  5536. ((eq org-indirect-buffer-display 'other-window)
  5537. (pop-to-buffer ibuf))
  5538. (t (error "Invalid value")))
  5539. (if (featurep 'xemacs)
  5540. (save-excursion (org-mode) (turn-on-font-lock)))
  5541. (narrow-to-region beg end)
  5542. (show-all)
  5543. (goto-char pos)
  5544. (and (window-live-p cwin) (select-window cwin))))
  5545. (defun org-get-indirect-buffer (&optional buffer)
  5546. (setq buffer (or buffer (current-buffer)))
  5547. (let ((n 1) (base (buffer-name buffer)) bname)
  5548. (while (buffer-live-p
  5549. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5550. (setq n (1+ n)))
  5551. (condition-case nil
  5552. (make-indirect-buffer buffer bname 'clone)
  5553. (error (make-indirect-buffer buffer bname)))))
  5554. (defun org-set-frame-title (title)
  5555. "Set the title of the current frame to the string TITLE."
  5556. ;; FIXME: how to name a single frame in XEmacs???
  5557. (unless (featurep 'xemacs)
  5558. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5559. ;;;; Structure editing
  5560. ;;; Inserting headlines
  5561. (defun org-previous-line-empty-p ()
  5562. (save-excursion
  5563. (and (not (bobp))
  5564. (or (beginning-of-line 0) t)
  5565. (save-match-data
  5566. (looking-at "[ \t]*$")))))
  5567. (defun org-insert-heading (&optional force-heading invisible-ok)
  5568. "Insert a new heading or item with same depth at point.
  5569. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5570. If point is at the beginning of a headline, insert a sibling before the
  5571. current headline. If point is not at the beginning, do not split the line,
  5572. but create the new headline after the current line.
  5573. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5574. This is important for non-interactive uses of the command."
  5575. (interactive "P")
  5576. (if (or (= (buffer-size) 0)
  5577. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5578. (org-on-heading-p))))
  5579. (not (org-in-item-p))))
  5580. (insert "\n* ")
  5581. (when (or force-heading (not (org-insert-item)))
  5582. (let* ((empty-line-p nil)
  5583. (head (save-excursion
  5584. (condition-case nil
  5585. (progn
  5586. (org-back-to-heading invisible-ok)
  5587. (setq empty-line-p (org-previous-line-empty-p))
  5588. (match-string 0))
  5589. (error "*"))))
  5590. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5591. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5592. pos hide-previous previous-pos)
  5593. (cond
  5594. ((and (org-on-heading-p) (bolp)
  5595. (or (bobp)
  5596. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5597. ;; insert before the current line
  5598. (open-line (if blank 2 1)))
  5599. ((and (bolp)
  5600. (not org-insert-heading-respect-content)
  5601. (or (bobp)
  5602. (save-excursion
  5603. (backward-char 1) (not (org-invisible-p)))))
  5604. ;; insert right here
  5605. nil)
  5606. (t
  5607. ;; somewhere in the line
  5608. (save-excursion
  5609. (setq previous-pos (point-at-bol))
  5610. (end-of-line)
  5611. (setq hide-previous (org-invisible-p)))
  5612. (and org-insert-heading-respect-content (org-show-subtree))
  5613. (let ((split
  5614. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5615. (save-excursion
  5616. (let ((p (point)))
  5617. (goto-char (point-at-bol))
  5618. (and (looking-at org-complex-heading-regexp)
  5619. (> p (match-beginning 4)))))))
  5620. tags pos)
  5621. (cond
  5622. (org-insert-heading-respect-content
  5623. (org-end-of-subtree nil t)
  5624. (or (bolp) (newline))
  5625. (or (org-previous-line-empty-p)
  5626. (and blank (newline)))
  5627. (open-line 1))
  5628. ((org-on-heading-p)
  5629. (when hide-previous
  5630. (show-children)
  5631. (org-show-entry))
  5632. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5633. (setq tags (and (match-end 2) (match-string 2)))
  5634. (and (match-end 1)
  5635. (delete-region (match-beginning 1) (match-end 1)))
  5636. (setq pos (point-at-bol))
  5637. (or split (end-of-line 1))
  5638. (delete-horizontal-space)
  5639. (if (string-match "\\`\\*+\\'"
  5640. (buffer-substring (point-at-bol) (point)))
  5641. (insert " "))
  5642. (newline (if blank 2 1))
  5643. (when tags
  5644. (save-excursion
  5645. (goto-char pos)
  5646. (end-of-line 1)
  5647. (insert " " tags)
  5648. (org-set-tags nil 'align))))
  5649. (t
  5650. (or split (end-of-line 1))
  5651. (newline (if blank 2 1)))))))
  5652. (insert head) (just-one-space)
  5653. (setq pos (point))
  5654. (end-of-line 1)
  5655. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5656. (when (and org-insert-heading-respect-content hide-previous)
  5657. (save-excursion
  5658. (goto-char previous-pos)
  5659. (hide-subtree)))
  5660. (run-hooks 'org-insert-heading-hook)))))
  5661. (defun org-get-heading (&optional no-tags)
  5662. "Return the heading of the current entry, without the stars."
  5663. (save-excursion
  5664. (org-back-to-heading t)
  5665. (if (looking-at
  5666. (if no-tags
  5667. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5668. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5669. (match-string 1) "")))
  5670. (defun org-heading-components ()
  5671. "Return the components of the current heading.
  5672. This is a list with the following elements:
  5673. - the level as an integer
  5674. - the reduced level, different if `org-odd-levels-only' is set.
  5675. - the TODO keyword, or nil
  5676. - the priority character, like ?A, or nil if no priority is given
  5677. - the headline text itself, or the tags string if no headline text
  5678. - the tags string, or nil."
  5679. (save-excursion
  5680. (org-back-to-heading t)
  5681. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5682. (list (length (match-string 1))
  5683. (org-reduced-level (length (match-string 1)))
  5684. (org-match-string-no-properties 2)
  5685. (and (match-end 3) (aref (match-string 3) 2))
  5686. (org-match-string-no-properties 4)
  5687. (org-match-string-no-properties 5)))))
  5688. (defun org-get-entry ()
  5689. "Get the entry text, after heading, entire subtree."
  5690. (save-excursion
  5691. (org-back-to-heading t)
  5692. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5693. (defun org-insert-heading-after-current ()
  5694. "Insert a new heading with same level as current, after current subtree."
  5695. (interactive)
  5696. (org-back-to-heading)
  5697. (org-insert-heading)
  5698. (org-move-subtree-down)
  5699. (end-of-line 1))
  5700. (defun org-insert-heading-respect-content ()
  5701. (interactive)
  5702. (let ((org-insert-heading-respect-content t))
  5703. (org-insert-heading t)))
  5704. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5705. (interactive "P")
  5706. (let ((org-insert-heading-respect-content t))
  5707. (org-insert-todo-heading force-state t)))
  5708. (defun org-insert-todo-heading (arg &optional force-heading)
  5709. "Insert a new heading with the same level and TODO state as current heading.
  5710. If the heading has no TODO state, or if the state is DONE, use the first
  5711. state (TODO by default). Also with prefix arg, force first state."
  5712. (interactive "P")
  5713. (when (or force-heading (not (org-insert-item 'checkbox)))
  5714. (org-insert-heading force-heading)
  5715. (save-excursion
  5716. (org-back-to-heading)
  5717. (outline-previous-heading)
  5718. (looking-at org-todo-line-regexp))
  5719. (let*
  5720. ((new-mark-x
  5721. (if (or arg
  5722. (not (match-beginning 2))
  5723. (member (match-string 2) org-done-keywords))
  5724. (car org-todo-keywords-1)
  5725. (match-string 2)))
  5726. (new-mark
  5727. (or
  5728. (run-hook-with-args-until-success
  5729. 'org-todo-get-default-hook new-mark-x nil)
  5730. new-mark-x)))
  5731. (beginning-of-line 1)
  5732. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5733. (if org-treat-insert-todo-heading-as-state-change
  5734. (org-todo new-mark)
  5735. (insert new-mark " "))))
  5736. (when org-provide-todo-statistics
  5737. (org-update-parent-todo-statistics))))
  5738. (defun org-insert-subheading (arg)
  5739. "Insert a new subheading and demote it.
  5740. Works for outline headings and for plain lists alike."
  5741. (interactive "P")
  5742. (org-insert-heading arg)
  5743. (cond
  5744. ((org-on-heading-p) (org-do-demote))
  5745. ((org-at-item-p) (org-indent-item 1))))
  5746. (defun org-insert-todo-subheading (arg)
  5747. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5748. Works for outline headings and for plain lists alike."
  5749. (interactive "P")
  5750. (org-insert-todo-heading arg)
  5751. (cond
  5752. ((org-on-heading-p) (org-do-demote))
  5753. ((org-at-item-p) (org-indent-item 1))))
  5754. ;;; Promotion and Demotion
  5755. (defvar org-after-demote-entry-hook nil
  5756. "Hook run after an entry has been demoted.
  5757. The cursor will be at the beginning of the entry.
  5758. When a subtree is being demoted, the hook will be called for each node.")
  5759. (defvar org-after-promote-entry-hook nil
  5760. "Hook run after an entry has been promoted.
  5761. The cursor will be at the beginning of the entry.
  5762. When a subtree is being promoted, the hook will be called for each node.")
  5763. (defun org-promote-subtree ()
  5764. "Promote the entire subtree.
  5765. See also `org-promote'."
  5766. (interactive)
  5767. (save-excursion
  5768. (org-map-tree 'org-promote))
  5769. (org-fix-position-after-promote))
  5770. (defun org-demote-subtree ()
  5771. "Demote the entire subtree. See `org-demote'.
  5772. See also `org-promote'."
  5773. (interactive)
  5774. (save-excursion
  5775. (org-map-tree 'org-demote))
  5776. (org-fix-position-after-promote))
  5777. (defun org-do-promote ()
  5778. "Promote the current heading higher up the tree.
  5779. If the region is active in `transient-mark-mode', promote all headings
  5780. in the region."
  5781. (interactive)
  5782. (save-excursion
  5783. (if (org-region-active-p)
  5784. (org-map-region 'org-promote (region-beginning) (region-end))
  5785. (org-promote)))
  5786. (org-fix-position-after-promote))
  5787. (defun org-do-demote ()
  5788. "Demote the current heading lower down the tree.
  5789. If the region is active in `transient-mark-mode', demote all headings
  5790. in the region."
  5791. (interactive)
  5792. (save-excursion
  5793. (if (org-region-active-p)
  5794. (org-map-region 'org-demote (region-beginning) (region-end))
  5795. (org-demote)))
  5796. (org-fix-position-after-promote))
  5797. (defun org-fix-position-after-promote ()
  5798. "Make sure that after pro/demotion cursor position is right."
  5799. (let ((pos (point)))
  5800. (when (save-excursion
  5801. (beginning-of-line 1)
  5802. (looking-at org-todo-line-regexp)
  5803. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5804. (cond ((eobp) (insert " "))
  5805. ((eolp) (insert " "))
  5806. ((equal (char-after) ?\ ) (forward-char 1))))))
  5807. (defun org-current-level ()
  5808. "Return the level of the current entry, or nil if before the first headline.
  5809. The level is the number of stars at the beginning of the headline."
  5810. (save-excursion
  5811. (condition-case nil
  5812. (progn
  5813. (org-back-to-heading t)
  5814. (funcall outline-level))
  5815. (error nil))))
  5816. (defun org-get-previous-line-level ()
  5817. "Return the outline depth of the last headline before the current line.
  5818. Returns 0 for the first headline in the buffer, and nil if before the
  5819. first headline."
  5820. (let ((current-level (org-current-level))
  5821. (prev-level (when (> (line-number-at-pos) 1)
  5822. (save-excursion
  5823. (beginning-of-line 0)
  5824. (org-current-level)))))
  5825. (cond ((null current-level) nil) ; Before first headline
  5826. ((null prev-level) 0) ; At first headline
  5827. (prev-level))))
  5828. (defun org-reduced-level (l)
  5829. "Compute the effective level of a heading.
  5830. This takes into account the setting of `org-odd-levels-only'."
  5831. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5832. (defun org-level-increment ()
  5833. "Return the number of stars that will be added or removed at a
  5834. time to headlines when structure editing, based on the value of
  5835. `org-odd-levels-only'."
  5836. (if org-odd-levels-only 2 1))
  5837. (defun org-get-valid-level (level &optional change)
  5838. "Rectify a level change under the influence of `org-odd-levels-only'
  5839. LEVEL is a current level, CHANGE is by how much the level should be
  5840. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5841. even level numbers will become the next higher odd number."
  5842. (if org-odd-levels-only
  5843. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5844. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5845. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5846. (max 1 (+ level (or change 0)))))
  5847. (if (boundp 'define-obsolete-function-alias)
  5848. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5849. (define-obsolete-function-alias 'org-get-legal-level
  5850. 'org-get-valid-level)
  5851. (define-obsolete-function-alias 'org-get-legal-level
  5852. 'org-get-valid-level "23.1")))
  5853. (defun org-promote ()
  5854. "Promote the current heading higher up the tree.
  5855. If the region is active in `transient-mark-mode', promote all headings
  5856. in the region."
  5857. (org-back-to-heading t)
  5858. (let* ((level (save-match-data (funcall outline-level)))
  5859. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5860. (diff (abs (- level (length up-head) -1))))
  5861. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5862. (replace-match up-head nil t)
  5863. ;; Fixup tag positioning
  5864. (and org-auto-align-tags (org-set-tags nil t))
  5865. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5866. (run-hooks 'org-after-promote-entry-hook)))
  5867. (defun org-demote ()
  5868. "Demote the current heading lower down the tree.
  5869. If the region is active in `transient-mark-mode', demote all headings
  5870. in the region."
  5871. (org-back-to-heading t)
  5872. (let* ((level (save-match-data (funcall outline-level)))
  5873. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5874. (diff (abs (- level (length down-head) -1))))
  5875. (replace-match down-head nil t)
  5876. ;; Fixup tag positioning
  5877. (and org-auto-align-tags (org-set-tags nil t))
  5878. (if org-adapt-indentation (org-fixup-indentation diff))
  5879. (run-hooks 'org-after-demote-entry-hook)))
  5880. (defun org-cycle-level ()
  5881. "Cycle the level of an empty headline through possible states.
  5882. This goes first to child, then to parent, level, then up the hierarchy.
  5883. After top level, it switches back to sibling level."
  5884. (interactive)
  5885. (let ((org-adapt-indentation nil))
  5886. (when (org-point-at-end-of-empty-headline)
  5887. (setq this-command 'org-cycle-level) ; Only needed for caching
  5888. (let ((cur-level (org-current-level))
  5889. (prev-level (org-get-previous-line-level)))
  5890. (cond
  5891. ;; If first headline in file, promote to top-level.
  5892. ((= prev-level 0)
  5893. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5894. do (org-do-promote)))
  5895. ;; If same level as prev, demote one.
  5896. ((= prev-level cur-level)
  5897. (org-do-demote))
  5898. ;; If parent is top-level, promote to top level if not already.
  5899. ((= prev-level 1)
  5900. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5901. do (org-do-promote)))
  5902. ;; If top-level, return to prev-level.
  5903. ((= cur-level 1)
  5904. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5905. do (org-do-demote)))
  5906. ;; If less than prev-level, promote one.
  5907. ((< cur-level prev-level)
  5908. (org-do-promote))
  5909. ;; If deeper than prev-level, promote until higher than
  5910. ;; prev-level.
  5911. ((> cur-level prev-level)
  5912. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5913. do (org-do-promote))))
  5914. t))))
  5915. (defun org-map-tree (fun)
  5916. "Call FUN for every heading underneath the current one."
  5917. (org-back-to-heading)
  5918. (let ((level (funcall outline-level)))
  5919. (save-excursion
  5920. (funcall fun)
  5921. (while (and (progn
  5922. (outline-next-heading)
  5923. (> (funcall outline-level) level))
  5924. (not (eobp)))
  5925. (funcall fun)))))
  5926. (defun org-map-region (fun beg end)
  5927. "Call FUN for every heading between BEG and END."
  5928. (let ((org-ignore-region t))
  5929. (save-excursion
  5930. (setq end (copy-marker end))
  5931. (goto-char beg)
  5932. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5933. (< (point) end))
  5934. (funcall fun))
  5935. (while (and (progn
  5936. (outline-next-heading)
  5937. (< (point) end))
  5938. (not (eobp)))
  5939. (funcall fun)))))
  5940. (defun org-fixup-indentation (diff)
  5941. "Change the indentation in the current entry by DIFF
  5942. However, if any line in the current entry has no indentation, or if it
  5943. would end up with no indentation after the change, nothing at all is done."
  5944. (save-excursion
  5945. (let ((end (save-excursion (outline-next-heading)
  5946. (point-marker)))
  5947. (prohibit (if (> diff 0)
  5948. "^\\S-"
  5949. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5950. col)
  5951. (unless (save-excursion (end-of-line 1)
  5952. (re-search-forward prohibit end t))
  5953. (while (and (< (point) end)
  5954. (re-search-forward "^[ \t]+" end t))
  5955. (goto-char (match-end 0))
  5956. (setq col (current-column))
  5957. (if (< diff 0) (replace-match ""))
  5958. (org-indent-to-column (+ diff col))))
  5959. (move-marker end nil))))
  5960. (defun org-convert-to-odd-levels ()
  5961. "Convert an org-mode file with all levels allowed to one with odd levels.
  5962. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5963. level 5 etc."
  5964. (interactive)
  5965. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5966. (let ((outline-regexp org-outline-regexp)
  5967. (outline-level 'org-outline-level)
  5968. (org-odd-levels-only nil) n)
  5969. (save-excursion
  5970. (goto-char (point-min))
  5971. (while (re-search-forward "^\\*\\*+ " nil t)
  5972. (setq n (- (length (match-string 0)) 2))
  5973. (while (>= (setq n (1- n)) 0)
  5974. (org-demote))
  5975. (end-of-line 1))))))
  5976. (defun org-convert-to-oddeven-levels ()
  5977. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5978. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5979. section with an even level, conversion would destroy the structure of the file. An error
  5980. is signaled in this case."
  5981. (interactive)
  5982. (goto-char (point-min))
  5983. ;; First check if there are no even levels
  5984. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5985. (org-show-context t)
  5986. (error "Not all levels are odd in this file. Conversion not possible"))
  5987. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5988. (let ((outline-regexp org-outline-regexp)
  5989. (outline-level 'org-outline-level)
  5990. (org-odd-levels-only nil) n)
  5991. (save-excursion
  5992. (goto-char (point-min))
  5993. (while (re-search-forward "^\\*\\*+ " nil t)
  5994. (setq n (/ (1- (length (match-string 0))) 2))
  5995. (while (>= (setq n (1- n)) 0)
  5996. (org-promote))
  5997. (end-of-line 1))))))
  5998. (defun org-tr-level (n)
  5999. "Make N odd if required."
  6000. (if org-odd-levels-only (1+ (/ n 2)) n))
  6001. ;;; Vertical tree motion, cutting and pasting of subtrees
  6002. (defun org-move-subtree-up (&optional arg)
  6003. "Move the current subtree up past ARG headlines of the same level."
  6004. (interactive "p")
  6005. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6006. (defun org-move-subtree-down (&optional arg)
  6007. "Move the current subtree down past ARG headlines of the same level."
  6008. (interactive "p")
  6009. (setq arg (prefix-numeric-value arg))
  6010. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6011. 'org-get-last-sibling))
  6012. (ins-point (make-marker))
  6013. (cnt (abs arg))
  6014. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6015. ;; Select the tree
  6016. (org-back-to-heading)
  6017. (setq beg0 (point))
  6018. (save-excursion
  6019. (setq ne-beg (org-back-over-empty-lines))
  6020. (setq beg (point)))
  6021. (save-match-data
  6022. (save-excursion (outline-end-of-heading)
  6023. (setq folded (org-invisible-p)))
  6024. (outline-end-of-subtree))
  6025. (outline-next-heading)
  6026. (setq ne-end (org-back-over-empty-lines))
  6027. (setq end (point))
  6028. (goto-char beg0)
  6029. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6030. ;; include less whitespace
  6031. (save-excursion
  6032. (goto-char beg)
  6033. (forward-line (- ne-beg ne-end))
  6034. (setq beg (point))))
  6035. ;; Find insertion point, with error handling
  6036. (while (> cnt 0)
  6037. (or (and (funcall movfunc) (looking-at outline-regexp))
  6038. (progn (goto-char beg0)
  6039. (error "Cannot move past superior level or buffer limit")))
  6040. (setq cnt (1- cnt)))
  6041. (if (> arg 0)
  6042. ;; Moving forward - still need to move over subtree
  6043. (progn (org-end-of-subtree t t)
  6044. (save-excursion
  6045. (org-back-over-empty-lines)
  6046. (or (bolp) (newline)))))
  6047. (setq ne-ins (org-back-over-empty-lines))
  6048. (move-marker ins-point (point))
  6049. (setq txt (buffer-substring beg end))
  6050. (org-save-markers-in-region beg end)
  6051. (delete-region beg end)
  6052. (org-remove-empty-overlays-at beg)
  6053. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6054. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6055. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6056. (let ((bbb (point)))
  6057. (insert-before-markers txt)
  6058. (org-reinstall-markers-in-region bbb)
  6059. (move-marker ins-point bbb))
  6060. (or (bolp) (insert "\n"))
  6061. (setq ins-end (point))
  6062. (goto-char ins-point)
  6063. (org-skip-whitespace)
  6064. (when (and (< arg 0)
  6065. (org-first-sibling-p)
  6066. (> ne-ins ne-beg))
  6067. ;; Move whitespace back to beginning
  6068. (save-excursion
  6069. (goto-char ins-end)
  6070. (let ((kill-whole-line t))
  6071. (kill-line (- ne-ins ne-beg)) (point)))
  6072. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6073. (move-marker ins-point nil)
  6074. (if folded
  6075. (hide-subtree)
  6076. (org-show-entry)
  6077. (show-children)
  6078. (org-cycle-hide-drawers 'children))
  6079. (org-clean-visibility-after-subtree-move)))
  6080. (defvar org-subtree-clip ""
  6081. "Clipboard for cut and paste of subtrees.
  6082. This is actually only a copy of the kill, because we use the normal kill
  6083. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6084. (defvar org-subtree-clip-folded nil
  6085. "Was the last copied subtree folded?
  6086. This is used to fold the tree back after pasting.")
  6087. (defun org-cut-subtree (&optional n)
  6088. "Cut the current subtree into the clipboard.
  6089. With prefix arg N, cut this many sequential subtrees.
  6090. This is a short-hand for marking the subtree and then cutting it."
  6091. (interactive "p")
  6092. (org-copy-subtree n 'cut))
  6093. (defun org-copy-subtree (&optional n cut force-store-markers)
  6094. "Cut the current subtree into the clipboard.
  6095. With prefix arg N, cut this many sequential subtrees.
  6096. This is a short-hand for marking the subtree and then copying it.
  6097. If CUT is non-nil, actually cut the subtree.
  6098. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6099. of some markers in the region, even if CUT is non-nil. This is
  6100. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6101. (interactive "p")
  6102. (let (beg end folded (beg0 (point)))
  6103. (if (interactive-p)
  6104. (org-back-to-heading nil) ; take what looks like a subtree
  6105. (org-back-to-heading t)) ; take what is really there
  6106. (org-back-over-empty-lines)
  6107. (setq beg (point))
  6108. (skip-chars-forward " \t\r\n")
  6109. (save-match-data
  6110. (save-excursion (outline-end-of-heading)
  6111. (setq folded (org-invisible-p)))
  6112. (condition-case nil
  6113. (org-forward-same-level (1- n) t)
  6114. (error nil))
  6115. (org-end-of-subtree t t))
  6116. (org-back-over-empty-lines)
  6117. (setq end (point))
  6118. (goto-char beg0)
  6119. (when (> end beg)
  6120. (setq org-subtree-clip-folded folded)
  6121. (when (or cut force-store-markers)
  6122. (org-save-markers-in-region beg end))
  6123. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6124. (setq org-subtree-clip (current-kill 0))
  6125. (message "%s: Subtree(s) with %d characters"
  6126. (if cut "Cut" "Copied")
  6127. (length org-subtree-clip)))))
  6128. (defun org-paste-subtree (&optional level tree for-yank)
  6129. "Paste the clipboard as a subtree, with modification of headline level.
  6130. The entire subtree is promoted or demoted in order to match a new headline
  6131. level.
  6132. If the cursor is at the beginning of a headline, the same level as
  6133. that headline is used to paste the tree
  6134. If not, the new level is derived from the *visible* headings
  6135. before and after the insertion point, and taken to be the inferior headline
  6136. level of the two. So if the previous visible heading is level 3 and the
  6137. next is level 4 (or vice versa), level 4 will be used for insertion.
  6138. This makes sure that the subtree remains an independent subtree and does
  6139. not swallow low level entries.
  6140. You can also force a different level, either by using a numeric prefix
  6141. argument, or by inserting the heading marker by hand. For example, if the
  6142. cursor is after \"*****\", then the tree will be shifted to level 5.
  6143. If optional TREE is given, use this text instead of the kill ring.
  6144. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6145. move back over whitespace before inserting, and move point to the end of
  6146. the inserted text when done."
  6147. (interactive "P")
  6148. (setq tree (or tree (and kill-ring (current-kill 0))))
  6149. (unless (org-kill-is-subtree-p tree)
  6150. (error "%s"
  6151. (substitute-command-keys
  6152. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6153. (let* ((visp (not (org-invisible-p)))
  6154. (txt tree)
  6155. (^re (concat "^\\(" outline-regexp "\\)"))
  6156. (re (concat "\\(" outline-regexp "\\)"))
  6157. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6158. (old-level (if (string-match ^re txt)
  6159. (- (match-end 0) (match-beginning 0) 1)
  6160. -1))
  6161. (force-level (cond (level (prefix-numeric-value level))
  6162. ((and (looking-at "[ \t]*$")
  6163. (string-match
  6164. ^re_ (buffer-substring
  6165. (point-at-bol) (point))))
  6166. (- (match-end 1) (match-beginning 1)))
  6167. ((and (bolp)
  6168. (looking-at org-outline-regexp))
  6169. (- (match-end 0) (point) 1))
  6170. (t nil)))
  6171. (previous-level (save-excursion
  6172. (condition-case nil
  6173. (progn
  6174. (outline-previous-visible-heading 1)
  6175. (if (looking-at re)
  6176. (- (match-end 0) (match-beginning 0) 1)
  6177. 1))
  6178. (error 1))))
  6179. (next-level (save-excursion
  6180. (condition-case nil
  6181. (progn
  6182. (or (looking-at outline-regexp)
  6183. (outline-next-visible-heading 1))
  6184. (if (looking-at re)
  6185. (- (match-end 0) (match-beginning 0) 1)
  6186. 1))
  6187. (error 1))))
  6188. (new-level (or force-level (max previous-level next-level)))
  6189. (shift (if (or (= old-level -1)
  6190. (= new-level -1)
  6191. (= old-level new-level))
  6192. 0
  6193. (- new-level old-level)))
  6194. (delta (if (> shift 0) -1 1))
  6195. (func (if (> shift 0) 'org-demote 'org-promote))
  6196. (org-odd-levels-only nil)
  6197. beg end newend)
  6198. ;; Remove the forced level indicator
  6199. (if force-level
  6200. (delete-region (point-at-bol) (point)))
  6201. ;; Paste
  6202. (beginning-of-line 1)
  6203. (unless for-yank (org-back-over-empty-lines))
  6204. (setq beg (point))
  6205. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6206. (insert-before-markers txt)
  6207. (unless (string-match "\n\\'" txt) (insert "\n"))
  6208. (setq newend (point))
  6209. (org-reinstall-markers-in-region beg)
  6210. (setq end (point))
  6211. (goto-char beg)
  6212. (skip-chars-forward " \t\n\r")
  6213. (setq beg (point))
  6214. (if (and (org-invisible-p) visp)
  6215. (save-excursion (outline-show-heading)))
  6216. ;; Shift if necessary
  6217. (unless (= shift 0)
  6218. (save-restriction
  6219. (narrow-to-region beg end)
  6220. (while (not (= shift 0))
  6221. (org-map-region func (point-min) (point-max))
  6222. (setq shift (+ delta shift)))
  6223. (goto-char (point-min))
  6224. (setq newend (point-max))))
  6225. (when (or (interactive-p) for-yank)
  6226. (message "Clipboard pasted as level %d subtree" new-level))
  6227. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6228. kill-ring
  6229. (eq org-subtree-clip (current-kill 0))
  6230. org-subtree-clip-folded)
  6231. ;; The tree was folded before it was killed/copied
  6232. (hide-subtree))
  6233. (and for-yank (goto-char newend))))
  6234. (defun org-kill-is-subtree-p (&optional txt)
  6235. "Check if the current kill is an outline subtree, or a set of trees.
  6236. Returns nil if kill does not start with a headline, or if the first
  6237. headline level is not the largest headline level in the tree.
  6238. So this will actually accept several entries of equal levels as well,
  6239. which is OK for `org-paste-subtree'.
  6240. If optional TXT is given, check this string instead of the current kill."
  6241. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6242. (start-level (and kill
  6243. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6244. org-outline-regexp "\\)")
  6245. kill)
  6246. (- (match-end 2) (match-beginning 2) 1)))
  6247. (re (concat "^" org-outline-regexp))
  6248. (start (1+ (or (match-beginning 2) -1))))
  6249. (if (not start-level)
  6250. (progn
  6251. nil) ;; does not even start with a heading
  6252. (catch 'exit
  6253. (while (setq start (string-match re kill (1+ start)))
  6254. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6255. (throw 'exit nil)))
  6256. t))))
  6257. (defvar org-markers-to-move nil
  6258. "Markers that should be moved with a cut-and-paste operation.
  6259. Those markers are stored together with their positions relative to
  6260. the start of the region.")
  6261. (defun org-save-markers-in-region (beg end)
  6262. "Check markers in region.
  6263. If these markers are between BEG and END, record their position relative
  6264. to BEG, so that after moving the block of text, we can put the markers back
  6265. into place.
  6266. This function gets called just before an entry or tree gets cut from the
  6267. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6268. called immediately, to move the markers with the entries."
  6269. (setq org-markers-to-move nil)
  6270. (when (featurep 'org-clock)
  6271. (org-clock-save-markers-for-cut-and-paste beg end))
  6272. (when (featurep 'org-agenda)
  6273. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6274. (defun org-check-and-save-marker (marker beg end)
  6275. "Check if MARKER is between BEG and END.
  6276. If yes, remember the marker and the distance to BEG."
  6277. (when (and (marker-buffer marker)
  6278. (equal (marker-buffer marker) (current-buffer)))
  6279. (if (and (>= marker beg) (< marker end))
  6280. (push (cons marker (- marker beg)) org-markers-to-move))))
  6281. (defun org-reinstall-markers-in-region (beg)
  6282. "Move all remembered markers to their position relative to BEG."
  6283. (mapc (lambda (x)
  6284. (move-marker (car x) (+ beg (cdr x))))
  6285. org-markers-to-move)
  6286. (setq org-markers-to-move nil))
  6287. (defun org-narrow-to-subtree ()
  6288. "Narrow buffer to the current subtree."
  6289. (interactive)
  6290. (save-excursion
  6291. (save-match-data
  6292. (narrow-to-region
  6293. (progn (org-back-to-heading t) (point))
  6294. (progn (org-end-of-subtree t t)
  6295. (if (org-on-heading-p) (backward-char 1))
  6296. (point))))))
  6297. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6298. "Clone the task (subtree) at point N times.
  6299. The clones will be inserted as siblings.
  6300. In interactive use, the user will be prompted for the number of clones
  6301. to be produced, and for a time SHIFT, which may be a repeater as used
  6302. in time stamps, for example `+3d'.
  6303. When a valid repeater is given and the entry contains any time stamps,
  6304. the clones will become a sequence in time, with time stamps in the
  6305. subtree shifted for each clone produced. If SHIFT is nil or the
  6306. empty string, time stamps will be left alone.
  6307. If the original subtree did contain time stamps with a repeater,
  6308. the following will happen:
  6309. - the repeater will be removed in each clone
  6310. - an additional clone will be produced, with the current, unshifted
  6311. date(s) in the entry.
  6312. - the original entry will be placed *after* all the clones, with
  6313. repeater intact.
  6314. - the start days in the repeater in the original entry will be shifted
  6315. to past the last clone.
  6316. I this way you can spell out a number of instances of a repeating task,
  6317. and still retain the repeater to cover future instances of the task."
  6318. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6319. (let (beg end template task
  6320. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6321. (if (not (and (integerp n) (> n 0)))
  6322. (error "Invalid number of replications %s" n))
  6323. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6324. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6325. shift)))
  6326. (error "Invalid shift specification %s" shift))
  6327. (when doshift
  6328. (setq shift-n (string-to-number (match-string 1 shift))
  6329. shift-what (cdr (assoc (match-string 2 shift)
  6330. '(("d" . day) ("w" . week)
  6331. ("m" . month) ("y" . year))))))
  6332. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6333. (setq nmin 1 nmax n)
  6334. (org-back-to-heading t)
  6335. (setq beg (point))
  6336. (org-end-of-subtree t t)
  6337. (or (bolp) (insert "\n"))
  6338. (setq end (point))
  6339. (setq template (buffer-substring beg end))
  6340. (when (and doshift
  6341. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6342. (delete-region beg end)
  6343. (setq end beg)
  6344. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6345. (goto-char end)
  6346. (loop for n from nmin to nmax do
  6347. (if (not doshift)
  6348. (setq task template)
  6349. (with-temp-buffer
  6350. (insert template)
  6351. (org-mode)
  6352. (goto-char (point-min))
  6353. (while (re-search-forward org-ts-regexp-both nil t)
  6354. (org-timestamp-change (* n shift-n) shift-what))
  6355. (unless (= n n-no-remove)
  6356. (goto-char (point-min))
  6357. (while (re-search-forward org-ts-regexp nil t)
  6358. (save-excursion
  6359. (goto-char (match-beginning 0))
  6360. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6361. (delete-region (match-beginning 1) (match-end 1))))))
  6362. (setq task (buffer-string))))
  6363. (insert task))
  6364. (goto-char beg)))
  6365. ;;; Outline Sorting
  6366. (defun org-sort (with-case)
  6367. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6368. Optional argument WITH-CASE means sort case-sensitively.
  6369. With a double prefix argument, also remove duplicate entries."
  6370. (interactive "P")
  6371. (if (org-at-table-p)
  6372. (org-call-with-arg 'org-table-sort-lines with-case)
  6373. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6374. (defun org-sort-remove-invisible (s)
  6375. (remove-text-properties 0 (length s) org-rm-props s)
  6376. (while (string-match org-bracket-link-regexp s)
  6377. (setq s (replace-match (if (match-end 2)
  6378. (match-string 3 s)
  6379. (match-string 1 s)) t t s)))
  6380. s)
  6381. (defvar org-priority-regexp) ; defined later in the file
  6382. (defvar org-after-sorting-entries-or-items-hook nil
  6383. "Hook that is run after a bunch of entries or items have been sorted.
  6384. When children are sorted, the cursor is in the parent line when this
  6385. hook gets called. When a region or a plain list is sorted, the cursor
  6386. will be in the first entry of the sorted region/list.")
  6387. (defun org-sort-entries-or-items
  6388. (&optional with-case sorting-type getkey-func compare-func property)
  6389. "Sort entries on a certain level of an outline tree, or plain list items.
  6390. If there is an active region, the entries in the region are sorted.
  6391. Else, if the cursor is before the first entry, sort the top-level items.
  6392. Else, the children of the entry at point are sorted.
  6393. If the cursor is at the first item in a plain list, the list items will be
  6394. sorted.
  6395. Sorting can be alphabetically, numerically, by date/time as given by
  6396. a time stamp, by a property or by priority.
  6397. The command prompts for the sorting type unless it has been given to the
  6398. function through the SORTING-TYPE argument, which needs to a character,
  6399. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6400. precise meaning of each character:
  6401. n Numerically, by converting the beginning of the entry/item to a number.
  6402. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6403. t By date/time, either the first active time stamp in the entry, or, if
  6404. none exist, by the first inactive one.
  6405. In items, only the first line will be checked.
  6406. s By the scheduled date/time.
  6407. d By deadline date/time.
  6408. c By creation time, which is assumed to be the first inactive time stamp
  6409. at the beginning of a line.
  6410. p By priority according to the cookie.
  6411. r By the value of a property.
  6412. Capital letters will reverse the sort order.
  6413. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6414. called with point at the beginning of the record. It must return either
  6415. a string or a number that should serve as the sorting key for that record.
  6416. Comparing entries ignores case by default. However, with an optional argument
  6417. WITH-CASE, the sorting considers case as well."
  6418. (interactive "P")
  6419. (let ((case-func (if with-case 'identity 'downcase))
  6420. start beg end stars re re2
  6421. txt what tmp plain-list-p)
  6422. ;; Find beginning and end of region to sort
  6423. (cond
  6424. ((org-region-active-p)
  6425. ;; we will sort the region
  6426. (setq end (region-end)
  6427. what "region")
  6428. (goto-char (region-beginning))
  6429. (if (not (org-on-heading-p)) (outline-next-heading))
  6430. (setq start (point)))
  6431. ((org-at-item-p)
  6432. ;; we will sort this plain list
  6433. (org-beginning-of-item-list) (setq start (point))
  6434. (org-end-of-item-list)
  6435. (or (bolp) (insert "\n"))
  6436. (setq end (point))
  6437. (goto-char start)
  6438. (setq plain-list-p t
  6439. what "plain list"))
  6440. ((or (org-on-heading-p)
  6441. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6442. ;; we will sort the children of the current headline
  6443. (org-back-to-heading)
  6444. (setq start (point)
  6445. end (progn (org-end-of-subtree t t)
  6446. (or (bolp) (insert "\n"))
  6447. (org-back-over-empty-lines)
  6448. (point))
  6449. what "children")
  6450. (goto-char start)
  6451. (show-subtree)
  6452. (outline-next-heading))
  6453. (t
  6454. ;; we will sort the top-level entries in this file
  6455. (goto-char (point-min))
  6456. (or (org-on-heading-p) (outline-next-heading))
  6457. (setq start (point))
  6458. (goto-char (point-max))
  6459. (beginning-of-line 1)
  6460. (when (looking-at ".*?\\S-")
  6461. ;; File ends in a non-white line
  6462. (end-of-line 1)
  6463. (insert "\n"))
  6464. (setq end (point-max))
  6465. (setq what "top-level")
  6466. (goto-char start)
  6467. (show-all)))
  6468. (setq beg (point))
  6469. (if (>= beg end) (error "Nothing to sort"))
  6470. (unless plain-list-p
  6471. (looking-at "\\(\\*+\\)")
  6472. (setq stars (match-string 1)
  6473. re (concat "^" (regexp-quote stars) " +")
  6474. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6475. txt (buffer-substring beg end))
  6476. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6477. (if (and (not (equal stars "*")) (string-match re2 txt))
  6478. (error "Region to sort contains a level above the first entry")))
  6479. (unless sorting-type
  6480. (message
  6481. (if plain-list-p
  6482. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6483. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6484. [t]ime [s]cheduled [d]eadline [c]reated
  6485. A/N/T/S/D/C/P/O/F means reversed:")
  6486. what)
  6487. (setq sorting-type (read-char-exclusive))
  6488. (and (= (downcase sorting-type) ?f)
  6489. (setq getkey-func
  6490. (org-icompleting-read "Sort using function: "
  6491. obarray 'fboundp t nil nil))
  6492. (setq getkey-func (intern getkey-func)))
  6493. (and (= (downcase sorting-type) ?r)
  6494. (setq property
  6495. (org-icompleting-read "Property: "
  6496. (mapcar 'list (org-buffer-property-keys t))
  6497. nil t))))
  6498. (message "Sorting entries...")
  6499. (save-restriction
  6500. (narrow-to-region start end)
  6501. (let ((dcst (downcase sorting-type))
  6502. (case-fold-search nil)
  6503. (now (current-time)))
  6504. (sort-subr
  6505. (/= dcst sorting-type)
  6506. ;; This function moves to the beginning character of the "record" to
  6507. ;; be sorted.
  6508. (if plain-list-p
  6509. (lambda nil
  6510. (if (org-at-item-p) t (goto-char (point-max))))
  6511. (lambda nil
  6512. (if (re-search-forward re nil t)
  6513. (goto-char (match-beginning 0))
  6514. (goto-char (point-max)))))
  6515. ;; This function moves to the last character of the "record" being
  6516. ;; sorted.
  6517. (if plain-list-p
  6518. 'org-end-of-item
  6519. (lambda nil
  6520. (save-match-data
  6521. (condition-case nil
  6522. (outline-forward-same-level 1)
  6523. (error
  6524. (goto-char (point-max)))))))
  6525. ;; This function returns the value that gets sorted against.
  6526. (if plain-list-p
  6527. (lambda nil
  6528. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6529. (cond
  6530. ((= dcst ?n)
  6531. (string-to-number (buffer-substring (match-end 0)
  6532. (point-at-eol))))
  6533. ((= dcst ?a)
  6534. (buffer-substring (match-end 0) (point-at-eol)))
  6535. ((= dcst ?t)
  6536. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6537. (re-search-forward org-ts-regexp-both
  6538. (point-at-eol) t))
  6539. (org-time-string-to-seconds (match-string 0))
  6540. (org-float-time now)))
  6541. ((= dcst ?f)
  6542. (if getkey-func
  6543. (progn
  6544. (setq tmp (funcall getkey-func))
  6545. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6546. tmp)
  6547. (error "Invalid key function `%s'" getkey-func)))
  6548. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6549. (lambda nil
  6550. (cond
  6551. ((= dcst ?n)
  6552. (if (looking-at org-complex-heading-regexp)
  6553. (string-to-number (match-string 4))
  6554. nil))
  6555. ((= dcst ?a)
  6556. (if (looking-at org-complex-heading-regexp)
  6557. (funcall case-func (match-string 4))
  6558. nil))
  6559. ((= dcst ?t)
  6560. (let ((end (save-excursion (outline-next-heading) (point))))
  6561. (if (or (re-search-forward org-ts-regexp end t)
  6562. (re-search-forward org-ts-regexp-both end t))
  6563. (org-time-string-to-seconds (match-string 0))
  6564. (org-float-time now))))
  6565. ((= dcst ?c)
  6566. (let ((end (save-excursion (outline-next-heading) (point))))
  6567. (if (re-search-forward
  6568. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6569. end t)
  6570. (org-time-string-to-seconds (match-string 0))
  6571. (org-float-time now))))
  6572. ((= dcst ?s)
  6573. (let ((end (save-excursion (outline-next-heading) (point))))
  6574. (if (re-search-forward org-scheduled-time-regexp end t)
  6575. (org-time-string-to-seconds (match-string 1))
  6576. (org-float-time now))))
  6577. ((= dcst ?d)
  6578. (let ((end (save-excursion (outline-next-heading) (point))))
  6579. (if (re-search-forward org-deadline-time-regexp end t)
  6580. (org-time-string-to-seconds (match-string 1))
  6581. (org-float-time now))))
  6582. ((= dcst ?p)
  6583. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6584. (string-to-char (match-string 2))
  6585. org-default-priority))
  6586. ((= dcst ?r)
  6587. (or (org-entry-get nil property) ""))
  6588. ((= dcst ?o)
  6589. (if (looking-at org-complex-heading-regexp)
  6590. (- 9999 (length (member (match-string 2)
  6591. org-todo-keywords-1)))))
  6592. ((= dcst ?f)
  6593. (if getkey-func
  6594. (progn
  6595. (setq tmp (funcall getkey-func))
  6596. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6597. tmp)
  6598. (error "Invalid key function `%s'" getkey-func)))
  6599. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6600. nil
  6601. (cond
  6602. ((= dcst ?a) 'string<)
  6603. ((= dcst ?f) compare-func)
  6604. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6605. (t nil)))))
  6606. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6607. (message "Sorting entries...done")))
  6608. (defun org-do-sort (table what &optional with-case sorting-type)
  6609. "Sort TABLE of WHAT according to SORTING-TYPE.
  6610. The user will be prompted for the SORTING-TYPE if the call to this
  6611. function does not specify it. WHAT is only for the prompt, to indicate
  6612. what is being sorted. The sorting key will be extracted from
  6613. the car of the elements of the table.
  6614. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6615. (unless sorting-type
  6616. (message
  6617. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6618. what)
  6619. (setq sorting-type (read-char-exclusive)))
  6620. (let ((dcst (downcase sorting-type))
  6621. extractfun comparefun)
  6622. ;; Define the appropriate functions
  6623. (cond
  6624. ((= dcst ?n)
  6625. (setq extractfun 'string-to-number
  6626. comparefun (if (= dcst sorting-type) '< '>)))
  6627. ((= dcst ?a)
  6628. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6629. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6630. comparefun (if (= dcst sorting-type)
  6631. 'string<
  6632. (lambda (a b) (and (not (string< a b))
  6633. (not (string= a b)))))))
  6634. ((= dcst ?t)
  6635. (setq extractfun
  6636. (lambda (x)
  6637. (if (or (string-match org-ts-regexp x)
  6638. (string-match org-ts-regexp-both x))
  6639. (org-float-time
  6640. (org-time-string-to-time (match-string 0 x)))
  6641. 0))
  6642. comparefun (if (= dcst sorting-type) '< '>)))
  6643. (t (error "Invalid sorting type `%c'" sorting-type)))
  6644. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6645. table)
  6646. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6647. ;;; The orgstruct minor mode
  6648. ;; Define a minor mode which can be used in other modes in order to
  6649. ;; integrate the org-mode structure editing commands.
  6650. ;; This is really a hack, because the org-mode structure commands use
  6651. ;; keys which normally belong to the major mode. Here is how it
  6652. ;; works: The minor mode defines all the keys necessary to operate the
  6653. ;; structure commands, but wraps the commands into a function which
  6654. ;; tests if the cursor is currently at a headline or a plain list
  6655. ;; item. If that is the case, the structure command is used,
  6656. ;; temporarily setting many Org-mode variables like regular
  6657. ;; expressions for filling etc. However, when any of those keys is
  6658. ;; used at a different location, function uses `key-binding' to look
  6659. ;; up if the key has an associated command in another currently active
  6660. ;; keymap (minor modes, major mode, global), and executes that
  6661. ;; command. There might be problems if any of the keys is otherwise
  6662. ;; used as a prefix key.
  6663. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6664. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6665. ;; addresses this by checking explicitly for both bindings.
  6666. (defvar orgstruct-mode-map (make-sparse-keymap)
  6667. "Keymap for the minor `orgstruct-mode'.")
  6668. (defvar org-local-vars nil
  6669. "List of local variables, for use by `orgstruct-mode'")
  6670. ;;;###autoload
  6671. (define-minor-mode orgstruct-mode
  6672. "Toggle the minor more `orgstruct-mode'.
  6673. This mode is for using Org-mode structure commands in other modes.
  6674. The following key behave as if Org-mode was active, if the cursor
  6675. is on a headline, or on a plain list item (both in the definition
  6676. of Org-mode).
  6677. M-up Move entry/item up
  6678. M-down Move entry/item down
  6679. M-left Promote
  6680. M-right Demote
  6681. M-S-up Move entry/item up
  6682. M-S-down Move entry/item down
  6683. M-S-left Promote subtree
  6684. M-S-right Demote subtree
  6685. M-q Fill paragraph and items like in Org-mode
  6686. C-c ^ Sort entries
  6687. C-c - Cycle list bullet
  6688. TAB Cycle item visibility
  6689. M-RET Insert new heading/item
  6690. S-M-RET Insert new TODO heading / Checkbox item
  6691. C-c C-c Set tags / toggle checkbox"
  6692. nil " OrgStruct" nil
  6693. (org-load-modules-maybe)
  6694. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6695. ;;;###autoload
  6696. (defun turn-on-orgstruct ()
  6697. "Unconditionally turn on `orgstruct-mode'."
  6698. (orgstruct-mode 1))
  6699. (defun orgstruct++-mode (&optional arg)
  6700. "Toggle `orgstruct-mode', the enhanced version of it.
  6701. In addition to setting orgstruct-mode, this also exports all indentation
  6702. and autofilling variables from org-mode into the buffer. It will also
  6703. recognize item context in multiline items.
  6704. Note that turning off orgstruct-mode will *not* remove the
  6705. indentation/paragraph settings. This can only be done by refreshing the
  6706. major mode, for example with \\[normal-mode]."
  6707. (interactive "P")
  6708. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6709. (if (< arg 1)
  6710. (orgstruct-mode -1)
  6711. (orgstruct-mode 1)
  6712. (let (var val)
  6713. (mapc
  6714. (lambda (x)
  6715. (when (string-match
  6716. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6717. (symbol-name (car x)))
  6718. (setq var (car x) val (nth 1 x))
  6719. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6720. org-local-vars)
  6721. (org-set-local 'orgstruct-is-++ t))))
  6722. (defvar orgstruct-is-++ nil
  6723. "Is orgstruct-mode in ++ version in the current-buffer?")
  6724. (make-variable-buffer-local 'orgstruct-is-++)
  6725. ;;;###autoload
  6726. (defun turn-on-orgstruct++ ()
  6727. "Unconditionally turn on `orgstruct++-mode'."
  6728. (orgstruct++-mode 1))
  6729. (defun orgstruct-error ()
  6730. "Error when there is no default binding for a structure key."
  6731. (interactive)
  6732. (error "This key has no function outside structure elements"))
  6733. (defun orgstruct-setup ()
  6734. "Setup orgstruct keymaps."
  6735. (let ((nfunc 0)
  6736. (bindings
  6737. (list
  6738. '([(meta up)] org-metaup)
  6739. '([(meta down)] org-metadown)
  6740. '([(meta left)] org-metaleft)
  6741. '([(meta right)] org-metaright)
  6742. '([(meta shift up)] org-shiftmetaup)
  6743. '([(meta shift down)] org-shiftmetadown)
  6744. '([(meta shift left)] org-shiftmetaleft)
  6745. '([(meta shift right)] org-shiftmetaright)
  6746. '([?\e (up)] org-metaup)
  6747. '([?\e (down)] org-metadown)
  6748. '([?\e (left)] org-metaleft)
  6749. '([?\e (right)] org-metaright)
  6750. '([?\e (shift up)] org-shiftmetaup)
  6751. '([?\e (shift down)] org-shiftmetadown)
  6752. '([?\e (shift left)] org-shiftmetaleft)
  6753. '([?\e (shift right)] org-shiftmetaright)
  6754. '([(shift up)] org-shiftup)
  6755. '([(shift down)] org-shiftdown)
  6756. '([(shift left)] org-shiftleft)
  6757. '([(shift right)] org-shiftright)
  6758. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6759. '("\M-q" fill-paragraph)
  6760. '("\C-c^" org-sort)
  6761. '("\C-c-" org-cycle-list-bullet)))
  6762. elt key fun cmd)
  6763. (while (setq elt (pop bindings))
  6764. (setq nfunc (1+ nfunc))
  6765. (setq key (org-key (car elt))
  6766. fun (nth 1 elt)
  6767. cmd (orgstruct-make-binding fun nfunc key))
  6768. (org-defkey orgstruct-mode-map key cmd))
  6769. ;; Special treatment needed for TAB and RET
  6770. (org-defkey orgstruct-mode-map [(tab)]
  6771. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6772. (org-defkey orgstruct-mode-map "\C-i"
  6773. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6774. (org-defkey orgstruct-mode-map "\M-\C-m"
  6775. (orgstruct-make-binding 'org-insert-heading 105
  6776. "\M-\C-m" [(meta return)]))
  6777. (org-defkey orgstruct-mode-map [(meta return)]
  6778. (orgstruct-make-binding 'org-insert-heading 106
  6779. [(meta return)] "\M-\C-m"))
  6780. (org-defkey orgstruct-mode-map [(shift meta return)]
  6781. (orgstruct-make-binding 'org-insert-todo-heading 107
  6782. [(meta return)] "\M-\C-m"))
  6783. (org-defkey orgstruct-mode-map "\e\C-m"
  6784. (orgstruct-make-binding 'org-insert-heading 108
  6785. "\e\C-m" [?\e (return)]))
  6786. (org-defkey orgstruct-mode-map [?\e (return)]
  6787. (orgstruct-make-binding 'org-insert-heading 109
  6788. [?\e (return)] "\e\C-m"))
  6789. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6790. (orgstruct-make-binding 'org-insert-todo-heading 110
  6791. [?\e (return)] "\e\C-m"))
  6792. (unless org-local-vars
  6793. (setq org-local-vars (org-get-local-variables)))
  6794. t))
  6795. (defun orgstruct-make-binding (fun n &rest keys)
  6796. "Create a function for binding in the structure minor mode.
  6797. FUN is the command to call inside a table. N is used to create a unique
  6798. command name. KEYS are keys that should be checked in for a command
  6799. to execute outside of tables."
  6800. (eval
  6801. (list 'defun
  6802. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6803. '(arg)
  6804. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6805. "Outside of structure, run the binding of `"
  6806. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6807. "'.")
  6808. '(interactive "p")
  6809. (list 'if
  6810. `(org-context-p 'headline 'item
  6811. (and orgstruct-is-++
  6812. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6813. 'item-body))
  6814. (list 'org-run-like-in-org-mode (list 'quote fun))
  6815. (list 'let '(orgstruct-mode)
  6816. (list 'call-interactively
  6817. (append '(or)
  6818. (mapcar (lambda (k)
  6819. (list 'key-binding k))
  6820. keys)
  6821. '('orgstruct-error))))))))
  6822. (defun org-context-p (&rest contexts)
  6823. "Check if local context is any of CONTEXTS.
  6824. Possible values in the list of contexts are `table', `headline', and `item'."
  6825. (let ((pos (point)))
  6826. (goto-char (point-at-bol))
  6827. (prog1 (or (and (memq 'table contexts)
  6828. (looking-at "[ \t]*|"))
  6829. (and (memq 'headline contexts)
  6830. ;;????????? (looking-at "\\*+"))
  6831. (looking-at outline-regexp))
  6832. (and (memq 'item contexts)
  6833. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6834. (and (memq 'item-body contexts)
  6835. (org-in-item-p)))
  6836. (goto-char pos))))
  6837. (defun org-get-local-variables ()
  6838. "Return a list of all local variables in an org-mode buffer."
  6839. (let (varlist)
  6840. (with-current-buffer (get-buffer-create "*Org tmp*")
  6841. (erase-buffer)
  6842. (org-mode)
  6843. (setq varlist (buffer-local-variables)))
  6844. (kill-buffer "*Org tmp*")
  6845. (delq nil
  6846. (mapcar
  6847. (lambda (x)
  6848. (setq x
  6849. (if (symbolp x)
  6850. (list x)
  6851. (list (car x) (list 'quote (cdr x)))))
  6852. (if (string-match
  6853. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6854. (symbol-name (car x)))
  6855. x nil))
  6856. varlist))))
  6857. ;;;###autoload
  6858. (defun org-run-like-in-org-mode (cmd)
  6859. "Run a command, pretending that the current buffer is in Org-mode.
  6860. This will temporarily bind local variables that are typically bound in
  6861. Org-mode to the values they have in Org-mode, and then interactively
  6862. call CMD."
  6863. (org-load-modules-maybe)
  6864. (unless org-local-vars
  6865. (setq org-local-vars (org-get-local-variables)))
  6866. (eval (list 'let org-local-vars
  6867. (list 'call-interactively (list 'quote cmd)))))
  6868. ;;;; Archiving
  6869. (defun org-get-category (&optional pos)
  6870. "Get the category applying to position POS."
  6871. (get-text-property (or pos (point)) 'org-category))
  6872. (defun org-refresh-category-properties ()
  6873. "Refresh category text properties in the buffer."
  6874. (let ((def-cat (cond
  6875. ((null org-category)
  6876. (if buffer-file-name
  6877. (file-name-sans-extension
  6878. (file-name-nondirectory buffer-file-name))
  6879. "???"))
  6880. ((symbolp org-category) (symbol-name org-category))
  6881. (t org-category)))
  6882. beg end cat pos optionp)
  6883. (org-unmodified
  6884. (save-excursion
  6885. (save-restriction
  6886. (widen)
  6887. (goto-char (point-min))
  6888. (put-text-property (point) (point-max) 'org-category def-cat)
  6889. (while (re-search-forward
  6890. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6891. (setq pos (match-end 0)
  6892. optionp (equal (char-after (match-beginning 0)) ?#)
  6893. cat (org-trim (match-string 2)))
  6894. (if optionp
  6895. (setq beg (point-at-bol) end (point-max))
  6896. (org-back-to-heading t)
  6897. (setq beg (point) end (org-end-of-subtree t t)))
  6898. (put-text-property beg end 'org-category cat)
  6899. (goto-char pos)))))))
  6900. ;;;; Link Stuff
  6901. ;;; Link abbreviations
  6902. (defun org-link-expand-abbrev (link)
  6903. "Apply replacements as defined in `org-link-abbrev-alist."
  6904. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6905. (let* ((key (match-string 1 link))
  6906. (as (or (assoc key org-link-abbrev-alist-local)
  6907. (assoc key org-link-abbrev-alist)))
  6908. (tag (and (match-end 2) (match-string 3 link)))
  6909. rpl)
  6910. (if (not as)
  6911. link
  6912. (setq rpl (cdr as))
  6913. (cond
  6914. ((symbolp rpl) (funcall rpl tag))
  6915. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6916. ((string-match "%h" rpl)
  6917. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6918. (t (concat rpl tag)))))
  6919. link))
  6920. ;;; Storing and inserting links
  6921. (defvar org-insert-link-history nil
  6922. "Minibuffer history for links inserted with `org-insert-link'.")
  6923. (defvar org-stored-links nil
  6924. "Contains the links stored with `org-store-link'.")
  6925. (defvar org-store-link-plist nil
  6926. "Plist with info about the most recently link created with `org-store-link'.")
  6927. (defvar org-link-protocols nil
  6928. "Link protocols added to Org-mode using `org-add-link-type'.")
  6929. (defvar org-store-link-functions nil
  6930. "List of functions that are called to create and store a link.
  6931. Each function will be called in turn until one returns a non-nil
  6932. value. Each function should check if it is responsible for creating
  6933. this link (for example by looking at the major mode).
  6934. If not, it must exit and return nil.
  6935. If yes, it should return a non-nil value after a calling
  6936. `org-store-link-props' with a list of properties and values.
  6937. Special properties are:
  6938. :type The link prefix. like \"http\". This must be given.
  6939. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6940. This is obligatory as well.
  6941. :description Optional default description for the second pair
  6942. of brackets in an Org-mode link. The user can still change
  6943. this when inserting this link into an Org-mode buffer.
  6944. In addition to these, any additional properties can be specified
  6945. and then used in remember templates.")
  6946. (defun org-add-link-type (type &optional follow export)
  6947. "Add TYPE to the list of `org-link-types'.
  6948. Re-compute all regular expressions depending on `org-link-types'
  6949. FOLLOW and EXPORT are two functions.
  6950. FOLLOW should take the link path as the single argument and do whatever
  6951. is necessary to follow the link, for example find a file or display
  6952. a mail message.
  6953. EXPORT should format the link path for export to one of the export formats.
  6954. It should be a function accepting three arguments:
  6955. path the path of the link, the text after the prefix (like \"http:\")
  6956. desc the description of the link, if any, nil if there was no description
  6957. format the export format, a symbol like `html' or `latex'.
  6958. The function may use the FORMAT information to return different values
  6959. depending on the format. The return value will be put literally into
  6960. the exported file.
  6961. Org-mode has a built-in default for exporting links. If you are happy with
  6962. this default, there is no need to define an export function for the link
  6963. type. For a simple example of an export function, see `org-bbdb.el'."
  6964. (add-to-list 'org-link-types type t)
  6965. (org-make-link-regexps)
  6966. (if (assoc type org-link-protocols)
  6967. (setcdr (assoc type org-link-protocols) (list follow export))
  6968. (push (list type follow export) org-link-protocols)))
  6969. (defvar org-agenda-buffer-name)
  6970. ;;;###autoload
  6971. (defun org-store-link (arg)
  6972. "\\<org-mode-map>Store an org-link to the current location.
  6973. This link is added to `org-stored-links' and can later be inserted
  6974. into an org-buffer with \\[org-insert-link].
  6975. For some link types, a prefix arg is interpreted:
  6976. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6977. For file links, arg negates `org-context-in-file-links'."
  6978. (interactive "P")
  6979. (org-load-modules-maybe)
  6980. (setq org-store-link-plist nil) ; reset
  6981. (let ((outline-regexp (org-get-limited-outline-regexp))
  6982. link cpltxt desc description search txt custom-id)
  6983. (cond
  6984. ((run-hook-with-args-until-success 'org-store-link-functions)
  6985. (setq link (plist-get org-store-link-plist :link)
  6986. desc (or (plist-get org-store-link-plist :description) link)))
  6987. ((equal (buffer-name) "*Org Edit Src Example*")
  6988. (let (label gc)
  6989. (while (or (not label)
  6990. (save-excursion
  6991. (save-restriction
  6992. (widen)
  6993. (goto-char (point-min))
  6994. (re-search-forward
  6995. (regexp-quote (format org-coderef-label-format label))
  6996. nil t))))
  6997. (when label (message "Label exists already") (sit-for 2))
  6998. (setq label (read-string "Code line label: " label)))
  6999. (end-of-line 1)
  7000. (setq link (format org-coderef-label-format label))
  7001. (setq gc (- 79 (length link)))
  7002. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7003. (insert link)
  7004. (setq link (concat "(" label ")") desc nil)))
  7005. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7006. ;; We are in the agenda, link to referenced location
  7007. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7008. (get-text-property (point) 'org-marker))))
  7009. (when m
  7010. (org-with-point-at m
  7011. (call-interactively 'org-store-link)))))
  7012. ((eq major-mode 'calendar-mode)
  7013. (let ((cd (calendar-cursor-to-date)))
  7014. (setq link
  7015. (format-time-string
  7016. (car org-time-stamp-formats)
  7017. (apply 'encode-time
  7018. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7019. nil nil nil))))
  7020. (org-store-link-props :type "calendar" :date cd)))
  7021. ((eq major-mode 'w3-mode)
  7022. (setq cpltxt (if (and (buffer-name)
  7023. (not (string-match "Untitled" (buffer-name))))
  7024. (buffer-name)
  7025. (url-view-url t))
  7026. link (org-make-link (url-view-url t)))
  7027. (org-store-link-props :type "w3" :url (url-view-url t)))
  7028. ((eq major-mode 'w3m-mode)
  7029. (setq cpltxt (or w3m-current-title w3m-current-url)
  7030. link (org-make-link w3m-current-url))
  7031. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7032. ((setq search (run-hook-with-args-until-success
  7033. 'org-create-file-search-functions))
  7034. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7035. "::" search))
  7036. (setq cpltxt (or description link)))
  7037. ((eq major-mode 'image-mode)
  7038. (setq cpltxt (concat "file:"
  7039. (abbreviate-file-name buffer-file-name))
  7040. link (org-make-link cpltxt))
  7041. (org-store-link-props :type "image" :file buffer-file-name))
  7042. ((eq major-mode 'dired-mode)
  7043. ;; link to the file in the current line
  7044. (let ((file (dired-get-filename nil t)))
  7045. (setq file (if file
  7046. (abbreviate-file-name
  7047. (expand-file-name (dired-get-filename nil t)))
  7048. ;; otherwise, no file so use current directory.
  7049. default-directory))
  7050. (setq cpltxt (concat "file:" file)
  7051. link (org-make-link cpltxt))))
  7052. ((and buffer-file-name (org-mode-p))
  7053. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7054. (cond
  7055. ((org-in-regexp "<<\\(.*?\\)>>")
  7056. (setq cpltxt
  7057. (concat "file:"
  7058. (abbreviate-file-name buffer-file-name)
  7059. "::" (match-string 1))
  7060. link (org-make-link cpltxt)))
  7061. ((and (featurep 'org-id)
  7062. (or (eq org-link-to-org-use-id t)
  7063. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7064. (interactive-p))
  7065. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7066. (interactive-p)
  7067. (not custom-id))
  7068. (and org-link-to-org-use-id
  7069. (condition-case nil
  7070. (org-entry-get nil "ID")
  7071. (error nil)))))
  7072. ;; We can make a link using the ID.
  7073. (setq link (condition-case nil
  7074. (prog1 (org-id-store-link)
  7075. (setq desc (plist-get org-store-link-plist
  7076. :description)))
  7077. (error
  7078. ;; probably before first headline, link to file only
  7079. (concat "file:"
  7080. (abbreviate-file-name buffer-file-name))))))
  7081. (t
  7082. ;; Just link to current headline
  7083. (setq cpltxt (concat "file:"
  7084. (abbreviate-file-name buffer-file-name)))
  7085. ;; Add a context search string
  7086. (when (org-xor org-context-in-file-links arg)
  7087. (setq txt (cond
  7088. ((org-on-heading-p) nil)
  7089. ((org-region-active-p)
  7090. (buffer-substring (region-beginning) (region-end)))
  7091. (t nil)))
  7092. (when (or (null txt) (string-match "\\S-" txt))
  7093. (setq cpltxt
  7094. (concat cpltxt "::"
  7095. (condition-case nil
  7096. (org-make-org-heading-search-string txt)
  7097. (error "")))
  7098. desc (or (nth 4 (ignore-errors
  7099. (org-heading-components))) "NONE"))))
  7100. (if (string-match "::\\'" cpltxt)
  7101. (setq cpltxt (substring cpltxt 0 -2)))
  7102. (setq link (org-make-link cpltxt)))))
  7103. ((buffer-file-name (buffer-base-buffer))
  7104. ;; Just link to this file here.
  7105. (setq cpltxt (concat "file:"
  7106. (abbreviate-file-name
  7107. (buffer-file-name (buffer-base-buffer)))))
  7108. ;; Add a context string
  7109. (when (org-xor org-context-in-file-links arg)
  7110. (setq txt (if (org-region-active-p)
  7111. (buffer-substring (region-beginning) (region-end))
  7112. (buffer-substring (point-at-bol) (point-at-eol))))
  7113. ;; Only use search option if there is some text.
  7114. (when (string-match "\\S-" txt)
  7115. (setq cpltxt
  7116. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7117. desc "NONE")))
  7118. (setq link (org-make-link cpltxt)))
  7119. ((interactive-p)
  7120. (error "Cannot link to a buffer which is not visiting a file"))
  7121. (t (setq link nil)))
  7122. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7123. (setq link (or link cpltxt)
  7124. desc (or desc cpltxt))
  7125. (if (equal desc "NONE") (setq desc nil))
  7126. (if (and (or (interactive-p) executing-kbd-macro) link)
  7127. (progn
  7128. (setq org-stored-links
  7129. (cons (list link desc) org-stored-links))
  7130. (message "Stored: %s" (or desc link))
  7131. (when custom-id
  7132. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7133. "::#" custom-id))
  7134. (setq org-stored-links
  7135. (cons (list link desc) org-stored-links))))
  7136. (and link (org-make-link-string link desc)))))
  7137. (defun org-store-link-props (&rest plist)
  7138. "Store link properties, extract names and addresses."
  7139. (let (x adr)
  7140. (when (setq x (plist-get plist :from))
  7141. (setq adr (mail-extract-address-components x))
  7142. (setq plist (plist-put plist :fromname (car adr)))
  7143. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7144. (when (setq x (plist-get plist :to))
  7145. (setq adr (mail-extract-address-components x))
  7146. (setq plist (plist-put plist :toname (car adr)))
  7147. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7148. (let ((from (plist-get plist :from))
  7149. (to (plist-get plist :to)))
  7150. (when (and from to org-from-is-user-regexp)
  7151. (setq plist
  7152. (plist-put plist :fromto
  7153. (if (string-match org-from-is-user-regexp from)
  7154. (concat "to %t")
  7155. (concat "from %f"))))))
  7156. (setq org-store-link-plist plist))
  7157. (defun org-add-link-props (&rest plist)
  7158. "Add these properties to the link property list."
  7159. (let (key value)
  7160. (while plist
  7161. (setq key (pop plist) value (pop plist))
  7162. (setq org-store-link-plist
  7163. (plist-put org-store-link-plist key value)))))
  7164. (defun org-email-link-description (&optional fmt)
  7165. "Return the description part of an email link.
  7166. This takes information from `org-store-link-plist' and formats it
  7167. according to FMT (default from `org-email-link-description-format')."
  7168. (setq fmt (or fmt org-email-link-description-format))
  7169. (let* ((p org-store-link-plist)
  7170. (to (plist-get p :toaddress))
  7171. (from (plist-get p :fromaddress))
  7172. (table
  7173. (list
  7174. (cons "%c" (plist-get p :fromto))
  7175. (cons "%F" (plist-get p :from))
  7176. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7177. (cons "%T" (plist-get p :to))
  7178. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7179. (cons "%s" (plist-get p :subject))
  7180. (cons "%m" (plist-get p :message-id)))))
  7181. (when (string-match "%c" fmt)
  7182. ;; Check if the user wrote this message
  7183. (if (and org-from-is-user-regexp from to
  7184. (save-match-data (string-match org-from-is-user-regexp from)))
  7185. (setq fmt (replace-match "to %t" t t fmt))
  7186. (setq fmt (replace-match "from %f" t t fmt))))
  7187. (org-replace-escapes fmt table)))
  7188. (defun org-make-org-heading-search-string (&optional string heading)
  7189. "Make search string for STRING or current headline."
  7190. (interactive)
  7191. (let ((s (or string (org-get-heading))))
  7192. (unless (and string (not heading))
  7193. ;; We are using a headline, clean up garbage in there.
  7194. (if (string-match org-todo-regexp s)
  7195. (setq s (replace-match "" t t s)))
  7196. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7197. (setq s (replace-match "" t t s)))
  7198. (setq s (org-trim s))
  7199. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7200. org-comment-string "\\)") s)
  7201. (setq s (replace-match "" t t s)))
  7202. (while (string-match org-ts-regexp s)
  7203. (setq s (replace-match "" t t s))))
  7204. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7205. (setq s (replace-match " " t t s)))
  7206. (or string (setq s (concat "*" s))) ; Add * for headlines
  7207. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7208. (defun org-make-link (&rest strings)
  7209. "Concatenate STRINGS."
  7210. (apply 'concat strings))
  7211. (defun org-make-link-string (link &optional description)
  7212. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7213. (unless (string-match "\\S-" link)
  7214. (error "Empty link"))
  7215. (when (and description
  7216. (stringp description)
  7217. (not (string-match "\\S-" description)))
  7218. (setq description nil))
  7219. (when (stringp description)
  7220. ;; Remove brackets from the description, they are fatal.
  7221. (while (string-match "\\[" description)
  7222. (setq description (replace-match "{" t t description)))
  7223. (while (string-match "\\]" description)
  7224. (setq description (replace-match "}" t t description))))
  7225. (when (equal (org-link-escape link) description)
  7226. ;; No description needed, it is identical
  7227. (setq description nil))
  7228. (when (and (not description)
  7229. (not (equal link (org-link-escape link))))
  7230. (setq description (org-extract-attributes link)))
  7231. (concat "[[" (org-link-escape link) "]"
  7232. (if description (concat "[" description "]") "")
  7233. "]"))
  7234. (defconst org-link-escape-chars
  7235. '((?\ . "%20")
  7236. (?\[ . "%5B")
  7237. (?\] . "%5D")
  7238. (?\340 . "%E0") ; `a
  7239. (?\342 . "%E2") ; ^a
  7240. (?\347 . "%E7") ; ,c
  7241. (?\350 . "%E8") ; `e
  7242. (?\351 . "%E9") ; 'e
  7243. (?\352 . "%EA") ; ^e
  7244. (?\356 . "%EE") ; ^i
  7245. (?\364 . "%F4") ; ^o
  7246. (?\371 . "%F9") ; `u
  7247. (?\373 . "%FB") ; ^u
  7248. (?\; . "%3B")
  7249. ;; (?? . "%3F")
  7250. (?= . "%3D")
  7251. (?+ . "%2B")
  7252. )
  7253. "Association list of escapes for some characters problematic in links.
  7254. This is the list that is used for internal purposes.")
  7255. (defvar org-url-encoding-use-url-hexify nil)
  7256. (defconst org-link-escape-chars-browser
  7257. '((?\ . "%20")) ; 32 for the SPC char
  7258. "Association list of escapes for some characters problematic in links.
  7259. This is the list that is used before handing over to the browser.")
  7260. (defun org-link-escape (text &optional table)
  7261. "Escape characters in TEXT that are problematic for links."
  7262. (if (and org-url-encoding-use-url-hexify (not table))
  7263. (url-hexify-string text)
  7264. (setq table (or table org-link-escape-chars))
  7265. (when text
  7266. (let ((re (mapconcat (lambda (x) (regexp-quote
  7267. (char-to-string (car x))))
  7268. table "\\|")))
  7269. (while (string-match re text)
  7270. (setq text
  7271. (replace-match
  7272. (cdr (assoc (string-to-char (match-string 0 text))
  7273. table))
  7274. t t text)))
  7275. text))))
  7276. (defun org-link-unescape (text &optional table)
  7277. "Reverse the action of `org-link-escape'."
  7278. (if (and org-url-encoding-use-url-hexify (not table))
  7279. (url-unhex-string text)
  7280. (setq table (or table org-link-escape-chars))
  7281. (when text
  7282. (let ((case-fold-search t)
  7283. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7284. table "\\|")))
  7285. (while (string-match re text)
  7286. (setq text
  7287. (replace-match
  7288. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7289. table)))
  7290. t t text)))
  7291. text))))
  7292. (defun org-xor (a b)
  7293. "Exclusive or."
  7294. (if a (not b) b))
  7295. (defun org-fixup-message-id-for-http (s)
  7296. "Replace special characters in a message id, so it can be used in an http query."
  7297. (while (string-match "<" s)
  7298. (setq s (replace-match "%3C" t t s)))
  7299. (while (string-match ">" s)
  7300. (setq s (replace-match "%3E" t t s)))
  7301. (while (string-match "@" s)
  7302. (setq s (replace-match "%40" t t s)))
  7303. s)
  7304. ;;;###autoload
  7305. (defun org-insert-link-global ()
  7306. "Insert a link like Org-mode does.
  7307. This command can be called in any mode to insert a link in Org-mode syntax."
  7308. (interactive)
  7309. (org-load-modules-maybe)
  7310. (org-run-like-in-org-mode 'org-insert-link))
  7311. (defun org-insert-link (&optional complete-file link-location)
  7312. "Insert a link. At the prompt, enter the link.
  7313. Completion can be used to insert any of the link protocol prefixes like
  7314. http or ftp in use.
  7315. The history can be used to select a link previously stored with
  7316. `org-store-link'. When the empty string is entered (i.e. if you just
  7317. press RET at the prompt), the link defaults to the most recently
  7318. stored link. As SPC triggers completion in the minibuffer, you need to
  7319. use M-SPC or C-q SPC to force the insertion of a space character.
  7320. You will also be prompted for a description, and if one is given, it will
  7321. be displayed in the buffer instead of the link.
  7322. If there is already a link at point, this command will allow you to edit link
  7323. and description parts.
  7324. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7325. be selected using completion. The path to the file will be relative to the
  7326. current directory if the file is in the current directory or a subdirectory.
  7327. Otherwise, the link will be the absolute path as completed in the minibuffer
  7328. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7329. option `org-link-file-path-type'.
  7330. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7331. the current directory or below.
  7332. With three \\[universal-argument] prefixes, negate the meaning of
  7333. `org-keep-stored-link-after-insertion'.
  7334. If `org-make-link-description-function' is non-nil, this function will be
  7335. called with the link target, and the result will be the default
  7336. link description.
  7337. If the LINK-LOCATION parameter is non-nil, this value will be
  7338. used as the link location instead of reading one interactively."
  7339. (interactive "P")
  7340. (let* ((wcf (current-window-configuration))
  7341. (region (if (org-region-active-p)
  7342. (buffer-substring (region-beginning) (region-end))))
  7343. (remove (and region (list (region-beginning) (region-end))))
  7344. (desc region)
  7345. tmphist ; byte-compile incorrectly complains about this
  7346. (link link-location)
  7347. entry file all-prefixes)
  7348. (cond
  7349. (link-location) ; specified by arg, just use it.
  7350. ((org-in-regexp org-bracket-link-regexp 1)
  7351. ;; We do have a link at point, and we are going to edit it.
  7352. (setq remove (list (match-beginning 0) (match-end 0)))
  7353. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7354. (setq link (read-string "Link: "
  7355. (org-link-unescape
  7356. (org-match-string-no-properties 1)))))
  7357. ((or (org-in-regexp org-angle-link-re)
  7358. (org-in-regexp org-plain-link-re))
  7359. ;; Convert to bracket link
  7360. (setq remove (list (match-beginning 0) (match-end 0))
  7361. link (read-string "Link: "
  7362. (org-remove-angle-brackets (match-string 0)))))
  7363. ((member complete-file '((4) (16)))
  7364. ;; Completing read for file names.
  7365. (setq link (org-file-complete-link complete-file)))
  7366. (t
  7367. ;; Read link, with completion for stored links.
  7368. (with-output-to-temp-buffer "*Org Links*"
  7369. (princ "Insert a link.
  7370. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7371. (when org-stored-links
  7372. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7373. (princ (mapconcat
  7374. (lambda (x)
  7375. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7376. (reverse org-stored-links) "\n"))))
  7377. (let ((cw (selected-window)))
  7378. (select-window (get-buffer-window "*Org Links*" 'visible))
  7379. (setq truncate-lines t)
  7380. (unless (pos-visible-in-window-p (point-max))
  7381. (org-fit-window-to-buffer))
  7382. (and (window-live-p cw) (select-window cw)))
  7383. ;; Fake a link history, containing the stored links.
  7384. (setq tmphist (append (mapcar 'car org-stored-links)
  7385. org-insert-link-history))
  7386. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7387. (mapcar 'car org-link-abbrev-alist)
  7388. org-link-types))
  7389. (unwind-protect
  7390. (progn
  7391. (setq link
  7392. (let ((org-completion-use-ido nil)
  7393. (org-completion-use-iswitchb nil))
  7394. (org-completing-read
  7395. "Link: "
  7396. (append
  7397. (mapcar (lambda (x) (list (concat x ":")))
  7398. all-prefixes)
  7399. (mapcar 'car org-stored-links))
  7400. nil nil nil
  7401. 'tmphist
  7402. (car (car org-stored-links)))))
  7403. (if (not (string-match "\\S-" link))
  7404. (error "No link selected"))
  7405. (if (or (member link all-prefixes)
  7406. (and (equal ":" (substring link -1))
  7407. (member (substring link 0 -1) all-prefixes)
  7408. (setq link (substring link 0 -1))))
  7409. (setq link (org-link-try-special-completion link))))
  7410. (set-window-configuration wcf)
  7411. (kill-buffer "*Org Links*"))
  7412. (setq entry (assoc link org-stored-links))
  7413. (or entry (push link org-insert-link-history))
  7414. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7415. (not org-keep-stored-link-after-insertion))
  7416. (setq org-stored-links (delq (assoc link org-stored-links)
  7417. org-stored-links)))
  7418. (setq desc (or desc (nth 1 entry)))))
  7419. (if (string-match org-plain-link-re link)
  7420. ;; URL-like link, normalize the use of angular brackets.
  7421. (setq link (org-make-link (org-remove-angle-brackets link))))
  7422. ;; Check if we are linking to the current file with a search option
  7423. ;; If yes, simplify the link by using only the search option.
  7424. (when (and buffer-file-name
  7425. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7426. (let* ((path (match-string 1 link))
  7427. (case-fold-search nil)
  7428. (search (match-string 2 link)))
  7429. (save-match-data
  7430. (if (equal (file-truename buffer-file-name) (file-truename path))
  7431. ;; We are linking to this same file, with a search option
  7432. (setq link search)))))
  7433. ;; Check if we can/should use a relative path. If yes, simplify the link
  7434. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7435. (let* ((type (match-string 1 link))
  7436. (path (match-string 2 link))
  7437. (origpath path)
  7438. (case-fold-search nil))
  7439. (cond
  7440. ((or (eq org-link-file-path-type 'absolute)
  7441. (equal complete-file '(16)))
  7442. (setq path (abbreviate-file-name (expand-file-name path))))
  7443. ((eq org-link-file-path-type 'noabbrev)
  7444. (setq path (expand-file-name path)))
  7445. ((eq org-link-file-path-type 'relative)
  7446. (setq path (file-relative-name path)))
  7447. (t
  7448. (save-match-data
  7449. (if (string-match (concat "^" (regexp-quote
  7450. (file-name-as-directory
  7451. (expand-file-name "."))))
  7452. (expand-file-name path))
  7453. ;; We are linking a file with relative path name.
  7454. (setq path (substring (expand-file-name path)
  7455. (match-end 0)))
  7456. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7457. (setq link (concat type path))
  7458. (if (equal desc origpath)
  7459. (setq desc path))))
  7460. (if org-make-link-description-function
  7461. (setq desc (funcall org-make-link-description-function link desc)))
  7462. (setq desc (read-string "Description: " desc))
  7463. (unless (string-match "\\S-" desc) (setq desc nil))
  7464. (if remove (apply 'delete-region remove))
  7465. (insert (org-make-link-string link desc))))
  7466. (defun org-link-try-special-completion (type)
  7467. "If there is completion support for link type TYPE, offer it."
  7468. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7469. (if (functionp fun)
  7470. (funcall fun)
  7471. (read-string "Link (no completion support): " (concat type ":")))))
  7472. (defun org-file-complete-link (&optional arg)
  7473. "Create a file link using completion."
  7474. (let (file link)
  7475. (setq file (read-file-name "File: "))
  7476. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7477. (pwd1 (file-name-as-directory (abbreviate-file-name
  7478. (expand-file-name ".")))))
  7479. (cond
  7480. ((equal arg '(16))
  7481. (setq link (org-make-link
  7482. "file:"
  7483. (abbreviate-file-name (expand-file-name file)))))
  7484. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7485. (setq link (org-make-link "file:" (match-string 1 file))))
  7486. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7487. (expand-file-name file))
  7488. (setq link (org-make-link
  7489. "file:" (match-string 1 (expand-file-name file)))))
  7490. (t (setq link (org-make-link "file:" file)))))
  7491. link))
  7492. (defun org-completing-read (&rest args)
  7493. "Completing-read with SPACE being a normal character."
  7494. (let ((minibuffer-local-completion-map
  7495. (copy-keymap minibuffer-local-completion-map)))
  7496. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7497. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7498. (apply 'org-icompleting-read args)))
  7499. (defun org-completing-read-no-i (&rest args)
  7500. (let (org-completion-use-ido org-completion-use-iswitchb)
  7501. (apply 'org-completing-read args)))
  7502. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7503. "Use iswitch as a completing-read replacement to choose from choices.
  7504. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7505. from."
  7506. (let* ((iswitchb-use-virtual-buffers nil)
  7507. (iswitchb-make-buflist-hook
  7508. (lambda ()
  7509. (setq iswitchb-temp-buflist choices))))
  7510. (iswitchb-read-buffer prompt)))
  7511. (defun org-icompleting-read (&rest args)
  7512. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7513. (org-without-partial-completion
  7514. (if (and org-completion-use-ido
  7515. (fboundp 'ido-completing-read)
  7516. (boundp 'ido-mode) ido-mode
  7517. (listp (second args)))
  7518. (let ((ido-enter-matching-directory nil))
  7519. (apply 'ido-completing-read (concat (car args))
  7520. (if (consp (car (nth 1 args)))
  7521. (mapcar (lambda (x) (car x)) (nth 1 args))
  7522. (nth 1 args))
  7523. (cddr args)))
  7524. (if (and org-completion-use-iswitchb
  7525. (boundp 'iswitchb-mode) iswitchb-mode
  7526. (listp (second args)))
  7527. (apply 'org-iswitchb-completing-read (concat (car args))
  7528. (if (consp (car (nth 1 args)))
  7529. (mapcar (lambda (x) (car x)) (nth 1 args))
  7530. (nth 1 args))
  7531. (cddr args))
  7532. (apply 'completing-read args)))))
  7533. (defun org-extract-attributes (s)
  7534. "Extract the attributes cookie from a string and set as text property."
  7535. (let (a attr (start 0) key value)
  7536. (save-match-data
  7537. (when (string-match "{{\\([^}]+\\)}}$" s)
  7538. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7539. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7540. (setq key (match-string 1 a) value (match-string 2 a)
  7541. start (match-end 0)
  7542. attr (plist-put attr (intern key) value))))
  7543. (org-add-props s nil 'org-attr attr))
  7544. s))
  7545. (defun org-extract-attributes-from-string (tag)
  7546. (let (key value attr)
  7547. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7548. (setq key (match-string 1 tag) value (match-string 2 tag)
  7549. tag (replace-match "" t t tag)
  7550. attr (plist-put attr (intern key) value)))
  7551. (cons tag attr)))
  7552. (defun org-attributes-to-string (plist)
  7553. "Format a property list into an HTML attribute list."
  7554. (let ((s "") key value)
  7555. (while plist
  7556. (setq key (pop plist) value (pop plist))
  7557. (and value
  7558. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7559. s))
  7560. ;;; Opening/following a link
  7561. (defvar org-link-search-failed nil)
  7562. (defvar org-open-link-functions nil
  7563. "Hook for functions finding a plain text link.
  7564. These functions must take a single argument, the link content.
  7565. They will be called for links that look like [[link text][description]]
  7566. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7567. like a filename (e.g. \"./blue.png\").
  7568. These functions will be called *before* Org attempts to resolve the
  7569. link by doing text searches in the current buffer - so if you want a
  7570. link \"[[target]]\" to still find \"<<target>>\", your function should
  7571. handle this as a special case.
  7572. When the function does handle the link, it must return a non-nil value.
  7573. If it decides that it is not responsible for this link, it must return
  7574. nil to indicate that that Org-mode can continue with other options
  7575. like exact and fuzzy text search.")
  7576. (defun org-next-link ()
  7577. "Move forward to the next link.
  7578. If the link is in hidden text, expose it."
  7579. (interactive)
  7580. (when (and org-link-search-failed (eq this-command last-command))
  7581. (goto-char (point-min))
  7582. (message "Link search wrapped back to beginning of buffer"))
  7583. (setq org-link-search-failed nil)
  7584. (let* ((pos (point))
  7585. (ct (org-context))
  7586. (a (assoc :link ct)))
  7587. (if a (goto-char (nth 2 a)))
  7588. (if (re-search-forward org-any-link-re nil t)
  7589. (progn
  7590. (goto-char (match-beginning 0))
  7591. (if (org-invisible-p) (org-show-context)))
  7592. (goto-char pos)
  7593. (setq org-link-search-failed t)
  7594. (error "No further link found"))))
  7595. (defun org-previous-link ()
  7596. "Move backward to the previous link.
  7597. If the link is in hidden text, expose it."
  7598. (interactive)
  7599. (when (and org-link-search-failed (eq this-command last-command))
  7600. (goto-char (point-max))
  7601. (message "Link search wrapped back to end of buffer"))
  7602. (setq org-link-search-failed nil)
  7603. (let* ((pos (point))
  7604. (ct (org-context))
  7605. (a (assoc :link ct)))
  7606. (if a (goto-char (nth 1 a)))
  7607. (if (re-search-backward org-any-link-re nil t)
  7608. (progn
  7609. (goto-char (match-beginning 0))
  7610. (if (org-invisible-p) (org-show-context)))
  7611. (goto-char pos)
  7612. (setq org-link-search-failed t)
  7613. (error "No further link found"))))
  7614. (defun org-translate-link (s)
  7615. "Translate a link string if a translation function has been defined."
  7616. (if (and org-link-translation-function
  7617. (fboundp org-link-translation-function)
  7618. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7619. (progn
  7620. (setq s (funcall org-link-translation-function
  7621. (match-string 1) (match-string 2)))
  7622. (concat (car s) ":" (cdr s)))
  7623. s))
  7624. (defun org-translate-link-from-planner (type path)
  7625. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7626. This is still an experimental function, your mileage may vary."
  7627. (cond
  7628. ((member type '("http" "https" "news" "ftp"))
  7629. ;; standard Internet links are the same.
  7630. nil)
  7631. ((and (equal type "irc") (string-match "^//" path))
  7632. ;; Planner has two / at the beginning of an irc link, we have 1.
  7633. ;; We should have zero, actually....
  7634. (setq path (substring path 1)))
  7635. ((and (equal type "lisp") (string-match "^/" path))
  7636. ;; Planner has a slash, we do not.
  7637. (setq type "elisp" path (substring path 1)))
  7638. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7639. ;; A typical message link. Planner has the id after the final slash,
  7640. ;; we separate it with a hash mark
  7641. (setq path (concat (match-string 1 path) "#"
  7642. (org-remove-angle-brackets (match-string 2 path)))))
  7643. )
  7644. (cons type path))
  7645. (defun org-find-file-at-mouse (ev)
  7646. "Open file link or URL at mouse."
  7647. (interactive "e")
  7648. (mouse-set-point ev)
  7649. (org-open-at-point 'in-emacs))
  7650. (defun org-open-at-mouse (ev)
  7651. "Open file link or URL at mouse."
  7652. (interactive "e")
  7653. (mouse-set-point ev)
  7654. (if (eq major-mode 'org-agenda-mode)
  7655. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7656. (org-open-at-point))
  7657. (defvar org-window-config-before-follow-link nil
  7658. "The window configuration before following a link.
  7659. This is saved in case the need arises to restore it.")
  7660. (defvar org-open-link-marker (make-marker)
  7661. "Marker pointing to the location where `org-open-at-point; was called.")
  7662. ;;;###autoload
  7663. (defun org-open-at-point-global ()
  7664. "Follow a link like Org-mode does.
  7665. This command can be called in any mode to follow a link that has
  7666. Org-mode syntax."
  7667. (interactive)
  7668. (org-run-like-in-org-mode 'org-open-at-point))
  7669. ;;;###autoload
  7670. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7671. "Open a link in the string S, as if it was in Org-mode."
  7672. (interactive "sLink: \nP")
  7673. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7674. (with-temp-buffer
  7675. (let ((org-inhibit-startup t))
  7676. (org-mode)
  7677. (insert s)
  7678. (goto-char (point-min))
  7679. (when reference-buffer
  7680. (setq org-link-abbrev-alist-local
  7681. (with-current-buffer reference-buffer
  7682. org-link-abbrev-alist-local)))
  7683. (org-open-at-point arg reference-buffer)))))
  7684. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7685. "Open link at or after point.
  7686. If there is no link at point, this function will search forward up to
  7687. the end of the current line.
  7688. Normally, files will be opened by an appropriate application. If the
  7689. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7690. With a double prefix argument, try to open outside of Emacs, in the
  7691. application the system uses for this file type."
  7692. (interactive "P")
  7693. (org-load-modules-maybe)
  7694. (move-marker org-open-link-marker (point))
  7695. (setq org-window-config-before-follow-link (current-window-configuration))
  7696. (org-remove-occur-highlights nil nil t)
  7697. (cond
  7698. ((and (org-on-heading-p)
  7699. (not (org-in-regexp
  7700. (concat org-plain-link-re "\\|"
  7701. org-bracket-link-regexp "\\|"
  7702. org-angle-link-re "\\|"
  7703. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7704. (not (get-text-property (point) 'org-linked-text)))
  7705. (or (org-offer-links-in-entry in-emacs)
  7706. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7707. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7708. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7709. (org-footnote-action))
  7710. (t
  7711. (let (type path link line search (pos (point)))
  7712. (catch 'match
  7713. (save-excursion
  7714. (skip-chars-forward "^]\n\r")
  7715. (when (org-in-regexp org-bracket-link-regexp 1)
  7716. (setq link (org-extract-attributes
  7717. (org-link-unescape (org-match-string-no-properties 1))))
  7718. (while (string-match " *\n *" link)
  7719. (setq link (replace-match " " t t link)))
  7720. (setq link (org-link-expand-abbrev link))
  7721. (cond
  7722. ((or (file-name-absolute-p link)
  7723. (string-match "^\\.\\.?/" link))
  7724. (setq type "file" path link))
  7725. ((string-match org-link-re-with-space3 link)
  7726. (setq type (match-string 1 link) path (match-string 2 link)))
  7727. (t (setq type "thisfile" path link)))
  7728. (throw 'match t)))
  7729. (when (get-text-property (point) 'org-linked-text)
  7730. (setq type "thisfile"
  7731. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7732. (1+ (point)) (point))
  7733. path (buffer-substring
  7734. (previous-single-property-change pos 'org-linked-text)
  7735. (next-single-property-change pos 'org-linked-text)))
  7736. (throw 'match t))
  7737. (save-excursion
  7738. (when (or (org-in-regexp org-angle-link-re)
  7739. (org-in-regexp org-plain-link-re))
  7740. (setq type (match-string 1) path (match-string 2))
  7741. (throw 'match t)))
  7742. (save-excursion
  7743. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7744. (setq type "tags"
  7745. path (match-string 1))
  7746. (while (string-match ":" path)
  7747. (setq path (replace-match "+" t t path)))
  7748. (throw 'match t)))
  7749. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7750. (setq type "tree-match"
  7751. path (match-string 1))
  7752. (throw 'match t)))
  7753. (unless path
  7754. (error "No link found"))
  7755. ;; switch back to reference buffer
  7756. ;; needed when if called in a temporary buffer through
  7757. ;; org-open-link-from-string
  7758. (with-current-buffer (or reference-buffer (current-buffer))
  7759. ;; Remove any trailing spaces in path
  7760. (if (string-match " +\\'" path)
  7761. (setq path (replace-match "" t t path)))
  7762. (if (and org-link-translation-function
  7763. (fboundp org-link-translation-function))
  7764. ;; Check if we need to translate the link
  7765. (let ((tmp (funcall org-link-translation-function type path)))
  7766. (setq type (car tmp) path (cdr tmp))))
  7767. (cond
  7768. ((assoc type org-link-protocols)
  7769. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7770. ((equal type "mailto")
  7771. (let ((cmd (car org-link-mailto-program))
  7772. (args (cdr org-link-mailto-program)) args1
  7773. (address path) (subject "") a)
  7774. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7775. (setq address (match-string 1 path)
  7776. subject (org-link-escape (match-string 2 path))))
  7777. (while args
  7778. (cond
  7779. ((not (stringp (car args))) (push (pop args) args1))
  7780. (t (setq a (pop args))
  7781. (if (string-match "%a" a)
  7782. (setq a (replace-match address t t a)))
  7783. (if (string-match "%s" a)
  7784. (setq a (replace-match subject t t a)))
  7785. (push a args1))))
  7786. (apply cmd (nreverse args1))))
  7787. ((member type '("http" "https" "ftp" "news"))
  7788. (browse-url (concat type ":" (org-link-escape
  7789. path org-link-escape-chars-browser))))
  7790. ((member type '("message"))
  7791. (browse-url (concat type ":" path)))
  7792. ((string= type "tags")
  7793. (org-tags-view in-emacs path))
  7794. ((string= type "tree-match")
  7795. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7796. ((string= type "file")
  7797. (if (string-match "::\\([0-9]+\\)\\'" path)
  7798. (setq line (string-to-number (match-string 1 path))
  7799. path (substring path 0 (match-beginning 0)))
  7800. (if (string-match "::\\(.+\\)\\'" path)
  7801. (setq search (match-string 1 path)
  7802. path (substring path 0 (match-beginning 0)))))
  7803. (if (string-match "[*?{]" (file-name-nondirectory path))
  7804. (dired path)
  7805. (org-open-file path in-emacs line search)))
  7806. ((string= type "news")
  7807. (require 'org-gnus)
  7808. (org-gnus-follow-link path))
  7809. ((string= type "shell")
  7810. (let ((cmd path))
  7811. (if (or (not org-confirm-shell-link-function)
  7812. (funcall org-confirm-shell-link-function
  7813. (format "Execute \"%s\" in shell? "
  7814. (org-add-props cmd nil
  7815. 'face 'org-warning))))
  7816. (progn
  7817. (message "Executing %s" cmd)
  7818. (shell-command cmd))
  7819. (error "Abort"))))
  7820. ((string= type "elisp")
  7821. (let ((cmd path))
  7822. (if (or (not org-confirm-elisp-link-function)
  7823. (funcall org-confirm-elisp-link-function
  7824. (format "Execute \"%s\" as elisp? "
  7825. (org-add-props cmd nil
  7826. 'face 'org-warning))))
  7827. (message "%s => %s" cmd
  7828. (if (equal (string-to-char cmd) ?\()
  7829. (eval (read cmd))
  7830. (call-interactively (read cmd))))
  7831. (error "Abort"))))
  7832. ((and (string= type "thisfile")
  7833. (run-hook-with-args-until-success
  7834. 'org-open-link-functions path)))
  7835. ((string= type "thisfile")
  7836. (if in-emacs
  7837. (switch-to-buffer-other-window
  7838. (org-get-buffer-for-internal-link (current-buffer)))
  7839. (org-mark-ring-push))
  7840. (let ((cmd `(org-link-search
  7841. ,path
  7842. ,(cond ((equal in-emacs '(4)) 'occur)
  7843. ((equal in-emacs '(16)) 'org-occur)
  7844. (t nil))
  7845. ,pos)))
  7846. (condition-case nil (eval cmd)
  7847. (error (progn (widen) (eval cmd))))))
  7848. (t
  7849. (browse-url-at-point)))))))
  7850. (move-marker org-open-link-marker nil)
  7851. (run-hook-with-args 'org-follow-link-hook))
  7852. (defun org-offer-links-in-entry (&optional nth zero)
  7853. "Offer links in the current entry and follow the selected link.
  7854. If there is only one link, follow it immediately as well.
  7855. If NTH is an integer, immediately pick the NTH link found.
  7856. If ZERO is a string, check also this string for a link, and if
  7857. there is one, offer it as link number zero."
  7858. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7859. "\\(" org-angle-link-re "\\)\\|"
  7860. "\\(" org-plain-link-re "\\)"))
  7861. (cnt ?0)
  7862. (in-emacs (if (integerp nth) nil nth))
  7863. have-zero end links link c)
  7864. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7865. (push (match-string 0 zero) links)
  7866. (setq cnt (1- cnt) have-zero t))
  7867. (save-excursion
  7868. (org-back-to-heading t)
  7869. (setq end (save-excursion (outline-next-heading) (point)))
  7870. (while (re-search-forward re end t)
  7871. (push (match-string 0) links))
  7872. (setq links (org-uniquify (reverse links))))
  7873. (cond
  7874. ((null links)
  7875. (message "No links"))
  7876. ((equal (length links) 1)
  7877. (setq link (list (car links))))
  7878. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7879. (setq link (nth (if have-zero nth (1- nth)) links)))
  7880. (t ; we have to select a link
  7881. (save-excursion
  7882. (save-window-excursion
  7883. (delete-other-windows)
  7884. (with-output-to-temp-buffer "*Select Link*"
  7885. (mapc (lambda (l)
  7886. (if (not (string-match org-bracket-link-regexp l))
  7887. (princ (format "[%c] %s\n" (incf cnt)
  7888. (org-remove-angle-brackets l)))
  7889. (if (match-end 3)
  7890. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7891. (match-string 3 l) (match-string 1 l)))
  7892. (princ (format "[%c] %s\n" (incf cnt)
  7893. (match-string 1 l))))))
  7894. links))
  7895. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7896. (message "Select link to open, RET to open all:")
  7897. (setq c (read-char-exclusive))
  7898. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7899. (when (equal c ?q) (error "Abort"))
  7900. (if (equal c ?\C-m)
  7901. (setq link links)
  7902. (setq nth (- c ?0))
  7903. (if have-zero (setq nth (1+ nth)))
  7904. (unless (and (integerp nth) (>= (length links) nth))
  7905. (error "Invalid link selection"))
  7906. (setq link (list (nth (1- nth) links))))))
  7907. (if link
  7908. (let ((buf (current-buffer)))
  7909. (dolist (l link)
  7910. (org-open-link-from-string l in-emacs buf))
  7911. t)
  7912. nil)))
  7913. ;; Add special file links that specify the way of opening
  7914. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7915. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7916. (defun org-open-file-with-system (path)
  7917. "Open file at PATH using the system way of opeing it."
  7918. (org-open-file path 'system))
  7919. (defun org-open-file-with-emacs (path)
  7920. "Open file at PATH in emacs."
  7921. (org-open-file path 'emacs))
  7922. (defun org-remove-file-link-modifiers ()
  7923. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7924. (goto-char (point-min))
  7925. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7926. (org-if-unprotected
  7927. (replace-match "file:" t t))))
  7928. (eval-after-load "org-exp"
  7929. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7930. 'org-remove-file-link-modifiers))
  7931. ;;;; Time estimates
  7932. (defun org-get-effort (&optional pom)
  7933. "Get the effort estimate for the current entry."
  7934. (org-entry-get pom org-effort-property))
  7935. ;;; File search
  7936. (defvar org-create-file-search-functions nil
  7937. "List of functions to construct the right search string for a file link.
  7938. These functions are called in turn with point at the location to
  7939. which the link should point.
  7940. A function in the hook should first test if it would like to
  7941. handle this file type, for example by checking the major-mode or
  7942. the file extension. If it decides not to handle this file, it
  7943. should just return nil to give other functions a chance. If it
  7944. does handle the file, it must return the search string to be used
  7945. when following the link. The search string will be part of the
  7946. file link, given after a double colon, and `org-open-at-point'
  7947. will automatically search for it. If special measures must be
  7948. taken to make the search successful, another function should be
  7949. added to the companion hook `org-execute-file-search-functions',
  7950. which see.
  7951. A function in this hook may also use `setq' to set the variable
  7952. `description' to provide a suggestion for the descriptive text to
  7953. be used for this link when it gets inserted into an Org-mode
  7954. buffer with \\[org-insert-link].")
  7955. (defvar org-execute-file-search-functions nil
  7956. "List of functions to execute a file search triggered by a link.
  7957. Functions added to this hook must accept a single argument, the
  7958. search string that was part of the file link, the part after the
  7959. double colon. The function must first check if it would like to
  7960. handle this search, for example by checking the major-mode or the
  7961. file extension. If it decides not to handle this search, it
  7962. should just return nil to give other functions a chance. If it
  7963. does handle the search, it must return a non-nil value to keep
  7964. other functions from trying.
  7965. Each function can access the current prefix argument through the
  7966. variable `current-prefix-argument'. Note that a single prefix is
  7967. used to force opening a link in Emacs, so it may be good to only
  7968. use a numeric or double prefix to guide the search function.
  7969. In case this is needed, a function in this hook can also restore
  7970. the window configuration before `org-open-at-point' was called using:
  7971. (set-window-configuration org-window-config-before-follow-link)")
  7972. (defun org-link-search (s &optional type avoid-pos)
  7973. "Search for a link search option.
  7974. If S is surrounded by forward slashes, it is interpreted as a
  7975. regular expression. In org-mode files, this will create an `org-occur'
  7976. sparse tree. In ordinary files, `occur' will be used to list matches.
  7977. If the current buffer is in `dired-mode', grep will be used to search
  7978. in all files. If AVOID-POS is given, ignore matches near that position."
  7979. (let ((case-fold-search t)
  7980. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7981. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7982. (append '(("") (" ") ("\t") ("\n"))
  7983. org-emphasis-alist)
  7984. "\\|") "\\)"))
  7985. (pos (point))
  7986. (pre nil) (post nil)
  7987. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7988. (cond
  7989. ;; First check if there are any special
  7990. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7991. ;; Now try the builtin stuff
  7992. ((and (equal (string-to-char s0) ?#)
  7993. (> (length s0) 1)
  7994. (save-excursion
  7995. (goto-char (point-min))
  7996. (and
  7997. (re-search-forward
  7998. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7999. (setq type 'dedicated
  8000. pos (match-beginning 0))))
  8001. ;; There is an exact target for this
  8002. (goto-char pos)
  8003. (org-back-to-heading t)))
  8004. ((save-excursion
  8005. (goto-char (point-min))
  8006. (and
  8007. (re-search-forward
  8008. (concat "<<" (regexp-quote s0) ">>") nil t)
  8009. (setq type 'dedicated
  8010. pos (match-beginning 0))))
  8011. ;; There is an exact target for this
  8012. (goto-char pos))
  8013. ((and (string-match "^(\\(.*\\))$" s0)
  8014. (save-excursion
  8015. (goto-char (point-min))
  8016. (and
  8017. (re-search-forward
  8018. (concat "[^[]" (regexp-quote
  8019. (format org-coderef-label-format
  8020. (match-string 1 s0))))
  8021. nil t)
  8022. (setq type 'dedicated
  8023. pos (1+ (match-beginning 0))))))
  8024. ;; There is a coderef target for this
  8025. (goto-char pos))
  8026. ((string-match "^/\\(.*\\)/$" s)
  8027. ;; A regular expression
  8028. (cond
  8029. ((org-mode-p)
  8030. (org-occur (match-string 1 s)))
  8031. ;;((eq major-mode 'dired-mode)
  8032. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8033. (t (org-do-occur (match-string 1 s)))))
  8034. (t
  8035. ;; A normal search strings
  8036. (when (equal (string-to-char s) ?*)
  8037. ;; Anchor on headlines, post may include tags.
  8038. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8039. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8040. s (substring s 1)))
  8041. (remove-text-properties
  8042. 0 (length s)
  8043. '(face nil mouse-face nil keymap nil fontified nil) s)
  8044. ;; Make a series of regular expressions to find a match
  8045. (setq words (org-split-string s "[ \n\r\t]+")
  8046. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8047. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8048. "\\)" markers)
  8049. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8050. re2a (concat "[ \t\r\n]" re2a_)
  8051. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8052. re4 (concat "[^a-zA-Z_]" re4_)
  8053. re1 (concat pre re2 post)
  8054. re3 (concat pre (if pre re4_ re4) post)
  8055. re5 (concat pre ".*" re4)
  8056. re2 (concat pre re2)
  8057. re2a (concat pre (if pre re2a_ re2a))
  8058. re4 (concat pre (if pre re4_ re4))
  8059. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8060. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8061. re5 "\\)"
  8062. ))
  8063. (cond
  8064. ((eq type 'org-occur) (org-occur reall))
  8065. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8066. (t (goto-char (point-min))
  8067. (setq type 'fuzzy)
  8068. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8069. (org-search-not-self 1 re1 nil t)
  8070. (org-search-not-self 1 re2 nil t)
  8071. (org-search-not-self 1 re2a nil t)
  8072. (org-search-not-self 1 re3 nil t)
  8073. (org-search-not-self 1 re4 nil t)
  8074. (org-search-not-self 1 re5 nil t)
  8075. )
  8076. (goto-char (match-beginning 1))
  8077. (goto-char pos)
  8078. (error "No match")))))
  8079. (t
  8080. ;; Normal string-search
  8081. (goto-char (point-min))
  8082. (if (search-forward s nil t)
  8083. (goto-char (match-beginning 0))
  8084. (error "No match"))))
  8085. (and (org-mode-p) (org-show-context 'link-search))
  8086. type))
  8087. (defun org-search-not-self (group &rest args)
  8088. "Execute `re-search-forward', but only accept matches that do not
  8089. enclose the position of `org-open-link-marker'."
  8090. (let ((m org-open-link-marker))
  8091. (catch 'exit
  8092. (while (apply 're-search-forward args)
  8093. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8094. (goto-char (match-end group))
  8095. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8096. (> (match-beginning 0) (marker-position m))
  8097. (< (match-end 0) (marker-position m)))
  8098. (save-match-data
  8099. (or (not (org-in-regexp
  8100. org-bracket-link-analytic-regexp 1))
  8101. (not (match-end 4)) ; no description
  8102. (and (<= (match-beginning 4) (point))
  8103. (>= (match-end 4) (point))))))
  8104. (throw 'exit (point))))))))
  8105. (defun org-get-buffer-for-internal-link (buffer)
  8106. "Return a buffer to be used for displaying the link target of internal links."
  8107. (cond
  8108. ((not org-display-internal-link-with-indirect-buffer)
  8109. buffer)
  8110. ((string-match "(Clone)$" (buffer-name buffer))
  8111. (message "Buffer is already a clone, not making another one")
  8112. ;; we also do not modify visibility in this case
  8113. buffer)
  8114. (t ; make a new indirect buffer for displaying the link
  8115. (let* ((bn (buffer-name buffer))
  8116. (ibn (concat bn "(Clone)"))
  8117. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8118. (with-current-buffer ib (org-overview))
  8119. ib))))
  8120. (defun org-do-occur (regexp &optional cleanup)
  8121. "Call the Emacs command `occur'.
  8122. If CLEANUP is non-nil, remove the printout of the regular expression
  8123. in the *Occur* buffer. This is useful if the regex is long and not useful
  8124. to read."
  8125. (occur regexp)
  8126. (when cleanup
  8127. (let ((cwin (selected-window)) win beg end)
  8128. (when (setq win (get-buffer-window "*Occur*"))
  8129. (select-window win))
  8130. (goto-char (point-min))
  8131. (when (re-search-forward "match[a-z]+" nil t)
  8132. (setq beg (match-end 0))
  8133. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8134. (setq end (1- (match-beginning 0)))))
  8135. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8136. (goto-char (point-min))
  8137. (select-window cwin))))
  8138. ;;; The mark ring for links jumps
  8139. (defvar org-mark-ring nil
  8140. "Mark ring for positions before jumps in Org-mode.")
  8141. (defvar org-mark-ring-last-goto nil
  8142. "Last position in the mark ring used to go back.")
  8143. ;; Fill and close the ring
  8144. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8145. (loop for i from 1 to org-mark-ring-length do
  8146. (push (make-marker) org-mark-ring))
  8147. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8148. org-mark-ring)
  8149. (defun org-mark-ring-push (&optional pos buffer)
  8150. "Put the current position or POS into the mark ring and rotate it."
  8151. (interactive)
  8152. (setq pos (or pos (point)))
  8153. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8154. (move-marker (car org-mark-ring)
  8155. (or pos (point))
  8156. (or buffer (current-buffer)))
  8157. (message "%s"
  8158. (substitute-command-keys
  8159. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8160. (defun org-mark-ring-goto (&optional n)
  8161. "Jump to the previous position in the mark ring.
  8162. With prefix arg N, jump back that many stored positions. When
  8163. called several times in succession, walk through the entire ring.
  8164. Org-mode commands jumping to a different position in the current file,
  8165. or to another Org-mode file, automatically push the old position
  8166. onto the ring."
  8167. (interactive "p")
  8168. (let (p m)
  8169. (if (eq last-command this-command)
  8170. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8171. (setq p org-mark-ring))
  8172. (setq org-mark-ring-last-goto p)
  8173. (setq m (car p))
  8174. (switch-to-buffer (marker-buffer m))
  8175. (goto-char m)
  8176. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8177. (defun org-remove-angle-brackets (s)
  8178. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8179. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8180. s)
  8181. (defun org-add-angle-brackets (s)
  8182. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8183. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8184. s)
  8185. (defun org-remove-double-quotes (s)
  8186. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8187. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8188. s)
  8189. ;;; Following specific links
  8190. (defun org-follow-timestamp-link ()
  8191. (cond
  8192. ((org-at-date-range-p t)
  8193. (let ((org-agenda-start-on-weekday)
  8194. (t1 (match-string 1))
  8195. (t2 (match-string 2)))
  8196. (setq t1 (time-to-days (org-time-string-to-time t1))
  8197. t2 (time-to-days (org-time-string-to-time t2)))
  8198. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8199. ((org-at-timestamp-p t)
  8200. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8201. (substring (match-string 1) 0 10)))
  8202. 1))
  8203. (t (error "This should not happen"))))
  8204. ;;; Following file links
  8205. (defvar org-wait nil)
  8206. (defun org-open-file (path &optional in-emacs line search)
  8207. "Open the file at PATH.
  8208. First, this expands any special file name abbreviations. Then the
  8209. configuration variable `org-file-apps' is checked if it contains an
  8210. entry for this file type, and if yes, the corresponding command is launched.
  8211. If no application is found, Emacs simply visits the file.
  8212. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8213. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8214. and to use an external application to visit the file.
  8215. Optional LINE specifies a line to go to, optional SEARCH a string
  8216. to search for. If LINE or SEARCH is given, the file will be
  8217. opened in Emacs, unless an entry from org-file-apps that makes
  8218. use of groups in a regexp matches.
  8219. If the file does not exist, an error is thrown."
  8220. (let* ((file (if (equal path "")
  8221. buffer-file-name
  8222. (substitute-in-file-name (expand-file-name path))))
  8223. (file-apps (append org-file-apps (org-default-apps)))
  8224. (apps (org-remove-if
  8225. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8226. (apps-dlink (org-remove-if-not
  8227. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8228. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8229. (dirp (if remp nil (file-directory-p file)))
  8230. (file (if (and dirp org-open-directory-means-index-dot-org)
  8231. (concat (file-name-as-directory file) "index.org")
  8232. file))
  8233. (a-m-a-p (assq 'auto-mode apps))
  8234. (dfile (downcase file))
  8235. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8236. (link (cond ((and (eq line nil)
  8237. (eq search nil))
  8238. file)
  8239. (line
  8240. (concat file "::" (number-to-string line)))
  8241. (search
  8242. (concat file "::" search))))
  8243. (dlink (downcase link))
  8244. (old-buffer (current-buffer))
  8245. (old-pos (point))
  8246. (old-mode major-mode)
  8247. ext cmd link-match-data)
  8248. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8249. (setq ext (match-string 1 dfile))
  8250. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8251. (setq ext (match-string 1 dfile))))
  8252. (cond
  8253. ((member in-emacs '((16) system))
  8254. (setq cmd (cdr (assoc 'system apps))))
  8255. (in-emacs (setq cmd 'emacs))
  8256. (t
  8257. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8258. (and dirp (cdr (assoc 'directory apps)))
  8259. ; first, try matching against apps-dlink
  8260. ; if we get a match here, store the match data for later
  8261. (let ((match (assoc-default dlink apps-dlink
  8262. 'string-match)))
  8263. (if match
  8264. (progn (setq link-match-data (match-data))
  8265. match)
  8266. (progn (setq in-emacs (or in-emacs line search))
  8267. nil))) ; if we have no match in apps-dlink,
  8268. ; always open the file in emacs if line or search
  8269. ; is given (for backwards compatibility)
  8270. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8271. 'string-match)
  8272. (cdr (assoc ext apps))
  8273. (cdr (assoc t apps))))))
  8274. (when (eq cmd 'system)
  8275. (setq cmd (cdr (assoc 'system apps))))
  8276. (when (eq cmd 'default)
  8277. (setq cmd (cdr (assoc t apps))))
  8278. (when (eq cmd 'mailcap)
  8279. (require 'mailcap)
  8280. (mailcap-parse-mailcaps)
  8281. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8282. (command (mailcap-mime-info mime-type)))
  8283. (if (stringp command)
  8284. (setq cmd command)
  8285. (setq cmd 'emacs))))
  8286. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8287. (not (file-exists-p file))
  8288. (not org-open-non-existing-files))
  8289. (error "No such file: %s" file))
  8290. (cond
  8291. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8292. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8293. (while (string-match "['\"]%s['\"]" cmd)
  8294. (setq cmd (replace-match "%s" t t cmd)))
  8295. (while (string-match "%s" cmd)
  8296. (setq cmd (replace-match
  8297. (save-match-data
  8298. (shell-quote-argument
  8299. (convert-standard-filename file)))
  8300. t t cmd)))
  8301. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8302. (save-match-data
  8303. (let ((match-index 1)
  8304. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8305. (set-match-data link-match-data)
  8306. (while (<= match-index number-of-groups)
  8307. (let ((regex (concat "%" (number-to-string match-index)))
  8308. (replace-with (match-string match-index dlink)))
  8309. (while (string-match regex cmd)
  8310. (setq cmd (replace-match replace-with t t cmd))))
  8311. (setq match-index (+ match-index 1)))))
  8312. (save-window-excursion
  8313. (start-process-shell-command cmd nil cmd)
  8314. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8315. ))
  8316. ((or (stringp cmd)
  8317. (eq cmd 'emacs))
  8318. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8319. (widen)
  8320. (if line (org-goto-line line)
  8321. (if search (org-link-search search))))
  8322. ((consp cmd)
  8323. (let ((file (convert-standard-filename file)))
  8324. (save-match-data
  8325. (set-match-data link-match-data)
  8326. (eval cmd))))
  8327. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8328. (and (org-mode-p) (eq old-mode 'org-mode)
  8329. (or (not (equal old-buffer (current-buffer)))
  8330. (not (equal old-pos (point))))
  8331. (org-mark-ring-push old-pos old-buffer))))
  8332. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8333. "This function returns non-nil if `entry' uses a regular
  8334. expression which should be matched against the whole link by
  8335. org-open-file.
  8336. It assumes that is the case when the entry uses a regular
  8337. expression which has at least one grouping construct and the
  8338. action is either a lisp form or a command string containing
  8339. '%1', i.e. using at least one subexpression match as a
  8340. parameter."
  8341. (let ((selector (car entry))
  8342. (action (cdr entry)))
  8343. (if (stringp selector)
  8344. (and (> (regexp-opt-depth selector) 0)
  8345. (or (and (stringp action)
  8346. (string-match "%[0-9]" action))
  8347. (consp action)))
  8348. nil)))
  8349. (defun org-default-apps ()
  8350. "Return the default applications for this operating system."
  8351. (cond
  8352. ((eq system-type 'darwin)
  8353. org-file-apps-defaults-macosx)
  8354. ((eq system-type 'windows-nt)
  8355. org-file-apps-defaults-windowsnt)
  8356. (t org-file-apps-defaults-gnu)))
  8357. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8358. "Convert extensions to regular expressions in the cars of LIST.
  8359. Also, weed out any non-string entries, because the return value is used
  8360. only for regexp matching.
  8361. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8362. point to the symbol `emacs', indicating that the file should
  8363. be opened in Emacs."
  8364. (append
  8365. (delq nil
  8366. (mapcar (lambda (x)
  8367. (if (not (stringp (car x)))
  8368. nil
  8369. (if (string-match "\\W" (car x))
  8370. x
  8371. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8372. list))
  8373. (if add-auto-mode
  8374. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8375. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8376. (defun org-file-remote-p (file)
  8377. "Test whether FILE specifies a location on a remote system.
  8378. Return non-nil if the location is indeed remote.
  8379. For example, the filename \"/user@host:/foo\" specifies a location
  8380. on the system \"/user@host:\"."
  8381. (cond ((fboundp 'file-remote-p)
  8382. (file-remote-p file))
  8383. ((fboundp 'tramp-handle-file-remote-p)
  8384. (tramp-handle-file-remote-p file))
  8385. ((and (boundp 'ange-ftp-name-format)
  8386. (string-match (car ange-ftp-name-format) file))
  8387. t)
  8388. (t nil)))
  8389. ;;;; Refiling
  8390. (defun org-get-org-file ()
  8391. "Read a filename, with default directory `org-directory'."
  8392. (let ((default (or org-default-notes-file remember-data-file)))
  8393. (read-file-name (format "File name [%s]: " default)
  8394. (file-name-as-directory org-directory)
  8395. default)))
  8396. (defun org-notes-order-reversed-p ()
  8397. "Check if the current file should receive notes in reversed order."
  8398. (cond
  8399. ((not org-reverse-note-order) nil)
  8400. ((eq t org-reverse-note-order) t)
  8401. ((not (listp org-reverse-note-order)) nil)
  8402. (t (catch 'exit
  8403. (let ((all org-reverse-note-order)
  8404. entry)
  8405. (while (setq entry (pop all))
  8406. (if (string-match (car entry) buffer-file-name)
  8407. (throw 'exit (cdr entry))))
  8408. nil)))))
  8409. (defvar org-refile-target-table nil
  8410. "The list of refile targets, created by `org-refile'.")
  8411. (defvar org-agenda-new-buffers nil
  8412. "Buffers created to visit agenda files.")
  8413. (defun org-get-refile-targets (&optional default-buffer)
  8414. "Produce a table with refile targets."
  8415. (let ((case-fold-search nil)
  8416. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8417. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8418. targets txt re files f desc descre fast-path-p level pos0)
  8419. (message "Getting targets...")
  8420. (with-current-buffer (or default-buffer (current-buffer))
  8421. (while (setq entry (pop entries))
  8422. (setq files (car entry) desc (cdr entry))
  8423. (setq fast-path-p nil)
  8424. (cond
  8425. ((null files) (setq files (list (current-buffer))))
  8426. ((eq files 'org-agenda-files)
  8427. (setq files (org-agenda-files 'unrestricted)))
  8428. ((and (symbolp files) (fboundp files))
  8429. (setq files (funcall files)))
  8430. ((and (symbolp files) (boundp files))
  8431. (setq files (symbol-value files))))
  8432. (if (stringp files) (setq files (list files)))
  8433. (cond
  8434. ((eq (car desc) :tag)
  8435. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8436. ((eq (car desc) :todo)
  8437. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8438. ((eq (car desc) :regexp)
  8439. (setq descre (cdr desc)))
  8440. ((eq (car desc) :level)
  8441. (setq descre (concat "^\\*\\{" (number-to-string
  8442. (if org-odd-levels-only
  8443. (1- (* 2 (cdr desc)))
  8444. (cdr desc)))
  8445. "\\}[ \t]")))
  8446. ((eq (car desc) :maxlevel)
  8447. (setq fast-path-p t)
  8448. (setq descre (concat "^\\*\\{1," (number-to-string
  8449. (if org-odd-levels-only
  8450. (1- (* 2 (cdr desc)))
  8451. (cdr desc)))
  8452. "\\}[ \t]")))
  8453. (t (error "Bad refiling target description %s" desc)))
  8454. (while (setq f (pop files))
  8455. (with-current-buffer
  8456. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8457. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8458. (setq f (and f (expand-file-name f)))
  8459. (if (eq org-refile-use-outline-path 'file)
  8460. (push (list (file-name-nondirectory f) f nil nil) targets))
  8461. (save-excursion
  8462. (save-restriction
  8463. (widen)
  8464. (goto-char (point-min))
  8465. (while (re-search-forward descre nil t)
  8466. (goto-char (setq pos0 (point-at-bol)))
  8467. (catch 'next
  8468. (when org-refile-target-verify-function
  8469. (save-match-data
  8470. (or (funcall org-refile-target-verify-function)
  8471. (throw 'next t))))
  8472. (when (looking-at org-complex-heading-regexp)
  8473. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8474. txt (org-link-display-format (match-string 4))
  8475. re (concat "^" (regexp-quote
  8476. (buffer-substring (match-beginning 1)
  8477. (match-end 4)))))
  8478. (if (match-end 5) (setq re (concat re "[ \t]+"
  8479. (regexp-quote
  8480. (match-string 5)))))
  8481. (setq re (concat re "[ \t]*$"))
  8482. (when org-refile-use-outline-path
  8483. (setq txt (mapconcat 'org-protect-slash
  8484. (append
  8485. (if (eq org-refile-use-outline-path 'file)
  8486. (list (file-name-nondirectory
  8487. (buffer-file-name (buffer-base-buffer))))
  8488. (if (eq org-refile-use-outline-path 'full-file-path)
  8489. (list (buffer-file-name (buffer-base-buffer)))))
  8490. (org-get-outline-path fast-path-p level txt)
  8491. (list txt))
  8492. "/")))
  8493. (push (list txt f re (point)) targets)))
  8494. (when (= (point) pos0)
  8495. ;; verification function has not moved point
  8496. (goto-char (point-at-eol))))))))))
  8497. (message "Getting targets...done")
  8498. (nreverse targets)))
  8499. (defun org-protect-slash (s)
  8500. (while (string-match "/" s)
  8501. (setq s (replace-match "\\" t t s)))
  8502. s)
  8503. (defvar org-olpa (make-vector 20 nil))
  8504. (defun org-get-outline-path (&optional fastp level heading)
  8505. "Return the outline path to the current entry, as a list.
  8506. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8507. routine which makes outline path derivations for an entire file,
  8508. avoiding backtracing."
  8509. (if fastp
  8510. (progn
  8511. (if (> level 19)
  8512. (error "Outline path failure, more than 19 levels."))
  8513. (loop for i from level upto 19 do
  8514. (aset org-olpa i nil))
  8515. (prog1
  8516. (delq nil (append org-olpa nil))
  8517. (aset org-olpa level heading)))
  8518. (let (rtn case-fold-search)
  8519. (save-excursion
  8520. (save-restriction
  8521. (widen)
  8522. (while (org-up-heading-safe)
  8523. (when (looking-at org-complex-heading-regexp)
  8524. (push (org-match-string-no-properties 4) rtn)))
  8525. rtn)))))
  8526. (defun org-format-outline-path (path &optional width prefix)
  8527. "Format the outlie path PATH for display.
  8528. Width is the maximum number of characters that is available.
  8529. Prefix is a prefix to be included in the returned string,
  8530. such as the file name."
  8531. (setq width (or width 79))
  8532. (if prefix (setq width (- width (length prefix))))
  8533. (if (not path)
  8534. (or prefix "")
  8535. (let* ((nsteps (length path))
  8536. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8537. (maxwidth (if (<= total-width width)
  8538. 10000 ;; everything fits
  8539. ;; we need to shorten the level headings
  8540. (/ (- width nsteps) nsteps)))
  8541. (org-odd-levels-only nil)
  8542. (n 0)
  8543. (total (1+ (length prefix))))
  8544. (setq maxwidth (max maxwidth 10))
  8545. (concat prefix
  8546. (mapconcat
  8547. (lambda (h)
  8548. (setq n (1+ n))
  8549. (if (and (= n nsteps) (< maxwidth 10000))
  8550. (setq maxwidth (- total-width total)))
  8551. (if (< (length h) maxwidth)
  8552. (progn (setq total (+ total (length h) 1)) h)
  8553. (setq h (substring h 0 (- maxwidth 2))
  8554. total (+ total maxwidth 1))
  8555. (if (string-match "[ \t]+\\'" h)
  8556. (setq h (substring h 0 (match-beginning 0))))
  8557. (setq h (concat h "..")))
  8558. (org-add-props h nil 'face
  8559. (nth (% (1- n) org-n-level-faces)
  8560. org-level-faces))
  8561. h)
  8562. path "/")))))
  8563. (defun org-display-outline-path (&optional file current)
  8564. "Display the current outline path in the echo area."
  8565. (interactive "P")
  8566. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8567. (case-fold-search nil)
  8568. (path (and (org-mode-p) (org-get-outline-path))))
  8569. (if current (setq path (append path
  8570. (save-excursion
  8571. (org-back-to-heading t)
  8572. (if (looking-at org-complex-heading-regexp)
  8573. (list (match-string 4)))))))
  8574. (message "%s"
  8575. (org-format-outline-path
  8576. path
  8577. (1- (frame-width))
  8578. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8579. (defvar org-refile-history nil
  8580. "History for refiling operations.")
  8581. (defvar org-after-refile-insert-hook nil
  8582. "Hook run after `org-refile' has inserted its stuff at the new location.
  8583. Note that this is still *before* the stuff will be removed from
  8584. the *old* location.")
  8585. (defun org-refile (&optional goto default-buffer rfloc)
  8586. "Move the entry at point to another heading.
  8587. The list of target headings is compiled using the information in
  8588. `org-refile-targets', which see. This list is created before each use
  8589. and will therefore always be up-to-date.
  8590. At the target location, the entry is filed as a subitem of the target heading.
  8591. Depending on `org-reverse-note-order', the new subitem will either be the
  8592. first or the last subitem.
  8593. If there is an active region, all entries in that region will be moved.
  8594. However, the region must fulfil the requirement that the first heading
  8595. is the first one sets the top-level of the moved text - at most siblings
  8596. below it are allowed.
  8597. With prefix arg GOTO, the command will only visit the target location,
  8598. not actually move anything.
  8599. With a double prefix `C-u C-u', go to the location where the last refiling
  8600. operation has put the subtree.
  8601. With a prefix argument of `2', refile to the running clock.
  8602. RFLOC can be a refile location obtained in a different way.
  8603. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8604. (interactive "P")
  8605. (let* ((cbuf (current-buffer))
  8606. (regionp (org-region-active-p))
  8607. (region-start (and regionp (region-beginning)))
  8608. (region-end (and regionp (region-end)))
  8609. (region-length (and regionp (- region-end region-start)))
  8610. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8611. pos it nbuf file re level reversed)
  8612. (setq last-command nil)
  8613. (when regionp
  8614. (goto-char region-start)
  8615. (or (bolp) (goto-char (point-at-bol)))
  8616. (setq region-start (point))
  8617. (unless (org-kill-is-subtree-p
  8618. (buffer-substring region-start region-end))
  8619. (error "The region is not a (sequence of) subtree(s)")))
  8620. (if (equal goto '(16))
  8621. (org-refile-goto-last-stored)
  8622. (when (or
  8623. (and (equal goto 2)
  8624. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8625. (prog1
  8626. (setq it (list (or org-clock-heading "running clock")
  8627. (buffer-file-name
  8628. (marker-buffer org-clock-hd-marker))
  8629. ""
  8630. (marker-position org-clock-hd-marker)))
  8631. (setq goto nil)))
  8632. (setq it (or rfloc
  8633. (save-excursion
  8634. (org-refile-get-location
  8635. (if goto "Goto: " "Refile to: ") default-buffer
  8636. org-refile-allow-creating-parent-nodes)))))
  8637. (setq file (nth 1 it)
  8638. re (nth 2 it)
  8639. pos (nth 3 it))
  8640. (if (and (not goto)
  8641. pos
  8642. (equal (buffer-file-name) file)
  8643. (if regionp
  8644. (and (>= pos region-start)
  8645. (<= pos region-end))
  8646. (and (>= pos (point))
  8647. (< pos (save-excursion
  8648. (org-end-of-subtree t t))))))
  8649. (error "Cannot refile to position inside the tree or region"))
  8650. (setq nbuf (or (find-buffer-visiting file)
  8651. (find-file-noselect file)))
  8652. (if goto
  8653. (progn
  8654. (switch-to-buffer nbuf)
  8655. (goto-char pos)
  8656. (org-show-context 'org-goto))
  8657. (if regionp
  8658. (progn
  8659. (org-kill-new (buffer-substring region-start region-end))
  8660. (org-save-markers-in-region region-start region-end))
  8661. (org-copy-subtree 1 nil t))
  8662. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8663. (find-file-noselect file)))
  8664. (setq reversed (org-notes-order-reversed-p))
  8665. (save-excursion
  8666. (save-restriction
  8667. (widen)
  8668. (if pos
  8669. (progn
  8670. (goto-char pos)
  8671. (looking-at outline-regexp)
  8672. (setq level (org-get-valid-level (funcall outline-level) 1))
  8673. (goto-char
  8674. (if reversed
  8675. (or (outline-next-heading) (point-max))
  8676. (or (save-excursion (org-get-next-sibling))
  8677. (org-end-of-subtree t t)
  8678. (point-max)))))
  8679. (setq level 1)
  8680. (if (not reversed)
  8681. (goto-char (point-max))
  8682. (goto-char (point-min))
  8683. (or (outline-next-heading) (goto-char (point-max)))))
  8684. (if (not (bolp)) (newline))
  8685. (org-paste-subtree level)
  8686. (when org-log-refile
  8687. (org-add-log-setup 'refile nil nil 'findpos
  8688. org-log-refile)
  8689. (unless (eq org-log-refile 'note)
  8690. (save-excursion (org-add-log-note))))
  8691. (and org-auto-align-tags (org-set-tags nil t))
  8692. (bookmark-set "org-refile-last-stored")
  8693. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8694. (run-hooks 'org-after-refile-insert-hook))))
  8695. (if regionp
  8696. (delete-region (point) (+ (point) region-length))
  8697. (org-cut-subtree))
  8698. (when (featurep 'org-inlinetask)
  8699. (org-inlinetask-remove-END-maybe))
  8700. (setq org-markers-to-move nil)
  8701. (message "Refiled to \"%s\"" (car it))))))
  8702. (org-reveal))
  8703. (defun org-refile-goto-last-stored ()
  8704. "Go to the location where the last refile was stored."
  8705. (interactive)
  8706. (bookmark-jump "org-refile-last-stored")
  8707. (message "This is the location of the last refile"))
  8708. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8709. "Prompt the user for a refile location, using PROMPT."
  8710. (let ((org-refile-targets org-refile-targets)
  8711. (org-refile-use-outline-path org-refile-use-outline-path))
  8712. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8713. (unless org-refile-target-table
  8714. (error "No refile targets"))
  8715. (let* ((cbuf (current-buffer))
  8716. (partial-completion-mode nil)
  8717. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8718. (cfunc (if (and org-refile-use-outline-path
  8719. org-outline-path-complete-in-steps)
  8720. 'org-olpath-completing-read
  8721. 'org-icompleting-read))
  8722. (extra (if org-refile-use-outline-path "/" ""))
  8723. (filename (and cfn (expand-file-name cfn)))
  8724. (tbl (mapcar
  8725. (lambda (x)
  8726. (if (and (not (member org-refile-use-outline-path
  8727. '(file full-file-path)))
  8728. (not (equal filename (nth 1 x))))
  8729. (cons (concat (car x) extra " ("
  8730. (file-name-nondirectory (nth 1 x)) ")")
  8731. (cdr x))
  8732. (cons (concat (car x) extra) (cdr x))))
  8733. org-refile-target-table))
  8734. (completion-ignore-case t)
  8735. pa answ parent-target child parent old-hist)
  8736. (setq old-hist org-refile-history)
  8737. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8738. nil 'org-refile-history))
  8739. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8740. (if pa
  8741. (progn
  8742. (when (or (not org-refile-history)
  8743. (not (eq old-hist org-refile-history))
  8744. (not (equal (car pa) (car org-refile-history))))
  8745. (setq org-refile-history
  8746. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8747. org-refile-history
  8748. (cdr org-refile-history))))
  8749. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8750. (pop org-refile-history)))
  8751. pa)
  8752. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8753. (progn
  8754. (setq parent (match-string 1 answ)
  8755. child (match-string 2 answ))
  8756. (setq parent-target (or (assoc parent tbl)
  8757. (assoc (concat parent "/") tbl)))
  8758. (when (and parent-target
  8759. (or (eq new-nodes t)
  8760. (and (eq new-nodes 'confirm)
  8761. (y-or-n-p (format "Create new node \"%s\"? "
  8762. child)))))
  8763. (org-refile-new-child parent-target child)))
  8764. (error "Invalid target location")))))
  8765. (defun org-refile-new-child (parent-target child)
  8766. "Use refile target PARENT-TARGET to add new CHILD below it."
  8767. (unless parent-target
  8768. (error "Cannot find parent for new node"))
  8769. (let ((file (nth 1 parent-target))
  8770. (pos (nth 3 parent-target))
  8771. level)
  8772. (with-current-buffer (or (find-buffer-visiting file)
  8773. (find-file-noselect file))
  8774. (save-excursion
  8775. (save-restriction
  8776. (widen)
  8777. (if pos
  8778. (goto-char pos)
  8779. (goto-char (point-max))
  8780. (if (not (bolp)) (newline)))
  8781. (when (looking-at outline-regexp)
  8782. (setq level (funcall outline-level))
  8783. (org-end-of-subtree t t))
  8784. (org-back-over-empty-lines)
  8785. (insert "\n" (make-string
  8786. (if pos (org-get-valid-level level 1) 1) ?*)
  8787. " " child "\n")
  8788. (beginning-of-line 0)
  8789. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8790. (defun org-olpath-completing-read (prompt collection &rest args)
  8791. "Read an outline path like a file name."
  8792. (let ((thetable collection)
  8793. (org-completion-use-ido nil) ; does not work with ido.
  8794. (org-completion-use-iswitchb nil)) ; or iswitchb
  8795. (apply
  8796. 'org-icompleting-read prompt
  8797. (lambda (string predicate &optional flag)
  8798. (let (rtn r f (l (length string)))
  8799. (cond
  8800. ((eq flag nil)
  8801. ;; try completion
  8802. (try-completion string thetable))
  8803. ((eq flag t)
  8804. ;; all-completions
  8805. (setq rtn (all-completions string thetable predicate))
  8806. (mapcar
  8807. (lambda (x)
  8808. (setq r (substring x l))
  8809. (if (string-match " ([^)]*)$" x)
  8810. (setq f (match-string 0 x))
  8811. (setq f ""))
  8812. (if (string-match "/" r)
  8813. (concat string (substring r 0 (match-end 0)) f)
  8814. x))
  8815. rtn))
  8816. ((eq flag 'lambda)
  8817. ;; exact match?
  8818. (assoc string thetable)))
  8819. ))
  8820. args)))
  8821. ;;;; Dynamic blocks
  8822. (defun org-find-dblock (name)
  8823. "Find the first dynamic block with name NAME in the buffer.
  8824. If not found, stay at current position and return nil."
  8825. (let (pos)
  8826. (save-excursion
  8827. (goto-char (point-min))
  8828. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8829. nil t)
  8830. (match-beginning 0))))
  8831. (if pos (goto-char pos))
  8832. pos))
  8833. (defconst org-dblock-start-re
  8834. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8835. "Matches the start line of a dynamic block, with parameters.")
  8836. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8837. "Matches the end of a dynamic block.")
  8838. (defun org-create-dblock (plist)
  8839. "Create a dynamic block section, with parameters taken from PLIST.
  8840. PLIST must contain a :name entry which is used as name of the block."
  8841. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8842. (end-of-line 1)
  8843. (newline))
  8844. (let ((col (current-column))
  8845. (name (plist-get plist :name)))
  8846. (insert "#+BEGIN: " name)
  8847. (while plist
  8848. (if (eq (car plist) :name)
  8849. (setq plist (cddr plist))
  8850. (insert " " (prin1-to-string (pop plist)))))
  8851. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8852. (beginning-of-line -2)))
  8853. (defun org-prepare-dblock ()
  8854. "Prepare dynamic block for refresh.
  8855. This empties the block, puts the cursor at the insert position and returns
  8856. the property list including an extra property :name with the block name."
  8857. (unless (looking-at org-dblock-start-re)
  8858. (error "Not at a dynamic block"))
  8859. (let* ((begdel (1+ (match-end 0)))
  8860. (name (org-no-properties (match-string 1)))
  8861. (params (append (list :name name)
  8862. (read (concat "(" (match-string 3) ")")))))
  8863. (save-excursion
  8864. (beginning-of-line 1)
  8865. (skip-chars-forward " \t")
  8866. (setq params (plist-put params :indentation-column (current-column))))
  8867. (unless (re-search-forward org-dblock-end-re nil t)
  8868. (error "Dynamic block not terminated"))
  8869. (setq params
  8870. (append params
  8871. (list :content (buffer-substring
  8872. begdel (match-beginning 0)))))
  8873. (delete-region begdel (match-beginning 0))
  8874. (goto-char begdel)
  8875. (open-line 1)
  8876. params))
  8877. (defun org-map-dblocks (&optional command)
  8878. "Apply COMMAND to all dynamic blocks in the current buffer.
  8879. If COMMAND is not given, use `org-update-dblock'."
  8880. (let ((cmd (or command 'org-update-dblock)))
  8881. (save-excursion
  8882. (goto-char (point-min))
  8883. (while (re-search-forward org-dblock-start-re nil t)
  8884. (goto-char (match-beginning 0))
  8885. (save-excursion
  8886. (condition-case nil
  8887. (funcall cmd)
  8888. (error (message "Error during update of dynamic block"))))
  8889. (unless (re-search-forward org-dblock-end-re nil t)
  8890. (error "Dynamic block not terminated"))))))
  8891. (defun org-dblock-update (&optional arg)
  8892. "User command for updating dynamic blocks.
  8893. Update the dynamic block at point. With prefix ARG, update all dynamic
  8894. blocks in the buffer."
  8895. (interactive "P")
  8896. (if arg
  8897. (org-update-all-dblocks)
  8898. (or (looking-at org-dblock-start-re)
  8899. (org-beginning-of-dblock))
  8900. (org-update-dblock)))
  8901. (defun org-update-dblock ()
  8902. "Update the dynamic block at point
  8903. This means to empty the block, parse for parameters and then call
  8904. the correct writing function."
  8905. (save-window-excursion
  8906. (let* ((pos (point))
  8907. (line (org-current-line))
  8908. (params (org-prepare-dblock))
  8909. (name (plist-get params :name))
  8910. (indent (plist-get params :indentation-column))
  8911. (cmd (intern (concat "org-dblock-write:" name))))
  8912. (message "Updating dynamic block `%s' at line %d..." name line)
  8913. (funcall cmd params)
  8914. (message "Updating dynamic block `%s' at line %d...done" name line)
  8915. (goto-char pos)
  8916. (when (and indent (> indent 0))
  8917. (setq indent (make-string indent ?\ ))
  8918. (save-excursion
  8919. (org-beginning-of-dblock)
  8920. (forward-line 1)
  8921. (while (not (looking-at org-dblock-end-re))
  8922. (insert indent)
  8923. (beginning-of-line 2))
  8924. (when (looking-at org-dblock-end-re)
  8925. (and (looking-at "[ \t]+")
  8926. (replace-match ""))
  8927. (insert indent)))))))
  8928. (defun org-beginning-of-dblock ()
  8929. "Find the beginning of the dynamic block at point.
  8930. Error if there is no such block at point."
  8931. (let ((pos (point))
  8932. beg)
  8933. (end-of-line 1)
  8934. (if (and (re-search-backward org-dblock-start-re nil t)
  8935. (setq beg (match-beginning 0))
  8936. (re-search-forward org-dblock-end-re nil t)
  8937. (> (match-end 0) pos))
  8938. (goto-char beg)
  8939. (goto-char pos)
  8940. (error "Not in a dynamic block"))))
  8941. (defun org-update-all-dblocks ()
  8942. "Update all dynamic blocks in the buffer.
  8943. This function can be used in a hook."
  8944. (when (org-mode-p)
  8945. (org-map-dblocks 'org-update-dblock)))
  8946. ;;;; Completion
  8947. (defconst org-additional-option-like-keywords
  8948. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8949. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8950. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8951. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8952. "BEGIN:" "END:"
  8953. "ORGTBL" "TBLFM:" "TBLNAME:"
  8954. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8955. "BEGIN_QUOTE" "END_QUOTE"
  8956. "BEGIN_VERSE" "END_VERSE"
  8957. "BEGIN_CENTER" "END_CENTER"
  8958. "BEGIN_SRC" "END_SRC"
  8959. "CATEGORY" "COLUMNS"
  8960. "CAPTION" "LABEL"
  8961. "SETUPFILE"
  8962. "BIND"
  8963. "MACRO"))
  8964. (defcustom org-structure-template-alist
  8965. '(
  8966. ("s" "#+begin_src ?\n\n#+end_src"
  8967. "<src lang=\"?\">\n\n</src>")
  8968. ("e" "#+begin_example\n?\n#+end_example"
  8969. "<example>\n?\n</example>")
  8970. ("q" "#+begin_quote\n?\n#+end_quote"
  8971. "<quote>\n?\n</quote>")
  8972. ("v" "#+begin_verse\n?\n#+end_verse"
  8973. "<verse>\n?\n/verse>")
  8974. ("c" "#+begin_center\n?\n#+end_center"
  8975. "<center>\n?\n/center>")
  8976. ("l" "#+begin_latex\n?\n#+end_latex"
  8977. "<literal style=\"latex\">\n?\n</literal>")
  8978. ("L" "#+latex: "
  8979. "<literal style=\"latex\">?</literal>")
  8980. ("h" "#+begin_html\n?\n#+end_html"
  8981. "<literal style=\"html\">\n?\n</literal>")
  8982. ("H" "#+html: "
  8983. "<literal style=\"html\">?</literal>")
  8984. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8985. ("A" "#+ascii: ")
  8986. ("i" "#+include %file ?"
  8987. "<include file=%file markup=\"?\">")
  8988. )
  8989. "Structure completion elements.
  8990. This is a list of abbreviation keys and values. The value gets inserted
  8991. if you type `<' followed by the key and then press the completion key,
  8992. usually `M-TAB'. %file will be replaced by a file name after prompting
  8993. for the file using completion.
  8994. There are two templates for each key, the first uses the original Org syntax,
  8995. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8996. the default when the /org-mtags.el/ module has been loaded. See also the
  8997. variable `org-mtags-prefer-muse-templates'.
  8998. This is an experimental feature, it is undecided if it is going to stay in."
  8999. :group 'org-completion
  9000. :type '(repeat
  9001. (string :tag "Key")
  9002. (string :tag "Template")
  9003. (string :tag "Muse Template")))
  9004. (defun org-try-structure-completion ()
  9005. "Try to complete a structure template before point.
  9006. This looks for strings like \"<e\" on an otherwise empty line and
  9007. expands them."
  9008. (let ((l (buffer-substring (point-at-bol) (point)))
  9009. a)
  9010. (when (and (looking-at "[ \t]*$")
  9011. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9012. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9013. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9014. (match-beginning 1)) a)
  9015. t)))
  9016. (defun org-complete-expand-structure-template (start cell)
  9017. "Expand a structure template."
  9018. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9019. (rpl (nth (if musep 2 1) cell))
  9020. (ind ""))
  9021. (delete-region start (point))
  9022. (when (string-match "\\`#\\+" rpl)
  9023. (cond
  9024. ((bolp))
  9025. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9026. (setq ind (buffer-substring (point-at-bol) (point))))
  9027. (t (newline))))
  9028. (setq start (point))
  9029. (if (string-match "%file" rpl)
  9030. (setq rpl (replace-match
  9031. (concat
  9032. "\""
  9033. (save-match-data
  9034. (abbreviate-file-name (read-file-name "Include file: ")))
  9035. "\"")
  9036. t t rpl)))
  9037. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9038. (concat "\n" ind)))
  9039. (insert rpl)
  9040. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9041. (defun org-complete (&optional arg)
  9042. "Perform completion on word at point.
  9043. At the beginning of a headline, this completes TODO keywords as given in
  9044. `org-todo-keywords'.
  9045. If the current word is preceded by a backslash, completes the TeX symbols
  9046. that are supported for HTML support.
  9047. If the current word is preceded by \"#+\", completes special words for
  9048. setting file options.
  9049. In the line after \"#+STARTUP:, complete valid keywords.\"
  9050. At all other locations, this simply calls the value of
  9051. `org-completion-fallback-command'."
  9052. (interactive "P")
  9053. (org-without-partial-completion
  9054. (catch 'exit
  9055. (let* ((a nil)
  9056. (end (point))
  9057. (beg1 (save-excursion
  9058. (skip-chars-backward (org-re "[:alnum:]_@"))
  9059. (point)))
  9060. (beg (save-excursion
  9061. (skip-chars-backward "a-zA-Z0-9_:$")
  9062. (point)))
  9063. (confirm (lambda (x) (stringp (car x))))
  9064. (searchhead (equal (char-before beg) ?*))
  9065. (struct
  9066. (when (and (member (char-before beg1) '(?. ?<))
  9067. (setq a (assoc (buffer-substring beg1 (point))
  9068. org-structure-template-alist)))
  9069. (org-complete-expand-structure-template (1- beg1) a)
  9070. (throw 'exit t)))
  9071. (tag (and (equal (char-before beg1) ?:)
  9072. (equal (char-after (point-at-bol)) ?*)))
  9073. (prop (and (equal (char-before beg1) ?:)
  9074. (not (equal (char-after (point-at-bol)) ?*))))
  9075. (texp (equal (char-before beg) ?\\))
  9076. (link (equal (char-before beg) ?\[))
  9077. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9078. beg)
  9079. "#+"))
  9080. (startup (string-match "^#\\+STARTUP:.*"
  9081. (buffer-substring (point-at-bol) (point))))
  9082. (completion-ignore-case opt)
  9083. (type nil)
  9084. (tbl nil)
  9085. (table (cond
  9086. (opt
  9087. (setq type :opt)
  9088. (require 'org-exp)
  9089. (append
  9090. (delq nil
  9091. (mapcar
  9092. (lambda (x)
  9093. (if (string-match
  9094. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9095. (cons (match-string 2 x)
  9096. (match-string 1 x))))
  9097. (org-split-string (org-get-current-options) "\n")))
  9098. (mapcar 'list org-additional-option-like-keywords)))
  9099. (startup
  9100. (setq type :startup)
  9101. org-startup-options)
  9102. (link (append org-link-abbrev-alist-local
  9103. org-link-abbrev-alist))
  9104. (texp
  9105. (setq type :tex)
  9106. (append org-entities-user org-entities))
  9107. ((string-match "\\`\\*+[ \t]+\\'"
  9108. (buffer-substring (point-at-bol) beg))
  9109. (setq type :todo)
  9110. (mapcar 'list org-todo-keywords-1))
  9111. (searchhead
  9112. (setq type :searchhead)
  9113. (save-excursion
  9114. (goto-char (point-min))
  9115. (while (re-search-forward org-todo-line-regexp nil t)
  9116. (push (list
  9117. (org-make-org-heading-search-string
  9118. (match-string 3) t))
  9119. tbl)))
  9120. tbl)
  9121. (tag (setq type :tag beg beg1)
  9122. (or org-tag-alist (org-get-buffer-tags)))
  9123. (prop (setq type :prop beg beg1)
  9124. (mapcar 'list (org-buffer-property-keys nil t t)))
  9125. (t (progn
  9126. (call-interactively org-completion-fallback-command)
  9127. (throw 'exit nil)))))
  9128. (pattern (buffer-substring-no-properties beg end))
  9129. (completion (try-completion pattern table confirm)))
  9130. (cond ((eq completion t)
  9131. (if (not (assoc (upcase pattern) table))
  9132. (message "Already complete")
  9133. (if (and (equal type :opt)
  9134. (not (member (car (assoc (upcase pattern) table))
  9135. org-additional-option-like-keywords)))
  9136. (insert (substring (cdr (assoc (upcase pattern) table))
  9137. (length pattern)))
  9138. (if (memq type '(:tag :prop)) (insert ":")))))
  9139. ((null completion)
  9140. (message "Can't find completion for \"%s\"" pattern)
  9141. (ding))
  9142. ((not (string= pattern completion))
  9143. (delete-region beg end)
  9144. (if (string-match " +$" completion)
  9145. (setq completion (replace-match "" t t completion)))
  9146. (insert completion)
  9147. (if (get-buffer-window "*Completions*")
  9148. (delete-window (get-buffer-window "*Completions*")))
  9149. (if (assoc completion table)
  9150. (if (eq type :todo) (insert " ")
  9151. (if (memq type '(:tag :prop)) (insert ":"))))
  9152. (if (and (equal type :opt) (assoc completion table))
  9153. (message "%s" (substitute-command-keys
  9154. "Press \\[org-complete] again to insert example settings"))))
  9155. (t
  9156. (message "Making completion list...")
  9157. (let ((list (sort (all-completions pattern table confirm)
  9158. 'string<)))
  9159. (with-output-to-temp-buffer "*Completions*"
  9160. (condition-case nil
  9161. ;; Protection needed for XEmacs and emacs 21
  9162. (display-completion-list list pattern)
  9163. (error (display-completion-list list)))))
  9164. (message "Making completion list...%s" "done")))))))
  9165. ;;;; TODO, DEADLINE, Comments
  9166. (defun org-toggle-comment ()
  9167. "Change the COMMENT state of an entry."
  9168. (interactive)
  9169. (save-excursion
  9170. (org-back-to-heading)
  9171. (let (case-fold-search)
  9172. (if (looking-at (concat outline-regexp
  9173. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9174. (replace-match "" t t nil 1)
  9175. (if (looking-at outline-regexp)
  9176. (progn
  9177. (goto-char (match-end 0))
  9178. (insert org-comment-string " ")))))))
  9179. (defvar org-last-todo-state-is-todo nil
  9180. "This is non-nil when the last TODO state change led to a TODO state.
  9181. If the last change removed the TODO tag or switched to DONE, then
  9182. this is nil.")
  9183. (defvar org-setting-tags nil) ; dynamically skipped
  9184. (defun org-parse-local-options (string var)
  9185. "Parse STRING for startup setting relevant for variable VAR."
  9186. (let ((rtn (symbol-value var))
  9187. e opts)
  9188. (save-match-data
  9189. (if (or (not string) (not (string-match "\\S-" string)))
  9190. rtn
  9191. (setq opts (delq nil (mapcar (lambda (x)
  9192. (setq e (assoc x org-startup-options))
  9193. (if (eq (nth 1 e) var) e nil))
  9194. (org-split-string string "[ \t]+"))))
  9195. (if (not opts)
  9196. rtn
  9197. (setq rtn nil)
  9198. (while (setq e (pop opts))
  9199. (if (not (nth 3 e))
  9200. (setq rtn (nth 2 e))
  9201. (if (not (listp rtn)) (setq rtn nil))
  9202. (push (nth 2 e) rtn)))
  9203. rtn)))))
  9204. (defvar org-todo-setup-filter-hook nil
  9205. "Hook for functions that pre-filter todo specs.
  9206. Each function takes a todo spec and returns either `nil' or the spec
  9207. transformed into canonical form." )
  9208. (defvar org-todo-get-default-hook nil
  9209. "Hook for functions that get a default item for todo.
  9210. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9211. `nil' or a string to be used for the todo mark." )
  9212. (defvar org-agenda-headline-snapshot-before-repeat)
  9213. (defun org-todo (&optional arg)
  9214. "Change the TODO state of an item.
  9215. The state of an item is given by a keyword at the start of the heading,
  9216. like
  9217. *** TODO Write paper
  9218. *** DONE Call mom
  9219. The different keywords are specified in the variable `org-todo-keywords'.
  9220. By default the available states are \"TODO\" and \"DONE\".
  9221. So for this example: when the item starts with TODO, it is changed to DONE.
  9222. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9223. DONE are present, add TODO at the beginning of the heading.
  9224. With C-u prefix arg, use completion to determine the new state.
  9225. With numeric prefix arg, switch to that state.
  9226. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9227. With a triple C-u prefix, circumvent any state blocking.
  9228. For calling through lisp, arg is also interpreted in the following way:
  9229. 'none -> empty state
  9230. \"\"(empty string) -> switch to empty state
  9231. 'done -> switch to DONE
  9232. 'nextset -> switch to the next set of keywords
  9233. 'previousset -> switch to the previous set of keywords
  9234. \"WAITING\" -> switch to the specified keyword, but only if it
  9235. really is a member of `org-todo-keywords'."
  9236. (interactive "P")
  9237. (if (equal arg '(16)) (setq arg 'nextset))
  9238. (let ((org-blocker-hook org-blocker-hook)
  9239. (case-fold-search nil))
  9240. (when (equal arg '(64))
  9241. (setq arg nil org-blocker-hook nil))
  9242. (when (and org-blocker-hook
  9243. (or org-inhibit-blocking
  9244. (org-entry-get nil "NOBLOCKING")))
  9245. (setq org-blocker-hook nil))
  9246. (save-excursion
  9247. (catch 'exit
  9248. (org-back-to-heading t)
  9249. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9250. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9251. (looking-at " *"))
  9252. (let* ((match-data (match-data))
  9253. (startpos (point-at-bol))
  9254. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9255. (org-log-done org-log-done)
  9256. (org-log-repeat org-log-repeat)
  9257. (org-todo-log-states org-todo-log-states)
  9258. (this (match-string 1))
  9259. (hl-pos (match-beginning 0))
  9260. (head (org-get-todo-sequence-head this))
  9261. (ass (assoc head org-todo-kwd-alist))
  9262. (interpret (nth 1 ass))
  9263. (done-word (nth 3 ass))
  9264. (final-done-word (nth 4 ass))
  9265. (last-state (or this ""))
  9266. (completion-ignore-case t)
  9267. (member (member this org-todo-keywords-1))
  9268. (tail (cdr member))
  9269. (state (cond
  9270. ((and org-todo-key-trigger
  9271. (or (and (equal arg '(4))
  9272. (eq org-use-fast-todo-selection 'prefix))
  9273. (and (not arg) org-use-fast-todo-selection
  9274. (not (eq org-use-fast-todo-selection
  9275. 'prefix)))))
  9276. ;; Use fast selection
  9277. (org-fast-todo-selection))
  9278. ((and (equal arg '(4))
  9279. (or (not org-use-fast-todo-selection)
  9280. (not org-todo-key-trigger)))
  9281. ;; Read a state with completion
  9282. (org-icompleting-read
  9283. "State: " (mapcar (lambda(x) (list x))
  9284. org-todo-keywords-1)
  9285. nil t))
  9286. ((eq arg 'right)
  9287. (if this
  9288. (if tail (car tail) nil)
  9289. (car org-todo-keywords-1)))
  9290. ((eq arg 'left)
  9291. (if (equal member org-todo-keywords-1)
  9292. nil
  9293. (if this
  9294. (nth (- (length org-todo-keywords-1)
  9295. (length tail) 2)
  9296. org-todo-keywords-1)
  9297. (org-last org-todo-keywords-1))))
  9298. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9299. (setq arg nil))) ; hack to fall back to cycling
  9300. (arg
  9301. ;; user or caller requests a specific state
  9302. (cond
  9303. ((equal arg "") nil)
  9304. ((eq arg 'none) nil)
  9305. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9306. ((eq arg 'nextset)
  9307. (or (car (cdr (member head org-todo-heads)))
  9308. (car org-todo-heads)))
  9309. ((eq arg 'previousset)
  9310. (let ((org-todo-heads (reverse org-todo-heads)))
  9311. (or (car (cdr (member head org-todo-heads)))
  9312. (car org-todo-heads))))
  9313. ((car (member arg org-todo-keywords-1)))
  9314. ((stringp arg)
  9315. (error "State `%s' not valid in this file" arg))
  9316. ((nth (1- (prefix-numeric-value arg))
  9317. org-todo-keywords-1))))
  9318. ((null member) (or head (car org-todo-keywords-1)))
  9319. ((equal this final-done-word) nil) ;; -> make empty
  9320. ((null tail) nil) ;; -> first entry
  9321. ((memq interpret '(type priority))
  9322. (if (eq this-command last-command)
  9323. (car tail)
  9324. (if (> (length tail) 0)
  9325. (or done-word (car org-done-keywords))
  9326. nil)))
  9327. (t
  9328. (car tail))))
  9329. (state (or
  9330. (run-hook-with-args-until-success
  9331. 'org-todo-get-default-hook state last-state)
  9332. state))
  9333. (next (if state (concat " " state " ") " "))
  9334. (change-plist (list :type 'todo-state-change :from this :to state
  9335. :position startpos))
  9336. dolog now-done-p)
  9337. (when org-blocker-hook
  9338. (setq org-last-todo-state-is-todo
  9339. (not (member this org-done-keywords)))
  9340. (unless (save-excursion
  9341. (save-match-data
  9342. (run-hook-with-args-until-failure
  9343. 'org-blocker-hook change-plist)))
  9344. (if (interactive-p)
  9345. (error "TODO state change from %s to %s blocked" this state)
  9346. ;; fail silently
  9347. (message "TODO state change from %s to %s blocked" this state)
  9348. (throw 'exit nil))))
  9349. (store-match-data match-data)
  9350. (replace-match next t t)
  9351. (unless (pos-visible-in-window-p hl-pos)
  9352. (message "TODO state changed to %s" (org-trim next)))
  9353. (unless head
  9354. (setq head (org-get-todo-sequence-head state)
  9355. ass (assoc head org-todo-kwd-alist)
  9356. interpret (nth 1 ass)
  9357. done-word (nth 3 ass)
  9358. final-done-word (nth 4 ass)))
  9359. (when (memq arg '(nextset previousset))
  9360. (message "Keyword-Set %d/%d: %s"
  9361. (- (length org-todo-sets) -1
  9362. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9363. (length org-todo-sets)
  9364. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9365. (setq org-last-todo-state-is-todo
  9366. (not (member state org-done-keywords)))
  9367. (setq now-done-p (and (member state org-done-keywords)
  9368. (not (member this org-done-keywords))))
  9369. (and logging (org-local-logging logging))
  9370. (when (and (or org-todo-log-states org-log-done)
  9371. (not (eq org-inhibit-logging t))
  9372. (not (memq arg '(nextset previousset))))
  9373. ;; we need to look at recording a time and note
  9374. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9375. (nth 2 (assoc this org-todo-log-states))))
  9376. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9377. (setq dolog 'time))
  9378. (when (and state
  9379. (member state org-not-done-keywords)
  9380. (not (member this org-not-done-keywords)))
  9381. ;; This is now a todo state and was not one before
  9382. ;; If there was a CLOSED time stamp, get rid of it.
  9383. (org-add-planning-info nil nil 'closed))
  9384. (when (and now-done-p org-log-done)
  9385. ;; It is now done, and it was not done before
  9386. (org-add-planning-info 'closed (org-current-time))
  9387. (if (and (not dolog) (eq 'note org-log-done))
  9388. (org-add-log-setup 'done state this 'findpos 'note)))
  9389. (when (and state dolog)
  9390. ;; This is a non-nil state, and we need to log it
  9391. (org-add-log-setup 'state state this 'findpos dolog)))
  9392. ;; Fixup tag positioning
  9393. (org-todo-trigger-tag-changes state)
  9394. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9395. (when org-provide-todo-statistics
  9396. (org-update-parent-todo-statistics))
  9397. (run-hooks 'org-after-todo-state-change-hook)
  9398. (if (and arg (not (member state org-done-keywords)))
  9399. (setq head (org-get-todo-sequence-head state)))
  9400. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9401. ;; Do we need to trigger a repeat?
  9402. (when now-done-p
  9403. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9404. ;; This is for the agenda, take a snapshot of the headline.
  9405. (save-match-data
  9406. (setq org-agenda-headline-snapshot-before-repeat
  9407. (org-get-heading))))
  9408. (org-auto-repeat-maybe state))
  9409. ;; Fixup cursor location if close to the keyword
  9410. (if (and (outline-on-heading-p)
  9411. (not (bolp))
  9412. (save-excursion (beginning-of-line 1)
  9413. (looking-at org-todo-line-regexp))
  9414. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9415. (progn
  9416. (goto-char (or (match-end 2) (match-end 1)))
  9417. (and (looking-at " ") (just-one-space))))
  9418. (when org-trigger-hook
  9419. (save-excursion
  9420. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9421. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9422. "Block turning an entry into a TODO, using the hierarchy.
  9423. This checks whether the current task should be blocked from state
  9424. changes. Such blocking occurs when:
  9425. 1. The task has children which are not all in a completed state.
  9426. 2. A task has a parent with the property :ORDERED:, and there
  9427. are siblings prior to the current task with incomplete
  9428. status.
  9429. 3. The parent of the task is blocked because it has siblings that should
  9430. be done first, or is child of a block grandparent TODO entry."
  9431. (if (not org-enforce-todo-dependencies)
  9432. t ; if locally turned off don't block
  9433. (catch 'dont-block
  9434. ;; If this is not a todo state change, or if this entry is already DONE,
  9435. ;; do not block
  9436. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9437. (member (plist-get change-plist :from)
  9438. (cons 'done org-done-keywords))
  9439. (member (plist-get change-plist :to)
  9440. (cons 'todo org-not-done-keywords))
  9441. (not (plist-get change-plist :to)))
  9442. (throw 'dont-block t))
  9443. ;; If this task has children, and any are undone, it's blocked
  9444. (save-excursion
  9445. (org-back-to-heading t)
  9446. (let ((this-level (funcall outline-level)))
  9447. (outline-next-heading)
  9448. (let ((child-level (funcall outline-level)))
  9449. (while (and (not (eobp))
  9450. (> child-level this-level))
  9451. ;; this todo has children, check whether they are all
  9452. ;; completed
  9453. (if (and (not (org-entry-is-done-p))
  9454. (org-entry-is-todo-p))
  9455. (throw 'dont-block nil))
  9456. (outline-next-heading)
  9457. (setq child-level (funcall outline-level))))))
  9458. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9459. ;; any previous siblings are undone, it's blocked
  9460. (save-excursion
  9461. (org-back-to-heading t)
  9462. (let* ((pos (point))
  9463. (parent-pos (and (org-up-heading-safe) (point))))
  9464. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9465. (when (and (org-entry-get (point) "ORDERED")
  9466. (forward-line 1)
  9467. (re-search-forward org-not-done-heading-regexp pos t))
  9468. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9469. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9470. (while t
  9471. (goto-char parent-pos)
  9472. (if (not (looking-at org-not-done-heading-regexp))
  9473. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9474. (setq pos (point))
  9475. (setq parent-pos (and (org-up-heading-safe) (point)))
  9476. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9477. (when (and (org-entry-get (point) "ORDERED")
  9478. (forward-line 1)
  9479. (re-search-forward org-not-done-heading-regexp pos t))
  9480. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9481. (defcustom org-track-ordered-property-with-tag nil
  9482. "Should the ORDERED property also be shown as a tag?
  9483. The ORDERED property decides if an entry should require subtasks to be
  9484. completed in sequence. Since a property is not very visible, setting
  9485. this option means that toggling the ORDERED property with the command
  9486. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9487. not relevant for the behavior, but it makes things more visible.
  9488. Note that toggling the tag with tags commands will not change the property
  9489. and therefore not influence behavior!
  9490. This can be t, meaning the tag ORDERED should be used, It can also be a
  9491. string to select a different tag for this task."
  9492. :group 'org-todo
  9493. :type '(choice
  9494. (const :tag "No tracking" nil)
  9495. (const :tag "Track with ORDERED tag" t)
  9496. (string :tag "Use other tag")))
  9497. (defun org-toggle-ordered-property ()
  9498. "Toggle the ORDERED property of the current entry.
  9499. For better visibility, you can track the value of this property with a tag.
  9500. See variable `org-track-ordered-property-with-tag'."
  9501. (interactive)
  9502. (let* ((t1 org-track-ordered-property-with-tag)
  9503. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9504. (save-excursion
  9505. (org-back-to-heading)
  9506. (if (org-entry-get nil "ORDERED")
  9507. (progn
  9508. (org-delete-property "ORDERED")
  9509. (and tag (org-toggle-tag tag 'off))
  9510. (message "Subtasks can be completed in arbitrary order"))
  9511. (org-entry-put nil "ORDERED" "t")
  9512. (and tag (org-toggle-tag tag 'on))
  9513. (message "Subtasks must be completed in sequence")))))
  9514. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9515. (defun org-block-todo-from-checkboxes (change-plist)
  9516. "Block turning an entry into a TODO, using checkboxes.
  9517. This checks whether the current task should be blocked from state
  9518. changes because there are unchecked boxes in this entry."
  9519. (if (not org-enforce-todo-checkbox-dependencies)
  9520. t ; if locally turned off don't block
  9521. (catch 'dont-block
  9522. ;; If this is not a todo state change, or if this entry is already DONE,
  9523. ;; do not block
  9524. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9525. (member (plist-get change-plist :from)
  9526. (cons 'done org-done-keywords))
  9527. (member (plist-get change-plist :to)
  9528. (cons 'todo org-not-done-keywords))
  9529. (not (plist-get change-plist :to)))
  9530. (throw 'dont-block t))
  9531. ;; If this task has checkboxes that are not checked, it's blocked
  9532. (save-excursion
  9533. (org-back-to-heading t)
  9534. (let ((beg (point)) end)
  9535. (outline-next-heading)
  9536. (setq end (point))
  9537. (goto-char beg)
  9538. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9539. end t)
  9540. (progn
  9541. (if (boundp 'org-blocked-by-checkboxes)
  9542. (setq org-blocked-by-checkboxes t))
  9543. (throw 'dont-block nil)))))
  9544. t))) ; do not block
  9545. (defun org-entry-blocked-p ()
  9546. "Is the current entry blocked?"
  9547. (if (org-entry-get nil "NOBLOCKING")
  9548. nil ;; Never block this entry
  9549. (not
  9550. (run-hook-with-args-until-failure
  9551. 'org-blocker-hook
  9552. (list :type 'todo-state-change
  9553. :position (point)
  9554. :from 'todo
  9555. :to 'done)))))
  9556. (defun org-update-statistics-cookies (all)
  9557. "Update the statistics cookie, either from TODO or from checkboxes.
  9558. This should be called with the cursor in a line with a statistics cookie."
  9559. (interactive "P")
  9560. (if all
  9561. (progn
  9562. (org-update-checkbox-count 'all)
  9563. (org-map-entries 'org-update-parent-todo-statistics))
  9564. (if (not (org-on-heading-p))
  9565. (org-update-checkbox-count)
  9566. (let ((pos (move-marker (make-marker) (point)))
  9567. end l1 l2)
  9568. (ignore-errors (org-back-to-heading t))
  9569. (if (not (org-on-heading-p))
  9570. (org-update-checkbox-count)
  9571. (setq l1 (org-outline-level))
  9572. (setq end (save-excursion
  9573. (outline-next-heading)
  9574. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9575. (point)))
  9576. (if (and (save-excursion
  9577. (re-search-forward
  9578. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9579. (not (save-excursion (re-search-forward
  9580. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9581. (org-update-checkbox-count)
  9582. (if (and l2 (> l2 l1))
  9583. (progn
  9584. (goto-char end)
  9585. (org-update-parent-todo-statistics))
  9586. (goto-char pos)
  9587. (beginning-of-line 1)
  9588. (while (re-search-forward
  9589. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9590. (point-at-eol) t)
  9591. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9592. (goto-char pos)
  9593. (move-marker pos nil)))))
  9594. (defvar org-entry-property-inherited-from) ;; defined below
  9595. (defun org-update-parent-todo-statistics ()
  9596. "Update any statistics cookie in the parent of the current headline.
  9597. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9598. the entire subtree and this will travel up the hierarchy and update
  9599. statistics everywhere."
  9600. (interactive)
  9601. (let* ((lim 0) prop
  9602. (recursive (or (not org-hierarchical-todo-statistics)
  9603. (string-match
  9604. "\\<recursive\\>"
  9605. (or (setq prop (org-entry-get
  9606. nil "COOKIE_DATA" 'inherit)) ""))))
  9607. (lim (or (and prop (marker-position
  9608. org-entry-property-inherited-from))
  9609. lim))
  9610. (first t)
  9611. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9612. level ltoggle l1 new ndel
  9613. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9614. (catch 'exit
  9615. (save-excursion
  9616. (beginning-of-line 1)
  9617. (if (org-at-heading-p)
  9618. (setq ltoggle (funcall outline-level))
  9619. (error "This should not happen"))
  9620. (while (and (setq level (org-up-heading-safe))
  9621. (or recursive first)
  9622. (>= (point) lim))
  9623. (setq first nil cookie-present nil)
  9624. (unless (and level
  9625. (not (string-match
  9626. "\\<checkbox\\>"
  9627. (downcase
  9628. (or (org-entry-get
  9629. nil "COOKIE_DATA")
  9630. "")))))
  9631. (throw 'exit nil))
  9632. (while (re-search-forward box-re (point-at-eol) t)
  9633. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9634. (setq is-percent (match-end 2))
  9635. (save-match-data
  9636. (unless (outline-next-heading) (throw 'exit nil))
  9637. (while (and (looking-at org-complex-heading-regexp)
  9638. (> (setq l1 (length (match-string 1))) level))
  9639. (setq kwd (and (or recursive (= l1 ltoggle))
  9640. (match-string 2)))
  9641. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9642. (and (listp org-provide-todo-statistics)
  9643. (or (member kwd org-provide-todo-statistics)
  9644. (member kwd org-done-keywords))))
  9645. (setq cnt-all (1+ cnt-all))
  9646. (if (eq org-provide-todo-statistics t)
  9647. (and kwd (setq cnt-all (1+ cnt-all)))))
  9648. (and (member kwd org-done-keywords)
  9649. (setq cnt-done (1+ cnt-done)))
  9650. (outline-next-heading)))
  9651. (setq new
  9652. (if is-percent
  9653. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9654. (format "[%d/%d]" cnt-done cnt-all))
  9655. ndel (- (match-end 0) (match-beginning 0)))
  9656. (goto-char (match-beginning 0))
  9657. (insert new)
  9658. (delete-region (point) (+ (point) ndel)))
  9659. (when cookie-present
  9660. (run-hook-with-args 'org-after-todo-statistics-hook
  9661. cnt-done (- cnt-all cnt-done))))))
  9662. (run-hooks 'org-todo-statistics-hook)))
  9663. (defvar org-after-todo-statistics-hook nil
  9664. "Hook that is called after a TODO statistics cookie has been updated.
  9665. Each function is called with two arguments: the number of not-done entries
  9666. and the number of done entries.
  9667. For example, the following function, when added to this hook, will switch
  9668. an entry to DONE when all children are done, and back to TODO when new
  9669. entries are set to a TODO status. Note that this hook is only called
  9670. when there is a statistics cookie in the headline!
  9671. (defun org-summary-todo (n-done n-not-done)
  9672. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9673. (let (org-log-done org-log-states) ; turn off logging
  9674. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9675. ")
  9676. (defvar org-todo-statistics-hook nil
  9677. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9678. This hook runs even if there is no statistics cookie present, in which case
  9679. `org-after-todo-statistics-hook' would not run.")
  9680. (defun org-todo-trigger-tag-changes (state)
  9681. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9682. (let ((l org-todo-state-tags-triggers)
  9683. changes)
  9684. (when (or (not state) (equal state ""))
  9685. (setq changes (append changes (cdr (assoc "" l)))))
  9686. (when (and (stringp state) (> (length state) 0))
  9687. (setq changes (append changes (cdr (assoc state l)))))
  9688. (when (member state org-not-done-keywords)
  9689. (setq changes (append changes (cdr (assoc 'todo l)))))
  9690. (when (member state org-done-keywords)
  9691. (setq changes (append changes (cdr (assoc 'done l)))))
  9692. (dolist (c changes)
  9693. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9694. (defun org-local-logging (value)
  9695. "Get logging settings from a property VALUE."
  9696. (let* (words w a)
  9697. ;; directly set the variables, they are already local.
  9698. (setq org-log-done nil
  9699. org-log-repeat nil
  9700. org-todo-log-states nil)
  9701. (setq words (org-split-string value))
  9702. (while (setq w (pop words))
  9703. (cond
  9704. ((setq a (assoc w org-startup-options))
  9705. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9706. (set (nth 1 a) (nth 2 a))))
  9707. ((setq a (org-extract-log-state-settings w))
  9708. (and (member (car a) org-todo-keywords-1)
  9709. (push a org-todo-log-states)))))))
  9710. (defun org-get-todo-sequence-head (kwd)
  9711. "Return the head of the TODO sequence to which KWD belongs.
  9712. If KWD is not set, check if there is a text property remembering the
  9713. right sequence."
  9714. (let (p)
  9715. (cond
  9716. ((not kwd)
  9717. (or (get-text-property (point-at-bol) 'org-todo-head)
  9718. (progn
  9719. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9720. nil (point-at-eol)))
  9721. (get-text-property p 'org-todo-head))))
  9722. ((not (member kwd org-todo-keywords-1))
  9723. (car org-todo-keywords-1))
  9724. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9725. (defun org-fast-todo-selection ()
  9726. "Fast TODO keyword selection with single keys.
  9727. Returns the new TODO keyword, or nil if no state change should occur."
  9728. (let* ((fulltable org-todo-key-alist)
  9729. (done-keywords org-done-keywords) ;; needed for the faces.
  9730. (maxlen (apply 'max (mapcar
  9731. (lambda (x)
  9732. (if (stringp (car x)) (string-width (car x)) 0))
  9733. fulltable)))
  9734. (expert nil)
  9735. (fwidth (+ maxlen 3 1 3))
  9736. (ncol (/ (- (window-width) 4) fwidth))
  9737. tg cnt e c tbl
  9738. groups ingroup)
  9739. (save-excursion
  9740. (save-window-excursion
  9741. (if expert
  9742. (set-buffer (get-buffer-create " *Org todo*"))
  9743. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9744. (erase-buffer)
  9745. (org-set-local 'org-done-keywords done-keywords)
  9746. (setq tbl fulltable cnt 0)
  9747. (while (setq e (pop tbl))
  9748. (cond
  9749. ((equal e '(:startgroup))
  9750. (push '() groups) (setq ingroup t)
  9751. (when (not (= cnt 0))
  9752. (setq cnt 0)
  9753. (insert "\n"))
  9754. (insert "{ "))
  9755. ((equal e '(:endgroup))
  9756. (setq ingroup nil cnt 0)
  9757. (insert "}\n"))
  9758. ((equal e '(:newline))
  9759. (when (not (= cnt 0))
  9760. (setq cnt 0)
  9761. (insert "\n")
  9762. (setq e (car tbl))
  9763. (while (equal (car tbl) '(:newline))
  9764. (insert "\n")
  9765. (setq tbl (cdr tbl)))))
  9766. (t
  9767. (setq tg (car e) c (cdr e))
  9768. (if ingroup (push tg (car groups)))
  9769. (setq tg (org-add-props tg nil 'face
  9770. (org-get-todo-face tg)))
  9771. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9772. (insert "[" c "] " tg (make-string
  9773. (- fwidth 4 (length tg)) ?\ ))
  9774. (when (= (setq cnt (1+ cnt)) ncol)
  9775. (insert "\n")
  9776. (if ingroup (insert " "))
  9777. (setq cnt 0)))))
  9778. (insert "\n")
  9779. (goto-char (point-min))
  9780. (if (not expert) (org-fit-window-to-buffer))
  9781. (message "[a-z..]:Set [SPC]:clear")
  9782. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9783. (cond
  9784. ((or (= c ?\C-g)
  9785. (and (= c ?q) (not (rassoc c fulltable))))
  9786. (setq quit-flag t))
  9787. ((= c ?\ ) nil)
  9788. ((setq e (rassoc c fulltable) tg (car e))
  9789. tg)
  9790. (t (setq quit-flag t)))))))
  9791. (defun org-entry-is-todo-p ()
  9792. (member (org-get-todo-state) org-not-done-keywords))
  9793. (defun org-entry-is-done-p ()
  9794. (member (org-get-todo-state) org-done-keywords))
  9795. (defun org-get-todo-state ()
  9796. (save-excursion
  9797. (org-back-to-heading t)
  9798. (and (looking-at org-todo-line-regexp)
  9799. (match-end 2)
  9800. (match-string 2))))
  9801. (defun org-at-date-range-p (&optional inactive-ok)
  9802. "Is the cursor inside a date range?"
  9803. (interactive)
  9804. (save-excursion
  9805. (catch 'exit
  9806. (let ((pos (point)))
  9807. (skip-chars-backward "^[<\r\n")
  9808. (skip-chars-backward "<[")
  9809. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9810. (>= (match-end 0) pos)
  9811. (throw 'exit t))
  9812. (skip-chars-backward "^<[\r\n")
  9813. (skip-chars-backward "<[")
  9814. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9815. (>= (match-end 0) pos)
  9816. (throw 'exit t)))
  9817. nil)))
  9818. (defun org-get-repeat (&optional tagline)
  9819. "Check if there is a deadline/schedule with repeater in this entry."
  9820. (save-match-data
  9821. (save-excursion
  9822. (org-back-to-heading t)
  9823. (and (re-search-forward (if tagline
  9824. (concat tagline "\\s-*" org-repeat-re)
  9825. org-repeat-re)
  9826. (org-entry-end-position) t)
  9827. (match-string-no-properties 1)))))
  9828. (defvar org-last-changed-timestamp)
  9829. (defvar org-last-inserted-timestamp)
  9830. (defvar org-log-post-message)
  9831. (defvar org-log-note-purpose)
  9832. (defvar org-log-note-how)
  9833. (defvar org-log-note-extra)
  9834. (defun org-auto-repeat-maybe (done-word)
  9835. "Check if the current headline contains a repeated deadline/schedule.
  9836. If yes, set TODO state back to what it was and change the base date
  9837. of repeating deadline/scheduled time stamps to new date.
  9838. This function is run automatically after each state change to a DONE state."
  9839. ;; last-state is dynamically scoped into this function
  9840. (let* ((repeat (org-get-repeat))
  9841. (aa (assoc last-state org-todo-kwd-alist))
  9842. (interpret (nth 1 aa))
  9843. (head (nth 2 aa))
  9844. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9845. (msg "Entry repeats: ")
  9846. (org-log-done nil)
  9847. (org-todo-log-states nil)
  9848. (nshiftmax 10) (nshift 0)
  9849. re type n what ts time to-state)
  9850. (when repeat
  9851. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9852. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  9853. org-todo-repeat-to-state))
  9854. (unless (and to-state (member to-state org-todo-keywords-1))
  9855. (setq to-state (if (eq interpret 'type) last-state head)))
  9856. (org-todo to-state)
  9857. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  9858. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9859. (org-time-stamp-format t t))))
  9860. (when org-log-repeat
  9861. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9862. (memq 'org-add-log-note post-command-hook))
  9863. ;; OK, we are already setup for some record
  9864. (if (eq org-log-repeat 'note)
  9865. ;; make sure we take a note, not only a time stamp
  9866. (setq org-log-note-how 'note))
  9867. ;; Set up for taking a record
  9868. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9869. last-state
  9870. 'findpos org-log-repeat)))
  9871. (org-back-to-heading t)
  9872. (org-add-planning-info nil nil 'closed)
  9873. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9874. org-deadline-time-regexp "\\)\\|\\("
  9875. org-ts-regexp "\\)"))
  9876. (while (re-search-forward
  9877. re (save-excursion (outline-next-heading) (point)) t)
  9878. (setq type (if (match-end 1) org-scheduled-string
  9879. (if (match-end 3) org-deadline-string "Plain:"))
  9880. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9881. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9882. (setq n (string-to-number (match-string 2 ts))
  9883. what (match-string 3 ts))
  9884. (if (equal what "w") (setq n (* n 7) what "d"))
  9885. ;; Preparation, see if we need to modify the start date for the change
  9886. (when (match-end 1)
  9887. (setq time (save-match-data (org-time-string-to-time ts)))
  9888. (cond
  9889. ((equal (match-string 1 ts) ".")
  9890. ;; Shift starting date to today
  9891. (org-timestamp-change
  9892. (- (time-to-days (current-time)) (time-to-days time))
  9893. 'day))
  9894. ((equal (match-string 1 ts) "+")
  9895. (while (or (= nshift 0)
  9896. (<= (time-to-days time) (time-to-days (current-time))))
  9897. (when (= (incf nshift) nshiftmax)
  9898. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9899. (error "Abort")))
  9900. (org-timestamp-change n (cdr (assoc what whata)))
  9901. (org-at-timestamp-p t)
  9902. (setq ts (match-string 1))
  9903. (setq time (save-match-data (org-time-string-to-time ts))))
  9904. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9905. ;; rematch, so that we have everything in place for the real shift
  9906. (org-at-timestamp-p t)
  9907. (setq ts (match-string 1))
  9908. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9909. (org-timestamp-change n (cdr (assoc what whata)))
  9910. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9911. (setq org-log-post-message msg)
  9912. (message "%s" msg))))
  9913. (defun org-show-todo-tree (arg)
  9914. "Make a compact tree which shows all headlines marked with TODO.
  9915. The tree will show the lines where the regexp matches, and all higher
  9916. headlines above the match.
  9917. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9918. With a numeric prefix N, construct a sparse tree for the Nth element
  9919. of `org-todo-keywords-1'."
  9920. (interactive "P")
  9921. (let ((case-fold-search nil)
  9922. (kwd-re
  9923. (cond ((null arg) org-not-done-regexp)
  9924. ((equal arg '(4))
  9925. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9926. (mapcar 'list org-todo-keywords-1))))
  9927. (concat "\\("
  9928. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9929. "\\)\\>")))
  9930. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9931. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9932. org-todo-keywords-1)))
  9933. (t (error "Invalid prefix argument: %s" arg)))))
  9934. (message "%d TODO entries found"
  9935. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9936. (defun org-deadline (&optional remove time)
  9937. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9938. With argument REMOVE, remove any deadline from the item.
  9939. When TIME is set, it should be an internal time specification, and the
  9940. scheduling will use the corresponding date."
  9941. (interactive "P")
  9942. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9943. (repeater (and old-date
  9944. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9945. (match-string 1 old-date))))
  9946. (if remove
  9947. (progn
  9948. (when (and old-date org-log-redeadline)
  9949. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9950. org-log-redeadline))
  9951. (org-remove-timestamp-with-keyword org-deadline-string)
  9952. (message "Item no longer has a deadline."))
  9953. (org-add-planning-info 'deadline time 'closed)
  9954. (when (and old-date org-log-redeadline
  9955. (not (equal old-date
  9956. (substring org-last-inserted-timestamp 1 -1))))
  9957. (org-add-log-setup 'redeadline nil old-date 'findpos
  9958. org-log-redeadline))
  9959. (when repeater
  9960. (save-excursion
  9961. (org-back-to-heading t)
  9962. (when (re-search-forward (concat org-deadline-string " "
  9963. org-last-inserted-timestamp)
  9964. (save-excursion
  9965. (outline-next-heading) (point)) t)
  9966. (goto-char (1- (match-end 0)))
  9967. (insert " " repeater)
  9968. (setq org-last-inserted-timestamp
  9969. (concat (substring org-last-inserted-timestamp 0 -1)
  9970. " " repeater
  9971. (substring org-last-inserted-timestamp -1))))))
  9972. (message "Deadline on %s" org-last-inserted-timestamp))))
  9973. (defun org-schedule (&optional remove time)
  9974. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9975. With argument REMOVE, remove any scheduling date from the item.
  9976. When TIME is set, it should be an internal time specification, and the
  9977. scheduling will use the corresponding date."
  9978. (interactive "P")
  9979. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  9980. (repeater (and old-date
  9981. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9982. (match-string 1 old-date))))
  9983. (if remove
  9984. (progn
  9985. (when (and old-date org-log-reschedule)
  9986. (org-add-log-setup 'delschedule nil old-date 'findpos
  9987. org-log-reschedule))
  9988. (org-remove-timestamp-with-keyword org-scheduled-string)
  9989. (message "Item is no longer scheduled."))
  9990. (org-add-planning-info 'scheduled time 'closed)
  9991. (when (and old-date org-log-reschedule
  9992. (not (equal old-date
  9993. (substring org-last-inserted-timestamp 1 -1))))
  9994. (org-add-log-setup 'reschedule nil old-date 'findpos
  9995. org-log-reschedule))
  9996. (when repeater
  9997. (save-excursion
  9998. (org-back-to-heading t)
  9999. (when (re-search-forward (concat org-scheduled-string " "
  10000. org-last-inserted-timestamp)
  10001. (save-excursion
  10002. (outline-next-heading) (point)) t)
  10003. (goto-char (1- (match-end 0)))
  10004. (insert " " repeater)
  10005. (setq org-last-inserted-timestamp
  10006. (concat (substring org-last-inserted-timestamp 0 -1)
  10007. " " repeater
  10008. (substring org-last-inserted-timestamp -1))))))
  10009. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10010. (defun org-get-scheduled-time (pom &optional inherit)
  10011. "Get the scheduled time as a time tuple, of a format suitable
  10012. for calling org-schedule with, or if there is no scheduling,
  10013. returns nil."
  10014. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10015. (when time
  10016. (apply 'encode-time (org-parse-time-string time)))))
  10017. (defun org-get-deadline-time (pom &optional inherit)
  10018. "Get the deadine as a time tuple, of a format suitable for
  10019. calling org-deadline with, or if there is no scheduling, returns
  10020. nil."
  10021. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10022. (when time
  10023. (apply 'encode-time (org-parse-time-string time)))))
  10024. (defun org-remove-timestamp-with-keyword (keyword)
  10025. "Remove all time stamps with KEYWORD in the current entry."
  10026. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10027. beg)
  10028. (save-excursion
  10029. (org-back-to-heading t)
  10030. (setq beg (point))
  10031. (outline-next-heading)
  10032. (while (re-search-backward re beg t)
  10033. (replace-match "")
  10034. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10035. (equal (char-before) ?\ ))
  10036. (backward-delete-char 1)
  10037. (if (string-match "^[ \t]*$" (buffer-substring
  10038. (point-at-bol) (point-at-eol)))
  10039. (delete-region (point-at-bol)
  10040. (min (point-max) (1+ (point-at-eol))))))))))
  10041. (defun org-add-planning-info (what &optional time &rest remove)
  10042. "Insert new timestamp with keyword in the line directly after the headline.
  10043. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10044. If non is given, the user is prompted for a date.
  10045. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10046. be removed."
  10047. (interactive)
  10048. (let (org-time-was-given org-end-time-was-given ts
  10049. end default-time default-input)
  10050. (catch 'exit
  10051. (when (and (not time) (memq what '(scheduled deadline)))
  10052. ;; Try to get a default date/time from existing timestamp
  10053. (save-excursion
  10054. (org-back-to-heading t)
  10055. (setq end (save-excursion (outline-next-heading) (point)))
  10056. (when (re-search-forward (if (eq what 'scheduled)
  10057. org-scheduled-time-regexp
  10058. org-deadline-time-regexp)
  10059. end t)
  10060. (setq ts (match-string 1)
  10061. default-time
  10062. (apply 'encode-time (org-parse-time-string ts))
  10063. default-input (and ts (org-get-compact-tod ts))))))
  10064. (when what
  10065. ;; If necessary, get the time from the user
  10066. (setq time (or time (org-read-date nil 'to-time nil nil
  10067. default-time default-input))))
  10068. (when (and org-insert-labeled-timestamps-at-point
  10069. (member what '(scheduled deadline)))
  10070. (insert
  10071. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10072. (org-insert-time-stamp time org-time-was-given
  10073. nil nil nil (list org-end-time-was-given))
  10074. (setq what nil))
  10075. (save-excursion
  10076. (save-restriction
  10077. (let (col list elt ts buffer-invisibility-spec)
  10078. (org-back-to-heading t)
  10079. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10080. (goto-char (match-end 1))
  10081. (setq col (current-column))
  10082. (goto-char (match-end 0))
  10083. (if (eobp) (insert "\n") (forward-char 1))
  10084. (when (and (not what)
  10085. (not (looking-at
  10086. (concat "[ \t]*"
  10087. org-keyword-time-not-clock-regexp))))
  10088. ;; Nothing to add, nothing to remove...... :-)
  10089. (throw 'exit nil))
  10090. (if (and (not (looking-at outline-regexp))
  10091. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10092. "[^\r\n]*"))
  10093. (not (equal (match-string 1) org-clock-string)))
  10094. (narrow-to-region (match-beginning 0) (match-end 0))
  10095. (insert-before-markers "\n")
  10096. (backward-char 1)
  10097. (narrow-to-region (point) (point))
  10098. (and org-adapt-indentation (org-indent-to-column col)))
  10099. ;; Check if we have to remove something.
  10100. (setq list (cons what remove))
  10101. (while list
  10102. (setq elt (pop list))
  10103. (goto-char (point-min))
  10104. (when (or (and (eq elt 'scheduled)
  10105. (re-search-forward org-scheduled-time-regexp nil t))
  10106. (and (eq elt 'deadline)
  10107. (re-search-forward org-deadline-time-regexp nil t))
  10108. (and (eq elt 'closed)
  10109. (re-search-forward org-closed-time-regexp nil t)))
  10110. (replace-match "")
  10111. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10112. (skip-chars-backward " ")
  10113. (if (looking-at " +") (replace-match ""))))
  10114. (goto-char (point-max))
  10115. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10116. (when what
  10117. (insert
  10118. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10119. (cond ((eq what 'scheduled) org-scheduled-string)
  10120. ((eq what 'deadline) org-deadline-string)
  10121. ((eq what 'closed) org-closed-string))
  10122. " ")
  10123. (setq ts (org-insert-time-stamp
  10124. time
  10125. (or org-time-was-given
  10126. (and (eq what 'closed) org-log-done-with-time))
  10127. (eq what 'closed)
  10128. nil nil (list org-end-time-was-given)))
  10129. (end-of-line 1))
  10130. (goto-char (point-min))
  10131. (widen)
  10132. (if (and (looking-at "[ \t]+\n")
  10133. (equal (char-before) ?\n))
  10134. (delete-region (1- (point)) (point-at-eol)))
  10135. ts))))))
  10136. (defvar org-log-note-marker (make-marker))
  10137. (defvar org-log-note-purpose nil)
  10138. (defvar org-log-note-state nil)
  10139. (defvar org-log-note-previous-state nil)
  10140. (defvar org-log-note-how nil)
  10141. (defvar org-log-note-extra nil)
  10142. (defvar org-log-note-window-configuration nil)
  10143. (defvar org-log-note-return-to (make-marker))
  10144. (defvar org-log-post-message nil
  10145. "Message to be displayed after a log note has been stored.
  10146. The auto-repeater uses this.")
  10147. (defun org-add-note ()
  10148. "Add a note to the current entry.
  10149. This is done in the same way as adding a state change note."
  10150. (interactive)
  10151. (org-add-log-setup 'note nil nil 'findpos nil))
  10152. (defvar org-property-end-re)
  10153. (defun org-add-log-setup (&optional purpose state prev-state
  10154. findpos how &optional extra)
  10155. "Set up the post command hook to take a note.
  10156. If this is about to TODO state change, the new state is expected in STATE.
  10157. When FINDPOS is non-nil, find the correct position for the note in
  10158. the current entry. If not, assume that it can be inserted at point.
  10159. HOW is an indicator what kind of note should be created.
  10160. EXTRA is additional text that will be inserted into the notes buffer."
  10161. (let* ((org-log-into-drawer (org-log-into-drawer))
  10162. (drawer (cond ((stringp org-log-into-drawer)
  10163. org-log-into-drawer)
  10164. (org-log-into-drawer "LOGBOOK")
  10165. (t nil))))
  10166. (save-restriction
  10167. (save-excursion
  10168. (when findpos
  10169. (org-back-to-heading t)
  10170. (narrow-to-region (point) (save-excursion
  10171. (outline-next-heading) (point)))
  10172. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10173. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10174. "[^\r\n]*\\)?"))
  10175. (goto-char (match-end 0))
  10176. (cond
  10177. (drawer
  10178. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10179. nil t)
  10180. (progn
  10181. (goto-char (match-end 0))
  10182. (or org-log-states-order-reversed
  10183. (and (re-search-forward org-property-end-re nil t)
  10184. (goto-char (1- (match-beginning 0))))))
  10185. (insert "\n:" drawer ":\n:END:")
  10186. (beginning-of-line 0)
  10187. (org-indent-line-function)
  10188. (beginning-of-line 2)
  10189. (org-indent-line-function)
  10190. (end-of-line 0)))
  10191. ((and org-log-state-notes-insert-after-drawers
  10192. (save-excursion
  10193. (forward-line) (looking-at org-drawer-regexp)))
  10194. (forward-line)
  10195. (while (looking-at org-drawer-regexp)
  10196. (goto-char (match-end 0))
  10197. (re-search-forward org-property-end-re (point-max) t)
  10198. (forward-line))
  10199. (forward-line -1)))
  10200. (unless org-log-states-order-reversed
  10201. (and (= (char-after) ?\n) (forward-char 1))
  10202. (org-skip-over-state-notes)
  10203. (skip-chars-backward " \t\n\r")))
  10204. (move-marker org-log-note-marker (point))
  10205. (setq org-log-note-purpose purpose
  10206. org-log-note-state state
  10207. org-log-note-previous-state prev-state
  10208. org-log-note-how how
  10209. org-log-note-extra extra)
  10210. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10211. (defun org-skip-over-state-notes ()
  10212. "Skip past the list of State notes in an entry."
  10213. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10214. (while (looking-at "[ \t]*- State")
  10215. (condition-case nil
  10216. (org-next-item)
  10217. (error (org-end-of-item)))))
  10218. (defun org-add-log-note (&optional purpose)
  10219. "Pop up a window for taking a note, and add this note later at point."
  10220. (remove-hook 'post-command-hook 'org-add-log-note)
  10221. (setq org-log-note-window-configuration (current-window-configuration))
  10222. (delete-other-windows)
  10223. (move-marker org-log-note-return-to (point))
  10224. (switch-to-buffer (marker-buffer org-log-note-marker))
  10225. (goto-char org-log-note-marker)
  10226. (org-switch-to-buffer-other-window "*Org Note*")
  10227. (erase-buffer)
  10228. (if (memq org-log-note-how '(time state))
  10229. (let (current-prefix-arg) (org-store-log-note))
  10230. (let ((org-inhibit-startup t)) (org-mode))
  10231. (insert (format "# Insert note for %s.
  10232. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10233. (cond
  10234. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10235. ((eq org-log-note-purpose 'done) "closed todo item")
  10236. ((eq org-log-note-purpose 'state)
  10237. (format "state change from \"%s\" to \"%s\""
  10238. (or org-log-note-previous-state "")
  10239. (or org-log-note-state "")))
  10240. ((eq org-log-note-purpose 'reschedule)
  10241. "rescheduling")
  10242. ((eq org-log-note-purpose 'delschedule)
  10243. "no longer scheduled")
  10244. ((eq org-log-note-purpose 'redeadline)
  10245. "changing deadline")
  10246. ((eq org-log-note-purpose 'deldeadline)
  10247. "removing deadline")
  10248. ((eq org-log-note-purpose 'refile)
  10249. "refiling")
  10250. ((eq org-log-note-purpose 'note)
  10251. "this entry")
  10252. (t (error "This should not happen")))))
  10253. (if org-log-note-extra (insert org-log-note-extra))
  10254. (org-set-local 'org-finish-function 'org-store-log-note)))
  10255. (defvar org-note-abort nil) ; dynamically scoped
  10256. (defun org-store-log-note ()
  10257. "Finish taking a log note, and insert it to where it belongs."
  10258. (let ((txt (buffer-string))
  10259. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10260. lines ind)
  10261. (kill-buffer (current-buffer))
  10262. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10263. (setq txt (replace-match "" t t txt)))
  10264. (if (string-match "\\s-+\\'" txt)
  10265. (setq txt (replace-match "" t t txt)))
  10266. (setq lines (org-split-string txt "\n"))
  10267. (when (and note (string-match "\\S-" note))
  10268. (setq note
  10269. (org-replace-escapes
  10270. note
  10271. (list (cons "%u" (user-login-name))
  10272. (cons "%U" user-full-name)
  10273. (cons "%t" (format-time-string
  10274. (org-time-stamp-format 'long 'inactive)
  10275. (current-time)))
  10276. (cons "%s" (if org-log-note-state
  10277. (concat "\"" org-log-note-state "\"")
  10278. ""))
  10279. (cons "%S" (if org-log-note-previous-state
  10280. (concat "\"" org-log-note-previous-state "\"")
  10281. "\"\"")))))
  10282. (if lines (setq note (concat note " \\\\")))
  10283. (push note lines))
  10284. (when (or current-prefix-arg org-note-abort)
  10285. (when org-log-into-drawer
  10286. (org-remove-empty-drawer-at
  10287. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10288. org-log-note-marker))
  10289. (setq lines nil))
  10290. (when lines
  10291. (with-current-buffer (marker-buffer org-log-note-marker)
  10292. (save-excursion
  10293. (goto-char org-log-note-marker)
  10294. (move-marker org-log-note-marker nil)
  10295. (end-of-line 1)
  10296. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10297. (insert "- " (pop lines))
  10298. (org-indent-line-function)
  10299. (beginning-of-line 1)
  10300. (looking-at "[ \t]*")
  10301. (setq ind (concat (match-string 0) " "))
  10302. (end-of-line 1)
  10303. (while lines (insert "\n" ind (pop lines)))
  10304. (message "Note stored")
  10305. (org-back-to-heading t)
  10306. (org-cycle-hide-drawers 'children)))))
  10307. (set-window-configuration org-log-note-window-configuration)
  10308. (with-current-buffer (marker-buffer org-log-note-return-to)
  10309. (goto-char org-log-note-return-to))
  10310. (move-marker org-log-note-return-to nil)
  10311. (and org-log-post-message (message "%s" org-log-post-message)))
  10312. (defun org-remove-empty-drawer-at (drawer pos)
  10313. "Remove an empty drawer DRAWER at position POS.
  10314. POS may also be a marker."
  10315. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10316. (save-excursion
  10317. (save-restriction
  10318. (widen)
  10319. (goto-char pos)
  10320. (if (org-in-regexp
  10321. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10322. (replace-match ""))))))
  10323. (defun org-sparse-tree (&optional arg)
  10324. "Create a sparse tree, prompt for the details.
  10325. This command can create sparse trees. You first need to select the type
  10326. of match used to create the tree:
  10327. t Show entries with a specific TODO keyword.
  10328. m Show entries selected by a tags/property match.
  10329. p Enter a property name and its value (both with completion on existing
  10330. names/values) and show entries with that property.
  10331. / Show entries matching a regular expression (`r' can be used as well)
  10332. d Show deadlines due within `org-deadline-warning-days'.
  10333. b Show deadlines and scheduled items before a date.
  10334. a Show deadlines and scheduled items after a date."
  10335. (interactive "P")
  10336. (let (ans kwd value)
  10337. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10338. (setq ans (read-char-exclusive))
  10339. (cond
  10340. ((equal ans ?d)
  10341. (call-interactively 'org-check-deadlines))
  10342. ((equal ans ?b)
  10343. (call-interactively 'org-check-before-date))
  10344. ((equal ans ?a)
  10345. (call-interactively 'org-check-after-date))
  10346. ((equal ans ?t)
  10347. (org-show-todo-tree '(4)))
  10348. ((member ans '(?T ?m))
  10349. (call-interactively 'org-match-sparse-tree))
  10350. ((member ans '(?p ?P))
  10351. (setq kwd (org-icompleting-read "Property: "
  10352. (mapcar 'list (org-buffer-property-keys))))
  10353. (setq value (org-icompleting-read "Value: "
  10354. (mapcar 'list (org-property-values kwd))))
  10355. (unless (string-match "\\`{.*}\\'" value)
  10356. (setq value (concat "\"" value "\"")))
  10357. (org-match-sparse-tree arg (concat kwd "=" value)))
  10358. ((member ans '(?r ?R ?/))
  10359. (call-interactively 'org-occur))
  10360. (t (error "No such sparse tree command \"%c\"" ans)))))
  10361. (defvar org-occur-highlights nil
  10362. "List of overlays used for occur matches.")
  10363. (make-variable-buffer-local 'org-occur-highlights)
  10364. (defvar org-occur-parameters nil
  10365. "Parameters of the active org-occur calls.
  10366. This is a list, each call to org-occur pushes as cons cell,
  10367. containing the regular expression and the callback, onto the list.
  10368. The list can contain several entries if `org-occur' has been called
  10369. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10370. will only contain one set of parameters. When the highlights are
  10371. removed (for example with `C-c C-c', or with the next edit (depending
  10372. on `org-remove-highlights-with-change'), this variable is emptied
  10373. as well.")
  10374. (make-variable-buffer-local 'org-occur-parameters)
  10375. (defun org-occur (regexp &optional keep-previous callback)
  10376. "Make a compact tree which shows all matches of REGEXP.
  10377. The tree will show the lines where the regexp matches, and all higher
  10378. headlines above the match. It will also show the heading after the match,
  10379. to make sure editing the matching entry is easy.
  10380. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10381. call to `org-occur' will be kept, to allow stacking of calls to this
  10382. command.
  10383. If CALLBACK is non-nil, it is a function which is called to confirm
  10384. that the match should indeed be shown."
  10385. (interactive "sRegexp: \nP")
  10386. (when (equal regexp "")
  10387. (error "Regexp cannot be empty"))
  10388. (unless keep-previous
  10389. (org-remove-occur-highlights nil nil t))
  10390. (push (cons regexp callback) org-occur-parameters)
  10391. (let ((cnt 0))
  10392. (save-excursion
  10393. (goto-char (point-min))
  10394. (if (or (not keep-previous) ; do not want to keep
  10395. (not org-occur-highlights)) ; no previous matches
  10396. ;; hide everything
  10397. (org-overview))
  10398. (while (re-search-forward regexp nil t)
  10399. (when (or (not callback)
  10400. (save-match-data (funcall callback)))
  10401. (setq cnt (1+ cnt))
  10402. (when org-highlight-sparse-tree-matches
  10403. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10404. (org-show-context 'occur-tree))))
  10405. (when org-remove-highlights-with-change
  10406. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10407. nil 'local))
  10408. (unless org-sparse-tree-open-archived-trees
  10409. (org-hide-archived-subtrees (point-min) (point-max)))
  10410. (run-hooks 'org-occur-hook)
  10411. (if (interactive-p)
  10412. (message "%d match(es) for regexp %s" cnt regexp))
  10413. cnt))
  10414. (defun org-show-context (&optional key)
  10415. "Make sure point and context and visible.
  10416. How much context is shown depends upon the variables
  10417. `org-show-hierarchy-above', `org-show-following-heading'. and
  10418. `org-show-siblings'."
  10419. (let ((heading-p (org-on-heading-p t))
  10420. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10421. (following-p (org-get-alist-option org-show-following-heading key))
  10422. (entry-p (org-get-alist-option org-show-entry-below key))
  10423. (siblings-p (org-get-alist-option org-show-siblings key)))
  10424. (catch 'exit
  10425. ;; Show heading or entry text
  10426. (if (and heading-p (not entry-p))
  10427. (org-flag-heading nil) ; only show the heading
  10428. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10429. (org-show-hidden-entry))) ; show entire entry
  10430. (when following-p
  10431. ;; Show next sibling, or heading below text
  10432. (save-excursion
  10433. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10434. (org-flag-heading nil))))
  10435. (when siblings-p (org-show-siblings))
  10436. (when hierarchy-p
  10437. ;; show all higher headings, possibly with siblings
  10438. (save-excursion
  10439. (while (and (condition-case nil
  10440. (progn (org-up-heading-all 1) t)
  10441. (error nil))
  10442. (not (bobp)))
  10443. (org-flag-heading nil)
  10444. (when siblings-p (org-show-siblings))))))))
  10445. (defvar org-reveal-start-hook nil
  10446. "Hook run before revealing a location.")
  10447. (defun org-reveal (&optional siblings)
  10448. "Show current entry, hierarchy above it, and the following headline.
  10449. This can be used to show a consistent set of context around locations
  10450. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10451. not t for the search context.
  10452. With optional argument SIBLINGS, on each level of the hierarchy all
  10453. siblings are shown. This repairs the tree structure to what it would
  10454. look like when opened with hierarchical calls to `org-cycle'.
  10455. With double optional argument `C-u C-u', go to the parent and show the
  10456. entire tree."
  10457. (interactive "P")
  10458. (run-hooks 'org-reveal-start-hook)
  10459. (let ((org-show-hierarchy-above t)
  10460. (org-show-following-heading t)
  10461. (org-show-siblings (if siblings t org-show-siblings)))
  10462. (org-show-context nil))
  10463. (when (equal siblings '(16))
  10464. (save-excursion
  10465. (when (org-up-heading-safe)
  10466. (org-show-subtree)
  10467. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10468. (defun org-highlight-new-match (beg end)
  10469. "Highlight from BEG to END and mark the highlight is an occur headline."
  10470. (let ((ov (make-overlay beg end)))
  10471. (overlay-put ov 'face 'secondary-selection)
  10472. (push ov org-occur-highlights)))
  10473. (defun org-remove-occur-highlights (&optional beg end noremove)
  10474. "Remove the occur highlights from the buffer.
  10475. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10476. from the `before-change-functions' in the current buffer."
  10477. (interactive)
  10478. (unless org-inhibit-highlight-removal
  10479. (mapc 'delete-overlay org-occur-highlights)
  10480. (setq org-occur-highlights nil)
  10481. (setq org-occur-parameters nil)
  10482. (unless noremove
  10483. (remove-hook 'before-change-functions
  10484. 'org-remove-occur-highlights 'local))))
  10485. ;;;; Priorities
  10486. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10487. "Regular expression matching the priority indicator.")
  10488. (defvar org-remove-priority-next-time nil)
  10489. (defun org-priority-up ()
  10490. "Increase the priority of the current item."
  10491. (interactive)
  10492. (org-priority 'up))
  10493. (defun org-priority-down ()
  10494. "Decrease the priority of the current item."
  10495. (interactive)
  10496. (org-priority 'down))
  10497. (defun org-priority (&optional action)
  10498. "Change the priority of an item by ARG.
  10499. ACTION can be `set', `up', `down', or a character."
  10500. (interactive)
  10501. (unless org-enable-priority-commands
  10502. (error "Priority commands are disabled"))
  10503. (setq action (or action 'set))
  10504. (let (current new news have remove)
  10505. (save-excursion
  10506. (org-back-to-heading t)
  10507. (if (looking-at org-priority-regexp)
  10508. (setq current (string-to-char (match-string 2))
  10509. have t)
  10510. (setq current org-default-priority))
  10511. (cond
  10512. ((eq action 'remove)
  10513. (setq remove t new ?\ ))
  10514. ((or (eq action 'set)
  10515. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10516. (if (not (eq action 'set))
  10517. (setq new action)
  10518. (message "Priority %c-%c, SPC to remove: "
  10519. org-highest-priority org-lowest-priority)
  10520. (setq new (read-char-exclusive)))
  10521. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10522. (= (upcase org-lowest-priority) org-lowest-priority))
  10523. (setq new (upcase new)))
  10524. (cond ((equal new ?\ ) (setq remove t))
  10525. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10526. (error "Priority must be between `%c' and `%c'"
  10527. org-highest-priority org-lowest-priority))))
  10528. ((eq action 'up)
  10529. (if (and (not have) (eq last-command this-command))
  10530. (setq new org-lowest-priority)
  10531. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10532. org-default-priority (1- current)))))
  10533. ((eq action 'down)
  10534. (if (and (not have) (eq last-command this-command))
  10535. (setq new org-highest-priority)
  10536. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10537. org-default-priority (1+ current)))))
  10538. (t (error "Invalid action")))
  10539. (if (or (< (upcase new) org-highest-priority)
  10540. (> (upcase new) org-lowest-priority))
  10541. (setq remove t))
  10542. (setq news (format "%c" new))
  10543. (if have
  10544. (if remove
  10545. (replace-match "" t t nil 1)
  10546. (replace-match news t t nil 2))
  10547. (if remove
  10548. (error "No priority cookie found in line")
  10549. (let ((case-fold-search nil))
  10550. (looking-at org-todo-line-regexp))
  10551. (if (match-end 2)
  10552. (progn
  10553. (goto-char (match-end 2))
  10554. (insert " [#" news "]"))
  10555. (goto-char (match-beginning 3))
  10556. (insert "[#" news "] "))))
  10557. (org-preserve-lc (org-set-tags nil 'align)))
  10558. (if remove
  10559. (message "Priority removed")
  10560. (message "Priority of current item set to %s" news))))
  10561. (defun org-get-priority (s)
  10562. "Find priority cookie and return priority."
  10563. (save-match-data
  10564. (if (not (string-match org-priority-regexp s))
  10565. (* 1000 (- org-lowest-priority org-default-priority))
  10566. (* 1000 (- org-lowest-priority
  10567. (string-to-char (match-string 2 s)))))))
  10568. ;;;; Tags
  10569. (defvar org-agenda-archives-mode)
  10570. (defvar org-map-continue-from nil
  10571. "Position from where mapping should continue.
  10572. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10573. (defvar org-scanner-tags nil
  10574. "The current tag list while the tags scanner is running.")
  10575. (defvar org-trust-scanner-tags nil
  10576. "Should `org-get-tags-at' use the tags fro the scanner.
  10577. This is for internal dynamical scoping only.
  10578. When this is non-nil, the function `org-get-tags-at' will return the value
  10579. of `org-scanner-tags' instead of building the list by itself. This
  10580. can lead to large speed-ups when the tags scanner is used in a file with
  10581. many entries, and when the list of tags is retrieved, for example to
  10582. obtain a list of properties. Building the tags list for each entry in such
  10583. a file becomes an N^2 operation - but with this variable set, it scales
  10584. as N.")
  10585. (defun org-scan-tags (action matcher &optional todo-only)
  10586. "Scan headline tags with inheritance and produce output ACTION.
  10587. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10588. or `agenda' to produce an entry list for an agenda view. It can also be
  10589. a Lisp form or a function that should be called at each matched headline, in
  10590. this case the return value is a list of all return values from these calls.
  10591. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10592. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10593. only lines with a TODO keyword are included in the output."
  10594. (require 'org-agenda)
  10595. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10596. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10597. (org-re
  10598. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10599. (props (list 'face 'default
  10600. 'done-face 'org-agenda-done
  10601. 'undone-face 'default
  10602. 'mouse-face 'highlight
  10603. 'org-not-done-regexp org-not-done-regexp
  10604. 'org-todo-regexp org-todo-regexp
  10605. 'help-echo
  10606. (format "mouse-2 or RET jump to org file %s"
  10607. (abbreviate-file-name
  10608. (or (buffer-file-name (buffer-base-buffer))
  10609. (buffer-name (buffer-base-buffer)))))))
  10610. (case-fold-search nil)
  10611. (org-map-continue-from nil)
  10612. lspos tags tags-list
  10613. (tags-alist (list (cons 0 org-file-tags)))
  10614. (llast 0) rtn rtn1 level category i txt
  10615. todo marker entry priority)
  10616. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10617. (setq action (list 'lambda nil action)))
  10618. (save-excursion
  10619. (goto-char (point-min))
  10620. (when (eq action 'sparse-tree)
  10621. (org-overview)
  10622. (org-remove-occur-highlights))
  10623. (while (re-search-forward re nil t)
  10624. (catch :skip
  10625. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10626. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10627. (goto-char (setq lspos (match-beginning 0)))
  10628. (setq level (org-reduced-level (funcall outline-level))
  10629. category (org-get-category))
  10630. (setq i llast llast level)
  10631. ;; remove tag lists from same and sublevels
  10632. (while (>= i level)
  10633. (when (setq entry (assoc i tags-alist))
  10634. (setq tags-alist (delete entry tags-alist)))
  10635. (setq i (1- i)))
  10636. ;; add the next tags
  10637. (when tags
  10638. (setq tags (org-split-string tags ":")
  10639. tags-alist
  10640. (cons (cons level tags) tags-alist)))
  10641. ;; compile tags for current headline
  10642. (setq tags-list
  10643. (if org-use-tag-inheritance
  10644. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10645. tags)
  10646. org-scanner-tags tags-list)
  10647. (when org-use-tag-inheritance
  10648. (setcdr (car tags-alist)
  10649. (mapcar (lambda (x)
  10650. (setq x (copy-sequence x))
  10651. (org-add-prop-inherited x))
  10652. (cdar tags-alist))))
  10653. (when (and tags org-use-tag-inheritance
  10654. (or (not (eq t org-use-tag-inheritance))
  10655. org-tags-exclude-from-inheritance))
  10656. ;; selective inheritance, remove uninherited ones
  10657. (setcdr (car tags-alist)
  10658. (org-remove-uniherited-tags (cdar tags-alist))))
  10659. (when (and (or (not todo-only)
  10660. (and (member todo org-not-done-keywords)
  10661. (or (not org-agenda-tags-todo-honor-ignore-options)
  10662. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10663. (let ((case-fold-search t)) (eval matcher))
  10664. (or
  10665. (not (member org-archive-tag tags-list))
  10666. ;; we have an archive tag, should we use this anyway?
  10667. (or (not org-agenda-skip-archived-trees)
  10668. (and (eq action 'agenda) org-agenda-archives-mode))))
  10669. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10670. ;; select this headline
  10671. (cond
  10672. ((eq action 'sparse-tree)
  10673. (and org-highlight-sparse-tree-matches
  10674. (org-get-heading) (match-end 0)
  10675. (org-highlight-new-match
  10676. (match-beginning 0) (match-beginning 1)))
  10677. (org-show-context 'tags-tree))
  10678. ((eq action 'agenda)
  10679. (setq txt (org-format-agenda-item
  10680. ""
  10681. (concat
  10682. (if (eq org-tags-match-list-sublevels 'indented)
  10683. (make-string (1- level) ?.) "")
  10684. (org-get-heading))
  10685. category
  10686. tags-list
  10687. )
  10688. priority (org-get-priority txt))
  10689. (goto-char lspos)
  10690. (setq marker (org-agenda-new-marker))
  10691. (org-add-props txt props
  10692. 'org-marker marker 'org-hd-marker marker 'org-category category
  10693. 'todo-state todo
  10694. 'priority priority 'type "tagsmatch")
  10695. (push txt rtn))
  10696. ((functionp action)
  10697. (setq org-map-continue-from nil)
  10698. (save-excursion
  10699. (setq rtn1 (funcall action))
  10700. (push rtn1 rtn)))
  10701. (t (error "Invalid action")))
  10702. ;; if we are to skip sublevels, jump to end of subtree
  10703. (unless org-tags-match-list-sublevels
  10704. (org-end-of-subtree t)
  10705. (backward-char 1))))
  10706. ;; Get the correct position from where to continue
  10707. (if org-map-continue-from
  10708. (goto-char org-map-continue-from)
  10709. (and (= (point) lspos) (end-of-line 1)))))
  10710. (when (and (eq action 'sparse-tree)
  10711. (not org-sparse-tree-open-archived-trees))
  10712. (org-hide-archived-subtrees (point-min) (point-max)))
  10713. (nreverse rtn)))
  10714. (defun org-remove-uniherited-tags (tags)
  10715. "Remove all tags that are not inherited from the list TAGS."
  10716. (cond
  10717. ((eq org-use-tag-inheritance t)
  10718. (if org-tags-exclude-from-inheritance
  10719. (org-delete-all org-tags-exclude-from-inheritance tags)
  10720. tags))
  10721. ((not org-use-tag-inheritance) nil)
  10722. ((stringp org-use-tag-inheritance)
  10723. (delq nil (mapcar
  10724. (lambda (x)
  10725. (if (and (string-match org-use-tag-inheritance x)
  10726. (not (member x org-tags-exclude-from-inheritance)))
  10727. x nil))
  10728. tags)))
  10729. ((listp org-use-tag-inheritance)
  10730. (delq nil (mapcar
  10731. (lambda (x)
  10732. (if (member x org-use-tag-inheritance) x nil))
  10733. tags)))))
  10734. (defvar todo-only) ;; dynamically scoped
  10735. (defun org-match-sparse-tree (&optional todo-only match)
  10736. "Create a sparse tree according to tags string MATCH.
  10737. MATCH can contain positive and negative selection of tags, like
  10738. \"+WORK+URGENT-WITHBOSS\".
  10739. If optional argument TODO-ONLY is non-nil, only select lines that are
  10740. also TODO lines."
  10741. (interactive "P")
  10742. (org-prepare-agenda-buffers (list (current-buffer)))
  10743. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10744. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10745. (defvar org-cached-props nil)
  10746. (defun org-cached-entry-get (pom property)
  10747. (if (or (eq t org-use-property-inheritance)
  10748. (and (stringp org-use-property-inheritance)
  10749. (string-match org-use-property-inheritance property))
  10750. (and (listp org-use-property-inheritance)
  10751. (member property org-use-property-inheritance)))
  10752. ;; Caching is not possible, check it directly
  10753. (org-entry-get pom property 'inherit)
  10754. ;; Get all properties, so that we can do complicated checks easily
  10755. (cdr (assoc property (or org-cached-props
  10756. (setq org-cached-props
  10757. (org-entry-properties pom)))))))
  10758. (defun org-global-tags-completion-table (&optional files)
  10759. "Return the list of all tags in all agenda buffer/files."
  10760. (save-excursion
  10761. (org-uniquify
  10762. (delq nil
  10763. (apply 'append
  10764. (mapcar
  10765. (lambda (file)
  10766. (set-buffer (find-file-noselect file))
  10767. (append (org-get-buffer-tags)
  10768. (mapcar (lambda (x) (if (stringp (car-safe x))
  10769. (list (car-safe x)) nil))
  10770. org-tag-alist)))
  10771. (if (and files (car files))
  10772. files
  10773. (org-agenda-files))))))))
  10774. (defun org-make-tags-matcher (match)
  10775. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10776. ;; todo-only is scoped dynamically into this function, and the function
  10777. ;; may change it if the matcher asks for it.
  10778. (unless match
  10779. ;; Get a new match request, with completion
  10780. (let ((org-last-tags-completion-table
  10781. (org-global-tags-completion-table)))
  10782. (setq match (org-completing-read-no-i
  10783. "Match: " 'org-tags-completion-function nil nil nil
  10784. 'org-tags-history))))
  10785. ;; Parse the string and create a lisp form
  10786. (let ((match0 match)
  10787. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10788. minus tag mm
  10789. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10790. orterms term orlist re-p str-p level-p level-op time-p
  10791. prop-p pn pv po cat-p gv rest)
  10792. (if (string-match "/+" match)
  10793. ;; match contains also a todo-matching request
  10794. (progn
  10795. (setq tagsmatch (substring match 0 (match-beginning 0))
  10796. todomatch (substring match (match-end 0)))
  10797. (if (string-match "^!" todomatch)
  10798. (setq todo-only t todomatch (substring todomatch 1)))
  10799. (if (string-match "^\\s-*$" todomatch)
  10800. (setq todomatch nil)))
  10801. ;; only matching tags
  10802. (setq tagsmatch match todomatch nil))
  10803. ;; Make the tags matcher
  10804. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10805. (setq tagsmatcher t)
  10806. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10807. (while (setq term (pop orterms))
  10808. (while (and (equal (substring term -1) "\\") orterms)
  10809. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10810. (while (string-match re term)
  10811. (setq rest (substring term (match-end 0))
  10812. minus (and (match-end 1)
  10813. (equal (match-string 1 term) "-"))
  10814. tag (match-string 2 term)
  10815. re-p (equal (string-to-char tag) ?{)
  10816. level-p (match-end 4)
  10817. prop-p (match-end 5)
  10818. mm (cond
  10819. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10820. (level-p
  10821. (setq level-op (org-op-to-function (match-string 3 term)))
  10822. `(,level-op level ,(string-to-number
  10823. (match-string 4 term))))
  10824. (prop-p
  10825. (setq pn (match-string 5 term)
  10826. po (match-string 6 term)
  10827. pv (match-string 7 term)
  10828. cat-p (equal pn "CATEGORY")
  10829. re-p (equal (string-to-char pv) ?{)
  10830. str-p (equal (string-to-char pv) ?\")
  10831. time-p (save-match-data
  10832. (string-match "^\"[[<].*[]>]\"$" pv))
  10833. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10834. (if time-p (setq pv (org-matcher-time pv)))
  10835. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10836. (cond
  10837. ((equal pn "CATEGORY")
  10838. (setq gv '(get-text-property (point) 'org-category)))
  10839. ((equal pn "TODO")
  10840. (setq gv 'todo))
  10841. (t
  10842. (setq gv `(org-cached-entry-get nil ,pn))))
  10843. (if re-p
  10844. (if (eq po 'org<>)
  10845. `(not (string-match ,pv (or ,gv "")))
  10846. `(string-match ,pv (or ,gv "")))
  10847. (if str-p
  10848. `(,po (or ,gv "") ,pv)
  10849. `(,po (string-to-number (or ,gv ""))
  10850. ,(string-to-number pv) ))))
  10851. (t `(member ,tag tags-list)))
  10852. mm (if minus (list 'not mm) mm)
  10853. term rest)
  10854. (push mm tagsmatcher))
  10855. (push (if (> (length tagsmatcher) 1)
  10856. (cons 'and tagsmatcher)
  10857. (car tagsmatcher))
  10858. orlist)
  10859. (setq tagsmatcher nil))
  10860. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10861. (setq tagsmatcher
  10862. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10863. ;; Make the todo matcher
  10864. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10865. (setq todomatcher t)
  10866. (setq orterms (org-split-string todomatch "|") orlist nil)
  10867. (while (setq term (pop orterms))
  10868. (while (string-match re term)
  10869. (setq minus (and (match-end 1)
  10870. (equal (match-string 1 term) "-"))
  10871. kwd (match-string 2 term)
  10872. re-p (equal (string-to-char kwd) ?{)
  10873. term (substring term (match-end 0))
  10874. mm (if re-p
  10875. `(string-match ,(substring kwd 1 -1) todo)
  10876. (list 'equal 'todo kwd))
  10877. mm (if minus (list 'not mm) mm))
  10878. (push mm todomatcher))
  10879. (push (if (> (length todomatcher) 1)
  10880. (cons 'and todomatcher)
  10881. (car todomatcher))
  10882. orlist)
  10883. (setq todomatcher nil))
  10884. (setq todomatcher (if (> (length orlist) 1)
  10885. (cons 'or orlist) (car orlist))))
  10886. ;; Return the string and lisp forms of the matcher
  10887. (setq matcher (if todomatcher
  10888. (list 'and tagsmatcher todomatcher)
  10889. tagsmatcher))
  10890. (cons match0 matcher)))
  10891. (defun org-op-to-function (op &optional stringp)
  10892. "Turn an operator into the appropriate function."
  10893. (setq op
  10894. (cond
  10895. ((equal op "<" ) '(< string< org-time<))
  10896. ((equal op ">" ) '(> org-string> org-time>))
  10897. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10898. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10899. ((member op '("=" "==")) '(= string= org-time=))
  10900. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10901. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10902. (defun org<> (a b) (not (= a b)))
  10903. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10904. (defun org-string>= (a b) (not (string< a b)))
  10905. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10906. (defun org-string<> (a b) (not (string= a b)))
  10907. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10908. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10909. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10910. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10911. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10912. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10913. (defun org-2ft (s)
  10914. "Convert S to a floating point time.
  10915. If S is already a number, just return it. If it is a string, parse
  10916. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10917. (cond
  10918. ((numberp s) s)
  10919. ((stringp s)
  10920. (condition-case nil
  10921. (float-time (apply 'encode-time (org-parse-time-string s)))
  10922. (error 0.)))
  10923. (t 0.)))
  10924. (defun org-time-today ()
  10925. "Time in seconds today at 0:00.
  10926. Returns the float number of seconds since the beginning of the
  10927. epoch to the beginning of today (00:00)."
  10928. (float-time (apply 'encode-time
  10929. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10930. (defun org-matcher-time (s)
  10931. "Interpret a time comparison value."
  10932. (save-match-data
  10933. (cond
  10934. ((string= s "<now>") (float-time))
  10935. ((string= s "<today>") (org-time-today))
  10936. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10937. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10938. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10939. (+ (org-time-today)
  10940. (* (string-to-number (match-string 1 s))
  10941. (cdr (assoc (match-string 2 s)
  10942. '(("d" . 86400.0) ("w" . 604800.0)
  10943. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10944. (t (org-2ft s)))))
  10945. (defun org-match-any-p (re list)
  10946. "Does re match any element of list?"
  10947. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10948. (delq nil list))
  10949. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10950. (defvar org-tags-overlay (make-overlay 1 1))
  10951. (org-detach-overlay org-tags-overlay)
  10952. (defun org-get-local-tags-at (&optional pos)
  10953. "Get a list of tags defined in the current headline."
  10954. (org-get-tags-at pos 'local))
  10955. (defun org-get-local-tags ()
  10956. "Get a list of tags defined in the current headline."
  10957. (org-get-tags-at nil 'local))
  10958. (defun org-get-tags-at (&optional pos local)
  10959. "Get a list of all headline tags applicable at POS.
  10960. POS defaults to point. If tags are inherited, the list contains
  10961. the targets in the same sequence as the headlines appear, i.e.
  10962. the tags of the current headline come last.
  10963. When LOCAL is non-nil, only return tags from the current headline,
  10964. ignore inherited ones."
  10965. (interactive)
  10966. (if (and org-trust-scanner-tags
  10967. (or (not pos) (equal pos (point)))
  10968. (not local))
  10969. org-scanner-tags
  10970. (let (tags ltags lastpos parent)
  10971. (save-excursion
  10972. (save-restriction
  10973. (widen)
  10974. (goto-char (or pos (point)))
  10975. (save-match-data
  10976. (catch 'done
  10977. (condition-case nil
  10978. (progn
  10979. (org-back-to-heading t)
  10980. (while (not (equal lastpos (point)))
  10981. (setq lastpos (point))
  10982. (when (looking-at
  10983. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10984. (setq ltags (org-split-string
  10985. (org-match-string-no-properties 1) ":"))
  10986. (when parent
  10987. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10988. (setq tags (append
  10989. (if parent
  10990. (org-remove-uniherited-tags ltags)
  10991. ltags)
  10992. tags)))
  10993. (or org-use-tag-inheritance (throw 'done t))
  10994. (if local (throw 'done t))
  10995. (or (org-up-heading-safe) (error nil))
  10996. (setq parent t)))
  10997. (error nil)))))
  10998. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10999. (defun org-add-prop-inherited (s)
  11000. (add-text-properties 0 (length s) '(inherited t) s)
  11001. s)
  11002. (defun org-toggle-tag (tag &optional onoff)
  11003. "Toggle the tag TAG for the current line.
  11004. If ONOFF is `on' or `off', don't toggle but set to this state."
  11005. (let (res current)
  11006. (save-excursion
  11007. (org-back-to-heading t)
  11008. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  11009. (point-at-eol) t)
  11010. (progn
  11011. (setq current (match-string 1))
  11012. (replace-match ""))
  11013. (setq current ""))
  11014. (setq current (nreverse (org-split-string current ":")))
  11015. (cond
  11016. ((eq onoff 'on)
  11017. (setq res t)
  11018. (or (member tag current) (push tag current)))
  11019. ((eq onoff 'off)
  11020. (or (not (member tag current)) (setq current (delete tag current))))
  11021. (t (if (member tag current)
  11022. (setq current (delete tag current))
  11023. (setq res t)
  11024. (push tag current))))
  11025. (end-of-line 1)
  11026. (if current
  11027. (progn
  11028. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11029. (org-set-tags nil t))
  11030. (delete-horizontal-space))
  11031. (run-hooks 'org-after-tags-change-hook))
  11032. res))
  11033. (defun org-align-tags-here (to-col)
  11034. ;; Assumes that this is a headline
  11035. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11036. (beginning-of-line 1)
  11037. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11038. (< pos (match-beginning 2)))
  11039. (progn
  11040. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11041. (goto-char (match-beginning 1))
  11042. (insert " ")
  11043. (delete-region (point) (1+ (match-beginning 2)))
  11044. (setq ncol (max (1+ (current-column))
  11045. (1+ col)
  11046. (if (> to-col 0)
  11047. to-col
  11048. (- (abs to-col) tags-l))))
  11049. (setq p (point))
  11050. (insert (make-string (- ncol (current-column)) ?\ ))
  11051. (setq ncol (current-column))
  11052. (when indent-tabs-mode (tabify p (point-at-eol)))
  11053. (org-move-to-column (min ncol col) t))
  11054. (goto-char pos))))
  11055. (defun org-set-tags-command (&optional arg just-align)
  11056. "Call the set-tags command for the current entry."
  11057. (interactive "P")
  11058. (if (org-on-heading-p)
  11059. (org-set-tags arg just-align)
  11060. (save-excursion
  11061. (org-back-to-heading t)
  11062. (org-set-tags arg just-align))))
  11063. (defun org-set-tags-to (data)
  11064. "Set the tags of the current entry to DATA, replacing the current tags.
  11065. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11066. If DATA is nil or the empty string, any tags will be removed."
  11067. (interactive "sTags: ")
  11068. (setq data
  11069. (cond
  11070. ((eq data nil) "")
  11071. ((equal data "") "")
  11072. ((stringp data)
  11073. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11074. ":"))
  11075. ((listp data)
  11076. (concat ":" (mapconcat 'identity data ":") ":"))
  11077. (t nil)))
  11078. (when data
  11079. (save-excursion
  11080. (org-back-to-heading t)
  11081. (when (looking-at org-complex-heading-regexp)
  11082. (if (match-end 5)
  11083. (progn
  11084. (goto-char (match-beginning 5))
  11085. (insert data)
  11086. (delete-region (point) (point-at-eol))
  11087. (org-set-tags nil 'align))
  11088. (goto-char (point-at-eol))
  11089. (insert " " data)
  11090. (org-set-tags nil 'align)))
  11091. (beginning-of-line 1)
  11092. (if (looking-at ".*?\\([ \t]+\\)$")
  11093. (delete-region (match-beginning 1) (match-end 1))))))
  11094. (defun org-align-all-tags ()
  11095. "Align the tags i all headings."
  11096. (interactive)
  11097. (save-excursion
  11098. (or (ignore-errors (org-back-to-heading t))
  11099. (outline-next-heading))
  11100. (if (org-on-heading-p)
  11101. (org-set-tags t)
  11102. (message "No headings"))))
  11103. (defun org-set-tags (&optional arg just-align)
  11104. "Set the tags for the current headline.
  11105. With prefix ARG, realign all tags in headings in the current buffer."
  11106. (interactive "P")
  11107. (let* ((re (concat "^" outline-regexp))
  11108. (current (org-get-tags-string))
  11109. (col (current-column))
  11110. (org-setting-tags t)
  11111. table current-tags inherited-tags ; computed below when needed
  11112. tags p0 c0 c1 rpl)
  11113. (if arg
  11114. (save-excursion
  11115. (goto-char (point-min))
  11116. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11117. (while (re-search-forward re nil t)
  11118. (org-set-tags nil t)
  11119. (end-of-line 1)))
  11120. (message "All tags realigned to column %d" org-tags-column))
  11121. (if just-align
  11122. (setq tags current)
  11123. ;; Get a new set of tags from the user
  11124. (save-excursion
  11125. (setq table (append org-tag-persistent-alist
  11126. (or org-tag-alist (org-get-buffer-tags))
  11127. (and org-complete-tags-always-offer-all-agenda-tags
  11128. (org-global-tags-completion-table (org-agenda-files))))
  11129. org-last-tags-completion-table table
  11130. current-tags (org-split-string current ":")
  11131. inherited-tags (nreverse
  11132. (nthcdr (length current-tags)
  11133. (nreverse (org-get-tags-at))))
  11134. tags
  11135. (if (or (eq t org-use-fast-tag-selection)
  11136. (and org-use-fast-tag-selection
  11137. (delq nil (mapcar 'cdr table))))
  11138. (org-fast-tag-selection
  11139. current-tags inherited-tags table
  11140. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11141. (let ((org-add-colon-after-tag-completion t))
  11142. (org-trim
  11143. (org-without-partial-completion
  11144. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11145. nil nil current 'org-tags-history)))))))
  11146. (while (string-match "[-+&]+" tags)
  11147. ;; No boolean logic, just a list
  11148. (setq tags (replace-match ":" t t tags))))
  11149. (if org-tags-sort-function
  11150. (setq tags (mapconcat 'identity
  11151. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11152. org-tags-sort-function) ":")))
  11153. (if (string-match "\\`[\t ]*\\'" tags)
  11154. (setq tags "")
  11155. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11156. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11157. ;; Insert new tags at the correct column
  11158. (beginning-of-line 1)
  11159. (cond
  11160. ((and (equal current "") (equal tags "")))
  11161. ((re-search-forward
  11162. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11163. (point-at-eol) t)
  11164. (if (equal tags "")
  11165. (setq rpl "")
  11166. (goto-char (match-beginning 0))
  11167. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11168. (1+ (point)) (point))
  11169. c1 (max (1+ c0) (if (> org-tags-column 0)
  11170. org-tags-column
  11171. (- (- org-tags-column) (length tags))))
  11172. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11173. (replace-match rpl t t)
  11174. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11175. tags)
  11176. (t (error "Tags alignment failed")))
  11177. (org-move-to-column col)
  11178. (unless just-align
  11179. (run-hooks 'org-after-tags-change-hook)))))
  11180. (defun org-change-tag-in-region (beg end tag off)
  11181. "Add or remove TAG for each entry in the region.
  11182. This works in the agenda, and also in an org-mode buffer."
  11183. (interactive
  11184. (list (region-beginning) (region-end)
  11185. (let ((org-last-tags-completion-table
  11186. (if (org-mode-p)
  11187. (org-get-buffer-tags)
  11188. (org-global-tags-completion-table))))
  11189. (org-icompleting-read
  11190. "Tag: " 'org-tags-completion-function nil nil nil
  11191. 'org-tags-history))
  11192. (progn
  11193. (message "[s]et or [r]emove? ")
  11194. (equal (read-char-exclusive) ?r))))
  11195. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11196. (let ((agendap (equal major-mode 'org-agenda-mode))
  11197. l1 l2 m buf pos newhead (cnt 0))
  11198. (goto-char end)
  11199. (setq l2 (1- (org-current-line)))
  11200. (goto-char beg)
  11201. (setq l1 (org-current-line))
  11202. (loop for l from l1 to l2 do
  11203. (org-goto-line l)
  11204. (setq m (get-text-property (point) 'org-hd-marker))
  11205. (when (or (and (org-mode-p) (org-on-heading-p))
  11206. (and agendap m))
  11207. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11208. pos (if agendap m (point)))
  11209. (with-current-buffer buf
  11210. (save-excursion
  11211. (save-restriction
  11212. (goto-char pos)
  11213. (setq cnt (1+ cnt))
  11214. (org-toggle-tag tag (if off 'off 'on))
  11215. (setq newhead (org-get-heading)))))
  11216. (and agendap (org-agenda-change-all-lines newhead m))))
  11217. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11218. (defun org-tags-completion-function (string predicate &optional flag)
  11219. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11220. (confirm (lambda (x) (stringp (car x)))))
  11221. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11222. (setq s1 (match-string 1 string)
  11223. s2 (match-string 2 string))
  11224. (setq s1 "" s2 string))
  11225. (cond
  11226. ((eq flag nil)
  11227. ;; try completion
  11228. (setq rtn (try-completion s2 ctable confirm))
  11229. (if (stringp rtn)
  11230. (setq rtn
  11231. (concat s1 s2 (substring rtn (length s2))
  11232. (if (and org-add-colon-after-tag-completion
  11233. (assoc rtn ctable))
  11234. ":" ""))))
  11235. rtn)
  11236. ((eq flag t)
  11237. ;; all-completions
  11238. (all-completions s2 ctable confirm)
  11239. )
  11240. ((eq flag 'lambda)
  11241. ;; exact match?
  11242. (assoc s2 ctable)))
  11243. ))
  11244. (defun org-fast-tag-insert (kwd tags face &optional end)
  11245. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11246. (insert (format "%-12s" (concat kwd ":"))
  11247. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11248. (or end "")))
  11249. (defun org-fast-tag-show-exit (flag)
  11250. (save-excursion
  11251. (org-goto-line 3)
  11252. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11253. (replace-match ""))
  11254. (when flag
  11255. (end-of-line 1)
  11256. (org-move-to-column (- (window-width) 19) t)
  11257. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11258. (defun org-set-current-tags-overlay (current prefix)
  11259. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11260. (if (featurep 'xemacs)
  11261. (org-overlay-display org-tags-overlay (concat prefix s)
  11262. 'secondary-selection)
  11263. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11264. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11265. (defvar org-last-tag-selection-key nil)
  11266. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11267. "Fast tag selection with single keys.
  11268. CURRENT is the current list of tags in the headline, INHERITED is the
  11269. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11270. possibly with grouping information. TODO-TABLE is a similar table with
  11271. TODO keywords, should these have keys assigned to them.
  11272. If the keys are nil, a-z are automatically assigned.
  11273. Returns the new tags string, or nil to not change the current settings."
  11274. (let* ((fulltable (append table todo-table))
  11275. (maxlen (apply 'max (mapcar
  11276. (lambda (x)
  11277. (if (stringp (car x)) (string-width (car x)) 0))
  11278. fulltable)))
  11279. (buf (current-buffer))
  11280. (expert (eq org-fast-tag-selection-single-key 'expert))
  11281. (buffer-tags nil)
  11282. (fwidth (+ maxlen 3 1 3))
  11283. (ncol (/ (- (window-width) 4) fwidth))
  11284. (i-face 'org-done)
  11285. (c-face 'org-todo)
  11286. tg cnt e c char c1 c2 ntable tbl rtn
  11287. ov-start ov-end ov-prefix
  11288. (exit-after-next org-fast-tag-selection-single-key)
  11289. (done-keywords org-done-keywords)
  11290. groups ingroup)
  11291. (save-excursion
  11292. (beginning-of-line 1)
  11293. (if (looking-at
  11294. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11295. (setq ov-start (match-beginning 1)
  11296. ov-end (match-end 1)
  11297. ov-prefix "")
  11298. (setq ov-start (1- (point-at-eol))
  11299. ov-end (1+ ov-start))
  11300. (skip-chars-forward "^\n\r")
  11301. (setq ov-prefix
  11302. (concat
  11303. (buffer-substring (1- (point)) (point))
  11304. (if (> (current-column) org-tags-column)
  11305. " "
  11306. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11307. (move-overlay org-tags-overlay ov-start ov-end)
  11308. (save-window-excursion
  11309. (if expert
  11310. (set-buffer (get-buffer-create " *Org tags*"))
  11311. (delete-other-windows)
  11312. (split-window-vertically)
  11313. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11314. (erase-buffer)
  11315. (org-set-local 'org-done-keywords done-keywords)
  11316. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11317. (org-fast-tag-insert "Current" current c-face "\n\n")
  11318. (org-fast-tag-show-exit exit-after-next)
  11319. (org-set-current-tags-overlay current ov-prefix)
  11320. (setq tbl fulltable char ?a cnt 0)
  11321. (while (setq e (pop tbl))
  11322. (cond
  11323. ((equal (car e) :startgroup)
  11324. (push '() groups) (setq ingroup t)
  11325. (when (not (= cnt 0))
  11326. (setq cnt 0)
  11327. (insert "\n"))
  11328. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11329. ((equal (car e) :endgroup)
  11330. (setq ingroup nil cnt 0)
  11331. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11332. ((equal e '(:newline))
  11333. (when (not (= cnt 0))
  11334. (setq cnt 0)
  11335. (insert "\n")
  11336. (setq e (car tbl))
  11337. (while (equal (car tbl) '(:newline))
  11338. (insert "\n")
  11339. (setq tbl (cdr tbl)))))
  11340. (t
  11341. (setq tg (copy-sequence (car e)) c2 nil)
  11342. (if (cdr e)
  11343. (setq c (cdr e))
  11344. ;; automatically assign a character.
  11345. (setq c1 (string-to-char
  11346. (downcase (substring
  11347. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11348. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11349. (while (or (rassoc char ntable) (rassoc char table))
  11350. (setq char (1+ char)))
  11351. (setq c2 c1))
  11352. (setq c (or c2 char)))
  11353. (if ingroup (push tg (car groups)))
  11354. (setq tg (org-add-props tg nil 'face
  11355. (cond
  11356. ((not (assoc tg table))
  11357. (org-get-todo-face tg))
  11358. ((member tg current) c-face)
  11359. ((member tg inherited) i-face)
  11360. (t nil))))
  11361. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11362. (insert "[" c "] " tg (make-string
  11363. (- fwidth 4 (length tg)) ?\ ))
  11364. (push (cons tg c) ntable)
  11365. (when (= (setq cnt (1+ cnt)) ncol)
  11366. (insert "\n")
  11367. (if ingroup (insert " "))
  11368. (setq cnt 0)))))
  11369. (setq ntable (nreverse ntable))
  11370. (insert "\n")
  11371. (goto-char (point-min))
  11372. (if (not expert) (org-fit-window-to-buffer))
  11373. (setq rtn
  11374. (catch 'exit
  11375. (while t
  11376. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11377. (if (not groups) "no " "")
  11378. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11379. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11380. (setq org-last-tag-selection-key c)
  11381. (cond
  11382. ((= c ?\r) (throw 'exit t))
  11383. ((= c ?!)
  11384. (setq groups (not groups))
  11385. (goto-char (point-min))
  11386. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11387. ((= c ?\C-c)
  11388. (if (not expert)
  11389. (org-fast-tag-show-exit
  11390. (setq exit-after-next (not exit-after-next)))
  11391. (setq expert nil)
  11392. (delete-other-windows)
  11393. (split-window-vertically)
  11394. (org-switch-to-buffer-other-window " *Org tags*")
  11395. (org-fit-window-to-buffer)))
  11396. ((or (= c ?\C-g)
  11397. (and (= c ?q) (not (rassoc c ntable))))
  11398. (org-detach-overlay org-tags-overlay)
  11399. (setq quit-flag t))
  11400. ((= c ?\ )
  11401. (setq current nil)
  11402. (if exit-after-next (setq exit-after-next 'now)))
  11403. ((= c ?\t)
  11404. (condition-case nil
  11405. (setq tg (org-icompleting-read
  11406. "Tag: "
  11407. (or buffer-tags
  11408. (with-current-buffer buf
  11409. (org-get-buffer-tags)))))
  11410. (quit (setq tg "")))
  11411. (when (string-match "\\S-" tg)
  11412. (add-to-list 'buffer-tags (list tg))
  11413. (if (member tg current)
  11414. (setq current (delete tg current))
  11415. (push tg current)))
  11416. (if exit-after-next (setq exit-after-next 'now)))
  11417. ((setq e (rassoc c todo-table) tg (car e))
  11418. (with-current-buffer buf
  11419. (save-excursion (org-todo tg)))
  11420. (if exit-after-next (setq exit-after-next 'now)))
  11421. ((setq e (rassoc c ntable) tg (car e))
  11422. (if (member tg current)
  11423. (setq current (delete tg current))
  11424. (loop for g in groups do
  11425. (if (member tg g)
  11426. (mapc (lambda (x)
  11427. (setq current (delete x current)))
  11428. g)))
  11429. (push tg current))
  11430. (if exit-after-next (setq exit-after-next 'now))))
  11431. ;; Create a sorted list
  11432. (setq current
  11433. (sort current
  11434. (lambda (a b)
  11435. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11436. (if (eq exit-after-next 'now) (throw 'exit t))
  11437. (goto-char (point-min))
  11438. (beginning-of-line 2)
  11439. (delete-region (point) (point-at-eol))
  11440. (org-fast-tag-insert "Current" current c-face)
  11441. (org-set-current-tags-overlay current ov-prefix)
  11442. (while (re-search-forward
  11443. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11444. (setq tg (match-string 1))
  11445. (add-text-properties
  11446. (match-beginning 1) (match-end 1)
  11447. (list 'face
  11448. (cond
  11449. ((member tg current) c-face)
  11450. ((member tg inherited) i-face)
  11451. (t (get-text-property (match-beginning 1) 'face))))))
  11452. (goto-char (point-min)))))
  11453. (org-detach-overlay org-tags-overlay)
  11454. (if rtn
  11455. (mapconcat 'identity current ":")
  11456. nil))))
  11457. (defun org-get-tags-string ()
  11458. "Get the TAGS string in the current headline."
  11459. (unless (org-on-heading-p t)
  11460. (error "Not on a heading"))
  11461. (save-excursion
  11462. (beginning-of-line 1)
  11463. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11464. (org-match-string-no-properties 1)
  11465. "")))
  11466. (defun org-get-tags ()
  11467. "Get the list of tags specified in the current headline."
  11468. (org-split-string (org-get-tags-string) ":"))
  11469. (defun org-get-buffer-tags ()
  11470. "Get a table of all tags used in the buffer, for completion."
  11471. (let (tags)
  11472. (save-excursion
  11473. (goto-char (point-min))
  11474. (while (re-search-forward
  11475. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11476. (when (equal (char-after (point-at-bol 0)) ?*)
  11477. (mapc (lambda (x) (add-to-list 'tags x))
  11478. (org-split-string (org-match-string-no-properties 1) ":")))))
  11479. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11480. (mapcar 'list tags)))
  11481. ;;;; The mapping API
  11482. ;;;###autoload
  11483. (defun org-map-entries (func &optional match scope &rest skip)
  11484. "Call FUNC at each headline selected by MATCH in SCOPE.
  11485. FUNC is a function or a lisp form. The function will be called without
  11486. arguments, with the cursor positioned at the beginning of the headline.
  11487. The return values of all calls to the function will be collected and
  11488. returned as a list.
  11489. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11490. does not need to preserve point. After evaluation, the cursor will be
  11491. moved to the end of the line (presumably of the headline of the
  11492. processed entry) and search continues from there. Under some
  11493. circumstances, this may not produce the wanted results. For example,
  11494. if you have removed (e.g. archived) the current (sub)tree it could
  11495. mean that the next entry will be skipped entirely. In such cases, you
  11496. can specify the position from where search should continue by making
  11497. FUNC set the variable `org-map-continue-from' to the desired buffer
  11498. position.
  11499. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11500. Only headlines that are matched by this query will be considered during
  11501. the iteration. When MATCH is nil or t, all headlines will be
  11502. visited by the iteration.
  11503. SCOPE determines the scope of this command. It can be any of:
  11504. nil The current buffer, respecting the restriction if any
  11505. tree The subtree started with the entry at point
  11506. file The current buffer, without restriction
  11507. file-with-archives
  11508. The current buffer, and any archives associated with it
  11509. agenda All agenda files
  11510. agenda-with-archives
  11511. All agenda files with any archive files associated with them
  11512. \(file1 file2 ...)
  11513. If this is a list, all files in the list will be scanned
  11514. The remaining args are treated as settings for the skipping facilities of
  11515. the scanner. The following items can be given here:
  11516. archive skip trees with the archive tag.
  11517. comment skip trees with the COMMENT keyword
  11518. function or Emacs Lisp form:
  11519. will be used as value for `org-agenda-skip-function', so whenever
  11520. the function returns t, FUNC will not be called for that
  11521. entry and search will continue from the point where the
  11522. function leaves it.
  11523. If your function needs to retrieve the tags including inherited tags
  11524. at the *current* entry, you can use the value of the variable
  11525. `org-scanner-tags' which will be much faster than getting the value
  11526. with `org-get-tags-at'. If your function gets properties with
  11527. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11528. to t around the call to `org-entry-properties' to get the same speedup.
  11529. Note that if your function moves around to retrieve tags and properties at
  11530. a *different* entry, you cannot use these techniques."
  11531. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11532. (org-agenda-skip-archived-trees (memq 'archive skip))
  11533. (org-agenda-skip-comment-trees (memq 'comment skip))
  11534. (org-agenda-skip-function
  11535. (car (org-delete-all '(comment archive) skip)))
  11536. (org-tags-match-list-sublevels t)
  11537. matcher file res
  11538. org-todo-keywords-for-agenda
  11539. org-done-keywords-for-agenda
  11540. org-todo-keyword-alist-for-agenda
  11541. org-drawers-for-agenda
  11542. org-tag-alist-for-agenda)
  11543. (cond
  11544. ((eq match t) (setq matcher t))
  11545. ((eq match nil) (setq matcher t))
  11546. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11547. (save-excursion
  11548. (save-restriction
  11549. (when (eq scope 'tree)
  11550. (org-back-to-heading t)
  11551. (org-narrow-to-subtree)
  11552. (setq scope nil))
  11553. (if (not scope)
  11554. (progn
  11555. (org-prepare-agenda-buffers
  11556. (list (buffer-file-name (current-buffer))))
  11557. (setq res (org-scan-tags func matcher)))
  11558. ;; Get the right scope
  11559. (cond
  11560. ((and scope (listp scope) (symbolp (car scope)))
  11561. (setq scope (eval scope)))
  11562. ((eq scope 'agenda)
  11563. (setq scope (org-agenda-files t)))
  11564. ((eq scope 'agenda-with-archives)
  11565. (setq scope (org-agenda-files t))
  11566. (setq scope (org-add-archive-files scope)))
  11567. ((eq scope 'file)
  11568. (setq scope (list (buffer-file-name))))
  11569. ((eq scope 'file-with-archives)
  11570. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11571. (org-prepare-agenda-buffers scope)
  11572. (while (setq file (pop scope))
  11573. (with-current-buffer (org-find-base-buffer-visiting file)
  11574. (save-excursion
  11575. (save-restriction
  11576. (widen)
  11577. (goto-char (point-min))
  11578. (setq res (append res (org-scan-tags func matcher))))))))))
  11579. res))
  11580. ;;;; Properties
  11581. ;;; Setting and retrieving properties
  11582. (defconst org-special-properties
  11583. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11584. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11585. "The special properties valid in Org-mode.
  11586. These are properties that are not defined in the property drawer,
  11587. but in some other way.")
  11588. (defconst org-default-properties
  11589. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11590. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11591. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11592. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11593. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  11594. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11595. "Some properties that are used by Org-mode for various purposes.
  11596. Being in this list makes sure that they are offered for completion.")
  11597. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11598. "Regular expression matching the first line of a property drawer.")
  11599. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11600. "Regular expression matching the last line of a property drawer.")
  11601. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11602. "Regular expression matching the first line of a property drawer.")
  11603. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11604. "Regular expression matching the first line of a property drawer.")
  11605. (defconst org-property-drawer-re
  11606. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11607. org-property-end-re "\\)\n?")
  11608. "Matches an entire property drawer.")
  11609. (defconst org-clock-drawer-re
  11610. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11611. org-property-end-re "\\)\n?")
  11612. "Matches an entire clock drawer.")
  11613. (defun org-property-action ()
  11614. "Do an action on properties."
  11615. (interactive)
  11616. (let (c)
  11617. (org-at-property-p)
  11618. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11619. (setq c (read-char-exclusive))
  11620. (cond
  11621. ((equal c ?s)
  11622. (call-interactively 'org-set-property))
  11623. ((equal c ?d)
  11624. (call-interactively 'org-delete-property))
  11625. ((equal c ?D)
  11626. (call-interactively 'org-delete-property-globally))
  11627. ((equal c ?c)
  11628. (call-interactively 'org-compute-property-at-point))
  11629. (t (error "No such property action %c" c)))))
  11630. (defun org-set-effort (&optional value)
  11631. "Set the effort property of the current entry.
  11632. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11633. allowed value."
  11634. (interactive "P")
  11635. (if (equal value 0) (setq value 10))
  11636. (let* ((completion-ignore-case t)
  11637. (prop org-effort-property)
  11638. (cur (org-entry-get nil prop))
  11639. (allowed (org-property-get-allowed-values nil prop 'table))
  11640. (existing (mapcar 'list (org-property-values prop)))
  11641. rpl
  11642. (val (cond
  11643. ((stringp value) value)
  11644. ((and allowed (integerp value))
  11645. (or (car (nth (1- value) allowed))
  11646. (car (org-last allowed))))
  11647. (allowed
  11648. (message "Select 1-9,0, [RET%s]: %s"
  11649. (if cur (concat "=" cur) "")
  11650. (mapconcat 'car allowed " "))
  11651. (setq rpl (read-char-exclusive))
  11652. (if (equal rpl ?\r)
  11653. cur
  11654. (setq rpl (- rpl ?0))
  11655. (if (equal rpl 0) (setq rpl 10))
  11656. (if (and (> rpl 0) (<= rpl (length allowed)))
  11657. (car (nth (1- rpl) allowed))
  11658. (org-completing-read "Effort: " allowed nil))))
  11659. (t
  11660. (let (org-completion-use-ido org-completion-use-iswitchb)
  11661. (org-completing-read
  11662. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11663. (concat "[" cur "]") "")
  11664. ": ")
  11665. existing nil nil "" nil cur))))))
  11666. (unless (equal (org-entry-get nil prop) val)
  11667. (org-entry-put nil prop val))
  11668. (message "%s is now %s" prop val)))
  11669. (defun org-at-property-p ()
  11670. "Is cursor inside a property drawer?"
  11671. (save-excursion
  11672. (beginning-of-line 1)
  11673. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11674. (let ((match (match-data)) ;; Keep match-data for use by calling
  11675. (p (point)) ;; procedures.
  11676. (range (unless (org-before-first-heading-p)
  11677. (org-get-property-block))))
  11678. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11679. (set-match-data match))))))
  11680. (defun org-get-property-block (&optional beg end force)
  11681. "Return the (beg . end) range of the body of the property drawer.
  11682. BEG and END can be beginning and end of subtree, if not given
  11683. they will be found.
  11684. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11685. (catch 'exit
  11686. (save-excursion
  11687. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11688. (end (or end (progn (outline-next-heading) (point)))))
  11689. (goto-char beg)
  11690. (if (re-search-forward org-property-start-re end t)
  11691. (setq beg (1+ (match-end 0)))
  11692. (if force
  11693. (save-excursion
  11694. (org-insert-property-drawer)
  11695. (setq end (progn (outline-next-heading) (point))))
  11696. (throw 'exit nil))
  11697. (goto-char beg)
  11698. (if (re-search-forward org-property-start-re end t)
  11699. (setq beg (1+ (match-end 0)))))
  11700. (if (re-search-forward org-property-end-re end t)
  11701. (setq end (match-beginning 0))
  11702. (or force (throw 'exit nil))
  11703. (goto-char beg)
  11704. (setq end beg)
  11705. (org-indent-line-function)
  11706. (insert ":END:\n"))
  11707. (cons beg end)))))
  11708. (defun org-entry-properties (&optional pom which specific)
  11709. "Get all properties of the entry at point-or-marker POM.
  11710. This includes the TODO keyword, the tags, time strings for deadline,
  11711. scheduled, and clocking, and any additional properties defined in the
  11712. entry. The return value is an alist, keys may occur multiple times
  11713. if the property key was used several times.
  11714. POM may also be nil, in which case the current entry is used.
  11715. If WHICH is nil or `all', get all properties. If WHICH is
  11716. `special' or `standard', only get that subclass. If WHICH
  11717. is a string only get exactly this property. Specific can be a string, the
  11718. specific property we are interested in. Specifying it can speed
  11719. things up because then unnecessary parsing is avoided."
  11720. (setq which (or which 'all))
  11721. (org-with-point-at pom
  11722. (let ((clockstr (substring org-clock-string 0 -1))
  11723. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11724. (case-fold-search nil)
  11725. beg end range props sum-props key value string clocksum)
  11726. (save-excursion
  11727. (when (condition-case nil
  11728. (and (org-mode-p) (org-back-to-heading t))
  11729. (error nil))
  11730. (setq beg (point))
  11731. (setq sum-props (get-text-property (point) 'org-summaries))
  11732. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11733. (outline-next-heading)
  11734. (setq end (point))
  11735. (when (memq which '(all special))
  11736. ;; Get the special properties, like TODO and tags
  11737. (goto-char beg)
  11738. (when (and (or (not specific) (string= specific "TODO"))
  11739. (looking-at org-todo-line-regexp) (match-end 2))
  11740. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11741. (when (and (or (not specific) (string= specific "PRIORITY"))
  11742. (looking-at org-priority-regexp))
  11743. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11744. (when (and (or (not specific) (string= specific "TAGS"))
  11745. (setq value (org-get-tags-string))
  11746. (string-match "\\S-" value))
  11747. (push (cons "TAGS" value) props))
  11748. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11749. (setq value (org-get-tags-at)))
  11750. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11751. ":"))
  11752. props))
  11753. (when (or (not specific) (string= specific "BLOCKED"))
  11754. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11755. (when (or (not specific)
  11756. (member specific org-all-time-keywords)
  11757. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11758. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11759. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11760. string (if (equal key clockstr)
  11761. (org-no-properties
  11762. (org-trim
  11763. (buffer-substring
  11764. (match-beginning 3) (goto-char (point-at-eol)))))
  11765. (substring (org-match-string-no-properties 3) 1 -1)))
  11766. (unless key
  11767. (if (= (char-after (match-beginning 3)) ?\[)
  11768. (setq key "TIMESTAMP_IA")
  11769. (setq key "TIMESTAMP")))
  11770. (when (or (equal key clockstr) (not (assoc key props)))
  11771. (push (cons key string) props))))
  11772. )
  11773. (when (memq which '(all standard))
  11774. ;; Get the standard properties, like :PROP: ...
  11775. (setq range (org-get-property-block beg end))
  11776. (when range
  11777. (goto-char (car range))
  11778. (while (re-search-forward
  11779. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11780. (cdr range) t)
  11781. (setq key (org-match-string-no-properties 1)
  11782. value (org-trim (or (org-match-string-no-properties 2) "")))
  11783. (unless (member key excluded)
  11784. (push (cons key (or value "")) props)))))
  11785. (if clocksum
  11786. (push (cons "CLOCKSUM"
  11787. (org-columns-number-to-string (/ (float clocksum) 60.)
  11788. 'add_times))
  11789. props))
  11790. (unless (assoc "CATEGORY" props)
  11791. (setq value (or (org-get-category)
  11792. (progn (org-refresh-category-properties)
  11793. (org-get-category))))
  11794. (push (cons "CATEGORY" value) props))
  11795. (append sum-props (nreverse props)))))))
  11796. (defun org-entry-get (pom property &optional inherit)
  11797. "Get value of PROPERTY for entry at point-or-marker POM.
  11798. If INHERIT is non-nil and the entry does not have the property,
  11799. then also check higher levels of the hierarchy.
  11800. If INHERIT is the symbol `selective', use inheritance only if the setting
  11801. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11802. If the property is present but empty, the return value is the empty string.
  11803. If the property is not present at all, nil is returned."
  11804. (org-with-point-at pom
  11805. (if (and inherit (if (eq inherit 'selective)
  11806. (org-property-inherit-p property)
  11807. t))
  11808. (org-entry-get-with-inheritance property)
  11809. (if (member property org-special-properties)
  11810. ;; We need a special property. Use `org-entry-properties' to
  11811. ;; retrieve it, but specify the wanted property
  11812. (cdr (assoc property (org-entry-properties nil 'special property)))
  11813. (let ((range (org-get-property-block)))
  11814. (if (and range
  11815. (goto-char (car range))
  11816. (re-search-forward
  11817. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11818. (cdr range) t))
  11819. ;; Found the property, return it.
  11820. (if (match-end 1)
  11821. (org-match-string-no-properties 1)
  11822. "")))))))
  11823. (defun org-property-or-variable-value (var &optional inherit)
  11824. "Check if there is a property fixing the value of VAR.
  11825. If yes, return this value. If not, return the current value of the variable."
  11826. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11827. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11828. (read prop)
  11829. (symbol-value var))))
  11830. (defun org-entry-delete (pom property)
  11831. "Delete the property PROPERTY from entry at point-or-marker POM."
  11832. (org-with-point-at pom
  11833. (if (member property org-special-properties)
  11834. nil ; cannot delete these properties.
  11835. (let ((range (org-get-property-block)))
  11836. (if (and range
  11837. (goto-char (car range))
  11838. (re-search-forward
  11839. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11840. (cdr range) t))
  11841. (progn
  11842. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11843. t)
  11844. nil)))))
  11845. ;; Multi-values properties are properties that contain multiple values
  11846. ;; These values are assumed to be single words, separated by whitespace.
  11847. (defun org-entry-add-to-multivalued-property (pom property value)
  11848. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11849. (let* ((old (org-entry-get pom property))
  11850. (values (and old (org-split-string old "[ \t]"))))
  11851. (setq value (org-entry-protect-space value))
  11852. (unless (member value values)
  11853. (setq values (cons value values))
  11854. (org-entry-put pom property
  11855. (mapconcat 'identity values " ")))))
  11856. (defun org-entry-remove-from-multivalued-property (pom property value)
  11857. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11858. (let* ((old (org-entry-get pom property))
  11859. (values (and old (org-split-string old "[ \t]"))))
  11860. (setq value (org-entry-protect-space value))
  11861. (when (member value values)
  11862. (setq values (delete value values))
  11863. (org-entry-put pom property
  11864. (mapconcat 'identity values " ")))))
  11865. (defun org-entry-member-in-multivalued-property (pom property value)
  11866. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11867. (let* ((old (org-entry-get pom property))
  11868. (values (and old (org-split-string old "[ \t]"))))
  11869. (setq value (org-entry-protect-space value))
  11870. (member value values)))
  11871. (defun org-entry-get-multivalued-property (pom property)
  11872. "Return a list of values in a multivalued property."
  11873. (let* ((value (org-entry-get pom property))
  11874. (values (and value (org-split-string value "[ \t]"))))
  11875. (mapcar 'org-entry-restore-space values)))
  11876. (defun org-entry-put-multivalued-property (pom property &rest values)
  11877. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11878. VALUES should be a list of strings. Spaces will be protected."
  11879. (org-entry-put pom property
  11880. (mapconcat 'org-entry-protect-space values " "))
  11881. (let* ((value (org-entry-get pom property))
  11882. (values (and value (org-split-string value "[ \t]"))))
  11883. (mapcar 'org-entry-restore-space values)))
  11884. (defun org-entry-protect-space (s)
  11885. "Protect spaces and newline in string S."
  11886. (while (string-match " " s)
  11887. (setq s (replace-match "%20" t t s)))
  11888. (while (string-match "\n" s)
  11889. (setq s (replace-match "%0A" t t s)))
  11890. s)
  11891. (defun org-entry-restore-space (s)
  11892. "Restore spaces and newline in string S."
  11893. (while (string-match "%20" s)
  11894. (setq s (replace-match " " t t s)))
  11895. (while (string-match "%0A" s)
  11896. (setq s (replace-match "\n" t t s)))
  11897. s)
  11898. (defvar org-entry-property-inherited-from (make-marker)
  11899. "Marker pointing to the entry from where a property was inherited.
  11900. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11901. location of the entry where the inheritance search matched. If there was
  11902. no match, the marker will point nowhere.
  11903. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11904. is set.")
  11905. (defun org-entry-get-with-inheritance (property)
  11906. "Get entry property, and search higher levels if not present."
  11907. (move-marker org-entry-property-inherited-from nil)
  11908. (let (tmp)
  11909. (save-excursion
  11910. (save-restriction
  11911. (widen)
  11912. (catch 'ex
  11913. (while t
  11914. (when (setq tmp (org-entry-get nil property))
  11915. (org-back-to-heading t)
  11916. (move-marker org-entry-property-inherited-from (point))
  11917. (throw 'ex tmp))
  11918. (or (org-up-heading-safe) (throw 'ex nil)))))
  11919. (or tmp
  11920. (cdr (assoc property org-file-properties))
  11921. (cdr (assoc property org-global-properties))
  11922. (cdr (assoc property org-global-properties-fixed))))))
  11923. (defvar org-property-changed-functions nil
  11924. "Hook called when the value of a property has changed.
  11925. Each hook function should accept two arguments, the name of the property
  11926. and the new value.")
  11927. (defun org-entry-put (pom property value)
  11928. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11929. (org-with-point-at pom
  11930. (org-back-to-heading t)
  11931. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11932. range)
  11933. (cond
  11934. ((equal property "TODO")
  11935. (when (and (stringp value) (string-match "\\S-" value)
  11936. (not (member value org-todo-keywords-1)))
  11937. (error "\"%s\" is not a valid TODO state" value))
  11938. (if (or (not value)
  11939. (not (string-match "\\S-" value)))
  11940. (setq value 'none))
  11941. (org-todo value)
  11942. (org-set-tags nil 'align))
  11943. ((equal property "PRIORITY")
  11944. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11945. (string-to-char value) ?\ ))
  11946. (org-set-tags nil 'align))
  11947. ((equal property "SCHEDULED")
  11948. (if (re-search-forward org-scheduled-time-regexp end t)
  11949. (cond
  11950. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11951. ((eq value 'later) (org-timestamp-change 1 'day))
  11952. (t (call-interactively 'org-schedule)))
  11953. (call-interactively 'org-schedule)))
  11954. ((equal property "DEADLINE")
  11955. (if (re-search-forward org-deadline-time-regexp end t)
  11956. (cond
  11957. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11958. ((eq value 'later) (org-timestamp-change 1 'day))
  11959. (t (call-interactively 'org-deadline)))
  11960. (call-interactively 'org-deadline)))
  11961. ((member property org-special-properties)
  11962. (error "The %s property can not yet be set with `org-entry-put'"
  11963. property))
  11964. (t ; a non-special property
  11965. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11966. (setq range (org-get-property-block beg end 'force))
  11967. (goto-char (car range))
  11968. (if (re-search-forward
  11969. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11970. (progn
  11971. (delete-region (match-beginning 1) (match-end 1))
  11972. (goto-char (match-beginning 1)))
  11973. (goto-char (cdr range))
  11974. (insert "\n")
  11975. (backward-char 1)
  11976. (org-indent-line-function)
  11977. (insert ":" property ":"))
  11978. (and value (insert " " value))
  11979. (org-indent-line-function)))))
  11980. (run-hook-with-args 'org-property-changed-functions property value)))
  11981. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11982. "Get all property keys in the current buffer.
  11983. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11984. like tags and TODO state.
  11985. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11986. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11987. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11988. formats in the current buffer."
  11989. (let (rtn range cfmt s p)
  11990. (save-excursion
  11991. (save-restriction
  11992. (widen)
  11993. (goto-char (point-min))
  11994. (while (re-search-forward org-property-start-re nil t)
  11995. (setq range (org-get-property-block))
  11996. (goto-char (car range))
  11997. (while (re-search-forward
  11998. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11999. (cdr range) t)
  12000. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12001. (outline-next-heading))))
  12002. (when include-specials
  12003. (setq rtn (append org-special-properties rtn)))
  12004. (when include-defaults
  12005. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12006. (add-to-list 'rtn org-effort-property))
  12007. (when include-columns
  12008. (save-excursion
  12009. (save-restriction
  12010. (widen)
  12011. (goto-char (point-min))
  12012. (while (re-search-forward
  12013. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12014. nil t)
  12015. (setq cfmt (match-string 2) s 0)
  12016. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12017. cfmt s)
  12018. (setq s (match-end 0)
  12019. p (match-string 1 cfmt))
  12020. (unless (or (equal p "ITEM")
  12021. (member p org-special-properties))
  12022. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12023. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12024. (defun org-property-values (key)
  12025. "Return a list of all values of property KEY."
  12026. (save-excursion
  12027. (save-restriction
  12028. (widen)
  12029. (goto-char (point-min))
  12030. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  12031. values)
  12032. (while (re-search-forward re nil t)
  12033. (add-to-list 'values (org-trim (match-string 1))))
  12034. (delete "" values)))))
  12035. (defun org-insert-property-drawer ()
  12036. "Insert a property drawer into the current entry."
  12037. (interactive)
  12038. (org-back-to-heading t)
  12039. (looking-at outline-regexp)
  12040. (let ((indent (if org-adapt-indentation
  12041. (- (match-end 0)(match-beginning 0))
  12042. 0))
  12043. (beg (point))
  12044. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12045. end hiddenp)
  12046. (outline-next-heading)
  12047. (setq end (point))
  12048. (goto-char beg)
  12049. (while (re-search-forward re end t))
  12050. (setq hiddenp (org-invisible-p))
  12051. (end-of-line 1)
  12052. (and (equal (char-after) ?\n) (forward-char 1))
  12053. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12054. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12055. ;; just skip this line
  12056. (beginning-of-line 2)
  12057. ;; Drawer start, find the end
  12058. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12059. (beginning-of-line 1)))
  12060. (org-skip-over-state-notes)
  12061. (skip-chars-backward " \t\n\r")
  12062. (if (eq (char-before) ?*) (forward-char 1))
  12063. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12064. (beginning-of-line 0)
  12065. (org-indent-to-column indent)
  12066. (beginning-of-line 2)
  12067. (org-indent-to-column indent)
  12068. (beginning-of-line 0)
  12069. (if hiddenp
  12070. (save-excursion
  12071. (org-back-to-heading t)
  12072. (hide-entry))
  12073. (org-flag-drawer t))))
  12074. (defun org-set-property (property value)
  12075. "In the current entry, set PROPERTY to VALUE.
  12076. When called interactively, this will prompt for a property name, offering
  12077. completion on existing and default properties. And then it will prompt
  12078. for a value, offering completion either on allowed values (via an inherited
  12079. xxx_ALL property) or on existing values in other instances of this property
  12080. in the current file."
  12081. (interactive
  12082. (let* ((completion-ignore-case t)
  12083. (keys (org-buffer-property-keys nil t t))
  12084. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12085. (prop (if (member prop0 keys)
  12086. prop0
  12087. (or (cdr (assoc (downcase prop0)
  12088. (mapcar (lambda (x) (cons (downcase x) x))
  12089. keys)))
  12090. prop0)))
  12091. (cur (org-entry-get nil prop))
  12092. (prompt (concat prop " value"
  12093. (if (and cur (string-match "\\S-" cur))
  12094. (concat " [" cur "]") "") ": "))
  12095. (allowed (org-property-get-allowed-values nil prop 'table))
  12096. (existing (mapcar 'list (org-property-values prop)))
  12097. (val (if allowed
  12098. (org-completing-read prompt allowed nil
  12099. (not (get-text-property 0 'org-unrestricted
  12100. (caar allowed))))
  12101. (let (org-completion-use-ido org-completion-use-iswitchb)
  12102. (org-completing-read prompt existing nil nil "" nil cur)))))
  12103. (list prop (if (equal val "") cur val))))
  12104. (unless (equal (org-entry-get nil property) value)
  12105. (org-entry-put nil property value)))
  12106. (defun org-delete-property (property)
  12107. "In the current entry, delete PROPERTY."
  12108. (interactive
  12109. (let* ((completion-ignore-case t)
  12110. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12111. (list prop)))
  12112. (message "Property %s %s" property
  12113. (if (org-entry-delete nil property)
  12114. "deleted"
  12115. "was not present in the entry")))
  12116. (defun org-delete-property-globally (property)
  12117. "Remove PROPERTY globally, from all entries."
  12118. (interactive
  12119. (let* ((completion-ignore-case t)
  12120. (prop (org-icompleting-read
  12121. "Globally remove property: "
  12122. (mapcar 'list (org-buffer-property-keys)))))
  12123. (list prop)))
  12124. (save-excursion
  12125. (save-restriction
  12126. (widen)
  12127. (goto-char (point-min))
  12128. (let ((cnt 0))
  12129. (while (re-search-forward
  12130. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12131. nil t)
  12132. (setq cnt (1+ cnt))
  12133. (replace-match ""))
  12134. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12135. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12136. (defun org-compute-property-at-point ()
  12137. "Compute the property at point.
  12138. This looks for an enclosing column format, extracts the operator and
  12139. then applies it to the property in the column format's scope."
  12140. (interactive)
  12141. (unless (org-at-property-p)
  12142. (error "Not at a property"))
  12143. (let ((prop (org-match-string-no-properties 2)))
  12144. (org-columns-get-format-and-top-level)
  12145. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12146. (error "No operator defined for property %s" prop))
  12147. (org-columns-compute prop)))
  12148. (defvar org-property-allowed-value-functions nil
  12149. "Hook for functions supplying allowed values for a specific property.
  12150. The functions must take a single argument, the name of the property, and
  12151. return a flat list of allowed values. If \":ETC\" is one of
  12152. the values, this means that these values are intended as defaults for
  12153. completion, but that other values should be allowed too.
  12154. The functions must return nil if they are not responsible for this
  12155. property.")
  12156. (defun org-property-get-allowed-values (pom property &optional table)
  12157. "Get allowed values for the property PROPERTY.
  12158. When TABLE is non-nil, return an alist that can directly be used for
  12159. completion."
  12160. (let (vals)
  12161. (cond
  12162. ((equal property "TODO")
  12163. (setq vals (org-with-point-at pom
  12164. (append org-todo-keywords-1 '("")))))
  12165. ((equal property "PRIORITY")
  12166. (let ((n org-lowest-priority))
  12167. (while (>= n org-highest-priority)
  12168. (push (char-to-string n) vals)
  12169. (setq n (1- n)))))
  12170. ((member property org-special-properties))
  12171. ((setq vals (run-hook-with-args-until-success
  12172. 'org-property-allowed-value-functions property)))
  12173. (t
  12174. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12175. (when (and vals (string-match "\\S-" vals))
  12176. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12177. (setq vals (mapcar (lambda (x)
  12178. (cond ((stringp x) x)
  12179. ((numberp x) (number-to-string x))
  12180. ((symbolp x) (symbol-name x))
  12181. (t "???")))
  12182. vals)))))
  12183. (when (member ":ETC" vals)
  12184. (setq vals (remove ":ETC" vals))
  12185. (org-add-props (car vals) '(org-unrestricted t)))
  12186. (if table (mapcar 'list vals) vals)))
  12187. (defun org-property-previous-allowed-value (&optional previous)
  12188. "Switch to the next allowed value for this property."
  12189. (interactive)
  12190. (org-property-next-allowed-value t))
  12191. (defun org-property-next-allowed-value (&optional previous)
  12192. "Switch to the next allowed value for this property."
  12193. (interactive)
  12194. (unless (org-at-property-p)
  12195. (error "Not at a property"))
  12196. (let* ((key (match-string 2))
  12197. (value (match-string 3))
  12198. (allowed (or (org-property-get-allowed-values (point) key)
  12199. (and (member value '("[ ]" "[-]" "[X]"))
  12200. '("[ ]" "[X]"))))
  12201. nval)
  12202. (unless allowed
  12203. (error "Allowed values for this property have not been defined"))
  12204. (if previous (setq allowed (reverse allowed)))
  12205. (if (member value allowed)
  12206. (setq nval (car (cdr (member value allowed)))))
  12207. (setq nval (or nval (car allowed)))
  12208. (if (equal nval value)
  12209. (error "Only one allowed value for this property"))
  12210. (org-at-property-p)
  12211. (replace-match (concat " :" key ": " nval) t t)
  12212. (org-indent-line-function)
  12213. (beginning-of-line 1)
  12214. (skip-chars-forward " \t")
  12215. (run-hook-with-args 'org-property-changed-functions key nval)))
  12216. (defun org-find-entry-with-id (ident)
  12217. "Locate the entry that contains the ID property with exact value IDENT.
  12218. IDENT can be a string, a symbol or a number, this function will search for
  12219. the string representation of it.
  12220. Return the position where this entry starts, or nil if there is no such entry."
  12221. (interactive "sID: ")
  12222. (let ((id (cond
  12223. ((stringp ident) ident)
  12224. ((symbol-name ident) (symbol-name ident))
  12225. ((numberp ident) (number-to-string ident))
  12226. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12227. (case-fold-search nil))
  12228. (save-excursion
  12229. (save-restriction
  12230. (widen)
  12231. (goto-char (point-min))
  12232. (when (re-search-forward
  12233. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12234. nil t)
  12235. (org-back-to-heading t)
  12236. (point))))))
  12237. ;;;; Timestamps
  12238. (defvar org-last-changed-timestamp nil)
  12239. (defvar org-last-inserted-timestamp nil
  12240. "The last time stamp inserted with `org-insert-time-stamp'.")
  12241. (defvar org-time-was-given) ; dynamically scoped parameter
  12242. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12243. (defvar org-ts-what) ; dynamically scoped parameter
  12244. (defun org-time-stamp (arg &optional inactive)
  12245. "Prompt for a date/time and insert a time stamp.
  12246. If the user specifies a time like HH:MM, or if this command is called
  12247. with a prefix argument, the time stamp will contain date and time.
  12248. Otherwise, only the date will be included. All parts of a date not
  12249. specified by the user will be filled in from the current date/time.
  12250. So if you press just return without typing anything, the time stamp
  12251. will represent the current date/time. If there is already a timestamp
  12252. at the cursor, it will be modified."
  12253. (interactive "P")
  12254. (let* ((ts nil)
  12255. (default-time
  12256. ;; Default time is either today, or, when entering a range,
  12257. ;; the range start.
  12258. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12259. (save-excursion
  12260. (re-search-backward
  12261. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12262. (- (point) 20) t)))
  12263. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12264. (current-time)))
  12265. (default-input (and ts (org-get-compact-tod ts)))
  12266. org-time-was-given org-end-time-was-given time)
  12267. (cond
  12268. ((and (org-at-timestamp-p t)
  12269. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12270. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12271. (insert "--")
  12272. (setq time (let ((this-command this-command))
  12273. (org-read-date arg 'totime nil nil
  12274. default-time default-input)))
  12275. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12276. ((org-at-timestamp-p t)
  12277. (setq time (let ((this-command this-command))
  12278. (org-read-date arg 'totime nil nil default-time default-input)))
  12279. (when (org-at-timestamp-p t) ; just to get the match data
  12280. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12281. (replace-match "")
  12282. (setq org-last-changed-timestamp
  12283. (org-insert-time-stamp
  12284. time (or org-time-was-given arg)
  12285. inactive nil nil (list org-end-time-was-given))))
  12286. (message "Timestamp updated"))
  12287. (t
  12288. (setq time (let ((this-command this-command))
  12289. (org-read-date arg 'totime nil nil default-time default-input)))
  12290. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12291. nil nil (list org-end-time-was-given))))))
  12292. ;; FIXME: can we use this for something else, like computing time differences?
  12293. (defun org-get-compact-tod (s)
  12294. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12295. (let* ((t1 (match-string 1 s))
  12296. (h1 (string-to-number (match-string 2 s)))
  12297. (m1 (string-to-number (match-string 3 s)))
  12298. (t2 (and (match-end 4) (match-string 5 s)))
  12299. (h2 (and t2 (string-to-number (match-string 6 s))))
  12300. (m2 (and t2 (string-to-number (match-string 7 s))))
  12301. dh dm)
  12302. (if (not t2)
  12303. t1
  12304. (setq dh (- h2 h1) dm (- m2 m1))
  12305. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12306. (concat t1 "+" (number-to-string dh)
  12307. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12308. (defun org-time-stamp-inactive (&optional arg)
  12309. "Insert an inactive time stamp.
  12310. An inactive time stamp is enclosed in square brackets instead of angle
  12311. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12312. does not link to the calendar and cannot be changed with the S-cursor keys.
  12313. So these are more for recording a certain time/date."
  12314. (interactive "P")
  12315. (org-time-stamp arg 'inactive))
  12316. (defvar org-date-ovl (make-overlay 1 1))
  12317. (overlay-put org-date-ovl 'face 'org-warning)
  12318. (org-detach-overlay org-date-ovl)
  12319. (defvar org-ans1) ; dynamically scoped parameter
  12320. (defvar org-ans2) ; dynamically scoped parameter
  12321. (defvar org-plain-time-of-day-regexp) ; defined below
  12322. (defvar org-overriding-default-time nil) ; dynamically scoped
  12323. (defvar org-read-date-overlay nil)
  12324. (defvar org-dcst nil) ; dynamically scoped
  12325. (defvar org-read-date-history nil)
  12326. (defvar org-read-date-final-answer nil)
  12327. (defun org-read-date (&optional with-time to-time from-string prompt
  12328. default-time default-input)
  12329. "Read a date, possibly a time, and make things smooth for the user.
  12330. The prompt will suggest to enter an ISO date, but you can also enter anything
  12331. which will at least partially be understood by `parse-time-string'.
  12332. Unrecognized parts of the date will default to the current day, month, year,
  12333. hour and minute. If this command is called to replace a timestamp at point,
  12334. of to enter the second timestamp of a range, the default time is taken from the
  12335. existing stamp. For example,
  12336. 3-2-5 --> 2003-02-05
  12337. feb 15 --> currentyear-02-15
  12338. sep 12 9 --> 2009-09-12
  12339. 12:45 --> today 12:45
  12340. 22 sept 0:34 --> currentyear-09-22 0:34
  12341. 12 --> currentyear-currentmonth-12
  12342. Fri --> nearest Friday (today or later)
  12343. etc.
  12344. Furthermore you can specify a relative date by giving, as the *first* thing
  12345. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12346. change in days weeks, months, years.
  12347. With a single plus or minus, the date is relative to today. With a double
  12348. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12349. +4d --> four days from today
  12350. +4 --> same as above
  12351. +2w --> two weeks from today
  12352. ++5 --> five days from default date
  12353. The function understands only English month and weekday abbreviations,
  12354. but this can be configured with the variables `parse-time-months' and
  12355. `parse-time-weekdays'.
  12356. While prompting, a calendar is popped up - you can also select the
  12357. date with the mouse (button 1). The calendar shows a period of three
  12358. months. To scroll it to other months, use the keys `>' and `<'.
  12359. If you don't like the calendar, turn it off with
  12360. \(setq org-read-date-popup-calendar nil)
  12361. With optional argument TO-TIME, the date will immediately be converted
  12362. to an internal time.
  12363. With an optional argument WITH-TIME, the prompt will suggest to also
  12364. insert a time. Note that when WITH-TIME is not set, you can still
  12365. enter a time, and this function will inform the calling routine about
  12366. this change. The calling routine may then choose to change the format
  12367. used to insert the time stamp into the buffer to include the time.
  12368. With optional argument FROM-STRING, read from this string instead from
  12369. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12370. the time/date that is used for everything that is not specified by the
  12371. user."
  12372. (require 'parse-time)
  12373. (let* ((org-time-stamp-rounding-minutes
  12374. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12375. (org-dcst org-display-custom-times)
  12376. (ct (org-current-time))
  12377. (def (or org-overriding-default-time default-time ct))
  12378. (defdecode (decode-time def))
  12379. (dummy (progn
  12380. (when (< (nth 2 defdecode) org-extend-today-until)
  12381. (setcar (nthcdr 2 defdecode) -1)
  12382. (setcar (nthcdr 1 defdecode) 59)
  12383. (setq def (apply 'encode-time defdecode)
  12384. defdecode (decode-time def)))))
  12385. (calendar-frame-setup nil)
  12386. (calendar-move-hook nil)
  12387. (calendar-view-diary-initially-flag nil)
  12388. (view-diary-entries-initially nil)
  12389. (calendar-view-holidays-initially-flag nil)
  12390. (view-calendar-holidays-initially nil)
  12391. (timestr (format-time-string
  12392. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12393. (prompt (concat (if prompt (concat prompt " ") "")
  12394. (format "Date+time [%s]: " timestr)))
  12395. ans (org-ans0 "") org-ans1 org-ans2 final)
  12396. (cond
  12397. (from-string (setq ans from-string))
  12398. (org-read-date-popup-calendar
  12399. (save-excursion
  12400. (save-window-excursion
  12401. (calendar)
  12402. (calendar-forward-day (- (time-to-days def)
  12403. (calendar-absolute-from-gregorian
  12404. (calendar-current-date))))
  12405. (org-eval-in-calendar nil t)
  12406. (let* ((old-map (current-local-map))
  12407. (map (copy-keymap calendar-mode-map))
  12408. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12409. (org-defkey map (kbd "RET") 'org-calendar-select)
  12410. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12411. 'org-calendar-select-mouse)
  12412. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12413. 'org-calendar-select-mouse)
  12414. (org-defkey minibuffer-local-map [(meta shift left)]
  12415. (lambda () (interactive)
  12416. (org-eval-in-calendar '(calendar-backward-month 1))))
  12417. (org-defkey minibuffer-local-map [(meta shift right)]
  12418. (lambda () (interactive)
  12419. (org-eval-in-calendar '(calendar-forward-month 1))))
  12420. (org-defkey minibuffer-local-map [(meta shift up)]
  12421. (lambda () (interactive)
  12422. (org-eval-in-calendar '(calendar-backward-year 1))))
  12423. (org-defkey minibuffer-local-map [(meta shift down)]
  12424. (lambda () (interactive)
  12425. (org-eval-in-calendar '(calendar-forward-year 1))))
  12426. (org-defkey minibuffer-local-map [?\e (shift left)]
  12427. (lambda () (interactive)
  12428. (org-eval-in-calendar '(calendar-backward-month 1))))
  12429. (org-defkey minibuffer-local-map [?\e (shift right)]
  12430. (lambda () (interactive)
  12431. (org-eval-in-calendar '(calendar-forward-month 1))))
  12432. (org-defkey minibuffer-local-map [?\e (shift up)]
  12433. (lambda () (interactive)
  12434. (org-eval-in-calendar '(calendar-backward-year 1))))
  12435. (org-defkey minibuffer-local-map [?\e (shift down)]
  12436. (lambda () (interactive)
  12437. (org-eval-in-calendar '(calendar-forward-year 1))))
  12438. (org-defkey minibuffer-local-map [(shift up)]
  12439. (lambda () (interactive)
  12440. (org-eval-in-calendar '(calendar-backward-week 1))))
  12441. (org-defkey minibuffer-local-map [(shift down)]
  12442. (lambda () (interactive)
  12443. (org-eval-in-calendar '(calendar-forward-week 1))))
  12444. (org-defkey minibuffer-local-map [(shift left)]
  12445. (lambda () (interactive)
  12446. (org-eval-in-calendar '(calendar-backward-day 1))))
  12447. (org-defkey minibuffer-local-map [(shift right)]
  12448. (lambda () (interactive)
  12449. (org-eval-in-calendar '(calendar-forward-day 1))))
  12450. (org-defkey minibuffer-local-map ">"
  12451. (lambda () (interactive)
  12452. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12453. (org-defkey minibuffer-local-map "<"
  12454. (lambda () (interactive)
  12455. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12456. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12457. (unwind-protect
  12458. (progn
  12459. (use-local-map map)
  12460. (add-hook 'post-command-hook 'org-read-date-display)
  12461. (setq org-ans0 (read-string prompt default-input
  12462. 'org-read-date-history nil))
  12463. ;; org-ans0: from prompt
  12464. ;; org-ans1: from mouse click
  12465. ;; org-ans2: from calendar motion
  12466. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12467. (remove-hook 'post-command-hook 'org-read-date-display)
  12468. (use-local-map old-map)
  12469. (when org-read-date-overlay
  12470. (delete-overlay org-read-date-overlay)
  12471. (setq org-read-date-overlay nil)))))))
  12472. (t ; Naked prompt only
  12473. (unwind-protect
  12474. (setq ans (read-string prompt default-input
  12475. 'org-read-date-history timestr))
  12476. (when org-read-date-overlay
  12477. (delete-overlay org-read-date-overlay)
  12478. (setq org-read-date-overlay nil)))))
  12479. (setq final (org-read-date-analyze ans def defdecode))
  12480. (setq org-read-date-final-answer ans)
  12481. (if to-time
  12482. (apply 'encode-time final)
  12483. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12484. (format "%04d-%02d-%02d %02d:%02d"
  12485. (nth 5 final) (nth 4 final) (nth 3 final)
  12486. (nth 2 final) (nth 1 final))
  12487. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12488. (defvar def)
  12489. (defvar defdecode)
  12490. (defvar with-time)
  12491. (defvar org-read-date-analyze-futurep nil)
  12492. (defun org-read-date-display ()
  12493. "Display the current date prompt interpretation in the minibuffer."
  12494. (when org-read-date-display-live
  12495. (when org-read-date-overlay
  12496. (delete-overlay org-read-date-overlay))
  12497. (let ((p (point)))
  12498. (end-of-line 1)
  12499. (while (not (equal (buffer-substring
  12500. (max (point-min) (- (point) 4)) (point))
  12501. " "))
  12502. (insert " "))
  12503. (goto-char p))
  12504. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12505. " " (or org-ans1 org-ans2)))
  12506. (org-end-time-was-given nil)
  12507. (f (org-read-date-analyze ans def defdecode))
  12508. (fmts (if org-dcst
  12509. org-time-stamp-custom-formats
  12510. org-time-stamp-formats))
  12511. (fmt (if (or with-time
  12512. (and (boundp 'org-time-was-given) org-time-was-given))
  12513. (cdr fmts)
  12514. (car fmts)))
  12515. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12516. (when (and org-end-time-was-given
  12517. (string-match org-plain-time-of-day-regexp txt))
  12518. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12519. org-end-time-was-given
  12520. (substring txt (match-end 0)))))
  12521. (when org-read-date-analyze-futurep
  12522. (setq txt (concat txt " (=>F)")))
  12523. (setq org-read-date-overlay
  12524. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  12525. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12526. (defun org-read-date-analyze (ans def defdecode)
  12527. "Analyse the combined answer of the date prompt."
  12528. ;; FIXME: cleanup and comment
  12529. (let ((nowdecode (decode-time (current-time)))
  12530. delta deltan deltaw deltadef year month day
  12531. hour minute second wday pm h2 m2 tl wday1
  12532. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12533. (setq org-read-date-analyze-futurep nil)
  12534. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12535. (setq ans "+0"))
  12536. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12537. (setq ans (replace-match "" t t ans)
  12538. deltan (car delta)
  12539. deltaw (nth 1 delta)
  12540. deltadef (nth 2 delta)))
  12541. ;; Check if there is an iso week date in there
  12542. ;; If yes, store the info and postpone interpreting it until the rest
  12543. ;; of the parsing is done
  12544. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12545. (setq iso-year (if (match-end 1)
  12546. (org-small-year-to-year
  12547. (string-to-number (match-string 1 ans))))
  12548. iso-weekday (if (match-end 3)
  12549. (string-to-number (match-string 3 ans)))
  12550. iso-week (string-to-number (match-string 2 ans)))
  12551. (setq ans (replace-match "" t t ans)))
  12552. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12553. (when (string-match
  12554. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12555. (setq year (if (match-end 2)
  12556. (string-to-number (match-string 2 ans))
  12557. (progn (setq kill-year t)
  12558. (string-to-number (format-time-string "%Y"))))
  12559. month (string-to-number (match-string 3 ans))
  12560. day (string-to-number (match-string 4 ans)))
  12561. (if (< year 100) (setq year (+ 2000 year)))
  12562. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12563. t nil ans)))
  12564. ;; Help matching american dates, like 5/30 or 5/30/7
  12565. (when (string-match
  12566. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12567. (setq year (if (match-end 4)
  12568. (string-to-number (match-string 4 ans))
  12569. (progn (setq kill-year t)
  12570. (string-to-number (format-time-string "%Y"))))
  12571. month (string-to-number (match-string 1 ans))
  12572. day (string-to-number (match-string 2 ans)))
  12573. (if (< year 100) (setq year (+ 2000 year)))
  12574. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12575. t nil ans)))
  12576. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12577. ;; If there is a time with am/pm, and *no* time without it, we convert
  12578. ;; so that matching will be successful.
  12579. (loop for i from 1 to 2 do ; twice, for end time as well
  12580. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12581. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12582. (setq hour (string-to-number (match-string 1 ans))
  12583. minute (if (match-end 3)
  12584. (string-to-number (match-string 3 ans))
  12585. 0)
  12586. pm (equal ?p
  12587. (string-to-char (downcase (match-string 4 ans)))))
  12588. (if (and (= hour 12) (not pm))
  12589. (setq hour 0)
  12590. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12591. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12592. t t ans))))
  12593. ;; Check if a time range is given as a duration
  12594. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12595. (setq hour (string-to-number (match-string 1 ans))
  12596. h2 (+ hour (string-to-number (match-string 3 ans)))
  12597. minute (string-to-number (match-string 2 ans))
  12598. m2 (+ minute (if (match-end 5) (string-to-number
  12599. (match-string 5 ans))0)))
  12600. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12601. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12602. t t ans)))
  12603. ;; Check if there is a time range
  12604. (when (boundp 'org-end-time-was-given)
  12605. (setq org-time-was-given nil)
  12606. (when (and (string-match org-plain-time-of-day-regexp ans)
  12607. (match-end 8))
  12608. (setq org-end-time-was-given (match-string 8 ans))
  12609. (setq ans (concat (substring ans 0 (match-beginning 7))
  12610. (substring ans (match-end 7))))))
  12611. (setq tl (parse-time-string ans)
  12612. day (or (nth 3 tl) (nth 3 defdecode))
  12613. month (or (nth 4 tl)
  12614. (if (and org-read-date-prefer-future
  12615. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12616. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12617. (nth 4 defdecode)))
  12618. year (or (and (not kill-year) (nth 5 tl))
  12619. (if (and org-read-date-prefer-future
  12620. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12621. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12622. (nth 5 defdecode)))
  12623. hour (or (nth 2 tl) (nth 2 defdecode))
  12624. minute (or (nth 1 tl) (nth 1 defdecode))
  12625. second (or (nth 0 tl) 0)
  12626. wday (nth 6 tl))
  12627. (when (and (eq org-read-date-prefer-future 'time)
  12628. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12629. (equal day (nth 3 nowdecode))
  12630. (equal month (nth 4 nowdecode))
  12631. (equal year (nth 5 nowdecode))
  12632. (nth 2 tl)
  12633. (or (< (nth 2 tl) (nth 2 nowdecode))
  12634. (and (= (nth 2 tl) (nth 2 nowdecode))
  12635. (nth 1 tl)
  12636. (< (nth 1 tl) (nth 1 nowdecode)))))
  12637. (setq day (1+ day)
  12638. futurep t))
  12639. ;; Special date definitions below
  12640. (cond
  12641. (iso-week
  12642. ;; There was an iso week
  12643. (require 'cal-iso)
  12644. (setq futurep nil)
  12645. (setq year (or iso-year year)
  12646. day (or iso-weekday wday 1)
  12647. wday nil ; to make sure that the trigger below does not match
  12648. iso-date (calendar-gregorian-from-absolute
  12649. (calendar-absolute-from-iso
  12650. (list iso-week day year))))
  12651. ; FIXME: Should we also push ISO weeks into the future?
  12652. ; (when (and org-read-date-prefer-future
  12653. ; (not iso-year)
  12654. ; (< (calendar-absolute-from-gregorian iso-date)
  12655. ; (time-to-days (current-time))))
  12656. ; (setq year (1+ year)
  12657. ; iso-date (calendar-gregorian-from-absolute
  12658. ; (calendar-absolute-from-iso
  12659. ; (list iso-week day year)))))
  12660. (setq month (car iso-date)
  12661. year (nth 2 iso-date)
  12662. day (nth 1 iso-date)))
  12663. (deltan
  12664. (setq futurep nil)
  12665. (unless deltadef
  12666. (let ((now (decode-time (current-time))))
  12667. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12668. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12669. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12670. ((equal deltaw "m") (setq month (+ month deltan)))
  12671. ((equal deltaw "y") (setq year (+ year deltan)))))
  12672. ((and wday (not (nth 3 tl)))
  12673. (setq futurep nil)
  12674. ;; Weekday was given, but no day, so pick that day in the week
  12675. ;; on or after the derived date.
  12676. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12677. (unless (equal wday wday1)
  12678. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12679. (if (and (boundp 'org-time-was-given)
  12680. (nth 2 tl))
  12681. (setq org-time-was-given t))
  12682. (if (< year 100) (setq year (+ 2000 year)))
  12683. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12684. (setq org-read-date-analyze-futurep futurep)
  12685. (list second minute hour day month year)))
  12686. (defvar parse-time-weekdays)
  12687. (defun org-read-date-get-relative (s today default)
  12688. "Check string S for special relative date string.
  12689. TODAY and DEFAULT are internal times, for today and for a default.
  12690. Return shift list (N what def-flag)
  12691. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12692. N is the number of WHATs to shift.
  12693. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12694. the DEFAULT date rather than TODAY."
  12695. (when (and
  12696. (string-match
  12697. (concat
  12698. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12699. "\\([0-9]+\\)?"
  12700. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12701. "\\([ \t]\\|$\\)") s)
  12702. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12703. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12704. (string-to-char (substring (match-string 1 s) -1))
  12705. ?+))
  12706. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12707. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12708. (what (if (match-end 3) (match-string 3 s) "d"))
  12709. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12710. (date (if rel default today))
  12711. (wday (nth 6 (decode-time date)))
  12712. delta)
  12713. (if wday1
  12714. (progn
  12715. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12716. (if (= dir ?-) (setq delta (- delta 7)))
  12717. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12718. (list delta "d" rel))
  12719. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12720. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12721. "Turn a user-specified date into the internal representation.
  12722. The internal representation needed by the calendar is (month day year).
  12723. This is a wrapper to handle the brain-dead convention in calendar that
  12724. user function argument order change dependent on argument order."
  12725. (if (boundp 'calendar-date-style)
  12726. (cond
  12727. ((eq calendar-date-style 'american)
  12728. (list arg1 arg2 arg3))
  12729. ((eq calendar-date-style 'european)
  12730. (list arg2 arg1 arg3))
  12731. ((eq calendar-date-style 'iso)
  12732. (list arg2 arg3 arg1)))
  12733. (if (org-bound-and-true-p european-calendar-style)
  12734. (list arg2 arg1 arg3)
  12735. (list arg1 arg2 arg3))))
  12736. (defun org-eval-in-calendar (form &optional keepdate)
  12737. "Eval FORM in the calendar window and return to current window.
  12738. Also, store the cursor date in variable org-ans2."
  12739. (let ((sf (selected-frame))
  12740. (sw (selected-window)))
  12741. (select-window (get-buffer-window "*Calendar*" t))
  12742. (eval form)
  12743. (when (and (not keepdate) (calendar-cursor-to-date))
  12744. (let* ((date (calendar-cursor-to-date))
  12745. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12746. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12747. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12748. (select-window sw)
  12749. (org-select-frame-set-input-focus sf)))
  12750. (defun org-calendar-select ()
  12751. "Return to `org-read-date' with the date currently selected.
  12752. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12753. (interactive)
  12754. (when (calendar-cursor-to-date)
  12755. (let* ((date (calendar-cursor-to-date))
  12756. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12757. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12758. (if (active-minibuffer-window) (exit-minibuffer))))
  12759. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12760. "Insert a date stamp for the date given by the internal TIME.
  12761. WITH-HM means use the stamp format that includes the time of the day.
  12762. INACTIVE means use square brackets instead of angular ones, so that the
  12763. stamp will not contribute to the agenda.
  12764. PRE and POST are optional strings to be inserted before and after the
  12765. stamp.
  12766. The command returns the inserted time stamp."
  12767. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12768. stamp)
  12769. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12770. (insert-before-markers (or pre ""))
  12771. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12772. (when (listp extra)
  12773. (setq extra (car extra))
  12774. (if (and (stringp extra)
  12775. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12776. (setq extra (format "-%02d:%02d"
  12777. (string-to-number (match-string 1 extra))
  12778. (string-to-number (match-string 2 extra))))
  12779. (setq extra nil)))
  12780. (when extra
  12781. (backward-char 1)
  12782. (insert-before-markers extra)
  12783. (forward-char 1))
  12784. (insert-before-markers (or post ""))
  12785. (setq org-last-inserted-timestamp stamp)))
  12786. (defun org-toggle-time-stamp-overlays ()
  12787. "Toggle the use of custom time stamp formats."
  12788. (interactive)
  12789. (setq org-display-custom-times (not org-display-custom-times))
  12790. (unless org-display-custom-times
  12791. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12792. (while (setq p (next-single-property-change p 'display))
  12793. (if (and (get-text-property p 'display)
  12794. (eq (get-text-property p 'face) 'org-date))
  12795. (remove-text-properties
  12796. p (setq p (next-single-property-change p 'display))
  12797. '(display t))))
  12798. (set-buffer-modified-p bmp)))
  12799. (if (featurep 'xemacs)
  12800. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12801. (org-restart-font-lock)
  12802. (setq org-table-may-need-update t)
  12803. (if org-display-custom-times
  12804. (message "Time stamps are overlayed with custom format")
  12805. (message "Time stamp overlays removed")))
  12806. (defun org-display-custom-time (beg end)
  12807. "Overlay modified time stamp format over timestamp between BEG and END."
  12808. (let* ((ts (buffer-substring beg end))
  12809. t1 w1 with-hm tf time str w2 (off 0))
  12810. (save-match-data
  12811. (setq t1 (org-parse-time-string ts t))
  12812. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12813. (setq off (- (match-end 0) (match-beginning 0)))))
  12814. (setq end (- end off))
  12815. (setq w1 (- end beg)
  12816. with-hm (and (nth 1 t1) (nth 2 t1))
  12817. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12818. time (org-fix-decoded-time t1)
  12819. str (org-add-props
  12820. (format-time-string
  12821. (substring tf 1 -1) (apply 'encode-time time))
  12822. nil 'mouse-face 'highlight)
  12823. w2 (length str))
  12824. (if (not (= w2 w1))
  12825. (add-text-properties (1+ beg) (+ 2 beg)
  12826. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12827. (if (featurep 'xemacs)
  12828. (progn
  12829. (put-text-property beg end 'invisible t)
  12830. (put-text-property beg end 'end-glyph (make-glyph str)))
  12831. (put-text-property beg end 'display str))))
  12832. (defun org-translate-time (string)
  12833. "Translate all timestamps in STRING to custom format.
  12834. But do this only if the variable `org-display-custom-times' is set."
  12835. (when org-display-custom-times
  12836. (save-match-data
  12837. (let* ((start 0)
  12838. (re org-ts-regexp-both)
  12839. t1 with-hm inactive tf time str beg end)
  12840. (while (setq start (string-match re string start))
  12841. (setq beg (match-beginning 0)
  12842. end (match-end 0)
  12843. t1 (save-match-data
  12844. (org-parse-time-string (substring string beg end) t))
  12845. with-hm (and (nth 1 t1) (nth 2 t1))
  12846. inactive (equal (substring string beg (1+ beg)) "[")
  12847. tf (funcall (if with-hm 'cdr 'car)
  12848. org-time-stamp-custom-formats)
  12849. time (org-fix-decoded-time t1)
  12850. str (format-time-string
  12851. (concat
  12852. (if inactive "[" "<") (substring tf 1 -1)
  12853. (if inactive "]" ">"))
  12854. (apply 'encode-time time))
  12855. string (replace-match str t t string)
  12856. start (+ start (length str)))))))
  12857. string)
  12858. (defun org-fix-decoded-time (time)
  12859. "Set 0 instead of nil for the first 6 elements of time.
  12860. Don't touch the rest."
  12861. (let ((n 0))
  12862. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12863. (defun org-days-to-time (timestamp-string)
  12864. "Difference between TIMESTAMP-STRING and now in days."
  12865. (- (time-to-days (org-time-string-to-time timestamp-string))
  12866. (time-to-days (current-time))))
  12867. (defun org-deadline-close (timestamp-string &optional ndays)
  12868. "Is the time in TIMESTAMP-STRING close to the current date?"
  12869. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12870. (and (< (org-days-to-time timestamp-string) ndays)
  12871. (not (org-entry-is-done-p))))
  12872. (defun org-get-wdays (ts)
  12873. "Get the deadline lead time appropriate for timestring TS."
  12874. (cond
  12875. ((<= org-deadline-warning-days 0)
  12876. ;; 0 or negative, enforce this value no matter what
  12877. (- org-deadline-warning-days))
  12878. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12879. ;; lead time is specified.
  12880. (floor (* (string-to-number (match-string 1 ts))
  12881. (cdr (assoc (match-string 2 ts)
  12882. '(("d" . 1) ("w" . 7)
  12883. ("m" . 30.4) ("y" . 365.25)))))))
  12884. ;; go for the default.
  12885. (t org-deadline-warning-days)))
  12886. (defun org-calendar-select-mouse (ev)
  12887. "Return to `org-read-date' with the date currently selected.
  12888. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12889. (interactive "e")
  12890. (mouse-set-point ev)
  12891. (when (calendar-cursor-to-date)
  12892. (let* ((date (calendar-cursor-to-date))
  12893. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12894. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12895. (if (active-minibuffer-window) (exit-minibuffer))))
  12896. (defun org-check-deadlines (ndays)
  12897. "Check if there are any deadlines due or past due.
  12898. A deadline is considered due if it happens within `org-deadline-warning-days'
  12899. days from today's date. If the deadline appears in an entry marked DONE,
  12900. it is not shown. The prefix arg NDAYS can be used to test that many
  12901. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12902. (interactive "P")
  12903. (let* ((org-warn-days
  12904. (cond
  12905. ((equal ndays '(4)) 100000)
  12906. (ndays (prefix-numeric-value ndays))
  12907. (t (abs org-deadline-warning-days))))
  12908. (case-fold-search nil)
  12909. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12910. (callback
  12911. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12912. (message "%d deadlines past-due or due within %d days"
  12913. (org-occur regexp nil callback)
  12914. org-warn-days)))
  12915. (defun org-check-before-date (date)
  12916. "Check if there are deadlines or scheduled entries before DATE."
  12917. (interactive (list (org-read-date)))
  12918. (let ((case-fold-search nil)
  12919. (regexp (concat "\\<\\(" org-deadline-string
  12920. "\\|" org-scheduled-string
  12921. "\\) *<\\([^>]+\\)>"))
  12922. (callback
  12923. (lambda () (time-less-p
  12924. (org-time-string-to-time (match-string 2))
  12925. (org-time-string-to-time date)))))
  12926. (message "%d entries before %s"
  12927. (org-occur regexp nil callback) date)))
  12928. (defun org-check-after-date (date)
  12929. "Check if there are deadlines or scheduled entries after DATE."
  12930. (interactive (list (org-read-date)))
  12931. (let ((case-fold-search nil)
  12932. (regexp (concat "\\<\\(" org-deadline-string
  12933. "\\|" org-scheduled-string
  12934. "\\) *<\\([^>]+\\)>"))
  12935. (callback
  12936. (lambda () (not
  12937. (time-less-p
  12938. (org-time-string-to-time (match-string 2))
  12939. (org-time-string-to-time date))))))
  12940. (message "%d entries after %s"
  12941. (org-occur regexp nil callback) date)))
  12942. (defun org-evaluate-time-range (&optional to-buffer)
  12943. "Evaluate a time range by computing the difference between start and end.
  12944. Normally the result is just printed in the echo area, but with prefix arg
  12945. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12946. If the time range is actually in a table, the result is inserted into the
  12947. next column.
  12948. For time difference computation, a year is assumed to be exactly 365
  12949. days in order to avoid rounding problems."
  12950. (interactive "P")
  12951. (or
  12952. (org-clock-update-time-maybe)
  12953. (save-excursion
  12954. (unless (org-at-date-range-p t)
  12955. (goto-char (point-at-bol))
  12956. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12957. (if (not (org-at-date-range-p t))
  12958. (error "Not at a time-stamp range, and none found in current line")))
  12959. (let* ((ts1 (match-string 1))
  12960. (ts2 (match-string 2))
  12961. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12962. (match-end (match-end 0))
  12963. (time1 (org-time-string-to-time ts1))
  12964. (time2 (org-time-string-to-time ts2))
  12965. (t1 (org-float-time time1))
  12966. (t2 (org-float-time time2))
  12967. (diff (abs (- t2 t1)))
  12968. (negative (< (- t2 t1) 0))
  12969. ;; (ys (floor (* 365 24 60 60)))
  12970. (ds (* 24 60 60))
  12971. (hs (* 60 60))
  12972. (fy "%dy %dd %02d:%02d")
  12973. (fy1 "%dy %dd")
  12974. (fd "%dd %02d:%02d")
  12975. (fd1 "%dd")
  12976. (fh "%02d:%02d")
  12977. y d h m align)
  12978. (if havetime
  12979. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12980. y 0
  12981. d (floor (/ diff ds)) diff (mod diff ds)
  12982. h (floor (/ diff hs)) diff (mod diff hs)
  12983. m (floor (/ diff 60)))
  12984. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12985. y 0
  12986. d (floor (+ (/ diff ds) 0.5))
  12987. h 0 m 0))
  12988. (if (not to-buffer)
  12989. (message "%s" (org-make-tdiff-string y d h m))
  12990. (if (org-at-table-p)
  12991. (progn
  12992. (goto-char match-end)
  12993. (setq align t)
  12994. (and (looking-at " *|") (goto-char (match-end 0))))
  12995. (goto-char match-end))
  12996. (if (looking-at
  12997. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12998. (replace-match ""))
  12999. (if negative (insert " -"))
  13000. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13001. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13002. (insert " " (format fh h m))))
  13003. (if align (org-table-align))
  13004. (message "Time difference inserted")))))
  13005. (defun org-make-tdiff-string (y d h m)
  13006. (let ((fmt "")
  13007. (l nil))
  13008. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13009. l (push y l)))
  13010. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13011. l (push d l)))
  13012. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13013. l (push h l)))
  13014. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13015. l (push m l)))
  13016. (apply 'format fmt (nreverse l))))
  13017. (defun org-time-string-to-time (s)
  13018. (apply 'encode-time (org-parse-time-string s)))
  13019. (defun org-time-string-to-seconds (s)
  13020. (org-float-time (org-time-string-to-time s)))
  13021. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13022. "Convert a time stamp to an absolute day number.
  13023. If there is a specifyer for a cyclic time stamp, get the closest date to
  13024. DAYNR.
  13025. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13026. the variable date is bound by the calendar when this is called."
  13027. (cond
  13028. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13029. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13030. daynr
  13031. (+ daynr 1000)))
  13032. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13033. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13034. (time-to-days (current-time))) (match-string 0 s)
  13035. prefer show-all))
  13036. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13037. (defun org-days-to-iso-week (days)
  13038. "Return the iso week number."
  13039. (require 'cal-iso)
  13040. (car (calendar-iso-from-absolute days)))
  13041. (defun org-small-year-to-year (year)
  13042. "Convert 2-digit years into 4-digit years.
  13043. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13044. The year 2000 cannot be abbreviated. Any year larger than 99
  13045. is returned unchanged."
  13046. (if (< year 38)
  13047. (setq year (+ 2000 year))
  13048. (if (< year 100)
  13049. (setq year (+ 1900 year))))
  13050. year)
  13051. (defun org-time-from-absolute (d)
  13052. "Return the time corresponding to date D.
  13053. D may be an absolute day number, or a calendar-type list (month day year)."
  13054. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13055. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13056. (defun org-calendar-holiday ()
  13057. "List of holidays, for Diary display in Org-mode."
  13058. (require 'holidays)
  13059. (let ((hl (funcall
  13060. (if (fboundp 'calendar-check-holidays)
  13061. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13062. (if hl (mapconcat 'identity hl "; "))))
  13063. (defun org-diary-sexp-entry (sexp entry date)
  13064. "Process a SEXP diary ENTRY for DATE."
  13065. (require 'diary-lib)
  13066. (let ((result (if calendar-debug-sexp
  13067. (let ((stack-trace-on-error t))
  13068. (eval (car (read-from-string sexp))))
  13069. (condition-case nil
  13070. (eval (car (read-from-string sexp)))
  13071. (error
  13072. (beep)
  13073. (message "Bad sexp at line %d in %s: %s"
  13074. (org-current-line)
  13075. (buffer-file-name) sexp)
  13076. (sleep-for 2))))))
  13077. (cond ((stringp result) result)
  13078. ((and (consp result)
  13079. (stringp (cdr result))) (cdr result))
  13080. (result entry)
  13081. (t nil))))
  13082. (defun org-diary-to-ical-string (frombuf)
  13083. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13084. This uses the icalendar.el library."
  13085. (let* ((tmpdir (if (featurep 'xemacs)
  13086. (temp-directory)
  13087. temporary-file-directory))
  13088. (tmpfile (make-temp-name
  13089. (expand-file-name "orgics" tmpdir)))
  13090. buf rtn b e)
  13091. (with-current-buffer frombuf
  13092. (icalendar-export-region (point-min) (point-max) tmpfile)
  13093. (setq buf (find-buffer-visiting tmpfile))
  13094. (set-buffer buf)
  13095. (goto-char (point-min))
  13096. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13097. (setq b (match-beginning 0)))
  13098. (goto-char (point-max))
  13099. (if (re-search-backward "^END:VEVENT" nil t)
  13100. (setq e (match-end 0)))
  13101. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13102. (kill-buffer buf)
  13103. (delete-file tmpfile)
  13104. rtn))
  13105. (defun org-closest-date (start current change prefer show-all)
  13106. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13107. When PREFER is `past' return a date that is either CURRENT or past.
  13108. When PREFER is `future', return a date that is either CURRENT or future.
  13109. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13110. ;; Make the proper lists from the dates
  13111. (catch 'exit
  13112. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13113. dn dw sday cday n1 n2 n0
  13114. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13115. (setq start (org-date-to-gregorian start)
  13116. current (org-date-to-gregorian
  13117. (if show-all
  13118. current
  13119. (time-to-days (current-time))))
  13120. sday (calendar-absolute-from-gregorian start)
  13121. cday (calendar-absolute-from-gregorian current))
  13122. (if (<= cday sday) (throw 'exit sday))
  13123. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13124. (setq dn (string-to-number (match-string 1 change))
  13125. dw (cdr (assoc (match-string 2 change) a1)))
  13126. (error "Invalid change specifyer: %s" change))
  13127. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13128. (cond
  13129. ((eq dw 'day)
  13130. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13131. n2 (+ n1 dn)))
  13132. ((eq dw 'year)
  13133. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13134. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13135. (setq date1 (list m d y1)
  13136. n1 (calendar-absolute-from-gregorian date1)
  13137. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13138. n2 (calendar-absolute-from-gregorian date2)))
  13139. ((eq dw 'month)
  13140. ;; approx number of month between the two dates
  13141. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13142. ;; How often does dn fit in there?
  13143. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13144. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13145. m (+ m nm)
  13146. ny (floor (/ m 12))
  13147. y (+ y ny)
  13148. m (- m (* ny 12)))
  13149. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13150. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13151. (setq m2 (+ m dn) y2 y)
  13152. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13153. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13154. (while (<= n2 cday)
  13155. (setq n1 n2 m m2 y y2)
  13156. (setq m2 (+ m dn) y2 y)
  13157. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13158. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13159. ;; Make sure n1 is the earlier date
  13160. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13161. (if show-all
  13162. (cond
  13163. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13164. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13165. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13166. (cond
  13167. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13168. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13169. (t (if (= cday n1) n1 n2)))))))
  13170. (defun org-date-to-gregorian (date)
  13171. "Turn any specification of DATE into a gregorian date for the calendar."
  13172. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13173. ((and (listp date) (= (length date) 3)) date)
  13174. ((stringp date)
  13175. (setq date (org-parse-time-string date))
  13176. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13177. ((listp date)
  13178. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13179. (defun org-parse-time-string (s &optional nodefault)
  13180. "Parse the standard Org-mode time string.
  13181. This should be a lot faster than the normal `parse-time-string'.
  13182. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13183. hour and minute fields will be nil if not given."
  13184. (if (string-match org-ts-regexp0 s)
  13185. (list 0
  13186. (if (or (match-beginning 8) (not nodefault))
  13187. (string-to-number (or (match-string 8 s) "0")))
  13188. (if (or (match-beginning 7) (not nodefault))
  13189. (string-to-number (or (match-string 7 s) "0")))
  13190. (string-to-number (match-string 4 s))
  13191. (string-to-number (match-string 3 s))
  13192. (string-to-number (match-string 2 s))
  13193. nil nil nil)
  13194. (error "Not a standard Org-mode time string: %s" s)))
  13195. (defun org-timestamp-up (&optional arg)
  13196. "Increase the date item at the cursor by one.
  13197. If the cursor is on the year, change the year. If it is on the month or
  13198. the day, change that.
  13199. With prefix ARG, change by that many units."
  13200. (interactive "p")
  13201. (org-timestamp-change (prefix-numeric-value arg)))
  13202. (defun org-timestamp-down (&optional arg)
  13203. "Decrease the date item at the cursor by one.
  13204. If the cursor is on the year, change the year. If it is on the month or
  13205. the day, change that.
  13206. With prefix ARG, change by that many units."
  13207. (interactive "p")
  13208. (org-timestamp-change (- (prefix-numeric-value arg))))
  13209. (defun org-timestamp-up-day (&optional arg)
  13210. "Increase the date in the time stamp by one day.
  13211. With prefix ARG, change that many days."
  13212. (interactive "p")
  13213. (if (and (not (org-at-timestamp-p t))
  13214. (org-on-heading-p))
  13215. (org-todo 'up)
  13216. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13217. (defun org-timestamp-down-day (&optional arg)
  13218. "Decrease the date in the time stamp by one day.
  13219. With prefix ARG, change that many days."
  13220. (interactive "p")
  13221. (if (and (not (org-at-timestamp-p t))
  13222. (org-on-heading-p))
  13223. (org-todo 'down)
  13224. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13225. (defun org-at-timestamp-p (&optional inactive-ok)
  13226. "Determine if the cursor is in or at a timestamp."
  13227. (interactive)
  13228. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13229. (pos (point))
  13230. (ans (or (looking-at tsr)
  13231. (save-excursion
  13232. (skip-chars-backward "^[<\n\r\t")
  13233. (if (> (point) (point-min)) (backward-char 1))
  13234. (and (looking-at tsr)
  13235. (> (- (match-end 0) pos) -1))))))
  13236. (and ans
  13237. (boundp 'org-ts-what)
  13238. (setq org-ts-what
  13239. (cond
  13240. ((= pos (match-beginning 0)) 'bracket)
  13241. ((= pos (1- (match-end 0))) 'bracket)
  13242. ((org-pos-in-match-range pos 2) 'year)
  13243. ((org-pos-in-match-range pos 3) 'month)
  13244. ((org-pos-in-match-range pos 7) 'hour)
  13245. ((org-pos-in-match-range pos 8) 'minute)
  13246. ((or (org-pos-in-match-range pos 4)
  13247. (org-pos-in-match-range pos 5)) 'day)
  13248. ((and (> pos (or (match-end 8) (match-end 5)))
  13249. (< pos (match-end 0)))
  13250. (- pos (or (match-end 8) (match-end 5))))
  13251. (t 'day))))
  13252. ans))
  13253. (defun org-toggle-timestamp-type ()
  13254. "Toggle the type (<active> or [inactive]) of a time stamp."
  13255. (interactive)
  13256. (when (org-at-timestamp-p t)
  13257. (let ((beg (match-beginning 0)) (end (match-end 0))
  13258. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13259. (save-excursion
  13260. (goto-char beg)
  13261. (while (re-search-forward "[][<>]" end t)
  13262. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13263. t t)))
  13264. (message "Timestamp is now %sactive"
  13265. (if (equal (char-after beg) ?<) "" "in")))))
  13266. (defun org-timestamp-change (n &optional what)
  13267. "Change the date in the time stamp at point.
  13268. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13269. `year', `minute', `second'. If WHAT is not given, the cursor position
  13270. in the timestamp determines what will be changed."
  13271. (let ((pos (point))
  13272. with-hm inactive
  13273. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13274. org-ts-what
  13275. extra rem
  13276. ts time time0)
  13277. (if (not (org-at-timestamp-p t))
  13278. (error "Not at a timestamp"))
  13279. (if (and (not what) (eq org-ts-what 'bracket))
  13280. (org-toggle-timestamp-type)
  13281. (if (and (not what) (not (eq org-ts-what 'day))
  13282. org-display-custom-times
  13283. (get-text-property (point) 'display)
  13284. (not (get-text-property (1- (point)) 'display)))
  13285. (setq org-ts-what 'day))
  13286. (setq org-ts-what (or what org-ts-what)
  13287. inactive (= (char-after (match-beginning 0)) ?\[)
  13288. ts (match-string 0))
  13289. (replace-match "")
  13290. (if (string-match
  13291. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13292. ts)
  13293. (setq extra (match-string 1 ts)))
  13294. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13295. (setq with-hm t))
  13296. (setq time0 (org-parse-time-string ts))
  13297. (when (and (eq org-ts-what 'minute)
  13298. (eq current-prefix-arg nil))
  13299. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13300. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13301. (setcar (cdr time0) (+ (nth 1 time0)
  13302. (if (> n 0) (- rem) (- dm rem))))))
  13303. (setq time
  13304. (encode-time (or (car time0) 0)
  13305. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13306. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13307. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13308. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13309. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13310. (nthcdr 6 time0)))
  13311. (when (and (member org-ts-what '(hour minute))
  13312. extra
  13313. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13314. (setq extra (org-modify-ts-extra
  13315. extra
  13316. (if (eq org-ts-what 'hour) 2 5)
  13317. n dm)))
  13318. (when (integerp org-ts-what)
  13319. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13320. (if (eq what 'calendar)
  13321. (let ((cal-date (org-get-date-from-calendar)))
  13322. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13323. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13324. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13325. (setcar time0 (or (car time0) 0))
  13326. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13327. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13328. (setq time (apply 'encode-time time0))))
  13329. (setq org-last-changed-timestamp
  13330. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13331. (org-clock-update-time-maybe)
  13332. (goto-char pos)
  13333. ;; Try to recenter the calendar window, if any
  13334. (if (and org-calendar-follow-timestamp-change
  13335. (get-buffer-window "*Calendar*" t)
  13336. (memq org-ts-what '(day month year)))
  13337. (org-recenter-calendar (time-to-days time))))))
  13338. (defun org-modify-ts-extra (s pos n dm)
  13339. "Change the different parts of the lead-time and repeat fields in timestamp."
  13340. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13341. ng h m new rem)
  13342. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13343. (cond
  13344. ((or (org-pos-in-match-range pos 2)
  13345. (org-pos-in-match-range pos 3))
  13346. (setq m (string-to-number (match-string 3 s))
  13347. h (string-to-number (match-string 2 s)))
  13348. (if (org-pos-in-match-range pos 2)
  13349. (setq h (+ h n))
  13350. (setq n (* dm (org-no-warnings (signum n))))
  13351. (when (not (= 0 (setq rem (% m dm))))
  13352. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13353. (setq m (+ m n)))
  13354. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13355. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13356. (setq h (min 24 (max 0 h)))
  13357. (setq ng 1 new (format "-%02d:%02d" h m)))
  13358. ((org-pos-in-match-range pos 6)
  13359. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13360. ((org-pos-in-match-range pos 5)
  13361. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13362. ((org-pos-in-match-range pos 9)
  13363. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13364. ((org-pos-in-match-range pos 8)
  13365. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13366. (when ng
  13367. (setq s (concat
  13368. (substring s 0 (match-beginning ng))
  13369. new
  13370. (substring s (match-end ng))))))
  13371. s))
  13372. (defun org-recenter-calendar (date)
  13373. "If the calendar is visible, recenter it to DATE."
  13374. (let* ((win (selected-window))
  13375. (cwin (get-buffer-window "*Calendar*" t))
  13376. (calendar-move-hook nil))
  13377. (when cwin
  13378. (select-window cwin)
  13379. (calendar-goto-date (if (listp date) date
  13380. (calendar-gregorian-from-absolute date)))
  13381. (select-window win))))
  13382. (defun org-goto-calendar (&optional arg)
  13383. "Go to the Emacs calendar at the current date.
  13384. If there is a time stamp in the current line, go to that date.
  13385. A prefix ARG can be used to force the current date."
  13386. (interactive "P")
  13387. (let ((tsr org-ts-regexp) diff
  13388. (calendar-move-hook nil)
  13389. (calendar-view-holidays-initially-flag nil)
  13390. (view-calendar-holidays-initially nil)
  13391. (calendar-view-diary-initially-flag nil)
  13392. (view-diary-entries-initially nil))
  13393. (if (or (org-at-timestamp-p)
  13394. (save-excursion
  13395. (beginning-of-line 1)
  13396. (looking-at (concat ".*" tsr))))
  13397. (let ((d1 (time-to-days (current-time)))
  13398. (d2 (time-to-days
  13399. (org-time-string-to-time (match-string 1)))))
  13400. (setq diff (- d2 d1))))
  13401. (calendar)
  13402. (calendar-goto-today)
  13403. (if (and diff (not arg)) (calendar-forward-day diff))))
  13404. (defun org-get-date-from-calendar ()
  13405. "Return a list (month day year) of date at point in calendar."
  13406. (with-current-buffer "*Calendar*"
  13407. (save-match-data
  13408. (calendar-cursor-to-date))))
  13409. (defun org-date-from-calendar ()
  13410. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13411. If there is already a time stamp at the cursor position, update it."
  13412. (interactive)
  13413. (if (org-at-timestamp-p t)
  13414. (org-timestamp-change 0 'calendar)
  13415. (let ((cal-date (org-get-date-from-calendar)))
  13416. (org-insert-time-stamp
  13417. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13418. (defun org-minutes-to-hh:mm-string (m)
  13419. "Compute H:MM from a number of minutes."
  13420. (let ((h (/ m 60)))
  13421. (setq m (- m (* 60 h)))
  13422. (format org-time-clocksum-format h m)))
  13423. (defun org-hh:mm-string-to-minutes (s)
  13424. "Convert a string H:MM to a number of minutes.
  13425. If the string is just a number, interpret it as minutes.
  13426. In fact, the first hh:mm or number in the string will be taken,
  13427. there can be extra stuff in the string.
  13428. If no number is found, the return value is 0."
  13429. (cond
  13430. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13431. (+ (* (string-to-number (match-string 1 s)) 60)
  13432. (string-to-number (match-string 2 s))))
  13433. ((string-match "\\([0-9]+\\)" s)
  13434. (string-to-number (match-string 1 s)))
  13435. (t 0)))
  13436. ;;;; Files
  13437. (defun org-save-all-org-buffers ()
  13438. "Save all Org-mode buffers without user confirmation."
  13439. (interactive)
  13440. (message "Saving all Org-mode buffers...")
  13441. (save-some-buffers t 'org-mode-p)
  13442. (when (featurep 'org-id) (org-id-locations-save))
  13443. (message "Saving all Org-mode buffers... done"))
  13444. (defun org-revert-all-org-buffers ()
  13445. "Revert all Org-mode buffers.
  13446. Prompt for confirmation when there are unsaved changes.
  13447. Be sure you know what you are doing before letting this function
  13448. overwrite your changes.
  13449. This function is useful in a setup where one tracks org files
  13450. with a version control system, to revert on one machine after pulling
  13451. changes from another. I believe the procedure must be like this:
  13452. 1. M-x org-save-all-org-buffers
  13453. 2. Pull changes from the other machine, resolve conflicts
  13454. 3. M-x org-revert-all-org-buffers"
  13455. (interactive)
  13456. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13457. (error "Abort"))
  13458. (save-excursion
  13459. (save-window-excursion
  13460. (mapc
  13461. (lambda (b)
  13462. (when (and (with-current-buffer b (org-mode-p))
  13463. (with-current-buffer b buffer-file-name))
  13464. (switch-to-buffer b)
  13465. (revert-buffer t 'no-confirm)))
  13466. (buffer-list))
  13467. (when (and (featurep 'org-id) org-id-track-globally)
  13468. (org-id-locations-load)))))
  13469. ;;;; Agenda files
  13470. ;;;###autoload
  13471. (defun org-iswitchb (&optional arg)
  13472. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13473. With a prefix argument, restrict available to files.
  13474. With two prefix arguments, restrict available buffers to agenda files."
  13475. (interactive "P")
  13476. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13477. ((equal arg '(16)) (org-buffer-list 'agenda))
  13478. (t (org-buffer-list)))))
  13479. (switch-to-buffer
  13480. (org-icompleting-read "Org buffer: "
  13481. (mapcar 'list (mapcar 'buffer-name blist))
  13482. nil t))))
  13483. ;;;###autoload
  13484. (defalias 'org-ido-switchb 'org-iswitchb)
  13485. (defun org-buffer-list (&optional predicate exclude-tmp)
  13486. "Return a list of Org buffers.
  13487. PREDICATE can be `export', `files' or `agenda'.
  13488. export restrict the list to Export buffers.
  13489. files restrict the list to buffers visiting Org files.
  13490. agenda restrict the list to buffers visiting agenda files.
  13491. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13492. (let* ((bfn nil)
  13493. (agenda-files (and (eq predicate 'agenda)
  13494. (mapcar 'file-truename (org-agenda-files t))))
  13495. (filter
  13496. (cond
  13497. ((eq predicate 'files)
  13498. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13499. ((eq predicate 'export)
  13500. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13501. ((eq predicate 'agenda)
  13502. (lambda (b)
  13503. (with-current-buffer b
  13504. (and (eq major-mode 'org-mode)
  13505. (setq bfn (buffer-file-name b))
  13506. (member (file-truename bfn) agenda-files)))))
  13507. (t (lambda (b) (with-current-buffer b
  13508. (or (eq major-mode 'org-mode)
  13509. (string-match "\*Org .*Export"
  13510. (buffer-name b)))))))))
  13511. (delq nil
  13512. (mapcar
  13513. (lambda(b)
  13514. (if (and (funcall filter b)
  13515. (or (not exclude-tmp)
  13516. (not (string-match "tmp" (buffer-name b)))))
  13517. b
  13518. nil))
  13519. (buffer-list)))))
  13520. (defun org-agenda-files (&optional unrestricted archives)
  13521. "Get the list of agenda files.
  13522. Optional UNRESTRICTED means return the full list even if a restriction
  13523. is currently in place.
  13524. When ARCHIVES is t, include all archive files that are really being
  13525. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13526. `org-agenda-archives-mode' is t."
  13527. (let ((files
  13528. (cond
  13529. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13530. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13531. ((listp org-agenda-files) org-agenda-files)
  13532. (t (error "Invalid value of `org-agenda-files'")))))
  13533. (setq files (apply 'append
  13534. (mapcar (lambda (f)
  13535. (if (file-directory-p f)
  13536. (directory-files
  13537. f t org-agenda-file-regexp)
  13538. (list f)))
  13539. files)))
  13540. (when org-agenda-skip-unavailable-files
  13541. (setq files (delq nil
  13542. (mapcar (function
  13543. (lambda (file)
  13544. (and (file-readable-p file) file)))
  13545. files))))
  13546. (when (or (eq archives t)
  13547. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13548. (setq files (org-add-archive-files files)))
  13549. files))
  13550. (defun org-edit-agenda-file-list ()
  13551. "Edit the list of agenda files.
  13552. Depending on setup, this either uses customize to edit the variable
  13553. `org-agenda-files', or it visits the file that is holding the list. In the
  13554. latter case, the buffer is set up in a way that saving it automatically kills
  13555. the buffer and restores the previous window configuration."
  13556. (interactive)
  13557. (if (stringp org-agenda-files)
  13558. (let ((cw (current-window-configuration)))
  13559. (find-file org-agenda-files)
  13560. (org-set-local 'org-window-configuration cw)
  13561. (org-add-hook 'after-save-hook
  13562. (lambda ()
  13563. (set-window-configuration
  13564. (prog1 org-window-configuration
  13565. (kill-buffer (current-buffer))))
  13566. (org-install-agenda-files-menu)
  13567. (message "New agenda file list installed"))
  13568. nil 'local)
  13569. (message "%s" (substitute-command-keys
  13570. "Edit list and finish with \\[save-buffer]")))
  13571. (customize-variable 'org-agenda-files)))
  13572. (defun org-store-new-agenda-file-list (list)
  13573. "Set new value for the agenda file list and save it correctly."
  13574. (if (stringp org-agenda-files)
  13575. (let ((fe (org-read-agenda-file-list t)) b u)
  13576. (while (setq b (find-buffer-visiting org-agenda-files))
  13577. (kill-buffer b))
  13578. (with-temp-file org-agenda-files
  13579. (insert
  13580. (mapconcat
  13581. (lambda (f) ;; Keep un-expanded entries.
  13582. (if (setq u (assoc f fe))
  13583. (cdr u)
  13584. f))
  13585. list "\n")
  13586. "\n")))
  13587. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13588. (org-insert-mode-line-in-empty-file nil))
  13589. (setq org-agenda-files list)
  13590. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13591. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13592. "Read the list of agenda files from a file.
  13593. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13594. filenames, used by `org-store-new-agenda-file-list' to write back
  13595. un-expanded file names."
  13596. (when (file-directory-p org-agenda-files)
  13597. (error "`org-agenda-files' cannot be a single directory"))
  13598. (when (stringp org-agenda-files)
  13599. (with-temp-buffer
  13600. (insert-file-contents org-agenda-files)
  13601. (mapcar
  13602. (lambda (f)
  13603. (let ((e (expand-file-name (substitute-in-file-name f)
  13604. org-directory)))
  13605. (if pair-with-expansion
  13606. (cons e f)
  13607. e)))
  13608. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13609. ;;;###autoload
  13610. (defun org-cycle-agenda-files ()
  13611. "Cycle through the files in `org-agenda-files'.
  13612. If the current buffer visits an agenda file, find the next one in the list.
  13613. If the current buffer does not, find the first agenda file."
  13614. (interactive)
  13615. (let* ((fs (org-agenda-files t))
  13616. (files (append fs (list (car fs))))
  13617. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13618. file)
  13619. (unless files (error "No agenda files"))
  13620. (catch 'exit
  13621. (while (setq file (pop files))
  13622. (if (equal (file-truename file) tcf)
  13623. (when (car files)
  13624. (find-file (car files))
  13625. (throw 'exit t))))
  13626. (find-file (car fs)))
  13627. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13628. (defun org-agenda-file-to-front (&optional to-end)
  13629. "Move/add the current file to the top of the agenda file list.
  13630. If the file is not present in the list, it is added to the front. If it is
  13631. present, it is moved there. With optional argument TO-END, add/move to the
  13632. end of the list."
  13633. (interactive "P")
  13634. (let ((org-agenda-skip-unavailable-files nil)
  13635. (file-alist (mapcar (lambda (x)
  13636. (cons (file-truename x) x))
  13637. (org-agenda-files t)))
  13638. (ctf (file-truename buffer-file-name))
  13639. x had)
  13640. (setq x (assoc ctf file-alist) had x)
  13641. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13642. (if to-end
  13643. (setq file-alist (append (delq x file-alist) (list x)))
  13644. (setq file-alist (cons x (delq x file-alist))))
  13645. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13646. (org-install-agenda-files-menu)
  13647. (message "File %s to %s of agenda file list"
  13648. (if had "moved" "added") (if to-end "end" "front"))))
  13649. (defun org-remove-file (&optional file)
  13650. "Remove current file from the list of files in variable `org-agenda-files'.
  13651. These are the files which are being checked for agenda entries.
  13652. Optional argument FILE means use this file instead of the current."
  13653. (interactive)
  13654. (let* ((org-agenda-skip-unavailable-files nil)
  13655. (file (or file buffer-file-name))
  13656. (true-file (file-truename file))
  13657. (afile (abbreviate-file-name file))
  13658. (files (delq nil (mapcar
  13659. (lambda (x)
  13660. (if (equal true-file
  13661. (file-truename x))
  13662. nil x))
  13663. (org-agenda-files t)))))
  13664. (if (not (= (length files) (length (org-agenda-files t))))
  13665. (progn
  13666. (org-store-new-agenda-file-list files)
  13667. (org-install-agenda-files-menu)
  13668. (message "Removed file: %s" afile))
  13669. (message "File was not in list: %s (not removed)" afile))))
  13670. (defun org-file-menu-entry (file)
  13671. (vector file (list 'find-file file) t))
  13672. (defun org-check-agenda-file (file)
  13673. "Make sure FILE exists. If not, ask user what to do."
  13674. (when (not (file-exists-p file))
  13675. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13676. (abbreviate-file-name file))
  13677. (let ((r (downcase (read-char-exclusive))))
  13678. (cond
  13679. ((equal r ?r)
  13680. (org-remove-file file)
  13681. (throw 'nextfile t))
  13682. (t (error "Abort"))))))
  13683. (defun org-get-agenda-file-buffer (file)
  13684. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13685. it to the list of buffers which might be released later."
  13686. (let ((buf (org-find-base-buffer-visiting file)))
  13687. (if buf
  13688. buf ; just return it
  13689. ;; Make a new buffer and remember it
  13690. (setq buf (find-file-noselect file))
  13691. (if buf (push buf org-agenda-new-buffers))
  13692. buf)))
  13693. (defun org-release-buffers (blist)
  13694. "Release all buffers in list, asking the user for confirmation when needed.
  13695. When a buffer is unmodified, it is just killed. When modified, it is saved
  13696. \(if the user agrees) and then killed."
  13697. (let (buf file)
  13698. (while (setq buf (pop blist))
  13699. (setq file (buffer-file-name buf))
  13700. (when (and (buffer-modified-p buf)
  13701. file
  13702. (y-or-n-p (format "Save file %s? " file)))
  13703. (with-current-buffer buf (save-buffer)))
  13704. (kill-buffer buf))))
  13705. (defun org-prepare-agenda-buffers (files)
  13706. "Create buffers for all agenda files, protect archived trees and comments."
  13707. (interactive)
  13708. (let ((pa '(:org-archived t))
  13709. (pc '(:org-comment t))
  13710. (pall '(:org-archived t :org-comment t))
  13711. (inhibit-read-only t)
  13712. (rea (concat ":" org-archive-tag ":"))
  13713. bmp file re)
  13714. (save-excursion
  13715. (save-restriction
  13716. (while (setq file (pop files))
  13717. (catch 'nextfile
  13718. (if (bufferp file)
  13719. (set-buffer file)
  13720. (org-check-agenda-file file)
  13721. (set-buffer (org-get-agenda-file-buffer file)))
  13722. (widen)
  13723. (setq bmp (buffer-modified-p))
  13724. (org-refresh-category-properties)
  13725. (setq org-todo-keywords-for-agenda
  13726. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13727. (setq org-done-keywords-for-agenda
  13728. (append org-done-keywords-for-agenda org-done-keywords))
  13729. (setq org-todo-keyword-alist-for-agenda
  13730. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13731. (setq org-drawers-for-agenda
  13732. (append org-drawers-for-agenda org-drawers))
  13733. (setq org-tag-alist-for-agenda
  13734. (append org-tag-alist-for-agenda org-tag-alist))
  13735. (save-excursion
  13736. (remove-text-properties (point-min) (point-max) pall)
  13737. (when org-agenda-skip-archived-trees
  13738. (goto-char (point-min))
  13739. (while (re-search-forward rea nil t)
  13740. (if (org-on-heading-p t)
  13741. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13742. (goto-char (point-min))
  13743. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13744. (while (re-search-forward re nil t)
  13745. (add-text-properties
  13746. (match-beginning 0) (org-end-of-subtree t) pc)))
  13747. (set-buffer-modified-p bmp)))))
  13748. (setq org-todo-keywords-for-agenda
  13749. (org-uniquify org-todo-keywords-for-agenda))
  13750. (setq org-todo-keyword-alist-for-agenda
  13751. (org-uniquify org-todo-keyword-alist-for-agenda)
  13752. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13753. ;;;; Embedded LaTeX
  13754. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13755. "Keymap for the minor `org-cdlatex-mode'.")
  13756. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13757. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13758. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13759. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13760. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13761. (defvar org-cdlatex-texmathp-advice-is-done nil
  13762. "Flag remembering if we have applied the advice to texmathp already.")
  13763. (define-minor-mode org-cdlatex-mode
  13764. "Toggle the minor `org-cdlatex-mode'.
  13765. This mode supports entering LaTeX environment and math in LaTeX fragments
  13766. in Org-mode.
  13767. \\{org-cdlatex-mode-map}"
  13768. nil " OCDL" nil
  13769. (when org-cdlatex-mode (require 'cdlatex))
  13770. (unless org-cdlatex-texmathp-advice-is-done
  13771. (setq org-cdlatex-texmathp-advice-is-done t)
  13772. (defadvice texmathp (around org-math-always-on activate)
  13773. "Always return t in org-mode buffers.
  13774. This is because we want to insert math symbols without dollars even outside
  13775. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13776. an embedded LaTeX fragment, let texmathp do its job.
  13777. \\[org-cdlatex-mode-map]"
  13778. (interactive)
  13779. (let (p)
  13780. (cond
  13781. ((not (org-mode-p)) ad-do-it)
  13782. ((eq this-command 'cdlatex-math-symbol)
  13783. (setq ad-return-value t
  13784. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13785. (t
  13786. (let ((p (org-inside-LaTeX-fragment-p)))
  13787. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13788. (setq ad-return-value t
  13789. texmathp-why '("Org-mode embedded math" . 0))
  13790. (if p ad-do-it)))))))))
  13791. (defun turn-on-org-cdlatex ()
  13792. "Unconditionally turn on `org-cdlatex-mode'."
  13793. (org-cdlatex-mode 1))
  13794. (defun org-inside-LaTeX-fragment-p ()
  13795. "Test if point is inside a LaTeX fragment.
  13796. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13797. sequence appearing also before point.
  13798. Even though the matchers for math are configurable, this function assumes
  13799. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13800. delimiters are skipped when they have been removed by customization.
  13801. The return value is nil, or a cons cell with the delimiter and
  13802. and the position of this delimiter.
  13803. This function does a reasonably good job, but can locally be fooled by
  13804. for example currency specifications. For example it will assume being in
  13805. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13806. fragments that are properly closed, but during editing, we have to live
  13807. with the uncertainty caused by missing closing delimiters. This function
  13808. looks only before point, not after."
  13809. (catch 'exit
  13810. (let ((pos (point))
  13811. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13812. (lim (progn
  13813. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13814. (point)))
  13815. dd-on str (start 0) m re)
  13816. (goto-char pos)
  13817. (when dodollar
  13818. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13819. re (nth 1 (assoc "$" org-latex-regexps)))
  13820. (while (string-match re str start)
  13821. (cond
  13822. ((= (match-end 0) (length str))
  13823. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13824. ((= (match-end 0) (- (length str) 5))
  13825. (throw 'exit nil))
  13826. (t (setq start (match-end 0))))))
  13827. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13828. (goto-char pos)
  13829. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13830. (and (match-beginning 2) (throw 'exit nil))
  13831. ;; count $$
  13832. (while (re-search-backward "\\$\\$" lim t)
  13833. (setq dd-on (not dd-on)))
  13834. (goto-char pos)
  13835. (if dd-on (cons "$$" m))))))
  13836. (defun org-inside-latex-macro-p ()
  13837. "Is point inside a LaTeX macro or its arguments?"
  13838. (save-match-data
  13839. (org-in-regexp
  13840. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13841. (defun test ()
  13842. (interactive)
  13843. (message "%s" (org-inside-latex-macro-p)))
  13844. (defun org-try-cdlatex-tab ()
  13845. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13846. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13847. - inside a LaTeX fragment, or
  13848. - after the first word in a line, where an abbreviation expansion could
  13849. insert a LaTeX environment."
  13850. (when org-cdlatex-mode
  13851. (cond
  13852. ((save-excursion
  13853. (skip-chars-backward "a-zA-Z0-9*")
  13854. (skip-chars-backward " \t")
  13855. (bolp))
  13856. (cdlatex-tab) t)
  13857. ((org-inside-LaTeX-fragment-p)
  13858. (cdlatex-tab) t)
  13859. (t nil))))
  13860. (defun org-cdlatex-underscore-caret (&optional arg)
  13861. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13862. Revert to the normal definition outside of these fragments."
  13863. (interactive "P")
  13864. (if (org-inside-LaTeX-fragment-p)
  13865. (call-interactively 'cdlatex-sub-superscript)
  13866. (let (org-cdlatex-mode)
  13867. (call-interactively (key-binding (vector last-input-event))))))
  13868. (defun org-cdlatex-math-modify (&optional arg)
  13869. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13870. Revert to the normal definition outside of these fragments."
  13871. (interactive "P")
  13872. (if (org-inside-LaTeX-fragment-p)
  13873. (call-interactively 'cdlatex-math-modify)
  13874. (let (org-cdlatex-mode)
  13875. (call-interactively (key-binding (vector last-input-event))))))
  13876. (defvar org-latex-fragment-image-overlays nil
  13877. "List of overlays carrying the images of latex fragments.")
  13878. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13879. (defun org-remove-latex-fragment-image-overlays ()
  13880. "Remove all overlays with LaTeX fragment images in current buffer."
  13881. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  13882. (setq org-latex-fragment-image-overlays nil))
  13883. (defun org-preview-latex-fragment (&optional subtree)
  13884. "Preview the LaTeX fragment at point, or all locally or globally.
  13885. If the cursor is in a LaTeX fragment, create the image and overlay
  13886. it over the source code. If there is no fragment at point, display
  13887. all fragments in the current text, from one headline to the next. With
  13888. prefix SUBTREE, display all fragments in the current subtree. With a
  13889. double prefix `C-u C-u', or when the cursor is before the first headline,
  13890. display all fragments in the buffer.
  13891. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13892. (interactive "P")
  13893. (org-remove-latex-fragment-image-overlays)
  13894. (save-excursion
  13895. (save-restriction
  13896. (let (beg end at msg)
  13897. (cond
  13898. ((or (equal subtree '(16))
  13899. (not (save-excursion
  13900. (re-search-backward (concat "^" outline-regexp) nil t))))
  13901. (setq beg (point-min) end (point-max)
  13902. msg "Creating images for buffer...%s"))
  13903. ((equal subtree '(4))
  13904. (org-back-to-heading)
  13905. (setq beg (point) end (org-end-of-subtree t)
  13906. msg "Creating images for subtree...%s"))
  13907. (t
  13908. (if (setq at (org-inside-LaTeX-fragment-p))
  13909. (goto-char (max (point-min) (- (cdr at) 2)))
  13910. (org-back-to-heading))
  13911. (setq beg (point) end (progn (outline-next-heading) (point))
  13912. msg (if at "Creating image...%s"
  13913. "Creating images for entry...%s"))))
  13914. (message msg "")
  13915. (narrow-to-region beg end)
  13916. (goto-char beg)
  13917. (org-format-latex
  13918. (concat "ltxpng/" (file-name-sans-extension
  13919. (file-name-nondirectory
  13920. buffer-file-name)))
  13921. default-directory 'overlays msg at 'forbuffer)
  13922. (message msg "done. Use `C-c C-c' to remove images.")))))
  13923. (defvar org-latex-regexps
  13924. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13925. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13926. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13927. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13928. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13929. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13930. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13931. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13932. "Regular expressions for matching embedded LaTeX.")
  13933. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13934. "Replace LaTeX fragments with links to an image, and produce images.
  13935. Some of the options can be changed using the variable
  13936. `org-format-latex-options'."
  13937. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13938. (let* ((prefixnodir (file-name-nondirectory prefix))
  13939. (absprefix (expand-file-name prefix dir))
  13940. (todir (file-name-directory absprefix))
  13941. (opt org-format-latex-options)
  13942. (matchers (plist-get opt :matchers))
  13943. (re-list org-latex-regexps)
  13944. (org-format-latex-header-extra
  13945. (plist-get (org-infile-export-plist) :latex-header-extra))
  13946. (cnt 0) txt hash link beg end re e checkdir
  13947. executables-checked
  13948. m n block linkfile movefile ov)
  13949. ;; Check the different regular expressions
  13950. (while (setq e (pop re-list))
  13951. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13952. block (if (nth 3 e) "\n\n" ""))
  13953. (when (member m matchers)
  13954. (goto-char (point-min))
  13955. (while (re-search-forward re nil t)
  13956. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13957. (not (get-text-property (match-beginning n)
  13958. 'org-protected))
  13959. (or (not overlays)
  13960. (not (eq (get-char-property (match-beginning n)
  13961. 'org-overlay-type)
  13962. 'org-latex-overlay))))
  13963. (setq txt (match-string n)
  13964. beg (match-beginning n) end (match-end n)
  13965. cnt (1+ cnt))
  13966. (let (print-length print-level) ; make sure full list is printed
  13967. (setq hash (sha1 (prin1-to-string
  13968. (list org-format-latex-header
  13969. org-format-latex-header-extra
  13970. org-export-latex-default-packages-alist
  13971. org-export-latex-packages-alist
  13972. org-format-latex-options
  13973. forbuffer txt)))
  13974. linkfile (format "%s_%s.png" prefix hash)
  13975. movefile (format "%s_%s.png" absprefix hash)))
  13976. (setq link (concat block "[[file:" linkfile "]]" block))
  13977. (if msg (message msg cnt))
  13978. (goto-char beg)
  13979. (unless checkdir ; make sure the directory exists
  13980. (setq checkdir t)
  13981. (or (file-directory-p todir) (make-directory todir)))
  13982. (unless executables-checked
  13983. (org-check-external-command
  13984. "latex" "needed to convert LaTeX fragments to images")
  13985. (org-check-external-command
  13986. "dvipng" "needed to convert LaTeX fragments to images")
  13987. (setq executables-checked t))
  13988. (unless (file-exists-p movefile)
  13989. (org-create-formula-image
  13990. txt movefile opt forbuffer))
  13991. (if overlays
  13992. (progn
  13993. (mapc (lambda (o)
  13994. (if (eq (overlay-get o 'org-overlay-type)
  13995. 'org-latex-overlay)
  13996. (delete-overlay o)))
  13997. (org-overlays-in beg end))
  13998. (setq ov (make-overlay beg end))
  13999. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14000. (if (featurep 'xemacs)
  14001. (progn
  14002. (overlay-put ov 'invisible t)
  14003. (overlay-put
  14004. ov 'end-glyph
  14005. (make-glyph (vector 'png :file movefile))))
  14006. (overlay-put
  14007. ov 'display
  14008. (list 'image :type 'png :file movefile :ascent 'center)))
  14009. (push ov org-latex-fragment-image-overlays)
  14010. (goto-char end))
  14011. (delete-region beg end)
  14012. (insert (org-add-props link
  14013. (list 'org-latex-src
  14014. (replace-regexp-in-string "\"" "" txt)))))))))))
  14015. ;; This function borrows from Ganesh Swami's latex2png.el
  14016. (defun org-create-formula-image (string tofile options buffer)
  14017. "This calls dvipng."
  14018. (require 'org-latex)
  14019. (let* ((tmpdir (if (featurep 'xemacs)
  14020. (temp-directory)
  14021. temporary-file-directory))
  14022. (texfilebase (make-temp-name
  14023. (expand-file-name "orgtex" tmpdir)))
  14024. (texfile (concat texfilebase ".tex"))
  14025. (dvifile (concat texfilebase ".dvi"))
  14026. (pngfile (concat texfilebase ".png"))
  14027. (fnh (if (featurep 'xemacs)
  14028. (font-height (get-face-font 'default))
  14029. (face-attribute 'default :height nil)))
  14030. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14031. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14032. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14033. "Black"))
  14034. (bg (or (plist-get options (if buffer :background :html-background))
  14035. "Transparent")))
  14036. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14037. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14038. (with-temp-file texfile
  14039. (insert (org-splice-latex-header
  14040. org-format-latex-header
  14041. org-export-latex-default-packages-alist
  14042. org-export-latex-packages-alist
  14043. org-format-latex-header-extra))
  14044. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14045. (require 'org-latex)
  14046. (org-export-latex-fix-inputenc))
  14047. (let ((dir default-directory))
  14048. (condition-case nil
  14049. (progn
  14050. (cd tmpdir)
  14051. (call-process "latex" nil nil nil texfile))
  14052. (error nil))
  14053. (cd dir))
  14054. (if (not (file-exists-p dvifile))
  14055. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14056. (condition-case nil
  14057. (call-process "dvipng" nil nil nil
  14058. "-fg" fg "-bg" bg
  14059. "-D" dpi
  14060. ;;"-x" scale "-y" scale
  14061. "-T" "tight"
  14062. "-o" pngfile
  14063. dvifile)
  14064. (error nil))
  14065. (if (not (file-exists-p pngfile))
  14066. (if org-format-latex-signal-error
  14067. (error "Failed to create png file from %s" texfile)
  14068. (message "Failed to create png file from %s" texfile)
  14069. nil)
  14070. ;; Use the requested file name and clean up
  14071. (copy-file pngfile tofile 'replace)
  14072. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14073. (delete-file (concat texfilebase e)))
  14074. pngfile))))
  14075. (defun org-splice-latex-header (tpl def-pkg pkg &optional extra)
  14076. "Fill a LaTeX header template TPL.
  14077. In the template, the following place holders will be recognized:
  14078. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14079. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14080. [PACKAGES] \\usepackage statements for PKG
  14081. [NO-PACKAGES] do not include PKG
  14082. [EXTRA] the string EXTRA
  14083. [NO-EXTRA] do not include EXTRA
  14084. For backward compatibility, if both the positive and the negative place
  14085. holder is missing, the positive one (without the \"NO-\") will be
  14086. assumed to be present at the end of the template.
  14087. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14088. EXTRA is a string."
  14089. (let (rpl (end ""))
  14090. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14091. (setq rpl (if (or (match-end 1) (not def-pkg))
  14092. "" (org-latex-packages-to-string def-pkg t))
  14093. tpl (replace-match rpl t t tpl))
  14094. (if def-pkg (setq end (org-latex-packages-to-string def-pkg))))
  14095. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14096. (setq rpl (if (or (match-end 1) (not pkg))
  14097. "" (org-latex-packages-to-string pkg t))
  14098. tpl (replace-match rpl t t tpl))
  14099. (if pkg (setq end (concat end "\n" (org-latex-packages-to-string pkg)))))
  14100. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14101. (setq rpl (if (or (match-end 1) (not extra))
  14102. "" (concat extra "\n"))
  14103. tpl (replace-match rpl t t tpl))
  14104. (if (and extra (string-match "\\S-" extra))
  14105. (setq end (concat end "\n" extra))))
  14106. (if (string-match "\\S-" end)
  14107. (concat tpl "\n" end)
  14108. tpl)))
  14109. (defun org-latex-packages-to-string (pkg &optional newline)
  14110. "Turn an alist of packages into a string with the \\usepackage macros."
  14111. (setq pkg (mapconcat (lambda(p)
  14112. (cond
  14113. ((stringp p) p)
  14114. ((equal "" (car p))
  14115. (format "\\usepackage{%s}" (cadr p)))
  14116. (t
  14117. (format "\\usepackage[%s]{%s}"
  14118. (car p) (cadr p)))))
  14119. pkg
  14120. "\n"))
  14121. (if newline (concat pkg "\n") pkg))
  14122. (defun org-dvipng-color (attr)
  14123. "Return an rgb color specification for dvipng."
  14124. (apply 'format "rgb %s %s %s"
  14125. (mapcar 'org-normalize-color
  14126. (color-values (face-attribute 'default attr nil)))))
  14127. (defun org-normalize-color (value)
  14128. "Return string to be used as color value for an RGB component."
  14129. (format "%g" (/ value 65535.0)))
  14130. ;;;; Key bindings
  14131. ;; Make `C-c C-x' a prefix key
  14132. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14133. ;; TAB key with modifiers
  14134. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14135. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14136. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14137. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14138. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14139. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14140. ;; The following line is necessary under Suse GNU/Linux
  14141. (unless (featurep 'xemacs)
  14142. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14143. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14144. (define-key org-mode-map [backtab] 'org-shifttab)
  14145. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14146. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14147. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14148. ;; Cursor keys with modifiers
  14149. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14150. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14151. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14152. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14153. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14154. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14155. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14156. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14157. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14158. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14159. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14160. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14161. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14162. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14163. ;;; Extra keys for tty access.
  14164. ;; We only set them when really needed because otherwise the
  14165. ;; menus don't show the simple keys
  14166. (when (or org-use-extra-keys
  14167. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14168. (not window-system))
  14169. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14170. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14171. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14172. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14173. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14174. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14175. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14176. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14177. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14178. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14179. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14180. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14181. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14182. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14183. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14184. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14185. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14186. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14187. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14188. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14189. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14190. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14191. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14192. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14193. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14194. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14195. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14196. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14197. ;; All the other keys
  14198. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14199. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14200. (if (boundp 'narrow-map)
  14201. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14202. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14203. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14204. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14205. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14206. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14207. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14208. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14209. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14210. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14211. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14212. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14213. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14214. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14215. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14216. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14217. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14218. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14219. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14220. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14221. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14222. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14223. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14224. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14225. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14226. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14227. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14228. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14229. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14230. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14231. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14232. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14233. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14234. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14235. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14236. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14237. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14238. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14239. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14240. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14241. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14242. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14243. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14244. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14245. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14246. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14247. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14248. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14249. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14250. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14251. (org-defkey org-mode-map "\C-m" 'org-return)
  14252. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14253. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14254. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14255. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14256. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14257. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14258. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14259. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14260. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14261. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14262. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14263. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14264. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14265. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14266. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14267. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14268. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14269. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14270. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14271. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14272. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14273. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14274. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14275. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14276. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14277. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14278. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14279. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14280. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14281. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14282. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14283. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14284. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14285. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14286. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14287. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14288. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14289. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14290. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14291. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14292. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14293. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14294. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14295. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14296. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14297. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14298. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14299. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14300. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14301. (when (featurep 'xemacs)
  14302. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14303. (defconst org-speed-commands-default
  14304. '(
  14305. ("Outline Navigation")
  14306. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14307. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14308. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14309. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14310. ("u" . (org-speed-move-safe 'outline-up-heading))
  14311. ("j" . org-goto)
  14312. ("g" . (org-refile t))
  14313. ("Outline Visibility")
  14314. ("c" . org-cycle)
  14315. ("C" . org-shifttab)
  14316. (" " . org-display-outline-path)
  14317. ("Outline Structure Editing")
  14318. ("U" . org-shiftmetaup)
  14319. ("D" . org-shiftmetadown)
  14320. ("r" . org-metaright)
  14321. ("l" . org-metaleft)
  14322. ("R" . org-shiftmetaright)
  14323. ("L" . org-shiftmetaleft)
  14324. ("i" . (progn (forward-char 1) (call-interactively
  14325. 'org-insert-heading-respect-content)))
  14326. ("^" . org-sort)
  14327. ("w" . org-refile)
  14328. ("a" . org-archive-subtree-default-with-confirmation)
  14329. ("." . outline-mark-subtree)
  14330. ("Clock Commands")
  14331. ("I" . org-clock-in)
  14332. ("O" . org-clock-out)
  14333. ("Meta Data Editing")
  14334. ("t" . org-todo)
  14335. ("0" . (org-priority ?\ ))
  14336. ("1" . (org-priority ?A))
  14337. ("2" . (org-priority ?B))
  14338. ("3" . (org-priority ?C))
  14339. (";" . org-set-tags-command)
  14340. ("e" . org-set-effort)
  14341. ("Agenda Views etc")
  14342. ("v" . org-agenda)
  14343. ("/" . org-sparse-tree)
  14344. ("Misc")
  14345. ("o" . org-open-at-point)
  14346. ("?" . org-speed-command-help)
  14347. )
  14348. "The default speed commands.")
  14349. (defun org-print-speed-command (e)
  14350. (if (> (length (car e)) 1)
  14351. (progn
  14352. (princ "\n")
  14353. (princ (car e))
  14354. (princ "\n")
  14355. (princ (make-string (length (car e)) ?-))
  14356. (princ "\n"))
  14357. (princ (car e))
  14358. (princ " ")
  14359. (if (symbolp (cdr e))
  14360. (princ (symbol-name (cdr e)))
  14361. (prin1 (cdr e)))
  14362. (princ "\n")))
  14363. (defun org-speed-command-help ()
  14364. "Show the available speed commands."
  14365. (interactive)
  14366. (if (not org-use-speed-commands)
  14367. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14368. (with-output-to-temp-buffer "*Help*"
  14369. (princ "User-defined Speed commands\n===========================\n")
  14370. (mapc 'org-print-speed-command org-speed-commands-user)
  14371. (princ "\n")
  14372. (princ "Built-in Speed commands\n=======================\n")
  14373. (mapc 'org-print-speed-command org-speed-commands-default))
  14374. (with-current-buffer "*Help*"
  14375. (setq truncate-lines t))))
  14376. (defun org-speed-move-safe (cmd)
  14377. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14378. If not, return to the original position and throw an error."
  14379. (interactive)
  14380. (let ((pos (point)))
  14381. (call-interactively cmd)
  14382. (unless (and (bolp) (org-on-heading-p))
  14383. (goto-char pos)
  14384. (error "Boundary reached while executing %s" cmd))))
  14385. (defvar org-self-insert-command-undo-counter 0)
  14386. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14387. (defvar org-speed-command nil)
  14388. (defun org-self-insert-command (N)
  14389. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14390. If the cursor is in a table looking at whitespace, the whitespace is
  14391. overwritten, and the table is not marked as requiring realignment."
  14392. (interactive "p")
  14393. (cond
  14394. ((and org-use-speed-commands
  14395. (or (and (bolp) (looking-at outline-regexp))
  14396. (and (functionp org-use-speed-commands)
  14397. (funcall org-use-speed-commands)))
  14398. (setq
  14399. org-speed-command
  14400. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14401. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14402. (cond
  14403. ((commandp org-speed-command)
  14404. (setq this-command org-speed-command)
  14405. (call-interactively org-speed-command))
  14406. ((functionp org-speed-command)
  14407. (funcall org-speed-command))
  14408. ((and org-speed-command (listp org-speed-command))
  14409. (eval org-speed-command))
  14410. (t (let (org-use-speed-commands)
  14411. (call-interactively 'org-self-insert-command)))))
  14412. ((and
  14413. (org-table-p)
  14414. (progn
  14415. ;; check if we blank the field, and if that triggers align
  14416. (and (featurep 'org-table) org-table-auto-blank-field
  14417. (member last-command
  14418. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14419. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14420. ;; got extra space, this field does not determine column width
  14421. (let (org-table-may-need-update) (org-table-blank-field))
  14422. ;; no extra space, this field may determine column width
  14423. (org-table-blank-field)))
  14424. t)
  14425. (eq N 1)
  14426. (looking-at "[^|\n]* |"))
  14427. (let (org-table-may-need-update)
  14428. (goto-char (1- (match-end 0)))
  14429. (delete-backward-char 1)
  14430. (goto-char (match-beginning 0))
  14431. (self-insert-command N)))
  14432. (t
  14433. (setq org-table-may-need-update t)
  14434. (self-insert-command N)
  14435. (org-fix-tags-on-the-fly)
  14436. (if org-self-insert-cluster-for-undo
  14437. (if (not (eq last-command 'org-self-insert-command))
  14438. (setq org-self-insert-command-undo-counter 1)
  14439. (if (>= org-self-insert-command-undo-counter 20)
  14440. (setq org-self-insert-command-undo-counter 1)
  14441. (and (> org-self-insert-command-undo-counter 0)
  14442. buffer-undo-list
  14443. (not (cadr buffer-undo-list)) ; remove nil entry
  14444. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14445. (setq org-self-insert-command-undo-counter
  14446. (1+ org-self-insert-command-undo-counter))))))))
  14447. (defun org-fix-tags-on-the-fly ()
  14448. (when (and (equal (char-after (point-at-bol)) ?*)
  14449. (org-on-heading-p))
  14450. (org-align-tags-here org-tags-column)))
  14451. (defun org-delete-backward-char (N)
  14452. "Like `delete-backward-char', insert whitespace at field end in tables.
  14453. When deleting backwards, in tables this function will insert whitespace in
  14454. front of the next \"|\" separator, to keep the table aligned. The table will
  14455. still be marked for re-alignment if the field did fill the entire column,
  14456. because, in this case the deletion might narrow the column."
  14457. (interactive "p")
  14458. (if (and (org-table-p)
  14459. (eq N 1)
  14460. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14461. (looking-at ".*?|"))
  14462. (let ((pos (point))
  14463. (noalign (looking-at "[^|\n\r]* |"))
  14464. (c org-table-may-need-update))
  14465. (backward-delete-char N)
  14466. (skip-chars-forward "^|")
  14467. (insert " ")
  14468. (goto-char (1- pos))
  14469. ;; noalign: if there were two spaces at the end, this field
  14470. ;; does not determine the width of the column.
  14471. (if noalign (setq org-table-may-need-update c)))
  14472. (backward-delete-char N)
  14473. (org-fix-tags-on-the-fly)))
  14474. (defun org-delete-char (N)
  14475. "Like `delete-char', but insert whitespace at field end in tables.
  14476. When deleting characters, in tables this function will insert whitespace in
  14477. front of the next \"|\" separator, to keep the table aligned. The table will
  14478. still be marked for re-alignment if the field did fill the entire column,
  14479. because, in this case the deletion might narrow the column."
  14480. (interactive "p")
  14481. (if (and (org-table-p)
  14482. (not (bolp))
  14483. (not (= (char-after) ?|))
  14484. (eq N 1))
  14485. (if (looking-at ".*?|")
  14486. (let ((pos (point))
  14487. (noalign (looking-at "[^|\n\r]* |"))
  14488. (c org-table-may-need-update))
  14489. (replace-match (concat
  14490. (substring (match-string 0) 1 -1)
  14491. " |"))
  14492. (goto-char pos)
  14493. ;; noalign: if there were two spaces at the end, this field
  14494. ;; does not determine the width of the column.
  14495. (if noalign (setq org-table-may-need-update c)))
  14496. (delete-char N))
  14497. (delete-char N)
  14498. (org-fix-tags-on-the-fly)))
  14499. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14500. (put 'org-self-insert-command 'delete-selection t)
  14501. (put 'orgtbl-self-insert-command 'delete-selection t)
  14502. (put 'org-delete-char 'delete-selection 'supersede)
  14503. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14504. (put 'org-yank 'delete-selection 'yank)
  14505. ;; Make `flyspell-mode' delay after some commands
  14506. (put 'org-self-insert-command 'flyspell-delayed t)
  14507. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14508. (put 'org-delete-char 'flyspell-delayed t)
  14509. (put 'org-delete-backward-char 'flyspell-delayed t)
  14510. ;; Make pabbrev-mode expand after org-mode commands
  14511. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14512. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14513. ;; How to do this: Measure non-white length of current string
  14514. ;; If equal to column width, we should realign.
  14515. (defun org-remap (map &rest commands)
  14516. "In MAP, remap the functions given in COMMANDS.
  14517. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14518. (let (new old)
  14519. (while commands
  14520. (setq old (pop commands) new (pop commands))
  14521. (if (fboundp 'command-remapping)
  14522. (org-defkey map (vector 'remap old) new)
  14523. (substitute-key-definition old new map global-map)))))
  14524. (when (eq org-enable-table-editor 'optimized)
  14525. ;; If the user wants maximum table support, we need to hijack
  14526. ;; some standard editing functions
  14527. (org-remap org-mode-map
  14528. 'self-insert-command 'org-self-insert-command
  14529. 'delete-char 'org-delete-char
  14530. 'delete-backward-char 'org-delete-backward-char)
  14531. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14532. (defvar org-ctrl-c-ctrl-c-hook nil
  14533. "Hook for functions attaching themselves to `C-c C-c'.
  14534. This can be used to add additional functionality to the C-c C-c key which
  14535. executes context-dependent commands.
  14536. Each function will be called with no arguments. The function must check
  14537. if the context is appropriate for it to act. If yes, it should do its
  14538. thing and then return a non-nil value. If the context is wrong,
  14539. just do nothing and return nil.")
  14540. (defvar org-tab-first-hook nil
  14541. "Hook for functions to attach themselves to TAB.
  14542. See `org-ctrl-c-ctrl-c-hook' for more information.
  14543. This hook runs as the first action when TAB is pressed, even before
  14544. `org-cycle' messes around with the `outline-regexp' to cater for
  14545. inline tasks and plain list item folding.
  14546. If any function in this hook returns t, not other actions like table
  14547. field motion visibility cycling will be done.")
  14548. (defvar org-tab-after-check-for-table-hook nil
  14549. "Hook for functions to attach themselves to TAB.
  14550. See `org-ctrl-c-ctrl-c-hook' for more information.
  14551. This hook runs after it has been established that the cursor is not in a
  14552. table, but before checking if the cursor is in a headline or if global cycling
  14553. should be done.
  14554. If any function in this hook returns t, not other actions like visibility
  14555. cycling will be done.")
  14556. (defvar org-tab-after-check-for-cycling-hook nil
  14557. "Hook for functions to attach themselves to TAB.
  14558. See `org-ctrl-c-ctrl-c-hook' for more information.
  14559. This hook runs after it has been established that not table field motion and
  14560. not visibility should be done because of current context. This is probably
  14561. the place where a package like yasnippets can hook in.")
  14562. (defvar org-tab-before-tab-emulation-hook nil
  14563. "Hook for functions to attach themselves to TAB.
  14564. See `org-ctrl-c-ctrl-c-hook' for more information.
  14565. This hook runs after every other options for TAB have been exhausted, but
  14566. before indentation and \t insertion takes place.")
  14567. (defvar org-metaleft-hook nil
  14568. "Hook for functions attaching themselves to `M-left'.
  14569. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14570. (defvar org-metaright-hook nil
  14571. "Hook for functions attaching themselves to `M-right'.
  14572. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14573. (defvar org-metaup-hook nil
  14574. "Hook for functions attaching themselves to `M-up'.
  14575. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14576. (defvar org-metadown-hook nil
  14577. "Hook for functions attaching themselves to `M-down'.
  14578. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14579. (defvar org-shiftmetaleft-hook nil
  14580. "Hook for functions attaching themselves to `M-S-left'.
  14581. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14582. (defvar org-shiftmetaright-hook nil
  14583. "Hook for functions attaching themselves to `M-S-right'.
  14584. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14585. (defvar org-shiftmetaup-hook nil
  14586. "Hook for functions attaching themselves to `M-S-up'.
  14587. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14588. (defvar org-shiftmetadown-hook nil
  14589. "Hook for functions attaching themselves to `M-S-down'.
  14590. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14591. (defvar org-metareturn-hook nil
  14592. "Hook for functions attaching themselves to `M-RET'.
  14593. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14594. (defun org-modifier-cursor-error ()
  14595. "Throw an error, a modified cursor command was applied in wrong context."
  14596. (error "This command is active in special context like tables, headlines or items"))
  14597. (defun org-shiftselect-error ()
  14598. "Throw an error because Shift-Cursor command was applied in wrong context."
  14599. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14600. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14601. (error "This command works only in special context like headlines or timestamps")))
  14602. (defun org-call-for-shift-select (cmd)
  14603. (let ((this-command-keys-shift-translated t))
  14604. (call-interactively cmd)))
  14605. (defun org-shifttab (&optional arg)
  14606. "Global visibility cycling or move to previous table field.
  14607. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14608. on context.
  14609. See the individual commands for more information."
  14610. (interactive "P")
  14611. (cond
  14612. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14613. ((integerp arg)
  14614. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14615. (message "Content view to level: %d" arg)
  14616. (org-content (prefix-numeric-value arg2))
  14617. (setq org-cycle-global-status 'overview)))
  14618. (t (call-interactively 'org-global-cycle))))
  14619. (defun org-shiftmetaleft ()
  14620. "Promote subtree or delete table column.
  14621. Calls `org-promote-subtree', `org-outdent-item',
  14622. or `org-table-delete-column', depending on context.
  14623. See the individual commands for more information."
  14624. (interactive)
  14625. (cond
  14626. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14627. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14628. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14629. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14630. (t (org-modifier-cursor-error))))
  14631. (defun org-shiftmetaright ()
  14632. "Demote subtree or insert table column.
  14633. Calls `org-demote-subtree', `org-indent-item',
  14634. or `org-table-insert-column', depending on context.
  14635. See the individual commands for more information."
  14636. (interactive)
  14637. (cond
  14638. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14639. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14640. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14641. ((org-at-item-p) (call-interactively 'org-indent-item))
  14642. (t (org-modifier-cursor-error))))
  14643. (defun org-shiftmetaup (&optional arg)
  14644. "Move subtree up or kill table row.
  14645. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14646. `org-move-item-up' depending on context. See the individual commands
  14647. for more information."
  14648. (interactive "P")
  14649. (cond
  14650. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14651. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14652. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14653. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14654. (t (org-modifier-cursor-error))))
  14655. (defun org-shiftmetadown (&optional arg)
  14656. "Move subtree down or insert table row.
  14657. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14658. `org-move-item-down', depending on context. See the individual
  14659. commands for more information."
  14660. (interactive "P")
  14661. (cond
  14662. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14663. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14664. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14665. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14666. (t (org-modifier-cursor-error))))
  14667. (defun org-metaleft (&optional arg)
  14668. "Promote heading or move table column to left.
  14669. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14670. With no specific context, calls the Emacs default `backward-word'.
  14671. See the individual commands for more information."
  14672. (interactive "P")
  14673. (cond
  14674. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14675. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14676. ((or (org-on-heading-p)
  14677. (and (org-region-active-p)
  14678. (save-excursion
  14679. (goto-char (region-beginning))
  14680. (org-on-heading-p))))
  14681. (call-interactively 'org-do-promote))
  14682. ((or (org-at-item-p)
  14683. (and (org-region-active-p)
  14684. (save-excursion
  14685. (goto-char (region-beginning))
  14686. (org-at-item-p))))
  14687. (call-interactively 'org-outdent-item))
  14688. (t (call-interactively 'backward-word))))
  14689. (defun org-metaright (&optional arg)
  14690. "Demote subtree or move table column to right.
  14691. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14692. With no specific context, calls the Emacs default `forward-word'.
  14693. See the individual commands for more information."
  14694. (interactive "P")
  14695. (cond
  14696. ((run-hook-with-args-until-success 'org-metaright-hook))
  14697. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14698. ((or (org-on-heading-p)
  14699. (and (org-region-active-p)
  14700. (save-excursion
  14701. (goto-char (region-beginning))
  14702. (org-on-heading-p))))
  14703. (call-interactively 'org-do-demote))
  14704. ((or (org-at-item-p)
  14705. (and (org-region-active-p)
  14706. (save-excursion
  14707. (goto-char (region-beginning))
  14708. (org-at-item-p))))
  14709. (call-interactively 'org-indent-item))
  14710. (t (call-interactively 'forward-word))))
  14711. (defun org-metaup (&optional arg)
  14712. "Move subtree up or move table row up.
  14713. Calls `org-move-subtree-up' or `org-table-move-row' or
  14714. `org-move-item-up', depending on context. See the individual commands
  14715. for more information."
  14716. (interactive "P")
  14717. (cond
  14718. ((run-hook-with-args-until-success 'org-metaup-hook))
  14719. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14720. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14721. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14722. (t (transpose-lines 1) (beginning-of-line -1))))
  14723. (defun org-metadown (&optional arg)
  14724. "Move subtree down or move table row down.
  14725. Calls `org-move-subtree-down' or `org-table-move-row' or
  14726. `org-move-item-down', depending on context. See the individual
  14727. commands for more information."
  14728. (interactive "P")
  14729. (cond
  14730. ((run-hook-with-args-until-success 'org-metadown-hook))
  14731. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14732. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14733. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14734. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14735. (defun org-shiftup (&optional arg)
  14736. "Increase item in timestamp or increase priority of current headline.
  14737. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14738. depending on context. See the individual commands for more information."
  14739. (interactive "P")
  14740. (cond
  14741. ((and org-support-shift-select (org-region-active-p))
  14742. (org-call-for-shift-select 'previous-line))
  14743. ((org-at-timestamp-p t)
  14744. (call-interactively (if org-edit-timestamp-down-means-later
  14745. 'org-timestamp-down 'org-timestamp-up)))
  14746. ((and (not (eq org-support-shift-select 'always))
  14747. org-enable-priority-commands
  14748. (org-on-heading-p))
  14749. (call-interactively 'org-priority-up))
  14750. ((and (not org-support-shift-select) (org-at-item-p))
  14751. (call-interactively 'org-previous-item))
  14752. ((org-clocktable-try-shift 'up arg))
  14753. (org-support-shift-select
  14754. (org-call-for-shift-select 'previous-line))
  14755. (t (org-shiftselect-error))))
  14756. (defun org-shiftdown (&optional arg)
  14757. "Decrease item in timestamp or decrease priority of current headline.
  14758. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14759. depending on context. See the individual commands for more information."
  14760. (interactive "P")
  14761. (cond
  14762. ((and org-support-shift-select (org-region-active-p))
  14763. (org-call-for-shift-select 'next-line))
  14764. ((org-at-timestamp-p t)
  14765. (call-interactively (if org-edit-timestamp-down-means-later
  14766. 'org-timestamp-up 'org-timestamp-down)))
  14767. ((and (not (eq org-support-shift-select 'always))
  14768. org-enable-priority-commands
  14769. (org-on-heading-p))
  14770. (call-interactively 'org-priority-down))
  14771. ((and (not org-support-shift-select) (org-at-item-p))
  14772. (call-interactively 'org-next-item))
  14773. ((org-clocktable-try-shift 'down arg))
  14774. (org-support-shift-select
  14775. (org-call-for-shift-select 'next-line))
  14776. (t (org-shiftselect-error))))
  14777. (defun org-shiftright (&optional arg)
  14778. "Cycle the thing at point or in the current line, depending on context.
  14779. Depending on context, this does one of the following:
  14780. - switch a timestamp at point one day into the future
  14781. - on a headline, switch to the next TODO keyword.
  14782. - on an item, switch entire list to the next bullet type
  14783. - on a property line, switch to the next allowed value
  14784. - on a clocktable definition line, move time block into the future"
  14785. (interactive "P")
  14786. (cond
  14787. ((and org-support-shift-select (org-region-active-p))
  14788. (org-call-for-shift-select 'forward-char))
  14789. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14790. ((and (not (eq org-support-shift-select 'always))
  14791. (org-on-heading-p))
  14792. (let ((org-inhibit-logging
  14793. (not org-treat-S-cursor-todo-selection-as-state-change))
  14794. (org-inhibit-blocking
  14795. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14796. (org-call-with-arg 'org-todo 'right)))
  14797. ((or (and org-support-shift-select
  14798. (not (eq org-support-shift-select 'always))
  14799. (org-at-item-bullet-p))
  14800. (and (not org-support-shift-select) (org-at-item-p)))
  14801. (org-call-with-arg 'org-cycle-list-bullet nil))
  14802. ((and (not (eq org-support-shift-select 'always))
  14803. (org-at-property-p))
  14804. (call-interactively 'org-property-next-allowed-value))
  14805. ((org-clocktable-try-shift 'right arg))
  14806. (org-support-shift-select
  14807. (org-call-for-shift-select 'forward-char))
  14808. (t (org-shiftselect-error))))
  14809. (defun org-shiftleft (&optional arg)
  14810. "Cycle the thing at point or in the current line, depending on context.
  14811. Depending on context, this does one of the following:
  14812. - switch a timestamp at point one day into the past
  14813. - on a headline, switch to the previous TODO keyword.
  14814. - on an item, switch entire list to the previous bullet type
  14815. - on a property line, switch to the previous allowed value
  14816. - on a clocktable definition line, move time block into the past"
  14817. (interactive "P")
  14818. (cond
  14819. ((and org-support-shift-select (org-region-active-p))
  14820. (org-call-for-shift-select 'backward-char))
  14821. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14822. ((and (not (eq org-support-shift-select 'always))
  14823. (org-on-heading-p))
  14824. (let ((org-inhibit-logging
  14825. (not org-treat-S-cursor-todo-selection-as-state-change))
  14826. (org-inhibit-blocking
  14827. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14828. (org-call-with-arg 'org-todo 'left)))
  14829. ((or (and org-support-shift-select
  14830. (not (eq org-support-shift-select 'always))
  14831. (org-at-item-bullet-p))
  14832. (and (not org-support-shift-select) (org-at-item-p)))
  14833. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14834. ((and (not (eq org-support-shift-select 'always))
  14835. (org-at-property-p))
  14836. (call-interactively 'org-property-previous-allowed-value))
  14837. ((org-clocktable-try-shift 'left arg))
  14838. (org-support-shift-select
  14839. (org-call-for-shift-select 'backward-char))
  14840. (t (org-shiftselect-error))))
  14841. (defun org-shiftcontrolright ()
  14842. "Switch to next TODO set."
  14843. (interactive)
  14844. (cond
  14845. ((and org-support-shift-select (org-region-active-p))
  14846. (org-call-for-shift-select 'forward-word))
  14847. ((and (not (eq org-support-shift-select 'always))
  14848. (org-on-heading-p))
  14849. (org-call-with-arg 'org-todo 'nextset))
  14850. (org-support-shift-select
  14851. (org-call-for-shift-select 'forward-word))
  14852. (t (org-shiftselect-error))))
  14853. (defun org-shiftcontrolleft ()
  14854. "Switch to previous TODO set."
  14855. (interactive)
  14856. (cond
  14857. ((and org-support-shift-select (org-region-active-p))
  14858. (org-call-for-shift-select 'backward-word))
  14859. ((and (not (eq org-support-shift-select 'always))
  14860. (org-on-heading-p))
  14861. (org-call-with-arg 'org-todo 'previousset))
  14862. (org-support-shift-select
  14863. (org-call-for-shift-select 'backward-word))
  14864. (t (org-shiftselect-error))))
  14865. (defun org-ctrl-c-ret ()
  14866. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14867. (interactive)
  14868. (cond
  14869. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14870. (t (call-interactively 'org-insert-heading))))
  14871. (defun org-copy-special ()
  14872. "Copy region in table or copy current subtree.
  14873. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14874. See the individual commands for more information."
  14875. (interactive)
  14876. (call-interactively
  14877. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14878. (defun org-cut-special ()
  14879. "Cut region in table or cut current subtree.
  14880. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14881. See the individual commands for more information."
  14882. (interactive)
  14883. (call-interactively
  14884. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14885. (defun org-paste-special (arg)
  14886. "Paste rectangular region into table, or past subtree relative to level.
  14887. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14888. See the individual commands for more information."
  14889. (interactive "P")
  14890. (if (org-at-table-p)
  14891. (org-table-paste-rectangle)
  14892. (org-paste-subtree arg)))
  14893. (defun org-edit-special ()
  14894. "Call a special editor for the stuff at point.
  14895. When at a table, call the formula editor with `org-table-edit-formulas'.
  14896. When at the first line of an src example, call `org-edit-src-code'.
  14897. When in an #+include line, visit the include file. Otherwise call
  14898. `ffap' to visit the file at point."
  14899. (interactive)
  14900. (cond
  14901. ((org-at-table.el-p)
  14902. (org-edit-src-code))
  14903. ((org-at-table-p)
  14904. (call-interactively 'org-table-edit-formulas))
  14905. ((save-excursion
  14906. (beginning-of-line 1)
  14907. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14908. (find-file (org-trim (match-string 1))))
  14909. ((org-edit-src-code))
  14910. ((org-edit-fixed-width-region))
  14911. (t (call-interactively 'ffap))))
  14912. (defun org-ctrl-c-ctrl-c (&optional arg)
  14913. "Set tags in headline, or update according to changed information at point.
  14914. This command does many different things, depending on context:
  14915. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14916. this is what we do.
  14917. - If the cursor is on a statistics cookie, update it.
  14918. - If the cursor is in a headline, prompt for tags and insert them
  14919. into the current line, aligned to `org-tags-column'. When called
  14920. with prefix arg, realign all tags in the current buffer.
  14921. - If the cursor is in one of the special #+KEYWORD lines, this
  14922. triggers scanning the buffer for these lines and updating the
  14923. information.
  14924. - If the cursor is inside a table, realign the table. This command
  14925. works even if the automatic table editor has been turned off.
  14926. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14927. the entire table.
  14928. - If the cursor is at a footnote reference or definition, jump to
  14929. the corresponding definition or references, respectively.
  14930. - If the cursor is a the beginning of a dynamic block, update it.
  14931. - If the current buffer is a remember buffer, close note and file
  14932. it. A prefix argument of 1 files to the default location
  14933. without further interaction. A prefix argument of 2 files to
  14934. the currently clocking task.
  14935. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14936. links in this buffer.
  14937. - If the cursor is on a numbered item in a plain list, renumber the
  14938. ordered list.
  14939. - If the cursor is on a checkbox, toggle it."
  14940. (interactive "P")
  14941. (let ((org-enable-table-editor t))
  14942. (cond
  14943. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14944. org-occur-highlights
  14945. org-latex-fragment-image-overlays)
  14946. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14947. (org-remove-occur-highlights)
  14948. (org-remove-latex-fragment-image-overlays)
  14949. (message "Temporary highlights/overlays removed from current buffer"))
  14950. ((and (local-variable-p 'org-finish-function (current-buffer))
  14951. (fboundp org-finish-function))
  14952. (funcall org-finish-function))
  14953. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14954. ((or (looking-at org-property-start-re)
  14955. (org-at-property-p))
  14956. (call-interactively 'org-property-action))
  14957. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14958. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14959. (or (org-on-heading-p) (org-at-item-p)))
  14960. (call-interactively 'org-update-statistics-cookies))
  14961. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14962. ((org-at-table.el-p)
  14963. (message "Use C-c ' to edit table.el tables"))
  14964. ((org-at-table-p)
  14965. (org-table-maybe-eval-formula)
  14966. (if arg
  14967. (call-interactively 'org-table-recalculate)
  14968. (org-table-maybe-recalculate-line))
  14969. (call-interactively 'org-table-align))
  14970. ((or (org-footnote-at-reference-p)
  14971. (org-footnote-at-definition-p))
  14972. (call-interactively 'org-footnote-action))
  14973. ((org-at-item-checkbox-p)
  14974. (call-interactively 'org-toggle-checkbox))
  14975. ((org-at-item-p)
  14976. (if arg
  14977. (call-interactively 'org-toggle-checkbox)
  14978. (call-interactively 'org-maybe-renumber-ordered-list)))
  14979. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14980. ;; Dynamic block
  14981. (beginning-of-line 1)
  14982. (save-excursion (org-update-dblock)))
  14983. ((save-excursion
  14984. (beginning-of-line 1)
  14985. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14986. (cond
  14987. ((equal (match-string 1) "TBLFM")
  14988. ;; Recalculate the table before this line
  14989. (save-excursion
  14990. (beginning-of-line 1)
  14991. (skip-chars-backward " \r\n\t")
  14992. (if (org-at-table-p)
  14993. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14994. (t
  14995. (let ((org-inhibit-startup-visibility-stuff t)
  14996. (org-startup-align-all-tables nil))
  14997. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  14998. (message "Local setup has been refreshed"))))
  14999. ((org-clock-update-time-maybe))
  15000. (t (error "C-c C-c can do nothing useful at this location")))))
  15001. (defun org-mode-restart ()
  15002. "Restart Org-mode, to scan again for special lines.
  15003. Also updates the keyword regular expressions."
  15004. (interactive)
  15005. (org-mode)
  15006. (message "Org-mode restarted"))
  15007. (defun org-kill-note-or-show-branches ()
  15008. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  15009. (interactive)
  15010. (if (not org-finish-function)
  15011. (call-interactively 'show-branches)
  15012. (let ((org-note-abort t))
  15013. (funcall org-finish-function))))
  15014. (defun org-return (&optional indent)
  15015. "Goto next table row or insert a newline.
  15016. Calls `org-table-next-row' or `newline', depending on context.
  15017. See the individual commands for more information."
  15018. (interactive)
  15019. (cond
  15020. ((bobp) (if indent (newline-and-indent) (newline)))
  15021. ((org-at-table-p)
  15022. (org-table-justify-field-maybe)
  15023. (call-interactively 'org-table-next-row))
  15024. ((and org-return-follows-link
  15025. (eq (get-text-property (point) 'face) 'org-link))
  15026. (call-interactively 'org-open-at-point))
  15027. ((and (org-at-heading-p)
  15028. (looking-at
  15029. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  15030. (org-show-entry)
  15031. (end-of-line 1)
  15032. (newline))
  15033. (t (if indent (newline-and-indent) (newline)))))
  15034. (defun org-return-indent ()
  15035. "Goto next table row or insert a newline and indent.
  15036. Calls `org-table-next-row' or `newline-and-indent', depending on
  15037. context. See the individual commands for more information."
  15038. (interactive)
  15039. (org-return t))
  15040. (defun org-ctrl-c-star ()
  15041. "Compute table, or change heading status of lines.
  15042. Calls `org-table-recalculate' or `org-toggle-heading',
  15043. depending on context."
  15044. (interactive)
  15045. (cond
  15046. ((org-at-table-p)
  15047. (call-interactively 'org-table-recalculate))
  15048. (t
  15049. ;; Convert all lines in region to list items
  15050. (call-interactively 'org-toggle-heading))))
  15051. (defun org-ctrl-c-minus ()
  15052. "Insert separator line in table or modify bullet status of line.
  15053. Also turns a plain line or a region of lines into list items.
  15054. Calls `org-table-insert-hline', `org-toggle-item', or
  15055. `org-cycle-list-bullet', depending on context."
  15056. (interactive)
  15057. (cond
  15058. ((org-at-table-p)
  15059. (call-interactively 'org-table-insert-hline))
  15060. ((org-region-active-p)
  15061. (call-interactively 'org-toggle-item))
  15062. ((org-in-item-p)
  15063. (call-interactively 'org-cycle-list-bullet))
  15064. (t
  15065. (call-interactively 'org-toggle-item))))
  15066. (defun org-toggle-item ()
  15067. "Convert headings or normal lines to items, items to normal lines.
  15068. If there is no active region, only the current line is considered.
  15069. If the first line in the region is a headline, convert all headlines to items.
  15070. If the first line in the region is an item, convert all items to normal lines.
  15071. If the first line is normal text, add an item bullet to each line."
  15072. (interactive)
  15073. (let (l2 l beg end)
  15074. (if (org-region-active-p)
  15075. (setq beg (region-beginning) end (region-end))
  15076. (setq beg (point-at-bol)
  15077. end (min (1+ (point-at-eol)) (point-max))))
  15078. (save-excursion
  15079. (goto-char end)
  15080. (setq l2 (org-current-line))
  15081. (goto-char beg)
  15082. (beginning-of-line 1)
  15083. (setq l (1- (org-current-line)))
  15084. (if (org-at-item-p)
  15085. ;; We already have items, de-itemize
  15086. (while (< (setq l (1+ l)) l2)
  15087. (when (org-at-item-p)
  15088. (goto-char (match-beginning 2))
  15089. (delete-region (match-beginning 2) (match-end 2))
  15090. (and (looking-at "[ \t]+") (replace-match "")))
  15091. (beginning-of-line 2))
  15092. (if (org-on-heading-p)
  15093. ;; Headings, convert to items
  15094. (while (< (setq l (1+ l)) l2)
  15095. (if (looking-at org-outline-regexp)
  15096. (replace-match "- " t t))
  15097. (beginning-of-line 2))
  15098. ;; normal lines, turn them into items
  15099. (while (< (setq l (1+ l)) l2)
  15100. (unless (org-at-item-p)
  15101. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15102. (replace-match "\\1- \\2")))
  15103. (beginning-of-line 2)))))))
  15104. (defun org-toggle-heading (&optional nstars)
  15105. "Convert headings to normal text, or items or text to headings.
  15106. If there is no active region, only the current line is considered.
  15107. If the first line is a heading, remove the stars from all headlines
  15108. in the region.
  15109. If the first line is a plain list item, turn all plain list items
  15110. into headings.
  15111. If the first line is a normal line, turn each and every line in the
  15112. region into a heading.
  15113. When converting a line into a heading, the number of stars is chosen
  15114. such that the lines become children of the current entry. However,
  15115. when a prefix argument is given, its value determines the number of
  15116. stars to add."
  15117. (interactive "P")
  15118. (let (l2 l itemp beg end)
  15119. (if (org-region-active-p)
  15120. (setq beg (region-beginning) end (region-end))
  15121. (setq beg (point-at-bol)
  15122. end (min (1+ (point-at-eol)) (point-max))))
  15123. (save-excursion
  15124. (goto-char end)
  15125. (setq l2 (org-current-line))
  15126. (goto-char beg)
  15127. (beginning-of-line 1)
  15128. (setq l (1- (org-current-line)))
  15129. (if (org-on-heading-p)
  15130. ;; We already have headlines, de-star them
  15131. (while (< (setq l (1+ l)) l2)
  15132. (when (org-on-heading-p t)
  15133. (and (looking-at outline-regexp) (replace-match "")))
  15134. (beginning-of-line 2))
  15135. (setq itemp (org-at-item-p))
  15136. (let* ((stars
  15137. (if nstars
  15138. (make-string (prefix-numeric-value current-prefix-arg)
  15139. ?*)
  15140. (save-excursion
  15141. (if (re-search-backward org-complex-heading-regexp nil t)
  15142. (match-string 1) ""))))
  15143. (add-stars (cond (nstars "")
  15144. ((equal stars "") "*")
  15145. (org-odd-levels-only "**")
  15146. (t "*")))
  15147. (rpl (concat stars add-stars " ")))
  15148. (while (< (setq l (1+ l)) l2)
  15149. (if itemp
  15150. (and (org-at-item-p) (replace-match rpl t t))
  15151. (unless (org-on-heading-p)
  15152. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15153. (replace-match (concat rpl (match-string 2))))))
  15154. (beginning-of-line 2)))))))
  15155. (defun org-meta-return (&optional arg)
  15156. "Insert a new heading or wrap a region in a table.
  15157. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15158. See the individual commands for more information."
  15159. (interactive "P")
  15160. (cond
  15161. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15162. ((org-at-table-p)
  15163. (call-interactively 'org-table-wrap-region))
  15164. (t (call-interactively 'org-insert-heading))))
  15165. ;;; Menu entries
  15166. ;; Define the Org-mode menus
  15167. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15168. '("Tbl"
  15169. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15170. ["Next Field" org-cycle (org-at-table-p)]
  15171. ["Previous Field" org-shifttab (org-at-table-p)]
  15172. ["Next Row" org-return (org-at-table-p)]
  15173. "--"
  15174. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15175. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15176. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15177. "--"
  15178. ("Column"
  15179. ["Move Column Left" org-metaleft (org-at-table-p)]
  15180. ["Move Column Right" org-metaright (org-at-table-p)]
  15181. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15182. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15183. ("Row"
  15184. ["Move Row Up" org-metaup (org-at-table-p)]
  15185. ["Move Row Down" org-metadown (org-at-table-p)]
  15186. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15187. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15188. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15189. "--"
  15190. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15191. ("Rectangle"
  15192. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15193. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15194. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15195. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15196. "--"
  15197. ("Calculate"
  15198. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15199. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15200. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15201. "--"
  15202. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15203. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15204. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15205. "--"
  15206. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15207. "--"
  15208. ["Sum Column/Rectangle" org-table-sum
  15209. (or (org-at-table-p) (org-region-active-p))]
  15210. ["Which Column?" org-table-current-column (org-at-table-p)])
  15211. ["Debug Formulas"
  15212. org-table-toggle-formula-debugger
  15213. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15214. ["Show Col/Row Numbers"
  15215. org-table-toggle-coordinate-overlays
  15216. :style toggle
  15217. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15218. "--"
  15219. ["Create" org-table-create (and (not (org-at-table-p))
  15220. org-enable-table-editor)]
  15221. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15222. ["Import from File" org-table-import (not (org-at-table-p))]
  15223. ["Export to File" org-table-export (org-at-table-p)]
  15224. "--"
  15225. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15226. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15227. '("Org"
  15228. ("Show/Hide"
  15229. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15230. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15231. ["Sparse Tree..." org-sparse-tree t]
  15232. ["Reveal Context" org-reveal t]
  15233. ["Show All" show-all t]
  15234. "--"
  15235. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15236. "--"
  15237. ["New Heading" org-insert-heading t]
  15238. ("Navigate Headings"
  15239. ["Up" outline-up-heading t]
  15240. ["Next" outline-next-visible-heading t]
  15241. ["Previous" outline-previous-visible-heading t]
  15242. ["Next Same Level" outline-forward-same-level t]
  15243. ["Previous Same Level" outline-backward-same-level t]
  15244. "--"
  15245. ["Jump" org-goto t])
  15246. ("Edit Structure"
  15247. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15248. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15249. "--"
  15250. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15251. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15252. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15253. "--"
  15254. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15255. "--"
  15256. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15257. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15258. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15259. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15260. "--"
  15261. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15262. "--"
  15263. ["Convert to odd levels" org-convert-to-odd-levels t]
  15264. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15265. ("Editing"
  15266. ["Emphasis..." org-emphasize t]
  15267. ["Edit Source Example" org-edit-special t]
  15268. "--"
  15269. ["Footnote new/jump" org-footnote-action t]
  15270. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15271. ("Archive"
  15272. ["Archive (default method)" org-archive-subtree-default t]
  15273. "--"
  15274. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15275. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15276. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15277. )
  15278. "--"
  15279. ("Hyperlinks"
  15280. ["Store Link (Global)" org-store-link t]
  15281. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15282. ["Insert Link" org-insert-link t]
  15283. ["Follow Link" org-open-at-point t]
  15284. "--"
  15285. ["Next link" org-next-link t]
  15286. ["Previous link" org-previous-link t]
  15287. "--"
  15288. ["Descriptive Links"
  15289. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15290. :style radio
  15291. :selected (member '(org-link) buffer-invisibility-spec)]
  15292. ["Literal Links"
  15293. (progn
  15294. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15295. :style radio
  15296. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15297. "--"
  15298. ("TODO Lists"
  15299. ["TODO/DONE/-" org-todo t]
  15300. ("Select keyword"
  15301. ["Next keyword" org-shiftright (org-on-heading-p)]
  15302. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15303. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15304. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15305. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15306. ["Show TODO Tree" org-show-todo-tree t]
  15307. ["Global TODO list" org-todo-list t]
  15308. "--"
  15309. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15310. :selected org-enforce-todo-dependencies :style toggle :active t]
  15311. "Settings for tree at point"
  15312. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15313. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15314. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15315. ["Do Children parallel" org-toggle-ordered-property :style radio
  15316. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15317. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15318. "--"
  15319. ["Set Priority" org-priority t]
  15320. ["Priority Up" org-shiftup t]
  15321. ["Priority Down" org-shiftdown t]
  15322. "--"
  15323. ["Get news from all feeds" org-feed-update-all t]
  15324. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15325. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15326. ("TAGS and Properties"
  15327. ["Set Tags" org-set-tags-command t]
  15328. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15329. "--"
  15330. ["Set property" org-set-property t]
  15331. ["Column view of properties" org-columns t]
  15332. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15333. ("Dates and Scheduling"
  15334. ["Timestamp" org-time-stamp t]
  15335. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15336. ("Change Date"
  15337. ["1 Day Later" org-shiftright t]
  15338. ["1 Day Earlier" org-shiftleft t]
  15339. ["1 ... Later" org-shiftup t]
  15340. ["1 ... Earlier" org-shiftdown t])
  15341. ["Compute Time Range" org-evaluate-time-range t]
  15342. ["Schedule Item" org-schedule t]
  15343. ["Deadline" org-deadline t]
  15344. "--"
  15345. ["Custom time format" org-toggle-time-stamp-overlays
  15346. :style radio :selected org-display-custom-times]
  15347. "--"
  15348. ["Goto Calendar" org-goto-calendar t]
  15349. ["Date from Calendar" org-date-from-calendar t]
  15350. "--"
  15351. ["Start/Restart Timer" org-timer-start t]
  15352. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15353. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15354. ["Insert Timer String" org-timer t]
  15355. ["Insert Timer Item" org-timer-item t])
  15356. ("Logging work"
  15357. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15358. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15359. ["Clock out" org-clock-out t]
  15360. ["Clock cancel" org-clock-cancel t]
  15361. "--"
  15362. ["Mark as default task" org-clock-mark-default-task t]
  15363. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15364. ["Goto running clock" org-clock-goto t]
  15365. "--"
  15366. ["Display times" org-clock-display t]
  15367. ["Create clock table" org-clock-report t]
  15368. "--"
  15369. ["Record DONE time"
  15370. (progn (setq org-log-done (not org-log-done))
  15371. (message "Switching to %s will %s record a timestamp"
  15372. (car org-done-keywords)
  15373. (if org-log-done "automatically" "not")))
  15374. :style toggle :selected org-log-done])
  15375. "--"
  15376. ["Agenda Command..." org-agenda t]
  15377. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15378. ("File List for Agenda")
  15379. ("Special views current file"
  15380. ["TODO Tree" org-show-todo-tree t]
  15381. ["Check Deadlines" org-check-deadlines t]
  15382. ["Timeline" org-timeline t]
  15383. ["Tags/Property tree" org-match-sparse-tree t])
  15384. "--"
  15385. ["Export/Publish..." org-export t]
  15386. ("LaTeX"
  15387. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15388. :selected org-cdlatex-mode]
  15389. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15390. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15391. ["Modify math symbol" org-cdlatex-math-modify
  15392. (org-inside-LaTeX-fragment-p)]
  15393. ["Insert citation" org-reftex-citation t]
  15394. "--"
  15395. ["Export LaTeX fragments as images"
  15396. (if (featurep 'org-exp)
  15397. (setq org-export-with-LaTeX-fragments
  15398. (not org-export-with-LaTeX-fragments))
  15399. (require 'org-exp))
  15400. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15401. org-export-with-LaTeX-fragments)]
  15402. "--"
  15403. ["Template for BEAMER" org-beamer-settings-template t])
  15404. "--"
  15405. ("MobileOrg"
  15406. ["Push Files and Views" org-mobile-push t]
  15407. ["Get Captured and Flagged" org-mobile-pull t]
  15408. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15409. "--"
  15410. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15411. "--"
  15412. ("Documentation"
  15413. ["Show Version" org-version t]
  15414. ["Info Documentation" org-info t])
  15415. ("Customize"
  15416. ["Browse Org Group" org-customize t]
  15417. "--"
  15418. ["Expand This Menu" org-create-customize-menu
  15419. (fboundp 'customize-menu-create)])
  15420. ["Send bug report" org-submit-bug-report t]
  15421. "--"
  15422. ("Refresh/Reload"
  15423. ["Refresh setup current buffer" org-mode-restart t]
  15424. ["Reload Org (after update)" org-reload t]
  15425. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15426. ))
  15427. (defun org-info (&optional node)
  15428. "Read documentation for Org-mode in the info system.
  15429. With optional NODE, go directly to that node."
  15430. (interactive)
  15431. (info (format "(org)%s" (or node ""))))
  15432. ;;;###autoload
  15433. (defun org-submit-bug-report ()
  15434. "Submit a bug report on Org-mode via mail.
  15435. Don't hesitate to report any problems or inaccurate documentation.
  15436. If you don't have setup sending mail from (X)Emacs, please copy the
  15437. output buffer into your mail program, as it gives us important
  15438. information about your Org-mode version and configuration."
  15439. (interactive)
  15440. (require 'reporter)
  15441. (org-load-modules-maybe)
  15442. (org-require-autoloaded-modules)
  15443. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15444. (reporter-submit-bug-report
  15445. "emacs-orgmode@gnu.org"
  15446. (org-version)
  15447. (let (list)
  15448. (save-window-excursion
  15449. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15450. (delete-other-windows)
  15451. (erase-buffer)
  15452. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15453. We would like to add your full Org-mode and Outline configuration to the
  15454. bug report. This greatly simplifies the work of the maintainer and
  15455. other experts on the mailing list.
  15456. HOWEVER, some variables you have customized may contain private
  15457. information. The names of customers, colleagues, or friends, might
  15458. appear in the form of file names, tags, todo states, or search strings.
  15459. If you answer yes to the prompt, you might want to check and remove
  15460. such private information before sending the email.")
  15461. (add-text-properties (point-min) (point-max) '(face org-warning))
  15462. (when (yes-or-no-p "Include your Org-mode configuration ")
  15463. (mapatoms
  15464. (lambda (v)
  15465. (and (boundp v)
  15466. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15467. (or (and (symbol-value v)
  15468. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15469. (and
  15470. (get v 'custom-type) (get v 'standard-value)
  15471. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15472. (push v list)))))
  15473. (kill-buffer (get-buffer "*Warn about privacy*"))
  15474. list))
  15475. nil nil
  15476. "Remember to cover the basics, that is, what you expected to happen and
  15477. what in fact did happen. You don't know how to make a good report? See
  15478. http://orgmode.org/manual/Feedback.html#Feedback
  15479. Your bug report will be posted to the Org-mode mailing list.
  15480. ------------------------------------------------------------------------")
  15481. (save-excursion
  15482. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15483. (replace-match "\\1Bug: \\3 [\\2]")))))
  15484. (defun org-install-agenda-files-menu ()
  15485. (let ((bl (buffer-list)))
  15486. (save-excursion
  15487. (while bl
  15488. (set-buffer (pop bl))
  15489. (if (org-mode-p) (setq bl nil)))
  15490. (when (org-mode-p)
  15491. (easy-menu-change
  15492. '("Org") "File List for Agenda"
  15493. (append
  15494. (list
  15495. ["Edit File List" (org-edit-agenda-file-list) t]
  15496. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15497. ["Remove Current File from List" org-remove-file t]
  15498. ["Cycle through agenda files" org-cycle-agenda-files t]
  15499. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15500. "--")
  15501. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15502. ;;;; Documentation
  15503. ;;;###autoload
  15504. (defun org-require-autoloaded-modules ()
  15505. (interactive)
  15506. (mapc 'require
  15507. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15508. org-docbook org-exp org-html org-icalendar
  15509. org-id org-latex
  15510. org-publish org-remember org-table
  15511. org-timer org-xoxo)))
  15512. ;;;###autoload
  15513. (defun org-reload (&optional uncompiled)
  15514. "Reload all org lisp files.
  15515. With prefix arg UNCOMPILED, load the uncompiled versions."
  15516. (interactive "P")
  15517. (require 'find-func)
  15518. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15519. (dir-org (file-name-directory (org-find-library-name "org")))
  15520. (dir-org-contrib (ignore-errors
  15521. (file-name-directory
  15522. (org-find-library-name "org-contribdir"))))
  15523. (files
  15524. (append (directory-files dir-org t file-re)
  15525. (and dir-org-contrib
  15526. (directory-files dir-org-contrib t file-re))))
  15527. (remove-re (concat (if (featurep 'xemacs)
  15528. "org-colview" "org-colview-xemacs")
  15529. "\\'")))
  15530. (setq files (mapcar 'file-name-sans-extension files))
  15531. (setq files (mapcar
  15532. (lambda (x) (if (string-match remove-re x) nil x))
  15533. files))
  15534. (setq files (delq nil files))
  15535. (mapc
  15536. (lambda (f)
  15537. (when (featurep (intern (file-name-nondirectory f)))
  15538. (if (and (not uncompiled)
  15539. (file-exists-p (concat f ".elc")))
  15540. (load (concat f ".elc") nil nil t)
  15541. (load (concat f ".el") nil nil t))))
  15542. files))
  15543. (org-version))
  15544. ;;;###autoload
  15545. (defun org-customize ()
  15546. "Call the customize function with org as argument."
  15547. (interactive)
  15548. (org-load-modules-maybe)
  15549. (org-require-autoloaded-modules)
  15550. (customize-browse 'org))
  15551. (defun org-create-customize-menu ()
  15552. "Create a full customization menu for Org-mode, insert it into the menu."
  15553. (interactive)
  15554. (org-load-modules-maybe)
  15555. (org-require-autoloaded-modules)
  15556. (if (fboundp 'customize-menu-create)
  15557. (progn
  15558. (easy-menu-change
  15559. '("Org") "Customize"
  15560. `(["Browse Org group" org-customize t]
  15561. "--"
  15562. ,(customize-menu-create 'org)
  15563. ["Set" Custom-set t]
  15564. ["Save" Custom-save t]
  15565. ["Reset to Current" Custom-reset-current t]
  15566. ["Reset to Saved" Custom-reset-saved t]
  15567. ["Reset to Standard Settings" Custom-reset-standard t]))
  15568. (message "\"Org\"-menu now contains full customization menu"))
  15569. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15570. ;;;; Miscellaneous stuff
  15571. ;;; Generally useful functions
  15572. (defun org-get-at-bol (property)
  15573. "Get text property PROPERTY at beginning of line."
  15574. (get-text-property (point-at-bol) property))
  15575. (defun org-find-text-property-in-string (prop s)
  15576. "Return the first non-nil value of property PROP in string S."
  15577. (or (get-text-property 0 prop s)
  15578. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15579. prop s)))
  15580. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15581. "Display the given MESSAGE as a warning."
  15582. (if (fboundp 'display-warning)
  15583. (display-warning 'org message
  15584. (if (featurep 'xemacs)
  15585. 'warning
  15586. :warning))
  15587. (let ((buf (get-buffer-create "*Org warnings*")))
  15588. (with-current-buffer buf
  15589. (goto-char (point-max))
  15590. (insert "Warning (Org): " message)
  15591. (unless (bolp)
  15592. (newline)))
  15593. (display-buffer buf)
  15594. (sit-for 0))))
  15595. (defun org-in-commented-line ()
  15596. "Is point in a line starting with `#'?"
  15597. (equal (char-after (point-at-bol)) ?#))
  15598. (defun org-in-verbatim-emphasis ()
  15599. (save-match-data
  15600. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15601. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15602. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15603. (if (and marker (marker-buffer marker)
  15604. (buffer-live-p (marker-buffer marker)))
  15605. (progn
  15606. (switch-to-buffer (marker-buffer marker))
  15607. (if (or (> marker (point-max)) (< marker (point-min)))
  15608. (widen))
  15609. (goto-char marker)
  15610. (org-show-context 'org-goto))
  15611. (if bookmark
  15612. (bookmark-jump bookmark)
  15613. (error "Cannot find location"))))
  15614. (defun org-quote-csv-field (s)
  15615. "Quote field for inclusion in CSV material."
  15616. (if (string-match "[\",]" s)
  15617. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15618. s))
  15619. (defun org-plist-delete (plist property)
  15620. "Delete PROPERTY from PLIST.
  15621. This is in contrast to merely setting it to 0."
  15622. (let (p)
  15623. (while plist
  15624. (if (not (eq property (car plist)))
  15625. (setq p (plist-put p (car plist) (nth 1 plist))))
  15626. (setq plist (cddr plist)))
  15627. p))
  15628. (defun org-force-self-insert (N)
  15629. "Needed to enforce self-insert under remapping."
  15630. (interactive "p")
  15631. (self-insert-command N))
  15632. (defun org-string-width (s)
  15633. "Compute width of string, ignoring invisible characters.
  15634. This ignores character with invisibility property `org-link', and also
  15635. characters with property `org-cwidth', because these will become invisible
  15636. upon the next fontification round."
  15637. (let (b l)
  15638. (when (or (eq t buffer-invisibility-spec)
  15639. (assq 'org-link buffer-invisibility-spec))
  15640. (while (setq b (text-property-any 0 (length s)
  15641. 'invisible 'org-link s))
  15642. (setq s (concat (substring s 0 b)
  15643. (substring s (or (next-single-property-change
  15644. b 'invisible s) (length s)))))))
  15645. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15646. (setq s (concat (substring s 0 b)
  15647. (substring s (or (next-single-property-change
  15648. b 'org-cwidth s) (length s))))))
  15649. (setq l (string-width s) b -1)
  15650. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15651. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15652. l))
  15653. (defun org-get-indentation (&optional line)
  15654. "Get the indentation of the current line, interpreting tabs.
  15655. When LINE is given, assume it represents a line and compute its indentation."
  15656. (if line
  15657. (if (string-match "^ *" (org-remove-tabs line))
  15658. (match-end 0))
  15659. (save-excursion
  15660. (beginning-of-line 1)
  15661. (skip-chars-forward " \t")
  15662. (current-column))))
  15663. (defun org-remove-tabs (s &optional width)
  15664. "Replace tabulators in S with spaces.
  15665. Assumes that s is a single line, starting in column 0."
  15666. (setq width (or width tab-width))
  15667. (while (string-match "\t" s)
  15668. (setq s (replace-match
  15669. (make-string
  15670. (- (* width (/ (+ (match-beginning 0) width) width))
  15671. (match-beginning 0)) ?\ )
  15672. t t s)))
  15673. s)
  15674. (defun org-fix-indentation (line ind)
  15675. "Fix indentation in LINE.
  15676. IND is a cons cell with target and minimum indentation.
  15677. If the current indentation in LINE is smaller than the minimum,
  15678. leave it alone. If it is larger than ind, set it to the target."
  15679. (let* ((l (org-remove-tabs line))
  15680. (i (org-get-indentation l))
  15681. (i1 (car ind)) (i2 (cdr ind)))
  15682. (if (>= i i2) (setq l (substring line i2)))
  15683. (if (> i1 0)
  15684. (concat (make-string i1 ?\ ) l)
  15685. l)))
  15686. (defun org-remove-indentation (code &optional n)
  15687. "Remove the maximum common indentation from the lines in CODE.
  15688. N may optionally be the number of spaces to remove."
  15689. (with-temp-buffer
  15690. (insert code)
  15691. (org-do-remove-indentation n)
  15692. (buffer-string)))
  15693. (defun org-do-remove-indentation (&optional n)
  15694. "Remove the maximum common indentation from the buffer."
  15695. (untabify (point-min) (point-max))
  15696. (let ((min 10000) re)
  15697. (if n
  15698. (setq min n)
  15699. (goto-char (point-min))
  15700. (while (re-search-forward "^ *[^ \n]" nil t)
  15701. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15702. (unless (or (= min 0) (= min 10000))
  15703. (setq re (format "^ \\{%d\\}" min))
  15704. (goto-char (point-min))
  15705. (while (re-search-forward re nil t)
  15706. (replace-match "")
  15707. (end-of-line 1))
  15708. min)))
  15709. (defun org-fill-template (template alist)
  15710. "Find each %key of ALIST in TEMPLATE and replace it."
  15711. (let ((case-fold-search nil)
  15712. entry key value)
  15713. (setq alist (sort (copy-sequence alist)
  15714. (lambda (a b) (< (length (car a)) (length (car b))))))
  15715. (while (setq entry (pop alist))
  15716. (setq template
  15717. (replace-regexp-in-string
  15718. (concat "%" (regexp-quote (car entry)))
  15719. (cdr entry) template t t)))
  15720. template))
  15721. (defun org-base-buffer (buffer)
  15722. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15723. (if (not buffer)
  15724. buffer
  15725. (or (buffer-base-buffer buffer)
  15726. buffer)))
  15727. (defun org-trim (s)
  15728. "Remove whitespace at beginning and end of string."
  15729. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15730. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15731. s)
  15732. (defun org-wrap (string &optional width lines)
  15733. "Wrap string to either a number of lines, or a width in characters.
  15734. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15735. that costs. If there is a word longer than WIDTH, the text is actually
  15736. wrapped to the length of that word.
  15737. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15738. many lines, whatever width that takes.
  15739. The return value is a list of lines, without newlines at the end."
  15740. (let* ((words (org-split-string string "[ \t\n]+"))
  15741. (maxword (apply 'max (mapcar 'org-string-width words)))
  15742. w ll)
  15743. (cond (width
  15744. (org-do-wrap words (max maxword width)))
  15745. (lines
  15746. (setq w maxword)
  15747. (setq ll (org-do-wrap words maxword))
  15748. (if (<= (length ll) lines)
  15749. ll
  15750. (setq ll words)
  15751. (while (> (length ll) lines)
  15752. (setq w (1+ w))
  15753. (setq ll (org-do-wrap words w)))
  15754. ll))
  15755. (t (error "Cannot wrap this")))))
  15756. (defun org-do-wrap (words width)
  15757. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15758. (let (lines line)
  15759. (while words
  15760. (setq line (pop words))
  15761. (while (and words (< (+ (length line) (length (car words))) width))
  15762. (setq line (concat line " " (pop words))))
  15763. (setq lines (push line lines)))
  15764. (nreverse lines)))
  15765. (defun org-split-string (string &optional separators)
  15766. "Splits STRING into substrings at SEPARATORS.
  15767. No empty strings are returned if there are matches at the beginning
  15768. and end of string."
  15769. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15770. (start 0)
  15771. notfirst
  15772. (list nil))
  15773. (while (and (string-match rexp string
  15774. (if (and notfirst
  15775. (= start (match-beginning 0))
  15776. (< start (length string)))
  15777. (1+ start) start))
  15778. (< (match-beginning 0) (length string)))
  15779. (setq notfirst t)
  15780. (or (eq (match-beginning 0) 0)
  15781. (and (eq (match-beginning 0) (match-end 0))
  15782. (eq (match-beginning 0) start))
  15783. (setq list
  15784. (cons (substring string start (match-beginning 0))
  15785. list)))
  15786. (setq start (match-end 0)))
  15787. (or (eq start (length string))
  15788. (setq list
  15789. (cons (substring string start)
  15790. list)))
  15791. (nreverse list)))
  15792. (defun org-quote-vert (s)
  15793. "Replace \"|\" with \"\\vert\"."
  15794. (while (string-match "|" s)
  15795. (setq s (replace-match "\\vert" t t s)))
  15796. s)
  15797. (defun org-uuidgen-p (s)
  15798. "Is S an ID created by UUIDGEN?"
  15799. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15800. (defun org-context ()
  15801. "Return a list of contexts of the current cursor position.
  15802. If several contexts apply, all are returned.
  15803. Each context entry is a list with a symbol naming the context, and
  15804. two positions indicating start and end of the context. Possible
  15805. contexts are:
  15806. :headline anywhere in a headline
  15807. :headline-stars on the leading stars in a headline
  15808. :todo-keyword on a TODO keyword (including DONE) in a headline
  15809. :tags on the TAGS in a headline
  15810. :priority on the priority cookie in a headline
  15811. :item on the first line of a plain list item
  15812. :item-bullet on the bullet/number of a plain list item
  15813. :checkbox on the checkbox in a plain list item
  15814. :table in an org-mode table
  15815. :table-special on a special filed in a table
  15816. :table-table in a table.el table
  15817. :link on a hyperlink
  15818. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15819. :target on a <<target>>
  15820. :radio-target on a <<<radio-target>>>
  15821. :latex-fragment on a LaTeX fragment
  15822. :latex-preview on a LaTeX fragment with overlayed preview image
  15823. This function expects the position to be visible because it uses font-lock
  15824. faces as a help to recognize the following contexts: :table-special, :link,
  15825. and :keyword."
  15826. (let* ((f (get-text-property (point) 'face))
  15827. (faces (if (listp f) f (list f)))
  15828. (p (point)) clist o)
  15829. ;; First the large context
  15830. (cond
  15831. ((org-on-heading-p t)
  15832. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15833. (when (progn
  15834. (beginning-of-line 1)
  15835. (looking-at org-todo-line-tags-regexp))
  15836. (push (org-point-in-group p 1 :headline-stars) clist)
  15837. (push (org-point-in-group p 2 :todo-keyword) clist)
  15838. (push (org-point-in-group p 4 :tags) clist))
  15839. (goto-char p)
  15840. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15841. (if (looking-at "\\[#[A-Z0-9]\\]")
  15842. (push (org-point-in-group p 0 :priority) clist)))
  15843. ((org-at-item-p)
  15844. (push (org-point-in-group p 2 :item-bullet) clist)
  15845. (push (list :item (point-at-bol)
  15846. (save-excursion (org-end-of-item) (point)))
  15847. clist)
  15848. (and (org-at-item-checkbox-p)
  15849. (push (org-point-in-group p 0 :checkbox) clist)))
  15850. ((org-at-table-p)
  15851. (push (list :table (org-table-begin) (org-table-end)) clist)
  15852. (if (memq 'org-formula faces)
  15853. (push (list :table-special
  15854. (previous-single-property-change p 'face)
  15855. (next-single-property-change p 'face)) clist)))
  15856. ((org-at-table-p 'any)
  15857. (push (list :table-table) clist)))
  15858. (goto-char p)
  15859. ;; Now the small context
  15860. (cond
  15861. ((org-at-timestamp-p)
  15862. (push (org-point-in-group p 0 :timestamp) clist))
  15863. ((memq 'org-link faces)
  15864. (push (list :link
  15865. (previous-single-property-change p 'face)
  15866. (next-single-property-change p 'face)) clist))
  15867. ((memq 'org-special-keyword faces)
  15868. (push (list :keyword
  15869. (previous-single-property-change p 'face)
  15870. (next-single-property-change p 'face)) clist))
  15871. ((org-on-target-p)
  15872. (push (org-point-in-group p 0 :target) clist)
  15873. (goto-char (1- (match-beginning 0)))
  15874. (if (looking-at org-radio-target-regexp)
  15875. (push (org-point-in-group p 0 :radio-target) clist))
  15876. (goto-char p))
  15877. ((setq o (car (delq nil
  15878. (mapcar
  15879. (lambda (x)
  15880. (if (memq x org-latex-fragment-image-overlays) x))
  15881. (org-overlays-at (point))))))
  15882. (push (list :latex-fragment
  15883. (overlay-start o) (overlay-end o)) clist)
  15884. (push (list :latex-preview
  15885. (overlay-start o) (overlay-end o)) clist))
  15886. ((org-inside-LaTeX-fragment-p)
  15887. ;; FIXME: positions wrong.
  15888. (push (list :latex-fragment (point) (point)) clist)))
  15889. (setq clist (nreverse (delq nil clist)))
  15890. clist))
  15891. ;; FIXME: Compare with at-regexp-p Do we need both?
  15892. (defun org-in-regexp (re &optional nlines visually)
  15893. "Check if point is inside a match of regexp.
  15894. Normally only the current line is checked, but you can include NLINES extra
  15895. lines both before and after point into the search.
  15896. If VISUALLY is set, require that the cursor is not after the match but
  15897. really on, so that the block visually is on the match."
  15898. (catch 'exit
  15899. (let ((pos (point))
  15900. (eol (point-at-eol (+ 1 (or nlines 0))))
  15901. (inc (if visually 1 0)))
  15902. (save-excursion
  15903. (beginning-of-line (- 1 (or nlines 0)))
  15904. (while (re-search-forward re eol t)
  15905. (if (and (<= (match-beginning 0) pos)
  15906. (>= (+ inc (match-end 0)) pos))
  15907. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15908. (defun org-at-regexp-p (regexp)
  15909. "Is point inside a match of REGEXP in the current line?"
  15910. (catch 'exit
  15911. (save-excursion
  15912. (let ((pos (point)) (end (point-at-eol)))
  15913. (beginning-of-line 1)
  15914. (while (re-search-forward regexp end t)
  15915. (if (and (<= (match-beginning 0) pos)
  15916. (>= (match-end 0) pos))
  15917. (throw 'exit t)))
  15918. nil))))
  15919. (defun org-in-regexps-block-p (start-re end-re)
  15920. "Returns t if the current point is between matches of START-RE and END-RE.
  15921. This will also return to if point is on one of the two matches."
  15922. (interactive)
  15923. (let ((p (point)))
  15924. (save-excursion
  15925. (and (or (org-at-regexp-p start-re)
  15926. (re-search-backward start-re nil t))
  15927. (re-search-forward end-re nil t)
  15928. (>= (point) p)))))
  15929. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15930. "Call `multi-occur' with buffers for all agenda files."
  15931. (interactive "sOrg-files matching: \np")
  15932. (let* ((files (org-agenda-files))
  15933. (tnames (mapcar 'file-truename files))
  15934. (extra org-agenda-text-search-extra-files)
  15935. f)
  15936. (when (eq (car extra) 'agenda-archives)
  15937. (setq extra (cdr extra))
  15938. (setq files (org-add-archive-files files)))
  15939. (while (setq f (pop extra))
  15940. (unless (member (file-truename f) tnames)
  15941. (add-to-list 'files f 'append)
  15942. (add-to-list 'tnames (file-truename f) 'append)))
  15943. (multi-occur
  15944. (mapcar (lambda (x)
  15945. (with-current-buffer
  15946. (or (get-file-buffer x) (find-file-noselect x))
  15947. (widen)
  15948. (current-buffer)))
  15949. files)
  15950. regexp)))
  15951. (if (boundp 'occur-mode-find-occurrence-hook)
  15952. ;; Emacs 23
  15953. (add-hook 'occur-mode-find-occurrence-hook
  15954. (lambda ()
  15955. (when (org-mode-p)
  15956. (org-reveal))))
  15957. ;; Emacs 22
  15958. (defadvice occur-mode-goto-occurrence
  15959. (after org-occur-reveal activate)
  15960. (and (org-mode-p) (org-reveal)))
  15961. (defadvice occur-mode-goto-occurrence-other-window
  15962. (after org-occur-reveal activate)
  15963. (and (org-mode-p) (org-reveal)))
  15964. (defadvice occur-mode-display-occurrence
  15965. (after org-occur-reveal activate)
  15966. (when (org-mode-p)
  15967. (let ((pos (occur-mode-find-occurrence)))
  15968. (with-current-buffer (marker-buffer pos)
  15969. (save-excursion
  15970. (goto-char pos)
  15971. (org-reveal)))))))
  15972. (defun org-occur-link-in-agenda-files ()
  15973. "Create a link and search for it in the agendas.
  15974. The link is not stored in `org-stored-links', it is just created
  15975. for the search purpose."
  15976. (interactive)
  15977. (let ((link (condition-case nil
  15978. (org-store-link nil)
  15979. (error "Unable to create a link to here"))))
  15980. (org-occur-in-agenda-files (regexp-quote link))))
  15981. (defun org-uniquify (list)
  15982. "Remove duplicate elements from LIST."
  15983. (let (res)
  15984. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15985. res))
  15986. (defun org-delete-all (elts list)
  15987. "Remove all elements in ELTS from LIST."
  15988. (while elts
  15989. (setq list (delete (pop elts) list)))
  15990. list)
  15991. (defun org-remove-if (predicate seq)
  15992. "Remove everything from SEQ that fulfills PREDICATE."
  15993. (let (res e)
  15994. (while seq
  15995. (setq e (pop seq))
  15996. (if (not (funcall predicate e)) (push e res)))
  15997. (nreverse res)))
  15998. (defun org-remove-if-not (predicate seq)
  15999. "Remove everything from SEQ that does not fulfill PREDICATE."
  16000. (let (res e)
  16001. (while seq
  16002. (setq e (pop seq))
  16003. (if (funcall predicate e) (push e res)))
  16004. (nreverse res)))
  16005. (defun org-back-over-empty-lines ()
  16006. "Move backwards over whitespace, to the beginning of the first empty line.
  16007. Returns the number of empty lines passed."
  16008. (let ((pos (point)))
  16009. (skip-chars-backward " \t\n\r")
  16010. (beginning-of-line 2)
  16011. (goto-char (min (point) pos))
  16012. (count-lines (point) pos)))
  16013. (defun org-skip-whitespace ()
  16014. (skip-chars-forward " \t\n\r"))
  16015. (defun org-point-in-group (point group &optional context)
  16016. "Check if POINT is in match-group GROUP.
  16017. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  16018. match. If the match group does ot exist or point is not inside it,
  16019. return nil."
  16020. (and (match-beginning group)
  16021. (>= point (match-beginning group))
  16022. (<= point (match-end group))
  16023. (if context
  16024. (list context (match-beginning group) (match-end group))
  16025. t)))
  16026. (defun org-switch-to-buffer-other-window (&rest args)
  16027. "Switch to buffer in a second window on the current frame.
  16028. In particular, do not allow pop-up frames."
  16029. (let (pop-up-frames special-display-buffer-names special-display-regexps
  16030. special-display-function)
  16031. (apply 'switch-to-buffer-other-window args)))
  16032. (defun org-combine-plists (&rest plists)
  16033. "Create a single property list from all plists in PLISTS.
  16034. The process starts by copying the first list, and then setting properties
  16035. from the other lists. Settings in the last list are the most significant
  16036. ones and overrule settings in the other lists."
  16037. (let ((rtn (copy-sequence (pop plists)))
  16038. p v ls)
  16039. (while plists
  16040. (setq ls (pop plists))
  16041. (while ls
  16042. (setq p (pop ls) v (pop ls))
  16043. (setq rtn (plist-put rtn p v))))
  16044. rtn))
  16045. (defun org-move-line-down (arg)
  16046. "Move the current line down. With prefix argument, move it past ARG lines."
  16047. (interactive "p")
  16048. (let ((col (current-column))
  16049. beg end pos)
  16050. (beginning-of-line 1) (setq beg (point))
  16051. (beginning-of-line 2) (setq end (point))
  16052. (beginning-of-line (+ 1 arg))
  16053. (setq pos (move-marker (make-marker) (point)))
  16054. (insert (delete-and-extract-region beg end))
  16055. (goto-char pos)
  16056. (org-move-to-column col)))
  16057. (defun org-move-line-up (arg)
  16058. "Move the current line up. With prefix argument, move it past ARG lines."
  16059. (interactive "p")
  16060. (let ((col (current-column))
  16061. beg end pos)
  16062. (beginning-of-line 1) (setq beg (point))
  16063. (beginning-of-line 2) (setq end (point))
  16064. (beginning-of-line (- arg))
  16065. (setq pos (move-marker (make-marker) (point)))
  16066. (insert (delete-and-extract-region beg end))
  16067. (goto-char pos)
  16068. (org-move-to-column col)))
  16069. (defun org-replace-escapes (string table)
  16070. "Replace %-escapes in STRING with values in TABLE.
  16071. TABLE is an association list with keys like \"%a\" and string values.
  16072. The sequences in STRING may contain normal field width and padding information,
  16073. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16074. so values can contain further %-escapes if they are define later in TABLE."
  16075. (let ((case-fold-search nil)
  16076. e re rpl)
  16077. (while (setq e (pop table))
  16078. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16079. (while (string-match re string)
  16080. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16081. (cdr e)))
  16082. (setq string (replace-match rpl t t string))))
  16083. string))
  16084. (defun org-sublist (list start end)
  16085. "Return a section of LIST, from START to END.
  16086. Counting starts at 1."
  16087. (let (rtn (c start))
  16088. (setq list (nthcdr (1- start) list))
  16089. (while (and list (<= c end))
  16090. (push (pop list) rtn)
  16091. (setq c (1+ c)))
  16092. (nreverse rtn)))
  16093. (defun org-find-base-buffer-visiting (file)
  16094. "Like `find-buffer-visiting' but always return the base buffer and
  16095. not an indirect buffer."
  16096. (let ((buf (or (get-file-buffer file)
  16097. (find-buffer-visiting file))))
  16098. (if buf
  16099. (or (buffer-base-buffer buf) buf)
  16100. nil)))
  16101. (defun org-image-file-name-regexp (&optional extensions)
  16102. "Return regexp matching the file names of images.
  16103. If EXTENSIONS is given, only match these."
  16104. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16105. (image-file-name-regexp)
  16106. (let ((image-file-name-extensions
  16107. (or extensions
  16108. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16109. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16110. (concat "\\."
  16111. (regexp-opt (nconc (mapcar 'upcase
  16112. image-file-name-extensions)
  16113. image-file-name-extensions)
  16114. t)
  16115. "\\'"))))
  16116. (defun org-file-image-p (file &optional extensions)
  16117. "Return non-nil if FILE is an image."
  16118. (save-match-data
  16119. (string-match (org-image-file-name-regexp extensions) file)))
  16120. (defun org-get-cursor-date ()
  16121. "Return the date at cursor in as a time.
  16122. This works in the calendar and in the agenda, anywhere else it just
  16123. returns the current time."
  16124. (let (date day defd)
  16125. (cond
  16126. ((eq major-mode 'calendar-mode)
  16127. (setq date (calendar-cursor-to-date)
  16128. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16129. ((eq major-mode 'org-agenda-mode)
  16130. (setq day (get-text-property (point) 'day))
  16131. (if day
  16132. (setq date (calendar-gregorian-from-absolute day)
  16133. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16134. (nth 2 date))))))
  16135. (or defd (current-time))))
  16136. (defvar org-agenda-action-marker (make-marker)
  16137. "Marker pointing to the entry for the next agenda action.")
  16138. (defun org-mark-entry-for-agenda-action ()
  16139. "Mark the current entry as target of an agenda action.
  16140. Agenda actions are actions executed from the agenda with the key `k',
  16141. which make use of the date at the cursor."
  16142. (interactive)
  16143. (move-marker org-agenda-action-marker
  16144. (save-excursion (org-back-to-heading t) (point))
  16145. (current-buffer))
  16146. (message
  16147. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16148. ;;; Paragraph filling stuff.
  16149. ;; We want this to be just right, so use the full arsenal.
  16150. (defun org-indent-line-function ()
  16151. "Indent line like previous, but further if previous was headline or item."
  16152. (interactive)
  16153. (let* ((pos (point))
  16154. (itemp (org-at-item-p))
  16155. (case-fold-search t)
  16156. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16157. column bpos bcol tpos tcol bullet btype bullet-type)
  16158. ;; Find the previous relevant line
  16159. (beginning-of-line 1)
  16160. (cond
  16161. ((looking-at "#") (setq column 0))
  16162. ((looking-at "\\*+ ") (setq column 0))
  16163. ((and (looking-at "[ \t]*:END:")
  16164. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16165. (save-excursion
  16166. (goto-char (1- (match-beginning 1)))
  16167. (setq column (current-column))))
  16168. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16169. (save-excursion
  16170. (re-search-backward
  16171. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16172. (setq column (org-get-indentation (match-string 0))))
  16173. (t
  16174. (beginning-of-line 0)
  16175. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16176. (not (looking-at "[ \t]*:END:"))
  16177. (not (looking-at org-drawer-regexp)))
  16178. (beginning-of-line 0))
  16179. (cond
  16180. ((looking-at "\\*+[ \t]+")
  16181. (if (not org-adapt-indentation)
  16182. (setq column 0)
  16183. (goto-char (match-end 0))
  16184. (setq column (current-column))))
  16185. ((looking-at org-drawer-regexp)
  16186. (goto-char (1- (match-beginning 1)))
  16187. (setq column (current-column)))
  16188. ((looking-at "\\([ \t]*\\):END:")
  16189. (goto-char (match-end 1))
  16190. (setq column (current-column)))
  16191. ((org-in-item-p)
  16192. (org-beginning-of-item)
  16193. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16194. (setq bpos (match-beginning 1) tpos (match-end 0)
  16195. bcol (progn (goto-char bpos) (current-column))
  16196. tcol (progn (goto-char tpos) (current-column))
  16197. bullet (match-string 1)
  16198. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16199. (if (> tcol (+ bcol org-description-max-indent))
  16200. (setq tcol (+ bcol 5)))
  16201. (if (not itemp)
  16202. (setq column tcol)
  16203. (goto-char pos)
  16204. (beginning-of-line 1)
  16205. (if (looking-at "\\S-")
  16206. (progn
  16207. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16208. (setq bullet (match-string 1)
  16209. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16210. (setq column (if (equal btype bullet-type) bcol tcol)))
  16211. (setq column (org-get-indentation)))))
  16212. (t (setq column (org-get-indentation))))))
  16213. (goto-char pos)
  16214. (if (<= (current-column) (current-indentation))
  16215. (org-indent-line-to column)
  16216. (save-excursion (org-indent-line-to column)))
  16217. (setq column (current-column))
  16218. (beginning-of-line 1)
  16219. (if (looking-at
  16220. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16221. (replace-match (concat (match-string 1)
  16222. (format org-property-format
  16223. (match-string 2) (match-string 3)))
  16224. t t))
  16225. (org-move-to-column column)))
  16226. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16227. "Variable to store copy of `adaptive-fill-regexp'.
  16228. Since `adaptive-fill-regexp' is set to never match, we need to
  16229. store a backup of its value before entering `org-mode' so that
  16230. the functionality can be provided as a fall-back.")
  16231. (defun org-set-autofill-regexps ()
  16232. (interactive)
  16233. ;; In the paragraph separator we include headlines, because filling
  16234. ;; text in a line directly attached to a headline would otherwise
  16235. ;; fill the headline as well.
  16236. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16237. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16238. ;; The paragraph starter includes hand-formatted lists.
  16239. (org-set-local
  16240. 'paragraph-start
  16241. (concat
  16242. "\f" "\\|"
  16243. "[ ]*$" "\\|"
  16244. "\\*+ " "\\|"
  16245. "[ \t]*#" "\\|"
  16246. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16247. "[ \t]*[:|]" "\\|"
  16248. "\\$\\$" "\\|"
  16249. "\\\\\\(begin\\|end\\|[][]\\)"))
  16250. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16251. ;; But only if the user has not turned off tables or fixed-width regions
  16252. (org-set-local
  16253. 'auto-fill-inhibit-regexp
  16254. (concat "\\*+ \\|#\\+"
  16255. "\\|[ \t]*" org-keyword-time-regexp
  16256. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16257. (concat
  16258. "\\|[ \t]*["
  16259. (if org-enable-table-editor "|" "")
  16260. (if org-enable-fixed-width-editor ":" "")
  16261. "]"))))
  16262. ;; We use our own fill-paragraph function, to make sure that tables
  16263. ;; and fixed-width regions are not wrapped. That function will pass
  16264. ;; through to `fill-paragraph' when appropriate.
  16265. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16266. ;; Adaptive filling: To get full control, first make sure that
  16267. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16268. (unless (local-variable-p 'adaptive-fill-regexp)
  16269. (org-set-local 'org-adaptive-fill-regexp-backup
  16270. adaptive-fill-regexp))
  16271. (org-set-local 'adaptive-fill-regexp "\000")
  16272. (org-set-local 'adaptive-fill-function
  16273. 'org-adaptive-fill-function)
  16274. (org-set-local
  16275. 'align-mode-rules-list
  16276. '((org-in-buffer-settings
  16277. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16278. (modes . '(org-mode))))))
  16279. (defun org-fill-paragraph (&optional justify)
  16280. "Re-align a table, pass through to fill-paragraph if no table."
  16281. (let ((table-p (org-at-table-p))
  16282. (table.el-p (org-at-table.el-p)))
  16283. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16284. (save-excursion (goto-char (point-at-bol))
  16285. (looking-at outline-regexp)))
  16286. t) ; skip headlines
  16287. (table.el-p t) ; skip table.el tables
  16288. (table-p (org-table-align) t) ; align org-mode tables
  16289. (t nil)))) ; call paragraph-fill
  16290. ;; For reference, this is the default value of adaptive-fill-regexp
  16291. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16292. (defun org-adaptive-fill-function ()
  16293. "Return a fill prefix for org-mode files.
  16294. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16295. work correctly."
  16296. (cond
  16297. ;; Comment line
  16298. ((looking-at "#[ \t]+")
  16299. (match-string-no-properties 0))
  16300. ;; Description list
  16301. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16302. (save-excursion
  16303. (if (> (match-end 1) (+ (match-beginning 1)
  16304. org-description-max-indent))
  16305. (goto-char (+ (match-beginning 1) 5))
  16306. (goto-char (match-end 0)))
  16307. (make-string (current-column) ?\ )))
  16308. ;; Ordered or unordered list
  16309. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16310. (save-excursion
  16311. (goto-char (match-end 0))
  16312. (make-string (current-column) ?\ )))
  16313. ;; Other text
  16314. ((looking-at org-adaptive-fill-regexp-backup)
  16315. (match-string-no-properties 0))))
  16316. ;;; Other stuff.
  16317. (defun org-toggle-fixed-width-section (arg)
  16318. "Toggle the fixed-width export.
  16319. If there is no active region, the QUOTE keyword at the current headline is
  16320. inserted or removed. When present, it causes the text between this headline
  16321. and the next to be exported as fixed-width text, and unmodified.
  16322. If there is an active region, this command adds or removes a colon as the
  16323. first character of this line. If the first character of a line is a colon,
  16324. this line is also exported in fixed-width font."
  16325. (interactive "P")
  16326. (let* ((cc 0)
  16327. (regionp (org-region-active-p))
  16328. (beg (if regionp (region-beginning) (point)))
  16329. (end (if regionp (region-end)))
  16330. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16331. (case-fold-search nil)
  16332. (re "[ \t]*\\(: \\)")
  16333. off)
  16334. (if regionp
  16335. (save-excursion
  16336. (goto-char beg)
  16337. (setq cc (current-column))
  16338. (beginning-of-line 1)
  16339. (setq off (looking-at re))
  16340. (while (> nlines 0)
  16341. (setq nlines (1- nlines))
  16342. (beginning-of-line 1)
  16343. (cond
  16344. (arg
  16345. (org-move-to-column cc t)
  16346. (insert ": \n")
  16347. (forward-line -1))
  16348. ((and off (looking-at re))
  16349. (replace-match "" t t nil 1))
  16350. ((not off) (org-move-to-column cc t) (insert ": ")))
  16351. (forward-line 1)))
  16352. (save-excursion
  16353. (org-back-to-heading)
  16354. (if (looking-at (concat outline-regexp
  16355. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16356. (replace-match "" t t nil 1)
  16357. (if (looking-at outline-regexp)
  16358. (progn
  16359. (goto-char (match-end 0))
  16360. (insert org-quote-string " "))))))))
  16361. (defun org-reftex-citation ()
  16362. "Use reftex-citation to insert a citation into the buffer.
  16363. This looks for a line like
  16364. #+BIBLIOGRAPHY: foo plain option:-d
  16365. and derives from it that foo.bib is the bibliography file relevant
  16366. for this document. It then installs the necessary environment for RefTeX
  16367. to work in this buffer and calls `reftex-citation' to insert a citation
  16368. into the buffer.
  16369. Export of such citations to both LaTeX and HTML is handled by the contributed
  16370. package org-exp-bibtex by Taru Karttunen."
  16371. (interactive)
  16372. (let ((reftex-docstruct-symbol 'rds)
  16373. (reftex-cite-format "\\cite{%l}")
  16374. rds bib)
  16375. (save-excursion
  16376. (save-restriction
  16377. (widen)
  16378. (let ((case-fold-search t)
  16379. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16380. (if (not (save-excursion
  16381. (or (re-search-forward re nil t)
  16382. (re-search-backward re nil t))))
  16383. (error "No bibliography defined in file")
  16384. (setq bib (concat (match-string 1) ".bib")
  16385. rds (list (list 'bib bib)))))))
  16386. (call-interactively 'reftex-citation)))
  16387. ;;;; Functions extending outline functionality
  16388. (defun org-beginning-of-line (&optional arg)
  16389. "Go to the beginning of the current line. If that is invisible, continue
  16390. to a visible line beginning. This makes the function of C-a more intuitive.
  16391. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16392. first attempt, and only move to after the tags when the cursor is already
  16393. beyond the end of the headline."
  16394. (interactive "P")
  16395. (let ((pos (point))
  16396. (special (if (consp org-special-ctrl-a/e)
  16397. (car org-special-ctrl-a/e)
  16398. org-special-ctrl-a/e))
  16399. refpos)
  16400. (if (org-bound-and-true-p line-move-visual)
  16401. (beginning-of-visual-line 1)
  16402. (beginning-of-line 1))
  16403. (if (and arg (fboundp 'move-beginning-of-line))
  16404. (call-interactively 'move-beginning-of-line)
  16405. (if (bobp)
  16406. nil
  16407. (backward-char 1)
  16408. (if (org-invisible-p)
  16409. (while (and (not (bobp)) (org-invisible-p))
  16410. (backward-char 1)
  16411. (beginning-of-line 1))
  16412. (forward-char 1))))
  16413. (when special
  16414. (cond
  16415. ((and (looking-at org-complex-heading-regexp)
  16416. (= (char-after (match-end 1)) ?\ ))
  16417. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16418. (point-at-eol)))
  16419. (goto-char
  16420. (if (eq special t)
  16421. (cond ((> pos refpos) refpos)
  16422. ((= pos (point)) refpos)
  16423. (t (point)))
  16424. (cond ((> pos (point)) (point))
  16425. ((not (eq last-command this-command)) (point))
  16426. (t refpos)))))
  16427. ((org-at-item-p)
  16428. (goto-char
  16429. (if (eq special t)
  16430. (cond ((> pos (match-end 4)) (match-end 4))
  16431. ((= pos (point)) (match-end 4))
  16432. (t (point)))
  16433. (cond ((> pos (point)) (point))
  16434. ((not (eq last-command this-command)) (point))
  16435. (t (match-end 4))))))))
  16436. (org-no-warnings
  16437. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16438. (defun org-end-of-line (&optional arg)
  16439. "Go to the end of the line.
  16440. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16441. first attempt, and only move to after the tags when the cursor is already
  16442. beyond the end of the headline."
  16443. (interactive "P")
  16444. (let ((special (if (consp org-special-ctrl-a/e)
  16445. (cdr org-special-ctrl-a/e)
  16446. org-special-ctrl-a/e)))
  16447. (if (or (not special)
  16448. (not (org-on-heading-p))
  16449. arg)
  16450. (call-interactively
  16451. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16452. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16453. (t 'end-of-line)))
  16454. (let ((pos (point)))
  16455. (beginning-of-line 1)
  16456. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16457. (if (eq special t)
  16458. (if (or (< pos (match-beginning 1))
  16459. (= pos (match-end 0)))
  16460. (goto-char (match-beginning 1))
  16461. (goto-char (match-end 0)))
  16462. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16463. (goto-char (match-end 0))
  16464. (goto-char (match-beginning 1))))
  16465. (call-interactively (if (fboundp 'move-end-of-line)
  16466. 'move-end-of-line
  16467. 'end-of-line)))))
  16468. (org-no-warnings
  16469. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16470. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16471. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16472. (define-key org-mode-map [home] 'org-beginning-of-line)
  16473. (define-key org-mode-map [end] 'org-end-of-line)
  16474. (defun org-backward-sentence (&optional arg)
  16475. "Go to beginning of sentence, or beginning of table field.
  16476. This will call `backward-sentence' or `org-table-beginning-of-field',
  16477. depending on context."
  16478. (interactive "P")
  16479. (cond
  16480. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16481. (t (call-interactively 'backward-sentence))))
  16482. (defun org-forward-sentence (&optional arg)
  16483. "Go to end of sentence, or end of table field.
  16484. This will call `forward-sentence' or `org-table-end-of-field',
  16485. depending on context."
  16486. (interactive "P")
  16487. (cond
  16488. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16489. (t (call-interactively 'forward-sentence))))
  16490. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16491. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16492. (defun org-kill-line (&optional arg)
  16493. "Kill line, to tags or end of line."
  16494. (interactive "P")
  16495. (cond
  16496. ((or (not org-special-ctrl-k)
  16497. (bolp)
  16498. (not (org-on-heading-p)))
  16499. (call-interactively 'kill-line))
  16500. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16501. (kill-region (point) (match-beginning 1))
  16502. (org-set-tags nil t))
  16503. (t (kill-region (point) (point-at-eol)))))
  16504. (define-key org-mode-map "\C-k" 'org-kill-line)
  16505. (defun org-yank (&optional arg)
  16506. "Yank. If the kill is a subtree, treat it specially.
  16507. This command will look at the current kill and check if is a single
  16508. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16509. cursor is at the beginning of a line or after the stars of a currently
  16510. empty headline, then the yank is handled specially. How exactly depends
  16511. on the value of the following variables, both set by default.
  16512. org-yank-folded-subtrees
  16513. When set, the subtree(s) will be folded after insertion, but only
  16514. if doing so would now swallow text after the yanked text.
  16515. org-yank-adjusted-subtrees
  16516. When set, the subtree will be promoted or demoted in order to
  16517. fit into the local outline tree structure, which means that the level
  16518. will be adjusted so that it becomes the smaller one of the two
  16519. *visible* surrounding headings.
  16520. Any prefix to this command will cause `yank' to be called directly with
  16521. no special treatment. In particular, a simple `C-u' prefix will just
  16522. plainly yank the text as it is.
  16523. \[1] The test checks if the first non-white line is a heading
  16524. and if there are no other headings with fewer stars."
  16525. (interactive "P")
  16526. (org-yank-generic 'yank arg))
  16527. (defun org-yank-generic (command arg)
  16528. "Perform some yank-like command.
  16529. This function implements the behavior described in the `org-yank'
  16530. documentation. However, it has been generalized to work for any
  16531. interactive command with similar behavior."
  16532. ;; pretend to be command COMMAND
  16533. (setq this-command command)
  16534. (if arg
  16535. (call-interactively command)
  16536. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16537. (and (org-kill-is-subtree-p)
  16538. (or (bolp)
  16539. (and (looking-at "[ \t]*$")
  16540. (string-match
  16541. "\\`\\*+\\'"
  16542. (buffer-substring (point-at-bol) (point)))))))
  16543. swallowp)
  16544. (cond
  16545. ((and subtreep org-yank-folded-subtrees)
  16546. (let ((beg (point))
  16547. end)
  16548. (if (and subtreep org-yank-adjusted-subtrees)
  16549. (org-paste-subtree nil nil 'for-yank)
  16550. (call-interactively command))
  16551. (setq end (point))
  16552. (goto-char beg)
  16553. (when (and (bolp) subtreep
  16554. (not (setq swallowp
  16555. (org-yank-folding-would-swallow-text beg end))))
  16556. (or (looking-at outline-regexp)
  16557. (re-search-forward (concat "^" outline-regexp) end t))
  16558. (while (and (< (point) end) (looking-at outline-regexp))
  16559. (hide-subtree)
  16560. (org-cycle-show-empty-lines 'folded)
  16561. (condition-case nil
  16562. (outline-forward-same-level 1)
  16563. (error (goto-char end)))))
  16564. (when swallowp
  16565. (message
  16566. "Inserted text not folded because that would swallow text"))
  16567. (goto-char end)
  16568. (skip-chars-forward " \t\n\r")
  16569. (beginning-of-line 1)
  16570. (push-mark beg 'nomsg)))
  16571. ((and subtreep org-yank-adjusted-subtrees)
  16572. (let ((beg (point-at-bol)))
  16573. (org-paste-subtree nil nil 'for-yank)
  16574. (push-mark beg 'nomsg)))
  16575. (t
  16576. (call-interactively command))))))
  16577. (defun org-yank-folding-would-swallow-text (beg end)
  16578. "Would hide-subtree at BEG swallow any text after END?"
  16579. (let (level)
  16580. (save-excursion
  16581. (goto-char beg)
  16582. (when (or (looking-at outline-regexp)
  16583. (re-search-forward (concat "^" outline-regexp) end t))
  16584. (setq level (org-outline-level)))
  16585. (goto-char end)
  16586. (skip-chars-forward " \t\r\n\v\f")
  16587. (if (or (eobp)
  16588. (and (bolp) (looking-at org-outline-regexp)
  16589. (<= (org-outline-level) level)))
  16590. nil ; Nothing would be swallowed
  16591. t)))) ; something would swallow
  16592. (define-key org-mode-map "\C-y" 'org-yank)
  16593. (defun org-invisible-p ()
  16594. "Check if point is at a character currently not visible."
  16595. ;; Early versions of noutline don't have `outline-invisible-p'.
  16596. (if (fboundp 'outline-invisible-p)
  16597. (outline-invisible-p)
  16598. (get-char-property (point) 'invisible)))
  16599. (defun org-invisible-p2 ()
  16600. "Check if point is at a character currently not visible."
  16601. (save-excursion
  16602. (if (and (eolp) (not (bobp))) (backward-char 1))
  16603. ;; Early versions of noutline don't have `outline-invisible-p'.
  16604. (if (fboundp 'outline-invisible-p)
  16605. (outline-invisible-p)
  16606. (get-char-property (point) 'invisible))))
  16607. (defun org-back-to-heading (&optional invisible-ok)
  16608. "Call `outline-back-to-heading', but provide a better error message."
  16609. (condition-case nil
  16610. (outline-back-to-heading invisible-ok)
  16611. (error (error "Before first headline at position %d in buffer %s"
  16612. (point) (current-buffer)))))
  16613. (defun org-before-first-heading-p ()
  16614. "Before first heading?"
  16615. (save-excursion
  16616. (null (re-search-backward "^\\*+ " nil t))))
  16617. (defun org-on-heading-p (&optional ignored)
  16618. (outline-on-heading-p t))
  16619. (defun org-at-heading-p (&optional ignored)
  16620. (outline-on-heading-p t))
  16621. (defun org-point-at-end-of-empty-headline ()
  16622. "If point is at the end of an empty headline, return t, else nil.
  16623. If the heading only contains a TODO keyword, it is still still considered
  16624. empty."
  16625. (and (looking-at "[ \t]*$")
  16626. (save-excursion
  16627. (beginning-of-line 1)
  16628. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16629. "\\)?[ \t]*$")))))
  16630. (defun org-at-heading-or-item-p ()
  16631. (or (org-on-heading-p) (org-at-item-p)))
  16632. (defun org-on-target-p ()
  16633. (or (org-in-regexp org-radio-target-regexp)
  16634. (org-in-regexp org-target-regexp)))
  16635. (defun org-up-heading-all (arg)
  16636. "Move to the heading line of which the present line is a subheading.
  16637. This function considers both visible and invisible heading lines.
  16638. With argument, move up ARG levels."
  16639. (if (fboundp 'outline-up-heading-all)
  16640. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16641. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16642. (defun org-up-heading-safe ()
  16643. "Move to the heading line of which the present line is a subheading.
  16644. This version will not throw an error. It will return the level of the
  16645. headline found, or nil if no higher level is found.
  16646. Also, this function will be a lot faster than `outline-up-heading',
  16647. because it relies on stars being the outline starters. This can really
  16648. make a significant difference in outlines with very many siblings."
  16649. (let (start-level re)
  16650. (org-back-to-heading t)
  16651. (setq start-level (funcall outline-level))
  16652. (if (equal start-level 1)
  16653. nil
  16654. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16655. (if (re-search-backward re nil t)
  16656. (funcall outline-level)))))
  16657. (defun org-first-sibling-p ()
  16658. "Is this heading the first child of its parents?"
  16659. (interactive)
  16660. (let ((re (concat "^" outline-regexp))
  16661. level l)
  16662. (unless (org-at-heading-p t)
  16663. (error "Not at a heading"))
  16664. (setq level (funcall outline-level))
  16665. (save-excursion
  16666. (if (not (re-search-backward re nil t))
  16667. t
  16668. (setq l (funcall outline-level))
  16669. (< l level)))))
  16670. (defun org-goto-sibling (&optional previous)
  16671. "Goto the next sibling, even if it is invisible.
  16672. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16673. when a sibling was found. When none is found, return nil and don't
  16674. move point."
  16675. (let ((fun (if previous 're-search-backward 're-search-forward))
  16676. (pos (point))
  16677. (re (concat "^" outline-regexp))
  16678. level l)
  16679. (when (condition-case nil (org-back-to-heading t) (error nil))
  16680. (setq level (funcall outline-level))
  16681. (catch 'exit
  16682. (or previous (forward-char 1))
  16683. (while (funcall fun re nil t)
  16684. (setq l (funcall outline-level))
  16685. (when (< l level) (goto-char pos) (throw 'exit nil))
  16686. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16687. (goto-char pos)
  16688. nil))))
  16689. (defun org-show-siblings ()
  16690. "Show all siblings of the current headline."
  16691. (save-excursion
  16692. (while (org-goto-sibling) (org-flag-heading nil)))
  16693. (save-excursion
  16694. (while (org-goto-sibling 'previous)
  16695. (org-flag-heading nil))))
  16696. (defun org-show-hidden-entry ()
  16697. "Show an entry where even the heading is hidden."
  16698. (save-excursion
  16699. (org-show-entry)))
  16700. (defun org-flag-heading (flag &optional entry)
  16701. "Flag the current heading. FLAG non-nil means make invisible.
  16702. When ENTRY is non-nil, show the entire entry."
  16703. (save-excursion
  16704. (org-back-to-heading t)
  16705. ;; Check if we should show the entire entry
  16706. (if entry
  16707. (progn
  16708. (org-show-entry)
  16709. (save-excursion
  16710. (and (outline-next-heading)
  16711. (org-flag-heading nil))))
  16712. (outline-flag-region (max (point-min) (1- (point)))
  16713. (save-excursion (outline-end-of-heading) (point))
  16714. flag))))
  16715. (defun org-get-next-sibling ()
  16716. "Move to next heading of the same level, and return point.
  16717. If there is no such heading, return nil.
  16718. This is like outline-next-sibling, but invisible headings are ok."
  16719. (let ((level (funcall outline-level)))
  16720. (outline-next-heading)
  16721. (while (and (not (eobp)) (> (funcall outline-level) level))
  16722. (outline-next-heading))
  16723. (if (or (eobp) (< (funcall outline-level) level))
  16724. nil
  16725. (point))))
  16726. (defun org-get-last-sibling ()
  16727. "Move to previous heading of the same level, and return point.
  16728. If there is no such heading, return nil."
  16729. (let ((opoint (point))
  16730. (level (funcall outline-level)))
  16731. (outline-previous-heading)
  16732. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16733. (while (and (> (funcall outline-level) level)
  16734. (not (bobp)))
  16735. (outline-previous-heading))
  16736. (if (< (funcall outline-level) level)
  16737. nil
  16738. (point)))))
  16739. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16740. ;; This contains an exact copy of the original function, but it uses
  16741. ;; `org-back-to-heading', to make it work also in invisible
  16742. ;; trees. And is uses an invisible-OK argument.
  16743. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16744. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16745. ;; with many children and grandchildren etc, this can be much faster
  16746. ;; than the outline version.
  16747. (org-back-to-heading invisible-OK)
  16748. (let ((first t)
  16749. (level (funcall outline-level)))
  16750. (if (and (org-mode-p) (< level 1000))
  16751. ;; A true heading (not a plain list item), in Org-mode
  16752. ;; This means we can easily find the end by looking
  16753. ;; only for the right number of stars. Using a regexp to do
  16754. ;; this is so much faster than using a Lisp loop.
  16755. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16756. (forward-char 1)
  16757. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16758. ;; something else, do it the slow way
  16759. (while (and (not (eobp))
  16760. (or first (> (funcall outline-level) level)))
  16761. (setq first nil)
  16762. (outline-next-heading)))
  16763. (unless to-heading
  16764. (if (memq (preceding-char) '(?\n ?\^M))
  16765. (progn
  16766. ;; Go to end of line before heading
  16767. (forward-char -1)
  16768. (if (memq (preceding-char) '(?\n ?\^M))
  16769. ;; leave blank line before heading
  16770. (forward-char -1))))))
  16771. (point))
  16772. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16773. "Use Org version in org-mode, for dramatic speed-up."
  16774. (if (eq major-mode 'org-mode)
  16775. (progn
  16776. (org-end-of-subtree nil t)
  16777. (unless (eobp) (backward-char 1)))
  16778. ad-do-it))
  16779. (defun org-forward-same-level (arg &optional invisible-ok)
  16780. "Move forward to the arg'th subheading at same level as this one.
  16781. Stop at the first and last subheadings of a superior heading."
  16782. (interactive "p")
  16783. (org-back-to-heading invisible-ok)
  16784. (org-on-heading-p)
  16785. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16786. (re (format "^\\*\\{1,%d\\} " level))
  16787. l)
  16788. (forward-char 1)
  16789. (while (> arg 0)
  16790. (while (and (re-search-forward re nil 'move)
  16791. (setq l (- (match-end 0) (match-beginning 0) 1))
  16792. (= l level)
  16793. (not invisible-ok)
  16794. (progn (backward-char 1) (org-invisible-p)))
  16795. (if (< l level) (setq arg 1)))
  16796. (setq arg (1- arg)))
  16797. (beginning-of-line 1)))
  16798. (defun org-backward-same-level (arg &optional invisible-ok)
  16799. "Move backward to the arg'th subheading at same level as this one.
  16800. Stop at the first and last subheadings of a superior heading."
  16801. (interactive "p")
  16802. (org-back-to-heading)
  16803. (org-on-heading-p)
  16804. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16805. (re (format "^\\*\\{1,%d\\} " level))
  16806. l)
  16807. (while (> arg 0)
  16808. (while (and (re-search-backward re nil 'move)
  16809. (setq l (- (match-end 0) (match-beginning 0) 1))
  16810. (= l level)
  16811. (not invisible-ok)
  16812. (org-invisible-p))
  16813. (if (< l level) (setq arg 1)))
  16814. (setq arg (1- arg)))))
  16815. (defun org-show-subtree ()
  16816. "Show everything after this heading at deeper levels."
  16817. (outline-flag-region
  16818. (point)
  16819. (save-excursion
  16820. (org-end-of-subtree t t))
  16821. nil))
  16822. (defun org-show-entry ()
  16823. "Show the body directly following this heading.
  16824. Show the heading too, if it is currently invisible."
  16825. (interactive)
  16826. (save-excursion
  16827. (condition-case nil
  16828. (progn
  16829. (org-back-to-heading t)
  16830. (outline-flag-region
  16831. (max (point-min) (1- (point)))
  16832. (save-excursion
  16833. (if (re-search-forward
  16834. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16835. (match-beginning 1)
  16836. (point-max)))
  16837. nil)
  16838. (org-cycle-hide-drawers 'children))
  16839. (error nil))))
  16840. (defun org-make-options-regexp (kwds &optional extra)
  16841. "Make a regular expression for keyword lines."
  16842. (concat
  16843. "^"
  16844. "#?[ \t]*\\+\\("
  16845. (mapconcat 'regexp-quote kwds "\\|")
  16846. (if extra (concat "\\|" extra))
  16847. "\\):[ \t]*"
  16848. "\\(.*\\)"))
  16849. ;; Make isearch reveal the necessary context
  16850. (defun org-isearch-end ()
  16851. "Reveal context after isearch exits."
  16852. (when isearch-success ; only if search was successful
  16853. (if (featurep 'xemacs)
  16854. ;; Under XEmacs, the hook is run in the correct place,
  16855. ;; we directly show the context.
  16856. (org-show-context 'isearch)
  16857. ;; In Emacs the hook runs *before* restoring the overlays.
  16858. ;; So we have to use a one-time post-command-hook to do this.
  16859. ;; (Emacs 22 has a special variable, see function `org-mode')
  16860. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16861. isearch-mode-end-hook-quit)
  16862. ;; Only when the isearch was not quitted.
  16863. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16864. 'append 'local)))))
  16865. (defun org-isearch-post-command ()
  16866. "Remove self from hook, and show context."
  16867. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16868. (org-show-context 'isearch))
  16869. ;;;; Integration with and fixes for other packages
  16870. ;;; Imenu support
  16871. (defvar org-imenu-markers nil
  16872. "All markers currently used by Imenu.")
  16873. (make-variable-buffer-local 'org-imenu-markers)
  16874. (defun org-imenu-new-marker (&optional pos)
  16875. "Return a new marker for use by Imenu, and remember the marker."
  16876. (let ((m (make-marker)))
  16877. (move-marker m (or pos (point)))
  16878. (push m org-imenu-markers)
  16879. m))
  16880. (defun org-imenu-get-tree ()
  16881. "Produce the index for Imenu."
  16882. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16883. (setq org-imenu-markers nil)
  16884. (let* ((n org-imenu-depth)
  16885. (re (concat "^" outline-regexp))
  16886. (subs (make-vector (1+ n) nil))
  16887. (last-level 0)
  16888. m level head)
  16889. (save-excursion
  16890. (save-restriction
  16891. (widen)
  16892. (goto-char (point-max))
  16893. (while (re-search-backward re nil t)
  16894. (setq level (org-reduced-level (funcall outline-level)))
  16895. (when (<= level n)
  16896. (looking-at org-complex-heading-regexp)
  16897. (setq head (org-link-display-format
  16898. (org-match-string-no-properties 4))
  16899. m (org-imenu-new-marker))
  16900. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16901. (if (>= level last-level)
  16902. (push (cons head m) (aref subs level))
  16903. (push (cons head (aref subs (1+ level))) (aref subs level))
  16904. (loop for i from (1+ level) to n do (aset subs i nil)))
  16905. (setq last-level level)))))
  16906. (aref subs 1)))
  16907. (eval-after-load "imenu"
  16908. '(progn
  16909. (add-hook 'imenu-after-jump-hook
  16910. (lambda ()
  16911. (if (eq major-mode 'org-mode)
  16912. (org-show-context 'org-goto))))))
  16913. (defun org-link-display-format (link)
  16914. "Replace a link with either the description, or the link target
  16915. if no description is present"
  16916. (save-match-data
  16917. (if (string-match org-bracket-link-analytic-regexp link)
  16918. (replace-match (if (match-end 5)
  16919. (match-string 5 link)
  16920. (concat (match-string 1 link)
  16921. (match-string 3 link)))
  16922. nil t link)
  16923. link)))
  16924. ;; Speedbar support
  16925. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  16926. "Overlay marking the agenda restriction line in speedbar.")
  16927. (overlay-put org-speedbar-restriction-lock-overlay
  16928. 'face 'org-agenda-restriction-lock)
  16929. (overlay-put org-speedbar-restriction-lock-overlay
  16930. 'help-echo "Agendas are currently limited to this item.")
  16931. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16932. (defun org-speedbar-set-agenda-restriction ()
  16933. "Restrict future agenda commands to the location at point in speedbar.
  16934. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16935. (interactive)
  16936. (require 'org-agenda)
  16937. (let (p m tp np dir txt)
  16938. (cond
  16939. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16940. 'org-imenu t))
  16941. (setq m (get-text-property p 'org-imenu-marker))
  16942. (with-current-buffer (marker-buffer m)
  16943. (goto-char m)
  16944. (org-agenda-set-restriction-lock 'subtree)))
  16945. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16946. 'speedbar-function 'speedbar-find-file))
  16947. (setq tp (previous-single-property-change
  16948. (1+ p) 'speedbar-function)
  16949. np (next-single-property-change
  16950. tp 'speedbar-function)
  16951. dir (speedbar-line-directory)
  16952. txt (buffer-substring-no-properties (or tp (point-min))
  16953. (or np (point-max))))
  16954. (with-current-buffer (find-file-noselect
  16955. (let ((default-directory dir))
  16956. (expand-file-name txt)))
  16957. (unless (org-mode-p)
  16958. (error "Cannot restrict to non-Org-mode file"))
  16959. (org-agenda-set-restriction-lock 'file)))
  16960. (t (error "Don't know how to restrict Org-mode's agenda")))
  16961. (move-overlay org-speedbar-restriction-lock-overlay
  16962. (point-at-bol) (point-at-eol))
  16963. (setq current-prefix-arg nil)
  16964. (org-agenda-maybe-redo)))
  16965. (eval-after-load "speedbar"
  16966. '(progn
  16967. (speedbar-add-supported-extension ".org")
  16968. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16969. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16970. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16971. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16972. (add-hook 'speedbar-visiting-tag-hook
  16973. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16974. ;;; Fixes and Hacks for problems with other packages
  16975. ;; Make flyspell not check words in links, to not mess up our keymap
  16976. (defun org-mode-flyspell-verify ()
  16977. "Don't let flyspell put overlays at active buttons."
  16978. (and (not (get-text-property (point) 'keymap))
  16979. (not (get-text-property (point) 'org-no-flyspell))))
  16980. (defun org-remove-flyspell-overlays-in (beg end)
  16981. "Remove flyspell overlays in region."
  16982. (and (org-bound-and-true-p flyspell-mode)
  16983. (fboundp 'flyspell-delete-region-overlays)
  16984. (flyspell-delete-region-overlays beg end))
  16985. (add-text-properties beg end '(org-no-flyspell t)))
  16986. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16987. (eval-after-load "bookmark"
  16988. '(if (boundp 'bookmark-after-jump-hook)
  16989. ;; We can use the hook
  16990. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16991. ;; Hook not available, use advice
  16992. (defadvice bookmark-jump (after org-make-visible activate)
  16993. "Make the position visible."
  16994. (org-bookmark-jump-unhide))))
  16995. ;; Make sure saveplace shows the location if it was hidden
  16996. (eval-after-load "saveplace"
  16997. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16998. "Make the position visible."
  16999. (org-bookmark-jump-unhide)))
  17000. ;; Make sure ecb shows the location if it was hidden
  17001. (eval-after-load "ecb"
  17002. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  17003. "Make hierarchy visible when jumping into location from ECB tree buffer."
  17004. (if (eq major-mode 'org-mode)
  17005. (org-show-context))))
  17006. (defun org-bookmark-jump-unhide ()
  17007. "Unhide the current position, to show the bookmark location."
  17008. (and (org-mode-p)
  17009. (or (org-invisible-p)
  17010. (save-excursion (goto-char (max (point-min) (1- (point))))
  17011. (org-invisible-p)))
  17012. (org-show-context 'bookmark-jump)))
  17013. ;; Make session.el ignore our circular variable
  17014. (eval-after-load "session"
  17015. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  17016. ;;;; Experimental code
  17017. (defun org-closed-in-range ()
  17018. "Sparse tree of items closed in a certain time range.
  17019. Still experimental, may disappear in the future."
  17020. (interactive)
  17021. ;; Get the time interval from the user.
  17022. (let* ((time1 (org-float-time
  17023. (org-read-date nil 'to-time nil "Starting date: ")))
  17024. (time2 (org-float-time
  17025. (org-read-date nil 'to-time nil "End date:")))
  17026. ;; callback function
  17027. (callback (lambda ()
  17028. (let ((time
  17029. (org-float-time
  17030. (apply 'encode-time
  17031. (org-parse-time-string
  17032. (match-string 1))))))
  17033. ;; check if time in interval
  17034. (and (>= time time1) (<= time time2))))))
  17035. ;; make tree, check each match with the callback
  17036. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  17037. ;;;; Finish up
  17038. (provide 'org)
  17039. (run-hooks 'org-load-hook)
  17040. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  17041. ;;; org.el ends here