org.el 599 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104
  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
  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.25b
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.25b"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  150. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  151. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  152. (const :tag " mew Links to Mew folders/messages" org-mew)
  153. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  154. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  155. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  156. (const :tag " vm: Links to VM folders/messages" org-vm)
  157. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  158. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  159. (const :tag " mouse: Additional mouse support" org-mouse)
  160. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  161. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  162. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  163. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  164. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  165. (const :tag "C collector: Collect properties into tables" org-collector)
  166. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  167. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  168. (const :tag "C eval: Include command output as text" org-eval)
  169. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  170. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  171. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  172. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  173. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  174. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  175. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  176. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  177. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  178. (const :tag "C R: Computation using the R language" org-R)
  179. (const :tag "C registry: A registry for Org links" org-registry)
  180. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  181. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  182. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  183. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  184. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  185. (defcustom org-support-shift-select nil
  186. "Non-nil means, make shift-cursor commands select text when possible.
  187. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  188. selecting a region, or enlarge thusly regions started in this way.
  189. In Org-mode, in special contexts, these same keys are used for other
  190. purposes, important enough to compete with shift selection. Org tries
  191. to balance these needs by supporting `shift-select-mode' outside these
  192. special contexts, under control of this variable.
  193. The default of this variable is nil, to avoid confusing behavior. Shifted
  194. cursor keys will then execute Org commands in the following contexts:
  195. - on a headline, changing TODO state (left/right) and priority (up/down)
  196. - on a time stamp, changing the time
  197. - in a plain list item, changing the bullet type
  198. - in a property definition line, switching between allowed values
  199. - in the BEGIN line of a clock table (changing the time block).
  200. Outside these contexts, the commands will throw an error.
  201. When this variable is t and the cursor is not in a special context,
  202. Org-mode will support shift-selection for making and enlarging regions.
  203. To make this more effective, the bullet cycling will no longer happen
  204. anywhere in an item line, but only if the cursor is exactly on the bullet.
  205. If you set this variable to the symbol `always', then the keys
  206. will not be special in headlines, property lines, and item lines, to make
  207. shift selection work there as well. If this is what you want, you can
  208. use the following alternative commands: `C-c C-t' and `C-c ,' to
  209. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  210. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  211. edited by hand or in column view.
  212. However, when the cursor is on a timestamp, shift-cursor commands
  213. will still edit the time stamp - this is just too good to give up.
  214. XEmacs user should have this variable set to nil, because shift-select-mode
  215. is Emacs 23 only."
  216. :group 'org
  217. :type '(choice
  218. (const :tag "Never" nil)
  219. (const :tag "When outside special context" t)
  220. (const :tag "Everywhere except timestamps" always)))
  221. (defgroup org-startup nil
  222. "Options concerning startup of Org-mode."
  223. :tag "Org Startup"
  224. :group 'org)
  225. (defcustom org-startup-folded t
  226. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: fold
  230. #+STARTUP: nofold
  231. #+STARTUP: content"
  232. :group 'org-startup
  233. :type '(choice
  234. (const :tag "nofold: show all" nil)
  235. (const :tag "fold: overview" t)
  236. (const :tag "content: all headlines" content)))
  237. (defcustom org-startup-truncated t
  238. "Non-nil means, entering Org-mode will set `truncate-lines'.
  239. This is useful since some lines containing links can be very long and
  240. uninteresting. Also tables look terrible when wrapped."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (defcustom org-startup-align-all-tables nil
  244. "Non-nil means, align all tables when visiting a file.
  245. This is useful when the column width in tables is forced with <N> cookies
  246. in table fields. Such tables will look correct only after the first re-align.
  247. This can also be configured on a per-file basis by adding one of
  248. the following lines anywhere in the buffer:
  249. #+STARTUP: align
  250. #+STARTUP: noalign"
  251. :group 'org-startup
  252. :type 'boolean)
  253. (defcustom org-insert-mode-line-in-empty-file nil
  254. "Non-nil means insert the first line setting Org-mode in empty files.
  255. When the function `org-mode' is called interactively in an empty file, this
  256. normally means that the file name does not automatically trigger Org-mode.
  257. To ensure that the file will always be in Org-mode in the future, a
  258. line enforcing Org-mode will be inserted into the buffer, if this option
  259. has been set."
  260. :group 'org-startup
  261. :type 'boolean)
  262. (defcustom org-replace-disputed-keys nil
  263. "Non-nil means use alternative key bindings for some keys.
  264. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  265. These keys are also used by other packages like shift-selection-mode'
  266. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  267. If you want to use Org-mode together with one of these other modes,
  268. or more generally if you would like to move some Org-mode commands to
  269. other keys, set this variable and configure the keys with the variable
  270. `org-disputed-keys'.
  271. This option is only relevant at load-time of Org-mode, and must be set
  272. *before* org.el is loaded. Changing it requires a restart of Emacs to
  273. become effective."
  274. :group 'org-startup
  275. :type 'boolean)
  276. (defcustom org-use-extra-keys nil
  277. "Non-nil means use extra key sequence definitions for certain
  278. commands. This happens automatically if you run XEmacs or if
  279. window-system is nil. This variable lets you do the same
  280. manually. You must set it before loading org.
  281. Example: on Carbon Emacs 22 running graphically, with an external
  282. keyboard on a Powerbook, the default way of setting M-left might
  283. not work for either Alt or ESC. Setting this variable will make
  284. it work for ESC."
  285. :group 'org-startup
  286. :type 'boolean)
  287. (if (fboundp 'defvaralias)
  288. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  289. (defcustom org-disputed-keys
  290. '(([(shift up)] . [(meta p)])
  291. ([(shift down)] . [(meta n)])
  292. ([(shift left)] . [(meta -)])
  293. ([(shift right)] . [(meta +)])
  294. ([(control shift right)] . [(meta shift +)])
  295. ([(control shift left)] . [(meta shift -)]))
  296. "Keys for which Org-mode and other modes compete.
  297. This is an alist, cars are the default keys, second element specifies
  298. the alternative to use when `org-replace-disputed-keys' is t.
  299. Keys can be specified in any syntax supported by `define-key'.
  300. The value of this option takes effect only at Org-mode's startup,
  301. therefore you'll have to restart Emacs to apply it after changing."
  302. :group 'org-startup
  303. :type 'alist)
  304. (defun org-key (key)
  305. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  306. Or return the original if not disputed."
  307. (if org-replace-disputed-keys
  308. (let* ((nkey (key-description key))
  309. (x (org-find-if (lambda (x)
  310. (equal (key-description (car x)) nkey))
  311. org-disputed-keys)))
  312. (if x (cdr x) key))
  313. key))
  314. (defun org-find-if (predicate seq)
  315. (catch 'exit
  316. (while seq
  317. (if (funcall predicate (car seq))
  318. (throw 'exit (car seq))
  319. (pop seq)))))
  320. (defun org-defkey (keymap key def)
  321. "Define a key, possibly translated, as returned by `org-key'."
  322. (define-key keymap (org-key key) def))
  323. (defcustom org-ellipsis nil
  324. "The ellipsis to use in the Org-mode outline.
  325. When nil, just use the standard three dots. When a string, use that instead,
  326. When a face, use the standard 3 dots, but with the specified face.
  327. The change affects only Org-mode (which will then use its own display table).
  328. Changing this requires executing `M-x org-mode' in a buffer to become
  329. effective."
  330. :group 'org-startup
  331. :type '(choice (const :tag "Default" nil)
  332. (face :tag "Face" :value org-warning)
  333. (string :tag "String" :value "...#")))
  334. (defvar org-display-table nil
  335. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  336. (defgroup org-keywords nil
  337. "Keywords in Org-mode."
  338. :tag "Org Keywords"
  339. :group 'org)
  340. (defcustom org-deadline-string "DEADLINE:"
  341. "String to mark deadline entries.
  342. A deadline is this string, followed by a time stamp. Should be a word,
  343. terminated by a colon. You can insert a schedule keyword and
  344. a timestamp with \\[org-deadline].
  345. Changes become only effective after restarting Emacs."
  346. :group 'org-keywords
  347. :type 'string)
  348. (defcustom org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].
  353. Changes become only effective after restarting Emacs."
  354. :group 'org-keywords
  355. :type 'string)
  356. (defcustom org-closed-string "CLOSED:"
  357. "String used as the prefix for timestamps logging closing a TODO entry."
  358. :group 'org-keywords
  359. :type 'string)
  360. (defcustom org-clock-string "CLOCK:"
  361. "String used as prefix for timestamps clocking work hours on an item."
  362. :group 'org-keywords
  363. :type 'string)
  364. (defcustom org-comment-string "COMMENT"
  365. "Entries starting with this keyword will never be exported.
  366. An entry can be toggled between COMMENT and normal with
  367. \\[org-toggle-comment].
  368. Changes become only effective after restarting Emacs."
  369. :group 'org-keywords
  370. :type 'string)
  371. (defcustom org-quote-string "QUOTE"
  372. "Entries starting with this keyword will be exported in fixed-width font.
  373. Quoting applies only to the text in the entry following the headline, and does
  374. not extend beyond the next headline, even if that is lower level.
  375. An entry can be toggled between QUOTE and normal with
  376. \\[org-toggle-fixed-width-section]."
  377. :group 'org-keywords
  378. :type 'string)
  379. (defconst org-repeat-re
  380. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  381. "Regular expression for specifying repeated events.
  382. After a match, group 1 contains the repeat expression.")
  383. (defgroup org-structure nil
  384. "Options concerning the general structure of Org-mode files."
  385. :tag "Org Structure"
  386. :group 'org)
  387. (defgroup org-reveal-location nil
  388. "Options about how to make context of a location visible."
  389. :tag "Org Reveal Location"
  390. :group 'org-structure)
  391. (defconst org-context-choice
  392. '(choice
  393. (const :tag "Always" t)
  394. (const :tag "Never" nil)
  395. (repeat :greedy t :tag "Individual contexts"
  396. (cons
  397. (choice :tag "Context"
  398. (const agenda)
  399. (const org-goto)
  400. (const occur-tree)
  401. (const tags-tree)
  402. (const link-search)
  403. (const mark-goto)
  404. (const bookmark-jump)
  405. (const isearch)
  406. (const default))
  407. (boolean))))
  408. "Contexts for the reveal options.")
  409. (defcustom org-show-hierarchy-above '((default . t))
  410. "Non-nil means, show full hierarchy when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the hierarchy of headings
  413. above the exposed location is shown.
  414. Turning this off for example for sparse trees makes them very compact.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. Valid contexts are
  417. agenda when exposing an entry from the agenda
  418. org-goto when using the command `org-goto' on key C-c C-j
  419. occur-tree when using the command `org-occur' on key C-c /
  420. tags-tree when constructing a sparse tree based on tags matches
  421. link-search when exposing search matches associated with a link
  422. mark-goto when exposing the jump goal of a mark
  423. bookmark-jump when exposing a bookmark location
  424. isearch when exiting from an incremental search
  425. default default for all contexts not set explicitly"
  426. :group 'org-reveal-location
  427. :type org-context-choice)
  428. (defcustom org-show-following-heading '((default . nil))
  429. "Non-nil means, show following heading when revealing a location.
  430. Org-mode often shows locations in an org-mode file which might have
  431. been invisible before. When this is set, the heading following the
  432. match is shown.
  433. Turning this off for example for sparse trees makes them very compact,
  434. but makes it harder to edit the location of the match. In such a case,
  435. use the command \\[org-reveal] to show more context.
  436. Instead of t, this can also be an alist specifying this option for different
  437. contexts. See `org-show-hierarchy-above' for valid contexts."
  438. :group 'org-reveal-location
  439. :type org-context-choice)
  440. (defcustom org-show-siblings '((default . nil) (isearch t))
  441. "Non-nil means, show all sibling heading when revealing a location.
  442. Org-mode often shows locations in an org-mode file which might have
  443. been invisible before. When this is set, the sibling of the current entry
  444. heading are all made visible. If `org-show-hierarchy-above' is t,
  445. the same happens on each level of the hierarchy above the current entry.
  446. By default this is on for the isearch context, off for all other contexts.
  447. Turning this off for example for sparse trees makes them very compact,
  448. but makes it harder to edit the location of the match. In such a case,
  449. use the command \\[org-reveal] to show more context.
  450. Instead of t, this can also be an alist specifying this option for different
  451. contexts. See `org-show-hierarchy-above' for valid contexts."
  452. :group 'org-reveal-location
  453. :type org-context-choice)
  454. (defcustom org-show-entry-below '((default . nil))
  455. "Non-nil means, show the entry below a headline when revealing a location.
  456. Org-mode often shows locations in an org-mode file which might have
  457. been invisible before. When this is set, the text below the headline that is
  458. exposed is also shown.
  459. By default this is off for all contexts.
  460. Instead of t, this can also be an alist specifying this option for different
  461. contexts. See `org-show-hierarchy-above' for valid contexts."
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-indirect-buffer-display 'other-window
  465. "How should indirect tree buffers be displayed?
  466. This applies to indirect buffers created with the commands
  467. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  468. Valid values are:
  469. current-window Display in the current window
  470. other-window Just display in another window.
  471. dedicated-frame Create one new frame, and re-use it each time.
  472. new-frame Make a new frame each time. Note that in this case
  473. previously-made indirect buffers are kept, and you need to
  474. kill these buffers yourself."
  475. :group 'org-structure
  476. :group 'org-agenda-windows
  477. :type '(choice
  478. (const :tag "In current window" current-window)
  479. (const :tag "In current frame, other window" other-window)
  480. (const :tag "Each time a new frame" new-frame)
  481. (const :tag "One dedicated frame" dedicated-frame)))
  482. (defgroup org-cycle nil
  483. "Options concerning visibility cycling in Org-mode."
  484. :tag "Org Cycle"
  485. :group 'org-structure)
  486. (defcustom org-cycle-max-level nil
  487. "Maximum level which should still be subject to visibility cycling.
  488. Levels higher than this will, for cycling, be treated as text, not a headline.
  489. When `org-odd-levels-only' is set, a value of N in this variable actually
  490. means 2N-1 stars as the limiting headline.
  491. When nil, cycle all levels.
  492. Note that the limiting level of cycling is also influenced by
  493. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  494. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  495. than its value."
  496. :group 'org-cycle
  497. :type '(choice
  498. (const :tag "No limit" nil)
  499. (integer :tag "Maximum level")))
  500. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  501. "Names of drawers. Drawers are not opened by cycling on the headline above.
  502. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  503. this:
  504. :DRAWERNAME:
  505. .....
  506. :END:
  507. The drawer \"PROPERTIES\" is special for capturing properties through
  508. the property API.
  509. Drawers can be defined on the per-file basis with a line like:
  510. #+DRAWERS: HIDDEN STATE PROPERTIES"
  511. :group 'org-structure
  512. :group 'org-cycle
  513. :type '(repeat (string :tag "Drawer Name")))
  514. (defcustom org-cycle-global-at-bob nil
  515. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  516. This makes it possible to do global cycling without having to use S-TAB or
  517. C-u TAB. For this special case to work, the first line of the buffer
  518. must not be a headline - it may be empty or some other text. When used in
  519. this way, `org-cycle-hook' is disables temporarily, to make sure the
  520. cursor stays at the beginning of the buffer.
  521. When this option is nil, don't do anything special at the beginning
  522. of the buffer."
  523. :group 'org-cycle
  524. :type 'boolean)
  525. (defcustom org-cycle-emulate-tab t
  526. "Where should `org-cycle' emulate TAB.
  527. nil Never
  528. white Only in completely white lines
  529. whitestart Only at the beginning of lines, before the first non-white char
  530. t Everywhere except in headlines
  531. exc-hl-bol Everywhere except at the start of a headline
  532. If TAB is used in a place where it does not emulate TAB, the current subtree
  533. visibility is cycled."
  534. :group 'org-cycle
  535. :type '(choice (const :tag "Never" nil)
  536. (const :tag "Only in completely white lines" white)
  537. (const :tag "Before first char in a line" whitestart)
  538. (const :tag "Everywhere except in headlines" t)
  539. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  540. ))
  541. (defcustom org-cycle-separator-lines 2
  542. "Number of empty lines needed to keep an empty line between collapsed trees.
  543. If you leave an empty line between the end of a subtree and the following
  544. headline, this empty line is hidden when the subtree is folded.
  545. Org-mode will leave (exactly) one empty line visible if the number of
  546. empty lines is equal or larger to the number given in this variable.
  547. So the default 2 means, at least 2 empty lines after the end of a subtree
  548. are needed to produce free space between a collapsed subtree and the
  549. following headline.
  550. Special case: when 0, never leave empty lines in collapsed view."
  551. :group 'org-cycle
  552. :type 'integer)
  553. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  554. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  555. org-cycle-hide-drawers
  556. org-cycle-show-empty-lines
  557. org-optimize-window-after-visibility-change)
  558. "Hook that is run after `org-cycle' has changed the buffer visibility.
  559. The function(s) in this hook must accept a single argument which indicates
  560. the new state that was set by the most recent `org-cycle' command. The
  561. argument is a symbol. After a global state change, it can have the values
  562. `overview', `content', or `all'. After a local state change, it can have
  563. the values `folded', `children', or `subtree'."
  564. :group 'org-cycle
  565. :type 'hook)
  566. (defgroup org-edit-structure nil
  567. "Options concerning structure editing in Org-mode."
  568. :tag "Org Edit Structure"
  569. :group 'org-structure)
  570. (defcustom org-odd-levels-only nil
  571. "Non-nil means, skip even levels and only use odd levels for the outline.
  572. This has the effect that two stars are being added/taken away in
  573. promotion/demotion commands. It also influences how levels are
  574. handled by the exporters.
  575. Changing it requires restart of `font-lock-mode' to become effective
  576. for fontification also in regions already fontified.
  577. You may also set this on a per-file basis by adding one of the following
  578. lines to the buffer:
  579. #+STARTUP: odd
  580. #+STARTUP: oddeven"
  581. :group 'org-edit-structure
  582. :group 'org-font-lock
  583. :type 'boolean)
  584. (defcustom org-adapt-indentation t
  585. "Non-nil means, adapt indentation when promoting and demoting.
  586. When this is set and the *entire* text in an entry is indented, the
  587. indentation is increased by one space in a demotion command, and
  588. decreased by one in a promotion command. If any line in the entry
  589. body starts at column 0, indentation is not changed at all."
  590. :group 'org-edit-structure
  591. :type 'boolean)
  592. (defcustom org-special-ctrl-a/e nil
  593. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  594. When t, `C-a' will bring back the cursor to the beginning of the
  595. headline text, i.e. after the stars and after a possible TODO keyword.
  596. In an item, this will be the position after the bullet.
  597. When the cursor is already at that position, another `C-a' will bring
  598. it to the beginning of the line.
  599. `C-e' will jump to the end of the headline, ignoring the presence of tags
  600. in the headline. A second `C-e' will then jump to the true end of the
  601. line, after any tags.
  602. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  603. and only a directly following, identical keypress will bring the cursor
  604. to the special positions.
  605. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  606. set separately."
  607. :group 'org-edit-structure
  608. :type '(choice
  609. (const :tag "off" nil)
  610. (const :tag "after stars/bullet and before tags first" t)
  611. (const :tag "true line boundary first" reversed)
  612. (cons :tag "Set C-a and C-e separately"
  613. (choice :tag "Special C-a"
  614. (const :tag "off" nil)
  615. (const :tag "after stars/bullet first" t)
  616. (const :tag "before stars/bullet first" reversed))
  617. (choice :tag "Special C-e"
  618. (const :tag "off" nil)
  619. (const :tag "before tags first" t)
  620. (const :tag "after tags first" reversed)))))
  621. (if (fboundp 'defvaralias)
  622. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  623. (defcustom org-special-ctrl-k nil
  624. "Non-nil means `C-k' will behave specially in headlines.
  625. When nil, `C-k' will call the default `kill-line' command.
  626. When t, the following will happen while the cursor is in the headline:
  627. - When the cursor is at the beginning of a headline, kill the entire
  628. line and possible the folded subtree below the line.
  629. - When in the middle of the headline text, kill the headline up to the tags.
  630. - When after the headline text, kill the tags."
  631. :group 'org-edit-structure
  632. :type 'boolean)
  633. (defcustom org-yank-folded-subtrees t
  634. "Non-nil means, when yanking subtrees, fold them.
  635. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  636. it starts with a heading and all other headings in it are either children
  637. or siblings, then fold all the subtrees. However, do this only if no
  638. text after the yank would be swallowed into a folded tree by this action."
  639. :group 'org-edit-structure
  640. :type 'boolean)
  641. (defcustom org-yank-adjusted-subtrees nil
  642. "Non-nil means, when yanking subtrees, adjust the level.
  643. With this setting, `org-paste-subtree' is used to insert the subtree, see
  644. this function for details."
  645. :group 'org-edit-structure
  646. :type 'boolean)
  647. (defcustom org-M-RET-may-split-line '((default . t))
  648. "Non-nil means, M-RET will split the line at the cursor position.
  649. When nil, it will go to the end of the line before making a
  650. new line.
  651. You may also set this option in a different way for different
  652. contexts. Valid contexts are:
  653. headline when creating a new headline
  654. item when creating a new item
  655. table in a table field
  656. default the value to be used for all contexts not explicitly
  657. customized"
  658. :group 'org-structure
  659. :group 'org-table
  660. :type '(choice
  661. (const :tag "Always" t)
  662. (const :tag "Never" nil)
  663. (repeat :greedy t :tag "Individual contexts"
  664. (cons
  665. (choice :tag "Context"
  666. (const headline)
  667. (const item)
  668. (const table)
  669. (const default))
  670. (boolean)))))
  671. (defcustom org-insert-heading-respect-content nil
  672. "Non-nil means, insert new headings after the current subtree.
  673. When nil, the new heading is created directly after the current line.
  674. The commands \\[org-insert-heading-respect-content] and
  675. \\[org-insert-todo-heading-respect-content] turn this variable on
  676. for the duration of the command."
  677. :group 'org-structure
  678. :type 'boolean)
  679. (defcustom org-blank-before-new-entry '((heading . auto)
  680. (plain-list-item . auto))
  681. "Should `org-insert-heading' leave a blank line before new heading/item?
  682. The value is an alist, with `heading' and `plain-list-item' as car,
  683. and a boolean flag as cdr. For plain lists, if the variable
  684. `org-empty-line-terminates-plain-lists' is set, the setting here
  685. is ignored and no empty line is inserted, to keep the list in tact."
  686. :group 'org-edit-structure
  687. :type '(list
  688. (cons (const heading)
  689. (choice (const :tag "Never" nil)
  690. (const :tag "Always" t)
  691. (const :tag "Auto" auto)))
  692. (cons (const plain-list-item)
  693. (choice (const :tag "Never" nil)
  694. (const :tag "Always" t)
  695. (const :tag "Auto" auto)))))
  696. (defcustom org-insert-heading-hook nil
  697. "Hook being run after inserting a new heading."
  698. :group 'org-edit-structure
  699. :type 'hook)
  700. (defcustom org-enable-fixed-width-editor t
  701. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  702. This currently only means, they are never auto-wrapped.
  703. When nil, such lines will be treated like ordinary lines.
  704. See also the QUOTE keyword."
  705. :group 'org-edit-structure
  706. :type 'boolean)
  707. (defcustom org-edit-src-region-extra nil
  708. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  709. For examples see the function `org-edit-src-find-region-and-lang'.
  710. The regular expression identifying the begin marker should end with a newline,
  711. and the regexp marking the end line should start with a newline, to make sure
  712. there are kept outside the narrowed region."
  713. :group 'org-edit-structure
  714. :type '(repeat
  715. (list
  716. (regexp :tag "begin regexp")
  717. (regexp :tag "end regexp")
  718. (choice :tag "language"
  719. (string :tag "specify")
  720. (integer :tag "from match group")
  721. (const :tag "from `lang' element")
  722. (const :tag "from `style' element")))))
  723. (defcustom org-coderef-label-format "(ref:%s)"
  724. "The default coderef format.
  725. This format string will be used to search for coderef labels in literal
  726. examples (EXAMPLE and SRC blocks). The format can be overwritten
  727. an individual literal example with the -f option, like
  728. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  729. ...
  730. #+END_SRC
  731. If you want to use this for HTML export, make sure that the format does
  732. not introduce special font-locking, and avoid the HTML special
  733. characters `<', `>', and `&'. The reason for this restriction is that
  734. the labels are searched for only after htmlize has done its job."
  735. :group 'org-edit-structure ; FIXME this is not in the right group
  736. :type 'string)
  737. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  738. "The mode that should be used to edit fixed-width regions.
  739. These are the regions where each line starts with a colon."
  740. :group 'org-edit-structure
  741. :type '(choice
  742. (const artist-mode)
  743. (const picture-mode)
  744. (const fundamental-mode)
  745. (function :tag "Other (specify)")))
  746. (defcustom org-goto-auto-isearch t
  747. "Non-nil means, typing characters in org-goto starts incremental search."
  748. :group 'org-edit-structure
  749. :type 'boolean)
  750. (defgroup org-sparse-trees nil
  751. "Options concerning sparse trees in Org-mode."
  752. :tag "Org Sparse Trees"
  753. :group 'org-structure)
  754. (defcustom org-highlight-sparse-tree-matches t
  755. "Non-nil means, highlight all matches that define a sparse tree.
  756. The highlights will automatically disappear the next time the buffer is
  757. changed by an edit command."
  758. :group 'org-sparse-trees
  759. :type 'boolean)
  760. (defcustom org-remove-highlights-with-change t
  761. "Non-nil means, any change to the buffer will remove temporary highlights.
  762. Such highlights are created by `org-occur' and `org-clock-display'.
  763. When nil, `C-c C-c needs to be used to get rid of the highlights.
  764. The highlights created by `org-preview-latex-fragment' always need
  765. `C-c C-c' to be removed."
  766. :group 'org-sparse-trees
  767. :group 'org-time
  768. :type 'boolean)
  769. (defcustom org-occur-hook '(org-first-headline-recenter)
  770. "Hook that is run after `org-occur' has constructed a sparse tree.
  771. This can be used to recenter the window to show as much of the structure
  772. as possible."
  773. :group 'org-sparse-trees
  774. :type 'hook)
  775. (defgroup org-imenu-and-speedbar nil
  776. "Options concerning imenu and speedbar in Org-mode."
  777. :tag "Org Imenu and Speedbar"
  778. :group 'org-structure)
  779. (defcustom org-imenu-depth 2
  780. "The maximum level for Imenu access to Org-mode headlines.
  781. This also applied for speedbar access."
  782. :group 'org-imenu-and-speedbar
  783. :type 'integer)
  784. (defgroup org-table nil
  785. "Options concerning tables in Org-mode."
  786. :tag "Org Table"
  787. :group 'org)
  788. (defcustom org-enable-table-editor 'optimized
  789. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  790. When nil, such lines will be treated like ordinary lines.
  791. When equal to the symbol `optimized', the table editor will be optimized to
  792. do the following:
  793. - Automatic overwrite mode in front of whitespace in table fields.
  794. This makes the structure of the table stay in tact as long as the edited
  795. field does not exceed the column width.
  796. - Minimize the number of realigns. Normally, the table is aligned each time
  797. TAB or RET are pressed to move to another field. With optimization this
  798. happens only if changes to a field might have changed the column width.
  799. Optimization requires replacing the functions `self-insert-command',
  800. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  801. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  802. very good at guessing when a re-align will be necessary, but you can always
  803. force one with \\[org-ctrl-c-ctrl-c].
  804. If you would like to use the optimized version in Org-mode, but the
  805. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  806. This variable can be used to turn on and off the table editor during a session,
  807. but in order to toggle optimization, a restart is required.
  808. See also the variable `org-table-auto-blank-field'."
  809. :group 'org-table
  810. :type '(choice
  811. (const :tag "off" nil)
  812. (const :tag "on" t)
  813. (const :tag "on, optimized" optimized)))
  814. (defcustom org-self-insert-cluster-for-undo t
  815. "Non-nil means cluster self-insert commands for undo when possible.
  816. If this is set, then, like in the Emacs command loop, 20 consequtive
  817. characters will be undone together.
  818. This is configurable, because there is some impact on typing performance."
  819. :group 'org-table
  820. :type 'boolean)
  821. (defcustom org-table-tab-recognizes-table.el t
  822. "Non-nil means, TAB will automatically notice a table.el table.
  823. When it sees such a table, it moves point into it and - if necessary -
  824. calls `table-recognize-table'."
  825. :group 'org-table-editing
  826. :type 'boolean)
  827. (defgroup org-link nil
  828. "Options concerning links in Org-mode."
  829. :tag "Org Link"
  830. :group 'org)
  831. (defvar org-link-abbrev-alist-local nil
  832. "Buffer-local version of `org-link-abbrev-alist', which see.
  833. The value of this is taken from the #+LINK lines.")
  834. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  835. (defcustom org-link-abbrev-alist nil
  836. "Alist of link abbreviations.
  837. The car of each element is a string, to be replaced at the start of a link.
  838. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  839. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  840. [[linkkey:tag][description]]
  841. The 'linkkey' must be a word word, starting with a letter, followed
  842. by letters, numbers, '-' or '_'.
  843. If REPLACE is a string, the tag will simply be appended to create the link.
  844. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  845. the placeholder \"%h\" will cause a url-encoded version of the tag to
  846. be inserted at that point (see the function `url-hexify-string').
  847. REPLACE may also be a function that will be called with the tag as the
  848. only argument to create the link, which should be returned as a string.
  849. See the manual for examples."
  850. :group 'org-link
  851. :type '(repeat
  852. (cons
  853. (string :tag "Protocol")
  854. (choice
  855. (string :tag "Format")
  856. (function)))))
  857. (defcustom org-descriptive-links t
  858. "Non-nil means, hide link part and only show description of bracket links.
  859. Bracket links are like [[link][description]]. This variable sets the initial
  860. state in new org-mode buffers. The setting can then be toggled on a
  861. per-buffer basis from the Org->Hyperlinks menu."
  862. :group 'org-link
  863. :type 'boolean)
  864. (defcustom org-link-file-path-type 'adaptive
  865. "How the path name in file links should be stored.
  866. Valid values are:
  867. relative Relative to the current directory, i.e. the directory of the file
  868. into which the link is being inserted.
  869. absolute Absolute path, if possible with ~ for home directory.
  870. noabbrev Absolute path, no abbreviation of home directory.
  871. adaptive Use relative path for files in the current directory and sub-
  872. directories of it. For other files, use an absolute path."
  873. :group 'org-link
  874. :type '(choice
  875. (const relative)
  876. (const absolute)
  877. (const noabbrev)
  878. (const adaptive)))
  879. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  880. "Types of links that should be activated in Org-mode files.
  881. This is a list of symbols, each leading to the activation of a certain link
  882. type. In principle, it does not hurt to turn on most link types - there may
  883. be a small gain when turning off unused link types. The types are:
  884. bracket The recommended [[link][description]] or [[link]] links with hiding.
  885. angular Links in angular brackets that may contain whitespace like
  886. <bbdb:Carsten Dominik>.
  887. plain Plain links in normal text, no whitespace, like http://google.com.
  888. radio Text that is matched by a radio target, see manual for details.
  889. tag Tag settings in a headline (link to tag search).
  890. date Time stamps (link to calendar).
  891. footnote Footnote labels.
  892. Changing this variable requires a restart of Emacs to become effective."
  893. :group 'org-link
  894. :type '(set :greedy t
  895. (const :tag "Double bracket links (new style)" bracket)
  896. (const :tag "Angular bracket links (old style)" angular)
  897. (const :tag "Plain text links" plain)
  898. (const :tag "Radio target matches" radio)
  899. (const :tag "Tags" tag)
  900. (const :tag "Timestamps" date)
  901. (const :tag "Footnotes" footnote)))
  902. (defcustom org-make-link-description-function nil
  903. "Function to use to generate link descriptions from links. If
  904. nil the link location will be used. This function must take two
  905. parameters; the first is the link and the second the description
  906. org-insert-link has generated, and should return the description
  907. to use."
  908. :group 'org-link
  909. :type 'function)
  910. (defgroup org-link-store nil
  911. "Options concerning storing links in Org-mode."
  912. :tag "Org Store Link"
  913. :group 'org-link)
  914. (defcustom org-email-link-description-format "Email %c: %.30s"
  915. "Format of the description part of a link to an email or usenet message.
  916. The following %-escapes will be replaced by corresponding information:
  917. %F full \"From\" field
  918. %f name, taken from \"From\" field, address if no name
  919. %T full \"To\" field
  920. %t first name in \"To\" field, address if no name
  921. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  922. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  923. %s subject
  924. %m message-id.
  925. You may use normal field width specification between the % and the letter.
  926. This is for example useful to limit the length of the subject.
  927. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  928. :group 'org-link-store
  929. :type 'string)
  930. (defcustom org-from-is-user-regexp
  931. (let (r1 r2)
  932. (when (and user-mail-address (not (string= user-mail-address "")))
  933. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  934. (when (and user-full-name (not (string= user-full-name "")))
  935. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  936. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  937. "Regexp matched against the \"From:\" header of an email or usenet message.
  938. It should match if the message is from the user him/herself."
  939. :group 'org-link-store
  940. :type 'regexp)
  941. (defcustom org-link-to-org-use-id 'create-if-interactive
  942. "Non-nil means, storing a link to an Org file will use entry IDs.
  943. Note that before this variable is even considered, org-id must be loaded,
  944. to please customize `org-modules' and turn it on.
  945. The variable can have the following values:
  946. t Create an ID if needed to make a link to the current entry.
  947. create-if-interactive
  948. If `org-store-link' is called directly (interactively, as a user
  949. command), do create an ID to support the link. But when doing the
  950. job for remember, only use the ID if it already exists. The
  951. purpose of this setting is to avoid proliferation of unwanted
  952. IDs, just because you happen to be in an Org file when you
  953. call `org-remember' that automatically and preemptively
  954. creates a link. If you do want to get an ID link in a remember
  955. template to an entry not having an ID, create it first by
  956. explicitly creating a link to it, using `C-c C-l' first.
  957. use-existing
  958. Use existing ID, do not create one.
  959. nil Never use an ID to make a link, instead link using a text search for
  960. the headline text."
  961. :group 'org-link-store
  962. :type '(choice
  963. (const :tag "Create ID to make link" t)
  964. (const :tag "Create if string link interactively"
  965. 'create-if-interactive)
  966. (const :tag "Only use existing" 'use-existing)
  967. (const :tag "Do not use ID to create link" nil)))
  968. (defcustom org-context-in-file-links t
  969. "Non-nil means, file links from `org-store-link' contain context.
  970. A search string will be added to the file name with :: as separator and
  971. used to find the context when the link is activated by the command
  972. `org-open-at-point'.
  973. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  974. negates this setting for the duration of the command."
  975. :group 'org-link-store
  976. :type 'boolean)
  977. (defcustom org-keep-stored-link-after-insertion nil
  978. "Non-nil means, keep link in list for entire session.
  979. The command `org-store-link' adds a link pointing to the current
  980. location to an internal list. These links accumulate during a session.
  981. The command `org-insert-link' can be used to insert links into any
  982. Org-mode file (offering completion for all stored links). When this
  983. option is nil, every link which has been inserted once using \\[org-insert-link]
  984. will be removed from the list, to make completing the unused links
  985. more efficient."
  986. :group 'org-link-store
  987. :type 'boolean)
  988. (defgroup org-link-follow nil
  989. "Options concerning following links in Org-mode."
  990. :tag "Org Follow Link"
  991. :group 'org-link)
  992. (defcustom org-link-translation-function nil
  993. "Function to translate links with different syntax to Org syntax.
  994. This can be used to translate links created for example by the Planner
  995. or emacs-wiki packages to Org syntax.
  996. The function must accept two parameters, a TYPE containing the link
  997. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  998. which is everything after the link protocol. It should return a cons
  999. with possibly modified values of type and path.
  1000. Org contains a function for this, so if you set this variable to
  1001. `org-translate-link-from-planner', you should be able follow many
  1002. links created by planner."
  1003. :group 'org-link-follow
  1004. :type 'function)
  1005. (defcustom org-follow-link-hook nil
  1006. "Hook that is run after a link has been followed."
  1007. :group 'org-link-follow
  1008. :type 'hook)
  1009. (defcustom org-tab-follows-link nil
  1010. "Non-nil means, on links TAB will follow the link.
  1011. Needs to be set before org.el is loaded.
  1012. This really should not be used, it does not make sense, and the
  1013. implementation is bad."
  1014. :group 'org-link-follow
  1015. :type 'boolean)
  1016. (defcustom org-return-follows-link nil
  1017. "Non-nil means, on links RET will follow the link.
  1018. Needs to be set before org.el is loaded."
  1019. :group 'org-link-follow
  1020. :type 'boolean)
  1021. (defcustom org-mouse-1-follows-link
  1022. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1023. "Non-nil means, mouse-1 on a link will follow the link.
  1024. A longer mouse click will still set point. Does not work on XEmacs.
  1025. Needs to be set before org.el is loaded."
  1026. :group 'org-link-follow
  1027. :type 'boolean)
  1028. (defcustom org-mark-ring-length 4
  1029. "Number of different positions to be recorded in the ring
  1030. Changing this requires a restart of Emacs to work correctly."
  1031. :group 'org-link-follow
  1032. :type 'integer)
  1033. (defcustom org-link-frame-setup
  1034. '((vm . vm-visit-folder-other-frame)
  1035. (gnus . gnus-other-frame)
  1036. (file . find-file-other-window))
  1037. "Setup the frame configuration for following links.
  1038. When following a link with Emacs, it may often be useful to display
  1039. this link in another window or frame. This variable can be used to
  1040. set this up for the different types of links.
  1041. For VM, use any of
  1042. `vm-visit-folder'
  1043. `vm-visit-folder-other-frame'
  1044. For Gnus, use any of
  1045. `gnus'
  1046. `gnus-other-frame'
  1047. `org-gnus-no-new-news'
  1048. For FILE, use any of
  1049. `find-file'
  1050. `find-file-other-window'
  1051. `find-file-other-frame'
  1052. For the calendar, use the variable `calendar-setup'.
  1053. For BBDB, it is currently only possible to display the matches in
  1054. another window."
  1055. :group 'org-link-follow
  1056. :type '(list
  1057. (cons (const vm)
  1058. (choice
  1059. (const vm-visit-folder)
  1060. (const vm-visit-folder-other-window)
  1061. (const vm-visit-folder-other-frame)))
  1062. (cons (const gnus)
  1063. (choice
  1064. (const gnus)
  1065. (const gnus-other-frame)
  1066. (const org-gnus-no-new-news)))
  1067. (cons (const file)
  1068. (choice
  1069. (const find-file)
  1070. (const find-file-other-window)
  1071. (const find-file-other-frame)))))
  1072. (defcustom org-display-internal-link-with-indirect-buffer nil
  1073. "Non-nil means, use indirect buffer to display infile links.
  1074. Activating internal links (from one location in a file to another location
  1075. in the same file) normally just jumps to the location. When the link is
  1076. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1077. another window. When this option is set, the other window actually displays
  1078. an indirect buffer clone of the current buffer, to avoid any visibility
  1079. changes to the current buffer."
  1080. :group 'org-link-follow
  1081. :type 'boolean)
  1082. (defcustom org-open-non-existing-files nil
  1083. "Non-nil means, `org-open-file' will open non-existing files.
  1084. When nil, an error will be generated."
  1085. :group 'org-link-follow
  1086. :type 'boolean)
  1087. (defcustom org-open-directory-means-index-dot-org nil
  1088. "Non-nil means, a link to a directory really means to index.org.
  1089. When nil, following a directory link will run dired or open a finder/explorer
  1090. window on that directory."
  1091. :group 'org-link-follow
  1092. :type 'boolean)
  1093. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1094. "Function and arguments to call for following mailto links.
  1095. This is a list with the first element being a lisp function, and the
  1096. remaining elements being arguments to the function. In string arguments,
  1097. %a will be replaced by the address, and %s will be replaced by the subject
  1098. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1099. :group 'org-link-follow
  1100. :type '(choice
  1101. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1102. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1103. (const :tag "message-mail" (message-mail "%a" "%s"))
  1104. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1105. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1106. "Non-nil means, ask for confirmation before executing shell links.
  1107. Shell links can be dangerous: just think about a link
  1108. [[shell:rm -rf ~/*][Google Search]]
  1109. This link would show up in your Org-mode document as \"Google Search\",
  1110. but really it would remove your entire home directory.
  1111. Therefore we advise against setting this variable to nil.
  1112. Just change it to `y-or-n-p' if you want to confirm with a
  1113. single keystroke rather than having to type \"yes\"."
  1114. :group 'org-link-follow
  1115. :type '(choice
  1116. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1117. (const :tag "with y-or-n (faster)" y-or-n-p)
  1118. (const :tag "no confirmation (dangerous)" nil)))
  1119. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1120. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1121. Elisp links can be dangerous: just think about a link
  1122. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1123. This link would show up in your Org-mode document as \"Google Search\",
  1124. but really it would remove your entire home directory.
  1125. Therefore we advise against setting this variable to nil.
  1126. Just change it to `y-or-n-p' if you want to confirm with a
  1127. single keystroke rather than having to type \"yes\"."
  1128. :group 'org-link-follow
  1129. :type '(choice
  1130. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1131. (const :tag "with y-or-n (faster)" y-or-n-p)
  1132. (const :tag "no confirmation (dangerous)" nil)))
  1133. (defconst org-file-apps-defaults-gnu
  1134. '((remote . emacs)
  1135. (system . mailcap)
  1136. (t . mailcap))
  1137. "Default file applications on a UNIX or GNU/Linux system.
  1138. See `org-file-apps'.")
  1139. (defconst org-file-apps-defaults-macosx
  1140. '((remote . emacs)
  1141. (t . "open %s")
  1142. (system . "open %s")
  1143. ("ps.gz" . "gv %s")
  1144. ("eps.gz" . "gv %s")
  1145. ("dvi" . "xdvi %s")
  1146. ("fig" . "xfig %s"))
  1147. "Default file applications on a MacOS X system.
  1148. The system \"open\" is known as a default, but we use X11 applications
  1149. for some files for which the OS does not have a good default.
  1150. See `org-file-apps'.")
  1151. (defconst org-file-apps-defaults-windowsnt
  1152. (list
  1153. '(remote . emacs)
  1154. (cons t
  1155. (list (if (featurep 'xemacs)
  1156. 'mswindows-shell-execute
  1157. 'w32-shell-execute)
  1158. "open" 'file))
  1159. (cons 'system
  1160. (list (if (featurep 'xemacs)
  1161. 'mswindows-shell-execute
  1162. 'w32-shell-execute)
  1163. "open" 'file)))
  1164. "Default file applications on a Windows NT system.
  1165. The system \"open\" is used for most files.
  1166. See `org-file-apps'.")
  1167. (defcustom org-file-apps
  1168. '(
  1169. (auto-mode . emacs)
  1170. ("\\.x?html?\\'" . default)
  1171. ("\\.pdf\\'" . default)
  1172. )
  1173. "External applications for opening `file:path' items in a document.
  1174. Org-mode uses system defaults for different file types, but
  1175. you can use this variable to set the application for a given file
  1176. extension. The entries in this list are cons cells where the car identifies
  1177. files and the cdr the corresponding command. Possible values for the
  1178. file identifier are
  1179. \"regex\" Regular expression matched against the file name. For backward
  1180. compatibility, this can also be a string with only alphanumeric
  1181. characters, which is then interpreted as an extension.
  1182. `directory' Matches a directory
  1183. `remote' Matches a remote file, accessible through tramp or efs.
  1184. Remote files most likely should be visited through Emacs
  1185. because external applications cannot handle such paths.
  1186. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1187. so all files Emacs knows how to handle. Using this with
  1188. command `emacs' will open most files in Emacs. Beware that this
  1189. will also open html files inside Emacs, unless you add
  1190. (\"html\" . default) to the list as well.
  1191. t Default for files not matched by any of the other options.
  1192. `system' The system command to open files, like `open' on Windows
  1193. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1194. that will be selected if you call `C-c C-o' with a double
  1195. `C-u C-u' prefix.
  1196. Possible values for the command are:
  1197. `emacs' The file will be visited by the current Emacs process.
  1198. `default' Use the default application for this file type, which is the
  1199. association for t in the list, most likely in the system-specific
  1200. part.
  1201. This can be used to overrule an unwanted setting in the
  1202. system-specific variable.
  1203. `system' Use the system command for opening files, like \"open\".
  1204. This command is specified by the entry whose car is `system'.
  1205. Most likely, the system-specific version of this variable
  1206. does define this command, but you can overrule/replace it
  1207. here.
  1208. string A command to be executed by a shell; %s will be replaced
  1209. by the path to the file.
  1210. sexp A Lisp form which will be evaluated. The file path will
  1211. be available in the Lisp variable `file'.
  1212. For more examples, see the system specific constants
  1213. `org-file-apps-defaults-macosx'
  1214. `org-file-apps-defaults-windowsnt'
  1215. `org-file-apps-defaults-gnu'."
  1216. :group 'org-link-follow
  1217. :type '(repeat
  1218. (cons (choice :value ""
  1219. (string :tag "Extension")
  1220. (const :tag "System command to open files" system)
  1221. (const :tag "Default for unrecognized files" t)
  1222. (const :tag "Remote file" remote)
  1223. (const :tag "Links to a directory" directory)
  1224. (const :tag "Any files that have Emacs modes"
  1225. auto-mode))
  1226. (choice :value ""
  1227. (const :tag "Visit with Emacs" emacs)
  1228. (const :tag "Use default" default)
  1229. (const :tag "Use the system command" system)
  1230. (string :tag "Command")
  1231. (sexp :tag "Lisp form")))))
  1232. (defgroup org-refile nil
  1233. "Options concerning refiling entries in Org-mode."
  1234. :tag "Org Refile"
  1235. :group 'org)
  1236. (defcustom org-directory "~/org"
  1237. "Directory with org files.
  1238. This is just a default location to look for Org files. There is no need
  1239. at all to put your files into this directory. It is only used in the
  1240. following situations:
  1241. 1. When a remember template specifies a target file that is not an
  1242. absolute path. The path will then be interpreted relative to
  1243. `org-directory'
  1244. 2. When a remember note is filed away in an interactive way (when exiting the
  1245. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1246. with `org-directory' as the default path."
  1247. :group 'org-refile
  1248. :group 'org-remember
  1249. :type 'directory)
  1250. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1251. "Default target for storing notes.
  1252. Used by the hooks for remember.el. This can be a string, or nil to mean
  1253. the value of `remember-data-file'.
  1254. You can set this on a per-template basis with the variable
  1255. `org-remember-templates'."
  1256. :group 'org-refile
  1257. :group 'org-remember
  1258. :type '(choice
  1259. (const :tag "Default from remember-data-file" nil)
  1260. file))
  1261. (defcustom org-goto-interface 'outline
  1262. "The default interface to be used for `org-goto'.
  1263. Allowed values are:
  1264. outline The interface shows an outline of the relevant file
  1265. and the correct heading is found by moving through
  1266. the outline or by searching with incremental search.
  1267. outline-path-completion Headlines in the current buffer are offered via
  1268. completion. This is the interface also used by
  1269. the refile command."
  1270. :group 'org-refile
  1271. :type '(choice
  1272. (const :tag "Outline" outline)
  1273. (const :tag "Outline-path-completion" outline-path-completion)))
  1274. (defcustom org-goto-max-level 5
  1275. "Maximum level to be considered when running org-goto with refile interface."
  1276. :group 'org-refile
  1277. :type 'integer)
  1278. (defcustom org-reverse-note-order nil
  1279. "Non-nil means, store new notes at the beginning of a file or entry.
  1280. When nil, new notes will be filed to the end of a file or entry.
  1281. This can also be a list with cons cells of regular expressions that
  1282. are matched against file names, and values."
  1283. :group 'org-remember
  1284. :group 'org-refile
  1285. :type '(choice
  1286. (const :tag "Reverse always" t)
  1287. (const :tag "Reverse never" nil)
  1288. (repeat :tag "By file name regexp"
  1289. (cons regexp boolean))))
  1290. (defcustom org-refile-targets nil
  1291. "Targets for refiling entries with \\[org-refile].
  1292. This is list of cons cells. Each cell contains:
  1293. - a specification of the files to be considered, either a list of files,
  1294. or a symbol whose function or variable value will be used to retrieve
  1295. a file name or a list of file names. If you use `org-agenda-files' for
  1296. that, all agenda files will be scanned for targets. Nil means, consider
  1297. headings in the current buffer.
  1298. - A specification of how to find candidate refile targets. This may be
  1299. any of:
  1300. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1301. This tag has to be present in all target headlines, inheritance will
  1302. not be considered.
  1303. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1304. todo keyword.
  1305. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1306. headlines that are refiling targets.
  1307. - a cons cell (:level . N). Any headline of level N is considered a target.
  1308. Note that, when `org-odd-levels-only' is set, level corresponds to
  1309. order in hierarchy, not to the number of stars.
  1310. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1311. Note that, when `org-odd-levels-only' is set, level corresponds to
  1312. order in hierarchy, not to the number of stars.
  1313. When this variable is nil, all top-level headlines in the current buffer
  1314. are used, equivalent to the value `((nil . (:level . 1))'."
  1315. :group 'org-refile
  1316. :type '(repeat
  1317. (cons
  1318. (choice :value org-agenda-files
  1319. (const :tag "All agenda files" org-agenda-files)
  1320. (const :tag "Current buffer" nil)
  1321. (function) (variable) (file))
  1322. (choice :tag "Identify target headline by"
  1323. (cons :tag "Specific tag" (const :value :tag) (string))
  1324. (cons :tag "TODO keyword" (const :value :todo) (string))
  1325. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1326. (cons :tag "Level number" (const :value :level) (integer))
  1327. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1328. (defcustom org-refile-use-outline-path nil
  1329. "Non-nil means, provide refile targets as paths.
  1330. So a level 3 headline will be available as level1/level2/level3.
  1331. When the value is `file', also include the file name (without directory)
  1332. into the path. When `full-file-path', include the full file path."
  1333. :group 'org-refile
  1334. :type '(choice
  1335. (const :tag "Not" nil)
  1336. (const :tag "Yes" t)
  1337. (const :tag "Start with file name" file)
  1338. (const :tag "Start with full file path" full-file-path)))
  1339. (defcustom org-outline-path-complete-in-steps t
  1340. "Non-nil means, complete the outline path in hierarchical steps.
  1341. When Org-mode uses the refile interface to select an outline path
  1342. \(see variable `org-refile-use-outline-path'), the completion of
  1343. the path can be done is a single go, or if can be done in steps down
  1344. the headline hierarchy. Going in steps is probably the best if you
  1345. do not use a special completion package like `ido' or `icicles'.
  1346. However, when using these packages, going in one step can be very
  1347. fast, while still showing the whole path to the entry."
  1348. :group 'org-refile
  1349. :type 'boolean)
  1350. (defgroup org-todo nil
  1351. "Options concerning TODO items in Org-mode."
  1352. :tag "Org TODO"
  1353. :group 'org)
  1354. (defgroup org-progress nil
  1355. "Options concerning Progress logging in Org-mode."
  1356. :tag "Org Progress"
  1357. :group 'org-time)
  1358. (defvar org-todo-interpretation-widgets
  1359. '(
  1360. (:tag "Sequence (cycling hits every state)" sequence)
  1361. (:tag "Type (cycling directly to DONE)" type))
  1362. "The available interpretation symbols for customizing
  1363. `org-todo-keywords'.
  1364. Interested libraries should add to this list.")
  1365. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1366. "List of TODO entry keyword sequences and their interpretation.
  1367. \\<org-mode-map>This is a list of sequences.
  1368. Each sequence starts with a symbol, either `sequence' or `type',
  1369. indicating if the keywords should be interpreted as a sequence of
  1370. action steps, or as different types of TODO items. The first
  1371. keywords are states requiring action - these states will select a headline
  1372. for inclusion into the global TODO list Org-mode produces. If one of
  1373. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1374. signify that no further action is necessary. If \"|\" is not found,
  1375. the last keyword is treated as the only DONE state of the sequence.
  1376. The command \\[org-todo] cycles an entry through these states, and one
  1377. additional state where no keyword is present. For details about this
  1378. cycling, see the manual.
  1379. TODO keywords and interpretation can also be set on a per-file basis with
  1380. the special #+SEQ_TODO and #+TYP_TODO lines.
  1381. Each keyword can optionally specify a character for fast state selection
  1382. \(in combination with the variable `org-use-fast-todo-selection')
  1383. and specifiers for state change logging, using the same syntax
  1384. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1385. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1386. indicates to record a time stamp each time this state is selected.
  1387. Each keyword may also specify if a timestamp or a note should be
  1388. recorded when entering or leaving the state, by adding additional
  1389. characters in the parenthesis after the keyword. This looks like this:
  1390. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1391. record only the time of the state change. With X and Y being either
  1392. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1393. Y when leaving the state if and only if the *target* state does not
  1394. define X. You may omit any of the fast-selection key or X or /Y,
  1395. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1396. For backward compatibility, this variable may also be just a list
  1397. of keywords - in this case the interpretation (sequence or type) will be
  1398. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1399. :group 'org-todo
  1400. :group 'org-keywords
  1401. :type '(choice
  1402. (repeat :tag "Old syntax, just keywords"
  1403. (string :tag "Keyword"))
  1404. (repeat :tag "New syntax"
  1405. (cons
  1406. (choice
  1407. :tag "Interpretation"
  1408. ;;Quick and dirty way to see
  1409. ;;`org-todo-interpretations'. This takes the
  1410. ;;place of item arguments
  1411. :convert-widget
  1412. (lambda (widget)
  1413. (widget-put widget
  1414. :args (mapcar
  1415. #'(lambda (x)
  1416. (widget-convert
  1417. (cons 'const x)))
  1418. org-todo-interpretation-widgets))
  1419. widget))
  1420. (repeat
  1421. (string :tag "Keyword"))))))
  1422. (defvar org-todo-keywords-1 nil
  1423. "All TODO and DONE keywords active in a buffer.")
  1424. (make-variable-buffer-local 'org-todo-keywords-1)
  1425. (defvar org-todo-keywords-for-agenda nil)
  1426. (defvar org-done-keywords-for-agenda nil)
  1427. (defvar org-todo-keyword-alist-for-agenda nil)
  1428. (defvar org-tag-alist-for-agenda nil)
  1429. (defvar org-agenda-contributing-files nil)
  1430. (defvar org-not-done-keywords nil)
  1431. (make-variable-buffer-local 'org-not-done-keywords)
  1432. (defvar org-done-keywords nil)
  1433. (make-variable-buffer-local 'org-done-keywords)
  1434. (defvar org-todo-heads nil)
  1435. (make-variable-buffer-local 'org-todo-heads)
  1436. (defvar org-todo-sets nil)
  1437. (make-variable-buffer-local 'org-todo-sets)
  1438. (defvar org-todo-log-states nil)
  1439. (make-variable-buffer-local 'org-todo-log-states)
  1440. (defvar org-todo-kwd-alist nil)
  1441. (make-variable-buffer-local 'org-todo-kwd-alist)
  1442. (defvar org-todo-key-alist nil)
  1443. (make-variable-buffer-local 'org-todo-key-alist)
  1444. (defvar org-todo-key-trigger nil)
  1445. (make-variable-buffer-local 'org-todo-key-trigger)
  1446. (defcustom org-todo-interpretation 'sequence
  1447. "Controls how TODO keywords are interpreted.
  1448. This variable is in principle obsolete and is only used for
  1449. backward compatibility, if the interpretation of todo keywords is
  1450. not given already in `org-todo-keywords'. See that variable for
  1451. more information."
  1452. :group 'org-todo
  1453. :group 'org-keywords
  1454. :type '(choice (const sequence)
  1455. (const type)))
  1456. (defcustom org-use-fast-todo-selection t
  1457. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1458. This variable describes if and under what circumstances the cycling
  1459. mechanism for TODO keywords will be replaced by a single-key, direct
  1460. selection scheme.
  1461. When nil, fast selection is never used.
  1462. When the symbol `prefix', it will be used when `org-todo' is called with
  1463. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1464. in an agenda buffer.
  1465. When t, fast selection is used by default. In this case, the prefix
  1466. argument forces cycling instead.
  1467. In all cases, the special interface is only used if access keys have actually
  1468. been assigned by the user, i.e. if keywords in the configuration are followed
  1469. by a letter in parenthesis, like TODO(t)."
  1470. :group 'org-todo
  1471. :type '(choice
  1472. (const :tag "Never" nil)
  1473. (const :tag "By default" t)
  1474. (const :tag "Only with C-u C-c C-t" prefix)))
  1475. (defcustom org-provide-todo-statistics t
  1476. "Non-nil means, update todo statistics after insert and toggle.
  1477. When this is set, todo statistics is updated in the parent of the current
  1478. entry each time a todo state is changed."
  1479. :group 'org-todo
  1480. :type 'boolean)
  1481. (defcustom org-after-todo-state-change-hook nil
  1482. "Hook which is run after the state of a TODO item was changed.
  1483. The new state (a string with a TODO keyword, or nil) is available in the
  1484. Lisp variable `state'."
  1485. :group 'org-todo
  1486. :type 'hook)
  1487. (defvar org-blocker-hook nil
  1488. "Hook for functions that are allowed to block a state change.
  1489. Each function gets as its single argument a property list, see
  1490. `org-trigger-hook' for more information about this list.
  1491. If any of the functions in this hook returns nil, the state change
  1492. is blocked.")
  1493. (defvar org-trigger-hook nil
  1494. "Hook for functions that are triggered by a state change.
  1495. Each function gets as its single argument a property list with at least
  1496. the following elements:
  1497. (:type type-of-change :position pos-at-entry-start
  1498. :from old-state :to new-state)
  1499. Depending on the type, more properties may be present.
  1500. This mechanism is currently implemented for:
  1501. TODO state changes
  1502. ------------------
  1503. :type todo-state-change
  1504. :from previous state (keyword as a string), or nil, or a symbol
  1505. 'todo' or 'done', to indicate the general type of state.
  1506. :to new state, like in :from")
  1507. (defcustom org-enforce-todo-dependencies nil
  1508. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1509. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1510. be blocked if any prior sibling is not yet done.
  1511. Finally, if the parent is blocked because of ordered siblings of its own,
  1512. the child will also be blocked.
  1513. This variable needs to be set before org.el is loaded, and you need to
  1514. restart Emacs after a change to make the change effective. The only way
  1515. to change is while Emacs is running is through the customize interface."
  1516. :set (lambda (var val)
  1517. (set var val)
  1518. (if val
  1519. (add-hook 'org-blocker-hook
  1520. 'org-block-todo-from-children-or-siblings-or-parent)
  1521. (remove-hook 'org-blocker-hook
  1522. 'org-block-todo-from-children-or-siblings-or-parent)))
  1523. :group 'org-todo
  1524. :type 'boolean)
  1525. (defcustom org-enforce-todo-checkbox-dependencies nil
  1526. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1527. When this is nil, checkboxes have no influence on switching TODO states.
  1528. When non-nil, you first need to check off all check boxes before the TODO
  1529. entry can be switched to DONE.
  1530. This variable needs to be set before org.el is loaded, and you need to
  1531. restart Emacs after a change to make the change effective. The only way
  1532. to change is while Emacs is running is through the customize interface."
  1533. :set (lambda (var val)
  1534. (set var val)
  1535. (if val
  1536. (add-hook 'org-blocker-hook
  1537. 'org-block-todo-from-checkboxes)
  1538. (remove-hook 'org-blocker-hook
  1539. 'org-block-todo-from-checkboxes)))
  1540. :group 'org-todo
  1541. :type 'boolean)
  1542. (defcustom org-todo-state-tags-triggers nil
  1543. "Tag changes that should be triggered by TODO state changes.
  1544. This is a list. Each entry is
  1545. (state-change (tag . flag) .......)
  1546. State-change can be a string with a state, and empty string to indicate the
  1547. state that has no TODO keyword, or it can be one of the symbols `todo'
  1548. or `done', meaning any not-done or done state, respectively."
  1549. :group 'org-todo
  1550. :group 'org-tags
  1551. :type '(repeat
  1552. (cons (choice :tag "When changing to"
  1553. (const :tag "Not-done state" todo)
  1554. (const :tag "Done state" done)
  1555. (string :tag "State"))
  1556. (repeat
  1557. (cons :tag "Tag action"
  1558. (string :tag "Tag")
  1559. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1560. (defcustom org-log-done nil
  1561. "Information to record when a task moves to the DONE state.
  1562. Possible values are:
  1563. nil Don't add anything, just change the keyword
  1564. time Add a time stamp to the task
  1565. note Prompt a closing note and add it with template `org-log-note-headings'
  1566. This option can also be set with on a per-file-basis with
  1567. #+STARTUP: nologdone
  1568. #+STARTUP: logdone
  1569. #+STARTUP: lognotedone
  1570. You can have local logging settings for a subtree by setting the LOGGING
  1571. property to one or more of these keywords."
  1572. :group 'org-todo
  1573. :group 'org-progress
  1574. :type '(choice
  1575. (const :tag "No logging" nil)
  1576. (const :tag "Record CLOSED timestamp" time)
  1577. (const :tag "Record CLOSED timestamp with closing note." note)))
  1578. ;; Normalize old uses of org-log-done.
  1579. (cond
  1580. ((eq org-log-done t) (setq org-log-done 'time))
  1581. ((and (listp org-log-done) (memq 'done org-log-done))
  1582. (setq org-log-done 'note)))
  1583. (defcustom org-log-note-clock-out nil
  1584. "Non-nil means, record a note when clocking out of an item.
  1585. This can also be configured on a per-file basis by adding one of
  1586. the following lines anywhere in the buffer:
  1587. #+STARTUP: lognoteclock-out
  1588. #+STARTUP: nolognoteclock-out"
  1589. :group 'org-todo
  1590. :group 'org-progress
  1591. :type 'boolean)
  1592. (defcustom org-log-done-with-time t
  1593. "Non-nil means, the CLOSED time stamp will contain date and time.
  1594. When nil, only the date will be recorded."
  1595. :group 'org-progress
  1596. :type 'boolean)
  1597. (defcustom org-log-note-headings
  1598. '((done . "CLOSING NOTE %t")
  1599. (state . "State %-12s from %-12S %t")
  1600. (note . "Note taken on %t")
  1601. (clock-out . ""))
  1602. "Headings for notes added to entries.
  1603. The value is an alist, with the car being a symbol indicating the note
  1604. context, and the cdr is the heading to be used. The heading may also be the
  1605. empty string.
  1606. %t in the heading will be replaced by a time stamp.
  1607. %s will be replaced by the new TODO state, in double quotes.
  1608. %S will be replaced by the old TODO state, in double quotes.
  1609. %u will be replaced by the user name.
  1610. %U will be replaced by the full user name."
  1611. :group 'org-todo
  1612. :group 'org-progress
  1613. :type '(list :greedy t
  1614. (cons (const :tag "Heading when closing an item" done) string)
  1615. (cons (const :tag
  1616. "Heading when changing todo state (todo sequence only)"
  1617. state) string)
  1618. (cons (const :tag "Heading when just taking a note" note) string)
  1619. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1620. (unless (assq 'note org-log-note-headings)
  1621. (push '(note . "%t") org-log-note-headings))
  1622. (defcustom org-log-into-drawer nil
  1623. "Non-nil means, insert state change notes and time stamps into a drawer.
  1624. When nil, state changes notes will be inserted after the headline and
  1625. any scheduling and clock lines, but not inside a drawer.
  1626. The value of this variable should be the name of the drawer to use.
  1627. LOGBOOK is proposed at the default drawer for this purpose, you can
  1628. also set this to a string to define the drawer of your choice.
  1629. A value of t is also allowed, representing \"LOGBOOK\".
  1630. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1631. will be ignored."
  1632. :group 'org-todo
  1633. :group 'org-progress
  1634. :type '(choice
  1635. (const :tag "Not into a drawer" nil)
  1636. (const :tag "LOGBOOK" t)
  1637. (string :tag "Other")))
  1638. (if (fboundp 'defvaralias)
  1639. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1640. (defcustom org-log-state-notes-insert-after-drawers nil
  1641. "Non-nil means, insert state change notes after any drawers in entry.
  1642. Only the drawers that *immediately* follow the headline and the
  1643. deadline/scheduled line are skipped.
  1644. When nil, insert notes right after the heading and perhaps the line
  1645. with deadline/scheduling if present.
  1646. This variable will have no effect if `org-log-into-drawer' is
  1647. set."
  1648. :group 'org-todo
  1649. :group 'org-progress
  1650. :type 'boolean)
  1651. (defcustom org-log-states-order-reversed t
  1652. "Non-nil means, the latest state change note will be directly after heading.
  1653. When nil, the notes will be orderer according to time."
  1654. :group 'org-todo
  1655. :group 'org-progress
  1656. :type 'boolean)
  1657. (defcustom org-log-repeat 'time
  1658. "Non-nil means, record moving through the DONE state when triggering repeat.
  1659. An auto-repeating tasks is immediately switched back to TODO when marked
  1660. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1661. the TODO keyword definition, or recording a closing note by setting
  1662. `org-log-done', there will be no record of the task moving through DONE.
  1663. This variable forces taking a note anyway. Possible values are:
  1664. nil Don't force a record
  1665. time Record a time stamp
  1666. note Record a note
  1667. This option can also be set with on a per-file-basis with
  1668. #+STARTUP: logrepeat
  1669. #+STARTUP: lognoterepeat
  1670. #+STARTUP: nologrepeat
  1671. You can have local logging settings for a subtree by setting the LOGGING
  1672. property to one or more of these keywords."
  1673. :group 'org-todo
  1674. :group 'org-progress
  1675. :type '(choice
  1676. (const :tag "Don't force a record" nil)
  1677. (const :tag "Force recording the DONE state" time)
  1678. (const :tag "Force recording a note with the DONE state" note)))
  1679. (defgroup org-priorities nil
  1680. "Priorities in Org-mode."
  1681. :tag "Org Priorities"
  1682. :group 'org-todo)
  1683. (defcustom org-highest-priority ?A
  1684. "The highest priority of TODO items. A character like ?A, ?B etc.
  1685. Must have a smaller ASCII number than `org-lowest-priority'."
  1686. :group 'org-priorities
  1687. :type 'character)
  1688. (defcustom org-lowest-priority ?C
  1689. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1690. Must have a larger ASCII number than `org-highest-priority'."
  1691. :group 'org-priorities
  1692. :type 'character)
  1693. (defcustom org-default-priority ?B
  1694. "The default priority of TODO items.
  1695. This is the priority an item get if no explicit priority is given."
  1696. :group 'org-priorities
  1697. :type 'character)
  1698. (defcustom org-priority-start-cycle-with-default t
  1699. "Non-nil means, start with default priority when starting to cycle.
  1700. When this is nil, the first step in the cycle will be (depending on the
  1701. command used) one higher or lower that the default priority."
  1702. :group 'org-priorities
  1703. :type 'boolean)
  1704. (defgroup org-time nil
  1705. "Options concerning time stamps and deadlines in Org-mode."
  1706. :tag "Org Time"
  1707. :group 'org)
  1708. (defcustom org-insert-labeled-timestamps-at-point nil
  1709. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1710. When nil, these labeled time stamps are forces into the second line of an
  1711. entry, just after the headline. When scheduling from the global TODO list,
  1712. the time stamp will always be forced into the second line."
  1713. :group 'org-time
  1714. :type 'boolean)
  1715. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1716. "Formats for `format-time-string' which are used for time stamps.
  1717. It is not recommended to change this constant.")
  1718. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1719. "Number of minutes to round time stamps to.
  1720. These are two values, the first applies when first creating a time stamp.
  1721. The second applies when changing it with the commands `S-up' and `S-down'.
  1722. When changing the time stamp, this means that it will change in steps
  1723. of N minutes, as given by the second value.
  1724. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1725. numbers should be factors of 60, so for example 5, 10, 15.
  1726. When this is larger than 1, you can still force an exact time-stamp by using
  1727. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1728. and by using a prefix arg to `S-up/down' to specify the exact number
  1729. of minutes to shift."
  1730. :group 'org-time
  1731. :get '(lambda (var) ; Make sure all entries have 5 elements
  1732. (if (integerp (default-value var))
  1733. (list (default-value var) 5)
  1734. (default-value var)))
  1735. :type '(list
  1736. (integer :tag "when inserting times")
  1737. (integer :tag "when modifying times")))
  1738. ;; Normalize old customizations of this variable.
  1739. (when (integerp org-time-stamp-rounding-minutes)
  1740. (setq org-time-stamp-rounding-minutes
  1741. (list org-time-stamp-rounding-minutes
  1742. org-time-stamp-rounding-minutes)))
  1743. (defcustom org-display-custom-times nil
  1744. "Non-nil means, overlay custom formats over all time stamps.
  1745. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1746. To turn this on on a per-file basis, insert anywhere in the file:
  1747. #+STARTUP: customtime"
  1748. :group 'org-time
  1749. :set 'set-default
  1750. :type 'sexp)
  1751. (make-variable-buffer-local 'org-display-custom-times)
  1752. (defcustom org-time-stamp-custom-formats
  1753. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1754. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1755. These are overlayed over the default ISO format if the variable
  1756. `org-display-custom-times' is set. Time like %H:%M should be at the
  1757. end of the second format."
  1758. :group 'org-time
  1759. :type 'sexp)
  1760. (defun org-time-stamp-format (&optional long inactive)
  1761. "Get the right format for a time string."
  1762. (let ((f (if long (cdr org-time-stamp-formats)
  1763. (car org-time-stamp-formats))))
  1764. (if inactive
  1765. (concat "[" (substring f 1 -1) "]")
  1766. f)))
  1767. (defcustom org-time-clocksum-format "%d:%02d"
  1768. "The format string used when creating CLOCKSUM lines, or when
  1769. org-mode generates a time duration."
  1770. :group 'org-time
  1771. :type 'string)
  1772. (defcustom org-deadline-warning-days 14
  1773. "No. of days before expiration during which a deadline becomes active.
  1774. This variable governs the display in sparse trees and in the agenda.
  1775. When 0 or negative, it means use this number (the absolute value of it)
  1776. even if a deadline has a different individual lead time specified.
  1777. Custom commands can set this variable in the options section."
  1778. :group 'org-time
  1779. :group 'org-agenda-daily/weekly
  1780. :type 'integer)
  1781. (defcustom org-read-date-prefer-future t
  1782. "Non-nil means, assume future for incomplete date input from user.
  1783. This affects the following situations:
  1784. 1. The user gives a day, but no month.
  1785. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1786. read this as the third of *next* month. However, if you enter \"17\",
  1787. it will be considered as *this* month.
  1788. 2. The user gives a month but not a year.
  1789. For example, if it is april and you enter \"feb 2\", this will be read
  1790. as feb 2, *next* year. \"May 5\", however, will be this year.
  1791. Currently this does not work for ISO week specifications.
  1792. When this option is nil, the current month and year will always be used
  1793. as defaults."
  1794. :group 'org-time
  1795. :type 'boolean)
  1796. (defcustom org-read-date-display-live t
  1797. "Non-nil means, display current interpretation of date prompt live.
  1798. This display will be in an overlay, in the minibuffer."
  1799. :group 'org-time
  1800. :type 'boolean)
  1801. (defcustom org-read-date-popup-calendar t
  1802. "Non-nil means, pop up a calendar when prompting for a date.
  1803. In the calendar, the date can be selected with mouse-1. However, the
  1804. minibuffer will also be active, and you can simply enter the date as well.
  1805. When nil, only the minibuffer will be available."
  1806. :group 'org-time
  1807. :type 'boolean)
  1808. (if (fboundp 'defvaralias)
  1809. (defvaralias 'org-popup-calendar-for-date-prompt
  1810. 'org-read-date-popup-calendar))
  1811. (defcustom org-read-date-minibuffer-setup-hook nil
  1812. "Hook to be used to set up keys for the date/time interface.
  1813. Add key definitions to `minibuffer-local-map', which will be a temporary
  1814. copy."
  1815. :group 'org-time
  1816. :type 'hook)
  1817. (defcustom org-extend-today-until 0
  1818. "The hour when your day really ends. Must be an integer.
  1819. This has influence for the following applications:
  1820. - When switching the agenda to \"today\". It it is still earlier than
  1821. the time given here, the day recognized as TODAY is actually yesterday.
  1822. - When a date is read from the user and it is still before the time given
  1823. here, the current date and time will be assumed to be yesterday, 23:59.
  1824. Also, timestamps inserted in remember templates follow this rule.
  1825. IMPORTANT: This is a feature whose implementation is and likely will
  1826. remain incomplete. Really, it is only here because past midnight seems to
  1827. be the favorite working time of John Wiegley :-)"
  1828. :group 'org-time
  1829. :type 'integer)
  1830. (defcustom org-edit-timestamp-down-means-later nil
  1831. "Non-nil means, S-down will increase the time in a time stamp.
  1832. When nil, S-up will increase."
  1833. :group 'org-time
  1834. :type 'boolean)
  1835. (defcustom org-calendar-follow-timestamp-change t
  1836. "Non-nil means, make the calendar window follow timestamp changes.
  1837. When a timestamp is modified and the calendar window is visible, it will be
  1838. moved to the new date."
  1839. :group 'org-time
  1840. :type 'boolean)
  1841. (defgroup org-tags nil
  1842. "Options concerning tags in Org-mode."
  1843. :tag "Org Tags"
  1844. :group 'org)
  1845. (defcustom org-tag-alist nil
  1846. "List of tags allowed in Org-mode files.
  1847. When this list is nil, Org-mode will base TAG input on what is already in the
  1848. buffer.
  1849. The value of this variable is an alist, the car of each entry must be a
  1850. keyword as a string, the cdr may be a character that is used to select
  1851. that tag through the fast-tag-selection interface.
  1852. See the manual for details."
  1853. :group 'org-tags
  1854. :type '(repeat
  1855. (choice
  1856. (cons (string :tag "Tag name")
  1857. (character :tag "Access char"))
  1858. (const :tag "Start radio group" (:startgroup))
  1859. (const :tag "End radio group" (:endgroup))
  1860. (const :tag "New line" (:newline)))))
  1861. (defcustom org-tag-persistent-alist nil
  1862. "List of tags that will always appear in all Org-mode files.
  1863. This is in addition to any in buffer settings or customizations
  1864. of `org-tag-alist'.
  1865. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1866. The value of this variable is an alist, the car of each entry must be a
  1867. keyword as a string, the cdr may be a character that is used to select
  1868. that tag through the fast-tag-selection interface.
  1869. See the manual for details.
  1870. To disable these tags on a per-file basis, insert anywhere in the file:
  1871. #+STARTUP: noptag"
  1872. :group 'org-tags
  1873. :type '(repeat
  1874. (choice
  1875. (cons (string :tag "Tag name")
  1876. (character :tag "Access char"))
  1877. (const :tag "Start radio group" (:startgroup))
  1878. (const :tag "End radio group" (:endgroup))
  1879. (const :tag "New line" (:newline)))))
  1880. (defvar org-file-tags nil
  1881. "List of tags that can be inherited by all entries in the file.
  1882. The tags will be inherited if the variable `org-use-tag-inheritance'
  1883. says they should be.
  1884. This variable is populated from #+TAG lines.")
  1885. (defcustom org-use-fast-tag-selection 'auto
  1886. "Non-nil means, use fast tag selection scheme.
  1887. This is a special interface to select and deselect tags with single keys.
  1888. When nil, fast selection is never used.
  1889. When the symbol `auto', fast selection is used if and only if selection
  1890. characters for tags have been configured, either through the variable
  1891. `org-tag-alist' or through a #+TAGS line in the buffer.
  1892. When t, fast selection is always used and selection keys are assigned
  1893. automatically if necessary."
  1894. :group 'org-tags
  1895. :type '(choice
  1896. (const :tag "Always" t)
  1897. (const :tag "Never" nil)
  1898. (const :tag "When selection characters are configured" 'auto)))
  1899. (defcustom org-fast-tag-selection-single-key nil
  1900. "Non-nil means, fast tag selection exits after first change.
  1901. When nil, you have to press RET to exit it.
  1902. During fast tag selection, you can toggle this flag with `C-c'.
  1903. This variable can also have the value `expert'. In this case, the window
  1904. displaying the tags menu is not even shown, until you press C-c again."
  1905. :group 'org-tags
  1906. :type '(choice
  1907. (const :tag "No" nil)
  1908. (const :tag "Yes" t)
  1909. (const :tag "Expert" expert)))
  1910. (defvar org-fast-tag-selection-include-todo nil
  1911. "Non-nil means, fast tags selection interface will also offer TODO states.
  1912. This is an undocumented feature, you should not rely on it.")
  1913. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1914. "The column to which tags should be indented in a headline.
  1915. If this number is positive, it specifies the column. If it is negative,
  1916. it means that the tags should be flushright to that column. For example,
  1917. -80 works well for a normal 80 character screen."
  1918. :group 'org-tags
  1919. :type 'integer)
  1920. (defcustom org-auto-align-tags t
  1921. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1922. These operations change the length of a headline and therefore shift
  1923. the tags around. With this options turned on, after each such operation
  1924. the tags are again aligned to `org-tags-column'."
  1925. :group 'org-tags
  1926. :type 'boolean)
  1927. (defcustom org-use-tag-inheritance t
  1928. "Non-nil means, tags in levels apply also for sublevels.
  1929. When nil, only the tags directly given in a specific line apply there.
  1930. This may also be a list of tags that should be inherited, or a regexp that
  1931. matches tags that should be inherited. Additional control is possible
  1932. with the variable `org-tags-exclude-from-inheritance' which gives an
  1933. explicit list of tags to be excluded from inheritance., even if the value of
  1934. `org-use-tag-inheritance' would select it for inheritance.
  1935. If this option is t, a match early-on in a tree can lead to a large
  1936. number of matches in the subtree when constructing the agenda or creating
  1937. a sparse tree. If you only want to see the first match in a tree during
  1938. a search, check out the variable `org-tags-match-list-sublevels'."
  1939. :group 'org-tags
  1940. :type '(choice
  1941. (const :tag "Not" nil)
  1942. (const :tag "Always" t)
  1943. (repeat :tag "Specific tags" (string :tag "Tag"))
  1944. (regexp :tag "Tags matched by regexp")))
  1945. (defcustom org-tags-exclude-from-inheritance nil
  1946. "List of tags that should never be inherited.
  1947. This is a way to exclude a few tags from inheritance. For way to do
  1948. the opposite, to actively allow inheritance for selected tags,
  1949. see the variable `org-use-tag-inheritance'."
  1950. :group 'org-tags
  1951. :type '(repeat (string :tag "Tag")))
  1952. (defun org-tag-inherit-p (tag)
  1953. "Check if TAG is one that should be inherited."
  1954. (cond
  1955. ((member tag org-tags-exclude-from-inheritance) nil)
  1956. ((eq org-use-tag-inheritance t) t)
  1957. ((not org-use-tag-inheritance) nil)
  1958. ((stringp org-use-tag-inheritance)
  1959. (string-match org-use-tag-inheritance tag))
  1960. ((listp org-use-tag-inheritance)
  1961. (member tag org-use-tag-inheritance))
  1962. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1963. (defcustom org-tags-match-list-sublevels t
  1964. "Non-nil means list also sublevels of headlines matching a search.
  1965. This variable applies to tags/property searches, and also to stuck
  1966. projects because this search is based on a tags match as well.
  1967. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1968. the sublevels of a headline matching a tag search often also match
  1969. the same search. Listing all of them can create very long lists.
  1970. Setting this variable to nil causes subtrees of a match to be skipped.
  1971. As a special case, if the tag search is restricted to TODO items, the
  1972. value of this variable is ignored and sublevels are always checked, to
  1973. make sure all corresponding TODO items find their way into the list.
  1974. This variable is semi-obsolete and probably should always be true. It
  1975. is better to limit inheritance to certain tags using the variables
  1976. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1977. :group 'org-tags
  1978. :type '(choice
  1979. (const :tag "No, don't list them" nil)
  1980. (const :tag "Yes, do list them" t)
  1981. (const :tag "List them, indented with leading dots" indented)))
  1982. (defvar org-tags-history nil
  1983. "History of minibuffer reads for tags.")
  1984. (defvar org-last-tags-completion-table nil
  1985. "The last used completion table for tags.")
  1986. (defvar org-after-tags-change-hook nil
  1987. "Hook that is run after the tags in a line have changed.")
  1988. (defgroup org-properties nil
  1989. "Options concerning properties in Org-mode."
  1990. :tag "Org Properties"
  1991. :group 'org)
  1992. (defcustom org-property-format "%-10s %s"
  1993. "How property key/value pairs should be formatted by `indent-line'.
  1994. When `indent-line' hits a property definition, it will format the line
  1995. according to this format, mainly to make sure that the values are
  1996. lined-up with respect to each other."
  1997. :group 'org-properties
  1998. :type 'string)
  1999. (defcustom org-use-property-inheritance nil
  2000. "Non-nil means, properties apply also for sublevels.
  2001. This setting is chiefly used during property searches. Turning it on can
  2002. cause significant overhead when doing a search, which is why it is not
  2003. on by default.
  2004. When nil, only the properties directly given in the current entry count.
  2005. When t, every property is inherited. The value may also be a list of
  2006. properties that should have inheritance, or a regular expression matching
  2007. properties that should be inherited.
  2008. However, note that some special properties use inheritance under special
  2009. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2010. and the properties ending in \"_ALL\" when they are used as descriptor
  2011. for valid values of a property.
  2012. Note for programmers:
  2013. When querying an entry with `org-entry-get', you can control if inheritance
  2014. should be used. By default, `org-entry-get' looks only at the local
  2015. properties. You can request inheritance by setting the inherit argument
  2016. to t (to force inheritance) or to `selective' (to respect the setting
  2017. in this variable)."
  2018. :group 'org-properties
  2019. :type '(choice
  2020. (const :tag "Not" nil)
  2021. (const :tag "Always" t)
  2022. (repeat :tag "Specific properties" (string :tag "Property"))
  2023. (regexp :tag "Properties matched by regexp")))
  2024. (defun org-property-inherit-p (property)
  2025. "Check if PROPERTY is one that should be inherited."
  2026. (cond
  2027. ((eq org-use-property-inheritance t) t)
  2028. ((not org-use-property-inheritance) nil)
  2029. ((stringp org-use-property-inheritance)
  2030. (string-match org-use-property-inheritance property))
  2031. ((listp org-use-property-inheritance)
  2032. (member property org-use-property-inheritance))
  2033. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2034. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2035. "The default column format, if no other format has been defined.
  2036. This variable can be set on the per-file basis by inserting a line
  2037. #+COLUMNS: %25ITEM ....."
  2038. :group 'org-properties
  2039. :type 'string)
  2040. (defcustom org-columns-ellipses ".."
  2041. "The ellipses to be used when a field in column view is truncated.
  2042. When this is the empty string, as many characters as possible are shown,
  2043. but then there will be no visual indication that the field has been truncated.
  2044. When this is a string of length N, the last N characters of a truncated
  2045. field are replaced by this string. If the column is narrower than the
  2046. ellipses string, only part of the ellipses string will be shown."
  2047. :group 'org-properties
  2048. :type 'string)
  2049. (defcustom org-columns-modify-value-for-display-function nil
  2050. "Function that modifies values for display in column view.
  2051. For example, it can be used to cut out a certain part from a time stamp.
  2052. The function must take 2 arguments:
  2053. column-title The title of the column (*not* the property name)
  2054. value The value that should be modified.
  2055. The function should return the value that should be displayed,
  2056. or nil if the normal value should be used."
  2057. :group 'org-properties
  2058. :type 'function)
  2059. (defcustom org-effort-property "Effort"
  2060. "The property that is being used to keep track of effort estimates.
  2061. Effort estimates given in this property need to have the format H:MM."
  2062. :group 'org-properties
  2063. :group 'org-progress
  2064. :type '(string :tag "Property"))
  2065. (defconst org-global-properties-fixed
  2066. '(("VISIBILITY_ALL" . "folded children content all"))
  2067. "List of property/value pairs that can be inherited by any entry.
  2068. These are fixed values, for the preset properties. The user variable
  2069. that can be used to add to this list is `org-global-properties'.
  2070. The entries in this list are cons cells where the car is a property
  2071. name and cdr is a string with the value. If the value represents
  2072. multiple items like an \"_ALL\" property, separate the items by
  2073. spaces.")
  2074. (defcustom org-global-properties nil
  2075. "List of property/value pairs that can be inherited by any entry.
  2076. This list will be combined with the constant `org-global-properties-fixed'.
  2077. The entries in this list are cons cells where the car is a property
  2078. name and cdr is a string with the value.
  2079. You can set buffer-local values for the same purpose in the variable
  2080. `org-file-properties' this by adding lines like
  2081. #+PROPERTY: NAME VALUE"
  2082. :group 'org-properties
  2083. :type '(repeat
  2084. (cons (string :tag "Property")
  2085. (string :tag "Value"))))
  2086. (defvar org-file-properties nil
  2087. "List of property/value pairs that can be inherited by any entry.
  2088. Valid for the current buffer.
  2089. This variable is populated from #+PROPERTY lines.")
  2090. (make-variable-buffer-local 'org-file-properties)
  2091. (defgroup org-agenda nil
  2092. "Options concerning agenda views in Org-mode."
  2093. :tag "Org Agenda"
  2094. :group 'org)
  2095. (defvar org-category nil
  2096. "Variable used by org files to set a category for agenda display.
  2097. Such files should use a file variable to set it, for example
  2098. # -*- mode: org; org-category: \"ELisp\"
  2099. or contain a special line
  2100. #+CATEGORY: ELisp
  2101. If the file does not specify a category, then file's base name
  2102. is used instead.")
  2103. (make-variable-buffer-local 'org-category)
  2104. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2105. (defcustom org-agenda-files nil
  2106. "The files to be used for agenda display.
  2107. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2108. \\[org-remove-file]. You can also use customize to edit the list.
  2109. If an entry is a directory, all files in that directory that are matched by
  2110. `org-agenda-file-regexp' will be part of the file list.
  2111. If the value of the variable is not a list but a single file name, then
  2112. the list of agenda files is actually stored and maintained in that file, one
  2113. agenda file per line."
  2114. :group 'org-agenda
  2115. :type '(choice
  2116. (repeat :tag "List of files and directories" file)
  2117. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2118. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2119. "Regular expression to match files for `org-agenda-files'.
  2120. If any element in the list in that variable contains a directory instead
  2121. of a normal file, all files in that directory that are matched by this
  2122. regular expression will be included."
  2123. :group 'org-agenda
  2124. :type 'regexp)
  2125. (defcustom org-agenda-text-search-extra-files nil
  2126. "List of extra files to be searched by text search commands.
  2127. These files will be search in addition to the agenda files by the
  2128. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2129. Note that these files will only be searched for text search commands,
  2130. not for the other agenda views like todo lists, tag searches or the weekly
  2131. agenda. This variable is intended to list notes and possibly archive files
  2132. that should also be searched by these two commands.
  2133. In fact, if the first element in the list is the symbol `agenda-archives',
  2134. than all archive files of all agenda files will be added to the search
  2135. scope."
  2136. :group 'org-agenda
  2137. :type '(set :greedy t
  2138. (const :tag "Agenda Archives" agenda-archives)
  2139. (repeat :inline t (file))))
  2140. (if (fboundp 'defvaralias)
  2141. (defvaralias 'org-agenda-multi-occur-extra-files
  2142. 'org-agenda-text-search-extra-files))
  2143. (defcustom org-agenda-skip-unavailable-files nil
  2144. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2145. A nil value means to remove them, after a query, from the list."
  2146. :group 'org-agenda
  2147. :type 'boolean)
  2148. (defcustom org-calendar-to-agenda-key [?c]
  2149. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2150. The command `org-calendar-goto-agenda' will be bound to this key. The
  2151. default is the character `c' because then `c' can be used to switch back and
  2152. forth between agenda and calendar."
  2153. :group 'org-agenda
  2154. :type 'sexp)
  2155. (defcustom org-calendar-agenda-action-key [?k]
  2156. "The key to be installed in `calendar-mode-map' for agenda-action.
  2157. The command `org-agenda-action' will be bound to this key. The
  2158. default is the character `k' because we use the same key in the agenda."
  2159. :group 'org-agenda
  2160. :type 'sexp)
  2161. (eval-after-load "calendar"
  2162. '(progn
  2163. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2164. 'org-calendar-goto-agenda)
  2165. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2166. 'org-agenda-action)))
  2167. (defgroup org-latex nil
  2168. "Options for embedding LaTeX code into Org-mode."
  2169. :tag "Org LaTeX"
  2170. :group 'org)
  2171. (defcustom org-format-latex-options
  2172. '(:foreground default :background default :scale 1.0
  2173. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2174. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2175. "Options for creating images from LaTeX fragments.
  2176. This is a property list with the following properties:
  2177. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2178. `default' means use the foreground of the default face.
  2179. :background the background color, or \"Transparent\".
  2180. `default' means use the background of the default face.
  2181. :scale a scaling factor for the size of the images.
  2182. :html-foreground, :html-background, :html-scale
  2183. the same numbers for HTML export.
  2184. :matchers a list indicating which matchers should be used to
  2185. find LaTeX fragments. Valid members of this list are:
  2186. \"begin\" find environments
  2187. \"$1\" find single characters surrounded by $.$
  2188. \"$\" find math expressions surrounded by $...$
  2189. \"$$\" find math expressions surrounded by $$....$$
  2190. \"\\(\" find math expressions surrounded by \\(...\\)
  2191. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2192. :group 'org-latex
  2193. :type 'plist)
  2194. (defcustom org-format-latex-header "\\documentclass{article}
  2195. \\usepackage{fullpage} % do not remove
  2196. \\usepackage{amssymb}
  2197. \\usepackage[usenames]{color}
  2198. \\usepackage{amsmath}
  2199. \\usepackage{latexsym}
  2200. \\usepackage[mathscr]{eucal}
  2201. \\pagestyle{empty} % do not remove"
  2202. "The document header used for processing LaTeX fragments."
  2203. :group 'org-latex
  2204. :type 'string)
  2205. (defgroup org-font-lock nil
  2206. "Font-lock settings for highlighting in Org-mode."
  2207. :tag "Org Font Lock"
  2208. :group 'org)
  2209. (defcustom org-level-color-stars-only nil
  2210. "Non-nil means fontify only the stars in each headline.
  2211. When nil, the entire headline is fontified.
  2212. Changing it requires restart of `font-lock-mode' to become effective
  2213. also in regions already fontified."
  2214. :group 'org-font-lock
  2215. :type 'boolean)
  2216. (defcustom org-hide-leading-stars nil
  2217. "Non-nil means, hide the first N-1 stars in a headline.
  2218. This works by using the face `org-hide' for these stars. This
  2219. face is white for a light background, and black for a dark
  2220. background. You may have to customize the face `org-hide' to
  2221. make this work.
  2222. Changing it requires restart of `font-lock-mode' to become effective
  2223. also in regions already fontified.
  2224. You may also set this on a per-file basis by adding one of the following
  2225. lines to the buffer:
  2226. #+STARTUP: hidestars
  2227. #+STARTUP: showstars"
  2228. :group 'org-font-lock
  2229. :type 'boolean)
  2230. (defcustom org-fontify-done-headline nil
  2231. "Non-nil means, change the face of a headline if it is marked DONE.
  2232. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2233. When this is non-nil, the headline after the keyword is set to the
  2234. `org-headline-done' as an additional indication."
  2235. :group 'org-font-lock
  2236. :type 'boolean)
  2237. (defcustom org-fontify-emphasized-text t
  2238. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2239. Changing this variable requires a restart of Emacs to take effect."
  2240. :group 'org-font-lock
  2241. :type 'boolean)
  2242. (defcustom org-highlight-latex-fragments-and-specials nil
  2243. "Non-nil means, fontify what is treated specially by the exporters."
  2244. :group 'org-font-lock
  2245. :type 'boolean)
  2246. (defcustom org-hide-emphasis-markers nil
  2247. "Non-nil mean font-lock should hide the emphasis marker characters."
  2248. :group 'org-font-lock
  2249. :type 'boolean)
  2250. (defvar org-emph-re nil
  2251. "Regular expression for matching emphasis.")
  2252. (defvar org-verbatim-re nil
  2253. "Regular expression for matching verbatim text.")
  2254. (defvar org-emphasis-regexp-components) ; defined just below
  2255. (defvar org-emphasis-alist) ; defined just below
  2256. (defun org-set-emph-re (var val)
  2257. "Set variable and compute the emphasis regular expression."
  2258. (set var val)
  2259. (when (and (boundp 'org-emphasis-alist)
  2260. (boundp 'org-emphasis-regexp-components)
  2261. org-emphasis-alist org-emphasis-regexp-components)
  2262. (let* ((e org-emphasis-regexp-components)
  2263. (pre (car e))
  2264. (post (nth 1 e))
  2265. (border (nth 2 e))
  2266. (body (nth 3 e))
  2267. (nl (nth 4 e))
  2268. (body1 (concat body "*?"))
  2269. (markers (mapconcat 'car org-emphasis-alist ""))
  2270. (vmarkers (mapconcat
  2271. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2272. org-emphasis-alist "")))
  2273. ;; make sure special characters appear at the right position in the class
  2274. (if (string-match "\\^" markers)
  2275. (setq markers (concat (replace-match "" t t markers) "^")))
  2276. (if (string-match "-" markers)
  2277. (setq markers (concat (replace-match "" t t markers) "-")))
  2278. (if (string-match "\\^" vmarkers)
  2279. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2280. (if (string-match "-" vmarkers)
  2281. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2282. (if (> nl 0)
  2283. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2284. (int-to-string nl) "\\}")))
  2285. ;; Make the regexp
  2286. (setq org-emph-re
  2287. (concat "\\([" pre "]\\|^\\)"
  2288. "\\("
  2289. "\\([" markers "]\\)"
  2290. "\\("
  2291. "[^" border "]\\|"
  2292. "[^" border "]"
  2293. body1
  2294. "[^" border "]"
  2295. "\\)"
  2296. "\\3\\)"
  2297. "\\([" post "]\\|$\\)"))
  2298. (setq org-verbatim-re
  2299. (concat "\\([" pre "]\\|^\\)"
  2300. "\\("
  2301. "\\([" vmarkers "]\\)"
  2302. "\\("
  2303. "[^" border "]\\|"
  2304. "[^" border "]"
  2305. body1
  2306. "[^" border "]"
  2307. "\\)"
  2308. "\\3\\)"
  2309. "\\([" post "]\\|$\\)")))))
  2310. (defcustom org-emphasis-regexp-components
  2311. '(" \t('\"{" "- \t.,:!?;'\")}" " \t\r\n,\"'" "." 1)
  2312. "Components used to build the regular expression for emphasis.
  2313. This is a list with 6 entries. Terminology: In an emphasis string
  2314. like \" *strong word* \", we call the initial space PREMATCH, the final
  2315. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2316. and \"trong wor\" is the body. The different components in this variable
  2317. specify what is allowed/forbidden in each part:
  2318. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2319. post Chars allowed as postmatch. End of line will be allowed too.
  2320. border The chars *forbidden* as border characters.
  2321. body-regexp A regexp like \".\" to match a body character. Don't use
  2322. non-shy groups here, and don't allow newline here.
  2323. newline The maximum number of newlines allowed in an emphasis exp.
  2324. Use customize to modify this, or restart Emacs after changing it."
  2325. :group 'org-font-lock
  2326. :set 'org-set-emph-re
  2327. :type '(list
  2328. (sexp :tag "Allowed chars in pre ")
  2329. (sexp :tag "Allowed chars in post ")
  2330. (sexp :tag "Forbidden chars in border ")
  2331. (sexp :tag "Regexp for body ")
  2332. (integer :tag "number of newlines allowed")
  2333. (option (boolean :tag "Please ignore this button"))))
  2334. (defcustom org-emphasis-alist
  2335. `(("*" bold "<b>" "</b>")
  2336. ("/" italic "<i>" "</i>")
  2337. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2338. ("=" org-code "<code>" "</code>" verbatim)
  2339. ("~" org-verbatim "<code>" "</code>" verbatim)
  2340. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2341. "<del>" "</del>")
  2342. )
  2343. "Special syntax for emphasized text.
  2344. Text starting and ending with a special character will be emphasized, for
  2345. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2346. characters, the face to be used by font-lock for highlighting in Org-mode
  2347. Emacs buffers, and the HTML tags to be used for this.
  2348. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2349. Use customize to modify this, or restart Emacs after changing it."
  2350. :group 'org-font-lock
  2351. :set 'org-set-emph-re
  2352. :type '(repeat
  2353. (list
  2354. (string :tag "Marker character")
  2355. (choice
  2356. (face :tag "Font-lock-face")
  2357. (plist :tag "Face property list"))
  2358. (string :tag "HTML start tag")
  2359. (string :tag "HTML end tag")
  2360. (option (const verbatim)))))
  2361. ;;; Miscellaneous options
  2362. (defgroup org-completion nil
  2363. "Completion in Org-mode."
  2364. :tag "Org Completion"
  2365. :group 'org)
  2366. (defcustom org-completion-use-ido nil
  2367. "Non-nil means, use ido completion wherever possible.
  2368. Note that `ido-mode' must be active for this variable to be relevant.
  2369. If you decide to turn this variable on, you might well want to turn off
  2370. `org-outline-path-complete-in-steps'."
  2371. :group 'org-completion
  2372. :type 'boolean)
  2373. (defcustom org-completion-fallback-command 'hippie-expand
  2374. "The expansion command called by \\[org-complete] in normal context.
  2375. Normal means, no org-mode-specific context."
  2376. :group 'org-completion
  2377. :type 'function)
  2378. ;;; Functions and variables from ther packages
  2379. ;; Declared here to avoid compiler warnings
  2380. ;; XEmacs only
  2381. (defvar outline-mode-menu-heading)
  2382. (defvar outline-mode-menu-show)
  2383. (defvar outline-mode-menu-hide)
  2384. (defvar zmacs-regions) ; XEmacs regions
  2385. ;; Emacs only
  2386. (defvar mark-active)
  2387. ;; Various packages
  2388. (declare-function find-library-name "find-func" (library))
  2389. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2390. (declare-function calendar-forward-day "cal-move" (arg))
  2391. (declare-function calendar-goto-date "cal-move" (date))
  2392. (declare-function calendar-goto-today "cal-move" ())
  2393. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2394. (defvar calc-embedded-close-formula)
  2395. (defvar calc-embedded-open-formula)
  2396. (declare-function cdlatex-tab "ext:cdlatex" ())
  2397. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2398. (defvar font-lock-unfontify-region-function)
  2399. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2400. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2401. (defvar iswitchb-temp-buflist)
  2402. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2403. (defvar org-agenda-tags-todo-honor-ignore-options)
  2404. (declare-function org-agenda-skip "org-agenda" ())
  2405. (declare-function org-format-agenda-item "org-agenda"
  2406. (extra txt &optional category tags dotime noprefix remove-re))
  2407. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2408. (declare-function org-agenda-change-all-lines "org-agenda"
  2409. (newhead hdmarker &optional fixface just-this))
  2410. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2411. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2412. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2413. (beg end))
  2414. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2415. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2416. "org-agenda" (&optional end))
  2417. (declare-function parse-time-string "parse-time" (string))
  2418. (declare-function remember "remember" (&optional initial))
  2419. (declare-function remember-buffer-desc "remember" ())
  2420. (declare-function remember-finalize "remember" ())
  2421. (defvar remember-save-after-remembering)
  2422. (defvar remember-data-file)
  2423. (defvar remember-register)
  2424. (defvar remember-buffer)
  2425. (defvar remember-handler-functions)
  2426. (defvar remember-annotation-functions)
  2427. (defvar texmathp-why)
  2428. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2429. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2430. (defvar w3m-current-url)
  2431. (defvar w3m-current-title)
  2432. (defvar org-latex-regexps)
  2433. ;;; Autoload and prepare some org modules
  2434. ;; Some table stuff that needs to be defined here, because it is used
  2435. ;; by the functions setting up org-mode or checking for table context.
  2436. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2437. "Detects an org-type or table-type table.")
  2438. (defconst org-table-line-regexp "^[ \t]*|"
  2439. "Detects an org-type table line.")
  2440. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2441. "Detects an org-type table line.")
  2442. (defconst org-table-hline-regexp "^[ \t]*|-"
  2443. "Detects an org-type table hline.")
  2444. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2445. "Detects a table-type table hline.")
  2446. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2447. "Searching from within a table (any type) this finds the first line
  2448. outside the table.")
  2449. ;; Autoload the functions in org-table.el that are needed by functions here.
  2450. (eval-and-compile
  2451. (org-autoload "org-table"
  2452. '(org-table-align org-table-begin org-table-blank-field
  2453. org-table-convert org-table-convert-region org-table-copy-down
  2454. org-table-copy-region org-table-create
  2455. org-table-create-or-convert-from-region
  2456. org-table-create-with-table.el org-table-current-dline
  2457. org-table-cut-region org-table-delete-column org-table-edit-field
  2458. org-table-edit-formulas org-table-end org-table-eval-formula
  2459. org-table-export org-table-field-info
  2460. org-table-get-stored-formulas org-table-goto-column
  2461. org-table-hline-and-move org-table-import org-table-insert-column
  2462. org-table-insert-hline org-table-insert-row org-table-iterate
  2463. org-table-justify-field-maybe org-table-kill-row
  2464. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2465. org-table-move-column org-table-move-column-left
  2466. org-table-move-column-right org-table-move-row
  2467. org-table-move-row-down org-table-move-row-up
  2468. org-table-next-field org-table-next-row org-table-paste-rectangle
  2469. org-table-previous-field org-table-recalculate
  2470. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2471. org-table-toggle-coordinate-overlays
  2472. org-table-toggle-formula-debugger org-table-wrap-region
  2473. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2474. (defun org-at-table-p (&optional table-type)
  2475. "Return t if the cursor is inside an org-type table.
  2476. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2477. (if org-enable-table-editor
  2478. (save-excursion
  2479. (beginning-of-line 1)
  2480. (looking-at (if table-type org-table-any-line-regexp
  2481. org-table-line-regexp)))
  2482. nil))
  2483. (defsubst org-table-p () (org-at-table-p))
  2484. (defun org-at-table.el-p ()
  2485. "Return t if and only if we are at a table.el table."
  2486. (and (org-at-table-p 'any)
  2487. (save-excursion
  2488. (goto-char (org-table-begin 'any))
  2489. (looking-at org-table1-hline-regexp))))
  2490. (defun org-table-recognize-table.el ()
  2491. "If there is a table.el table nearby, recognize it and move into it."
  2492. (if org-table-tab-recognizes-table.el
  2493. (if (org-at-table.el-p)
  2494. (progn
  2495. (beginning-of-line 1)
  2496. (if (looking-at org-table-dataline-regexp)
  2497. nil
  2498. (if (looking-at org-table1-hline-regexp)
  2499. (progn
  2500. (beginning-of-line 2)
  2501. (if (looking-at org-table-any-border-regexp)
  2502. (beginning-of-line -1)))))
  2503. (if (re-search-forward "|" (org-table-end t) t)
  2504. (progn
  2505. (require 'table)
  2506. (if (table--at-cell-p (point))
  2507. t
  2508. (message "recognizing table.el table...")
  2509. (table-recognize-table)
  2510. (message "recognizing table.el table...done")))
  2511. (error "This should not happen..."))
  2512. t)
  2513. nil)
  2514. nil))
  2515. (defun org-at-table-hline-p ()
  2516. "Return t if the cursor is inside a hline in a table."
  2517. (if org-enable-table-editor
  2518. (save-excursion
  2519. (beginning-of-line 1)
  2520. (looking-at org-table-hline-regexp))
  2521. nil))
  2522. (defvar org-table-clean-did-remove-column nil)
  2523. (defun org-table-map-tables (function)
  2524. "Apply FUNCTION to the start of all tables in the buffer."
  2525. (save-excursion
  2526. (save-restriction
  2527. (widen)
  2528. (goto-char (point-min))
  2529. (while (re-search-forward org-table-any-line-regexp nil t)
  2530. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2531. (beginning-of-line 1)
  2532. (if (looking-at org-table-line-regexp)
  2533. (save-excursion (funcall function)))
  2534. (re-search-forward org-table-any-border-regexp nil 1))))
  2535. (message "Mapping tables: done"))
  2536. ;; Declare and autoload functions from org-exp.el
  2537. (declare-function org-default-export-plist "org-exp")
  2538. (declare-function org-infile-export-plist "org-exp")
  2539. (declare-function org-get-current-options "org-exp")
  2540. (eval-and-compile
  2541. (org-autoload "org-exp"
  2542. '(org-export org-export-as-ascii org-export-visible
  2543. org-insert-export-options-template org-export-as-html-and-open
  2544. org-export-as-html-batch org-export-as-html-to-buffer
  2545. org-replace-region-by-html org-export-region-as-html
  2546. org-export-as-html org-export-icalendar-this-file
  2547. org-export-icalendar-all-agenda-files
  2548. org-table-clean-before-export
  2549. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2550. ;; Declare and autoload functions from org-agenda.el
  2551. (eval-and-compile
  2552. (org-autoload "org-agenda"
  2553. '(org-agenda org-agenda-list org-search-view
  2554. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2555. org-diary org-agenda-to-appt
  2556. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2557. ;; Autoload org-remember
  2558. (eval-and-compile
  2559. (org-autoload "org-remember"
  2560. '(org-remember-insinuate org-remember-annotation
  2561. org-remember-apply-template org-remember org-remember-handler)))
  2562. ;; Autoload org-clock.el
  2563. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2564. (beg end))
  2565. (declare-function org-clock-update-mode-line "org-clock" ())
  2566. (defvar org-clock-start-time)
  2567. (defvar org-clock-marker (make-marker)
  2568. "Marker recording the last clock-in.")
  2569. (eval-and-compile
  2570. (org-autoload
  2571. "org-clock"
  2572. '(org-clock-in org-clock-out org-clock-cancel
  2573. org-clock-goto org-clock-sum org-clock-display
  2574. org-clock-remove-overlays org-clock-report
  2575. org-clocktable-shift org-dblock-write:clocktable
  2576. org-get-clocktable)))
  2577. (defun org-clock-update-time-maybe ()
  2578. "If this is a CLOCK line, update it and return t.
  2579. Otherwise, return nil."
  2580. (interactive)
  2581. (save-excursion
  2582. (beginning-of-line 1)
  2583. (skip-chars-forward " \t")
  2584. (when (looking-at org-clock-string)
  2585. (let ((re (concat "[ \t]*" org-clock-string
  2586. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2587. "\\([ \t]*=>.*\\)?\\)?"))
  2588. ts te h m s neg)
  2589. (cond
  2590. ((not (looking-at re))
  2591. nil)
  2592. ((not (match-end 2))
  2593. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2594. (> org-clock-marker (point))
  2595. (<= org-clock-marker (point-at-eol)))
  2596. ;; The clock is running here
  2597. (setq org-clock-start-time
  2598. (apply 'encode-time
  2599. (org-parse-time-string (match-string 1))))
  2600. (org-clock-update-mode-line)))
  2601. (t
  2602. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2603. (end-of-line 1)
  2604. (setq ts (match-string 1)
  2605. te (match-string 3))
  2606. (setq s (- (time-to-seconds
  2607. (apply 'encode-time (org-parse-time-string te)))
  2608. (time-to-seconds
  2609. (apply 'encode-time (org-parse-time-string ts))))
  2610. neg (< s 0)
  2611. s (abs s)
  2612. h (floor (/ s 3600))
  2613. s (- s (* 3600 h))
  2614. m (floor (/ s 60))
  2615. s (- s (* 60 s)))
  2616. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2617. t))))))
  2618. (defun org-check-running-clock ()
  2619. "Check if the current buffer contains the running clock.
  2620. If yes, offer to stop it and to save the buffer with the changes."
  2621. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2622. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2623. (buffer-name))))
  2624. (org-clock-out)
  2625. (when (y-or-n-p "Save changed buffer?")
  2626. (save-buffer))))
  2627. (defun org-clocktable-try-shift (dir n)
  2628. "Check if this line starts a clock table, if yes, shift the time block."
  2629. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2630. (org-clocktable-shift dir n)))
  2631. ;; Autoload org-timer.el
  2632. (eval-and-compile
  2633. (org-autoload
  2634. "org-timer"
  2635. '(org-timer-start org-timer org-timer-item
  2636. org-timer-change-times-in-region)))
  2637. ;; Autoload org-feed.el
  2638. (eval-and-compile
  2639. (org-autoload
  2640. "org-feed"
  2641. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2642. ;; Autoload archiving code
  2643. ;; The stuff that is needed for cycling and tags has to be defined here.
  2644. (defgroup org-archive nil
  2645. "Options concerning archiving in Org-mode."
  2646. :tag "Org Archive"
  2647. :group 'org-structure)
  2648. (defcustom org-archive-location "%s_archive::"
  2649. "The location where subtrees should be archived.
  2650. The value of this variable is a string, consisting of two parts,
  2651. separated by a double-colon. The first part is a filename and
  2652. the second part is a headline.
  2653. When the filename is omitted, archiving happens in the same file.
  2654. %s in the filename will be replaced by the current file
  2655. name (without the directory part). Archiving to a different file
  2656. is useful to keep archived entries from contributing to the
  2657. Org-mode Agenda.
  2658. The archived entries will be filed as subtrees of the specified
  2659. headline. When the headline is omitted, the subtrees are simply
  2660. filed away at the end of the file, as top-level entries. Also in
  2661. the heading you can use %s to represent the file name, this can be
  2662. useful when using the same archive for a number of different files.
  2663. Here are a few examples:
  2664. \"%s_archive::\"
  2665. If the current file is Projects.org, archive in file
  2666. Projects.org_archive, as top-level trees. This is the default.
  2667. \"::* Archived Tasks\"
  2668. Archive in the current file, under the top-level headline
  2669. \"* Archived Tasks\".
  2670. \"~/org/archive.org::\"
  2671. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2672. \"~/org/archive.org::From %s\"
  2673. Archive in file ~/org/archive.org (absolute path), und headlines
  2674. \"From FILENAME\" where file name is the current file name.
  2675. \"basement::** Finished Tasks\"
  2676. Archive in file ./basement (relative path), as level 3 trees
  2677. below the level 2 heading \"** Finished Tasks\".
  2678. You may set this option on a per-file basis by adding to the buffer a
  2679. line like
  2680. #+ARCHIVE: basement::** Finished Tasks
  2681. You may also define it locally for a subtree by setting an ARCHIVE property
  2682. in the entry. If such a property is found in an entry, or anywhere up
  2683. the hierarchy, it will be used."
  2684. :group 'org-archive
  2685. :type 'string)
  2686. (defcustom org-archive-tag "ARCHIVE"
  2687. "The tag that marks a subtree as archived.
  2688. An archived subtree does not open during visibility cycling, and does
  2689. not contribute to the agenda listings.
  2690. After changing this, font-lock must be restarted in the relevant buffers to
  2691. get the proper fontification."
  2692. :group 'org-archive
  2693. :group 'org-keywords
  2694. :type 'string)
  2695. (defcustom org-agenda-skip-archived-trees t
  2696. "Non-nil means, the agenda will skip any items located in archived trees.
  2697. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2698. variable is no longer recommended, you should leave it at the value t.
  2699. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2700. :group 'org-archive
  2701. :group 'org-agenda-skip
  2702. :type 'boolean)
  2703. (defcustom org-columns-skip-arrchived-trees t
  2704. "Non-nil means, irgnore archived trees when creating column view."
  2705. :group 'org-archive
  2706. :group 'org-properties
  2707. :type 'boolean)
  2708. (defcustom org-cycle-open-archived-trees nil
  2709. "Non-nil means, `org-cycle' will open archived trees.
  2710. An archived tree is a tree marked with the tag ARCHIVE.
  2711. When nil, archived trees will stay folded. You can still open them with
  2712. normal outline commands like `show-all', but not with the cycling commands."
  2713. :group 'org-archive
  2714. :group 'org-cycle
  2715. :type 'boolean)
  2716. (defcustom org-sparse-tree-open-archived-trees nil
  2717. "Non-nil means sparse tree construction shows matches in archived trees.
  2718. When nil, matches in these trees are highlighted, but the trees are kept in
  2719. collapsed state."
  2720. :group 'org-archive
  2721. :group 'org-sparse-trees
  2722. :type 'boolean)
  2723. (defun org-cycle-hide-archived-subtrees (state)
  2724. "Re-hide all archived subtrees after a visibility state change."
  2725. (when (and (not org-cycle-open-archived-trees)
  2726. (not (memq state '(overview folded))))
  2727. (save-excursion
  2728. (let* ((globalp (memq state '(contents all)))
  2729. (beg (if globalp (point-min) (point)))
  2730. (end (if globalp (point-max) (org-end-of-subtree t))))
  2731. (org-hide-archived-subtrees beg end)
  2732. (goto-char beg)
  2733. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2734. (message "%s" (substitute-command-keys
  2735. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2736. (defun org-force-cycle-archived ()
  2737. "Cycle subtree even if it is archived."
  2738. (interactive)
  2739. (setq this-command 'org-cycle)
  2740. (let ((org-cycle-open-archived-trees t))
  2741. (call-interactively 'org-cycle)))
  2742. (defun org-hide-archived-subtrees (beg end)
  2743. "Re-hide all archived subtrees after a visibility state change."
  2744. (save-excursion
  2745. (let* ((re (concat ":" org-archive-tag ":")))
  2746. (goto-char beg)
  2747. (while (re-search-forward re end t)
  2748. (and (org-on-heading-p) (hide-subtree))
  2749. (org-end-of-subtree t)))))
  2750. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2751. (eval-and-compile
  2752. (org-autoload "org-archive"
  2753. '(org-add-archive-files org-archive-subtree
  2754. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2755. ;; Autoload Column View Code
  2756. (declare-function org-columns-number-to-string "org-colview")
  2757. (declare-function org-columns-get-format-and-top-level "org-colview")
  2758. (declare-function org-columns-compute "org-colview")
  2759. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2760. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2761. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2762. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2763. ;; Autoload ID code
  2764. (declare-function org-id-store-link "org-id")
  2765. (declare-function org-id-locations-load "org-id")
  2766. (declare-function org-id-locations-save "org-id")
  2767. (defvar org-id-track-globally)
  2768. (org-autoload "org-id"
  2769. '(org-id-get-create org-id-new org-id-copy org-id-get
  2770. org-id-get-with-outline-path-completion
  2771. org-id-get-with-outline-drilling
  2772. org-id-goto org-id-find org-id-store-link))
  2773. ;; Autoload Plotting Code
  2774. (org-autoload "org-plot"
  2775. '(org-plot/gnuplot))
  2776. ;;; Variables for pre-computed regular expressions, all buffer local
  2777. (defvar org-drawer-regexp nil
  2778. "Matches first line of a hidden block.")
  2779. (make-variable-buffer-local 'org-drawer-regexp)
  2780. (defvar org-todo-regexp nil
  2781. "Matches any of the TODO state keywords.")
  2782. (make-variable-buffer-local 'org-todo-regexp)
  2783. (defvar org-not-done-regexp nil
  2784. "Matches any of the TODO state keywords except the last one.")
  2785. (make-variable-buffer-local 'org-not-done-regexp)
  2786. (defvar org-not-done-heading-regexp nil
  2787. "Matches a TODO headline that is not done.")
  2788. (make-variable-buffer-local 'org-not-done-regexp)
  2789. (defvar org-todo-line-regexp nil
  2790. "Matches a headline and puts TODO state into group 2 if present.")
  2791. (make-variable-buffer-local 'org-todo-line-regexp)
  2792. (defvar org-complex-heading-regexp nil
  2793. "Matches a headline and puts everything into groups:
  2794. group 1: the stars
  2795. group 2: The todo keyword, maybe
  2796. group 3: Priority cookie
  2797. group 4: True headline
  2798. group 5: Tags")
  2799. (make-variable-buffer-local 'org-complex-heading-regexp)
  2800. (defvar org-todo-line-tags-regexp nil
  2801. "Matches a headline and puts TODO state into group 2 if present.
  2802. Also put tags into group 4 if tags are present.")
  2803. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2804. (defvar org-nl-done-regexp nil
  2805. "Matches newline followed by a headline with the DONE keyword.")
  2806. (make-variable-buffer-local 'org-nl-done-regexp)
  2807. (defvar org-looking-at-done-regexp nil
  2808. "Matches the DONE keyword a point.")
  2809. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2810. (defvar org-ds-keyword-length 12
  2811. "Maximum length of the Deadline and SCHEDULED keywords.")
  2812. (make-variable-buffer-local 'org-ds-keyword-length)
  2813. (defvar org-deadline-regexp nil
  2814. "Matches the DEADLINE keyword.")
  2815. (make-variable-buffer-local 'org-deadline-regexp)
  2816. (defvar org-deadline-time-regexp nil
  2817. "Matches the DEADLINE keyword together with a time stamp.")
  2818. (make-variable-buffer-local 'org-deadline-time-regexp)
  2819. (defvar org-deadline-line-regexp nil
  2820. "Matches the DEADLINE keyword and the rest of the line.")
  2821. (make-variable-buffer-local 'org-deadline-line-regexp)
  2822. (defvar org-scheduled-regexp nil
  2823. "Matches the SCHEDULED keyword.")
  2824. (make-variable-buffer-local 'org-scheduled-regexp)
  2825. (defvar org-scheduled-time-regexp nil
  2826. "Matches the SCHEDULED keyword together with a time stamp.")
  2827. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2828. (defvar org-closed-time-regexp nil
  2829. "Matches the CLOSED keyword together with a time stamp.")
  2830. (make-variable-buffer-local 'org-closed-time-regexp)
  2831. (defvar org-keyword-time-regexp nil
  2832. "Matches any of the 4 keywords, together with the time stamp.")
  2833. (make-variable-buffer-local 'org-keyword-time-regexp)
  2834. (defvar org-keyword-time-not-clock-regexp nil
  2835. "Matches any of the 3 keywords, together with the time stamp.")
  2836. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2837. (defvar org-maybe-keyword-time-regexp nil
  2838. "Matches a timestamp, possibly preceeded by a keyword.")
  2839. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2840. (defvar org-planning-or-clock-line-re nil
  2841. "Matches a line with planning or clock info.")
  2842. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2843. (defconst org-plain-time-of-day-regexp
  2844. (concat
  2845. "\\(\\<[012]?[0-9]"
  2846. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2847. "\\(--?"
  2848. "\\(\\<[012]?[0-9]"
  2849. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2850. "\\)?")
  2851. "Regular expression to match a plain time or time range.
  2852. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2853. groups carry important information:
  2854. 0 the full match
  2855. 1 the first time, range or not
  2856. 8 the second time, if it is a range.")
  2857. (defconst org-plain-time-extension-regexp
  2858. (concat
  2859. "\\(\\<[012]?[0-9]"
  2860. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2861. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2862. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2863. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2864. groups carry important information:
  2865. 0 the full match
  2866. 7 hours of duration
  2867. 9 minutes of duration")
  2868. (defconst org-stamp-time-of-day-regexp
  2869. (concat
  2870. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2871. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2872. "\\(--?"
  2873. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2874. "Regular expression to match a timestamp time or time range.
  2875. After a match, the following groups carry important information:
  2876. 0 the full match
  2877. 1 date plus weekday, for backreferencing to make sure both times on same day
  2878. 2 the first time, range or not
  2879. 4 the second time, if it is a range.")
  2880. (defconst org-startup-options
  2881. '(("fold" org-startup-folded t)
  2882. ("overview" org-startup-folded t)
  2883. ("nofold" org-startup-folded nil)
  2884. ("showall" org-startup-folded nil)
  2885. ("content" org-startup-folded content)
  2886. ("hidestars" org-hide-leading-stars t)
  2887. ("showstars" org-hide-leading-stars nil)
  2888. ("odd" org-odd-levels-only t)
  2889. ("oddeven" org-odd-levels-only nil)
  2890. ("align" org-startup-align-all-tables t)
  2891. ("noalign" org-startup-align-all-tables nil)
  2892. ("customtime" org-display-custom-times t)
  2893. ("logdone" org-log-done time)
  2894. ("lognotedone" org-log-done note)
  2895. ("nologdone" org-log-done nil)
  2896. ("lognoteclock-out" org-log-note-clock-out t)
  2897. ("nolognoteclock-out" org-log-note-clock-out nil)
  2898. ("logrepeat" org-log-repeat state)
  2899. ("lognoterepeat" org-log-repeat note)
  2900. ("nologrepeat" org-log-repeat nil)
  2901. ("fninline" org-footnote-define-inline t)
  2902. ("nofninline" org-footnote-define-inline nil)
  2903. ("fnlocal" org-footnote-section nil)
  2904. ("fnauto" org-footnote-auto-label t)
  2905. ("fnprompt" org-footnote-auto-label nil)
  2906. ("fnconfirm" org-footnote-auto-label confirm)
  2907. ("fnplain" org-footnote-auto-label plain)
  2908. ("constcgs" constants-unit-system cgs)
  2909. ("constSI" constants-unit-system SI)
  2910. ("noptag" org-tag-persistent-alist nil))
  2911. "Variable associated with STARTUP options for org-mode.
  2912. Each element is a list of three items: The startup options as written
  2913. in the #+STARTUP line, the corresponding variable, and the value to
  2914. set this variable to if the option is found. An optional forth element PUSH
  2915. means to push this value onto the list in the variable.")
  2916. (defun org-set-regexps-and-options ()
  2917. "Precompute regular expressions for current buffer."
  2918. (when (org-mode-p)
  2919. (org-set-local 'org-todo-kwd-alist nil)
  2920. (org-set-local 'org-todo-key-alist nil)
  2921. (org-set-local 'org-todo-key-trigger nil)
  2922. (org-set-local 'org-todo-keywords-1 nil)
  2923. (org-set-local 'org-done-keywords nil)
  2924. (org-set-local 'org-todo-heads nil)
  2925. (org-set-local 'org-todo-sets nil)
  2926. (org-set-local 'org-todo-log-states nil)
  2927. (org-set-local 'org-file-properties nil)
  2928. (org-set-local 'org-file-tags nil)
  2929. (let ((re (org-make-options-regexp
  2930. '("CATEGORY" "TODO" "COLUMNS"
  2931. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2932. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2933. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2934. (splitre "[ \t]+")
  2935. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2936. tail sep kws1 prio props ftags drawers
  2937. ext-setup-or-nil setup-contents (start 0))
  2938. (save-excursion
  2939. (save-restriction
  2940. (widen)
  2941. (goto-char (point-min))
  2942. (while (or (and ext-setup-or-nil
  2943. (string-match re ext-setup-or-nil start)
  2944. (setq start (match-end 0)))
  2945. (and (setq ext-setup-or-nil nil start 0)
  2946. (re-search-forward re nil t)))
  2947. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2948. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2949. (cond
  2950. ((equal key "CATEGORY")
  2951. (if (string-match "[ \t]+$" value)
  2952. (setq value (replace-match "" t t value)))
  2953. (setq cat value))
  2954. ((member key '("SEQ_TODO" "TODO"))
  2955. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2956. ((equal key "TYP_TODO")
  2957. (push (cons 'type (org-split-string value splitre)) kwds))
  2958. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2959. ;; general TODO-like setup
  2960. (push (cons (intern (downcase (match-string 1 key)))
  2961. (org-split-string value splitre)) kwds))
  2962. ((equal key "TAGS")
  2963. (setq tags (append tags (if tags '("\\n") nil)
  2964. (org-split-string value splitre))))
  2965. ((equal key "COLUMNS")
  2966. (org-set-local 'org-columns-default-format value))
  2967. ((equal key "LINK")
  2968. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2969. (push (cons (match-string 1 value)
  2970. (org-trim (match-string 2 value)))
  2971. links)))
  2972. ((equal key "PRIORITIES")
  2973. (setq prio (org-split-string value " +")))
  2974. ((equal key "PROPERTY")
  2975. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2976. (push (cons (match-string 1 value) (match-string 2 value))
  2977. props)))
  2978. ((equal key "FILETAGS")
  2979. (when (string-match "\\S-" value)
  2980. (setq ftags
  2981. (append
  2982. ftags
  2983. (apply 'append
  2984. (mapcar (lambda (x) (org-split-string x ":"))
  2985. (org-split-string value)))))))
  2986. ((equal key "DRAWERS")
  2987. (setq drawers (org-split-string value splitre)))
  2988. ((equal key "CONSTANTS")
  2989. (setq const (append const (org-split-string value splitre))))
  2990. ((equal key "STARTUP")
  2991. (let ((opts (org-split-string value splitre))
  2992. l var val)
  2993. (while (setq l (pop opts))
  2994. (when (setq l (assoc l org-startup-options))
  2995. (setq var (nth 1 l) val (nth 2 l))
  2996. (if (not (nth 3 l))
  2997. (set (make-local-variable var) val)
  2998. (if (not (listp (symbol-value var)))
  2999. (set (make-local-variable var) nil))
  3000. (set (make-local-variable var) (symbol-value var))
  3001. (add-to-list var val))))))
  3002. ((equal key "ARCHIVE")
  3003. (string-match " *$" value)
  3004. (setq arch (replace-match "" t t value))
  3005. (remove-text-properties 0 (length arch)
  3006. '(face t fontified t) arch))
  3007. ((equal key "SETUPFILE")
  3008. (setq setup-contents (org-file-contents
  3009. (expand-file-name
  3010. (org-remove-double-quotes value))
  3011. 'noerror))
  3012. (if (not ext-setup-or-nil)
  3013. (setq ext-setup-or-nil setup-contents start 0)
  3014. (setq ext-setup-or-nil
  3015. (concat (substring ext-setup-or-nil 0 start)
  3016. "\n" setup-contents "\n"
  3017. (substring ext-setup-or-nil start)))))
  3018. ))))
  3019. (when cat
  3020. (org-set-local 'org-category (intern cat))
  3021. (push (cons "CATEGORY" cat) props))
  3022. (when prio
  3023. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3024. (setq prio (mapcar 'string-to-char prio))
  3025. (org-set-local 'org-highest-priority (nth 0 prio))
  3026. (org-set-local 'org-lowest-priority (nth 1 prio))
  3027. (org-set-local 'org-default-priority (nth 2 prio)))
  3028. (and props (org-set-local 'org-file-properties (nreverse props)))
  3029. (and ftags (org-set-local 'org-file-tags
  3030. (mapcar 'org-add-prop-inherited ftags)))
  3031. (and drawers (org-set-local 'org-drawers drawers))
  3032. (and arch (org-set-local 'org-archive-location arch))
  3033. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3034. ;; Process the TODO keywords
  3035. (unless kwds
  3036. ;; Use the global values as if they had been given locally.
  3037. (setq kwds (default-value 'org-todo-keywords))
  3038. (if (stringp (car kwds))
  3039. (setq kwds (list (cons org-todo-interpretation
  3040. (default-value 'org-todo-keywords)))))
  3041. (setq kwds (reverse kwds)))
  3042. (setq kwds (nreverse kwds))
  3043. (let (inter kws kw)
  3044. (while (setq kws (pop kwds))
  3045. (let ((kws (or
  3046. (run-hook-with-args-until-success
  3047. 'org-todo-setup-filter-hook kws)
  3048. kws)))
  3049. (setq inter (pop kws) sep (member "|" kws)
  3050. kws0 (delete "|" (copy-sequence kws))
  3051. kwsa nil
  3052. kws1 (mapcar
  3053. (lambda (x)
  3054. ;; 1 2
  3055. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3056. (progn
  3057. (setq kw (match-string 1 x)
  3058. key (and (match-end 2) (match-string 2 x))
  3059. log (org-extract-log-state-settings x))
  3060. (push (cons kw (and key (string-to-char key))) kwsa)
  3061. (and log (push log org-todo-log-states))
  3062. kw)
  3063. (error "Invalid TODO keyword %s" x)))
  3064. kws0)
  3065. kwsa (if kwsa (append '((:startgroup))
  3066. (nreverse kwsa)
  3067. '((:endgroup))))
  3068. hw (car kws1)
  3069. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3070. tail (list inter hw (car dws) (org-last dws))))
  3071. (add-to-list 'org-todo-heads hw 'append)
  3072. (push kws1 org-todo-sets)
  3073. (setq org-done-keywords (append org-done-keywords dws nil))
  3074. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3075. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3076. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3077. (setq org-todo-sets (nreverse org-todo-sets)
  3078. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3079. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3080. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3081. ;; Process the constants
  3082. (when const
  3083. (let (e cst)
  3084. (while (setq e (pop const))
  3085. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3086. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3087. (setq org-table-formula-constants-local cst)))
  3088. ;; Process the tags.
  3089. (when tags
  3090. (let (e tgs)
  3091. (while (setq e (pop tags))
  3092. (cond
  3093. ((equal e "{") (push '(:startgroup) tgs))
  3094. ((equal e "}") (push '(:endgroup) tgs))
  3095. ((equal e "\\n") (push '(:newline) tgs))
  3096. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3097. (push (cons (match-string 1 e)
  3098. (string-to-char (match-string 2 e)))
  3099. tgs))
  3100. (t (push (list e) tgs))))
  3101. (org-set-local 'org-tag-alist nil)
  3102. (while (setq e (pop tgs))
  3103. (or (and (stringp (car e))
  3104. (assoc (car e) org-tag-alist))
  3105. (push e org-tag-alist)))))
  3106. ;; Compute the regular expressions and other local variables
  3107. (if (not org-done-keywords)
  3108. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3109. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3110. (length org-scheduled-string)
  3111. (length org-clock-string)
  3112. (length org-closed-string)))
  3113. org-drawer-regexp
  3114. (concat "^[ \t]*:\\("
  3115. (mapconcat 'regexp-quote org-drawers "\\|")
  3116. "\\):[ \t]*$")
  3117. org-not-done-keywords
  3118. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3119. org-todo-regexp
  3120. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3121. "\\|") "\\)\\>")
  3122. org-not-done-regexp
  3123. (concat "\\<\\("
  3124. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3125. "\\)\\>")
  3126. org-not-done-heading-regexp
  3127. (concat "^\\(\\*+\\)[ \t]+\\("
  3128. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3129. "\\)\\>")
  3130. org-todo-line-regexp
  3131. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3132. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3133. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3134. org-complex-heading-regexp
  3135. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3136. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3137. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3138. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3139. org-nl-done-regexp
  3140. (concat "\n\\*+[ \t]+"
  3141. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3142. "\\)" "\\>")
  3143. org-todo-line-tags-regexp
  3144. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3145. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3146. (org-re
  3147. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3148. org-looking-at-done-regexp
  3149. (concat "^" "\\(?:"
  3150. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3151. "\\>")
  3152. org-deadline-regexp (concat "\\<" org-deadline-string)
  3153. org-deadline-time-regexp
  3154. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3155. org-deadline-line-regexp
  3156. (concat "\\<\\(" org-deadline-string "\\).*")
  3157. org-scheduled-regexp
  3158. (concat "\\<" org-scheduled-string)
  3159. org-scheduled-time-regexp
  3160. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3161. org-closed-time-regexp
  3162. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3163. org-keyword-time-regexp
  3164. (concat "\\<\\(" org-scheduled-string
  3165. "\\|" org-deadline-string
  3166. "\\|" org-closed-string
  3167. "\\|" org-clock-string "\\)"
  3168. " *[[<]\\([^]>]+\\)[]>]")
  3169. org-keyword-time-not-clock-regexp
  3170. (concat "\\<\\(" org-scheduled-string
  3171. "\\|" org-deadline-string
  3172. "\\|" org-closed-string
  3173. "\\)"
  3174. " *[[<]\\([^]>]+\\)[]>]")
  3175. org-maybe-keyword-time-regexp
  3176. (concat "\\(\\<\\(" org-scheduled-string
  3177. "\\|" org-deadline-string
  3178. "\\|" org-closed-string
  3179. "\\|" org-clock-string "\\)\\)?"
  3180. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3181. org-planning-or-clock-line-re
  3182. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3183. "\\|" org-deadline-string
  3184. "\\|" org-closed-string "\\|" org-clock-string
  3185. "\\)\\>\\)")
  3186. )
  3187. (org-compute-latex-and-specials-regexp)
  3188. (org-set-font-lock-defaults))))
  3189. (defun org-file-contents (file &optional noerror)
  3190. "Return the contents of FILE, as a string."
  3191. (if (or (not file)
  3192. (not (file-readable-p file)))
  3193. (if noerror
  3194. (progn
  3195. (message "Cannot read file %s" file)
  3196. (ding) (sit-for 2)
  3197. "")
  3198. (error "Cannot read file %s" file))
  3199. (with-temp-buffer
  3200. (insert-file-contents file)
  3201. (buffer-string))))
  3202. (defun org-extract-log-state-settings (x)
  3203. "Extract the log state setting from a TODO keyword string.
  3204. This will extract info from a string like \"WAIT(w@/!)\"."
  3205. (let (kw key log1 log2)
  3206. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3207. (setq kw (match-string 1 x)
  3208. key (and (match-end 2) (match-string 2 x))
  3209. log1 (and (match-end 3) (match-string 3 x))
  3210. log2 (and (match-end 4) (match-string 4 x)))
  3211. (and (or log1 log2)
  3212. (list kw
  3213. (and log1 (if (equal log1 "!") 'time 'note))
  3214. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3215. (defun org-remove-keyword-keys (list)
  3216. "Remove a pair of parenthesis at the end of each string in LIST."
  3217. (mapcar (lambda (x)
  3218. (if (string-match "(.*)$" x)
  3219. (substring x 0 (match-beginning 0))
  3220. x))
  3221. list))
  3222. ;; FIXME: this could be done much better, using second characters etc.
  3223. (defun org-assign-fast-keys (alist)
  3224. "Assign fast keys to a keyword-key alist.
  3225. Respect keys that are already there."
  3226. (let (new e k c c1 c2 (char ?a))
  3227. (while (setq e (pop alist))
  3228. (cond
  3229. ((equal e '(:startgroup)) (push e new))
  3230. ((equal e '(:endgroup)) (push e new))
  3231. ((equal e '(:newline)) (push e new))
  3232. (t
  3233. (setq k (car e) c2 nil)
  3234. (if (cdr e)
  3235. (setq c (cdr e))
  3236. ;; automatically assign a character.
  3237. (setq c1 (string-to-char
  3238. (downcase (substring
  3239. k (if (= (string-to-char k) ?@) 1 0)))))
  3240. (if (or (rassoc c1 new) (rassoc c1 alist))
  3241. (while (or (rassoc char new) (rassoc char alist))
  3242. (setq char (1+ char)))
  3243. (setq c2 c1))
  3244. (setq c (or c2 char)))
  3245. (push (cons k c) new))))
  3246. (nreverse new)))
  3247. ;;; Some variables used in various places
  3248. (defvar org-window-configuration nil
  3249. "Used in various places to store a window configuration.")
  3250. (defvar org-finish-function nil
  3251. "Function to be called when `C-c C-c' is used.
  3252. This is for getting out of special buffers like remember.")
  3253. ;; FIXME: Occasionally check by commenting these, to make sure
  3254. ;; no other functions uses these, forgetting to let-bind them.
  3255. (defvar entry)
  3256. (defvar state)
  3257. (defvar last-state)
  3258. (defvar date)
  3259. (defvar description)
  3260. ;; Defined somewhere in this file, but used before definition.
  3261. (defvar org-html-entities)
  3262. (defvar org-struct-menu)
  3263. (defvar org-org-menu)
  3264. (defvar org-tbl-menu)
  3265. (defvar org-agenda-keymap)
  3266. ;;;; Define the Org-mode
  3267. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3268. (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."))
  3269. ;; We use a before-change function to check if a table might need
  3270. ;; an update.
  3271. (defvar org-table-may-need-update t
  3272. "Indicates that a table might need an update.
  3273. This variable is set by `org-before-change-function'.
  3274. `org-table-align' sets it back to nil.")
  3275. (defun org-before-change-function (beg end)
  3276. "Every change indicates that a table might need an update."
  3277. (setq org-table-may-need-update t))
  3278. (defvar org-mode-map)
  3279. (defvar org-mode-hook nil
  3280. "Mode hook for Org-mode, run after the mode was turned on.")
  3281. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3282. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3283. (defvar org-table-buffer-is-an nil)
  3284. (defconst org-outline-regexp "\\*+ ")
  3285. ;;;###autoload
  3286. (define-derived-mode org-mode outline-mode "Org"
  3287. "Outline-based notes management and organizer, alias
  3288. \"Carsten's outline-mode for keeping track of everything.\"
  3289. Org-mode develops organizational tasks around a NOTES file which
  3290. contains information about projects as plain text. Org-mode is
  3291. implemented on top of outline-mode, which is ideal to keep the content
  3292. of large files well structured. It supports ToDo items, deadlines and
  3293. time stamps, which magically appear in the diary listing of the Emacs
  3294. calendar. Tables are easily created with a built-in table editor.
  3295. Plain text URL-like links connect to websites, emails (VM), Usenet
  3296. messages (Gnus), BBDB entries, and any files related to the project.
  3297. For printing and sharing of notes, an Org-mode file (or a part of it)
  3298. can be exported as a structured ASCII or HTML file.
  3299. The following commands are available:
  3300. \\{org-mode-map}"
  3301. ;; Get rid of Outline menus, they are not needed
  3302. ;; Need to do this here because define-derived-mode sets up
  3303. ;; the keymap so late. Still, it is a waste to call this each time
  3304. ;; we switch another buffer into org-mode.
  3305. (if (featurep 'xemacs)
  3306. (when (boundp 'outline-mode-menu-heading)
  3307. ;; Assume this is Greg's port, it used easymenu
  3308. (easy-menu-remove outline-mode-menu-heading)
  3309. (easy-menu-remove outline-mode-menu-show)
  3310. (easy-menu-remove outline-mode-menu-hide))
  3311. (define-key org-mode-map [menu-bar headings] 'undefined)
  3312. (define-key org-mode-map [menu-bar hide] 'undefined)
  3313. (define-key org-mode-map [menu-bar show] 'undefined))
  3314. (org-load-modules-maybe)
  3315. (easy-menu-add org-org-menu)
  3316. (easy-menu-add org-tbl-menu)
  3317. (org-install-agenda-files-menu)
  3318. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3319. (org-add-to-invisibility-spec '(org-cwidth))
  3320. (when (featurep 'xemacs)
  3321. (org-set-local 'line-move-ignore-invisible t))
  3322. (org-set-local 'outline-regexp org-outline-regexp)
  3323. (org-set-local 'outline-level 'org-outline-level)
  3324. (when (and org-ellipsis
  3325. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3326. (fboundp 'make-glyph-code))
  3327. (unless org-display-table
  3328. (setq org-display-table (make-display-table)))
  3329. (set-display-table-slot
  3330. org-display-table 4
  3331. (vconcat (mapcar
  3332. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3333. org-ellipsis)))
  3334. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3335. (setq buffer-display-table org-display-table))
  3336. (org-set-regexps-and-options)
  3337. (when (and org-tag-faces (not org-tags-special-faces-re))
  3338. ;; tag faces set outside customize.... force initialization.
  3339. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3340. ;; Calc embedded
  3341. (org-set-local 'calc-embedded-open-mode "# ")
  3342. (modify-syntax-entry ?# "<")
  3343. (modify-syntax-entry ?@ "w")
  3344. (if org-startup-truncated (setq truncate-lines t))
  3345. (org-set-local 'font-lock-unfontify-region-function
  3346. 'org-unfontify-region)
  3347. ;; Activate before-change-function
  3348. (org-set-local 'org-table-may-need-update t)
  3349. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3350. 'local)
  3351. ;; Check for running clock before killing a buffer
  3352. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3353. ;; Paragraphs and auto-filling
  3354. (org-set-autofill-regexps)
  3355. (setq indent-line-function 'org-indent-line-function)
  3356. (org-update-radio-target-regexp)
  3357. ;; Make sure dependence stuff works reliably, even for users who set it
  3358. ;; too late :-(
  3359. (if org-enforce-todo-dependencies
  3360. (add-hook 'org-blocker-hook
  3361. 'org-block-todo-from-children-or-siblings-or-parent)
  3362. (remove-hook 'org-blocker-hook
  3363. 'org-block-todo-from-children-or-siblings-or-parent))
  3364. (if org-enforce-todo-checkbox-dependencies
  3365. (add-hook 'org-blocker-hook
  3366. 'org-block-todo-from-checkboxes)
  3367. (remove-hook 'org-blocker-hook
  3368. 'org-block-todo-from-checkboxes))
  3369. ;; Comment characters
  3370. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3371. (org-set-local 'comment-padding " ")
  3372. ;; Align options lines
  3373. (org-set-local
  3374. 'align-mode-rules-list
  3375. '((org-in-buffer-settings
  3376. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3377. (modes . '(org-mode)))))
  3378. ;; Imenu
  3379. (org-set-local 'imenu-create-index-function
  3380. 'org-imenu-get-tree)
  3381. ;; Make isearch reveal context
  3382. (if (or (featurep 'xemacs)
  3383. (not (boundp 'outline-isearch-open-invisible-function)))
  3384. ;; Emacs 21 and XEmacs make use of the hook
  3385. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3386. ;; Emacs 22 deals with this through a special variable
  3387. (org-set-local 'outline-isearch-open-invisible-function
  3388. (lambda (&rest ignore) (org-show-context 'isearch))))
  3389. ;; If empty file that did not turn on org-mode automatically, make it to.
  3390. (if (and org-insert-mode-line-in-empty-file
  3391. (interactive-p)
  3392. (= (point-min) (point-max)))
  3393. (insert "# -*- mode: org -*-\n\n"))
  3394. (unless org-inhibit-startup
  3395. (when org-startup-align-all-tables
  3396. (let ((bmp (buffer-modified-p)))
  3397. (org-table-map-tables 'org-table-align)
  3398. (set-buffer-modified-p bmp)))
  3399. (org-set-startup-visibility)))
  3400. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3401. (defun org-current-time ()
  3402. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3403. (if (> (car org-time-stamp-rounding-minutes) 1)
  3404. (let ((r (car org-time-stamp-rounding-minutes))
  3405. (time (decode-time)))
  3406. (apply 'encode-time
  3407. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3408. (nthcdr 2 time))))
  3409. (current-time)))
  3410. ;;;; Font-Lock stuff, including the activators
  3411. (defvar org-mouse-map (make-sparse-keymap))
  3412. (org-defkey org-mouse-map
  3413. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3414. (org-defkey org-mouse-map
  3415. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3416. (when org-mouse-1-follows-link
  3417. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3418. (when org-tab-follows-link
  3419. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3420. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3421. (require 'font-lock)
  3422. (defconst org-non-link-chars "]\t\n\r<>")
  3423. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3424. "shell" "elisp"))
  3425. (defvar org-link-types-re nil
  3426. "Matches a link that has a url-like prefix like \"http:\"")
  3427. (defvar org-link-re-with-space nil
  3428. "Matches a link with spaces, optional angular brackets around it.")
  3429. (defvar org-link-re-with-space2 nil
  3430. "Matches a link with spaces, optional angular brackets around it.")
  3431. (defvar org-link-re-with-space3 nil
  3432. "Matches a link with spaces, only for internal part in bracket links.")
  3433. (defvar org-angle-link-re nil
  3434. "Matches link with angular brackets, spaces are allowed.")
  3435. (defvar org-plain-link-re nil
  3436. "Matches plain link, without spaces.")
  3437. (defvar org-bracket-link-regexp nil
  3438. "Matches a link in double brackets.")
  3439. (defvar org-bracket-link-analytic-regexp nil
  3440. "Regular expression used to analyze links.
  3441. Here is what the match groups contain after a match:
  3442. 1: http:
  3443. 2: http
  3444. 3: path
  3445. 4: [desc]
  3446. 5: desc")
  3447. (defvar org-bracket-link-analytic-regexp++ nil
  3448. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3449. (defvar org-any-link-re nil
  3450. "Regular expression matching any link.")
  3451. (defun org-make-link-regexps ()
  3452. "Update the link regular expressions.
  3453. This should be called after the variable `org-link-types' has changed."
  3454. (setq org-link-types-re
  3455. (concat
  3456. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3457. org-link-re-with-space
  3458. (concat
  3459. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3460. "\\([^" org-non-link-chars " ]"
  3461. "[^" org-non-link-chars "]*"
  3462. "[^" org-non-link-chars " ]\\)>?")
  3463. org-link-re-with-space2
  3464. (concat
  3465. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3466. "\\([^" org-non-link-chars " ]"
  3467. "[^\t\n\r]*"
  3468. "[^" org-non-link-chars " ]\\)>?")
  3469. org-link-re-with-space3
  3470. (concat
  3471. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3472. "\\([^" org-non-link-chars " ]"
  3473. "[^\t\n\r]*\\)")
  3474. org-angle-link-re
  3475. (concat
  3476. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3477. "\\([^" org-non-link-chars " ]"
  3478. "[^" org-non-link-chars "]*"
  3479. "\\)>")
  3480. org-plain-link-re
  3481. (concat
  3482. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3483. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3484. org-bracket-link-regexp
  3485. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3486. org-bracket-link-analytic-regexp
  3487. (concat
  3488. "\\[\\["
  3489. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3490. "\\([^]]+\\)"
  3491. "\\]"
  3492. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3493. "\\]")
  3494. org-bracket-link-analytic-regexp++
  3495. (concat
  3496. "\\[\\["
  3497. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3498. "\\([^]]+\\)"
  3499. "\\]"
  3500. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3501. "\\]")
  3502. org-any-link-re
  3503. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3504. org-angle-link-re "\\)\\|\\("
  3505. org-plain-link-re "\\)")))
  3506. (org-make-link-regexps)
  3507. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3508. "Regular expression for fast time stamp matching.")
  3509. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3510. "Regular expression for fast time stamp matching.")
  3511. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3512. "Regular expression matching time strings for analysis.
  3513. This one does not require the space after the date, so it can be used
  3514. on a string that terminates immediately after the date.")
  3515. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3516. "Regular expression matching time strings for analysis.")
  3517. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3518. "Regular expression matching time stamps, with groups.")
  3519. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3520. "Regular expression matching time stamps (also [..]), with groups.")
  3521. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3522. "Regular expression matching a time stamp range.")
  3523. (defconst org-tr-regexp-both
  3524. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3525. "Regular expression matching a time stamp range.")
  3526. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3527. org-ts-regexp "\\)?")
  3528. "Regular expression matching a time stamp or time stamp range.")
  3529. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3530. org-ts-regexp-both "\\)?")
  3531. "Regular expression matching a time stamp or time stamp range.
  3532. The time stamps may be either active or inactive.")
  3533. (defvar org-emph-face nil)
  3534. (defun org-do-emphasis-faces (limit)
  3535. "Run through the buffer and add overlays to links."
  3536. (let (rtn)
  3537. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3538. (if (not (= (char-after (match-beginning 3))
  3539. (char-after (match-beginning 4))))
  3540. (progn
  3541. (setq rtn t)
  3542. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3543. 'face
  3544. (nth 1 (assoc (match-string 3)
  3545. org-emphasis-alist)))
  3546. (add-text-properties (match-beginning 2) (match-end 2)
  3547. '(font-lock-multiline t))
  3548. (when org-hide-emphasis-markers
  3549. (add-text-properties (match-end 4) (match-beginning 5)
  3550. '(invisible org-link))
  3551. (add-text-properties (match-beginning 3) (match-end 3)
  3552. '(invisible org-link)))))
  3553. (backward-char 1))
  3554. rtn))
  3555. (defun org-emphasize (&optional char)
  3556. "Insert or change an emphasis, i.e. a font like bold or italic.
  3557. If there is an active region, change that region to a new emphasis.
  3558. If there is no region, just insert the marker characters and position
  3559. the cursor between them.
  3560. CHAR should be either the marker character, or the first character of the
  3561. HTML tag associated with that emphasis. If CHAR is a space, the means
  3562. to remove the emphasis of the selected region.
  3563. If char is not given (for example in an interactive call) it
  3564. will be prompted for."
  3565. (interactive)
  3566. (let ((eal org-emphasis-alist) e det
  3567. (erc org-emphasis-regexp-components)
  3568. (prompt "")
  3569. (string "") beg end move tag c s)
  3570. (if (org-region-active-p)
  3571. (setq beg (region-beginning) end (region-end)
  3572. string (buffer-substring beg end))
  3573. (setq move t))
  3574. (while (setq e (pop eal))
  3575. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3576. c (aref tag 0))
  3577. (push (cons c (string-to-char (car e))) det)
  3578. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3579. (substring tag 1)))))
  3580. (setq det (nreverse det))
  3581. (unless char
  3582. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3583. (setq char (read-char-exclusive)))
  3584. (setq char (or (cdr (assoc char det)) char))
  3585. (if (equal char ?\ )
  3586. (setq s "" move nil)
  3587. (unless (assoc (char-to-string char) org-emphasis-alist)
  3588. (error "No such emphasis marker: \"%c\"" char))
  3589. (setq s (char-to-string char)))
  3590. (while (and (> (length string) 1)
  3591. (equal (substring string 0 1) (substring string -1))
  3592. (assoc (substring string 0 1) org-emphasis-alist))
  3593. (setq string (substring string 1 -1)))
  3594. (setq string (concat s string s))
  3595. (if beg (delete-region beg end))
  3596. (unless (or (bolp)
  3597. (string-match (concat "[" (nth 0 erc) "\n]")
  3598. (char-to-string (char-before (point)))))
  3599. (insert " "))
  3600. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3601. (char-to-string (char-after (point))))
  3602. (insert " ") (backward-char 1))
  3603. (insert string)
  3604. (and move (backward-char 1))))
  3605. (defconst org-nonsticky-props
  3606. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3607. (defun org-activate-plain-links (limit)
  3608. "Run through the buffer and add overlays to links."
  3609. (catch 'exit
  3610. (let (f)
  3611. (while (re-search-forward org-plain-link-re limit t)
  3612. (setq f (get-text-property (match-beginning 0) 'face))
  3613. (if (or (eq f 'org-tag)
  3614. (and (listp f) (memq 'org-tag f)))
  3615. nil
  3616. (add-text-properties (match-beginning 0) (match-end 0)
  3617. (list 'mouse-face 'highlight
  3618. 'rear-nonsticky org-nonsticky-props
  3619. 'keymap org-mouse-map
  3620. ))
  3621. (throw 'exit t))))))
  3622. (defun org-activate-code (limit)
  3623. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3624. (progn
  3625. (remove-text-properties (match-beginning 0) (match-end 0)
  3626. '(display t invisible t intangible t))
  3627. t)))
  3628. (defun org-activate-angle-links (limit)
  3629. "Run through the buffer and add overlays to links."
  3630. (if (re-search-forward org-angle-link-re limit t)
  3631. (progn
  3632. (add-text-properties (match-beginning 0) (match-end 0)
  3633. (list 'mouse-face 'highlight
  3634. 'rear-nonsticky org-nonsticky-props
  3635. 'keymap org-mouse-map
  3636. ))
  3637. t)))
  3638. (defun org-activate-footnote-links (limit)
  3639. "Run through the buffer and add overlays to links."
  3640. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3641. limit t)
  3642. (progn
  3643. (add-text-properties (match-beginning 2) (match-end 2)
  3644. (list 'mouse-face 'highlight
  3645. 'rear-nonsticky org-nonsticky-props
  3646. 'keymap org-mouse-map
  3647. 'help-echo
  3648. (if (= (point-at-bol) (match-beginning 2))
  3649. "Footnote definition"
  3650. "Footnote reference")
  3651. ))
  3652. t)))
  3653. (defun org-activate-bracket-links (limit)
  3654. "Run through the buffer and add overlays to bracketed links."
  3655. (if (re-search-forward org-bracket-link-regexp limit t)
  3656. (let* ((help (concat "LINK: "
  3657. (org-match-string-no-properties 1)))
  3658. ;; FIXME: above we should remove the escapes.
  3659. ;; but that requires another match, protecting match data,
  3660. ;; a lot of overhead for font-lock.
  3661. (ip (org-maybe-intangible
  3662. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3663. 'keymap org-mouse-map 'mouse-face 'highlight
  3664. 'font-lock-multiline t 'help-echo help)))
  3665. (vp (list 'rear-nonsticky org-nonsticky-props
  3666. 'keymap org-mouse-map 'mouse-face 'highlight
  3667. 'font-lock-multiline t 'help-echo help)))
  3668. ;; We need to remove the invisible property here. Table narrowing
  3669. ;; may have made some of this invisible.
  3670. (remove-text-properties (match-beginning 0) (match-end 0)
  3671. '(invisible nil))
  3672. (if (match-end 3)
  3673. (progn
  3674. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3675. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3676. (add-text-properties (match-end 3) (match-end 0) ip))
  3677. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3678. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3679. (add-text-properties (match-end 1) (match-end 0) ip))
  3680. t)))
  3681. (defun org-activate-dates (limit)
  3682. "Run through the buffer and add overlays to dates."
  3683. (if (re-search-forward org-tsr-regexp-both limit t)
  3684. (progn
  3685. (add-text-properties (match-beginning 0) (match-end 0)
  3686. (list 'mouse-face 'highlight
  3687. 'rear-nonsticky org-nonsticky-props
  3688. 'keymap org-mouse-map))
  3689. (when org-display-custom-times
  3690. (if (match-end 3)
  3691. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3692. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3693. t)))
  3694. (defvar org-target-link-regexp nil
  3695. "Regular expression matching radio targets in plain text.")
  3696. (make-variable-buffer-local 'org-target-link-regexp)
  3697. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3698. "Regular expression matching a link target.")
  3699. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3700. "Regular expression matching a radio target.")
  3701. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3702. "Regular expression matching any target.")
  3703. (defun org-activate-target-links (limit)
  3704. "Run through the buffer and add overlays to target matches."
  3705. (when org-target-link-regexp
  3706. (let ((case-fold-search t))
  3707. (if (re-search-forward org-target-link-regexp limit t)
  3708. (progn
  3709. (add-text-properties (match-beginning 0) (match-end 0)
  3710. (list 'mouse-face 'highlight
  3711. 'rear-nonsticky org-nonsticky-props
  3712. 'keymap org-mouse-map
  3713. 'help-echo "Radio target link"
  3714. 'org-linked-text t))
  3715. t)))))
  3716. (defun org-update-radio-target-regexp ()
  3717. "Find all radio targets in this file and update the regular expression."
  3718. (interactive)
  3719. (when (memq 'radio org-activate-links)
  3720. (setq org-target-link-regexp
  3721. (org-make-target-link-regexp (org-all-targets 'radio)))
  3722. (org-restart-font-lock)))
  3723. (defun org-hide-wide-columns (limit)
  3724. (let (s e)
  3725. (setq s (text-property-any (point) (or limit (point-max))
  3726. 'org-cwidth t))
  3727. (when s
  3728. (setq e (next-single-property-change s 'org-cwidth))
  3729. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3730. (goto-char e)
  3731. t)))
  3732. (defvar org-latex-and-specials-regexp nil
  3733. "Regular expression for highlighting export special stuff.")
  3734. (defvar org-match-substring-regexp)
  3735. (defvar org-match-substring-with-braces-regexp)
  3736. (defvar org-export-html-special-string-regexps)
  3737. (defun org-compute-latex-and-specials-regexp ()
  3738. "Compute regular expression for stuff treated specially by exporters."
  3739. (if (not org-highlight-latex-fragments-and-specials)
  3740. (org-set-local 'org-latex-and-specials-regexp nil)
  3741. (require 'org-exp)
  3742. (let*
  3743. ((matchers (plist-get org-format-latex-options :matchers))
  3744. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3745. org-latex-regexps)))
  3746. (options (org-combine-plists (org-default-export-plist)
  3747. (org-infile-export-plist)))
  3748. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3749. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3750. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3751. (org-export-html-expand (plist-get options :expand-quoted-html))
  3752. (org-export-with-special-strings (plist-get options :special-strings))
  3753. (re-sub
  3754. (cond
  3755. ((equal org-export-with-sub-superscripts '{})
  3756. (list org-match-substring-with-braces-regexp))
  3757. (org-export-with-sub-superscripts
  3758. (list org-match-substring-regexp))
  3759. (t nil)))
  3760. (re-latex
  3761. (if org-export-with-LaTeX-fragments
  3762. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3763. (re-macros
  3764. (if org-export-with-TeX-macros
  3765. (list (concat "\\\\"
  3766. (regexp-opt
  3767. (append (mapcar 'car org-html-entities)
  3768. (if (boundp 'org-latex-entities)
  3769. org-latex-entities nil))
  3770. 'words))) ; FIXME
  3771. ))
  3772. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3773. (re-special (if org-export-with-special-strings
  3774. (mapcar (lambda (x) (car x))
  3775. org-export-html-special-string-regexps)))
  3776. (re-rest
  3777. (delq nil
  3778. (list
  3779. (if org-export-html-expand "@<[^>\n]+>")
  3780. ))))
  3781. (org-set-local
  3782. 'org-latex-and-specials-regexp
  3783. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3784. re-rest) "\\|")))))
  3785. (defun org-do-latex-and-special-faces (limit)
  3786. "Run through the buffer and add overlays to links."
  3787. (when org-latex-and-specials-regexp
  3788. (let (rtn d)
  3789. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3790. limit t))
  3791. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3792. 'face))
  3793. '(org-code org-verbatim underline)))
  3794. (progn
  3795. (setq rtn t
  3796. d (cond ((member (char-after (1+ (match-beginning 0)))
  3797. '(?_ ?^)) 1)
  3798. (t 0)))
  3799. (font-lock-prepend-text-property
  3800. (+ d (match-beginning 0)) (match-end 0)
  3801. 'face 'org-latex-and-export-specials)
  3802. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3803. '(font-lock-multiline t)))))
  3804. rtn)))
  3805. (defun org-restart-font-lock ()
  3806. "Restart font-lock-mode, to force refontification."
  3807. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3808. (font-lock-mode -1)
  3809. (font-lock-mode 1)))
  3810. (defun org-all-targets (&optional radio)
  3811. "Return a list of all targets in this file.
  3812. With optional argument RADIO, only find radio targets."
  3813. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3814. rtn)
  3815. (save-excursion
  3816. (goto-char (point-min))
  3817. (while (re-search-forward re nil t)
  3818. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3819. rtn)))
  3820. (defun org-make-target-link-regexp (targets)
  3821. "Make regular expression matching all strings in TARGETS.
  3822. The regular expression finds the targets also if there is a line break
  3823. between words."
  3824. (and targets
  3825. (concat
  3826. "\\<\\("
  3827. (mapconcat
  3828. (lambda (x)
  3829. (while (string-match " +" x)
  3830. (setq x (replace-match "\\s-+" t t x)))
  3831. x)
  3832. targets
  3833. "\\|")
  3834. "\\)\\>")))
  3835. (defun org-activate-tags (limit)
  3836. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3837. (progn
  3838. (add-text-properties (match-beginning 1) (match-end 1)
  3839. (list 'mouse-face 'highlight
  3840. 'rear-nonsticky org-nonsticky-props
  3841. 'keymap org-mouse-map))
  3842. t)))
  3843. (defun org-outline-level ()
  3844. (save-excursion
  3845. (looking-at outline-regexp)
  3846. (if (match-beginning 1)
  3847. (+ (org-get-string-indentation (match-string 1)) 1000)
  3848. (1- (- (match-end 0) (match-beginning 0))))))
  3849. (defvar org-font-lock-keywords nil)
  3850. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3851. "Regular expression matching a property line.")
  3852. (defvar org-font-lock-hook nil
  3853. "Functions to be called for special font lock stuff.")
  3854. (defun org-font-lock-hook (limit)
  3855. (run-hook-with-args 'org-font-lock-hook limit))
  3856. (defun org-set-font-lock-defaults ()
  3857. (let* ((em org-fontify-emphasized-text)
  3858. (lk org-activate-links)
  3859. (org-font-lock-extra-keywords
  3860. (list
  3861. ;; Call the hook
  3862. '(org-font-lock-hook)
  3863. ;; Headlines
  3864. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3865. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3866. ;; Table lines
  3867. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3868. (1 'org-table t))
  3869. ;; Table internals
  3870. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3871. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3872. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3873. ;; Drawers
  3874. (list org-drawer-regexp '(0 'org-special-keyword t))
  3875. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3876. ;; Properties
  3877. (list org-property-re
  3878. '(1 'org-special-keyword t)
  3879. '(3 'org-property-value t))
  3880. (if org-format-transports-properties-p
  3881. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3882. ;; Links
  3883. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3884. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3885. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3886. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3887. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3888. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3889. (if (memq 'footnote lk) '(org-activate-footnote-links
  3890. (2 'org-footnote t)))
  3891. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3892. '(org-hide-wide-columns (0 nil append))
  3893. ;; TODO lines
  3894. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3895. '(1 (org-get-todo-face 1) t))
  3896. ;; DONE
  3897. (if org-fontify-done-headline
  3898. (list (concat "^[*]+ +\\<\\("
  3899. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3900. "\\)\\(.*\\)")
  3901. '(2 'org-headline-done t))
  3902. nil)
  3903. ;; Priorities
  3904. '(org-font-lock-add-priority-faces)
  3905. ;; Tags
  3906. '(org-font-lock-add-tag-faces)
  3907. ;; Special keywords
  3908. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3909. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3910. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3911. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3912. ;; Emphasis
  3913. (if em
  3914. (if (featurep 'xemacs)
  3915. '(org-do-emphasis-faces (0 nil append))
  3916. '(org-do-emphasis-faces)))
  3917. ;; Checkboxes
  3918. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3919. 2 'org-checkbox prepend)
  3920. (if org-provide-checkbox-statistics
  3921. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3922. (0 (org-get-checkbox-statistics-face) t)))
  3923. ;; Description list items
  3924. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3925. 2 'bold prepend)
  3926. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3927. '(1 'org-archived prepend))
  3928. ;; Specials
  3929. '(org-do-latex-and-special-faces)
  3930. ;; Code
  3931. '(org-activate-code (1 'org-code t))
  3932. ;; COMMENT
  3933. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3934. "\\|" org-quote-string "\\)\\>")
  3935. '(1 'org-special-keyword t))
  3936. '("^#.*" (0 'font-lock-comment-face t))
  3937. )))
  3938. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3939. ;; Now set the full font-lock-keywords
  3940. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3941. (org-set-local 'font-lock-defaults
  3942. '(org-font-lock-keywords t nil nil backward-paragraph))
  3943. (kill-local-variable 'font-lock-keywords) nil))
  3944. (defvar org-m nil)
  3945. (defvar org-l nil)
  3946. (defvar org-f nil)
  3947. (defun org-get-level-face (n)
  3948. "Get the right face for match N in font-lock matching of headlines."
  3949. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3950. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3951. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3952. (cond
  3953. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3954. ((eq n 2) org-f)
  3955. (t (if org-level-color-stars-only nil org-f))))
  3956. (defun org-get-todo-face (kwd)
  3957. "Get the right face for a TODO keyword KWD.
  3958. If KWD is a number, get the corresponding match group."
  3959. (if (numberp kwd) (setq kwd (match-string kwd)))
  3960. (or (cdr (assoc kwd org-todo-keyword-faces))
  3961. (and (member kwd org-done-keywords) 'org-done)
  3962. 'org-todo))
  3963. (defun org-font-lock-add-tag-faces (limit)
  3964. "Add the special tag faces."
  3965. (when (and org-tag-faces org-tags-special-faces-re)
  3966. (while (re-search-forward org-tags-special-faces-re limit t)
  3967. (add-text-properties (match-beginning 1) (match-end 1)
  3968. (list 'face (org-get-tag-face 1)
  3969. 'font-lock-fontified t))
  3970. (backward-char 1))))
  3971. (defun org-font-lock-add-priority-faces (limit)
  3972. "Add the special priority faces."
  3973. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  3974. (add-text-properties
  3975. (match-beginning 0) (match-end 0)
  3976. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  3977. org-priority-faces))
  3978. 'org-special-keyword)
  3979. 'font-lock-fontified t))))
  3980. (defun org-get-tag-face (kwd)
  3981. "Get the right face for a TODO keyword KWD.
  3982. If KWD is a number, get the corresponding match group."
  3983. (if (numberp kwd) (setq kwd (match-string kwd)))
  3984. (or (cdr (assoc kwd org-tag-faces))
  3985. 'org-tag))
  3986. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3987. "Remove fontification and activation overlays from links."
  3988. (font-lock-default-unfontify-region beg end)
  3989. (let* ((buffer-undo-list t)
  3990. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3991. (inhibit-modification-hooks t)
  3992. deactivate-mark buffer-file-name buffer-file-truename)
  3993. (remove-text-properties beg end
  3994. '(mouse-face t keymap t org-linked-text t
  3995. invisible t intangible t))))
  3996. ;;;; Visibility cycling, including org-goto and indirect buffer
  3997. ;;; Cycling
  3998. (defvar org-cycle-global-status nil)
  3999. (make-variable-buffer-local 'org-cycle-global-status)
  4000. (defvar org-cycle-subtree-status nil)
  4001. (make-variable-buffer-local 'org-cycle-subtree-status)
  4002. ;;;###autoload
  4003. (defvar org-inlinetask-min-level)
  4004. (defun org-cycle (&optional arg)
  4005. "Visibility cycling for Org-mode.
  4006. - When this function is called with a prefix argument, rotate the entire
  4007. buffer through 3 states (global cycling)
  4008. 1. OVERVIEW: Show only top-level headlines.
  4009. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4010. 3. SHOW ALL: Show everything.
  4011. When called with two C-u C-u prefixes, switch to the startup visibility,
  4012. determined by the variable `org-startup-folded', and by any VISIBILITY
  4013. properties in the buffer.
  4014. When called with three C-u C-u C-u prefixed, show the entire buffer,
  4015. including drawers.
  4016. - When point is at the beginning of a headline, rotate the subtree started
  4017. by this line through 3 different states (local cycling)
  4018. 1. FOLDED: Only the main headline is shown.
  4019. 2. CHILDREN: The main headline and the direct children are shown.
  4020. From this state, you can move to one of the children
  4021. and zoom in further.
  4022. 3. SUBTREE: Show the entire subtree, including body text.
  4023. - When there is a numeric prefix, go up to a heading with level ARG, do
  4024. a `show-subtree' and return to the previous cursor position. If ARG
  4025. is negative, go up that many levels.
  4026. - When point is not at the beginning of a headline, execute the global
  4027. binding for TAB, which is re-indenting the line. See the option
  4028. `org-cycle-emulate-tab' for details.
  4029. - Special case: if point is at the beginning of the buffer and there is
  4030. no headline in line 1, this function will act as if called with prefix arg.
  4031. But only if also the variable `org-cycle-global-at-bob' is t."
  4032. (interactive "P")
  4033. (org-load-modules-maybe)
  4034. (let* ((limit-level
  4035. (or org-cycle-max-level
  4036. (and (boundp 'org-inlinetask-min-level)
  4037. org-inlinetask-min-level
  4038. (1- org-inlinetask-min-level))))
  4039. (nstars (and limit-level
  4040. (if org-odd-levels-only
  4041. (and limit-level (1- (* limit-level 2)))
  4042. limit-level)))
  4043. (outline-regexp
  4044. (cond
  4045. ((not (org-mode-p)) outline-regexp)
  4046. (org-cycle-include-plain-lists
  4047. (concat "\\(?:\\*"
  4048. (if nstars (format "\\{1,%d\\} " nstars) "+")
  4049. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4050. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4051. (bob-special (and org-cycle-global-at-bob (bobp)
  4052. (not (looking-at outline-regexp))))
  4053. (org-cycle-hook
  4054. (if bob-special
  4055. (delq 'org-optimize-window-after-visibility-change
  4056. (copy-sequence org-cycle-hook))
  4057. org-cycle-hook))
  4058. (pos (point)))
  4059. (if (or bob-special (equal arg '(4)))
  4060. ;; special case: use global cycling
  4061. (setq arg t))
  4062. (cond
  4063. ((equal arg '(16))
  4064. (org-set-startup-visibility)
  4065. (message "Startup visibility, plus VISIBILITY properties"))
  4066. ((equal arg '(64))
  4067. (show-all)
  4068. (message "Entire buffer visible, including drawers"))
  4069. ((org-at-table-p 'any)
  4070. ;; Enter the table or move to the next field in the table
  4071. (or (org-table-recognize-table.el)
  4072. (progn
  4073. (if arg (org-table-edit-field t)
  4074. (org-table-justify-field-maybe)
  4075. (call-interactively 'org-table-next-field)))))
  4076. ((eq arg t) ;; Global cycling
  4077. (cond
  4078. ((and (eq last-command this-command)
  4079. (eq org-cycle-global-status 'overview))
  4080. ;; We just created the overview - now do table of contents
  4081. ;; This can be slow in very large buffers, so indicate action
  4082. (message "CONTENTS...")
  4083. (org-content)
  4084. (message "CONTENTS...done")
  4085. (setq org-cycle-global-status 'contents)
  4086. (run-hook-with-args 'org-cycle-hook 'contents))
  4087. ((and (eq last-command this-command)
  4088. (eq org-cycle-global-status 'contents))
  4089. ;; We just showed the table of contents - now show everything
  4090. (show-all)
  4091. (message "SHOW ALL")
  4092. (setq org-cycle-global-status 'all)
  4093. (run-hook-with-args 'org-cycle-hook 'all))
  4094. (t
  4095. ;; Default action: go to overview
  4096. (org-overview)
  4097. (message "OVERVIEW")
  4098. (setq org-cycle-global-status 'overview)
  4099. (run-hook-with-args 'org-cycle-hook 'overview))))
  4100. ((and org-drawers org-drawer-regexp
  4101. (save-excursion
  4102. (beginning-of-line 1)
  4103. (looking-at org-drawer-regexp)))
  4104. ;; Toggle block visibility
  4105. (org-flag-drawer
  4106. (not (get-char-property (match-end 0) 'invisible))))
  4107. ((integerp arg)
  4108. ;; Show-subtree, ARG levels up from here.
  4109. (save-excursion
  4110. (org-back-to-heading)
  4111. (outline-up-heading (if (< arg 0) (- arg)
  4112. (- (funcall outline-level) arg)))
  4113. (org-show-subtree)))
  4114. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4115. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4116. ;; At a heading: rotate between three different views
  4117. (org-back-to-heading)
  4118. (let ((goal-column 0) eoh eol eos)
  4119. ;; First, some boundaries
  4120. (save-excursion
  4121. (org-back-to-heading)
  4122. (save-excursion
  4123. (beginning-of-line 2)
  4124. (while (and (not (eobp)) ;; this is like `next-line'
  4125. (get-char-property (1- (point)) 'invisible))
  4126. (beginning-of-line 2)) (setq eol (point)))
  4127. (outline-end-of-heading) (setq eoh (point))
  4128. (org-end-of-subtree t)
  4129. (unless (eobp)
  4130. (skip-chars-forward " \t\n")
  4131. (beginning-of-line 1) ; in case this is an item
  4132. )
  4133. (setq eos (1- (point))))
  4134. ;; Find out what to do next and set `this-command'
  4135. (cond
  4136. ((= eos eoh)
  4137. ;; Nothing is hidden behind this heading
  4138. (message "EMPTY ENTRY")
  4139. (setq org-cycle-subtree-status nil)
  4140. (save-excursion
  4141. (goto-char eos)
  4142. (outline-next-heading)
  4143. (if (org-invisible-p) (org-flag-heading nil))))
  4144. ((or (>= eol eos)
  4145. (not (string-match "\\S-" (buffer-substring eol eos))))
  4146. ;; Entire subtree is hidden in one line: open it
  4147. (org-show-entry)
  4148. (show-children)
  4149. (message "CHILDREN")
  4150. (save-excursion
  4151. (goto-char eos)
  4152. (outline-next-heading)
  4153. (if (org-invisible-p) (org-flag-heading nil)))
  4154. (setq org-cycle-subtree-status 'children)
  4155. (run-hook-with-args 'org-cycle-hook 'children))
  4156. ((and (eq last-command this-command)
  4157. (eq org-cycle-subtree-status 'children))
  4158. ;; We just showed the children, now show everything.
  4159. (org-show-subtree)
  4160. (message "SUBTREE")
  4161. (setq org-cycle-subtree-status 'subtree)
  4162. (run-hook-with-args 'org-cycle-hook 'subtree))
  4163. (t
  4164. ;; Default action: hide the subtree.
  4165. (hide-subtree)
  4166. (message "FOLDED")
  4167. (setq org-cycle-subtree-status 'folded)
  4168. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4169. ;; TAB emulation and template completion
  4170. (buffer-read-only (org-back-to-heading))
  4171. ((org-try-structure-completion))
  4172. ((org-try-cdlatex-tab))
  4173. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4174. (or (not (bolp))
  4175. (not (looking-at outline-regexp))))
  4176. (call-interactively (global-key-binding "\t")))
  4177. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4178. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4179. (or (and (eq org-cycle-emulate-tab 'white)
  4180. (= (match-end 0) (point-at-eol)))
  4181. (and (eq org-cycle-emulate-tab 'whitestart)
  4182. (>= (match-end 0) pos))))
  4183. t
  4184. (eq org-cycle-emulate-tab t))
  4185. (call-interactively (global-key-binding "\t")))
  4186. (t (save-excursion
  4187. (org-back-to-heading)
  4188. (org-cycle))))))
  4189. ;;;###autoload
  4190. (defun org-global-cycle (&optional arg)
  4191. "Cycle the global visibility. For details see `org-cycle'.
  4192. With C-u prefix arg, switch to startup visibility.
  4193. With a numeric prefix, show all headlines up to that level."
  4194. (interactive "P")
  4195. (let ((org-cycle-include-plain-lists
  4196. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4197. (cond
  4198. ((integerp arg)
  4199. (show-all)
  4200. (hide-sublevels arg)
  4201. (setq org-cycle-global-status 'contents))
  4202. ((equal arg '(4))
  4203. (org-set-startup-visibility)
  4204. (message "Startup visibility, plus VISIBILITY properties."))
  4205. (t
  4206. (org-cycle '(4))))))
  4207. (defun org-set-startup-visibility ()
  4208. "Set the visibility required by startup options and properties."
  4209. (cond
  4210. ((eq org-startup-folded t)
  4211. (org-cycle '(4)))
  4212. ((eq org-startup-folded 'content)
  4213. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4214. (org-cycle '(4)) (org-cycle '(4)))))
  4215. (org-set-visibility-according-to-property 'no-cleanup)
  4216. (org-cycle-hide-archived-subtrees 'all)
  4217. (org-cycle-hide-drawers 'all)
  4218. (org-cycle-show-empty-lines 'all))
  4219. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4220. "Switch subtree visibilities according to :VISIBILITY: property."
  4221. (interactive)
  4222. (let (org-show-entry-below state)
  4223. (save-excursion
  4224. (goto-char (point-min))
  4225. (while (re-search-forward
  4226. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4227. nil t)
  4228. (setq state (match-string 1))
  4229. (save-excursion
  4230. (org-back-to-heading t)
  4231. (hide-subtree)
  4232. (org-reveal)
  4233. (cond
  4234. ((equal state '("fold" "folded"))
  4235. (hide-subtree))
  4236. ((equal state "children")
  4237. (org-show-hidden-entry)
  4238. (show-children))
  4239. ((equal state "content")
  4240. (save-excursion
  4241. (save-restriction
  4242. (org-narrow-to-subtree)
  4243. (org-content))))
  4244. ((member state '("all" "showall"))
  4245. (show-subtree)))))
  4246. (unless no-cleanup
  4247. (org-cycle-hide-archived-subtrees 'all)
  4248. (org-cycle-hide-drawers 'all)
  4249. (org-cycle-show-empty-lines 'all)))))
  4250. (defun org-overview ()
  4251. "Switch to overview mode, showing only top-level headlines.
  4252. Really, this shows all headlines with level equal or greater than the level
  4253. of the first headline in the buffer. This is important, because if the
  4254. first headline is not level one, then (hide-sublevels 1) gives confusing
  4255. results."
  4256. (interactive)
  4257. (let ((level (save-excursion
  4258. (goto-char (point-min))
  4259. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4260. (progn
  4261. (goto-char (match-beginning 0))
  4262. (funcall outline-level))))))
  4263. (and level (hide-sublevels level))))
  4264. (defun org-content (&optional arg)
  4265. "Show all headlines in the buffer, like a table of contents.
  4266. With numerical argument N, show content up to level N."
  4267. (interactive "P")
  4268. (save-excursion
  4269. ;; Visit all headings and show their offspring
  4270. (and (integerp arg) (org-overview))
  4271. (goto-char (point-max))
  4272. (catch 'exit
  4273. (while (and (progn (condition-case nil
  4274. (outline-previous-visible-heading 1)
  4275. (error (goto-char (point-min))))
  4276. t)
  4277. (looking-at outline-regexp))
  4278. (if (integerp arg)
  4279. (show-children (1- arg))
  4280. (show-branches))
  4281. (if (bobp) (throw 'exit nil))))))
  4282. (defun org-optimize-window-after-visibility-change (state)
  4283. "Adjust the window after a change in outline visibility.
  4284. This function is the default value of the hook `org-cycle-hook'."
  4285. (when (get-buffer-window (current-buffer))
  4286. (cond
  4287. ((eq state 'content) nil)
  4288. ((eq state 'all) nil)
  4289. ((eq state 'folded) nil)
  4290. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4291. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4292. (defun org-compact-display-after-subtree-move ()
  4293. "Show a compacter version of the tree of the entry's parent."
  4294. (save-excursion
  4295. (if (org-up-heading-safe)
  4296. (progn
  4297. (hide-subtree)
  4298. (show-entry)
  4299. (show-children)
  4300. (org-cycle-show-empty-lines 'children)
  4301. (org-cycle-hide-drawers 'children))
  4302. (org-overview))))
  4303. (defun org-cycle-show-empty-lines (state)
  4304. "Show empty lines above all visible headlines.
  4305. The region to be covered depends on STATE when called through
  4306. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4307. entire buffer covered. Note that an empty line is only shown if there
  4308. are at least `org-cycle-separator-lines' empty lines before the headline."
  4309. (when (> org-cycle-separator-lines 0)
  4310. (save-excursion
  4311. (let* ((n org-cycle-separator-lines)
  4312. (re (cond
  4313. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4314. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4315. (t (let ((ns (number-to-string (- n 2))))
  4316. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4317. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4318. beg end)
  4319. (cond
  4320. ((memq state '(overview contents t))
  4321. (setq beg (point-min) end (point-max)))
  4322. ((memq state '(children folded))
  4323. (setq beg (point) end (progn (org-end-of-subtree t t)
  4324. (beginning-of-line 2)
  4325. (point)))))
  4326. (when beg
  4327. (goto-char beg)
  4328. (while (re-search-forward re end t)
  4329. (if (not (get-char-property (match-end 1) 'invisible))
  4330. (outline-flag-region
  4331. (match-beginning 1) (match-end 1) nil)))))))
  4332. ;; Never hide empty lines at the end of the file.
  4333. (save-excursion
  4334. (goto-char (point-max))
  4335. (outline-previous-heading)
  4336. (outline-end-of-heading)
  4337. (if (and (looking-at "[ \t\n]+")
  4338. (= (match-end 0) (point-max)))
  4339. (outline-flag-region (point) (match-end 0) nil))))
  4340. (defun org-show-empty-lines-in-parent ()
  4341. "Move to the parent and re-show empty lines before visible headlines."
  4342. (save-excursion
  4343. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4344. (org-cycle-show-empty-lines context))))
  4345. (defun org-cycle-hide-drawers (state)
  4346. "Re-hide all drawers after a visibility state change."
  4347. (when (and (org-mode-p)
  4348. (not (memq state '(overview folded))))
  4349. (save-excursion
  4350. (let* ((globalp (memq state '(contents all)))
  4351. (beg (if globalp (point-min) (point)))
  4352. (end (if globalp (point-max) (org-end-of-subtree t))))
  4353. (goto-char beg)
  4354. (while (re-search-forward org-drawer-regexp end t)
  4355. (org-flag-drawer t))))))
  4356. (defun org-flag-drawer (flag)
  4357. (save-excursion
  4358. (beginning-of-line 1)
  4359. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4360. (let ((b (match-end 0))
  4361. (outline-regexp org-outline-regexp))
  4362. (if (re-search-forward
  4363. "^[ \t]*:END:"
  4364. (save-excursion (outline-next-heading) (point)) t)
  4365. (outline-flag-region b (point-at-eol) flag)
  4366. (error ":END: line missing"))))))
  4367. (defun org-subtree-end-visible-p ()
  4368. "Is the end of the current subtree visible?"
  4369. (pos-visible-in-window-p
  4370. (save-excursion (org-end-of-subtree t) (point))))
  4371. (defun org-first-headline-recenter (&optional N)
  4372. "Move cursor to the first headline and recenter the headline.
  4373. Optional argument N means, put the headline into the Nth line of the window."
  4374. (goto-char (point-min))
  4375. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4376. (beginning-of-line)
  4377. (recenter (prefix-numeric-value N))))
  4378. ;;; Org-goto
  4379. (defvar org-goto-window-configuration nil)
  4380. (defvar org-goto-marker nil)
  4381. (defvar org-goto-map
  4382. (let ((map (make-sparse-keymap)))
  4383. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4384. (while (setq cmd (pop cmds))
  4385. (substitute-key-definition cmd cmd map global-map)))
  4386. (suppress-keymap map)
  4387. (org-defkey map "\C-m" 'org-goto-ret)
  4388. (org-defkey map [(return)] 'org-goto-ret)
  4389. (org-defkey map [(left)] 'org-goto-left)
  4390. (org-defkey map [(right)] 'org-goto-right)
  4391. (org-defkey map [(control ?g)] 'org-goto-quit)
  4392. (org-defkey map "\C-i" 'org-cycle)
  4393. (org-defkey map [(tab)] 'org-cycle)
  4394. (org-defkey map [(down)] 'outline-next-visible-heading)
  4395. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4396. (if org-goto-auto-isearch
  4397. (if (fboundp 'define-key-after)
  4398. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4399. nil)
  4400. (org-defkey map "q" 'org-goto-quit)
  4401. (org-defkey map "n" 'outline-next-visible-heading)
  4402. (org-defkey map "p" 'outline-previous-visible-heading)
  4403. (org-defkey map "f" 'outline-forward-same-level)
  4404. (org-defkey map "b" 'outline-backward-same-level)
  4405. (org-defkey map "u" 'outline-up-heading))
  4406. (org-defkey map "/" 'org-occur)
  4407. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4408. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4409. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4410. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4411. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4412. map))
  4413. (defconst org-goto-help
  4414. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4415. RET=jump to location [Q]uit and return to previous location
  4416. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4417. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4418. ;; FIXME: Docstring doe not mention both interfaces
  4419. (defun org-goto (&optional alternative-interface)
  4420. "Look up a different location in the current file, keeping current visibility.
  4421. When you want look-up or go to a different location in a document, the
  4422. fastest way is often to fold the entire buffer and then dive into the tree.
  4423. This method has the disadvantage, that the previous location will be folded,
  4424. which may not be what you want.
  4425. This command works around this by showing a copy of the current buffer
  4426. in an indirect buffer, in overview mode. You can dive into the tree in
  4427. that copy, use org-occur and incremental search to find a location.
  4428. When pressing RET or `Q', the command returns to the original buffer in
  4429. which the visibility is still unchanged. After RET is will also jump to
  4430. the location selected in the indirect buffer and expose the
  4431. the headline hierarchy above."
  4432. (interactive "P")
  4433. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4434. (org-refile-use-outline-path t)
  4435. (interface
  4436. (if (not alternative-interface)
  4437. org-goto-interface
  4438. (if (eq org-goto-interface 'outline)
  4439. 'outline-path-completion
  4440. 'outline)))
  4441. (org-goto-start-pos (point))
  4442. (selected-point
  4443. (if (eq interface 'outline)
  4444. (car (org-get-location (current-buffer) org-goto-help))
  4445. (nth 3 (org-refile-get-location "Goto: ")))))
  4446. (if selected-point
  4447. (progn
  4448. (org-mark-ring-push org-goto-start-pos)
  4449. (goto-char selected-point)
  4450. (if (or (org-invisible-p) (org-invisible-p2))
  4451. (org-show-context 'org-goto)))
  4452. (message "Quit"))))
  4453. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4454. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4455. (defvar org-goto-local-auto-isearch-map) ; defined below
  4456. (defun org-get-location (buf help)
  4457. "Let the user select a location in the Org-mode buffer BUF.
  4458. This function uses a recursive edit. It returns the selected position
  4459. or nil."
  4460. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4461. (isearch-hide-immediately nil)
  4462. (isearch-search-fun-function
  4463. (lambda () 'org-goto-local-search-headings))
  4464. (org-goto-selected-point org-goto-exit-command))
  4465. (save-excursion
  4466. (save-window-excursion
  4467. (delete-other-windows)
  4468. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4469. (switch-to-buffer
  4470. (condition-case nil
  4471. (make-indirect-buffer (current-buffer) "*org-goto*")
  4472. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4473. (with-output-to-temp-buffer "*Help*"
  4474. (princ help))
  4475. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4476. (setq buffer-read-only nil)
  4477. (let ((org-startup-truncated t)
  4478. (org-startup-folded nil)
  4479. (org-startup-align-all-tables nil))
  4480. (org-mode)
  4481. (org-overview))
  4482. (setq buffer-read-only t)
  4483. (if (and (boundp 'org-goto-start-pos)
  4484. (integer-or-marker-p org-goto-start-pos))
  4485. (let ((org-show-hierarchy-above t)
  4486. (org-show-siblings t)
  4487. (org-show-following-heading t))
  4488. (goto-char org-goto-start-pos)
  4489. (and (org-invisible-p) (org-show-context)))
  4490. (goto-char (point-min)))
  4491. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4492. (message "Select location and press RET")
  4493. (use-local-map org-goto-map)
  4494. (recursive-edit)
  4495. ))
  4496. (kill-buffer "*org-goto*")
  4497. (cons org-goto-selected-point org-goto-exit-command)))
  4498. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4499. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4500. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4501. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4502. (defun org-goto-local-search-headings (string bound noerror)
  4503. "Search and make sure that any matches are in headlines."
  4504. (catch 'return
  4505. (while (if isearch-forward
  4506. (search-forward string bound noerror)
  4507. (search-backward string bound noerror))
  4508. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4509. (and (member :headline context)
  4510. (not (member :tags context))))
  4511. (throw 'return (point))))))
  4512. (defun org-goto-local-auto-isearch ()
  4513. "Start isearch."
  4514. (interactive)
  4515. (goto-char (point-min))
  4516. (let ((keys (this-command-keys)))
  4517. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4518. (isearch-mode t)
  4519. (isearch-process-search-char (string-to-char keys)))))
  4520. (defun org-goto-ret (&optional arg)
  4521. "Finish `org-goto' by going to the new location."
  4522. (interactive "P")
  4523. (setq org-goto-selected-point (point)
  4524. org-goto-exit-command 'return)
  4525. (throw 'exit nil))
  4526. (defun org-goto-left ()
  4527. "Finish `org-goto' by going to the new location."
  4528. (interactive)
  4529. (if (org-on-heading-p)
  4530. (progn
  4531. (beginning-of-line 1)
  4532. (setq org-goto-selected-point (point)
  4533. org-goto-exit-command 'left)
  4534. (throw 'exit nil))
  4535. (error "Not on a heading")))
  4536. (defun org-goto-right ()
  4537. "Finish `org-goto' by going to the new location."
  4538. (interactive)
  4539. (if (org-on-heading-p)
  4540. (progn
  4541. (setq org-goto-selected-point (point)
  4542. org-goto-exit-command 'right)
  4543. (throw 'exit nil))
  4544. (error "Not on a heading")))
  4545. (defun org-goto-quit ()
  4546. "Finish `org-goto' without cursor motion."
  4547. (interactive)
  4548. (setq org-goto-selected-point nil)
  4549. (setq org-goto-exit-command 'quit)
  4550. (throw 'exit nil))
  4551. ;;; Indirect buffer display of subtrees
  4552. (defvar org-indirect-dedicated-frame nil
  4553. "This is the frame being used for indirect tree display.")
  4554. (defvar org-last-indirect-buffer nil)
  4555. (defun org-tree-to-indirect-buffer (&optional arg)
  4556. "Create indirect buffer and narrow it to current subtree.
  4557. With numerical prefix ARG, go up to this level and then take that tree.
  4558. If ARG is negative, go up that many levels.
  4559. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4560. indirect buffer previously made with this command, to avoid proliferation of
  4561. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4562. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4563. is kept so that you can work with several indirect buffers at the same time.
  4564. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4565. requests that a new frame be made for the new buffer, so that the dedicated
  4566. frame is not changed."
  4567. (interactive "P")
  4568. (let ((cbuf (current-buffer))
  4569. (cwin (selected-window))
  4570. (pos (point))
  4571. beg end level heading ibuf)
  4572. (save-excursion
  4573. (org-back-to-heading t)
  4574. (when (numberp arg)
  4575. (setq level (org-outline-level))
  4576. (if (< arg 0) (setq arg (+ level arg)))
  4577. (while (> (setq level (org-outline-level)) arg)
  4578. (outline-up-heading 1 t)))
  4579. (setq beg (point)
  4580. heading (org-get-heading))
  4581. (org-end-of-subtree t) (setq end (point)))
  4582. (if (and (buffer-live-p org-last-indirect-buffer)
  4583. (not (eq org-indirect-buffer-display 'new-frame))
  4584. (not arg))
  4585. (kill-buffer org-last-indirect-buffer))
  4586. (setq ibuf (org-get-indirect-buffer cbuf)
  4587. org-last-indirect-buffer ibuf)
  4588. (cond
  4589. ((or (eq org-indirect-buffer-display 'new-frame)
  4590. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4591. (select-frame (make-frame))
  4592. (delete-other-windows)
  4593. (switch-to-buffer ibuf)
  4594. (org-set-frame-title heading))
  4595. ((eq org-indirect-buffer-display 'dedicated-frame)
  4596. (raise-frame
  4597. (select-frame (or (and org-indirect-dedicated-frame
  4598. (frame-live-p org-indirect-dedicated-frame)
  4599. org-indirect-dedicated-frame)
  4600. (setq org-indirect-dedicated-frame (make-frame)))))
  4601. (delete-other-windows)
  4602. (switch-to-buffer ibuf)
  4603. (org-set-frame-title (concat "Indirect: " heading)))
  4604. ((eq org-indirect-buffer-display 'current-window)
  4605. (switch-to-buffer ibuf))
  4606. ((eq org-indirect-buffer-display 'other-window)
  4607. (pop-to-buffer ibuf))
  4608. (t (error "Invalid value.")))
  4609. (if (featurep 'xemacs)
  4610. (save-excursion (org-mode) (turn-on-font-lock)))
  4611. (narrow-to-region beg end)
  4612. (show-all)
  4613. (goto-char pos)
  4614. (and (window-live-p cwin) (select-window cwin))))
  4615. (defun org-get-indirect-buffer (&optional buffer)
  4616. (setq buffer (or buffer (current-buffer)))
  4617. (let ((n 1) (base (buffer-name buffer)) bname)
  4618. (while (buffer-live-p
  4619. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4620. (setq n (1+ n)))
  4621. (condition-case nil
  4622. (make-indirect-buffer buffer bname 'clone)
  4623. (error (make-indirect-buffer buffer bname)))))
  4624. (defun org-set-frame-title (title)
  4625. "Set the title of the current frame to the string TITLE."
  4626. ;; FIXME: how to name a single frame in XEmacs???
  4627. (unless (featurep 'xemacs)
  4628. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4629. ;;;; Structure editing
  4630. ;;; Inserting headlines
  4631. (defun org-previous-line-empty-p ()
  4632. (save-excursion
  4633. (and (not (bobp))
  4634. (or (beginning-of-line 0) t)
  4635. (save-match-data
  4636. (looking-at "[ \t]*$")))))
  4637. (defun org-insert-heading (&optional force-heading)
  4638. "Insert a new heading or item with same depth at point.
  4639. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4640. If point is at the beginning of a headline, insert a sibling before the
  4641. current headline. If point is not at the beginning, do not split the line,
  4642. but create the new headline after the current line."
  4643. (interactive "P")
  4644. (if (= (buffer-size) 0)
  4645. (insert "\n* ")
  4646. (when (or force-heading (not (org-insert-item)))
  4647. (let* ((empty-line-p nil)
  4648. (head (save-excursion
  4649. (condition-case nil
  4650. (progn
  4651. (org-back-to-heading)
  4652. (setq empty-line-p (org-previous-line-empty-p))
  4653. (match-string 0))
  4654. (error "*"))))
  4655. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4656. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4657. pos hide-previous previous-pos)
  4658. (cond
  4659. ((and (org-on-heading-p) (bolp)
  4660. (or (bobp)
  4661. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4662. ;; insert before the current line
  4663. (open-line (if blank 2 1)))
  4664. ((and (bolp)
  4665. (or (bobp)
  4666. (save-excursion
  4667. (backward-char 1) (not (org-invisible-p)))))
  4668. ;; insert right here
  4669. nil)
  4670. (t
  4671. ;; somewhere in the line
  4672. (save-excursion
  4673. (setq previous-pos (point-at-bol))
  4674. (end-of-line)
  4675. (setq hide-previous (org-invisible-p)))
  4676. (and org-insert-heading-respect-content (org-show-subtree))
  4677. (let ((split
  4678. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4679. (save-excursion
  4680. (let ((p (point)))
  4681. (goto-char (point-at-bol))
  4682. (and (looking-at org-complex-heading-regexp)
  4683. (> p (match-beginning 4)))))))
  4684. tags pos)
  4685. (cond
  4686. (org-insert-heading-respect-content
  4687. (org-end-of-subtree nil t)
  4688. (or (bolp) (newline))
  4689. (or (org-previous-line-empty-p)
  4690. (and blank (newline)))
  4691. (open-line 1))
  4692. ((org-on-heading-p)
  4693. (when hide-previous
  4694. (show-children)
  4695. (org-show-entry))
  4696. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4697. (setq tags (and (match-end 2) (match-string 2)))
  4698. (and (match-end 1)
  4699. (delete-region (match-beginning 1) (match-end 1)))
  4700. (setq pos (point-at-bol))
  4701. (or split (end-of-line 1))
  4702. (delete-horizontal-space)
  4703. (newline (if blank 2 1))
  4704. (when tags
  4705. (save-excursion
  4706. (goto-char pos)
  4707. (end-of-line 1)
  4708. (insert " " tags)
  4709. (org-set-tags nil 'align))))
  4710. (t
  4711. (or split (end-of-line 1))
  4712. (newline (if blank 2 1)))))))
  4713. (insert head) (just-one-space)
  4714. (setq pos (point))
  4715. (end-of-line 1)
  4716. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4717. (when (and org-insert-heading-respect-content hide-previous)
  4718. (save-excursion
  4719. (goto-char previous-pos)
  4720. (hide-subtree)))
  4721. (run-hooks 'org-insert-heading-hook)))))
  4722. (defun org-get-heading (&optional no-tags)
  4723. "Return the heading of the current entry, without the stars."
  4724. (save-excursion
  4725. (org-back-to-heading t)
  4726. (if (looking-at
  4727. (if no-tags
  4728. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4729. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4730. (match-string 1) "")))
  4731. (defun org-heading-components ()
  4732. "Return the components of the current heading.
  4733. This is a list with the following elements:
  4734. - the level as an integer
  4735. - the reduced level, different if `org-odd-levels-only' is set.
  4736. - the TODO keyword, or nil
  4737. - the priority character, like ?A, or nil if no priority is given
  4738. - the headline text itself, or the tags string if no headline text
  4739. - the tags string, or nil."
  4740. (save-excursion
  4741. (org-back-to-heading t)
  4742. (if (looking-at org-complex-heading-regexp)
  4743. (list (length (match-string 1))
  4744. (org-reduced-level (length (match-string 1)))
  4745. (org-match-string-no-properties 2)
  4746. (and (match-end 3) (aref (match-string 3) 2))
  4747. (org-match-string-no-properties 4)
  4748. (org-match-string-no-properties 5)))))
  4749. (defun org-insert-heading-after-current ()
  4750. "Insert a new heading with same level as current, after current subtree."
  4751. (interactive)
  4752. (org-back-to-heading)
  4753. (org-insert-heading)
  4754. (org-move-subtree-down)
  4755. (end-of-line 1))
  4756. (defun org-insert-heading-respect-content ()
  4757. (interactive)
  4758. (let ((org-insert-heading-respect-content t))
  4759. (org-insert-heading t)))
  4760. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4761. (interactive "P")
  4762. (let ((org-insert-heading-respect-content t))
  4763. (org-insert-todo-heading force-state t)))
  4764. (defun org-insert-todo-heading (arg &optional force-heading)
  4765. "Insert a new heading with the same level and TODO state as current heading.
  4766. If the heading has no TODO state, or if the state is DONE, use the first
  4767. state (TODO by default). Also with prefix arg, force first state."
  4768. (interactive "P")
  4769. (when (or force-heading (not (org-insert-item 'checkbox)))
  4770. (org-insert-heading force-heading)
  4771. (save-excursion
  4772. (org-back-to-heading)
  4773. (outline-previous-heading)
  4774. (looking-at org-todo-line-regexp))
  4775. (let*
  4776. ((new-mark-x
  4777. (if (or arg
  4778. (not (match-beginning 2))
  4779. (member (match-string 2) org-done-keywords))
  4780. (car org-todo-keywords-1)
  4781. (match-string 2)))
  4782. (new-mark
  4783. (or
  4784. (run-hook-with-args-until-success
  4785. 'org-todo-get-default-hook new-mark-x nil)
  4786. new-mark-x)))
  4787. (beginning-of-line 1)
  4788. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  4789. (insert new-mark " ")))
  4790. (when org-provide-todo-statistics
  4791. (org-update-parent-todo-statistics))))
  4792. (defun org-insert-subheading (arg)
  4793. "Insert a new subheading and demote it.
  4794. Works for outline headings and for plain lists alike."
  4795. (interactive "P")
  4796. (org-insert-heading arg)
  4797. (cond
  4798. ((org-on-heading-p) (org-do-demote))
  4799. ((org-at-item-p) (org-indent-item 1))))
  4800. (defun org-insert-todo-subheading (arg)
  4801. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4802. Works for outline headings and for plain lists alike."
  4803. (interactive "P")
  4804. (org-insert-todo-heading arg)
  4805. (cond
  4806. ((org-on-heading-p) (org-do-demote))
  4807. ((org-at-item-p) (org-indent-item 1))))
  4808. ;;; Promotion and Demotion
  4809. (defun org-promote-subtree ()
  4810. "Promote the entire subtree.
  4811. See also `org-promote'."
  4812. (interactive)
  4813. (save-excursion
  4814. (org-map-tree 'org-promote))
  4815. (org-fix-position-after-promote))
  4816. (defun org-demote-subtree ()
  4817. "Demote the entire subtree. See `org-demote'.
  4818. See also `org-promote'."
  4819. (interactive)
  4820. (save-excursion
  4821. (org-map-tree 'org-demote))
  4822. (org-fix-position-after-promote))
  4823. (defun org-do-promote ()
  4824. "Promote the current heading higher up the tree.
  4825. If the region is active in `transient-mark-mode', promote all headings
  4826. in the region."
  4827. (interactive)
  4828. (save-excursion
  4829. (if (org-region-active-p)
  4830. (org-map-region 'org-promote (region-beginning) (region-end))
  4831. (org-promote)))
  4832. (org-fix-position-after-promote))
  4833. (defun org-do-demote ()
  4834. "Demote the current heading lower down the tree.
  4835. If the region is active in `transient-mark-mode', demote all headings
  4836. in the region."
  4837. (interactive)
  4838. (save-excursion
  4839. (if (org-region-active-p)
  4840. (org-map-region 'org-demote (region-beginning) (region-end))
  4841. (org-demote)))
  4842. (org-fix-position-after-promote))
  4843. (defun org-fix-position-after-promote ()
  4844. "Make sure that after pro/demotion cursor position is right."
  4845. (let ((pos (point)))
  4846. (when (save-excursion
  4847. (beginning-of-line 1)
  4848. (looking-at org-todo-line-regexp)
  4849. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4850. (cond ((eobp) (insert " "))
  4851. ((eolp) (insert " "))
  4852. ((equal (char-after) ?\ ) (forward-char 1))))))
  4853. (defun org-reduced-level (l)
  4854. "Compute the effective level of a heading.
  4855. This takes into account the setting of `org-odd-levels-only'."
  4856. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4857. (defun org-get-valid-level (level &optional change)
  4858. "Rectify a level change under the influence of `org-odd-levels-only'
  4859. LEVEL is a current level, CHANGE is by how much the level should be
  4860. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4861. even level numbers will become the next higher odd number."
  4862. (if org-odd-levels-only
  4863. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4864. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4865. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4866. (max 1 (+ level (or change 0)))))
  4867. (if (boundp 'define-obsolete-function-alias)
  4868. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4869. (define-obsolete-function-alias 'org-get-legal-level
  4870. 'org-get-valid-level)
  4871. (define-obsolete-function-alias 'org-get-legal-level
  4872. 'org-get-valid-level "23.1")))
  4873. (defun org-promote ()
  4874. "Promote the current heading higher up the tree.
  4875. If the region is active in `transient-mark-mode', promote all headings
  4876. in the region."
  4877. (org-back-to-heading t)
  4878. (let* ((level (save-match-data (funcall outline-level)))
  4879. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4880. (diff (abs (- level (length up-head) -1))))
  4881. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4882. (replace-match up-head nil t)
  4883. ;; Fixup tag positioning
  4884. (and org-auto-align-tags (org-set-tags nil t))
  4885. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4886. (defun org-demote ()
  4887. "Demote the current heading lower down the tree.
  4888. If the region is active in `transient-mark-mode', demote all headings
  4889. in the region."
  4890. (org-back-to-heading t)
  4891. (let* ((level (save-match-data (funcall outline-level)))
  4892. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4893. (diff (abs (- level (length down-head) -1))))
  4894. (replace-match down-head nil t)
  4895. ;; Fixup tag positioning
  4896. (and org-auto-align-tags (org-set-tags nil t))
  4897. (if org-adapt-indentation (org-fixup-indentation diff))))
  4898. (defun org-map-tree (fun)
  4899. "Call FUN for every heading underneath the current one."
  4900. (org-back-to-heading)
  4901. (let ((level (funcall outline-level)))
  4902. (save-excursion
  4903. (funcall fun)
  4904. (while (and (progn
  4905. (outline-next-heading)
  4906. (> (funcall outline-level) level))
  4907. (not (eobp)))
  4908. (funcall fun)))))
  4909. (defun org-map-region (fun beg end)
  4910. "Call FUN for every heading between BEG and END."
  4911. (let ((org-ignore-region t))
  4912. (save-excursion
  4913. (setq end (copy-marker end))
  4914. (goto-char beg)
  4915. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4916. (< (point) end))
  4917. (funcall fun))
  4918. (while (and (progn
  4919. (outline-next-heading)
  4920. (< (point) end))
  4921. (not (eobp)))
  4922. (funcall fun)))))
  4923. (defun org-fixup-indentation (diff)
  4924. "Change the indentation in the current entry by DIFF
  4925. However, if any line in the current entry has no indentation, or if it
  4926. would end up with no indentation after the change, nothing at all is done."
  4927. (save-excursion
  4928. (let ((end (save-excursion (outline-next-heading)
  4929. (point-marker)))
  4930. (prohibit (if (> diff 0)
  4931. "^\\S-"
  4932. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4933. col)
  4934. (unless (save-excursion (end-of-line 1)
  4935. (re-search-forward prohibit end t))
  4936. (while (and (< (point) end)
  4937. (re-search-forward "^[ \t]+" end t))
  4938. (goto-char (match-end 0))
  4939. (setq col (current-column))
  4940. (if (< diff 0) (replace-match ""))
  4941. (org-indent-to-column (+ diff col))))
  4942. (move-marker end nil))))
  4943. (defun org-convert-to-odd-levels ()
  4944. "Convert an org-mode file with all levels allowed to one with odd levels.
  4945. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4946. level 5 etc."
  4947. (interactive)
  4948. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4949. (let ((org-odd-levels-only nil) n)
  4950. (save-excursion
  4951. (goto-char (point-min))
  4952. (while (re-search-forward "^\\*\\*+ " nil t)
  4953. (setq n (- (length (match-string 0)) 2))
  4954. (while (>= (setq n (1- n)) 0)
  4955. (org-demote))
  4956. (end-of-line 1))))))
  4957. (defun org-convert-to-oddeven-levels ()
  4958. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4959. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4960. section with an even level, conversion would destroy the structure of the file. An error
  4961. is signaled in this case."
  4962. (interactive)
  4963. (goto-char (point-min))
  4964. ;; First check if there are no even levels
  4965. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4966. (org-show-context t)
  4967. (error "Not all levels are odd in this file. Conversion not possible."))
  4968. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4969. (let ((org-odd-levels-only nil) n)
  4970. (save-excursion
  4971. (goto-char (point-min))
  4972. (while (re-search-forward "^\\*\\*+ " nil t)
  4973. (setq n (/ (1- (length (match-string 0))) 2))
  4974. (while (>= (setq n (1- n)) 0)
  4975. (org-promote))
  4976. (end-of-line 1))))))
  4977. (defun org-tr-level (n)
  4978. "Make N odd if required."
  4979. (if org-odd-levels-only (1+ (/ n 2)) n))
  4980. ;;; Vertical tree motion, cutting and pasting of subtrees
  4981. (defun org-move-subtree-up (&optional arg)
  4982. "Move the current subtree up past ARG headlines of the same level."
  4983. (interactive "p")
  4984. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4985. (defun org-move-subtree-down (&optional arg)
  4986. "Move the current subtree down past ARG headlines of the same level."
  4987. (interactive "p")
  4988. (setq arg (prefix-numeric-value arg))
  4989. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4990. 'outline-get-last-sibling))
  4991. (ins-point (make-marker))
  4992. (cnt (abs arg))
  4993. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4994. ;; Select the tree
  4995. (org-back-to-heading)
  4996. (setq beg0 (point))
  4997. (save-excursion
  4998. (setq ne-beg (org-back-over-empty-lines))
  4999. (setq beg (point)))
  5000. (save-match-data
  5001. (save-excursion (outline-end-of-heading)
  5002. (setq folded (org-invisible-p)))
  5003. (outline-end-of-subtree))
  5004. (outline-next-heading)
  5005. (setq ne-end (org-back-over-empty-lines))
  5006. (setq end (point))
  5007. (goto-char beg0)
  5008. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5009. ;; include less whitespace
  5010. (save-excursion
  5011. (goto-char beg)
  5012. (forward-line (- ne-beg ne-end))
  5013. (setq beg (point))))
  5014. ;; Find insertion point, with error handling
  5015. (while (> cnt 0)
  5016. (or (and (funcall movfunc) (looking-at outline-regexp))
  5017. (progn (goto-char beg0)
  5018. (error "Cannot move past superior level or buffer limit")))
  5019. (setq cnt (1- cnt)))
  5020. (if (> arg 0)
  5021. ;; Moving forward - still need to move over subtree
  5022. (progn (org-end-of-subtree t t)
  5023. (save-excursion
  5024. (org-back-over-empty-lines)
  5025. (or (bolp) (newline)))))
  5026. (setq ne-ins (org-back-over-empty-lines))
  5027. (move-marker ins-point (point))
  5028. (setq txt (buffer-substring beg end))
  5029. (org-save-markers-in-region beg end)
  5030. (delete-region beg end)
  5031. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5032. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5033. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5034. (let ((bbb (point)))
  5035. (insert-before-markers txt)
  5036. (org-reinstall-markers-in-region bbb)
  5037. (move-marker ins-point bbb))
  5038. (or (bolp) (insert "\n"))
  5039. (setq ins-end (point))
  5040. (goto-char ins-point)
  5041. (org-skip-whitespace)
  5042. (when (and (< arg 0)
  5043. (org-first-sibling-p)
  5044. (> ne-ins ne-beg))
  5045. ;; Move whitespace back to beginning
  5046. (save-excursion
  5047. (goto-char ins-end)
  5048. (let ((kill-whole-line t))
  5049. (kill-line (- ne-ins ne-beg)) (point)))
  5050. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5051. (move-marker ins-point nil)
  5052. (org-compact-display-after-subtree-move)
  5053. (org-show-empty-lines-in-parent)
  5054. (unless folded
  5055. (org-show-entry)
  5056. (show-children)
  5057. (org-cycle-hide-drawers 'children))))
  5058. (defvar org-subtree-clip ""
  5059. "Clipboard for cut and paste of subtrees.
  5060. This is actually only a copy of the kill, because we use the normal kill
  5061. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5062. (defvar org-subtree-clip-folded nil
  5063. "Was the last copied subtree folded?
  5064. This is used to fold the tree back after pasting.")
  5065. (defun org-cut-subtree (&optional n)
  5066. "Cut the current subtree into the clipboard.
  5067. With prefix arg N, cut this many sequential subtrees.
  5068. This is a short-hand for marking the subtree and then cutting it."
  5069. (interactive "p")
  5070. (org-copy-subtree n 'cut))
  5071. (defun org-copy-subtree (&optional n cut force-store-markers)
  5072. "Cut the current subtree into the clipboard.
  5073. With prefix arg N, cut this many sequential subtrees.
  5074. This is a short-hand for marking the subtree and then copying it.
  5075. If CUT is non-nil, actually cut the subtree.
  5076. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5077. of some markers in the region, even if CUT is non-nil. This is
  5078. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5079. (interactive "p")
  5080. (let (beg end folded (beg0 (point)))
  5081. (if (interactive-p)
  5082. (org-back-to-heading nil) ; take what looks like a subtree
  5083. (org-back-to-heading t)) ; take what is really there
  5084. (org-back-over-empty-lines)
  5085. (setq beg (point))
  5086. (skip-chars-forward " \t\r\n")
  5087. (save-match-data
  5088. (save-excursion (outline-end-of-heading)
  5089. (setq folded (org-invisible-p)))
  5090. (condition-case nil
  5091. (outline-forward-same-level (1- n))
  5092. (error nil))
  5093. (org-end-of-subtree t t))
  5094. (org-back-over-empty-lines)
  5095. (setq end (point))
  5096. (goto-char beg0)
  5097. (when (> end beg)
  5098. (setq org-subtree-clip-folded folded)
  5099. (when (or cut force-store-markers)
  5100. (org-save-markers-in-region beg end))
  5101. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5102. (setq org-subtree-clip (current-kill 0))
  5103. (message "%s: Subtree(s) with %d characters"
  5104. (if cut "Cut" "Copied")
  5105. (length org-subtree-clip)))))
  5106. (defun org-paste-subtree (&optional level tree for-yank)
  5107. "Paste the clipboard as a subtree, with modification of headline level.
  5108. The entire subtree is promoted or demoted in order to match a new headline
  5109. level.
  5110. If the cursor is at the beginning of a headline, the same level as
  5111. that headline is used to paste the tree
  5112. If not, the new level is derived from the *visible* headings
  5113. before and after the insertion point, and taken to be the inferior headline
  5114. level of the two. So if the previous visible heading is level 3 and the
  5115. next is level 4 (or vice versa), level 4 will be used for insertion.
  5116. This makes sure that the subtree remains an independent subtree and does
  5117. not swallow low level entries.
  5118. You can also force a different level, either by using a numeric prefix
  5119. argument, or by inserting the heading marker by hand. For example, if the
  5120. cursor is after \"*****\", then the tree will be shifted to level 5.
  5121. If optional TREE is given, use this text instead of the kill ring.
  5122. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5123. move back over whitespace before inserting, and move point to the end of
  5124. the inserted text when done."
  5125. (interactive "P")
  5126. (unless (org-kill-is-subtree-p tree)
  5127. (error "%s"
  5128. (substitute-command-keys
  5129. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5130. (let* ((visp (not (org-invisible-p)))
  5131. (txt (or tree (and kill-ring (current-kill 0))))
  5132. (^re (concat "^\\(" outline-regexp "\\)"))
  5133. (re (concat "\\(" outline-regexp "\\)"))
  5134. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5135. (old-level (if (string-match ^re txt)
  5136. (- (match-end 0) (match-beginning 0) 1)
  5137. -1))
  5138. (force-level (cond (level (prefix-numeric-value level))
  5139. ((and (looking-at "[ \t]*$")
  5140. (string-match
  5141. ^re_ (buffer-substring
  5142. (point-at-bol) (point))))
  5143. (- (match-end 1) (match-beginning 1)))
  5144. ((and (bolp)
  5145. (looking-at org-outline-regexp))
  5146. (- (match-end 0) (point) 1))
  5147. (t nil)))
  5148. (previous-level (save-excursion
  5149. (condition-case nil
  5150. (progn
  5151. (outline-previous-visible-heading 1)
  5152. (if (looking-at re)
  5153. (- (match-end 0) (match-beginning 0) 1)
  5154. 1))
  5155. (error 1))))
  5156. (next-level (save-excursion
  5157. (condition-case nil
  5158. (progn
  5159. (or (looking-at outline-regexp)
  5160. (outline-next-visible-heading 1))
  5161. (if (looking-at re)
  5162. (- (match-end 0) (match-beginning 0) 1)
  5163. 1))
  5164. (error 1))))
  5165. (new-level (or force-level (max previous-level next-level)))
  5166. (shift (if (or (= old-level -1)
  5167. (= new-level -1)
  5168. (= old-level new-level))
  5169. 0
  5170. (- new-level old-level)))
  5171. (delta (if (> shift 0) -1 1))
  5172. (func (if (> shift 0) 'org-demote 'org-promote))
  5173. (org-odd-levels-only nil)
  5174. beg end newend)
  5175. ;; Remove the forced level indicator
  5176. (if force-level
  5177. (delete-region (point-at-bol) (point)))
  5178. ;; Paste
  5179. (beginning-of-line 1)
  5180. (unless for-yank (org-back-over-empty-lines))
  5181. (setq beg (point))
  5182. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5183. (insert-before-markers txt)
  5184. (unless (string-match "\n\\'" txt) (insert "\n"))
  5185. (setq newend (point))
  5186. (org-reinstall-markers-in-region beg)
  5187. (setq end (point))
  5188. (goto-char beg)
  5189. (skip-chars-forward " \t\n\r")
  5190. (setq beg (point))
  5191. (if (and (org-invisible-p) visp)
  5192. (save-excursion (outline-show-heading)))
  5193. ;; Shift if necessary
  5194. (unless (= shift 0)
  5195. (save-restriction
  5196. (narrow-to-region beg end)
  5197. (while (not (= shift 0))
  5198. (org-map-region func (point-min) (point-max))
  5199. (setq shift (+ delta shift)))
  5200. (goto-char (point-min))
  5201. (setq newend (point-max))))
  5202. (when (or (interactive-p) for-yank)
  5203. (message "Clipboard pasted as level %d subtree" new-level))
  5204. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5205. kill-ring
  5206. (eq org-subtree-clip (current-kill 0))
  5207. org-subtree-clip-folded)
  5208. ;; The tree was folded before it was killed/copied
  5209. (hide-subtree))
  5210. (and for-yank (goto-char newend))))
  5211. (defun org-kill-is-subtree-p (&optional txt)
  5212. "Check if the current kill is an outline subtree, or a set of trees.
  5213. Returns nil if kill does not start with a headline, or if the first
  5214. headline level is not the largest headline level in the tree.
  5215. So this will actually accept several entries of equal levels as well,
  5216. which is OK for `org-paste-subtree'.
  5217. If optional TXT is given, check this string instead of the current kill."
  5218. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5219. (start-level (and kill
  5220. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5221. org-outline-regexp "\\)")
  5222. kill)
  5223. (- (match-end 2) (match-beginning 2) 1)))
  5224. (re (concat "^" org-outline-regexp))
  5225. (start (1+ (or (match-beginning 2) -1))))
  5226. (if (not start-level)
  5227. (progn
  5228. nil) ;; does not even start with a heading
  5229. (catch 'exit
  5230. (while (setq start (string-match re kill (1+ start)))
  5231. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5232. (throw 'exit nil)))
  5233. t))))
  5234. (defvar org-markers-to-move nil
  5235. "Markers that should be moved with a cut-and-paste operation.
  5236. Those markers are stored together with their positions relative to
  5237. the start of the region.")
  5238. (defun org-save-markers-in-region (beg end)
  5239. "Check markers in region.
  5240. If these markers are between BEG and END, record their position relative
  5241. to BEG, so that after moving the block of text, we can put the markers back
  5242. into place.
  5243. This function gets called just before an entry or tree gets cut from the
  5244. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5245. called immediately, to move the markers with the entries."
  5246. (setq org-markers-to-move nil)
  5247. (when (featurep 'org-clock)
  5248. (org-clock-save-markers-for-cut-and-paste beg end))
  5249. (when (featurep 'org-agenda)
  5250. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5251. (defun org-check-and-save-marker (marker beg end)
  5252. "Check if MARKER is between BEG and END.
  5253. If yes, remember the marker and the distance to BEG."
  5254. (when (and (marker-buffer marker)
  5255. (equal (marker-buffer marker) (current-buffer)))
  5256. (if (and (>= marker beg) (< marker end))
  5257. (push (cons marker (- marker beg)) org-markers-to-move))))
  5258. (defun org-reinstall-markers-in-region (beg)
  5259. "Move all remembered markers to their position relative to BEG."
  5260. (mapc (lambda (x)
  5261. (move-marker (car x) (+ beg (cdr x))))
  5262. org-markers-to-move)
  5263. (setq org-markers-to-move nil))
  5264. (defun org-narrow-to-subtree ()
  5265. "Narrow buffer to the current subtree."
  5266. (interactive)
  5267. (save-excursion
  5268. (save-match-data
  5269. (narrow-to-region
  5270. (progn (org-back-to-heading) (point))
  5271. (progn (org-end-of-subtree t) (point))))))
  5272. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5273. "Clone the task (subtree) at point N times.
  5274. The clones will be inserted as siblings.
  5275. In interactive use, the user will be prompted for the number of clones
  5276. to be produced, and for a time SHIFT, which may be a repeater as used
  5277. in time stamps, for example `+3d'.
  5278. When a valid repeater is given and the entry contains any time stamps,
  5279. the clones will become a sequence in time, with time stamps in the
  5280. subtree shifted for each clone produced. If SHIFT is nil or the
  5281. empty string, time stamps will be left alone.
  5282. If the original subtree did contain time stamps with a repeater,
  5283. the following will happen:
  5284. - the repeater will be removed in each clone
  5285. - an additional clone will be produced, with the current, unshifted
  5286. date(s) in the entry.
  5287. - the original entry will be placed *after* all the clones, with
  5288. repeater intact.
  5289. - the start days in the repeater in the original entry will be shifted
  5290. to past the last clone.
  5291. I this way you can spell out a number of instances of a repeating task,
  5292. and still retain the repeater to cover future instances of the task."
  5293. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5294. (let (beg end template task
  5295. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5296. (if (not (and (integerp n) (> n 0)))
  5297. (error "Invalid number of replications %s" n))
  5298. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5299. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5300. shift)))
  5301. (error "Invalid shift specification %s" shift))
  5302. (when doshift
  5303. (setq shift-n (string-to-number (match-string 1 shift))
  5304. shift-what (cdr (assoc (match-string 2 shift)
  5305. '(("d" . day) ("w" . week)
  5306. ("m" . month) ("y" . year))))))
  5307. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5308. (setq nmin 1 nmax n)
  5309. (org-back-to-heading t)
  5310. (setq beg (point))
  5311. (org-end-of-subtree t t)
  5312. (setq end (point))
  5313. (setq template (buffer-substring beg end))
  5314. (when (and doshift
  5315. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5316. (delete-region beg end)
  5317. (setq end beg)
  5318. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5319. (goto-char end)
  5320. (loop for n from nmin to nmax do
  5321. (if (not doshift)
  5322. (setq task template)
  5323. (with-temp-buffer
  5324. (insert template)
  5325. (org-mode)
  5326. (goto-char (point-min))
  5327. (while (re-search-forward org-ts-regexp nil t)
  5328. (org-timestamp-change (* n shift-n) shift-what))
  5329. (unless (= n n-no-remove)
  5330. (goto-char (point-min))
  5331. (while (re-search-forward org-ts-regexp nil t)
  5332. (save-excursion
  5333. (goto-char (match-beginning 0))
  5334. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5335. (delete-region (match-beginning 1) (match-end 1))))))
  5336. (setq task (buffer-string))))
  5337. (insert task))
  5338. (goto-char beg)))
  5339. ;;; Outline Sorting
  5340. (defun org-sort (with-case)
  5341. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5342. Optional argument WITH-CASE means sort case-sensitively.
  5343. With a double prefix argument, also remove duplicate entries."
  5344. (interactive "P")
  5345. (if (org-at-table-p)
  5346. (org-call-with-arg 'org-table-sort-lines with-case)
  5347. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5348. (defun org-sort-remove-invisible (s)
  5349. (remove-text-properties 0 (length s) org-rm-props s)
  5350. (while (string-match org-bracket-link-regexp s)
  5351. (setq s (replace-match (if (match-end 2)
  5352. (match-string 3 s)
  5353. (match-string 1 s)) t t s)))
  5354. s)
  5355. (defvar org-priority-regexp) ; defined later in the file
  5356. (defun org-sort-entries-or-items
  5357. (&optional with-case sorting-type getkey-func compare-func property)
  5358. "Sort entries on a certain level of an outline tree.
  5359. If there is an active region, the entries in the region are sorted.
  5360. Else, if the cursor is before the first entry, sort the top-level items.
  5361. Else, the children of the entry at point are sorted.
  5362. Sorting can be alphabetically, numerically, and by date/time as given by
  5363. the first time stamp in the entry. The command prompts for the sorting
  5364. type unless it has been given to the function through the SORTING-TYPE
  5365. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5366. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5367. called with point at the beginning of the record. It must return either
  5368. a string or a number that should serve as the sorting key for that record.
  5369. Comparing entries ignores case by default. However, with an optional argument
  5370. WITH-CASE, the sorting considers case as well."
  5371. (interactive "P")
  5372. (let ((case-func (if with-case 'identity 'downcase))
  5373. start beg end stars re re2
  5374. txt what tmp plain-list-p)
  5375. ;; Find beginning and end of region to sort
  5376. (cond
  5377. ((org-region-active-p)
  5378. ;; we will sort the region
  5379. (setq end (region-end)
  5380. what "region")
  5381. (goto-char (region-beginning))
  5382. (if (not (org-on-heading-p)) (outline-next-heading))
  5383. (setq start (point)))
  5384. ((org-at-item-p)
  5385. ;; we will sort this plain list
  5386. (org-beginning-of-item-list) (setq start (point))
  5387. (org-end-of-item-list) (setq end (point))
  5388. (goto-char start)
  5389. (setq plain-list-p t
  5390. what "plain list"))
  5391. ((or (org-on-heading-p)
  5392. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5393. ;; we will sort the children of the current headline
  5394. (org-back-to-heading)
  5395. (setq start (point)
  5396. end (progn (org-end-of-subtree t t)
  5397. (org-back-over-empty-lines)
  5398. (point))
  5399. what "children")
  5400. (goto-char start)
  5401. (show-subtree)
  5402. (outline-next-heading))
  5403. (t
  5404. ;; we will sort the top-level entries in this file
  5405. (goto-char (point-min))
  5406. (or (org-on-heading-p) (outline-next-heading))
  5407. (setq start (point) end (point-max) what "top-level")
  5408. (goto-char start)
  5409. (show-all)))
  5410. (setq beg (point))
  5411. (if (>= beg end) (error "Nothing to sort"))
  5412. (unless plain-list-p
  5413. (looking-at "\\(\\*+\\)")
  5414. (setq stars (match-string 1)
  5415. re (concat "^" (regexp-quote stars) " +")
  5416. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5417. txt (buffer-substring beg end))
  5418. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5419. (if (and (not (equal stars "*")) (string-match re2 txt))
  5420. (error "Region to sort contains a level above the first entry")))
  5421. (unless sorting-type
  5422. (message
  5423. (if plain-list-p
  5424. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5425. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5426. what)
  5427. (setq sorting-type (read-char-exclusive))
  5428. (and (= (downcase sorting-type) ?f)
  5429. (setq getkey-func
  5430. (org-ido-completing-read "Sort using function: "
  5431. obarray 'fboundp t nil nil))
  5432. (setq getkey-func (intern getkey-func)))
  5433. (and (= (downcase sorting-type) ?r)
  5434. (setq property
  5435. (org-ido-completing-read "Property: "
  5436. (mapcar 'list (org-buffer-property-keys t))
  5437. nil t))))
  5438. (message "Sorting entries...")
  5439. (save-restriction
  5440. (narrow-to-region start end)
  5441. (let ((dcst (downcase sorting-type))
  5442. (now (current-time)))
  5443. (sort-subr
  5444. (/= dcst sorting-type)
  5445. ;; This function moves to the beginning character of the "record" to
  5446. ;; be sorted.
  5447. (if plain-list-p
  5448. (lambda nil
  5449. (if (org-at-item-p) t (goto-char (point-max))))
  5450. (lambda nil
  5451. (if (re-search-forward re nil t)
  5452. (goto-char (match-beginning 0))
  5453. (goto-char (point-max)))))
  5454. ;; This function moves to the last character of the "record" being
  5455. ;; sorted.
  5456. (if plain-list-p
  5457. 'org-end-of-item
  5458. (lambda nil
  5459. (save-match-data
  5460. (condition-case nil
  5461. (outline-forward-same-level 1)
  5462. (error
  5463. (goto-char (point-max)))))))
  5464. ;; This function returns the value that gets sorted against.
  5465. (if plain-list-p
  5466. (lambda nil
  5467. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5468. (cond
  5469. ((= dcst ?n)
  5470. (string-to-number (buffer-substring (match-end 0)
  5471. (point-at-eol))))
  5472. ((= dcst ?a)
  5473. (buffer-substring (match-end 0) (point-at-eol)))
  5474. ((= dcst ?t)
  5475. (if (re-search-forward org-ts-regexp
  5476. (point-at-eol) t)
  5477. (org-time-string-to-time (match-string 0))
  5478. now))
  5479. ((= dcst ?f)
  5480. (if getkey-func
  5481. (progn
  5482. (setq tmp (funcall getkey-func))
  5483. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5484. tmp)
  5485. (error "Invalid key function `%s'" getkey-func)))
  5486. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5487. (lambda nil
  5488. (cond
  5489. ((= dcst ?n)
  5490. (if (looking-at org-complex-heading-regexp)
  5491. (string-to-number (match-string 4))
  5492. nil))
  5493. ((= dcst ?a)
  5494. (if (looking-at org-complex-heading-regexp)
  5495. (funcall case-func (match-string 4))
  5496. nil))
  5497. ((= dcst ?t)
  5498. (if (re-search-forward org-ts-regexp
  5499. (save-excursion
  5500. (forward-line 2)
  5501. (point)) t)
  5502. (org-time-string-to-time (match-string 0))
  5503. now))
  5504. ((= dcst ?p)
  5505. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5506. (string-to-char (match-string 2))
  5507. org-default-priority))
  5508. ((= dcst ?r)
  5509. (or (org-entry-get nil property) ""))
  5510. ((= dcst ?o)
  5511. (if (looking-at org-complex-heading-regexp)
  5512. (- 9999 (length (member (match-string 2)
  5513. org-todo-keywords-1)))))
  5514. ((= dcst ?f)
  5515. (if getkey-func
  5516. (progn
  5517. (setq tmp (funcall getkey-func))
  5518. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5519. tmp)
  5520. (error "Invalid key function `%s'" getkey-func)))
  5521. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5522. nil
  5523. (cond
  5524. ((= dcst ?a) 'string<)
  5525. ((= dcst ?t) 'time-less-p)
  5526. ((= dcst ?f) compare-func)
  5527. (t nil)))))
  5528. (message "Sorting entries...done")))
  5529. (defun org-do-sort (table what &optional with-case sorting-type)
  5530. "Sort TABLE of WHAT according to SORTING-TYPE.
  5531. The user will be prompted for the SORTING-TYPE if the call to this
  5532. function does not specify it. WHAT is only for the prompt, to indicate
  5533. what is being sorted. The sorting key will be extracted from
  5534. the car of the elements of the table.
  5535. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5536. (unless sorting-type
  5537. (message
  5538. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5539. what)
  5540. (setq sorting-type (read-char-exclusive)))
  5541. (let ((dcst (downcase sorting-type))
  5542. extractfun comparefun)
  5543. ;; Define the appropriate functions
  5544. (cond
  5545. ((= dcst ?n)
  5546. (setq extractfun 'string-to-number
  5547. comparefun (if (= dcst sorting-type) '< '>)))
  5548. ((= dcst ?a)
  5549. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5550. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5551. comparefun (if (= dcst sorting-type)
  5552. 'string<
  5553. (lambda (a b) (and (not (string< a b))
  5554. (not (string= a b)))))))
  5555. ((= dcst ?t)
  5556. (setq extractfun
  5557. (lambda (x)
  5558. (if (or (string-match org-ts-regexp x)
  5559. (string-match org-ts-regexp-both x))
  5560. (time-to-seconds
  5561. (org-time-string-to-time (match-string 0 x)))
  5562. 0))
  5563. comparefun (if (= dcst sorting-type) '< '>)))
  5564. (t (error "Invalid sorting type `%c'" sorting-type)))
  5565. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5566. table)
  5567. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5568. ;;; Editing source examples
  5569. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5570. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5571. (defvar org-edit-src-force-single-line nil)
  5572. (defvar org-edit-src-from-org-mode nil)
  5573. (defvar org-edit-src-picture nil)
  5574. (define-minor-mode org-exit-edit-mode
  5575. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5576. (defun org-edit-src-code ()
  5577. "Edit the source code example at point.
  5578. An indirect buffer is created, and that buffer is then narrowed to the
  5579. example at point and switched to the correct language mode. When done,
  5580. exit by killing the buffer with \\[org-edit-src-exit]."
  5581. (interactive)
  5582. (let ((line (org-current-line))
  5583. (case-fold-search t)
  5584. (msg (substitute-command-keys
  5585. "Edit, then exit with C-c ' (C-c and single quote)"))
  5586. (info (org-edit-src-find-region-and-lang))
  5587. (org-mode-p (eq major-mode 'org-mode))
  5588. beg end lang lang-f single lfmt)
  5589. (if (not info)
  5590. nil
  5591. (setq beg (nth 0 info)
  5592. end (nth 1 info)
  5593. lang (nth 2 info)
  5594. single (nth 3 info)
  5595. lfmt (nth 4 info)
  5596. lang-f (intern (concat lang "-mode")))
  5597. (unless (functionp lang-f)
  5598. (error "No such language mode: %s" lang-f))
  5599. (goto-line line)
  5600. (if (get-buffer "*Org Edit Src Example*")
  5601. (kill-buffer "*Org Edit Src Example*"))
  5602. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5603. "*Org Edit Src Example*"))
  5604. (narrow-to-region beg end)
  5605. (remove-text-properties beg end '(display nil invisible nil
  5606. intangible nil))
  5607. (let ((org-inhibit-startup t))
  5608. (funcall lang-f))
  5609. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5610. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5611. (when lfmt
  5612. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5613. (when org-mode-p
  5614. (goto-char (point-min))
  5615. (while (re-search-forward "^," nil t)
  5616. (replace-match "")))
  5617. (goto-line line)
  5618. (org-exit-edit-mode)
  5619. (org-set-local 'header-line-format msg)
  5620. (message "%s" msg)
  5621. t)))
  5622. (defun org-edit-fixed-width-region ()
  5623. "Edit the fixed-width ascii drawing at point.
  5624. This must be a region where each line starts with a colon followed by
  5625. a space character.
  5626. An indirect buffer is created, and that buffer is then narrowed to the
  5627. example at point and switched to artist-mode. When done,
  5628. exit by killing the buffer with \\[org-edit-src-exit]."
  5629. (interactive)
  5630. (let ((line (org-current-line))
  5631. (case-fold-search t)
  5632. (msg (substitute-command-keys
  5633. "Edit, then exit with C-c ' (C-c and single quote)"))
  5634. (org-mode-p (eq major-mode 'org-mode))
  5635. beg end)
  5636. (beginning-of-line 1)
  5637. (if (looking-at "[ \t]*[^:\n \t]")
  5638. nil
  5639. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5640. (setq beg (point) end beg)
  5641. (save-excursion
  5642. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5643. (setq beg (point-at-bol 2))
  5644. (setq beg (point))))
  5645. (save-excursion
  5646. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5647. (setq end (1- (match-beginning 0)))
  5648. (setq end (point))))
  5649. (goto-line line))
  5650. (if (get-buffer "*Org Edit Picture*")
  5651. (kill-buffer "*Org Edit Picture*"))
  5652. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5653. "*Org Edit Picture*"))
  5654. (narrow-to-region beg end)
  5655. (remove-text-properties beg end '(display nil invisible nil
  5656. intangible nil))
  5657. (when (fboundp 'font-lock-unfontify-region)
  5658. (font-lock-unfontify-region (point-min) (point-max)))
  5659. (cond
  5660. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5661. (fundamental-mode)
  5662. (artist-mode 1))
  5663. (t (funcall org-edit-fixed-width-region-mode)))
  5664. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5665. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5666. (set (make-local-variable 'org-edit-src-picture) t)
  5667. (goto-char (point-min))
  5668. (while (re-search-forward "^[ \t]*: ?" nil t)
  5669. (replace-match ""))
  5670. (goto-line line)
  5671. (org-exit-edit-mode)
  5672. (org-set-local 'header-line-format msg)
  5673. (message "%s" msg)
  5674. t)))
  5675. (defun org-edit-src-find-region-and-lang ()
  5676. "Find the region and language for a local edit.
  5677. Return a list with beginning and end of the region, a string representing
  5678. the language, a switch telling of the content should be in a single line."
  5679. (let ((re-list
  5680. (append
  5681. org-edit-src-region-extra
  5682. '(
  5683. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5684. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5685. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5686. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5687. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5688. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5689. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5690. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5691. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5692. ("^#\\+html:" "\n" "html" single-line)
  5693. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5694. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5695. ("^#\\+latex:" "\n" "latex" single-line)
  5696. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5697. ("^#\\+ascii:" "\n" "ascii" single-line)
  5698. )))
  5699. (pos (point))
  5700. re1 re2 single beg end lang lfmt match-re1)
  5701. (catch 'exit
  5702. (while (setq entry (pop re-list))
  5703. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5704. single (nth 3 entry))
  5705. (save-excursion
  5706. (if (or (looking-at re1)
  5707. (re-search-backward re1 nil t))
  5708. (progn
  5709. (setq match-re1 (match-string 0))
  5710. (setq beg (match-end 0)
  5711. lang (org-edit-src-get-lang lang)
  5712. lfmt (org-edit-src-get-label-format match-re1))
  5713. (if (and (re-search-forward re2 nil t)
  5714. (>= (match-end 0) pos))
  5715. (throw 'exit (list beg (match-beginning 0)
  5716. lang single lfmt))))
  5717. (if (or (looking-at re2)
  5718. (re-search-forward re2 nil t))
  5719. (progn
  5720. (setq end (match-beginning 0))
  5721. (if (and (re-search-backward re1 nil t)
  5722. (<= (match-beginning 0) pos))
  5723. (progn
  5724. (setq lfmt (org-edit-src-get-label-format
  5725. (match-string 0)))
  5726. (throw 'exit
  5727. (list (match-end 0) end
  5728. (org-edit-src-get-lang lang)
  5729. single lfmt))))))))))))
  5730. (defun org-edit-src-get-lang (lang)
  5731. "Extract the src language."
  5732. (let ((m (match-string 0)))
  5733. (cond
  5734. ((stringp lang) lang)
  5735. ((integerp lang) (match-string lang))
  5736. ((and (eq lang 'lang)
  5737. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5738. (match-string 1 m))
  5739. ((and (eq lang 'style)
  5740. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5741. (match-string 1 m))
  5742. (t "fundamental"))))
  5743. (defun org-edit-src-get-label-format (s)
  5744. "Extract the label format."
  5745. (save-match-data
  5746. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5747. (match-string 1 s))))
  5748. (defun org-edit-src-exit ()
  5749. "Exit special edit and protect problematic lines."
  5750. (interactive)
  5751. (unless (buffer-base-buffer (current-buffer))
  5752. (error "This is not an indirect buffer, something is wrong..."))
  5753. (unless (> (point-min) 1)
  5754. (error "This buffer is not narrowed, something is wrong..."))
  5755. (goto-char (point-min))
  5756. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5757. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5758. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5759. (goto-char (point-min))
  5760. (while (re-search-forward "\n" nil t)
  5761. (replace-match " "))
  5762. (goto-char (point-min))
  5763. (if (looking-at "\\s-*") (replace-match " "))
  5764. (if (re-search-forward "\\s-+\\'" nil t)
  5765. (replace-match "")))
  5766. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5767. (goto-char (point-min))
  5768. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5769. (replace-match ",\\1"))
  5770. (when font-lock-mode
  5771. (font-lock-unfontify-region (point-min) (point-max)))
  5772. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5773. (when (org-bound-and-true-p org-edit-src-picture)
  5774. (untabify (point-min) (point-max))
  5775. (goto-char (point-min))
  5776. (while (re-search-forward "^" nil t)
  5777. (replace-match ": "))
  5778. (when font-lock-mode
  5779. (font-lock-unfontify-region (point-min) (point-max)))
  5780. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5781. (kill-buffer (current-buffer))
  5782. (and (org-mode-p) (org-restart-font-lock)))
  5783. ;;; The orgstruct minor mode
  5784. ;; Define a minor mode which can be used in other modes in order to
  5785. ;; integrate the org-mode structure editing commands.
  5786. ;; This is really a hack, because the org-mode structure commands use
  5787. ;; keys which normally belong to the major mode. Here is how it
  5788. ;; works: The minor mode defines all the keys necessary to operate the
  5789. ;; structure commands, but wraps the commands into a function which
  5790. ;; tests if the cursor is currently at a headline or a plain list
  5791. ;; item. If that is the case, the structure command is used,
  5792. ;; temporarily setting many Org-mode variables like regular
  5793. ;; expressions for filling etc. However, when any of those keys is
  5794. ;; used at a different location, function uses `key-binding' to look
  5795. ;; up if the key has an associated command in another currently active
  5796. ;; keymap (minor modes, major mode, global), and executes that
  5797. ;; command. There might be problems if any of the keys is otherwise
  5798. ;; used as a prefix key.
  5799. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5800. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5801. ;; addresses this by checking explicitly for both bindings.
  5802. (defvar orgstruct-mode-map (make-sparse-keymap)
  5803. "Keymap for the minor `orgstruct-mode'.")
  5804. (defvar org-local-vars nil
  5805. "List of local variables, for use by `orgstruct-mode'")
  5806. ;;;###autoload
  5807. (define-minor-mode orgstruct-mode
  5808. "Toggle the minor more `orgstruct-mode'.
  5809. This mode is for using Org-mode structure commands in other modes.
  5810. The following key behave as if Org-mode was active, if the cursor
  5811. is on a headline, or on a plain list item (both in the definition
  5812. of Org-mode).
  5813. M-up Move entry/item up
  5814. M-down Move entry/item down
  5815. M-left Promote
  5816. M-right Demote
  5817. M-S-up Move entry/item up
  5818. M-S-down Move entry/item down
  5819. M-S-left Promote subtree
  5820. M-S-right Demote subtree
  5821. M-q Fill paragraph and items like in Org-mode
  5822. C-c ^ Sort entries
  5823. C-c - Cycle list bullet
  5824. TAB Cycle item visibility
  5825. M-RET Insert new heading/item
  5826. S-M-RET Insert new TODO heading / Checkbox item
  5827. C-c C-c Set tags / toggle checkbox"
  5828. nil " OrgStruct" nil
  5829. (org-load-modules-maybe)
  5830. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5831. ;;;###autoload
  5832. (defun turn-on-orgstruct ()
  5833. "Unconditionally turn on `orgstruct-mode'."
  5834. (orgstruct-mode 1))
  5835. (defun orgstruct++-mode (&optional arg)
  5836. "Toggle `orgstruct-mode', the enhanced version of it.
  5837. In addition to setting orgstruct-mode, this also exports all indentation and
  5838. autofilling variables from org-mode into the buffer. It will also
  5839. recognize item context in multiline items.
  5840. Note that turning off orgstruct-mode will *not* remove the
  5841. indentation/paragraph settings. This can only be done by refreshing the
  5842. major mode, for example with \[normal-mode]."
  5843. (interactive "P")
  5844. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5845. (if (< arg 1)
  5846. (orgstruct-mode -1)
  5847. (orgstruct-mode 1)
  5848. (let (var val)
  5849. (mapc
  5850. (lambda (x)
  5851. (when (string-match
  5852. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5853. (symbol-name (car x)))
  5854. (setq var (car x) val (nth 1 x))
  5855. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5856. org-local-vars)
  5857. (org-set-local 'orgstruct-is-++ t))))
  5858. (defvar orgstruct-is-++ nil
  5859. "Is orgstruct-mode in ++ version in the current-buffer?")
  5860. (make-variable-buffer-local 'orgstruct-is-++)
  5861. ;;;###autoload
  5862. (defun turn-on-orgstruct++ ()
  5863. "Unconditionally turn on `orgstruct++-mode'."
  5864. (orgstruct++-mode 1))
  5865. (defun orgstruct-error ()
  5866. "Error when there is no default binding for a structure key."
  5867. (interactive)
  5868. (error "This key has no function outside structure elements"))
  5869. (defun orgstruct-setup ()
  5870. "Setup orgstruct keymaps."
  5871. (let ((nfunc 0)
  5872. (bindings
  5873. (list
  5874. '([(meta up)] org-metaup)
  5875. '([(meta down)] org-metadown)
  5876. '([(meta left)] org-metaleft)
  5877. '([(meta right)] org-metaright)
  5878. '([(meta shift up)] org-shiftmetaup)
  5879. '([(meta shift down)] org-shiftmetadown)
  5880. '([(meta shift left)] org-shiftmetaleft)
  5881. '([(meta shift right)] org-shiftmetaright)
  5882. '([(shift up)] org-shiftup)
  5883. '([(shift down)] org-shiftdown)
  5884. '([(shift left)] org-shiftleft)
  5885. '([(shift right)] org-shiftright)
  5886. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5887. '("\M-q" fill-paragraph)
  5888. '("\C-c^" org-sort)
  5889. '("\C-c-" org-cycle-list-bullet)))
  5890. elt key fun cmd)
  5891. (while (setq elt (pop bindings))
  5892. (setq nfunc (1+ nfunc))
  5893. (setq key (org-key (car elt))
  5894. fun (nth 1 elt)
  5895. cmd (orgstruct-make-binding fun nfunc key))
  5896. (org-defkey orgstruct-mode-map key cmd))
  5897. ;; Special treatment needed for TAB and RET
  5898. (org-defkey orgstruct-mode-map [(tab)]
  5899. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5900. (org-defkey orgstruct-mode-map "\C-i"
  5901. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5902. (org-defkey orgstruct-mode-map "\M-\C-m"
  5903. (orgstruct-make-binding 'org-insert-heading 105
  5904. "\M-\C-m" [(meta return)]))
  5905. (org-defkey orgstruct-mode-map [(meta return)]
  5906. (orgstruct-make-binding 'org-insert-heading 106
  5907. [(meta return)] "\M-\C-m"))
  5908. (org-defkey orgstruct-mode-map [(shift meta return)]
  5909. (orgstruct-make-binding 'org-insert-todo-heading 107
  5910. [(meta return)] "\M-\C-m"))
  5911. (unless org-local-vars
  5912. (setq org-local-vars (org-get-local-variables)))
  5913. t))
  5914. (defun orgstruct-make-binding (fun n &rest keys)
  5915. "Create a function for binding in the structure minor mode.
  5916. FUN is the command to call inside a table. N is used to create a unique
  5917. command name. KEYS are keys that should be checked in for a command
  5918. to execute outside of tables."
  5919. (eval
  5920. (list 'defun
  5921. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5922. '(arg)
  5923. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5924. "Outside of structure, run the binding of `"
  5925. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5926. "'.")
  5927. '(interactive "p")
  5928. (list 'if
  5929. `(org-context-p 'headline 'item
  5930. (and orgstruct-is-++
  5931. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5932. 'item-body))
  5933. (list 'org-run-like-in-org-mode (list 'quote fun))
  5934. (list 'let '(orgstruct-mode)
  5935. (list 'call-interactively
  5936. (append '(or)
  5937. (mapcar (lambda (k)
  5938. (list 'key-binding k))
  5939. keys)
  5940. '('orgstruct-error))))))))
  5941. (defun org-context-p (&rest contexts)
  5942. "Check if local context is any of CONTEXTS.
  5943. Possible values in the list of contexts are `table', `headline', and `item'."
  5944. (let ((pos (point)))
  5945. (goto-char (point-at-bol))
  5946. (prog1 (or (and (memq 'table contexts)
  5947. (looking-at "[ \t]*|"))
  5948. (and (memq 'headline contexts)
  5949. ;;????????? (looking-at "\\*+"))
  5950. (looking-at outline-regexp))
  5951. (and (memq 'item contexts)
  5952. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5953. (and (memq 'item-body contexts)
  5954. (org-in-item-p)))
  5955. (goto-char pos))))
  5956. (defun org-get-local-variables ()
  5957. "Return a list of all local variables in an org-mode buffer."
  5958. (let (varlist)
  5959. (with-current-buffer (get-buffer-create "*Org tmp*")
  5960. (erase-buffer)
  5961. (org-mode)
  5962. (setq varlist (buffer-local-variables)))
  5963. (kill-buffer "*Org tmp*")
  5964. (delq nil
  5965. (mapcar
  5966. (lambda (x)
  5967. (setq x
  5968. (if (symbolp x)
  5969. (list x)
  5970. (list (car x) (list 'quote (cdr x)))))
  5971. (if (string-match
  5972. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5973. (symbol-name (car x)))
  5974. x nil))
  5975. varlist))))
  5976. ;;;###autoload
  5977. (defun org-run-like-in-org-mode (cmd)
  5978. "Run a command, pretending that the current buffer is in Org-mode.
  5979. This will temporarily bind local variables that are typically bound in
  5980. Org-mode to the values they have in Org-mode, and then interactively
  5981. call CMD."
  5982. (org-load-modules-maybe)
  5983. (unless org-local-vars
  5984. (setq org-local-vars (org-get-local-variables)))
  5985. (eval (list 'let org-local-vars
  5986. (list 'call-interactively (list 'quote cmd)))))
  5987. ;;;; Archiving
  5988. (defun org-get-category (&optional pos)
  5989. "Get the category applying to position POS."
  5990. (get-text-property (or pos (point)) 'org-category))
  5991. (defun org-refresh-category-properties ()
  5992. "Refresh category text properties in the buffer."
  5993. (let ((def-cat (cond
  5994. ((null org-category)
  5995. (if buffer-file-name
  5996. (file-name-sans-extension
  5997. (file-name-nondirectory buffer-file-name))
  5998. "???"))
  5999. ((symbolp org-category) (symbol-name org-category))
  6000. (t org-category)))
  6001. beg end cat pos optionp)
  6002. (org-unmodified
  6003. (save-excursion
  6004. (save-restriction
  6005. (widen)
  6006. (goto-char (point-min))
  6007. (put-text-property (point) (point-max) 'org-category def-cat)
  6008. (while (re-search-forward
  6009. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6010. (setq pos (match-end 0)
  6011. optionp (equal (char-after (match-beginning 0)) ?#)
  6012. cat (org-trim (match-string 2)))
  6013. (if optionp
  6014. (setq beg (point-at-bol) end (point-max))
  6015. (org-back-to-heading t)
  6016. (setq beg (point) end (org-end-of-subtree t t)))
  6017. (put-text-property beg end 'org-category cat)
  6018. (goto-char pos)))))))
  6019. ;;;; Link Stuff
  6020. ;;; Link abbreviations
  6021. (defun org-link-expand-abbrev (link)
  6022. "Apply replacements as defined in `org-link-abbrev-alist."
  6023. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6024. (let* ((key (match-string 1 link))
  6025. (as (or (assoc key org-link-abbrev-alist-local)
  6026. (assoc key org-link-abbrev-alist)))
  6027. (tag (and (match-end 2) (match-string 3 link)))
  6028. rpl)
  6029. (if (not as)
  6030. link
  6031. (setq rpl (cdr as))
  6032. (cond
  6033. ((symbolp rpl) (funcall rpl tag))
  6034. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6035. ((string-match "%h" rpl)
  6036. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6037. (t (concat rpl tag)))))
  6038. link))
  6039. ;;; Storing and inserting links
  6040. (defvar org-insert-link-history nil
  6041. "Minibuffer history for links inserted with `org-insert-link'.")
  6042. (defvar org-stored-links nil
  6043. "Contains the links stored with `org-store-link'.")
  6044. (defvar org-store-link-plist nil
  6045. "Plist with info about the most recently link created with `org-store-link'.")
  6046. (defvar org-link-protocols nil
  6047. "Link protocols added to Org-mode using `org-add-link-type'.")
  6048. (defvar org-store-link-functions nil
  6049. "List of functions that are called to create and store a link.
  6050. Each function will be called in turn until one returns a non-nil
  6051. value. Each function should check if it is responsible for creating
  6052. this link (for example by looking at the major mode).
  6053. If not, it must exit and return nil.
  6054. If yes, it should return a non-nil value after a calling
  6055. `org-store-link-props' with a list of properties and values.
  6056. Special properties are:
  6057. :type The link prefix. like \"http\". This must be given.
  6058. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6059. This is obligatory as well.
  6060. :description Optional default description for the second pair
  6061. of brackets in an Org-mode link. The user can still change
  6062. this when inserting this link into an Org-mode buffer.
  6063. In addition to these, any additional properties can be specified
  6064. and then used in remember templates.")
  6065. (defun org-add-link-type (type &optional follow export)
  6066. "Add TYPE to the list of `org-link-types'.
  6067. Re-compute all regular expressions depending on `org-link-types'
  6068. FOLLOW and EXPORT are two functions.
  6069. FOLLOW should take the link path as the single argument and do whatever
  6070. is necessary to follow the link, for example find a file or display
  6071. a mail message.
  6072. EXPORT should format the link path for export to one of the export formats.
  6073. It should be a function accepting three arguments:
  6074. path the path of the link, the text after the prefix (like \"http:\")
  6075. desc the description of the link, if any, nil if there was no description
  6076. format the export format, a symbol like `html' or `latex'.
  6077. The function may use the FORMAT information to return different values
  6078. depending on the format. The return value will be put literally into
  6079. the exported file.
  6080. Org-mode has a built-in default for exporting links. If you are happy with
  6081. this default, there is no need to define an export function for the link
  6082. type. For a simple example of an export function, see `org-bbdb.el'."
  6083. (add-to-list 'org-link-types type t)
  6084. (org-make-link-regexps)
  6085. (if (assoc type org-link-protocols)
  6086. (setcdr (assoc type org-link-protocols) (list follow export))
  6087. (push (list type follow export) org-link-protocols)))
  6088. ;;;###autoload
  6089. (defun org-store-link (arg)
  6090. "\\<org-mode-map>Store an org-link to the current location.
  6091. This link is added to `org-stored-links' and can later be inserted
  6092. into an org-buffer with \\[org-insert-link].
  6093. For some link types, a prefix arg is interpreted:
  6094. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6095. For file links, arg negates `org-context-in-file-links'."
  6096. (interactive "P")
  6097. (org-load-modules-maybe)
  6098. (setq org-store-link-plist nil) ; reset
  6099. (let (link cpltxt desc description search txt)
  6100. (cond
  6101. ((run-hook-with-args-until-success 'org-store-link-functions)
  6102. (setq link (plist-get org-store-link-plist :link)
  6103. desc (or (plist-get org-store-link-plist :description) link)))
  6104. ((equal (buffer-name) "*Org Edit Src Example*")
  6105. (let (label gc)
  6106. (while (or (not label)
  6107. (save-excursion
  6108. (save-restriction
  6109. (widen)
  6110. (goto-char (point-min))
  6111. (re-search-forward
  6112. (regexp-quote (format org-coderef-label-format label))
  6113. nil t))))
  6114. (when label (message "Label exists already") (sit-for 2))
  6115. (setq label (read-string "Code line label: " label)))
  6116. (end-of-line 1)
  6117. (setq link (format org-coderef-label-format label))
  6118. (setq gc (- 79 (length link)))
  6119. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6120. (insert link)
  6121. (setq link (concat "(" label ")") desc nil)))
  6122. ((eq major-mode 'calendar-mode)
  6123. (let ((cd (calendar-cursor-to-date)))
  6124. (setq link
  6125. (format-time-string
  6126. (car org-time-stamp-formats)
  6127. (apply 'encode-time
  6128. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6129. nil nil nil))))
  6130. (org-store-link-props :type "calendar" :date cd)))
  6131. ((eq major-mode 'w3-mode)
  6132. (setq cpltxt (if (and (buffer-name)
  6133. (not (string-match "Untitled" (buffer-name))))
  6134. (buffer-name)
  6135. (url-view-url t))
  6136. link (org-make-link (url-view-url t)))
  6137. (org-store-link-props :type "w3" :url (url-view-url t)))
  6138. ((eq major-mode 'w3m-mode)
  6139. (setq cpltxt (or w3m-current-title w3m-current-url)
  6140. link (org-make-link w3m-current-url))
  6141. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6142. ((setq search (run-hook-with-args-until-success
  6143. 'org-create-file-search-functions))
  6144. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6145. "::" search))
  6146. (setq cpltxt (or description link)))
  6147. ((eq major-mode 'image-mode)
  6148. (setq cpltxt (concat "file:"
  6149. (abbreviate-file-name buffer-file-name))
  6150. link (org-make-link cpltxt))
  6151. (org-store-link-props :type "image" :file buffer-file-name))
  6152. ((eq major-mode 'dired-mode)
  6153. ;; link to the file in the current line
  6154. (setq cpltxt (concat "file:"
  6155. (abbreviate-file-name
  6156. (expand-file-name
  6157. (dired-get-filename nil t))))
  6158. link (org-make-link cpltxt)))
  6159. ((and buffer-file-name (org-mode-p))
  6160. (cond
  6161. ((org-in-regexp "<<\\(.*?\\)>>")
  6162. (setq cpltxt
  6163. (concat "file:"
  6164. (abbreviate-file-name buffer-file-name)
  6165. "::" (match-string 1))
  6166. link (org-make-link cpltxt)))
  6167. ((and (featurep 'org-id)
  6168. (or (eq org-link-to-org-use-id t)
  6169. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6170. (interactive-p))
  6171. (and org-link-to-org-use-id
  6172. (condition-case nil
  6173. (org-entry-get nil "ID")
  6174. (error nil)))))
  6175. ;; We can make a link using the ID.
  6176. (setq link (condition-case nil
  6177. (prog1 (org-id-store-link)
  6178. (setq desc (plist-get org-store-link-plist
  6179. :description)))
  6180. (error
  6181. ;; probably before first headline, link to file only
  6182. (concat "file:"
  6183. (abbreviate-file-name buffer-file-name))))))
  6184. (t
  6185. ;; Just link to current headline
  6186. (setq cpltxt (concat "file:"
  6187. (abbreviate-file-name buffer-file-name)))
  6188. ;; Add a context search string
  6189. (when (org-xor org-context-in-file-links arg)
  6190. (setq txt (cond
  6191. ((org-on-heading-p) nil)
  6192. ((org-region-active-p)
  6193. (buffer-substring (region-beginning) (region-end)))
  6194. (t nil)))
  6195. (when (or (null txt) (string-match "\\S-" txt))
  6196. (setq cpltxt
  6197. (concat cpltxt "::"
  6198. (condition-case nil
  6199. (org-make-org-heading-search-string txt)
  6200. (error "")))
  6201. desc "NONE")))
  6202. (if (string-match "::\\'" cpltxt)
  6203. (setq cpltxt (substring cpltxt 0 -2)))
  6204. (setq link (org-make-link cpltxt)))))
  6205. ((buffer-file-name (buffer-base-buffer))
  6206. ;; Just link to this file here.
  6207. (setq cpltxt (concat "file:"
  6208. (abbreviate-file-name
  6209. (buffer-file-name (buffer-base-buffer)))))
  6210. ;; Add a context string
  6211. (when (org-xor org-context-in-file-links arg)
  6212. (setq txt (if (org-region-active-p)
  6213. (buffer-substring (region-beginning) (region-end))
  6214. (buffer-substring (point-at-bol) (point-at-eol))))
  6215. ;; Only use search option if there is some text.
  6216. (when (string-match "\\S-" txt)
  6217. (setq cpltxt
  6218. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6219. desc "NONE")))
  6220. (setq link (org-make-link cpltxt)))
  6221. ((interactive-p)
  6222. (error "Cannot link to a buffer which is not visiting a file"))
  6223. (t (setq link nil)))
  6224. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6225. (setq link (or link cpltxt)
  6226. desc (or desc cpltxt))
  6227. (if (equal desc "NONE") (setq desc nil))
  6228. (if (and (interactive-p) link)
  6229. (progn
  6230. (setq org-stored-links
  6231. (cons (list link desc) org-stored-links))
  6232. (message "Stored: %s" (or desc link)))
  6233. (and link (org-make-link-string link desc)))))
  6234. (defun org-store-link-props (&rest plist)
  6235. "Store link properties, extract names and addresses."
  6236. (let (x adr)
  6237. (when (setq x (plist-get plist :from))
  6238. (setq adr (mail-extract-address-components x))
  6239. (setq plist (plist-put plist :fromname (car adr)))
  6240. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6241. (when (setq x (plist-get plist :to))
  6242. (setq adr (mail-extract-address-components x))
  6243. (setq plist (plist-put plist :toname (car adr)))
  6244. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6245. (let ((from (plist-get plist :from))
  6246. (to (plist-get plist :to)))
  6247. (when (and from to org-from-is-user-regexp)
  6248. (setq plist
  6249. (plist-put plist :fromto
  6250. (if (string-match org-from-is-user-regexp from)
  6251. (concat "to %t")
  6252. (concat "from %f"))))))
  6253. (setq org-store-link-plist plist))
  6254. (defun org-add-link-props (&rest plist)
  6255. "Add these properties to the link property list."
  6256. (let (key value)
  6257. (while plist
  6258. (setq key (pop plist) value (pop plist))
  6259. (setq org-store-link-plist
  6260. (plist-put org-store-link-plist key value)))))
  6261. (defun org-email-link-description (&optional fmt)
  6262. "Return the description part of an email link.
  6263. This takes information from `org-store-link-plist' and formats it
  6264. according to FMT (default from `org-email-link-description-format')."
  6265. (setq fmt (or fmt org-email-link-description-format))
  6266. (let* ((p org-store-link-plist)
  6267. (to (plist-get p :toaddress))
  6268. (from (plist-get p :fromaddress))
  6269. (table
  6270. (list
  6271. (cons "%c" (plist-get p :fromto))
  6272. (cons "%F" (plist-get p :from))
  6273. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6274. (cons "%T" (plist-get p :to))
  6275. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6276. (cons "%s" (plist-get p :subject))
  6277. (cons "%m" (plist-get p :message-id)))))
  6278. (when (string-match "%c" fmt)
  6279. ;; Check if the user wrote this message
  6280. (if (and org-from-is-user-regexp from to
  6281. (save-match-data (string-match org-from-is-user-regexp from)))
  6282. (setq fmt (replace-match "to %t" t t fmt))
  6283. (setq fmt (replace-match "from %f" t t fmt))))
  6284. (org-replace-escapes fmt table)))
  6285. (defun org-make-org-heading-search-string (&optional string heading)
  6286. "Make search string for STRING or current headline."
  6287. (interactive)
  6288. (let ((s (or string (org-get-heading))))
  6289. (unless (and string (not heading))
  6290. ;; We are using a headline, clean up garbage in there.
  6291. (if (string-match org-todo-regexp s)
  6292. (setq s (replace-match "" t t s)))
  6293. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6294. (setq s (replace-match "" t t s)))
  6295. (setq s (org-trim s))
  6296. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6297. org-comment-string "\\)") s)
  6298. (setq s (replace-match "" t t s)))
  6299. (while (string-match org-ts-regexp s)
  6300. (setq s (replace-match "" t t s))))
  6301. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6302. (setq s (replace-match " " t t s)))
  6303. (or string (setq s (concat "*" s))) ; Add * for headlines
  6304. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6305. (defun org-make-link (&rest strings)
  6306. "Concatenate STRINGS."
  6307. (apply 'concat strings))
  6308. (defun org-make-link-string (link &optional description)
  6309. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6310. (unless (string-match "\\S-" link)
  6311. (error "Empty link"))
  6312. (when (stringp description)
  6313. ;; Remove brackets from the description, they are fatal.
  6314. (while (string-match "\\[" description)
  6315. (setq description (replace-match "{" t t description)))
  6316. (while (string-match "\\]" description)
  6317. (setq description (replace-match "}" t t description))))
  6318. (when (equal (org-link-escape link) description)
  6319. ;; No description needed, it is identical
  6320. (setq description nil))
  6321. (when (and (not description)
  6322. (not (equal link (org-link-escape link))))
  6323. (setq description (org-extract-attributes link)))
  6324. (concat "[[" (org-link-escape link) "]"
  6325. (if description (concat "[" description "]") "")
  6326. "]"))
  6327. (defconst org-link-escape-chars
  6328. '((?\ . "%20")
  6329. (?\[ . "%5B")
  6330. (?\] . "%5D")
  6331. (?\340 . "%E0") ; `a
  6332. (?\342 . "%E2") ; ^a
  6333. (?\347 . "%E7") ; ,c
  6334. (?\350 . "%E8") ; `e
  6335. (?\351 . "%E9") ; 'e
  6336. (?\352 . "%EA") ; ^e
  6337. (?\356 . "%EE") ; ^i
  6338. (?\364 . "%F4") ; ^o
  6339. (?\371 . "%F9") ; `u
  6340. (?\373 . "%FB") ; ^u
  6341. (?\; . "%3B")
  6342. (?? . "%3F")
  6343. (?= . "%3D")
  6344. (?+ . "%2B")
  6345. )
  6346. "Association list of escapes for some characters problematic in links.
  6347. This is the list that is used for internal purposes.")
  6348. (defconst org-link-escape-chars-browser
  6349. '((?\ . "%20")) ; 32 for the SPC char
  6350. "Association list of escapes for some characters problematic in links.
  6351. This is the list that is used before handing over to the browser.")
  6352. (defun org-link-escape (text &optional table)
  6353. "Escape characters in TEXT that are problematic for links."
  6354. (setq table (or table org-link-escape-chars))
  6355. (when text
  6356. (let ((re (mapconcat (lambda (x) (regexp-quote
  6357. (char-to-string (car x))))
  6358. table "\\|")))
  6359. (while (string-match re text)
  6360. (setq text
  6361. (replace-match
  6362. (cdr (assoc (string-to-char (match-string 0 text))
  6363. table))
  6364. t t text)))
  6365. text)))
  6366. (defun org-link-unescape (text &optional table)
  6367. "Reverse the action of `org-link-escape'."
  6368. (setq table (or table org-link-escape-chars))
  6369. (when text
  6370. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6371. table "\\|")))
  6372. (while (string-match re text)
  6373. (setq text
  6374. (replace-match
  6375. (char-to-string (car (rassoc (match-string 0 text) table)))
  6376. t t text)))
  6377. text)))
  6378. (defun org-xor (a b)
  6379. "Exclusive or."
  6380. (if a (not b) b))
  6381. (defun org-fixup-message-id-for-http (s)
  6382. "Replace special characters in a message id, so it can be used in an http query."
  6383. (while (string-match "<" s)
  6384. (setq s (replace-match "%3C" t t s)))
  6385. (while (string-match ">" s)
  6386. (setq s (replace-match "%3E" t t s)))
  6387. (while (string-match "@" s)
  6388. (setq s (replace-match "%40" t t s)))
  6389. s)
  6390. ;;;###autoload
  6391. (defun org-insert-link-global ()
  6392. "Insert a link like Org-mode does.
  6393. This command can be called in any mode to insert a link in Org-mode syntax."
  6394. (interactive)
  6395. (org-load-modules-maybe)
  6396. (org-run-like-in-org-mode 'org-insert-link))
  6397. (defun org-insert-link (&optional complete-file link-location)
  6398. "Insert a link. At the prompt, enter the link.
  6399. Completion can be used to insert any of the link protocol prefixes like
  6400. http or ftp in use.
  6401. The history can be used to select a link previously stored with
  6402. `org-store-link'. When the empty string is entered (i.e. if you just
  6403. press RET at the prompt), the link defaults to the most recently
  6404. stored link. As SPC triggers completion in the minibuffer, you need to
  6405. use M-SPC or C-q SPC to force the insertion of a space character.
  6406. You will also be prompted for a description, and if one is given, it will
  6407. be displayed in the buffer instead of the link.
  6408. If there is already a link at point, this command will allow you to edit link
  6409. and description parts.
  6410. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6411. be selected using completion. The path to the file will be relative to the
  6412. current directory if the file is in the current directory or a subdirectory.
  6413. Otherwise, the link will be the absolute path as completed in the minibuffer
  6414. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6415. option `org-link-file-path-type'.
  6416. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6417. the current directory or below.
  6418. With three \\[universal-argument] prefixes, negate the meaning of
  6419. `org-keep-stored-link-after-insertion'.
  6420. If `org-make-link-description-function' is non-nil, this function will be
  6421. called with the link target, and the result will be the default
  6422. link description.
  6423. If the LINK-LOCATION parameter is non-nil, this value will be
  6424. used as the link location instead of reading one interactively."
  6425. (interactive "P")
  6426. (let* ((wcf (current-window-configuration))
  6427. (region (if (org-region-active-p)
  6428. (buffer-substring (region-beginning) (region-end))))
  6429. (remove (and region (list (region-beginning) (region-end))))
  6430. (desc region)
  6431. tmphist ; byte-compile incorrectly complains about this
  6432. (link link-location)
  6433. entry file)
  6434. (cond
  6435. (link-location) ; specified by arg, just use it.
  6436. ((org-in-regexp org-bracket-link-regexp 1)
  6437. ;; We do have a link at point, and we are going to edit it.
  6438. (setq remove (list (match-beginning 0) (match-end 0)))
  6439. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6440. (setq link (read-string "Link: "
  6441. (org-link-unescape
  6442. (org-match-string-no-properties 1)))))
  6443. ((or (org-in-regexp org-angle-link-re)
  6444. (org-in-regexp org-plain-link-re))
  6445. ;; Convert to bracket link
  6446. (setq remove (list (match-beginning 0) (match-end 0))
  6447. link (read-string "Link: "
  6448. (org-remove-angle-brackets (match-string 0)))))
  6449. ((member complete-file '((4) (16)))
  6450. ;; Completing read for file names.
  6451. (setq file (read-file-name "File: "))
  6452. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6453. (pwd1 (file-name-as-directory (abbreviate-file-name
  6454. (expand-file-name ".")))))
  6455. (cond
  6456. ((equal complete-file '(16))
  6457. (setq link (org-make-link
  6458. "file:"
  6459. (abbreviate-file-name (expand-file-name file)))))
  6460. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6461. (setq link (org-make-link "file:" (match-string 1 file))))
  6462. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6463. (expand-file-name file))
  6464. (setq link (org-make-link
  6465. "file:" (match-string 1 (expand-file-name file)))))
  6466. (t (setq link (org-make-link "file:" file))))))
  6467. (t
  6468. ;; Read link, with completion for stored links.
  6469. (with-output-to-temp-buffer "*Org Links*"
  6470. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6471. (when org-stored-links
  6472. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6473. (princ (mapconcat
  6474. (lambda (x)
  6475. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6476. (reverse org-stored-links) "\n"))))
  6477. (let ((cw (selected-window)))
  6478. (select-window (get-buffer-window "*Org Links*"))
  6479. (setq truncate-lines t)
  6480. (org-fit-window-to-buffer)
  6481. (select-window cw))
  6482. ;; Fake a link history, containing the stored links.
  6483. (setq tmphist (append (mapcar 'car org-stored-links)
  6484. org-insert-link-history))
  6485. (unwind-protect
  6486. (setq link
  6487. (let ((org-completion-use-ido nil))
  6488. (org-completing-read
  6489. "Link: "
  6490. (append
  6491. (mapcar (lambda (x) (list (concat (car x) ":")))
  6492. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6493. (mapcar (lambda (x) (list (concat x ":")))
  6494. org-link-types))
  6495. nil nil nil
  6496. 'tmphist
  6497. (or (car (car org-stored-links))))))
  6498. (set-window-configuration wcf)
  6499. (kill-buffer "*Org Links*"))
  6500. (setq entry (assoc link org-stored-links))
  6501. (or entry (push link org-insert-link-history))
  6502. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6503. (not org-keep-stored-link-after-insertion))
  6504. (setq org-stored-links (delq (assoc link org-stored-links)
  6505. org-stored-links)))
  6506. (setq desc (or desc (nth 1 entry)))))
  6507. (if (string-match org-plain-link-re link)
  6508. ;; URL-like link, normalize the use of angular brackets.
  6509. (setq link (org-make-link (org-remove-angle-brackets link))))
  6510. ;; Check if we are linking to the current file with a search option
  6511. ;; If yes, simplify the link by using only the search option.
  6512. (when (and buffer-file-name
  6513. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6514. (let* ((path (match-string 1 link))
  6515. (case-fold-search nil)
  6516. (search (match-string 2 link)))
  6517. (save-match-data
  6518. (if (equal (file-truename buffer-file-name) (file-truename path))
  6519. ;; We are linking to this same file, with a search option
  6520. (setq link search)))))
  6521. ;; Check if we can/should use a relative path. If yes, simplify the link
  6522. (when (string-match "^file:\\(.*\\)" link)
  6523. (let* ((path (match-string 1 link))
  6524. (origpath path)
  6525. (case-fold-search nil))
  6526. (cond
  6527. ((or (eq org-link-file-path-type 'absolute)
  6528. (equal complete-file '(16)))
  6529. (setq path (abbreviate-file-name (expand-file-name path))))
  6530. ((eq org-link-file-path-type 'noabbrev)
  6531. (setq path (expand-file-name path)))
  6532. ((eq org-link-file-path-type 'relative)
  6533. (setq path (file-relative-name path)))
  6534. (t
  6535. (save-match-data
  6536. (if (string-match (concat "^" (regexp-quote
  6537. (file-name-as-directory
  6538. (expand-file-name "."))))
  6539. (expand-file-name path))
  6540. ;; We are linking a file with relative path name.
  6541. (setq path (substring (expand-file-name path)
  6542. (match-end 0)))
  6543. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6544. (setq link (concat "file:" path))
  6545. (if (equal desc origpath)
  6546. (setq desc path))))
  6547. (if org-make-link-description-function
  6548. (setq desc (funcall org-make-link-description-function link desc)))
  6549. (setq desc (read-string "Description: " desc))
  6550. (unless (string-match "\\S-" desc) (setq desc nil))
  6551. (if remove (apply 'delete-region remove))
  6552. (insert (org-make-link-string link desc))))
  6553. (defun org-completing-read (&rest args)
  6554. "Completing-read with SPACE being a normal character."
  6555. (let ((minibuffer-local-completion-map
  6556. (copy-keymap minibuffer-local-completion-map)))
  6557. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6558. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6559. (apply 'org-ido-completing-read args)))
  6560. (defun org-completing-read-no-ido (&rest args)
  6561. (let (org-completion-use-ido)
  6562. (apply 'org-completing-read args)))
  6563. (defun org-ido-completing-read (&rest args)
  6564. "Completing-read using `ido-mode' speedups if available"
  6565. (if (and org-completion-use-ido
  6566. (fboundp 'ido-completing-read)
  6567. (boundp 'ido-mode) ido-mode
  6568. (listp (second args)))
  6569. (let ((ido-enter-matching-directory nil))
  6570. (apply 'ido-completing-read (concat (car args))
  6571. (mapcar (lambda (x) (car x)) (nth 1 args))
  6572. (cddr args)))
  6573. (apply 'completing-read args)))
  6574. (defun org-extract-attributes (s)
  6575. "Extract the attributes cookie from a string and set as text property."
  6576. (let (a attr (start 0) key value)
  6577. (save-match-data
  6578. (when (string-match "{{\\([^}]+\\)}}$" s)
  6579. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6580. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6581. (setq key (match-string 1 a) value (match-string 2 a)
  6582. start (match-end 0)
  6583. attr (plist-put attr (intern key) value))))
  6584. (org-add-props s nil 'org-attr attr))
  6585. s))
  6586. (defun org-extract-attributes-from-string (tag)
  6587. (let (key value attr)
  6588. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6589. (setq key (match-string 1 tag) value (match-string 2 tag)
  6590. tag (replace-match "" t t tag)
  6591. attr (plist-put attr (intern key) value)))
  6592. (cons tag attr)))
  6593. (defun org-attributes-to-string (plist)
  6594. "Format a property list into an HTML attribute list."
  6595. (let ((s "") key value)
  6596. (while plist
  6597. (setq key (pop plist) value (pop plist))
  6598. (and value
  6599. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6600. s))
  6601. ;;; Opening/following a link
  6602. (defvar org-link-search-failed nil)
  6603. (defun org-next-link ()
  6604. "Move forward to the next link.
  6605. If the link is in hidden text, expose it."
  6606. (interactive)
  6607. (when (and org-link-search-failed (eq this-command last-command))
  6608. (goto-char (point-min))
  6609. (message "Link search wrapped back to beginning of buffer"))
  6610. (setq org-link-search-failed nil)
  6611. (let* ((pos (point))
  6612. (ct (org-context))
  6613. (a (assoc :link ct)))
  6614. (if a (goto-char (nth 2 a)))
  6615. (if (re-search-forward org-any-link-re nil t)
  6616. (progn
  6617. (goto-char (match-beginning 0))
  6618. (if (org-invisible-p) (org-show-context)))
  6619. (goto-char pos)
  6620. (setq org-link-search-failed t)
  6621. (error "No further link found"))))
  6622. (defun org-previous-link ()
  6623. "Move backward to the previous link.
  6624. If the link is in hidden text, expose it."
  6625. (interactive)
  6626. (when (and org-link-search-failed (eq this-command last-command))
  6627. (goto-char (point-max))
  6628. (message "Link search wrapped back to end of buffer"))
  6629. (setq org-link-search-failed nil)
  6630. (let* ((pos (point))
  6631. (ct (org-context))
  6632. (a (assoc :link ct)))
  6633. (if a (goto-char (nth 1 a)))
  6634. (if (re-search-backward org-any-link-re nil t)
  6635. (progn
  6636. (goto-char (match-beginning 0))
  6637. (if (org-invisible-p) (org-show-context)))
  6638. (goto-char pos)
  6639. (setq org-link-search-failed t)
  6640. (error "No further link found"))))
  6641. (defun org-translate-link (s)
  6642. "Translate a link string if a translation function has been defined."
  6643. (if (and org-link-translation-function
  6644. (fboundp org-link-translation-function)
  6645. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6646. (progn
  6647. (setq s (funcall org-link-translation-function
  6648. (match-string 1) (match-string 2)))
  6649. (concat (car s) ":" (cdr s)))
  6650. s))
  6651. (defun org-translate-link-from-planner (type path)
  6652. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6653. This is still an experimental function, your mileage may vary."
  6654. (cond
  6655. ((member type '("http" "https" "news" "ftp"))
  6656. ;; standard Internet links are the same.
  6657. nil)
  6658. ((and (equal type "irc") (string-match "^//" path))
  6659. ;; Planner has two / at the beginning of an irc link, we have 1.
  6660. ;; We should have zero, actually....
  6661. (setq path (substring path 1)))
  6662. ((and (equal type "lisp") (string-match "^/" path))
  6663. ;; Planner has a slash, we do not.
  6664. (setq type "elisp" path (substring path 1)))
  6665. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6666. ;; A typical message link. Planner has the id after the fina slash,
  6667. ;; we separate it with a hash mark
  6668. (setq path (concat (match-string 1 path) "#"
  6669. (org-remove-angle-brackets (match-string 2 path)))))
  6670. )
  6671. (cons type path))
  6672. (defun org-find-file-at-mouse (ev)
  6673. "Open file link or URL at mouse."
  6674. (interactive "e")
  6675. (mouse-set-point ev)
  6676. (org-open-at-point 'in-emacs))
  6677. (defun org-open-at-mouse (ev)
  6678. "Open file link or URL at mouse."
  6679. (interactive "e")
  6680. (mouse-set-point ev)
  6681. (if (eq major-mode 'org-agenda-mode)
  6682. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6683. (org-open-at-point))
  6684. (defvar org-window-config-before-follow-link nil
  6685. "The window configuration before following a link.
  6686. This is saved in case the need arises to restore it.")
  6687. (defvar org-open-link-marker (make-marker)
  6688. "Marker pointing to the location where `org-open-at-point; was called.")
  6689. ;;;###autoload
  6690. (defun org-open-at-point-global ()
  6691. "Follow a link like Org-mode does.
  6692. This command can be called in any mode to follow a link that has
  6693. Org-mode syntax."
  6694. (interactive)
  6695. (org-run-like-in-org-mode 'org-open-at-point))
  6696. ;;;###autoload
  6697. (defun org-open-link-from-string (s &optional arg)
  6698. "Open a link in the string S, as if it was in Org-mode."
  6699. (interactive "sLink: \nP")
  6700. (with-temp-buffer
  6701. (let ((org-inhibit-startup t))
  6702. (org-mode)
  6703. (insert s)
  6704. (goto-char (point-min))
  6705. (org-open-at-point arg))))
  6706. (defun org-open-at-point (&optional in-emacs)
  6707. "Open link at or after point.
  6708. If there is no link at point, this function will search forward up to
  6709. the end of the current subtree.
  6710. Normally, files will be opened by an appropriate application. If the
  6711. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6712. With a double prefix argument, try to open outside of Emacs, in the
  6713. application the system uses for this file type."
  6714. (interactive "P")
  6715. (org-load-modules-maybe)
  6716. (move-marker org-open-link-marker (point))
  6717. (setq org-window-config-before-follow-link (current-window-configuration))
  6718. (org-remove-occur-highlights nil nil t)
  6719. (cond
  6720. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6721. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6722. (org-footnote-action))
  6723. (t
  6724. (let (type path link line search (pos (point)))
  6725. (catch 'match
  6726. (save-excursion
  6727. (skip-chars-forward "^]\n\r")
  6728. (when (org-in-regexp org-bracket-link-regexp)
  6729. (setq link (org-extract-attributes
  6730. (org-link-unescape (org-match-string-no-properties 1))))
  6731. (while (string-match " *\n *" link)
  6732. (setq link (replace-match " " t t link)))
  6733. (setq link (org-link-expand-abbrev link))
  6734. (cond
  6735. ((or (file-name-absolute-p link)
  6736. (string-match "^\\.\\.?/" link))
  6737. (setq type "file" path link))
  6738. ((string-match org-link-re-with-space3 link)
  6739. (setq type (match-string 1 link) path (match-string 2 link)))
  6740. (t (setq type "thisfile" path link)))
  6741. (throw 'match t)))
  6742. (when (get-text-property (point) 'org-linked-text)
  6743. (setq type "thisfile"
  6744. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6745. (1+ (point)) (point))
  6746. path (buffer-substring
  6747. (previous-single-property-change pos 'org-linked-text)
  6748. (next-single-property-change pos 'org-linked-text)))
  6749. (throw 'match t))
  6750. (save-excursion
  6751. (when (or (org-in-regexp org-angle-link-re)
  6752. (org-in-regexp org-plain-link-re))
  6753. (setq type (match-string 1) path (match-string 2))
  6754. (throw 'match t)))
  6755. (save-excursion
  6756. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6757. (setq type "tags"
  6758. path (match-string 1))
  6759. (while (string-match ":" path)
  6760. (setq path (replace-match "+" t t path)))
  6761. (throw 'match t)))
  6762. (when (org-in-regexp "<\\([^><\n]+\\)>")
  6763. (setq type "tree-match"
  6764. path (match-string 1))
  6765. (throw 'match t)))
  6766. (unless path
  6767. (error "No link found"))
  6768. ;; Remove any trailing spaces in path
  6769. (if (string-match " +\\'" path)
  6770. (setq path (replace-match "" t t path)))
  6771. (if (and org-link-translation-function
  6772. (fboundp org-link-translation-function))
  6773. ;; Check if we need to translate the link
  6774. (let ((tmp (funcall org-link-translation-function type path)))
  6775. (setq type (car tmp) path (cdr tmp))))
  6776. (cond
  6777. ((assoc type org-link-protocols)
  6778. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6779. ((equal type "mailto")
  6780. (let ((cmd (car org-link-mailto-program))
  6781. (args (cdr org-link-mailto-program)) args1
  6782. (address path) (subject "") a)
  6783. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6784. (setq address (match-string 1 path)
  6785. subject (org-link-escape (match-string 2 path))))
  6786. (while args
  6787. (cond
  6788. ((not (stringp (car args))) (push (pop args) args1))
  6789. (t (setq a (pop args))
  6790. (if (string-match "%a" a)
  6791. (setq a (replace-match address t t a)))
  6792. (if (string-match "%s" a)
  6793. (setq a (replace-match subject t t a)))
  6794. (push a args1))))
  6795. (apply cmd (nreverse args1))))
  6796. ((member type '("http" "https" "ftp" "news"))
  6797. (browse-url (concat type ":" (org-link-escape
  6798. path org-link-escape-chars-browser))))
  6799. ((member type '("message"))
  6800. (browse-url (concat type ":" path)))
  6801. ((string= type "tags")
  6802. (org-tags-view in-emacs path))
  6803. ((string= type "thisfile")
  6804. (if in-emacs
  6805. (switch-to-buffer-other-window
  6806. (org-get-buffer-for-internal-link (current-buffer)))
  6807. (org-mark-ring-push))
  6808. (let ((cmd `(org-link-search
  6809. ,path
  6810. ,(cond ((equal in-emacs '(4)) 'occur)
  6811. ((equal in-emacs '(16)) 'org-occur)
  6812. (t nil))
  6813. ,pos)))
  6814. (condition-case nil (eval cmd)
  6815. (error (progn (widen) (eval cmd))))))
  6816. ((string= type "tree-match")
  6817. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6818. ((string= type "file")
  6819. (if (string-match "::\\([0-9]+\\)\\'" path)
  6820. (setq line (string-to-number (match-string 1 path))
  6821. path (substring path 0 (match-beginning 0)))
  6822. (if (string-match "::\\(.+\\)\\'" path)
  6823. (setq search (match-string 1 path)
  6824. path (substring path 0 (match-beginning 0)))))
  6825. (if (string-match "[*?{]" (file-name-nondirectory path))
  6826. (dired path)
  6827. (org-open-file path in-emacs line search)))
  6828. ((string= type "news")
  6829. (require 'org-gnus)
  6830. (org-gnus-follow-link path))
  6831. ((string= type "shell")
  6832. (let ((cmd path))
  6833. (if (or (not org-confirm-shell-link-function)
  6834. (funcall org-confirm-shell-link-function
  6835. (format "Execute \"%s\" in shell? "
  6836. (org-add-props cmd nil
  6837. 'face 'org-warning))))
  6838. (progn
  6839. (message "Executing %s" cmd)
  6840. (shell-command cmd))
  6841. (error "Abort"))))
  6842. ((string= type "elisp")
  6843. (let ((cmd path))
  6844. (if (or (not org-confirm-elisp-link-function)
  6845. (funcall org-confirm-elisp-link-function
  6846. (format "Execute \"%s\" as elisp? "
  6847. (org-add-props cmd nil
  6848. 'face 'org-warning))))
  6849. (message "%s => %s" cmd
  6850. (if (equal (string-to-char cmd) ?\()
  6851. (eval (read cmd))
  6852. (call-interactively (read cmd))))
  6853. (error "Abort"))))
  6854. (t
  6855. (browse-url-at-point))))))
  6856. (move-marker org-open-link-marker nil)
  6857. (run-hook-with-args 'org-follow-link-hook))
  6858. ;;;; Time estimates
  6859. (defun org-get-effort (&optional pom)
  6860. "Get the effort estimate for the current entry."
  6861. (org-entry-get pom org-effort-property))
  6862. ;;; File search
  6863. (defvar org-create-file-search-functions nil
  6864. "List of functions to construct the right search string for a file link.
  6865. These functions are called in turn with point at the location to
  6866. which the link should point.
  6867. A function in the hook should first test if it would like to
  6868. handle this file type, for example by checking the major-mode or
  6869. the file extension. If it decides not to handle this file, it
  6870. should just return nil to give other functions a chance. If it
  6871. does handle the file, it must return the search string to be used
  6872. when following the link. The search string will be part of the
  6873. file link, given after a double colon, and `org-open-at-point'
  6874. will automatically search for it. If special measures must be
  6875. taken to make the search successful, another function should be
  6876. added to the companion hook `org-execute-file-search-functions',
  6877. which see.
  6878. A function in this hook may also use `setq' to set the variable
  6879. `description' to provide a suggestion for the descriptive text to
  6880. be used for this link when it gets inserted into an Org-mode
  6881. buffer with \\[org-insert-link].")
  6882. (defvar org-execute-file-search-functions nil
  6883. "List of functions to execute a file search triggered by a link.
  6884. Functions added to this hook must accept a single argument, the
  6885. search string that was part of the file link, the part after the
  6886. double colon. The function must first check if it would like to
  6887. handle this search, for example by checking the major-mode or the
  6888. file extension. If it decides not to handle this search, it
  6889. should just return nil to give other functions a chance. If it
  6890. does handle the search, it must return a non-nil value to keep
  6891. other functions from trying.
  6892. Each function can access the current prefix argument through the
  6893. variable `current-prefix-argument'. Note that a single prefix is
  6894. used to force opening a link in Emacs, so it may be good to only
  6895. use a numeric or double prefix to guide the search function.
  6896. In case this is needed, a function in this hook can also restore
  6897. the window configuration before `org-open-at-point' was called using:
  6898. (set-window-configuration org-window-config-before-follow-link)")
  6899. (defun org-link-search (s &optional type avoid-pos)
  6900. "Search for a link search option.
  6901. If S is surrounded by forward slashes, it is interpreted as a
  6902. regular expression. In org-mode files, this will create an `org-occur'
  6903. sparse tree. In ordinary files, `occur' will be used to list matches.
  6904. If the current buffer is in `dired-mode', grep will be used to search
  6905. in all files. If AVOID-POS is given, ignore matches near that position."
  6906. (let ((case-fold-search t)
  6907. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6908. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6909. (append '(("") (" ") ("\t") ("\n"))
  6910. org-emphasis-alist)
  6911. "\\|") "\\)"))
  6912. (pos (point))
  6913. (pre nil) (post nil)
  6914. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6915. (cond
  6916. ;; First check if there are any special
  6917. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6918. ;; Now try the builtin stuff
  6919. ((save-excursion
  6920. (goto-char (point-min))
  6921. (and
  6922. (re-search-forward
  6923. (concat "<<" (regexp-quote s0) ">>") nil t)
  6924. (setq type 'dedicated
  6925. pos (match-beginning 0))))
  6926. ;; There is an exact target for this
  6927. (goto-char pos))
  6928. ((and (string-match "^(\\(.*\\))$" s0)
  6929. (save-excursion
  6930. (goto-char (point-min))
  6931. (and
  6932. (re-search-forward
  6933. (concat "[^[]" (regexp-quote
  6934. (format org-coderef-label-format
  6935. (match-string 1 s0))))
  6936. nil t)
  6937. (setq type 'dedicated
  6938. pos (1+ (match-beginning 0))))))
  6939. ;; There is a coderef target for this
  6940. (goto-char pos))
  6941. ((string-match "^/\\(.*\\)/$" s)
  6942. ;; A regular expression
  6943. (cond
  6944. ((org-mode-p)
  6945. (org-occur (match-string 1 s)))
  6946. ;;((eq major-mode 'dired-mode)
  6947. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6948. (t (org-do-occur (match-string 1 s)))))
  6949. (t
  6950. ;; A normal search strings
  6951. (when (equal (string-to-char s) ?*)
  6952. ;; Anchor on headlines, post may include tags.
  6953. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6954. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6955. s (substring s 1)))
  6956. (remove-text-properties
  6957. 0 (length s)
  6958. '(face nil mouse-face nil keymap nil fontified nil) s)
  6959. ;; Make a series of regular expressions to find a match
  6960. (setq words (org-split-string s "[ \n\r\t]+")
  6961. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6962. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6963. "\\)" markers)
  6964. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6965. re2a (concat "[ \t\r\n]" re2a_)
  6966. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6967. re4 (concat "[^a-zA-Z_]" re4_)
  6968. re1 (concat pre re2 post)
  6969. re3 (concat pre (if pre re4_ re4) post)
  6970. re5 (concat pre ".*" re4)
  6971. re2 (concat pre re2)
  6972. re2a (concat pre (if pre re2a_ re2a))
  6973. re4 (concat pre (if pre re4_ re4))
  6974. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6975. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6976. re5 "\\)"
  6977. ))
  6978. (cond
  6979. ((eq type 'org-occur) (org-occur reall))
  6980. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6981. (t (goto-char (point-min))
  6982. (setq type 'fuzzy)
  6983. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6984. (org-search-not-self 1 re1 nil t)
  6985. (org-search-not-self 1 re2 nil t)
  6986. (org-search-not-self 1 re2a nil t)
  6987. (org-search-not-self 1 re3 nil t)
  6988. (org-search-not-self 1 re4 nil t)
  6989. (org-search-not-self 1 re5 nil t)
  6990. )
  6991. (goto-char (match-beginning 1))
  6992. (goto-char pos)
  6993. (error "No match")))))
  6994. (t
  6995. ;; Normal string-search
  6996. (goto-char (point-min))
  6997. (if (search-forward s nil t)
  6998. (goto-char (match-beginning 0))
  6999. (error "No match"))))
  7000. (and (org-mode-p) (org-show-context 'link-search))
  7001. type))
  7002. (defun org-search-not-self (group &rest args)
  7003. "Execute `re-search-forward', but only accept matches that do not
  7004. enclose the position of `org-open-link-marker'."
  7005. (let ((m org-open-link-marker))
  7006. (catch 'exit
  7007. (while (apply 're-search-forward args)
  7008. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7009. (goto-char (match-end group))
  7010. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7011. (> (match-beginning 0) (marker-position m))
  7012. (< (match-end 0) (marker-position m)))
  7013. (save-match-data
  7014. (or (not (org-in-regexp
  7015. org-bracket-link-analytic-regexp 1))
  7016. (not (match-end 4)) ; no description
  7017. (and (<= (match-beginning 4) (point))
  7018. (>= (match-end 4) (point))))))
  7019. (throw 'exit (point))))))))
  7020. (defun org-get-buffer-for-internal-link (buffer)
  7021. "Return a buffer to be used for displaying the link target of internal links."
  7022. (cond
  7023. ((not org-display-internal-link-with-indirect-buffer)
  7024. buffer)
  7025. ((string-match "(Clone)$" (buffer-name buffer))
  7026. (message "Buffer is already a clone, not making another one")
  7027. ;; we also do not modify visibility in this case
  7028. buffer)
  7029. (t ; make a new indirect buffer for displaying the link
  7030. (let* ((bn (buffer-name buffer))
  7031. (ibn (concat bn "(Clone)"))
  7032. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7033. (with-current-buffer ib (org-overview))
  7034. ib))))
  7035. (defun org-do-occur (regexp &optional cleanup)
  7036. "Call the Emacs command `occur'.
  7037. If CLEANUP is non-nil, remove the printout of the regular expression
  7038. in the *Occur* buffer. This is useful if the regex is long and not useful
  7039. to read."
  7040. (occur regexp)
  7041. (when cleanup
  7042. (let ((cwin (selected-window)) win beg end)
  7043. (when (setq win (get-buffer-window "*Occur*"))
  7044. (select-window win))
  7045. (goto-char (point-min))
  7046. (when (re-search-forward "match[a-z]+" nil t)
  7047. (setq beg (match-end 0))
  7048. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7049. (setq end (1- (match-beginning 0)))))
  7050. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7051. (goto-char (point-min))
  7052. (select-window cwin))))
  7053. ;;; The mark ring for links jumps
  7054. (defvar org-mark-ring nil
  7055. "Mark ring for positions before jumps in Org-mode.")
  7056. (defvar org-mark-ring-last-goto nil
  7057. "Last position in the mark ring used to go back.")
  7058. ;; Fill and close the ring
  7059. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7060. (loop for i from 1 to org-mark-ring-length do
  7061. (push (make-marker) org-mark-ring))
  7062. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7063. org-mark-ring)
  7064. (defun org-mark-ring-push (&optional pos buffer)
  7065. "Put the current position or POS into the mark ring and rotate it."
  7066. (interactive)
  7067. (setq pos (or pos (point)))
  7068. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7069. (move-marker (car org-mark-ring)
  7070. (or pos (point))
  7071. (or buffer (current-buffer)))
  7072. (message "%s"
  7073. (substitute-command-keys
  7074. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7075. (defun org-mark-ring-goto (&optional n)
  7076. "Jump to the previous position in the mark ring.
  7077. With prefix arg N, jump back that many stored positions. When
  7078. called several times in succession, walk through the entire ring.
  7079. Org-mode commands jumping to a different position in the current file,
  7080. or to another Org-mode file, automatically push the old position
  7081. onto the ring."
  7082. (interactive "p")
  7083. (let (p m)
  7084. (if (eq last-command this-command)
  7085. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7086. (setq p org-mark-ring))
  7087. (setq org-mark-ring-last-goto p)
  7088. (setq m (car p))
  7089. (switch-to-buffer (marker-buffer m))
  7090. (goto-char m)
  7091. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7092. (defun org-remove-angle-brackets (s)
  7093. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7094. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7095. s)
  7096. (defun org-add-angle-brackets (s)
  7097. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7098. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7099. s)
  7100. (defun org-remove-double-quotes (s)
  7101. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7102. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7103. s)
  7104. ;;; Following specific links
  7105. (defun org-follow-timestamp-link ()
  7106. (cond
  7107. ((org-at-date-range-p t)
  7108. (let ((org-agenda-start-on-weekday)
  7109. (t1 (match-string 1))
  7110. (t2 (match-string 2)))
  7111. (setq t1 (time-to-days (org-time-string-to-time t1))
  7112. t2 (time-to-days (org-time-string-to-time t2)))
  7113. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7114. ((org-at-timestamp-p t)
  7115. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7116. (substring (match-string 1) 0 10)))
  7117. 1))
  7118. (t (error "This should not happen"))))
  7119. ;;; Following file links
  7120. (defvar org-wait nil)
  7121. (defun org-open-file (path &optional in-emacs line search)
  7122. "Open the file at PATH.
  7123. First, this expands any special file name abbreviations. Then the
  7124. configuration variable `org-file-apps' is checked if it contains an
  7125. entry for this file type, and if yes, the corresponding command is launched.
  7126. If no application is found, Emacs simply visits the file.
  7127. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7128. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7129. and o use an external application to visit the file.
  7130. Optional LINE specifies a line to go to, optional SEARCH a string to
  7131. search for. If LINE or SEARCH is given, the file will always be
  7132. opened in Emacs.
  7133. If the file does not exist, an error is thrown."
  7134. (setq in-emacs (or in-emacs line search))
  7135. (let* ((file (if (equal path "")
  7136. buffer-file-name
  7137. (substitute-in-file-name (expand-file-name path))))
  7138. (apps (append org-file-apps (org-default-apps)))
  7139. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7140. (dirp (if remp nil (file-directory-p file)))
  7141. (file (if (and dirp org-open-directory-means-index-dot-org)
  7142. (concat (file-name-as-directory file) "index.org")
  7143. file))
  7144. (a-m-a-p (assq 'auto-mode apps))
  7145. (dfile (downcase file))
  7146. (old-buffer (current-buffer))
  7147. (old-pos (point))
  7148. (old-mode major-mode)
  7149. ext cmd)
  7150. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7151. (setq ext (match-string 1 dfile))
  7152. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7153. (setq ext (match-string 1 dfile))))
  7154. (cond
  7155. ((equal in-emacs '(16))
  7156. (setq cmd (cdr (assoc 'system apps))))
  7157. (in-emacs (setq cmd 'emacs))
  7158. (t
  7159. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7160. (and dirp (cdr (assoc 'directory apps)))
  7161. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7162. 'string-match)
  7163. (cdr (assoc ext apps))
  7164. (cdr (assoc t apps))))))
  7165. (when (eq cmd 'system)
  7166. (setq cmd (cdr (assoc 'system apps))))
  7167. (when (eq cmd 'default)
  7168. (setq cmd (cdr (assoc t apps))))
  7169. (when (eq cmd 'mailcap)
  7170. (require 'mailcap)
  7171. (mailcap-parse-mailcaps)
  7172. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7173. (command (mailcap-mime-info mime-type)))
  7174. (if (stringp command)
  7175. (setq cmd command)
  7176. (setq cmd 'emacs))))
  7177. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7178. (not (file-exists-p file))
  7179. (not org-open-non-existing-files))
  7180. (error "No such file: %s" file))
  7181. (cond
  7182. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7183. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7184. (while (string-match "['\"]%s['\"]" cmd)
  7185. (setq cmd (replace-match "%s" t t cmd)))
  7186. (while (string-match "%s" cmd)
  7187. (setq cmd (replace-match
  7188. (save-match-data
  7189. (shell-quote-argument
  7190. (convert-standard-filename file)))
  7191. t t cmd)))
  7192. (save-window-excursion
  7193. (start-process-shell-command cmd nil cmd)
  7194. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7195. ))
  7196. ((or (stringp cmd)
  7197. (eq cmd 'emacs))
  7198. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7199. (widen)
  7200. (if line (goto-line line)
  7201. (if search (org-link-search search))))
  7202. ((consp cmd)
  7203. (let ((file (convert-standard-filename file)))
  7204. (eval cmd)))
  7205. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7206. (and (org-mode-p) (eq old-mode 'org-mode)
  7207. (or (not (equal old-buffer (current-buffer)))
  7208. (not (equal old-pos (point))))
  7209. (org-mark-ring-push old-pos old-buffer))))
  7210. (defun org-default-apps ()
  7211. "Return the default applications for this operating system."
  7212. (cond
  7213. ((eq system-type 'darwin)
  7214. org-file-apps-defaults-macosx)
  7215. ((eq system-type 'windows-nt)
  7216. org-file-apps-defaults-windowsnt)
  7217. (t org-file-apps-defaults-gnu)))
  7218. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7219. "Convert extensions to regular expressions in the cars of LIST.
  7220. Also, weed out any non-string entries, because the return value is used
  7221. only for regexp matching.
  7222. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7223. point to the symbol `emacs', indicating that the file should
  7224. be opened in Emacs."
  7225. (append
  7226. (delq nil
  7227. (mapcar (lambda (x)
  7228. (if (not (stringp (car x)))
  7229. nil
  7230. (if (string-match "\\W" (car x))
  7231. x
  7232. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7233. list))
  7234. (if add-auto-mode
  7235. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7236. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7237. (defun org-file-remote-p (file)
  7238. "Test whether FILE specifies a location on a remote system.
  7239. Return non-nil if the location is indeed remote.
  7240. For example, the filename \"/user@host:/foo\" specifies a location
  7241. on the system \"/user@host:\"."
  7242. (cond ((fboundp 'file-remote-p)
  7243. (file-remote-p file))
  7244. ((fboundp 'tramp-handle-file-remote-p)
  7245. (tramp-handle-file-remote-p file))
  7246. ((and (boundp 'ange-ftp-name-format)
  7247. (string-match (car ange-ftp-name-format) file))
  7248. t)
  7249. (t nil)))
  7250. ;;;; Refiling
  7251. (defun org-get-org-file ()
  7252. "Read a filename, with default directory `org-directory'."
  7253. (let ((default (or org-default-notes-file remember-data-file)))
  7254. (read-file-name (format "File name [%s]: " default)
  7255. (file-name-as-directory org-directory)
  7256. default)))
  7257. (defun org-notes-order-reversed-p ()
  7258. "Check if the current file should receive notes in reversed order."
  7259. (cond
  7260. ((not org-reverse-note-order) nil)
  7261. ((eq t org-reverse-note-order) t)
  7262. ((not (listp org-reverse-note-order)) nil)
  7263. (t (catch 'exit
  7264. (let ((all org-reverse-note-order)
  7265. entry)
  7266. (while (setq entry (pop all))
  7267. (if (string-match (car entry) buffer-file-name)
  7268. (throw 'exit (cdr entry))))
  7269. nil)))))
  7270. (defvar org-refile-target-table nil
  7271. "The list of refile targets, created by `org-refile'.")
  7272. (defvar org-agenda-new-buffers nil
  7273. "Buffers created to visit agenda files.")
  7274. (defun org-get-refile-targets (&optional default-buffer)
  7275. "Produce a table with refile targets."
  7276. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7277. targets txt re files f desc descre fast-path-p level)
  7278. (message "Getting targets...")
  7279. (with-current-buffer (or default-buffer (current-buffer))
  7280. (while (setq entry (pop entries))
  7281. (setq files (car entry) desc (cdr entry))
  7282. (setq fast-path-p nil)
  7283. (cond
  7284. ((null files) (setq files (list (current-buffer))))
  7285. ((eq files 'org-agenda-files)
  7286. (setq files (org-agenda-files 'unrestricted)))
  7287. ((and (symbolp files) (fboundp files))
  7288. (setq files (funcall files)))
  7289. ((and (symbolp files) (boundp files))
  7290. (setq files (symbol-value files))))
  7291. (if (stringp files) (setq files (list files)))
  7292. (cond
  7293. ((eq (car desc) :tag)
  7294. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7295. ((eq (car desc) :todo)
  7296. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7297. ((eq (car desc) :regexp)
  7298. (setq descre (cdr desc)))
  7299. ((eq (car desc) :level)
  7300. (setq descre (concat "^\\*\\{" (number-to-string
  7301. (if org-odd-levels-only
  7302. (1- (* 2 (cdr desc)))
  7303. (cdr desc)))
  7304. "\\}[ \t]")))
  7305. ((eq (car desc) :maxlevel)
  7306. (setq fast-path-p t)
  7307. (setq descre (concat "^\\*\\{1," (number-to-string
  7308. (if org-odd-levels-only
  7309. (1- (* 2 (cdr desc)))
  7310. (cdr desc)))
  7311. "\\}[ \t]")))
  7312. (t (error "Bad refiling target description %s" desc)))
  7313. (while (setq f (pop files))
  7314. (save-excursion
  7315. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7316. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7317. (setq f (expand-file-name f))
  7318. (save-excursion
  7319. (save-restriction
  7320. (widen)
  7321. (goto-char (point-min))
  7322. (while (re-search-forward descre nil t)
  7323. (goto-char (point-at-bol))
  7324. (when (looking-at org-complex-heading-regexp)
  7325. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7326. txt (org-link-display-format (match-string 4))
  7327. re (concat "^" (regexp-quote
  7328. (buffer-substring (match-beginning 1)
  7329. (match-end 4)))))
  7330. (if (match-end 5) (setq re (concat re "[ \t]+"
  7331. (regexp-quote
  7332. (match-string 5)))))
  7333. (setq re (concat re "[ \t]*$"))
  7334. (when org-refile-use-outline-path
  7335. (setq txt (mapconcat 'org-protect-slash
  7336. (append
  7337. (if (eq org-refile-use-outline-path 'file)
  7338. (list (file-name-nondirectory
  7339. (buffer-file-name (buffer-base-buffer))))
  7340. (if (eq org-refile-use-outline-path 'full-file-path)
  7341. (list (buffer-file-name (buffer-base-buffer)))))
  7342. (org-get-outline-path fast-path-p level txt)
  7343. (list txt))
  7344. "/")))
  7345. (push (list txt f re (point)) targets))
  7346. (goto-char (point-at-eol))))))))
  7347. (message "Getting targets...done")
  7348. (nreverse targets))))
  7349. (defun org-protect-slash (s)
  7350. (while (string-match "/" s)
  7351. (setq s (replace-match "\\" t t s)))
  7352. s)
  7353. (defvar org-olpa (make-vector 20 nil))
  7354. (defun org-get-outline-path (&optional fastp level heading)
  7355. "Return the outline path to the current entry, as a list."
  7356. (if fastp
  7357. (progn
  7358. (if (> level 19)
  7359. (error "Outline path failure, more than 19 levels."))
  7360. (loop for i from level upto 19 do
  7361. (aset org-olpa i nil))
  7362. (prog1
  7363. (delq nil (append org-olpa nil))
  7364. (aset org-olpa level heading)))
  7365. (let (rtn)
  7366. (save-excursion
  7367. (while (org-up-heading-safe)
  7368. (when (looking-at org-complex-heading-regexp)
  7369. (push (org-match-string-no-properties 4) rtn)))
  7370. rtn))))
  7371. (defvar org-refile-history nil
  7372. "History for refiling operations.")
  7373. (defun org-refile (&optional goto default-buffer)
  7374. "Move the entry at point to another heading.
  7375. The list of target headings is compiled using the information in
  7376. `org-refile-targets', which see. This list is created before each use
  7377. and will therefore always be up-to-date.
  7378. At the target location, the entry is filed as a subitem of the target heading.
  7379. Depending on `org-reverse-note-order', the new subitem will either be the
  7380. first or the last subitem.
  7381. If there is an active region, all entries in that region will be moved.
  7382. However, the region must fulfil the requirement that the first heading
  7383. is the first one sets the top-level of the moved text - at most siblings
  7384. below it are allowed.
  7385. With prefix arg GOTO, the command will only visit the target location,
  7386. not actually move anything.
  7387. With a double prefix `C-u C-u', go to the location where the last refiling
  7388. operation has put the subtree."
  7389. (interactive "P")
  7390. (let* ((cbuf (current-buffer))
  7391. (regionp (org-region-active-p))
  7392. (region-start (and regionp (region-beginning)))
  7393. (region-end (and regionp (region-end)))
  7394. (region-length (and regionp (- region-end region-start)))
  7395. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7396. pos it nbuf file re level reversed)
  7397. (when regionp (goto-char region-start)
  7398. (unless (org-kill-is-subtree-p
  7399. (buffer-substring region-start region-end))
  7400. (error "The region is not a (sequence of) subtree(s)")))
  7401. (if (equal goto '(16))
  7402. (org-refile-goto-last-stored)
  7403. (when (setq it (org-refile-get-location
  7404. (if goto "Goto: " "Refile to: ") default-buffer))
  7405. (setq file (nth 1 it)
  7406. re (nth 2 it)
  7407. pos (nth 3 it))
  7408. (if (and (not goto)
  7409. (equal (buffer-file-name) file)
  7410. (if regionp
  7411. (and (>= pos region-start)
  7412. (<= pos region-end))
  7413. (and (>= pos (point))
  7414. (< pos (save-excursion
  7415. (org-end-of-subtree t t))))))
  7416. (error "Cannot refile to position inside the tree or region"))
  7417. (setq nbuf (or (find-buffer-visiting file)
  7418. (find-file-noselect file)))
  7419. (if goto
  7420. (progn
  7421. (switch-to-buffer nbuf)
  7422. (goto-char pos)
  7423. (org-show-context 'org-goto))
  7424. (if regionp
  7425. (progn
  7426. (kill-new (buffer-substring region-start region-end))
  7427. (org-save-markers-in-region region-start region-end))
  7428. (org-copy-subtree 1 nil t))
  7429. (save-excursion
  7430. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7431. (find-file-noselect file))))
  7432. (setq reversed (org-notes-order-reversed-p))
  7433. (save-excursion
  7434. (save-restriction
  7435. (widen)
  7436. (goto-char pos)
  7437. (looking-at outline-regexp)
  7438. (setq level (org-get-valid-level (funcall outline-level) 1))
  7439. (goto-char
  7440. (if reversed
  7441. (or (outline-next-heading) (point-max))
  7442. (or (save-excursion (outline-get-next-sibling))
  7443. (org-end-of-subtree t t)
  7444. (point-max))))
  7445. (if (not (bolp)) (newline))
  7446. (bookmark-set "org-refile-last-stored")
  7447. (org-paste-subtree level))))
  7448. (if regionp
  7449. (delete-region (point) (+ (point) region-length))
  7450. (org-cut-subtree))
  7451. (setq org-markers-to-move nil)
  7452. (message "Refiled to \"%s\"" (car it)))))))
  7453. (defun org-refile-goto-last-stored ()
  7454. "Go to the location where the last refile was stored."
  7455. (interactive)
  7456. (bookmark-jump "org-refile-last-stored")
  7457. (message "This is the location of the last refile"))
  7458. (defun org-refile-get-location (&optional prompt default-buffer)
  7459. "Prompt the user for a refile location, using PROMPT."
  7460. (let ((org-refile-targets org-refile-targets)
  7461. (org-refile-use-outline-path org-refile-use-outline-path))
  7462. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7463. (unless org-refile-target-table
  7464. (error "No refile targets"))
  7465. (let* ((cbuf (current-buffer))
  7466. (partial-completion-mode nil)
  7467. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7468. (cfunc (if (and org-refile-use-outline-path
  7469. org-outline-path-complete-in-steps)
  7470. 'org-olpath-completing-read
  7471. 'org-ido-completing-read))
  7472. (extra (if org-refile-use-outline-path "/" ""))
  7473. (filename (and cfn (expand-file-name cfn)))
  7474. (tbl (mapcar
  7475. (lambda (x)
  7476. (if (and (not (member org-refile-use-outline-path
  7477. '(file full-file-path)))
  7478. (not (equal filename (nth 1 x))))
  7479. (cons (concat (car x) extra " ("
  7480. (file-name-nondirectory (nth 1 x)) ")")
  7481. (cdr x))
  7482. (cons (concat (car x) extra) (cdr x))))
  7483. org-refile-target-table))
  7484. (completion-ignore-case t))
  7485. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7486. tbl)))
  7487. (defun org-olpath-completing-read (prompt collection &rest args)
  7488. "Read an outline path like a file name."
  7489. (let ((thetable collection)
  7490. (org-completion-use-ido nil)) ; does not work with ido.
  7491. (apply
  7492. 'org-ido-completing-read prompt
  7493. (lambda (string predicate &optional flag)
  7494. (let (rtn r f (l (length string)))
  7495. (cond
  7496. ((eq flag nil)
  7497. ;; try completion
  7498. (try-completion string thetable))
  7499. ((eq flag t)
  7500. ;; all-completions
  7501. (setq rtn (all-completions string thetable predicate))
  7502. (mapcar
  7503. (lambda (x)
  7504. (setq r (substring x l))
  7505. (if (string-match " ([^)]*)$" x)
  7506. (setq f (match-string 0 x))
  7507. (setq f ""))
  7508. (if (string-match "/" r)
  7509. (concat string (substring r 0 (match-end 0)) f)
  7510. x))
  7511. rtn))
  7512. ((eq flag 'lambda)
  7513. ;; exact match?
  7514. (assoc string thetable)))
  7515. ))
  7516. args)))
  7517. ;;;; Dynamic blocks
  7518. (defun org-find-dblock (name)
  7519. "Find the first dynamic block with name NAME in the buffer.
  7520. If not found, stay at current position and return nil."
  7521. (let (pos)
  7522. (save-excursion
  7523. (goto-char (point-min))
  7524. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7525. nil t)
  7526. (match-beginning 0))))
  7527. (if pos (goto-char pos))
  7528. pos))
  7529. (defconst org-dblock-start-re
  7530. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7531. "Matches the startline of a dynamic block, with parameters.")
  7532. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7533. "Matches the end of a dynamic block.")
  7534. (defun org-create-dblock (plist)
  7535. "Create a dynamic block section, with parameters taken from PLIST.
  7536. PLIST must contain a :name entry which is used as name of the block."
  7537. (unless (bolp) (newline))
  7538. (let ((name (plist-get plist :name)))
  7539. (insert "#+BEGIN: " name)
  7540. (while plist
  7541. (if (eq (car plist) :name)
  7542. (setq plist (cddr plist))
  7543. (insert " " (prin1-to-string (pop plist)))))
  7544. (insert "\n\n#+END:\n")
  7545. (beginning-of-line -2)))
  7546. (defun org-prepare-dblock ()
  7547. "Prepare dynamic block for refresh.
  7548. This empties the block, puts the cursor at the insert position and returns
  7549. the property list including an extra property :name with the block name."
  7550. (unless (looking-at org-dblock-start-re)
  7551. (error "Not at a dynamic block"))
  7552. (let* ((begdel (1+ (match-end 0)))
  7553. (name (org-no-properties (match-string 1)))
  7554. (params (append (list :name name)
  7555. (read (concat "(" (match-string 3) ")")))))
  7556. (unless (re-search-forward org-dblock-end-re nil t)
  7557. (error "Dynamic block not terminated"))
  7558. (setq params
  7559. (append params
  7560. (list :content (buffer-substring
  7561. begdel (match-beginning 0)))))
  7562. (delete-region begdel (match-beginning 0))
  7563. (goto-char begdel)
  7564. (open-line 1)
  7565. params))
  7566. (defun org-map-dblocks (&optional command)
  7567. "Apply COMMAND to all dynamic blocks in the current buffer.
  7568. If COMMAND is not given, use `org-update-dblock'."
  7569. (let ((cmd (or command 'org-update-dblock))
  7570. pos)
  7571. (save-excursion
  7572. (goto-char (point-min))
  7573. (while (re-search-forward org-dblock-start-re nil t)
  7574. (goto-char (setq pos (match-beginning 0)))
  7575. (condition-case nil
  7576. (funcall cmd)
  7577. (error (message "Error during update of dynamic block")))
  7578. (goto-char pos)
  7579. (unless (re-search-forward org-dblock-end-re nil t)
  7580. (error "Dynamic block not terminated"))))))
  7581. (defun org-dblock-update (&optional arg)
  7582. "User command for updating dynamic blocks.
  7583. Update the dynamic block at point. With prefix ARG, update all dynamic
  7584. blocks in the buffer."
  7585. (interactive "P")
  7586. (if arg
  7587. (org-update-all-dblocks)
  7588. (or (looking-at org-dblock-start-re)
  7589. (org-beginning-of-dblock))
  7590. (org-update-dblock)))
  7591. (defun org-update-dblock ()
  7592. "Update the dynamic block at point
  7593. This means to empty the block, parse for parameters and then call
  7594. the correct writing function."
  7595. (save-window-excursion
  7596. (let* ((pos (point))
  7597. (line (org-current-line))
  7598. (params (org-prepare-dblock))
  7599. (name (plist-get params :name))
  7600. (cmd (intern (concat "org-dblock-write:" name))))
  7601. (message "Updating dynamic block `%s' at line %d..." name line)
  7602. (funcall cmd params)
  7603. (message "Updating dynamic block `%s' at line %d...done" name line)
  7604. (goto-char pos))))
  7605. (defun org-beginning-of-dblock ()
  7606. "Find the beginning of the dynamic block at point.
  7607. Error if there is no such block at point."
  7608. (let ((pos (point))
  7609. beg)
  7610. (end-of-line 1)
  7611. (if (and (re-search-backward org-dblock-start-re nil t)
  7612. (setq beg (match-beginning 0))
  7613. (re-search-forward org-dblock-end-re nil t)
  7614. (> (match-end 0) pos))
  7615. (goto-char beg)
  7616. (goto-char pos)
  7617. (error "Not in a dynamic block"))))
  7618. (defun org-update-all-dblocks ()
  7619. "Update all dynamic blocks in the buffer.
  7620. This function can be used in a hook."
  7621. (when (org-mode-p)
  7622. (org-map-dblocks 'org-update-dblock)))
  7623. ;;;; Completion
  7624. (defconst org-additional-option-like-keywords
  7625. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7626. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7627. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7628. "BEGIN_QUOTE" "END_QUOTE"
  7629. "BEGIN_VERSE" "END_VERSE"
  7630. "BEGIN_CENTER" "END_CENTER"
  7631. "BEGIN_SRC" "END_SRC"
  7632. "CATEGORY" "COLUMNS"
  7633. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7634. (defcustom org-structure-template-alist
  7635. '(
  7636. ("s" "#+begin_src ?\n\n#+end_src"
  7637. "<src lang=\"?\">\n\n</src>")
  7638. ("e" "#+begin_example\n?\n#+end_example"
  7639. "<example>\n?\n</example>")
  7640. ("q" "#+begin_quote\n?\n#+end_quote"
  7641. "<quote>\n?\n</quote>")
  7642. ("v" "#+begin_verse\n?\n#+end_verse"
  7643. "<verse>\n?\n/verse>")
  7644. ("c" "#+begin_center\n?\n#+end_center"
  7645. "<center>\n?\n/center>")
  7646. ("l" "#+begin_latex\n?\n#+end_latex"
  7647. "<literal style=\"latex\">\n?\n</literal>")
  7648. ("L" "#+latex: "
  7649. "<literal style=\"latex\">?</literal>")
  7650. ("h" "#+begin_html\n?\n#+end_html"
  7651. "<literal style=\"html\">\n?\n</literal>")
  7652. ("H" "#+html: "
  7653. "<literal style=\"html\">?</literal>")
  7654. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7655. ("A" "#+ascii: ")
  7656. ("i" "#+include %file ?"
  7657. "<include file=%file markup=\"?\">")
  7658. )
  7659. "Structure completion elements.
  7660. This is a list of abbreviation keys and values. The value gets inserted
  7661. it you type @samp{.} followed by the key and then the completion key,
  7662. usually `M-TAB'. %file will be replaced by a file name after prompting
  7663. for the file using completion.
  7664. There are two templates for each key, the first uses the original Org syntax,
  7665. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7666. the default when the /org-mtags.el/ module has been loaded. See also the
  7667. variable `org-mtags-prefer-muse-templates'.
  7668. This is an experimental feature, it is undecided if it is going to stay in."
  7669. :group 'org-completion
  7670. :type '(repeat
  7671. (string :tag "Key")
  7672. (string :tag "Template")
  7673. (string :tag "Muse Template")))
  7674. (defun org-try-structure-completion ()
  7675. "Try to complete a structure template before point.
  7676. This looks for strings like \"<e\" on an otherwise empty line and
  7677. expands them."
  7678. (let ((l (buffer-substring (point-at-bol) (point)))
  7679. a)
  7680. (when (and (looking-at "[ \t]*$")
  7681. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7682. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7683. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7684. (match-beginning 1)) a)
  7685. t)))
  7686. (defun org-complete-expand-structure-template (start cell)
  7687. "Expand a structure template."
  7688. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7689. (rpl (nth (if musep 2 1) cell)))
  7690. (delete-region start (point))
  7691. (when (string-match "\\`#\\+" rpl)
  7692. (cond
  7693. ((bolp))
  7694. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7695. (delete-region (point-at-bol) (point)))
  7696. (t (newline))))
  7697. (setq start (point))
  7698. (if (string-match "%file" rpl)
  7699. (setq rpl (replace-match
  7700. (concat
  7701. "\""
  7702. (save-match-data
  7703. (abbreviate-file-name (read-file-name "Include file: ")))
  7704. "\"")
  7705. t t rpl)))
  7706. (insert rpl)
  7707. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7708. (defun org-complete (&optional arg)
  7709. "Perform completion on word at point.
  7710. At the beginning of a headline, this completes TODO keywords as given in
  7711. `org-todo-keywords'.
  7712. If the current word is preceded by a backslash, completes the TeX symbols
  7713. that are supported for HTML support.
  7714. If the current word is preceded by \"#+\", completes special words for
  7715. setting file options.
  7716. In the line after \"#+STARTUP:, complete valid keywords.\"
  7717. At all other locations, this simply calls the value of
  7718. `org-completion-fallback-command'."
  7719. (interactive "P")
  7720. (org-without-partial-completion
  7721. (catch 'exit
  7722. (let* ((a nil)
  7723. (end (point))
  7724. (beg1 (save-excursion
  7725. (skip-chars-backward (org-re "[:alnum:]_@"))
  7726. (point)))
  7727. (beg (save-excursion
  7728. (skip-chars-backward "a-zA-Z0-9_:$")
  7729. (point)))
  7730. (confirm (lambda (x) (stringp (car x))))
  7731. (searchhead (equal (char-before beg) ?*))
  7732. (struct
  7733. (when (and (member (char-before beg1) '(?. ?<))
  7734. (setq a (assoc (buffer-substring beg1 (point))
  7735. org-structure-template-alist)))
  7736. (org-complete-expand-structure-template (1- beg1) a)
  7737. (throw 'exit t)))
  7738. (tag (and (equal (char-before beg1) ?:)
  7739. (equal (char-after (point-at-bol)) ?*)))
  7740. (prop (and (equal (char-before beg1) ?:)
  7741. (not (equal (char-after (point-at-bol)) ?*))))
  7742. (texp (equal (char-before beg) ?\\))
  7743. (link (equal (char-before beg) ?\[))
  7744. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7745. beg)
  7746. "#+"))
  7747. (startup (string-match "^#\\+STARTUP:.*"
  7748. (buffer-substring (point-at-bol) (point))))
  7749. (completion-ignore-case opt)
  7750. (type nil)
  7751. (tbl nil)
  7752. (table (cond
  7753. (opt
  7754. (setq type :opt)
  7755. (require 'org-exp)
  7756. (append
  7757. (mapcar
  7758. (lambda (x)
  7759. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7760. (cons (match-string 2 x) (match-string 1 x)))
  7761. (org-split-string (org-get-current-options) "\n"))
  7762. (mapcar 'list org-additional-option-like-keywords)))
  7763. (startup
  7764. (setq type :startup)
  7765. org-startup-options)
  7766. (link (append org-link-abbrev-alist-local
  7767. org-link-abbrev-alist))
  7768. (texp
  7769. (setq type :tex)
  7770. org-html-entities)
  7771. ((string-match "\\`\\*+[ \t]+\\'"
  7772. (buffer-substring (point-at-bol) beg))
  7773. (setq type :todo)
  7774. (mapcar 'list org-todo-keywords-1))
  7775. (searchhead
  7776. (setq type :searchhead)
  7777. (save-excursion
  7778. (goto-char (point-min))
  7779. (while (re-search-forward org-todo-line-regexp nil t)
  7780. (push (list
  7781. (org-make-org-heading-search-string
  7782. (match-string 3) t))
  7783. tbl)))
  7784. tbl)
  7785. (tag (setq type :tag beg beg1)
  7786. (or org-tag-alist (org-get-buffer-tags)))
  7787. (prop (setq type :prop beg beg1)
  7788. (mapcar 'list (org-buffer-property-keys nil t t)))
  7789. (t (progn
  7790. (call-interactively org-completion-fallback-command)
  7791. (throw 'exit nil)))))
  7792. (pattern (buffer-substring-no-properties beg end))
  7793. (completion (try-completion pattern table confirm)))
  7794. (cond ((eq completion t)
  7795. (if (not (assoc (upcase pattern) table))
  7796. (message "Already complete")
  7797. (if (and (equal type :opt)
  7798. (not (member (car (assoc (upcase pattern) table))
  7799. org-additional-option-like-keywords)))
  7800. (insert (substring (cdr (assoc (upcase pattern) table))
  7801. (length pattern)))
  7802. (if (memq type '(:tag :prop)) (insert ":")))))
  7803. ((null completion)
  7804. (message "Can't find completion for \"%s\"" pattern)
  7805. (ding))
  7806. ((not (string= pattern completion))
  7807. (delete-region beg end)
  7808. (if (string-match " +$" completion)
  7809. (setq completion (replace-match "" t t completion)))
  7810. (insert completion)
  7811. (if (get-buffer-window "*Completions*")
  7812. (delete-window (get-buffer-window "*Completions*")))
  7813. (if (assoc completion table)
  7814. (if (eq type :todo) (insert " ")
  7815. (if (memq type '(:tag :prop)) (insert ":"))))
  7816. (if (and (equal type :opt) (assoc completion table))
  7817. (message "%s" (substitute-command-keys
  7818. "Press \\[org-complete] again to insert example settings"))))
  7819. (t
  7820. (message "Making completion list...")
  7821. (let ((list (sort (all-completions pattern table confirm)
  7822. 'string<)))
  7823. (with-output-to-temp-buffer "*Completions*"
  7824. (condition-case nil
  7825. ;; Protection needed for XEmacs and emacs 21
  7826. (display-completion-list list pattern)
  7827. (error (display-completion-list list)))))
  7828. (message "Making completion list...%s" "done")))))))
  7829. ;;;; TODO, DEADLINE, Comments
  7830. (defun org-toggle-comment ()
  7831. "Change the COMMENT state of an entry."
  7832. (interactive)
  7833. (save-excursion
  7834. (org-back-to-heading)
  7835. (let (case-fold-search)
  7836. (if (looking-at (concat outline-regexp
  7837. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7838. (replace-match "" t t nil 1)
  7839. (if (looking-at outline-regexp)
  7840. (progn
  7841. (goto-char (match-end 0))
  7842. (insert org-comment-string " ")))))))
  7843. (defvar org-last-todo-state-is-todo nil
  7844. "This is non-nil when the last TODO state change led to a TODO state.
  7845. If the last change removed the TODO tag or switched to DONE, then
  7846. this is nil.")
  7847. (defvar org-setting-tags nil) ; dynamically skipped
  7848. (defun org-parse-local-options (string var)
  7849. "Parse STRING for startup setting relevant for variable VAR."
  7850. (let ((rtn (symbol-value var))
  7851. e opts)
  7852. (save-match-data
  7853. (if (or (not string) (not (string-match "\\S-" string)))
  7854. rtn
  7855. (setq opts (delq nil (mapcar (lambda (x)
  7856. (setq e (assoc x org-startup-options))
  7857. (if (eq (nth 1 e) var) e nil))
  7858. (org-split-string string "[ \t]+"))))
  7859. (if (not opts)
  7860. rtn
  7861. (setq rtn nil)
  7862. (while (setq e (pop opts))
  7863. (if (not (nth 3 e))
  7864. (setq rtn (nth 2 e))
  7865. (if (not (listp rtn)) (setq rtn nil))
  7866. (push (nth 2 e) rtn)))
  7867. rtn)))))
  7868. (defvar org-todo-setup-filter-hook nil
  7869. "Hook for functions that pre-filter todo specs.
  7870. Each function takes a todo spec and returns either `nil' or the spec
  7871. transformed into canonical form." )
  7872. (defvar org-todo-get-default-hook nil
  7873. "Hook for functions that get a default item for todo.
  7874. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7875. `nil' or a string to be used for the todo mark." )
  7876. (defvar org-agenda-headline-snapshot-before-repeat)
  7877. (defun org-todo (&optional arg)
  7878. "Change the TODO state of an item.
  7879. The state of an item is given by a keyword at the start of the heading,
  7880. like
  7881. *** TODO Write paper
  7882. *** DONE Call mom
  7883. The different keywords are specified in the variable `org-todo-keywords'.
  7884. By default the available states are \"TODO\" and \"DONE\".
  7885. So for this example: when the item starts with TODO, it is changed to DONE.
  7886. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7887. DONE are present, add TODO at the beginning of the heading.
  7888. With C-u prefix arg, use completion to determine the new state.
  7889. With numeric prefix arg, switch to that state.
  7890. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7891. With a tripple C-u prefix, circumvent any state blocking.
  7892. For calling through lisp, arg is also interpreted in the following way:
  7893. 'none -> empty state
  7894. \"\"(empty string) -> switch to empty state
  7895. 'done -> switch to DONE
  7896. 'nextset -> switch to the next set of keywords
  7897. 'previousset -> switch to the previous set of keywords
  7898. \"WAITING\" -> switch to the specified keyword, but only if it
  7899. really is a member of `org-todo-keywords'."
  7900. (interactive "P")
  7901. (if (equal arg '(16)) (setq arg 'nextset))
  7902. (let ((org-blocker-hook org-blocker-hook))
  7903. (when (equal arg '(64))
  7904. (setq arg nil org-blocker-hook nil))
  7905. (save-excursion
  7906. (catch 'exit
  7907. (org-back-to-heading)
  7908. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7909. (or (looking-at (concat " +" org-todo-regexp " *"))
  7910. (looking-at " *"))
  7911. (let* ((match-data (match-data))
  7912. (startpos (point-at-bol))
  7913. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7914. (org-log-done org-log-done)
  7915. (org-log-repeat org-log-repeat)
  7916. (org-todo-log-states org-todo-log-states)
  7917. (this (match-string 1))
  7918. (hl-pos (match-beginning 0))
  7919. (head (org-get-todo-sequence-head this))
  7920. (ass (assoc head org-todo-kwd-alist))
  7921. (interpret (nth 1 ass))
  7922. (done-word (nth 3 ass))
  7923. (final-done-word (nth 4 ass))
  7924. (last-state (or this ""))
  7925. (completion-ignore-case t)
  7926. (member (member this org-todo-keywords-1))
  7927. (tail (cdr member))
  7928. (state (cond
  7929. ((and org-todo-key-trigger
  7930. (or (and (equal arg '(4))
  7931. (eq org-use-fast-todo-selection 'prefix))
  7932. (and (not arg) org-use-fast-todo-selection
  7933. (not (eq org-use-fast-todo-selection
  7934. 'prefix)))))
  7935. ;; Use fast selection
  7936. (org-fast-todo-selection))
  7937. ((and (equal arg '(4))
  7938. (or (not org-use-fast-todo-selection)
  7939. (not org-todo-key-trigger)))
  7940. ;; Read a state with completion
  7941. (org-ido-completing-read
  7942. "State: " (mapcar (lambda(x) (list x))
  7943. org-todo-keywords-1)
  7944. nil t))
  7945. ((eq arg 'right)
  7946. (if this
  7947. (if tail (car tail) nil)
  7948. (car org-todo-keywords-1)))
  7949. ((eq arg 'left)
  7950. (if (equal member org-todo-keywords-1)
  7951. nil
  7952. (if this
  7953. (nth (- (length org-todo-keywords-1)
  7954. (length tail) 2)
  7955. org-todo-keywords-1)
  7956. (org-last org-todo-keywords-1))))
  7957. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7958. (setq arg nil))) ; hack to fall back to cycling
  7959. (arg
  7960. ;; user or caller requests a specific state
  7961. (cond
  7962. ((equal arg "") nil)
  7963. ((eq arg 'none) nil)
  7964. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7965. ((eq arg 'nextset)
  7966. (or (car (cdr (member head org-todo-heads)))
  7967. (car org-todo-heads)))
  7968. ((eq arg 'previousset)
  7969. (let ((org-todo-heads (reverse org-todo-heads)))
  7970. (or (car (cdr (member head org-todo-heads)))
  7971. (car org-todo-heads))))
  7972. ((car (member arg org-todo-keywords-1)))
  7973. ((nth (1- (prefix-numeric-value arg))
  7974. org-todo-keywords-1))))
  7975. ((null member) (or head (car org-todo-keywords-1)))
  7976. ((equal this final-done-word) nil) ;; -> make empty
  7977. ((null tail) nil) ;; -> first entry
  7978. ((memq interpret '(type priority))
  7979. (if (eq this-command last-command)
  7980. (car tail)
  7981. (if (> (length tail) 0)
  7982. (or done-word (car org-done-keywords))
  7983. nil)))
  7984. (t
  7985. (car tail))))
  7986. (state (or
  7987. (run-hook-with-args-until-success
  7988. 'org-todo-get-default-hook state last-state)
  7989. state))
  7990. (next (if state (concat " " state " ") " "))
  7991. (change-plist (list :type 'todo-state-change :from this :to state
  7992. :position startpos))
  7993. dolog now-done-p)
  7994. (when org-blocker-hook
  7995. (setq org-last-todo-state-is-todo
  7996. (not (member this org-done-keywords)))
  7997. (unless (save-excursion
  7998. (save-match-data
  7999. (run-hook-with-args-until-failure
  8000. 'org-blocker-hook change-plist)))
  8001. (if (interactive-p)
  8002. (error "TODO state change from %s to %s blocked" this state)
  8003. ;; fail silently
  8004. (message "TODO state change from %s to %s blocked" this state)
  8005. (throw 'exit nil))))
  8006. (store-match-data match-data)
  8007. (replace-match next t t)
  8008. (unless (pos-visible-in-window-p hl-pos)
  8009. (message "TODO state changed to %s" (org-trim next)))
  8010. (unless head
  8011. (setq head (org-get-todo-sequence-head state)
  8012. ass (assoc head org-todo-kwd-alist)
  8013. interpret (nth 1 ass)
  8014. done-word (nth 3 ass)
  8015. final-done-word (nth 4 ass)))
  8016. (when (memq arg '(nextset previousset))
  8017. (message "Keyword-Set %d/%d: %s"
  8018. (- (length org-todo-sets) -1
  8019. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8020. (length org-todo-sets)
  8021. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8022. (setq org-last-todo-state-is-todo
  8023. (not (member state org-done-keywords)))
  8024. (setq now-done-p (and (member state org-done-keywords)
  8025. (not (member this org-done-keywords))))
  8026. (and logging (org-local-logging logging))
  8027. (when (and (or org-todo-log-states org-log-done)
  8028. (not (memq arg '(nextset previousset))))
  8029. ;; we need to look at recording a time and note
  8030. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8031. (nth 2 (assoc this org-todo-log-states))))
  8032. (when (and state
  8033. (member state org-not-done-keywords)
  8034. (not (member this org-not-done-keywords)))
  8035. ;; This is now a todo state and was not one before
  8036. ;; If there was a CLOSED time stamp, get rid of it.
  8037. (org-add-planning-info nil nil 'closed))
  8038. (when (and now-done-p org-log-done)
  8039. ;; It is now done, and it was not done before
  8040. (org-add-planning-info 'closed (org-current-time))
  8041. (if (and (not dolog) (eq 'note org-log-done))
  8042. (org-add-log-setup 'done state this 'findpos 'note)))
  8043. (when (and state dolog)
  8044. ;; This is a non-nil state, and we need to log it
  8045. (org-add-log-setup 'state state this 'findpos dolog)))
  8046. ;; Fixup tag positioning
  8047. (org-todo-trigger-tag-changes state)
  8048. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8049. (when org-provide-todo-statistics
  8050. (org-update-parent-todo-statistics))
  8051. (run-hooks 'org-after-todo-state-change-hook)
  8052. (if (and arg (not (member state org-done-keywords)))
  8053. (setq head (org-get-todo-sequence-head state)))
  8054. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8055. ;; Do we need to trigger a repeat?
  8056. (when now-done-p
  8057. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8058. ;; This is for the agenda, take a snapshot of the headline.
  8059. (save-match-data
  8060. (setq org-agenda-headline-snapshot-before-repeat
  8061. (org-get-heading))))
  8062. (org-auto-repeat-maybe state))
  8063. ;; Fixup cursor location if close to the keyword
  8064. (if (and (outline-on-heading-p)
  8065. (not (bolp))
  8066. (save-excursion (beginning-of-line 1)
  8067. (looking-at org-todo-line-regexp))
  8068. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8069. (progn
  8070. (goto-char (or (match-end 2) (match-end 1)))
  8071. (just-one-space)))
  8072. (when org-trigger-hook
  8073. (save-excursion
  8074. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8075. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8076. "Block turning an entry into a TODO, using the hierarchy.
  8077. This checks whether the current task should be blocked from state
  8078. changes. Such blocking occurs when:
  8079. 1. The task has children which are not all in a completed state.
  8080. 2. A task has a parent with the property :ORDERED:, and there
  8081. are siblings prior to the current task with incomplete
  8082. status.
  8083. 3. The parent of the task is blocked because it has siblings that should
  8084. be done first, or is child of a block grandparent TODO entry."
  8085. (catch 'dont-block
  8086. ;; If this is not a todo state change, or if this entry is already DONE,
  8087. ;; do not block
  8088. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8089. (member (plist-get change-plist :from)
  8090. (cons 'done org-done-keywords))
  8091. (member (plist-get change-plist :to)
  8092. (cons 'todo org-not-done-keywords)))
  8093. (throw 'dont-block t))
  8094. ;; If this task has children, and any are undone, it's blocked
  8095. (save-excursion
  8096. (org-back-to-heading t)
  8097. (let ((this-level (funcall outline-level)))
  8098. (outline-next-heading)
  8099. (let ((child-level (funcall outline-level)))
  8100. (while (and (not (eobp))
  8101. (> child-level this-level))
  8102. ;; this todo has children, check whether they are all
  8103. ;; completed
  8104. (if (and (not (org-entry-is-done-p))
  8105. (org-entry-is-todo-p))
  8106. (throw 'dont-block nil))
  8107. (outline-next-heading)
  8108. (setq child-level (funcall outline-level))))))
  8109. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8110. ;; any previous siblings are undone, it's blocked
  8111. (save-excursion
  8112. (org-back-to-heading t)
  8113. (let* ((pos (point))
  8114. (parent-pos (and (org-up-heading-safe) (point))))
  8115. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8116. (when (and (org-entry-get (point) "ORDERED")
  8117. (forward-line 1)
  8118. (re-search-forward org-not-done-heading-regexp pos t))
  8119. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8120. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8121. (while t
  8122. (goto-char parent-pos)
  8123. (if (not (looking-at org-not-done-heading-regexp))
  8124. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8125. (setq pos (point))
  8126. (setq parent-pos (and (org-up-heading-safe) (point)))
  8127. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8128. (when (and (org-entry-get (point) "ORDERED")
  8129. (forward-line 1)
  8130. (re-search-forward org-not-done-heading-regexp pos t))
  8131. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8132. (defcustom org-track-ordered-property-with-tag nil
  8133. "Should the ORDERED property also be shown as a tag?
  8134. The ORDERED property decides if an entry should require subtasks to be
  8135. completed in sequence. Since a property is not very visible, setting
  8136. this option means that toggling the ORDERED property with the command
  8137. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8138. not relevant for the behavior, but it makes things more visible.
  8139. Note that toggling the tag with tags commands will not change the property
  8140. and therefore not influence behavior!
  8141. This can be t, meaning the tag ORDERED should be used, It can also be a
  8142. string to select a different tag for this task."
  8143. :group 'org-todo
  8144. :type '(choice
  8145. (const :tag "No tracking" nil)
  8146. (const :tag "Track with ORDERED tag" t)
  8147. (string :tag "Use other tag")))
  8148. (defun org-toggle-ordered-property ()
  8149. "Toggle the ORDERED property of the current entry.
  8150. For better visibility, you can track the value of this property with a tag.
  8151. See variable `org-track-ordered-property-with-tag'."
  8152. (interactive)
  8153. (let* ((t1 org-track-ordered-property-with-tag)
  8154. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8155. (save-excursion
  8156. (org-back-to-heading)
  8157. (if (org-entry-get nil "ORDERED")
  8158. (progn
  8159. (org-delete-property "ORDERED")
  8160. (and tag (org-toggle-tag tag 'off))
  8161. (message "Subtasks can be completed in arbitrary order"))
  8162. (org-entry-put nil "ORDERED" "t")
  8163. (and tag (org-toggle-tag tag 'on))
  8164. (message "Subtasks must be completed in sequence")))))
  8165. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8166. (defun org-block-todo-from-checkboxes (change-plist)
  8167. "Block turning an entry into a TODO, using checkboxes.
  8168. This checks whether the current task should be blocked from state
  8169. changes because there are uncheckd boxes in this entry."
  8170. (catch 'dont-block
  8171. ;; If this is not a todo state change, or if this entry is already DONE,
  8172. ;; do not block
  8173. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8174. (member (plist-get change-plist :from)
  8175. (cons 'done org-done-keywords))
  8176. (member (plist-get change-plist :to)
  8177. (cons 'todo org-not-done-keywords)))
  8178. (throw 'dont-block t))
  8179. ;; If this task has checkboxes that are not checked, it's blocked
  8180. (save-excursion
  8181. (org-back-to-heading t)
  8182. (let ((beg (point)) end)
  8183. (outline-next-heading)
  8184. (setq end (point))
  8185. (goto-char beg)
  8186. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8187. end t)
  8188. (progn
  8189. (if (boundp 'org-blocked-by-checkboxes)
  8190. (setq org-blocked-by-checkboxes t))
  8191. (throw 'dont-block nil)))))
  8192. t)) ; do not block
  8193. (defun org-update-parent-todo-statistics ()
  8194. "Update any statistics cookie in the parent of the current headline."
  8195. (interactive)
  8196. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8197. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8198. (catch 'exit
  8199. (save-excursion
  8200. (setq level (org-up-heading-safe))
  8201. (unless level
  8202. (throw 'exit nil))
  8203. (while (re-search-forward box-re (point-at-eol) t)
  8204. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8205. (setq is-percent (match-end 2))
  8206. (save-match-data
  8207. (unless (outline-next-heading) (throw 'exit nil))
  8208. (while (looking-at org-todo-line-regexp)
  8209. (setq kwd (match-string 2))
  8210. (and kwd (setq cnt-all (1+ cnt-all)))
  8211. (and (member kwd org-done-keywords)
  8212. (setq cnt-done (1+ cnt-done)))
  8213. (condition-case nil
  8214. (org-forward-same-level 1)
  8215. (error (end-of-line 1)))))
  8216. (replace-match
  8217. (if is-percent
  8218. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8219. (format "[%d/%d]" cnt-done cnt-all))))
  8220. (when cookie-present
  8221. (run-hook-with-args 'org-after-todo-statistics-hook
  8222. cnt-done (- cnt-all cnt-done)))))))
  8223. (defvar org-after-todo-statistics-hook nil
  8224. "Hook that is called after a TODO statistics cookie has been updated.
  8225. Each function is called with two arguments: the number of not-done entries
  8226. and the number of done entries.
  8227. For example, the following function, when added to this hook, will switch
  8228. an entry to DONE when all children are done, and back to TODO when new
  8229. entries are set to a TODO status. Note that this hook is only called
  8230. when there is a statistics cookie in the headline!
  8231. (defun org-summary-todo (n-done n-not-done)
  8232. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8233. (let (org-log-done org-log-states) ; turn off logging
  8234. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8235. ")
  8236. (defun org-todo-trigger-tag-changes (state)
  8237. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8238. (let ((l org-todo-state-tags-triggers)
  8239. changes)
  8240. (when (or (not state) (equal state ""))
  8241. (setq changes (append changes (cdr (assoc "" l)))))
  8242. (when (and (stringp state) (> (length state) 0))
  8243. (setq changes (append changes (cdr (assoc state l)))))
  8244. (when (member state org-not-done-keywords)
  8245. (setq changes (append changes (cdr (assoc 'todo l)))))
  8246. (when (member state org-done-keywords)
  8247. (setq changes (append changes (cdr (assoc 'done l)))))
  8248. (dolist (c changes)
  8249. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8250. (defun org-local-logging (value)
  8251. "Get logging settings from a property VALUE."
  8252. (let* (words w a)
  8253. ;; directly set the variables, they are already local.
  8254. (setq org-log-done nil
  8255. org-log-repeat nil
  8256. org-todo-log-states nil)
  8257. (setq words (org-split-string value))
  8258. (while (setq w (pop words))
  8259. (cond
  8260. ((setq a (assoc w org-startup-options))
  8261. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8262. (set (nth 1 a) (nth 2 a))))
  8263. ((setq a (org-extract-log-state-settings w))
  8264. (and (member (car a) org-todo-keywords-1)
  8265. (push a org-todo-log-states)))))))
  8266. (defun org-get-todo-sequence-head (kwd)
  8267. "Return the head of the TODO sequence to which KWD belongs.
  8268. If KWD is not set, check if there is a text property remembering the
  8269. right sequence."
  8270. (let (p)
  8271. (cond
  8272. ((not kwd)
  8273. (or (get-text-property (point-at-bol) 'org-todo-head)
  8274. (progn
  8275. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8276. nil (point-at-eol)))
  8277. (get-text-property p 'org-todo-head))))
  8278. ((not (member kwd org-todo-keywords-1))
  8279. (car org-todo-keywords-1))
  8280. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8281. (defun org-fast-todo-selection ()
  8282. "Fast TODO keyword selection with single keys.
  8283. Returns the new TODO keyword, or nil if no state change should occur."
  8284. (let* ((fulltable org-todo-key-alist)
  8285. (done-keywords org-done-keywords) ;; needed for the faces.
  8286. (maxlen (apply 'max (mapcar
  8287. (lambda (x)
  8288. (if (stringp (car x)) (string-width (car x)) 0))
  8289. fulltable)))
  8290. (expert nil)
  8291. (fwidth (+ maxlen 3 1 3))
  8292. (ncol (/ (- (window-width) 4) fwidth))
  8293. tg cnt e c tbl
  8294. groups ingroup)
  8295. (save-excursion
  8296. (save-window-excursion
  8297. (if expert
  8298. (set-buffer (get-buffer-create " *Org todo*"))
  8299. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8300. (erase-buffer)
  8301. (org-set-local 'org-done-keywords done-keywords)
  8302. (setq tbl fulltable cnt 0)
  8303. (while (setq e (pop tbl))
  8304. (cond
  8305. ((equal e '(:startgroup))
  8306. (push '() groups) (setq ingroup t)
  8307. (when (not (= cnt 0))
  8308. (setq cnt 0)
  8309. (insert "\n"))
  8310. (insert "{ "))
  8311. ((equal e '(:endgroup))
  8312. (setq ingroup nil cnt 0)
  8313. (insert "}\n"))
  8314. ((equal e '(:newline))
  8315. (when (not (= cnt 0))
  8316. (setq cnt 0)
  8317. (insert "\n")
  8318. (setq e (car tbl))
  8319. (while (equal (car tbl) '(:newline))
  8320. (insert "\n")
  8321. (setq tbl (cdr tbl)))))
  8322. (t
  8323. (setq tg (car e) c (cdr e))
  8324. (if ingroup (push tg (car groups)))
  8325. (setq tg (org-add-props tg nil 'face
  8326. (org-get-todo-face tg)))
  8327. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8328. (insert "[" c "] " tg (make-string
  8329. (- fwidth 4 (length tg)) ?\ ))
  8330. (when (= (setq cnt (1+ cnt)) ncol)
  8331. (insert "\n")
  8332. (if ingroup (insert " "))
  8333. (setq cnt 0)))))
  8334. (insert "\n")
  8335. (goto-char (point-min))
  8336. (if (not expert) (org-fit-window-to-buffer))
  8337. (message "[a-z..]:Set [SPC]:clear")
  8338. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8339. (cond
  8340. ((or (= c ?\C-g)
  8341. (and (= c ?q) (not (rassoc c fulltable))))
  8342. (setq quit-flag t))
  8343. ((= c ?\ ) nil)
  8344. ((setq e (rassoc c fulltable) tg (car e))
  8345. tg)
  8346. (t (setq quit-flag t)))))))
  8347. (defun org-entry-is-todo-p ()
  8348. (member (org-get-todo-state) org-not-done-keywords))
  8349. (defun org-entry-is-done-p ()
  8350. (member (org-get-todo-state) org-done-keywords))
  8351. (defun org-get-todo-state ()
  8352. (save-excursion
  8353. (org-back-to-heading t)
  8354. (and (looking-at org-todo-line-regexp)
  8355. (match-end 2)
  8356. (match-string 2))))
  8357. (defun org-at-date-range-p (&optional inactive-ok)
  8358. "Is the cursor inside a date range?"
  8359. (interactive)
  8360. (save-excursion
  8361. (catch 'exit
  8362. (let ((pos (point)))
  8363. (skip-chars-backward "^[<\r\n")
  8364. (skip-chars-backward "<[")
  8365. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8366. (>= (match-end 0) pos)
  8367. (throw 'exit t))
  8368. (skip-chars-backward "^<[\r\n")
  8369. (skip-chars-backward "<[")
  8370. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8371. (>= (match-end 0) pos)
  8372. (throw 'exit t)))
  8373. nil)))
  8374. (defun org-get-repeat ()
  8375. "Check if there is a deadline/schedule with repeater in this entry."
  8376. (save-match-data
  8377. (save-excursion
  8378. (org-back-to-heading t)
  8379. (if (re-search-forward
  8380. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8381. (match-string 1)))))
  8382. (defvar org-last-changed-timestamp)
  8383. (defvar org-last-inserted-timestamp)
  8384. (defvar org-log-post-message)
  8385. (defvar org-log-note-purpose)
  8386. (defvar org-log-note-how)
  8387. (defvar org-log-note-extra)
  8388. (defun org-auto-repeat-maybe (done-word)
  8389. "Check if the current headline contains a repeated deadline/schedule.
  8390. If yes, set TODO state back to what it was and change the base date
  8391. of repeating deadline/scheduled time stamps to new date.
  8392. This function is run automatically after each state change to a DONE state."
  8393. ;; last-state is dynamically scoped into this function
  8394. (let* ((repeat (org-get-repeat))
  8395. (aa (assoc last-state org-todo-kwd-alist))
  8396. (interpret (nth 1 aa))
  8397. (head (nth 2 aa))
  8398. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8399. (msg "Entry repeats: ")
  8400. (org-log-done nil)
  8401. (org-todo-log-states nil)
  8402. (nshiftmax 10) (nshift 0)
  8403. re type n what ts time)
  8404. (when repeat
  8405. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8406. (org-todo (if (eq interpret 'type) last-state head))
  8407. (when org-log-repeat
  8408. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8409. (memq 'org-add-log-note post-command-hook))
  8410. ;; OK, we are already setup for some record
  8411. (if (eq org-log-repeat 'note)
  8412. ;; make sure we take a note, not only a time stamp
  8413. (setq org-log-note-how 'note))
  8414. ;; Set up for taking a record
  8415. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8416. last-state
  8417. 'findpos org-log-repeat)))
  8418. (org-back-to-heading t)
  8419. (org-add-planning-info nil nil 'closed)
  8420. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8421. org-deadline-time-regexp "\\)\\|\\("
  8422. org-ts-regexp "\\)"))
  8423. (while (re-search-forward
  8424. re (save-excursion (outline-next-heading) (point)) t)
  8425. (setq type (if (match-end 1) org-scheduled-string
  8426. (if (match-end 3) org-deadline-string "Plain:"))
  8427. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8428. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8429. (setq n (string-to-number (match-string 2 ts))
  8430. what (match-string 3 ts))
  8431. (if (equal what "w") (setq n (* n 7) what "d"))
  8432. ;; Preparation, see if we need to modify the start date for the change
  8433. (when (match-end 1)
  8434. (setq time (save-match-data (org-time-string-to-time ts)))
  8435. (cond
  8436. ((equal (match-string 1 ts) ".")
  8437. ;; Shift starting date to today
  8438. (org-timestamp-change
  8439. (- (time-to-days (current-time)) (time-to-days time))
  8440. 'day))
  8441. ((equal (match-string 1 ts) "+")
  8442. (while (or (= nshift 0)
  8443. (<= (time-to-days time) (time-to-days (current-time))))
  8444. (when (= (incf nshift) nshiftmax)
  8445. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8446. (error "Abort")))
  8447. (org-timestamp-change n (cdr (assoc what whata)))
  8448. (org-at-timestamp-p t)
  8449. (setq ts (match-string 1))
  8450. (setq time (save-match-data (org-time-string-to-time ts))))
  8451. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8452. ;; rematch, so that we have everything in place for the real shift
  8453. (org-at-timestamp-p t)
  8454. (setq ts (match-string 1))
  8455. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8456. (org-timestamp-change n (cdr (assoc what whata)))
  8457. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8458. (setq org-log-post-message msg)
  8459. (message "%s" msg))))
  8460. (defun org-show-todo-tree (arg)
  8461. "Make a compact tree which shows all headlines marked with TODO.
  8462. The tree will show the lines where the regexp matches, and all higher
  8463. headlines above the match.
  8464. With a \\[universal-argument] prefix, also show the DONE entries.
  8465. With a numeric prefix N, construct a sparse tree for the Nth element
  8466. of `org-todo-keywords-1'."
  8467. (interactive "P")
  8468. (let ((case-fold-search nil)
  8469. (kwd-re
  8470. (cond ((null arg) org-not-done-regexp)
  8471. ((equal arg '(4))
  8472. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8473. (mapcar 'list org-todo-keywords-1))))
  8474. (concat "\\("
  8475. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8476. "\\)\\>")))
  8477. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8478. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8479. org-todo-keywords-1)))
  8480. (t (error "Invalid prefix argument: %s" arg)))))
  8481. (message "%d TODO entries found"
  8482. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8483. (defun org-deadline (&optional remove time)
  8484. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8485. With argument REMOVE, remove any deadline from the item.
  8486. When TIME is set, it should be an internal time specification, and the
  8487. scheduling will use the corresponding date."
  8488. (interactive "P")
  8489. (if remove
  8490. (progn
  8491. (org-remove-timestamp-with-keyword org-deadline-string)
  8492. (message "Item no longer has a deadline."))
  8493. (if (org-get-repeat)
  8494. (error "Cannot change deadline on task with repeater, please do that by hand")
  8495. (org-add-planning-info 'deadline time 'closed)
  8496. (message "Deadline on %s" org-last-inserted-timestamp))))
  8497. (defun org-schedule (&optional remove time)
  8498. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8499. With argument REMOVE, remove any scheduling date from the item.
  8500. When TIME is set, it should be an internal time specification, and the
  8501. scheduling will use the corresponding date."
  8502. (interactive "P")
  8503. (if remove
  8504. (progn
  8505. (org-remove-timestamp-with-keyword org-scheduled-string)
  8506. (message "Item is no longer scheduled."))
  8507. (if (org-get-repeat)
  8508. (error "Cannot reschedule task with repeater, please do that by hand")
  8509. (org-add-planning-info 'scheduled time 'closed)
  8510. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8511. (defun org-get-scheduled-time (pom &optional inherit)
  8512. "Get the scheduled time as a time tuple, of a format suitable
  8513. for calling org-schedule with, or if there is no scheduling,
  8514. returns nil."
  8515. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8516. (when time
  8517. (apply 'encode-time (org-parse-time-string time)))))
  8518. (defun org-get-deadline-time (pom &optional inherit)
  8519. "Get the deadine as a time tuple, of a format suitable for
  8520. calling org-deadlin with, or if there is no scheduling, returns
  8521. nil."
  8522. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8523. (when time
  8524. (apply 'encode-time (org-parse-time-string time)))))
  8525. (defun org-remove-timestamp-with-keyword (keyword)
  8526. "Remove all time stamps with KEYWORD in the current entry."
  8527. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8528. beg)
  8529. (save-excursion
  8530. (org-back-to-heading t)
  8531. (setq beg (point))
  8532. (org-end-of-subtree t t)
  8533. (while (re-search-backward re beg t)
  8534. (replace-match "")
  8535. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8536. (equal (char-before) ?\ ))
  8537. (backward-delete-char 1)
  8538. (if (string-match "^[ \t]*$" (buffer-substring
  8539. (point-at-bol) (point-at-eol)))
  8540. (delete-region (point-at-bol)
  8541. (min (point-max) (1+ (point-at-eol))))))))))
  8542. (defun org-add-planning-info (what &optional time &rest remove)
  8543. "Insert new timestamp with keyword in the line directly after the headline.
  8544. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8545. If non is given, the user is prompted for a date.
  8546. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8547. be removed."
  8548. (interactive)
  8549. (let (org-time-was-given org-end-time-was-given ts
  8550. end default-time default-input)
  8551. (catch 'exit
  8552. (when (and (not time) (memq what '(scheduled deadline)))
  8553. ;; Try to get a default date/time from existing timestamp
  8554. (save-excursion
  8555. (org-back-to-heading t)
  8556. (setq end (save-excursion (outline-next-heading) (point)))
  8557. (when (re-search-forward (if (eq what 'scheduled)
  8558. org-scheduled-time-regexp
  8559. org-deadline-time-regexp)
  8560. end t)
  8561. (setq ts (match-string 1)
  8562. default-time
  8563. (apply 'encode-time (org-parse-time-string ts))
  8564. default-input (and ts (org-get-compact-tod ts))))))
  8565. (when what
  8566. ;; If necessary, get the time from the user
  8567. (setq time (or time (org-read-date nil 'to-time nil nil
  8568. default-time default-input))))
  8569. (when (and org-insert-labeled-timestamps-at-point
  8570. (member what '(scheduled deadline)))
  8571. (insert
  8572. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8573. (org-insert-time-stamp time org-time-was-given
  8574. nil nil nil (list org-end-time-was-given))
  8575. (setq what nil))
  8576. (save-excursion
  8577. (save-restriction
  8578. (let (col list elt ts buffer-invisibility-spec)
  8579. (org-back-to-heading t)
  8580. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8581. (goto-char (match-end 1))
  8582. (setq col (current-column))
  8583. (goto-char (match-end 0))
  8584. (if (eobp) (insert "\n") (forward-char 1))
  8585. (when (and (not what)
  8586. (not (looking-at
  8587. (concat "[ \t]*"
  8588. org-keyword-time-not-clock-regexp))))
  8589. ;; Nothing to add, nothing to remove...... :-)
  8590. (throw 'exit nil))
  8591. (if (and (not (looking-at outline-regexp))
  8592. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8593. "[^\r\n]*"))
  8594. (not (equal (match-string 1) org-clock-string)))
  8595. (narrow-to-region (match-beginning 0) (match-end 0))
  8596. (insert-before-markers "\n")
  8597. (backward-char 1)
  8598. (narrow-to-region (point) (point))
  8599. (and org-adapt-indentation (org-indent-to-column col)))
  8600. ;; Check if we have to remove something.
  8601. (setq list (cons what remove))
  8602. (while list
  8603. (setq elt (pop list))
  8604. (goto-char (point-min))
  8605. (when (or (and (eq elt 'scheduled)
  8606. (re-search-forward org-scheduled-time-regexp nil t))
  8607. (and (eq elt 'deadline)
  8608. (re-search-forward org-deadline-time-regexp nil t))
  8609. (and (eq elt 'closed)
  8610. (re-search-forward org-closed-time-regexp nil t)))
  8611. (replace-match "")
  8612. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8613. (if (looking-at " +") (replace-match ""))))
  8614. (goto-char (point-max))
  8615. (when what
  8616. (insert
  8617. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8618. (cond ((eq what 'scheduled) org-scheduled-string)
  8619. ((eq what 'deadline) org-deadline-string)
  8620. ((eq what 'closed) org-closed-string))
  8621. " ")
  8622. (setq ts (org-insert-time-stamp
  8623. time
  8624. (or org-time-was-given
  8625. (and (eq what 'closed) org-log-done-with-time))
  8626. (eq what 'closed)
  8627. nil nil (list org-end-time-was-given)))
  8628. (end-of-line 1))
  8629. (goto-char (point-min))
  8630. (widen)
  8631. (if (and (looking-at "[ \t]+\n")
  8632. (equal (char-before) ?\n))
  8633. (delete-region (1- (point)) (point-at-eol)))
  8634. ts))))))
  8635. (defvar org-log-note-marker (make-marker))
  8636. (defvar org-log-note-purpose nil)
  8637. (defvar org-log-note-state nil)
  8638. (defvar org-log-note-previous-state nil)
  8639. (defvar org-log-note-how nil)
  8640. (defvar org-log-note-extra nil)
  8641. (defvar org-log-note-window-configuration nil)
  8642. (defvar org-log-note-return-to (make-marker))
  8643. (defvar org-log-post-message nil
  8644. "Message to be displayed after a log note has been stored.
  8645. The auto-repeater uses this.")
  8646. (defun org-add-note ()
  8647. "Add a note to the current entry.
  8648. This is done in the same way as adding a state change note."
  8649. (interactive)
  8650. (org-add-log-setup 'note nil nil 'findpos nil))
  8651. (defvar org-property-end-re)
  8652. (defun org-add-log-setup (&optional purpose state prev-state
  8653. findpos how &optional extra)
  8654. "Set up the post command hook to take a note.
  8655. If this is about to TODO state change, the new state is expected in STATE.
  8656. When FINDPOS is non-nil, find the correct position for the note in
  8657. the current entry. If not, assume that it can be inserted at point.
  8658. HOW is an indicator what kind of note should be created.
  8659. EXTRA is additional text that will be inserted into the notes buffer."
  8660. (let ((drawer (cond ((stringp org-log-into-drawer)
  8661. org-log-into-drawer)
  8662. (org-log-into-drawer "LOGBOOK")
  8663. (t nil))))
  8664. (save-restriction
  8665. (save-excursion
  8666. (when findpos
  8667. (org-back-to-heading t)
  8668. (narrow-to-region (point) (save-excursion
  8669. (outline-next-heading) (point)))
  8670. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8671. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8672. "[^\r\n]*\\)?"))
  8673. (goto-char (match-end 0))
  8674. (cond
  8675. (drawer
  8676. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8677. nil t)
  8678. (progn
  8679. (goto-char (match-end 0))
  8680. (or org-log-states-order-reversed
  8681. (and (re-search-forward org-property-end-re nil t)
  8682. (goto-char (1- (match-beginning 0))))))
  8683. (insert "\n:" drawer ":\n:END:")
  8684. (beginning-of-line 0)
  8685. (org-indent-line-function)
  8686. (beginning-of-line 2)
  8687. (org-indent-line-function)
  8688. (end-of-line 0)))
  8689. ((and org-log-state-notes-insert-after-drawers
  8690. (save-excursion
  8691. (forward-line) (looking-at org-drawer-regexp)))
  8692. (forward-line)
  8693. (while (looking-at org-drawer-regexp)
  8694. (goto-char (match-end 0))
  8695. (re-search-forward org-property-end-re (point-max) t)
  8696. (forward-line))
  8697. (forward-line -1)))
  8698. (unless org-log-states-order-reversed
  8699. (and (= (char-after) ?\n) (forward-char 1))
  8700. (org-skip-over-state-notes)
  8701. (skip-chars-backward " \t\n\r")))
  8702. (move-marker org-log-note-marker (point))
  8703. (setq org-log-note-purpose purpose
  8704. org-log-note-state state
  8705. org-log-note-previous-state prev-state
  8706. org-log-note-how how
  8707. org-log-note-extra extra)
  8708. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8709. (defun org-skip-over-state-notes ()
  8710. "Skip past the list of State notes in an entry."
  8711. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8712. (while (looking-at "[ \t]*- State")
  8713. (condition-case nil
  8714. (org-next-item)
  8715. (error (org-end-of-item)))))
  8716. (defun org-add-log-note (&optional purpose)
  8717. "Pop up a window for taking a note, and add this note later at point."
  8718. (remove-hook 'post-command-hook 'org-add-log-note)
  8719. (setq org-log-note-window-configuration (current-window-configuration))
  8720. (delete-other-windows)
  8721. (move-marker org-log-note-return-to (point))
  8722. (switch-to-buffer (marker-buffer org-log-note-marker))
  8723. (goto-char org-log-note-marker)
  8724. (org-switch-to-buffer-other-window "*Org Note*")
  8725. (erase-buffer)
  8726. (if (memq org-log-note-how '(time state))
  8727. (let (current-prefix-arg) (org-store-log-note))
  8728. (let ((org-inhibit-startup t)) (org-mode))
  8729. (insert (format "# Insert note for %s.
  8730. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8731. (cond
  8732. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8733. ((eq org-log-note-purpose 'done) "closed todo item")
  8734. ((eq org-log-note-purpose 'state)
  8735. (format "state change from \"%s\" to \"%s\""
  8736. (or org-log-note-previous-state "")
  8737. (or org-log-note-state "")))
  8738. ((eq org-log-note-purpose 'note)
  8739. "this entry")
  8740. (t (error "This should not happen")))))
  8741. (if org-log-note-extra (insert org-log-note-extra))
  8742. (org-set-local 'org-finish-function 'org-store-log-note)))
  8743. (defvar org-note-abort nil) ; dynamically scoped
  8744. (defun org-store-log-note ()
  8745. "Finish taking a log note, and insert it to where it belongs."
  8746. (let ((txt (buffer-string))
  8747. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8748. lines ind)
  8749. (kill-buffer (current-buffer))
  8750. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8751. (setq txt (replace-match "" t t txt)))
  8752. (if (string-match "\\s-+\\'" txt)
  8753. (setq txt (replace-match "" t t txt)))
  8754. (setq lines (org-split-string txt "\n"))
  8755. (when (and note (string-match "\\S-" note))
  8756. (setq note
  8757. (org-replace-escapes
  8758. note
  8759. (list (cons "%u" (user-login-name))
  8760. (cons "%U" user-full-name)
  8761. (cons "%t" (format-time-string
  8762. (org-time-stamp-format 'long 'inactive)
  8763. (current-time)))
  8764. (cons "%s" (if org-log-note-state
  8765. (concat "\"" org-log-note-state "\"")
  8766. ""))
  8767. (cons "%S" (if org-log-note-previous-state
  8768. (concat "\"" org-log-note-previous-state "\"")
  8769. "\"\"")))))
  8770. (if lines (setq note (concat note " \\\\")))
  8771. (push note lines))
  8772. (when (or current-prefix-arg org-note-abort)
  8773. (when org-log-into-drawer
  8774. (org-remove-empty-drawer-at
  8775. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  8776. org-log-note-marker))
  8777. (setq lines nil))
  8778. (when lines
  8779. (save-excursion
  8780. (set-buffer (marker-buffer org-log-note-marker))
  8781. (save-excursion
  8782. (goto-char org-log-note-marker)
  8783. (move-marker org-log-note-marker nil)
  8784. (end-of-line 1)
  8785. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8786. (insert "- " (pop lines))
  8787. (org-indent-line-function)
  8788. (beginning-of-line 1)
  8789. (looking-at "[ \t]*")
  8790. (setq ind (concat (match-string 0) " "))
  8791. (end-of-line 1)
  8792. (while lines (insert "\n" ind (pop lines)))
  8793. (message "Note stored")
  8794. (org-back-to-heading t)
  8795. (org-cycle-hide-drawers 'children)))))
  8796. (set-window-configuration org-log-note-window-configuration)
  8797. (with-current-buffer (marker-buffer org-log-note-return-to)
  8798. (goto-char org-log-note-return-to))
  8799. (move-marker org-log-note-return-to nil)
  8800. (and org-log-post-message (message "%s" org-log-post-message)))
  8801. (defun org-remove-empty-drawer-at (drawer pos)
  8802. "Remove an emptyr DARWER drawer at position POS.
  8803. POS may also be a marker."
  8804. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  8805. (save-excursion
  8806. (save-restriction
  8807. (widen)
  8808. (goto-char pos)
  8809. (if (org-in-regexp
  8810. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  8811. (replace-match ""))))))
  8812. (defun org-sparse-tree (&optional arg)
  8813. "Create a sparse tree, prompt for the details.
  8814. This command can create sparse trees. You first need to select the type
  8815. of match used to create the tree:
  8816. t Show entries with a specific TODO keyword.
  8817. m Show entries selected by a tags/property match.
  8818. p Enter a property name and its value (both with completion on existing
  8819. names/values) and show entries with that property.
  8820. r Show entries matching a regular expression.
  8821. d Show deadlines due within `org-deadline-warning-days'.
  8822. b Show deadlines and scheduled items before a date.
  8823. a Show deadlines and scheduled items after a date."
  8824. (interactive "P")
  8825. (let (ans kwd value)
  8826. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  8827. (setq ans (read-char-exclusive))
  8828. (cond
  8829. ((equal ans ?d)
  8830. (call-interactively 'org-check-deadlines))
  8831. ((equal ans ?b)
  8832. (call-interactively 'org-check-before-date))
  8833. ((equal ans ?a)
  8834. (call-interactively 'org-check-after-date))
  8835. ((equal ans ?t)
  8836. (org-show-todo-tree '(4)))
  8837. ((member ans '(?T ?m))
  8838. (call-interactively 'org-match-sparse-tree))
  8839. ((member ans '(?p ?P))
  8840. (setq kwd (org-ido-completing-read "Property: "
  8841. (mapcar 'list (org-buffer-property-keys))))
  8842. (setq value (org-ido-completing-read "Value: "
  8843. (mapcar 'list (org-property-values kwd))))
  8844. (unless (string-match "\\`{.*}\\'" value)
  8845. (setq value (concat "\"" value "\"")))
  8846. (org-match-sparse-tree arg (concat kwd "=" value)))
  8847. ((member ans '(?r ?R ?/))
  8848. (call-interactively 'org-occur))
  8849. (t (error "No such sparse tree command \"%c\"" ans)))))
  8850. (defvar org-occur-highlights nil
  8851. "List of overlays used for occur matches.")
  8852. (make-variable-buffer-local 'org-occur-highlights)
  8853. (defvar org-occur-parameters nil
  8854. "Parameters of the active org-occur calls.
  8855. This is a list, each call to org-occur pushes as cons cell,
  8856. containing the regular expression and the callback, onto the list.
  8857. The list can contain several entries if `org-occur' has been called
  8858. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8859. will only contain one set of parameters. When the highlights are
  8860. removed (for example with `C-c C-c', or with the next edit (depending
  8861. on `org-remove-highlights-with-change'), this variable is emptied
  8862. as well.")
  8863. (make-variable-buffer-local 'org-occur-parameters)
  8864. (defun org-occur (regexp &optional keep-previous callback)
  8865. "Make a compact tree which shows all matches of REGEXP.
  8866. The tree will show the lines where the regexp matches, and all higher
  8867. headlines above the match. It will also show the heading after the match,
  8868. to make sure editing the matching entry is easy.
  8869. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8870. call to `org-occur' will be kept, to allow stacking of calls to this
  8871. command.
  8872. If CALLBACK is non-nil, it is a function which is called to confirm
  8873. that the match should indeed be shown."
  8874. (interactive "sRegexp: \nP")
  8875. (unless keep-previous
  8876. (org-remove-occur-highlights nil nil t))
  8877. (push (cons regexp callback) org-occur-parameters)
  8878. (let ((cnt 0))
  8879. (save-excursion
  8880. (goto-char (point-min))
  8881. (if (or (not keep-previous) ; do not want to keep
  8882. (not org-occur-highlights)) ; no previous matches
  8883. ;; hide everything
  8884. (org-overview))
  8885. (while (re-search-forward regexp nil t)
  8886. (when (or (not callback)
  8887. (save-match-data (funcall callback)))
  8888. (setq cnt (1+ cnt))
  8889. (when org-highlight-sparse-tree-matches
  8890. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8891. (org-show-context 'occur-tree))))
  8892. (when org-remove-highlights-with-change
  8893. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8894. nil 'local))
  8895. (unless org-sparse-tree-open-archived-trees
  8896. (org-hide-archived-subtrees (point-min) (point-max)))
  8897. (run-hooks 'org-occur-hook)
  8898. (if (interactive-p)
  8899. (message "%d match(es) for regexp %s" cnt regexp))
  8900. cnt))
  8901. (defun org-show-context (&optional key)
  8902. "Make sure point and context and visible.
  8903. How much context is shown depends upon the variables
  8904. `org-show-hierarchy-above', `org-show-following-heading'. and
  8905. `org-show-siblings'."
  8906. (let ((heading-p (org-on-heading-p t))
  8907. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8908. (following-p (org-get-alist-option org-show-following-heading key))
  8909. (entry-p (org-get-alist-option org-show-entry-below key))
  8910. (siblings-p (org-get-alist-option org-show-siblings key)))
  8911. (catch 'exit
  8912. ;; Show heading or entry text
  8913. (if (and heading-p (not entry-p))
  8914. (org-flag-heading nil) ; only show the heading
  8915. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8916. (org-show-hidden-entry))) ; show entire entry
  8917. (when following-p
  8918. ;; Show next sibling, or heading below text
  8919. (save-excursion
  8920. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8921. (org-flag-heading nil))))
  8922. (when siblings-p (org-show-siblings))
  8923. (when hierarchy-p
  8924. ;; show all higher headings, possibly with siblings
  8925. (save-excursion
  8926. (while (and (condition-case nil
  8927. (progn (org-up-heading-all 1) t)
  8928. (error nil))
  8929. (not (bobp)))
  8930. (org-flag-heading nil)
  8931. (when siblings-p (org-show-siblings))))))))
  8932. (defun org-reveal (&optional siblings)
  8933. "Show current entry, hierarchy above it, and the following headline.
  8934. This can be used to show a consistent set of context around locations
  8935. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8936. not t for the search context.
  8937. With optional argument SIBLINGS, on each level of the hierarchy all
  8938. siblings are shown. This repairs the tree structure to what it would
  8939. look like when opened with hierarchical calls to `org-cycle'."
  8940. (interactive "P")
  8941. (let ((org-show-hierarchy-above t)
  8942. (org-show-following-heading t)
  8943. (org-show-siblings (if siblings t org-show-siblings)))
  8944. (org-show-context nil)))
  8945. (defun org-highlight-new-match (beg end)
  8946. "Highlight from BEG to END and mark the highlight is an occur headline."
  8947. (let ((ov (org-make-overlay beg end)))
  8948. (org-overlay-put ov 'face 'secondary-selection)
  8949. (push ov org-occur-highlights)))
  8950. (defun org-remove-occur-highlights (&optional beg end noremove)
  8951. "Remove the occur highlights from the buffer.
  8952. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8953. from the `before-change-functions' in the current buffer."
  8954. (interactive)
  8955. (unless org-inhibit-highlight-removal
  8956. (mapc 'org-delete-overlay org-occur-highlights)
  8957. (setq org-occur-highlights nil)
  8958. (setq org-occur-parameters nil)
  8959. (unless noremove
  8960. (remove-hook 'before-change-functions
  8961. 'org-remove-occur-highlights 'local))))
  8962. ;;;; Priorities
  8963. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8964. "Regular expression matching the priority indicator.")
  8965. (defvar org-remove-priority-next-time nil)
  8966. (defun org-priority-up ()
  8967. "Increase the priority of the current item."
  8968. (interactive)
  8969. (org-priority 'up))
  8970. (defun org-priority-down ()
  8971. "Decrease the priority of the current item."
  8972. (interactive)
  8973. (org-priority 'down))
  8974. (defun org-priority (&optional action)
  8975. "Change the priority of an item by ARG.
  8976. ACTION can be `set', `up', `down', or a character."
  8977. (interactive)
  8978. (setq action (or action 'set))
  8979. (let (current new news have remove)
  8980. (save-excursion
  8981. (org-back-to-heading t)
  8982. (if (looking-at org-priority-regexp)
  8983. (setq current (string-to-char (match-string 2))
  8984. have t)
  8985. (setq current org-default-priority))
  8986. (cond
  8987. ((or (eq action 'set)
  8988. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8989. (if (not (eq action 'set))
  8990. (setq new action)
  8991. (message "Priority %c-%c, SPC to remove: "
  8992. org-highest-priority org-lowest-priority)
  8993. (setq new (read-char-exclusive)))
  8994. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8995. (= (upcase org-lowest-priority) org-lowest-priority))
  8996. (setq new (upcase new)))
  8997. (cond ((equal new ?\ ) (setq remove t))
  8998. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8999. (error "Priority must be between `%c' and `%c'"
  9000. org-highest-priority org-lowest-priority))))
  9001. ((eq action 'up)
  9002. (if (and (not have) (eq last-command this-command))
  9003. (setq new org-lowest-priority)
  9004. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9005. org-default-priority (1- current)))))
  9006. ((eq action 'down)
  9007. (if (and (not have) (eq last-command this-command))
  9008. (setq new org-highest-priority)
  9009. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9010. org-default-priority (1+ current)))))
  9011. (t (error "Invalid action")))
  9012. (if (or (< (upcase new) org-highest-priority)
  9013. (> (upcase new) org-lowest-priority))
  9014. (setq remove t))
  9015. (setq news (format "%c" new))
  9016. (if have
  9017. (if remove
  9018. (replace-match "" t t nil 1)
  9019. (replace-match news t t nil 2))
  9020. (if remove
  9021. (error "No priority cookie found in line")
  9022. (looking-at org-todo-line-regexp)
  9023. (if (match-end 2)
  9024. (progn
  9025. (goto-char (match-end 2))
  9026. (insert " [#" news "]"))
  9027. (goto-char (match-beginning 3))
  9028. (insert "[#" news "] ")))))
  9029. (org-preserve-lc (org-set-tags nil 'align))
  9030. (if remove
  9031. (message "Priority removed")
  9032. (message "Priority of current item set to %s" news))))
  9033. (defun org-get-priority (s)
  9034. "Find priority cookie and return priority."
  9035. (save-match-data
  9036. (if (not (string-match org-priority-regexp s))
  9037. (* 1000 (- org-lowest-priority org-default-priority))
  9038. (* 1000 (- org-lowest-priority
  9039. (string-to-char (match-string 2 s)))))))
  9040. ;;;; Tags
  9041. (defvar org-agenda-archives-mode)
  9042. (defvar org-scanner-tags nil
  9043. "The current tag list while the tags scanner is running.")
  9044. (defvar org-trust-scanner-tags nil
  9045. "Should `org-get-tags-at' use the tags fro the scanner.
  9046. This is for internal dynamical scoping only.
  9047. When this is non-nil, the function `org-get-tags-at' will return the value
  9048. of `org-scanner-tags' instead of building the list by itself. This
  9049. can lead to large speed-ups when the tags scanner is used in a file with
  9050. many entries, and when the list of tags is retrieved, for example to
  9051. obtain a list of properties. Building the tags list for each entry in such
  9052. a file becomes an N^2 operation - but with this variable set, it scales
  9053. as N.")
  9054. (defun org-scan-tags (action matcher &optional todo-only)
  9055. "Scan headline tags with inheritance and produce output ACTION.
  9056. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9057. or `agenda' to produce an entry list for an agenda view. It can also be
  9058. a Lisp form or a function that should be called at each matched headline, in
  9059. this case the return value is a list of all return values from these calls.
  9060. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9061. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9062. only lines with a TODO keyword are included in the output."
  9063. (require 'org-agenda)
  9064. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  9065. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9066. (org-re
  9067. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9068. (props (list 'face 'default
  9069. 'done-face 'org-agenda-done
  9070. 'undone-face 'default
  9071. 'mouse-face 'highlight
  9072. 'org-not-done-regexp org-not-done-regexp
  9073. 'org-todo-regexp org-todo-regexp
  9074. 'keymap org-agenda-keymap
  9075. 'help-echo
  9076. (format "mouse-2 or RET jump to org file %s"
  9077. (abbreviate-file-name
  9078. (or (buffer-file-name (buffer-base-buffer))
  9079. (buffer-name (buffer-base-buffer)))))))
  9080. (case-fold-search nil)
  9081. lspos tags tags-list
  9082. (tags-alist (list (cons 0 org-file-tags)))
  9083. (llast 0) rtn rtn1 level category i txt
  9084. todo marker entry priority)
  9085. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9086. (setq action (list 'lambda nil action)))
  9087. (save-excursion
  9088. (goto-char (point-min))
  9089. (when (eq action 'sparse-tree)
  9090. (org-overview)
  9091. (org-remove-occur-highlights))
  9092. (while (re-search-forward re nil t)
  9093. (catch :skip
  9094. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9095. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9096. (goto-char (setq lspos (1+ (match-beginning 0))))
  9097. (setq level (org-reduced-level (funcall outline-level))
  9098. category (org-get-category))
  9099. (setq i llast llast level)
  9100. ;; remove tag lists from same and sublevels
  9101. (while (>= i level)
  9102. (when (setq entry (assoc i tags-alist))
  9103. (setq tags-alist (delete entry tags-alist)))
  9104. (setq i (1- i)))
  9105. ;; add the next tags
  9106. (when tags
  9107. (setq tags (org-split-string tags ":")
  9108. tags-alist
  9109. (cons (cons level tags) tags-alist)))
  9110. ;; compile tags for current headline
  9111. (setq tags-list
  9112. (if org-use-tag-inheritance
  9113. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9114. tags)
  9115. org-scanner-tags tags-list)
  9116. (when org-use-tag-inheritance
  9117. (setcdr (car tags-alist)
  9118. (mapcar (lambda (x)
  9119. (setq x (copy-sequence x))
  9120. (org-add-prop-inherited x))
  9121. (cdar tags-alist))))
  9122. (when (and tags org-use-tag-inheritance
  9123. (or (not (eq t org-use-tag-inheritance))
  9124. org-tags-exclude-from-inheritance))
  9125. ;; selective inheritance, remove uninherited ones
  9126. (setcdr (car tags-alist)
  9127. (org-remove-uniherited-tags (cdar tags-alist))))
  9128. (when (and (or (not todo-only)
  9129. (and (member todo org-not-done-keywords)
  9130. (or (not org-agenda-tags-todo-honor-ignore-options)
  9131. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9132. (let ((case-fold-search t)) (eval matcher))
  9133. (or
  9134. (not (member org-archive-tag tags-list))
  9135. ;; we have an archive tag, should we use this anyway?
  9136. (or (not org-agenda-skip-archived-trees)
  9137. (and (eq action 'agenda) org-agenda-archives-mode))))
  9138. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9139. ;; select this headline
  9140. (cond
  9141. ((eq action 'sparse-tree)
  9142. (and org-highlight-sparse-tree-matches
  9143. (org-get-heading) (match-end 0)
  9144. (org-highlight-new-match
  9145. (match-beginning 0) (match-beginning 1)))
  9146. (org-show-context 'tags-tree))
  9147. ((eq action 'agenda)
  9148. (setq txt (org-format-agenda-item
  9149. ""
  9150. (concat
  9151. (if (eq org-tags-match-list-sublevels 'indented)
  9152. (make-string (1- level) ?.) "")
  9153. (org-get-heading))
  9154. category
  9155. tags-list
  9156. )
  9157. priority (org-get-priority txt))
  9158. (goto-char lspos)
  9159. (setq marker (org-agenda-new-marker))
  9160. (org-add-props txt props
  9161. 'org-marker marker 'org-hd-marker marker 'org-category category
  9162. 'todo-state todo
  9163. 'priority priority 'type "tagsmatch")
  9164. (push txt rtn))
  9165. ((functionp action)
  9166. (save-excursion
  9167. (setq rtn1 (funcall action))
  9168. (push rtn1 rtn))
  9169. (goto-char (point-at-eol)))
  9170. (t (error "Invalid action")))
  9171. ;; if we are to skip sublevels, jump to end of subtree
  9172. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  9173. (when (and (eq action 'sparse-tree)
  9174. (not org-sparse-tree-open-archived-trees))
  9175. (org-hide-archived-subtrees (point-min) (point-max)))
  9176. (nreverse rtn)))
  9177. (defun org-remove-uniherited-tags (tags)
  9178. "Remove all tags that are not inherited from the list TAGS."
  9179. (cond
  9180. ((eq org-use-tag-inheritance t)
  9181. (if org-tags-exclude-from-inheritance
  9182. (org-delete-all org-tags-exclude-from-inheritance tags)
  9183. tags))
  9184. ((not org-use-tag-inheritance) nil)
  9185. ((stringp org-use-tag-inheritance)
  9186. (delq nil (mapcar
  9187. (lambda (x)
  9188. (if (and (string-match org-use-tag-inheritance x)
  9189. (not (member x org-tags-exclude-from-inheritance)))
  9190. x nil))
  9191. tags)))
  9192. ((listp org-use-tag-inheritance)
  9193. (delq nil (mapcar
  9194. (lambda (x)
  9195. (if (member x org-use-tag-inheritance) x nil))
  9196. tags)))))
  9197. (defvar todo-only) ;; dynamically scoped
  9198. (defun org-match-sparse-tree (&optional todo-only match)
  9199. "Create a sparse tree according to tags string MATCH.
  9200. MATCH can contain positive and negative selection of tags, like
  9201. \"+WORK+URGENT-WITHBOSS\".
  9202. If optional argument TODO-ONLY is non-nil, only select lines that are
  9203. also TODO lines."
  9204. (interactive "P")
  9205. (org-prepare-agenda-buffers (list (current-buffer)))
  9206. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9207. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9208. (defvar org-cached-props nil)
  9209. (defun org-cached-entry-get (pom property)
  9210. (if (or (eq t org-use-property-inheritance)
  9211. (and (stringp org-use-property-inheritance)
  9212. (string-match org-use-property-inheritance property))
  9213. (and (listp org-use-property-inheritance)
  9214. (member property org-use-property-inheritance)))
  9215. ;; Caching is not possible, check it directly
  9216. (org-entry-get pom property 'inherit)
  9217. ;; Get all properties, so that we can do complicated checks easily
  9218. (cdr (assoc property (or org-cached-props
  9219. (setq org-cached-props
  9220. (org-entry-properties pom)))))))
  9221. (defun org-global-tags-completion-table (&optional files)
  9222. "Return the list of all tags in all agenda buffer/files."
  9223. (save-excursion
  9224. (org-uniquify
  9225. (delq nil
  9226. (apply 'append
  9227. (mapcar
  9228. (lambda (file)
  9229. (set-buffer (find-file-noselect file))
  9230. (append (org-get-buffer-tags)
  9231. (mapcar (lambda (x) (if (stringp (car-safe x))
  9232. (list (car-safe x)) nil))
  9233. org-tag-alist)))
  9234. (if (and files (car files))
  9235. files
  9236. (org-agenda-files))))))))
  9237. (defun org-make-tags-matcher (match)
  9238. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9239. ;; todo-only is scoped dynamically into this function, and the function
  9240. ;; may change it if the matcher asks for it.
  9241. (unless match
  9242. ;; Get a new match request, with completion
  9243. (let ((org-last-tags-completion-table
  9244. (org-global-tags-completion-table)))
  9245. (setq match (org-completing-read-no-ido
  9246. "Match: " 'org-tags-completion-function nil nil nil
  9247. 'org-tags-history))))
  9248. ;; Parse the string and create a lisp form
  9249. (let ((match0 match)
  9250. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9251. minus tag mm
  9252. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9253. orterms term orlist re-p str-p level-p level-op time-p
  9254. prop-p pn pv po cat-p gv rest)
  9255. (if (string-match "/+" match)
  9256. ;; match contains also a todo-matching request
  9257. (progn
  9258. (setq tagsmatch (substring match 0 (match-beginning 0))
  9259. todomatch (substring match (match-end 0)))
  9260. (if (string-match "^!" todomatch)
  9261. (setq todo-only t todomatch (substring todomatch 1)))
  9262. (if (string-match "^\\s-*$" todomatch)
  9263. (setq todomatch nil)))
  9264. ;; only matching tags
  9265. (setq tagsmatch match todomatch nil))
  9266. ;; Make the tags matcher
  9267. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9268. (setq tagsmatcher t)
  9269. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9270. (while (setq term (pop orterms))
  9271. (while (and (equal (substring term -1) "\\") orterms)
  9272. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9273. (while (string-match re term)
  9274. (setq rest (substring term (match-end 0))
  9275. minus (and (match-end 1)
  9276. (equal (match-string 1 term) "-"))
  9277. tag (match-string 2 term)
  9278. re-p (equal (string-to-char tag) ?{)
  9279. level-p (match-end 4)
  9280. prop-p (match-end 5)
  9281. mm (cond
  9282. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9283. (level-p
  9284. (setq level-op (org-op-to-function (match-string 3 term)))
  9285. `(,level-op level ,(string-to-number
  9286. (match-string 4 term))))
  9287. (prop-p
  9288. (setq pn (match-string 5 term)
  9289. po (match-string 6 term)
  9290. pv (match-string 7 term)
  9291. cat-p (equal pn "CATEGORY")
  9292. re-p (equal (string-to-char pv) ?{)
  9293. str-p (equal (string-to-char pv) ?\")
  9294. time-p (save-match-data
  9295. (string-match "^\"[[<].*[]>]\"$" pv))
  9296. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9297. (if time-p (setq pv (org-matcher-time pv)))
  9298. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9299. (cond
  9300. ((equal pn "CATEGORY")
  9301. (setq gv '(get-text-property (point) 'org-category)))
  9302. ((equal pn "TODO")
  9303. (setq gv 'todo))
  9304. (t
  9305. (setq gv `(org-cached-entry-get nil ,pn))))
  9306. (if re-p
  9307. (if (eq po 'org<>)
  9308. `(not (string-match ,pv (or ,gv "")))
  9309. `(string-match ,pv (or ,gv "")))
  9310. (if str-p
  9311. `(,po (or ,gv "") ,pv)
  9312. `(,po (string-to-number (or ,gv ""))
  9313. ,(string-to-number pv) ))))
  9314. (t `(member ,tag tags-list)))
  9315. mm (if minus (list 'not mm) mm)
  9316. term rest)
  9317. (push mm tagsmatcher))
  9318. (push (if (> (length tagsmatcher) 1)
  9319. (cons 'and tagsmatcher)
  9320. (car tagsmatcher))
  9321. orlist)
  9322. (setq tagsmatcher nil))
  9323. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9324. (setq tagsmatcher
  9325. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9326. ;; Make the todo matcher
  9327. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9328. (setq todomatcher t)
  9329. (setq orterms (org-split-string todomatch "|") orlist nil)
  9330. (while (setq term (pop orterms))
  9331. (while (string-match re term)
  9332. (setq minus (and (match-end 1)
  9333. (equal (match-string 1 term) "-"))
  9334. kwd (match-string 2 term)
  9335. re-p (equal (string-to-char kwd) ?{)
  9336. term (substring term (match-end 0))
  9337. mm (if re-p
  9338. `(string-match ,(substring kwd 1 -1) todo)
  9339. (list 'equal 'todo kwd))
  9340. mm (if minus (list 'not mm) mm))
  9341. (push mm todomatcher))
  9342. (push (if (> (length todomatcher) 1)
  9343. (cons 'and todomatcher)
  9344. (car todomatcher))
  9345. orlist)
  9346. (setq todomatcher nil))
  9347. (setq todomatcher (if (> (length orlist) 1)
  9348. (cons 'or orlist) (car orlist))))
  9349. ;; Return the string and lisp forms of the matcher
  9350. (setq matcher (if todomatcher
  9351. (list 'and tagsmatcher todomatcher)
  9352. tagsmatcher))
  9353. (cons match0 matcher)))
  9354. (defun org-op-to-function (op &optional stringp)
  9355. "Turn an operator into the appropriate function."
  9356. (setq op
  9357. (cond
  9358. ((equal op "<" ) '(< string< org-time<))
  9359. ((equal op ">" ) '(> org-string> org-time>))
  9360. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9361. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9362. ((member op '("=" "==")) '(= string= org-time=))
  9363. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9364. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9365. (defun org<> (a b) (not (= a b)))
  9366. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9367. (defun org-string>= (a b) (not (string< a b)))
  9368. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9369. (defun org-string<> (a b) (not (string= a b)))
  9370. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9371. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9372. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9373. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9374. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9375. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9376. (defun org-2ft (s)
  9377. "Convert S to a floating point time.
  9378. If S is already a number, just return it. If it is a string, parse
  9379. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9380. (cond
  9381. ((numberp s) s)
  9382. ((stringp s)
  9383. (condition-case nil
  9384. (float-time (apply 'encode-time (org-parse-time-string s)))
  9385. (error 0.)))
  9386. (t 0.)))
  9387. (defun org-time-today ()
  9388. "Time in seconds today at 0:00.
  9389. Returns the float number of seconds since the beginning of the
  9390. epoch to the beginning of today (00:00)."
  9391. (float-time (apply 'encode-time
  9392. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9393. (defun org-matcher-time (s)
  9394. "Interpret a time comparison value."
  9395. (save-match-data
  9396. (cond
  9397. ((string= s "<now>") (float-time))
  9398. ((string= s "<today>") (org-time-today))
  9399. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9400. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9401. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9402. (+ (org-time-today)
  9403. (* (string-to-number (match-string 1 s))
  9404. (cdr (assoc (match-string 2 s)
  9405. '(("d" . 86400.0) ("w" . 604800.0)
  9406. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9407. (t (org-2ft s)))))
  9408. (defun org-match-any-p (re list)
  9409. "Does re match any element of list?"
  9410. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9411. (delq nil list))
  9412. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9413. (defvar org-tags-overlay (org-make-overlay 1 1))
  9414. (org-detach-overlay org-tags-overlay)
  9415. (defun org-get-local-tags-at (&optional pos)
  9416. "Get a list of tags defined in the current headline."
  9417. (org-get-tags-at pos 'local))
  9418. (defun org-get-local-tags ()
  9419. "Get a list of tags defined in the current headline."
  9420. (org-get-tags-at nil 'local))
  9421. (defun org-get-tags-at (&optional pos local)
  9422. "Get a list of all headline tags applicable at POS.
  9423. POS defaults to point. If tags are inherited, the list contains
  9424. the targets in the same sequence as the headlines appear, i.e.
  9425. the tags of the current headline come last.
  9426. When LOCAL is non-nil, only return tags from the current headline,
  9427. ignore inherited ones."
  9428. (interactive)
  9429. (if (and org-trust-scanner-tags
  9430. (or (not pos) (equal pos (point)))
  9431. (not local))
  9432. org-scanner-tags
  9433. (let (tags ltags lastpos parent)
  9434. (save-excursion
  9435. (save-restriction
  9436. (widen)
  9437. (goto-char (or pos (point)))
  9438. (save-match-data
  9439. (catch 'done
  9440. (condition-case nil
  9441. (progn
  9442. (org-back-to-heading t)
  9443. (while (not (equal lastpos (point)))
  9444. (setq lastpos (point))
  9445. (when (looking-at
  9446. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9447. (setq ltags (org-split-string
  9448. (org-match-string-no-properties 1) ":"))
  9449. (when parent
  9450. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9451. (setq tags (append
  9452. (if parent
  9453. (org-remove-uniherited-tags ltags)
  9454. ltags)
  9455. tags)))
  9456. (or org-use-tag-inheritance (throw 'done t))
  9457. (if local (throw 'done t))
  9458. (org-up-heading-all 1)
  9459. (setq parent t)))
  9460. (error nil)))))
  9461. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  9462. (defun org-add-prop-inherited (s)
  9463. (add-text-properties 0 (length s) '(inherited t) s)
  9464. s)
  9465. (defun org-toggle-tag (tag &optional onoff)
  9466. "Toggle the tag TAG for the current line.
  9467. If ONOFF is `on' or `off', don't toggle but set to this state."
  9468. (let (res current)
  9469. (save-excursion
  9470. (org-back-to-heading t)
  9471. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9472. (point-at-eol) t)
  9473. (progn
  9474. (setq current (match-string 1))
  9475. (replace-match ""))
  9476. (setq current ""))
  9477. (setq current (nreverse (org-split-string current ":")))
  9478. (cond
  9479. ((eq onoff 'on)
  9480. (setq res t)
  9481. (or (member tag current) (push tag current)))
  9482. ((eq onoff 'off)
  9483. (or (not (member tag current)) (setq current (delete tag current))))
  9484. (t (if (member tag current)
  9485. (setq current (delete tag current))
  9486. (setq res t)
  9487. (push tag current))))
  9488. (end-of-line 1)
  9489. (if current
  9490. (progn
  9491. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9492. (org-set-tags nil t))
  9493. (delete-horizontal-space))
  9494. (run-hooks 'org-after-tags-change-hook))
  9495. res))
  9496. (defun org-align-tags-here (to-col)
  9497. ;; Assumes that this is a headline
  9498. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9499. (beginning-of-line 1)
  9500. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9501. (< pos (match-beginning 2)))
  9502. (progn
  9503. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9504. (goto-char (match-beginning 1))
  9505. (insert " ")
  9506. (delete-region (point) (1+ (match-beginning 2)))
  9507. (setq ncol (max (1+ (current-column))
  9508. (1+ col)
  9509. (if (> to-col 0)
  9510. to-col
  9511. (- (abs to-col) tags-l))))
  9512. (setq p (point))
  9513. (insert (make-string (- ncol (current-column)) ?\ ))
  9514. (setq ncol (current-column))
  9515. (when indent-tabs-mode (tabify p (point-at-eol)))
  9516. (org-move-to-column (min ncol col) t))
  9517. (goto-char pos))))
  9518. (defun org-set-tags-command (&optional arg just-align)
  9519. "Call the set-tags command for the current entry."
  9520. (interactive "P")
  9521. (if (org-on-heading-p)
  9522. (org-set-tags arg just-align)
  9523. (save-excursion
  9524. (org-back-to-heading t)
  9525. (org-set-tags arg just-align))))
  9526. (defun org-set-tags (&optional arg just-align)
  9527. "Set the tags for the current headline.
  9528. With prefix ARG, realign all tags in headings in the current buffer."
  9529. (interactive "P")
  9530. (let* ((re (concat "^" outline-regexp))
  9531. (current (org-get-tags-string))
  9532. (col (current-column))
  9533. (org-setting-tags t)
  9534. table current-tags inherited-tags ; computed below when needed
  9535. tags p0 c0 c1 rpl)
  9536. (if arg
  9537. (save-excursion
  9538. (goto-char (point-min))
  9539. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9540. (while (re-search-forward re nil t)
  9541. (org-set-tags nil t)
  9542. (end-of-line 1)))
  9543. (message "All tags realigned to column %d" org-tags-column))
  9544. (if just-align
  9545. (setq tags current)
  9546. ;; Get a new set of tags from the user
  9547. (save-excursion
  9548. (setq table (append org-tag-persistent-alist
  9549. (or org-tag-alist (org-get-buffer-tags)))
  9550. org-last-tags-completion-table table
  9551. current-tags (org-split-string current ":")
  9552. inherited-tags (nreverse
  9553. (nthcdr (length current-tags)
  9554. (nreverse (org-get-tags-at))))
  9555. tags
  9556. (if (or (eq t org-use-fast-tag-selection)
  9557. (and org-use-fast-tag-selection
  9558. (delq nil (mapcar 'cdr table))))
  9559. (org-fast-tag-selection
  9560. current-tags inherited-tags table
  9561. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9562. (let ((org-add-colon-after-tag-completion t))
  9563. (org-trim
  9564. (org-without-partial-completion
  9565. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9566. nil nil current 'org-tags-history)))))))
  9567. (while (string-match "[-+&]+" tags)
  9568. ;; No boolean logic, just a list
  9569. (setq tags (replace-match ":" t t tags))))
  9570. (if (string-match "\\`[\t ]*\\'" tags)
  9571. (setq tags "")
  9572. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9573. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9574. ;; Insert new tags at the correct column
  9575. (beginning-of-line 1)
  9576. (cond
  9577. ((and (equal current "") (equal tags "")))
  9578. ((re-search-forward
  9579. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9580. (point-at-eol) t)
  9581. (if (equal tags "")
  9582. (setq rpl "")
  9583. (goto-char (match-beginning 0))
  9584. (setq c0 (current-column) p0 (point)
  9585. c1 (max (1+ c0) (if (> org-tags-column 0)
  9586. org-tags-column
  9587. (- (- org-tags-column) (length tags))))
  9588. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9589. (replace-match rpl t t)
  9590. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9591. tags)
  9592. (t (error "Tags alignment failed")))
  9593. (org-move-to-column col)
  9594. (unless just-align
  9595. (run-hooks 'org-after-tags-change-hook)))))
  9596. (defun org-change-tag-in-region (beg end tag off)
  9597. "Add or remove TAG for each entry in the region.
  9598. This works in the agenda, and also in an org-mode buffer."
  9599. (interactive
  9600. (list (region-beginning) (region-end)
  9601. (let ((org-last-tags-completion-table
  9602. (if (org-mode-p)
  9603. (org-get-buffer-tags)
  9604. (org-global-tags-completion-table))))
  9605. (org-ido-completing-read
  9606. "Tag: " 'org-tags-completion-function nil nil nil
  9607. 'org-tags-history))
  9608. (progn
  9609. (message "[s]et or [r]emove? ")
  9610. (equal (read-char-exclusive) ?r))))
  9611. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9612. (let ((agendap (equal major-mode 'org-agenda-mode))
  9613. l1 l2 m buf pos newhead (cnt 0))
  9614. (goto-char end)
  9615. (setq l2 (1- (org-current-line)))
  9616. (goto-char beg)
  9617. (setq l1 (org-current-line))
  9618. (loop for l from l1 to l2 do
  9619. (goto-line l)
  9620. (setq m (get-text-property (point) 'org-hd-marker))
  9621. (when (or (and (org-mode-p) (org-on-heading-p))
  9622. (and agendap m))
  9623. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9624. pos (if agendap m (point)))
  9625. (with-current-buffer buf
  9626. (save-excursion
  9627. (save-restriction
  9628. (goto-char pos)
  9629. (setq cnt (1+ cnt))
  9630. (org-toggle-tag tag (if off 'off 'on))
  9631. (setq newhead (org-get-heading)))))
  9632. (and agendap (org-agenda-change-all-lines newhead m))))
  9633. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9634. (defun org-tags-completion-function (string predicate &optional flag)
  9635. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9636. (confirm (lambda (x) (stringp (car x)))))
  9637. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9638. (setq s1 (match-string 1 string)
  9639. s2 (match-string 2 string))
  9640. (setq s1 "" s2 string))
  9641. (cond
  9642. ((eq flag nil)
  9643. ;; try completion
  9644. (setq rtn (try-completion s2 ctable confirm))
  9645. (if (stringp rtn)
  9646. (setq rtn
  9647. (concat s1 s2 (substring rtn (length s2))
  9648. (if (and org-add-colon-after-tag-completion
  9649. (assoc rtn ctable))
  9650. ":" ""))))
  9651. rtn)
  9652. ((eq flag t)
  9653. ;; all-completions
  9654. (all-completions s2 ctable confirm)
  9655. )
  9656. ((eq flag 'lambda)
  9657. ;; exact match?
  9658. (assoc s2 ctable)))
  9659. ))
  9660. (defun org-fast-tag-insert (kwd tags face &optional end)
  9661. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9662. (insert (format "%-12s" (concat kwd ":"))
  9663. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9664. (or end "")))
  9665. (defun org-fast-tag-show-exit (flag)
  9666. (save-excursion
  9667. (goto-line 3)
  9668. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9669. (replace-match ""))
  9670. (when flag
  9671. (end-of-line 1)
  9672. (org-move-to-column (- (window-width) 19) t)
  9673. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9674. (defun org-set-current-tags-overlay (current prefix)
  9675. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9676. (if (featurep 'xemacs)
  9677. (org-overlay-display org-tags-overlay (concat prefix s)
  9678. 'secondary-selection)
  9679. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9680. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9681. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9682. "Fast tag selection with single keys.
  9683. CURRENT is the current list of tags in the headline, INHERITED is the
  9684. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9685. possibly with grouping information. TODO-TABLE is a similar table with
  9686. TODO keywords, should these have keys assigned to them.
  9687. If the keys are nil, a-z are automatically assigned.
  9688. Returns the new tags string, or nil to not change the current settings."
  9689. (let* ((fulltable (append table todo-table))
  9690. (maxlen (apply 'max (mapcar
  9691. (lambda (x)
  9692. (if (stringp (car x)) (string-width (car x)) 0))
  9693. fulltable)))
  9694. (buf (current-buffer))
  9695. (expert (eq org-fast-tag-selection-single-key 'expert))
  9696. (buffer-tags nil)
  9697. (fwidth (+ maxlen 3 1 3))
  9698. (ncol (/ (- (window-width) 4) fwidth))
  9699. (i-face 'org-done)
  9700. (c-face 'org-todo)
  9701. tg cnt e c char c1 c2 ntable tbl rtn
  9702. ov-start ov-end ov-prefix
  9703. (exit-after-next org-fast-tag-selection-single-key)
  9704. (done-keywords org-done-keywords)
  9705. groups ingroup)
  9706. (save-excursion
  9707. (beginning-of-line 1)
  9708. (if (looking-at
  9709. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9710. (setq ov-start (match-beginning 1)
  9711. ov-end (match-end 1)
  9712. ov-prefix "")
  9713. (setq ov-start (1- (point-at-eol))
  9714. ov-end (1+ ov-start))
  9715. (skip-chars-forward "^\n\r")
  9716. (setq ov-prefix
  9717. (concat
  9718. (buffer-substring (1- (point)) (point))
  9719. (if (> (current-column) org-tags-column)
  9720. " "
  9721. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9722. (org-move-overlay org-tags-overlay ov-start ov-end)
  9723. (save-window-excursion
  9724. (if expert
  9725. (set-buffer (get-buffer-create " *Org tags*"))
  9726. (delete-other-windows)
  9727. (split-window-vertically)
  9728. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9729. (erase-buffer)
  9730. (org-set-local 'org-done-keywords done-keywords)
  9731. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9732. (org-fast-tag-insert "Current" current c-face "\n\n")
  9733. (org-fast-tag-show-exit exit-after-next)
  9734. (org-set-current-tags-overlay current ov-prefix)
  9735. (setq tbl fulltable char ?a cnt 0)
  9736. (while (setq e (pop tbl))
  9737. (cond
  9738. ((equal e '(:startgroup))
  9739. (push '() groups) (setq ingroup t)
  9740. (when (not (= cnt 0))
  9741. (setq cnt 0)
  9742. (insert "\n"))
  9743. (insert "{ "))
  9744. ((equal e '(:endgroup))
  9745. (setq ingroup nil cnt 0)
  9746. (insert "}\n"))
  9747. ((equal e '(:newline))
  9748. (when (not (= cnt 0))
  9749. (setq cnt 0)
  9750. (insert "\n")
  9751. (setq e (car tbl))
  9752. (while (equal (car tbl) '(:newline))
  9753. (insert "\n")
  9754. (setq tbl (cdr tbl)))))
  9755. (t
  9756. (setq tg (car e) c2 nil)
  9757. (if (cdr e)
  9758. (setq c (cdr e))
  9759. ;; automatically assign a character.
  9760. (setq c1 (string-to-char
  9761. (downcase (substring
  9762. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9763. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9764. (while (or (rassoc char ntable) (rassoc char table))
  9765. (setq char (1+ char)))
  9766. (setq c2 c1))
  9767. (setq c (or c2 char)))
  9768. (if ingroup (push tg (car groups)))
  9769. (setq tg (org-add-props tg nil 'face
  9770. (cond
  9771. ((not (assoc tg table))
  9772. (org-get-todo-face tg))
  9773. ((member tg current) c-face)
  9774. ((member tg inherited) i-face)
  9775. (t nil))))
  9776. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9777. (insert "[" c "] " tg (make-string
  9778. (- fwidth 4 (length tg)) ?\ ))
  9779. (push (cons tg c) ntable)
  9780. (when (= (setq cnt (1+ cnt)) ncol)
  9781. (insert "\n")
  9782. (if ingroup (insert " "))
  9783. (setq cnt 0)))))
  9784. (setq ntable (nreverse ntable))
  9785. (insert "\n")
  9786. (goto-char (point-min))
  9787. (if (not expert) (org-fit-window-to-buffer))
  9788. (setq rtn
  9789. (catch 'exit
  9790. (while t
  9791. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9792. (if groups " [!] no groups" " [!]groups")
  9793. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9794. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9795. (cond
  9796. ((= c ?\r) (throw 'exit t))
  9797. ((= c ?!)
  9798. (setq groups (not groups))
  9799. (goto-char (point-min))
  9800. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9801. ((= c ?\C-c)
  9802. (if (not expert)
  9803. (org-fast-tag-show-exit
  9804. (setq exit-after-next (not exit-after-next)))
  9805. (setq expert nil)
  9806. (delete-other-windows)
  9807. (split-window-vertically)
  9808. (org-switch-to-buffer-other-window " *Org tags*")
  9809. (org-fit-window-to-buffer)))
  9810. ((or (= c ?\C-g)
  9811. (and (= c ?q) (not (rassoc c ntable))))
  9812. (org-detach-overlay org-tags-overlay)
  9813. (setq quit-flag t))
  9814. ((= c ?\ )
  9815. (setq current nil)
  9816. (if exit-after-next (setq exit-after-next 'now)))
  9817. ((= c ?\t)
  9818. (condition-case nil
  9819. (setq tg (org-ido-completing-read
  9820. "Tag: "
  9821. (or buffer-tags
  9822. (with-current-buffer buf
  9823. (org-get-buffer-tags)))))
  9824. (quit (setq tg "")))
  9825. (when (string-match "\\S-" tg)
  9826. (add-to-list 'buffer-tags (list tg))
  9827. (if (member tg current)
  9828. (setq current (delete tg current))
  9829. (push tg current)))
  9830. (if exit-after-next (setq exit-after-next 'now)))
  9831. ((setq e (rassoc c todo-table) tg (car e))
  9832. (with-current-buffer buf
  9833. (save-excursion (org-todo tg)))
  9834. (if exit-after-next (setq exit-after-next 'now)))
  9835. ((setq e (rassoc c ntable) tg (car e))
  9836. (if (member tg current)
  9837. (setq current (delete tg current))
  9838. (loop for g in groups do
  9839. (if (member tg g)
  9840. (mapc (lambda (x)
  9841. (setq current (delete x current)))
  9842. g)))
  9843. (push tg current))
  9844. (if exit-after-next (setq exit-after-next 'now))))
  9845. ;; Create a sorted list
  9846. (setq current
  9847. (sort current
  9848. (lambda (a b)
  9849. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9850. (if (eq exit-after-next 'now) (throw 'exit t))
  9851. (goto-char (point-min))
  9852. (beginning-of-line 2)
  9853. (delete-region (point) (point-at-eol))
  9854. (org-fast-tag-insert "Current" current c-face)
  9855. (org-set-current-tags-overlay current ov-prefix)
  9856. (while (re-search-forward
  9857. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9858. (setq tg (match-string 1))
  9859. (add-text-properties
  9860. (match-beginning 1) (match-end 1)
  9861. (list 'face
  9862. (cond
  9863. ((member tg current) c-face)
  9864. ((member tg inherited) i-face)
  9865. (t (get-text-property (match-beginning 1) 'face))))))
  9866. (goto-char (point-min)))))
  9867. (org-detach-overlay org-tags-overlay)
  9868. (if rtn
  9869. (mapconcat 'identity current ":")
  9870. nil))))
  9871. (defun org-get-tags-string ()
  9872. "Get the TAGS string in the current headline."
  9873. (unless (org-on-heading-p t)
  9874. (error "Not on a heading"))
  9875. (save-excursion
  9876. (beginning-of-line 1)
  9877. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9878. (org-match-string-no-properties 1)
  9879. "")))
  9880. (defun org-get-tags ()
  9881. "Get the list of tags specified in the current headline."
  9882. (org-split-string (org-get-tags-string) ":"))
  9883. (defun org-get-buffer-tags ()
  9884. "Get a table of all tags used in the buffer, for completion."
  9885. (let (tags)
  9886. (save-excursion
  9887. (goto-char (point-min))
  9888. (while (re-search-forward
  9889. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9890. (when (equal (char-after (point-at-bol 0)) ?*)
  9891. (mapc (lambda (x) (add-to-list 'tags x))
  9892. (org-split-string (org-match-string-no-properties 1) ":")))))
  9893. (mapcar 'list tags)))
  9894. ;;;; The mapping API
  9895. ;;;###autoload
  9896. (defun org-map-entries (func &optional match scope &rest skip)
  9897. "Call FUNC at each headline selected by MATCH in SCOPE.
  9898. FUNC is a function or a lisp form. The function will be called without
  9899. arguments, with the cursor positioned at the beginning of the headline.
  9900. The return values of all calls to the function will be collected and
  9901. returned as a list.
  9902. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9903. Only headlines that are matched by this query will be considered during
  9904. the iteration. When MATCH is nil or t, all headlines will be
  9905. visited by the iteration.
  9906. SCOPE determines the scope of this command. It can be any of:
  9907. nil The current buffer, respecting the restriction if any
  9908. tree The subtree started with the entry at point
  9909. file The current buffer, without restriction
  9910. file-with-archives
  9911. The current buffer, and any archives associated with it
  9912. agenda All agenda files
  9913. agenda-with-archives
  9914. All agenda files with any archive files associated with them
  9915. \(file1 file2 ...)
  9916. If this is a list, all files in the list will be scanned
  9917. The remaining args are treated as settings for the skipping facilities of
  9918. the scanner. The following items can be given here:
  9919. archive skip trees with the archive tag.
  9920. comment skip trees with the COMMENT keyword
  9921. function or Emacs Lisp form:
  9922. will be used as value for `org-agenda-skip-function', so whenever
  9923. the the function returns t, FUNC will not be called for that
  9924. entry and search will continue from the point where the
  9925. function leaves it.
  9926. If your function needs to retrieve the tags including inherited tags
  9927. at the *current* entry, you can use the value of the variable
  9928. `org-scanner-tags' which will be much faster than getting the value
  9929. with `org-get-tags-at'. If your function gets properties with
  9930. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  9931. to t around the call to `org-entry-properties' to get the same speedup.
  9932. Note that if your function moves around to retrieve tags and properties at
  9933. a *different* entry, you cannot use these techniques."
  9934. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9935. (org-agenda-skip-archived-trees (memq 'archive skip))
  9936. (org-agenda-skip-comment-trees (memq 'comment skip))
  9937. (org-agenda-skip-function
  9938. (car (org-delete-all '(comment archive) skip)))
  9939. (org-tags-match-list-sublevels t)
  9940. matcher file res
  9941. org-todo-keywords-for-agenda
  9942. org-done-keywords-for-agenda
  9943. org-todo-keyword-alist-for-agenda
  9944. org-tag-alist-for-agenda)
  9945. (cond
  9946. ((eq match t) (setq matcher t))
  9947. ((eq match nil) (setq matcher t))
  9948. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9949. (save-excursion
  9950. (save-restriction
  9951. (when (eq scope 'tree)
  9952. (org-back-to-heading t)
  9953. (org-narrow-to-subtree)
  9954. (setq scope nil))
  9955. (if (not scope)
  9956. (progn
  9957. (org-prepare-agenda-buffers
  9958. (list (buffer-file-name (current-buffer))))
  9959. (setq res (org-scan-tags func matcher)))
  9960. ;; Get the right scope
  9961. (cond
  9962. ((and scope (listp scope) (symbolp (car scope)))
  9963. (setq scope (eval scope)))
  9964. ((eq scope 'agenda)
  9965. (setq scope (org-agenda-files t)))
  9966. ((eq scope 'agenda-with-archives)
  9967. (setq scope (org-agenda-files t))
  9968. (setq scope (org-add-archive-files scope)))
  9969. ((eq scope 'file)
  9970. (setq scope (list (buffer-file-name))))
  9971. ((eq scope 'file-with-archives)
  9972. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9973. (org-prepare-agenda-buffers scope)
  9974. (while (setq file (pop scope))
  9975. (with-current-buffer (org-find-base-buffer-visiting file)
  9976. (save-excursion
  9977. (save-restriction
  9978. (widen)
  9979. (goto-char (point-min))
  9980. (setq res (append res (org-scan-tags func matcher))))))))))
  9981. res))
  9982. ;;;; Properties
  9983. ;;; Setting and retrieving properties
  9984. (defconst org-special-properties
  9985. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9986. "TIMESTAMP" "TIMESTAMP_IA")
  9987. "The special properties valid in Org-mode.
  9988. These are properties that are not defined in the property drawer,
  9989. but in some other way.")
  9990. (defconst org-default-properties
  9991. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9992. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9993. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9994. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9995. "Some properties that are used by Org-mode for various purposes.
  9996. Being in this list makes sure that they are offered for completion.")
  9997. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9998. "Regular expression matching the first line of a property drawer.")
  9999. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10000. "Regular expression matching the first line of a property drawer.")
  10001. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10002. "Regular expression matching the first line of a property drawer.")
  10003. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10004. "Regular expression matching the first line of a property drawer.")
  10005. (defconst org-property-drawer-re
  10006. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10007. org-property-end-re "\\)\n?")
  10008. "Matches an entire property drawer.")
  10009. (defconst org-clock-drawer-re
  10010. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10011. org-property-end-re "\\)\n?")
  10012. "Matches an entire clock drawer.")
  10013. (defun org-property-action ()
  10014. "Do an action on properties."
  10015. (interactive)
  10016. (let (c)
  10017. (org-at-property-p)
  10018. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10019. (setq c (read-char-exclusive))
  10020. (cond
  10021. ((equal c ?s)
  10022. (call-interactively 'org-set-property))
  10023. ((equal c ?d)
  10024. (call-interactively 'org-delete-property))
  10025. ((equal c ?D)
  10026. (call-interactively 'org-delete-property-globally))
  10027. ((equal c ?c)
  10028. (call-interactively 'org-compute-property-at-point))
  10029. (t (error "No such property action %c" c)))))
  10030. (defun org-at-property-p ()
  10031. "Is the cursor in a property line?"
  10032. ;; FIXME: Does not check if we are actually in the drawer.
  10033. ;; FIXME: also returns true on any drawers.....
  10034. ;; This is used by C-c C-c for property action.
  10035. (save-excursion
  10036. (beginning-of-line 1)
  10037. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10038. (defun org-get-property-block (&optional beg end force)
  10039. "Return the (beg . end) range of the body of the property drawer.
  10040. BEG and END can be beginning and end of subtree, if not given
  10041. they will be found.
  10042. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10043. (catch 'exit
  10044. (save-excursion
  10045. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10046. (end (or end (progn (outline-next-heading) (point)))))
  10047. (goto-char beg)
  10048. (if (re-search-forward org-property-start-re end t)
  10049. (setq beg (1+ (match-end 0)))
  10050. (if force
  10051. (save-excursion
  10052. (org-insert-property-drawer)
  10053. (setq end (progn (outline-next-heading) (point))))
  10054. (throw 'exit nil))
  10055. (goto-char beg)
  10056. (if (re-search-forward org-property-start-re end t)
  10057. (setq beg (1+ (match-end 0)))))
  10058. (if (re-search-forward org-property-end-re end t)
  10059. (setq end (match-beginning 0))
  10060. (or force (throw 'exit nil))
  10061. (goto-char beg)
  10062. (setq end beg)
  10063. (org-indent-line-function)
  10064. (insert ":END:\n"))
  10065. (cons beg end)))))
  10066. (defun org-entry-properties (&optional pom which)
  10067. "Get all properties of the entry at point-or-marker POM.
  10068. This includes the TODO keyword, the tags, time strings for deadline,
  10069. scheduled, and clocking, and any additional properties defined in the
  10070. entry. The return value is an alist, keys may occur multiple times
  10071. if the property key was used several times.
  10072. POM may also be nil, in which case the current entry is used.
  10073. If WHICH is nil or `all', get all properties. If WHICH is
  10074. `special' or `standard', only get that subclass."
  10075. (setq which (or which 'all))
  10076. (org-with-point-at pom
  10077. (let ((clockstr (substring org-clock-string 0 -1))
  10078. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10079. beg end range props sum-props key value string clocksum)
  10080. (save-excursion
  10081. (when (condition-case nil
  10082. (and (org-mode-p) (org-back-to-heading t))
  10083. (error nil))
  10084. (setq beg (point))
  10085. (setq sum-props (get-text-property (point) 'org-summaries))
  10086. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10087. (outline-next-heading)
  10088. (setq end (point))
  10089. (when (memq which '(all special))
  10090. ;; Get the special properties, like TODO and tags
  10091. (goto-char beg)
  10092. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10093. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10094. (when (looking-at org-priority-regexp)
  10095. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10096. (when (and (setq value (org-get-tags-string))
  10097. (string-match "\\S-" value))
  10098. (push (cons "TAGS" value) props))
  10099. (when (setq value (org-get-tags-at))
  10100. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10101. props))
  10102. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10103. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10104. string (if (equal key clockstr)
  10105. (org-no-properties
  10106. (org-trim
  10107. (buffer-substring
  10108. (match-beginning 3) (goto-char (point-at-eol)))))
  10109. (substring (org-match-string-no-properties 3) 1 -1)))
  10110. (unless key
  10111. (if (= (char-after (match-beginning 3)) ?\[)
  10112. (setq key "TIMESTAMP_IA")
  10113. (setq key "TIMESTAMP")))
  10114. (when (or (equal key clockstr) (not (assoc key props)))
  10115. (push (cons key string) props)))
  10116. )
  10117. (when (memq which '(all standard))
  10118. ;; Get the standard properties, like :PROP: ...
  10119. (setq range (org-get-property-block beg end))
  10120. (when range
  10121. (goto-char (car range))
  10122. (while (re-search-forward
  10123. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10124. (cdr range) t)
  10125. (setq key (org-match-string-no-properties 1)
  10126. value (org-trim (or (org-match-string-no-properties 2) "")))
  10127. (unless (member key excluded)
  10128. (push (cons key (or value "")) props)))))
  10129. (if clocksum
  10130. (push (cons "CLOCKSUM"
  10131. (org-columns-number-to-string (/ (float clocksum) 60.)
  10132. 'add_times))
  10133. props))
  10134. (unless (assoc "CATEGORY" props)
  10135. (setq value (or (org-get-category)
  10136. (progn (org-refresh-category-properties)
  10137. (org-get-category))))
  10138. (push (cons "CATEGORY" value) props))
  10139. (append sum-props (nreverse props)))))))
  10140. (defun org-entry-get (pom property &optional inherit)
  10141. "Get value of PROPERTY for entry at point-or-marker POM.
  10142. If INHERIT is non-nil and the entry does not have the property,
  10143. then also check higher levels of the hierarchy.
  10144. If INHERIT is the symbol `selective', use inheritance only if the setting
  10145. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10146. If the property is present but empty, the return value is the empty string.
  10147. If the property is not present at all, nil is returned."
  10148. (org-with-point-at pom
  10149. (if (and inherit (if (eq inherit 'selective)
  10150. (org-property-inherit-p property)
  10151. t))
  10152. (org-entry-get-with-inheritance property)
  10153. (if (member property org-special-properties)
  10154. ;; We need a special property. Use brute force, get all properties.
  10155. (cdr (assoc property (org-entry-properties nil 'special)))
  10156. (let ((range (org-get-property-block)))
  10157. (if (and range
  10158. (goto-char (car range))
  10159. (re-search-forward
  10160. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10161. (cdr range) t))
  10162. ;; Found the property, return it.
  10163. (if (match-end 1)
  10164. (org-match-string-no-properties 1)
  10165. "")))))))
  10166. (defun org-property-or-variable-value (var &optional inherit)
  10167. "Check if there is a property fixing the value of VAR.
  10168. If yes, return this value. If not, return the current value of the variable."
  10169. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10170. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10171. (read prop)
  10172. (symbol-value var))))
  10173. (defun org-entry-delete (pom property)
  10174. "Delete the property PROPERTY from entry at point-or-marker POM."
  10175. (org-with-point-at pom
  10176. (if (member property org-special-properties)
  10177. nil ; cannot delete these properties.
  10178. (let ((range (org-get-property-block)))
  10179. (if (and range
  10180. (goto-char (car range))
  10181. (re-search-forward
  10182. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10183. (cdr range) t))
  10184. (progn
  10185. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10186. t)
  10187. nil)))))
  10188. ;; Multi-values properties are properties that contain multiple values
  10189. ;; These values are assumed to be single words, separated by whitespace.
  10190. (defun org-entry-add-to-multivalued-property (pom property value)
  10191. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10192. (let* ((old (org-entry-get pom property))
  10193. (values (and old (org-split-string old "[ \t]"))))
  10194. (setq value (org-entry-protect-space value))
  10195. (unless (member value values)
  10196. (setq values (cons value values))
  10197. (org-entry-put pom property
  10198. (mapconcat 'identity values " ")))))
  10199. (defun org-entry-remove-from-multivalued-property (pom property value)
  10200. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10201. (let* ((old (org-entry-get pom property))
  10202. (values (and old (org-split-string old "[ \t]"))))
  10203. (setq value (org-entry-protect-space value))
  10204. (when (member value values)
  10205. (setq values (delete value values))
  10206. (org-entry-put pom property
  10207. (mapconcat 'identity values " ")))))
  10208. (defun org-entry-member-in-multivalued-property (pom property value)
  10209. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10210. (let* ((old (org-entry-get pom property))
  10211. (values (and old (org-split-string old "[ \t]"))))
  10212. (setq value (org-entry-protect-space value))
  10213. (member value values)))
  10214. (defun org-entry-get-multivalued-property (pom property)
  10215. "Return a list of values in a multivalued property."
  10216. (let* ((value (org-entry-get pom property))
  10217. (values (and value (org-split-string value "[ \t]"))))
  10218. (mapcar 'org-entry-restore-space values)))
  10219. (defun org-entry-put-multivalued-property (pom property &rest values)
  10220. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10221. VALUES should be a list of strings. Spaces will be protected."
  10222. (org-entry-put pom property
  10223. (mapconcat 'org-entry-protect-space values " "))
  10224. (let* ((value (org-entry-get pom property))
  10225. (values (and value (org-split-string value "[ \t]"))))
  10226. (mapcar 'org-entry-restore-space values)))
  10227. (defun org-entry-protect-space (s)
  10228. "Protect spaces and newline in string S."
  10229. (while (string-match " " s)
  10230. (setq s (replace-match "%20" t t s)))
  10231. (while (string-match "\n" s)
  10232. (setq s (replace-match "%0A" t t s)))
  10233. s)
  10234. (defun org-entry-restore-space (s)
  10235. "Restore spaces and newline in string S."
  10236. (while (string-match "%20" s)
  10237. (setq s (replace-match " " t t s)))
  10238. (while (string-match "%0A" s)
  10239. (setq s (replace-match "\n" t t s)))
  10240. s)
  10241. (defvar org-entry-property-inherited-from (make-marker)
  10242. "Marker pointing to the entry from where a property was inherited.
  10243. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10244. location of the entry where the inheritance search matched. If there was
  10245. no match, the marker will point nowhere.
  10246. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10247. is set.")
  10248. (defun org-entry-get-with-inheritance (property)
  10249. "Get entry property, and search higher levels if not present."
  10250. (move-marker org-entry-property-inherited-from nil)
  10251. (let (tmp)
  10252. (save-excursion
  10253. (save-restriction
  10254. (widen)
  10255. (catch 'ex
  10256. (while t
  10257. (when (setq tmp (org-entry-get nil property))
  10258. (org-back-to-heading t)
  10259. (move-marker org-entry-property-inherited-from (point))
  10260. (throw 'ex tmp))
  10261. (or (org-up-heading-safe) (throw 'ex nil)))))
  10262. (or tmp
  10263. (cdr (assoc property org-file-properties))
  10264. (cdr (assoc property org-global-properties))
  10265. (cdr (assoc property org-global-properties-fixed))))))
  10266. (defun org-entry-put (pom property value)
  10267. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10268. (org-with-point-at pom
  10269. (org-back-to-heading t)
  10270. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10271. range)
  10272. (cond
  10273. ((equal property "TODO")
  10274. (when (and (stringp value) (string-match "\\S-" value)
  10275. (not (member value org-todo-keywords-1)))
  10276. (error "\"%s\" is not a valid TODO state" value))
  10277. (if (or (not value)
  10278. (not (string-match "\\S-" value)))
  10279. (setq value 'none))
  10280. (org-todo value)
  10281. (org-set-tags nil 'align))
  10282. ((equal property "PRIORITY")
  10283. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10284. (string-to-char value) ?\ ))
  10285. (org-set-tags nil 'align))
  10286. ((equal property "SCHEDULED")
  10287. (if (re-search-forward org-scheduled-time-regexp end t)
  10288. (cond
  10289. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10290. ((eq value 'later) (org-timestamp-change 1 'day))
  10291. (t (call-interactively 'org-schedule)))
  10292. (call-interactively 'org-schedule)))
  10293. ((equal property "DEADLINE")
  10294. (if (re-search-forward org-deadline-time-regexp end t)
  10295. (cond
  10296. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10297. ((eq value 'later) (org-timestamp-change 1 'day))
  10298. (t (call-interactively 'org-deadline)))
  10299. (call-interactively 'org-deadline)))
  10300. ((member property org-special-properties)
  10301. (error "The %s property can not yet be set with `org-entry-put'"
  10302. property))
  10303. (t ; a non-special property
  10304. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10305. (setq range (org-get-property-block beg end 'force))
  10306. (goto-char (car range))
  10307. (if (re-search-forward
  10308. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10309. (progn
  10310. (delete-region (match-beginning 1) (match-end 1))
  10311. (goto-char (match-beginning 1)))
  10312. (goto-char (cdr range))
  10313. (insert "\n")
  10314. (backward-char 1)
  10315. (org-indent-line-function)
  10316. (insert ":" property ":"))
  10317. (and value (insert " " value))
  10318. (org-indent-line-function)))))))
  10319. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10320. "Get all property keys in the current buffer.
  10321. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10322. like tags and TODO state.
  10323. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10324. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10325. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10326. formats in the current buffer."
  10327. (let (rtn range cfmt s p)
  10328. (save-excursion
  10329. (save-restriction
  10330. (widen)
  10331. (goto-char (point-min))
  10332. (while (re-search-forward org-property-start-re nil t)
  10333. (setq range (org-get-property-block))
  10334. (goto-char (car range))
  10335. (while (re-search-forward
  10336. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10337. (cdr range) t)
  10338. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10339. (outline-next-heading))))
  10340. (when include-specials
  10341. (setq rtn (append org-special-properties rtn)))
  10342. (when include-defaults
  10343. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10344. (when include-columns
  10345. (save-excursion
  10346. (save-restriction
  10347. (widen)
  10348. (goto-char (point-min))
  10349. (while (re-search-forward
  10350. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10351. nil t)
  10352. (setq cfmt (match-string 2) s 0)
  10353. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10354. cfmt s)
  10355. (setq s (match-end 0)
  10356. p (match-string 1 cfmt))
  10357. (unless (or (equal p "ITEM")
  10358. (member p org-special-properties))
  10359. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10360. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10361. (defun org-property-values (key)
  10362. "Return a list of all values of property KEY."
  10363. (save-excursion
  10364. (save-restriction
  10365. (widen)
  10366. (goto-char (point-min))
  10367. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10368. values)
  10369. (while (re-search-forward re nil t)
  10370. (add-to-list 'values (org-trim (match-string 1))))
  10371. (delete "" values)))))
  10372. (defun org-insert-property-drawer ()
  10373. "Insert a property drawer into the current entry."
  10374. (interactive)
  10375. (org-back-to-heading t)
  10376. (looking-at outline-regexp)
  10377. (let ((indent (- (match-end 0)(match-beginning 0)))
  10378. (beg (point))
  10379. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10380. end hiddenp)
  10381. (outline-next-heading)
  10382. (setq end (point))
  10383. (goto-char beg)
  10384. (while (re-search-forward re end t))
  10385. (setq hiddenp (org-invisible-p))
  10386. (end-of-line 1)
  10387. (and (equal (char-after) ?\n) (forward-char 1))
  10388. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10389. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10390. ;; just skip this line
  10391. (beginning-of-line 2)
  10392. ;; Drawer start, find the end
  10393. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10394. (beginning-of-line 1)))
  10395. (org-skip-over-state-notes)
  10396. (skip-chars-backward " \t\n\r")
  10397. (if (eq (char-before) ?*) (forward-char 1))
  10398. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10399. (beginning-of-line 0)
  10400. (org-indent-to-column indent)
  10401. (beginning-of-line 2)
  10402. (org-indent-to-column indent)
  10403. (beginning-of-line 0)
  10404. (if hiddenp
  10405. (save-excursion
  10406. (org-back-to-heading t)
  10407. (hide-entry))
  10408. (org-flag-drawer t))))
  10409. (defun org-set-property (property value)
  10410. "In the current entry, set PROPERTY to VALUE.
  10411. When called interactively, this will prompt for a property name, offering
  10412. completion on existing and default properties. And then it will prompt
  10413. for a value, offering completion either on allowed values (via an inherited
  10414. xxx_ALL property) or on existing values in other instances of this property
  10415. in the current file."
  10416. (interactive
  10417. (let* ((completion-ignore-case t)
  10418. (keys (org-buffer-property-keys nil t t))
  10419. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10420. (prop (if (member prop0 keys)
  10421. prop0
  10422. (or (cdr (assoc (downcase prop0)
  10423. (mapcar (lambda (x) (cons (downcase x) x))
  10424. keys)))
  10425. prop0)))
  10426. (cur (org-entry-get nil prop))
  10427. (allowed (org-property-get-allowed-values nil prop 'table))
  10428. (existing (mapcar 'list (org-property-values prop)))
  10429. (val (if allowed
  10430. (org-completing-read "Value: " allowed nil 'req-match)
  10431. (let (org-completion-use-ido)
  10432. (org-completing-read
  10433. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10434. (concat "[" cur "]") "")
  10435. ": ")
  10436. existing nil nil "" nil cur)))))
  10437. (list prop (if (equal val "") cur val))))
  10438. (unless (equal (org-entry-get nil property) value)
  10439. (org-entry-put nil property value)))
  10440. (defun org-delete-property (property)
  10441. "In the current entry, delete PROPERTY."
  10442. (interactive
  10443. (let* ((completion-ignore-case t)
  10444. (prop (org-ido-completing-read
  10445. "Property: " (org-entry-properties nil 'standard))))
  10446. (list prop)))
  10447. (message "Property %s %s" property
  10448. (if (org-entry-delete nil property)
  10449. "deleted"
  10450. "was not present in the entry")))
  10451. (defun org-delete-property-globally (property)
  10452. "Remove PROPERTY globally, from all entries."
  10453. (interactive
  10454. (let* ((completion-ignore-case t)
  10455. (prop (org-ido-completing-read
  10456. "Globally remove property: "
  10457. (mapcar 'list (org-buffer-property-keys)))))
  10458. (list prop)))
  10459. (save-excursion
  10460. (save-restriction
  10461. (widen)
  10462. (goto-char (point-min))
  10463. (let ((cnt 0))
  10464. (while (re-search-forward
  10465. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10466. nil t)
  10467. (setq cnt (1+ cnt))
  10468. (replace-match ""))
  10469. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10470. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10471. (defun org-compute-property-at-point ()
  10472. "Compute the property at point.
  10473. This looks for an enclosing column format, extracts the operator and
  10474. then applies it to the property in the column format's scope."
  10475. (interactive)
  10476. (unless (org-at-property-p)
  10477. (error "Not at a property"))
  10478. (let ((prop (org-match-string-no-properties 2)))
  10479. (org-columns-get-format-and-top-level)
  10480. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10481. (error "No operator defined for property %s" prop))
  10482. (org-columns-compute prop)))
  10483. (defun org-property-get-allowed-values (pom property &optional table)
  10484. "Get allowed values for the property PROPERTY.
  10485. When TABLE is non-nil, return an alist that can directly be used for
  10486. completion."
  10487. (let (vals)
  10488. (cond
  10489. ((equal property "TODO")
  10490. (setq vals (org-with-point-at pom
  10491. (append org-todo-keywords-1 '("")))))
  10492. ((equal property "PRIORITY")
  10493. (let ((n org-lowest-priority))
  10494. (while (>= n org-highest-priority)
  10495. (push (char-to-string n) vals)
  10496. (setq n (1- n)))))
  10497. ((member property org-special-properties))
  10498. (t
  10499. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10500. (when (and vals (string-match "\\S-" vals))
  10501. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10502. (setq vals (mapcar (lambda (x)
  10503. (cond ((stringp x) x)
  10504. ((numberp x) (number-to-string x))
  10505. ((symbolp x) (symbol-name x))
  10506. (t "???")))
  10507. vals)))))
  10508. (if table (mapcar 'list vals) vals)))
  10509. (defun org-property-previous-allowed-value (&optional previous)
  10510. "Switch to the next allowed value for this property."
  10511. (interactive)
  10512. (org-property-next-allowed-value t))
  10513. (defun org-property-next-allowed-value (&optional previous)
  10514. "Switch to the next allowed value for this property."
  10515. (interactive)
  10516. (unless (org-at-property-p)
  10517. (error "Not at a property"))
  10518. (let* ((key (match-string 2))
  10519. (value (match-string 3))
  10520. (allowed (or (org-property-get-allowed-values (point) key)
  10521. (and (member value '("[ ]" "[-]" "[X]"))
  10522. '("[ ]" "[X]"))))
  10523. nval)
  10524. (unless allowed
  10525. (error "Allowed values for this property have not been defined"))
  10526. (if previous (setq allowed (reverse allowed)))
  10527. (if (member value allowed)
  10528. (setq nval (car (cdr (member value allowed)))))
  10529. (setq nval (or nval (car allowed)))
  10530. (if (equal nval value)
  10531. (error "Only one allowed value for this property"))
  10532. (org-at-property-p)
  10533. (replace-match (concat " :" key ": " nval) t t)
  10534. (org-indent-line-function)
  10535. (beginning-of-line 1)
  10536. (skip-chars-forward " \t")))
  10537. (defun org-find-entry-with-id (ident)
  10538. "Locate the entry that contains the ID property with exact value IDENT.
  10539. IDENT can be a string, a symbol or a number, this function will search for
  10540. the string representation of it.
  10541. Return the position where this entry starts, or nil if there is no such entry."
  10542. (interactive "sID: ")
  10543. (let ((id (cond
  10544. ((stringp ident) ident)
  10545. ((symbol-name ident) (symbol-name ident))
  10546. ((numberp ident) (number-to-string ident))
  10547. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10548. (case-fold-search nil))
  10549. (save-excursion
  10550. (save-restriction
  10551. (widen)
  10552. (goto-char (point-min))
  10553. (when (re-search-forward
  10554. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10555. nil t)
  10556. (org-back-to-heading)
  10557. (point))))))
  10558. ;;;; Timestamps
  10559. (defvar org-last-changed-timestamp nil)
  10560. (defvar org-last-inserted-timestamp nil
  10561. "The last time stamp inserted with `org-insert-time-stamp'.")
  10562. (defvar org-time-was-given) ; dynamically scoped parameter
  10563. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10564. (defvar org-ts-what) ; dynamically scoped parameter
  10565. (defun org-time-stamp (arg &optional inactive)
  10566. "Prompt for a date/time and insert a time stamp.
  10567. If the user specifies a time like HH:MM, or if this command is called
  10568. with a prefix argument, the time stamp will contain date and time.
  10569. Otherwise, only the date will be included. All parts of a date not
  10570. specified by the user will be filled in from the current date/time.
  10571. So if you press just return without typing anything, the time stamp
  10572. will represent the current date/time. If there is already a timestamp
  10573. at the cursor, it will be modified."
  10574. (interactive "P")
  10575. (let* ((ts nil)
  10576. (default-time
  10577. ;; Default time is either today, or, when entering a range,
  10578. ;; the range start.
  10579. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10580. (save-excursion
  10581. (re-search-backward
  10582. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10583. (- (point) 20) t)))
  10584. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10585. (current-time)))
  10586. (default-input (and ts (org-get-compact-tod ts)))
  10587. org-time-was-given org-end-time-was-given time)
  10588. (cond
  10589. ((and (org-at-timestamp-p t)
  10590. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10591. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10592. (insert "--")
  10593. (setq time (let ((this-command this-command))
  10594. (org-read-date arg 'totime nil nil
  10595. default-time default-input)))
  10596. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10597. ((org-at-timestamp-p t)
  10598. (setq time (let ((this-command this-command))
  10599. (org-read-date arg 'totime nil nil default-time default-input)))
  10600. (when (org-at-timestamp-p t) ; just to get the match data
  10601. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10602. (replace-match "")
  10603. (setq org-last-changed-timestamp
  10604. (org-insert-time-stamp
  10605. time (or org-time-was-given arg)
  10606. inactive nil nil (list org-end-time-was-given))))
  10607. (message "Timestamp updated"))
  10608. (t
  10609. (setq time (let ((this-command this-command))
  10610. (org-read-date arg 'totime nil nil default-time default-input)))
  10611. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10612. nil nil (list org-end-time-was-given))))))
  10613. ;; FIXME: can we use this for something else, like computing time differences?
  10614. (defun org-get-compact-tod (s)
  10615. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10616. (let* ((t1 (match-string 1 s))
  10617. (h1 (string-to-number (match-string 2 s)))
  10618. (m1 (string-to-number (match-string 3 s)))
  10619. (t2 (and (match-end 4) (match-string 5 s)))
  10620. (h2 (and t2 (string-to-number (match-string 6 s))))
  10621. (m2 (and t2 (string-to-number (match-string 7 s))))
  10622. dh dm)
  10623. (if (not t2)
  10624. t1
  10625. (setq dh (- h2 h1) dm (- m2 m1))
  10626. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10627. (concat t1 "+" (number-to-string dh)
  10628. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10629. (defun org-time-stamp-inactive (&optional arg)
  10630. "Insert an inactive time stamp.
  10631. An inactive time stamp is enclosed in square brackets instead of angle
  10632. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10633. does not link to the calendar and cannot be changed with the S-cursor keys.
  10634. So these are more for recording a certain time/date."
  10635. (interactive "P")
  10636. (org-time-stamp arg 'inactive))
  10637. (defvar org-date-ovl (org-make-overlay 1 1))
  10638. (org-overlay-put org-date-ovl 'face 'org-warning)
  10639. (org-detach-overlay org-date-ovl)
  10640. (defvar org-ans1) ; dynamically scoped parameter
  10641. (defvar org-ans2) ; dynamically scoped parameter
  10642. (defvar org-plain-time-of-day-regexp) ; defined below
  10643. (defvar org-overriding-default-time nil) ; dynamically scoped
  10644. (defvar org-read-date-overlay nil)
  10645. (defvar org-dcst nil) ; dynamically scoped
  10646. (defvar org-read-date-history nil)
  10647. (defun org-read-date (&optional with-time to-time from-string prompt
  10648. default-time default-input)
  10649. "Read a date, possibly a time, and make things smooth for the user.
  10650. The prompt will suggest to enter an ISO date, but you can also enter anything
  10651. which will at least partially be understood by `parse-time-string'.
  10652. Unrecognized parts of the date will default to the current day, month, year,
  10653. hour and minute. If this command is called to replace a timestamp at point,
  10654. of to enter the second timestamp of a range, the default time is taken from the
  10655. existing stamp. For example,
  10656. 3-2-5 --> 2003-02-05
  10657. feb 15 --> currentyear-02-15
  10658. sep 12 9 --> 2009-09-12
  10659. 12:45 --> today 12:45
  10660. 22 sept 0:34 --> currentyear-09-22 0:34
  10661. 12 --> currentyear-currentmonth-12
  10662. Fri --> nearest Friday (today or later)
  10663. etc.
  10664. Furthermore you can specify a relative date by giving, as the *first* thing
  10665. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10666. change in days weeks, months, years.
  10667. With a single plus or minus, the date is relative to today. With a double
  10668. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10669. +4d --> four days from today
  10670. +4 --> same as above
  10671. +2w --> two weeks from today
  10672. ++5 --> five days from default date
  10673. The function understands only English month and weekday abbreviations,
  10674. but this can be configured with the variables `parse-time-months' and
  10675. `parse-time-weekdays'.
  10676. While prompting, a calendar is popped up - you can also select the
  10677. date with the mouse (button 1). The calendar shows a period of three
  10678. months. To scroll it to other months, use the keys `>' and `<'.
  10679. If you don't like the calendar, turn it off with
  10680. \(setq org-read-date-popup-calendar nil)
  10681. With optional argument TO-TIME, the date will immediately be converted
  10682. to an internal time.
  10683. With an optional argument WITH-TIME, the prompt will suggest to also
  10684. insert a time. Note that when WITH-TIME is not set, you can still
  10685. enter a time, and this function will inform the calling routine about
  10686. this change. The calling routine may then choose to change the format
  10687. used to insert the time stamp into the buffer to include the time.
  10688. With optional argument FROM-STRING, read from this string instead from
  10689. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10690. the time/date that is used for everything that is not specified by the
  10691. user."
  10692. (require 'parse-time)
  10693. (let* ((org-time-stamp-rounding-minutes
  10694. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10695. (org-dcst org-display-custom-times)
  10696. (ct (org-current-time))
  10697. (def (or org-overriding-default-time default-time ct))
  10698. (defdecode (decode-time def))
  10699. (dummy (progn
  10700. (when (< (nth 2 defdecode) org-extend-today-until)
  10701. (setcar (nthcdr 2 defdecode) -1)
  10702. (setcar (nthcdr 1 defdecode) 59)
  10703. (setq def (apply 'encode-time defdecode)
  10704. defdecode (decode-time def)))))
  10705. (calendar-move-hook nil)
  10706. (calendar-view-diary-initially-flag nil)
  10707. (view-diary-entries-initially nil)
  10708. (calendar-view-holidays-initially-flag nil)
  10709. (view-calendar-holidays-initially nil)
  10710. (timestr (format-time-string
  10711. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10712. (prompt (concat (if prompt (concat prompt " ") "")
  10713. (format "Date+time [%s]: " timestr)))
  10714. ans (org-ans0 "") org-ans1 org-ans2 final)
  10715. (cond
  10716. (from-string (setq ans from-string))
  10717. (org-read-date-popup-calendar
  10718. (save-excursion
  10719. (save-window-excursion
  10720. (calendar)
  10721. (calendar-forward-day (- (time-to-days def)
  10722. (calendar-absolute-from-gregorian
  10723. (calendar-current-date))))
  10724. (org-eval-in-calendar nil t)
  10725. (let* ((old-map (current-local-map))
  10726. (map (copy-keymap calendar-mode-map))
  10727. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10728. (org-defkey map (kbd "RET") 'org-calendar-select)
  10729. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10730. 'org-calendar-select-mouse)
  10731. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10732. 'org-calendar-select-mouse)
  10733. (org-defkey minibuffer-local-map [(meta shift left)]
  10734. (lambda () (interactive)
  10735. (org-eval-in-calendar '(calendar-backward-month 1))))
  10736. (org-defkey minibuffer-local-map [(meta shift right)]
  10737. (lambda () (interactive)
  10738. (org-eval-in-calendar '(calendar-forward-month 1))))
  10739. (org-defkey minibuffer-local-map [(meta shift up)]
  10740. (lambda () (interactive)
  10741. (org-eval-in-calendar '(calendar-backward-year 1))))
  10742. (org-defkey minibuffer-local-map [(meta shift down)]
  10743. (lambda () (interactive)
  10744. (org-eval-in-calendar '(calendar-forward-year 1))))
  10745. (org-defkey minibuffer-local-map [(shift up)]
  10746. (lambda () (interactive)
  10747. (org-eval-in-calendar '(calendar-backward-week 1))))
  10748. (org-defkey minibuffer-local-map [(shift down)]
  10749. (lambda () (interactive)
  10750. (org-eval-in-calendar '(calendar-forward-week 1))))
  10751. (org-defkey minibuffer-local-map [(shift left)]
  10752. (lambda () (interactive)
  10753. (org-eval-in-calendar '(calendar-backward-day 1))))
  10754. (org-defkey minibuffer-local-map [(shift right)]
  10755. (lambda () (interactive)
  10756. (org-eval-in-calendar '(calendar-forward-day 1))))
  10757. (org-defkey minibuffer-local-map ">"
  10758. (lambda () (interactive)
  10759. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10760. (org-defkey minibuffer-local-map "<"
  10761. (lambda () (interactive)
  10762. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10763. (run-hooks 'org-read-date-minibuffer-setup-hook)
  10764. (unwind-protect
  10765. (progn
  10766. (use-local-map map)
  10767. (add-hook 'post-command-hook 'org-read-date-display)
  10768. (setq org-ans0 (read-string prompt default-input
  10769. 'org-read-date-history nil))
  10770. ;; org-ans0: from prompt
  10771. ;; org-ans1: from mouse click
  10772. ;; org-ans2: from calendar motion
  10773. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10774. (remove-hook 'post-command-hook 'org-read-date-display)
  10775. (use-local-map old-map)
  10776. (when org-read-date-overlay
  10777. (org-delete-overlay org-read-date-overlay)
  10778. (setq org-read-date-overlay nil)))))))
  10779. (t ; Naked prompt only
  10780. (unwind-protect
  10781. (setq ans (read-string prompt default-input
  10782. 'org-read-date-history timestr))
  10783. (when org-read-date-overlay
  10784. (org-delete-overlay org-read-date-overlay)
  10785. (setq org-read-date-overlay nil)))))
  10786. (setq final (org-read-date-analyze ans def defdecode))
  10787. (if to-time
  10788. (apply 'encode-time final)
  10789. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10790. (format "%04d-%02d-%02d %02d:%02d"
  10791. (nth 5 final) (nth 4 final) (nth 3 final)
  10792. (nth 2 final) (nth 1 final))
  10793. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10794. (defvar def)
  10795. (defvar defdecode)
  10796. (defvar with-time)
  10797. (defun org-read-date-display ()
  10798. "Display the current date prompt interpretation in the minibuffer."
  10799. (when org-read-date-display-live
  10800. (when org-read-date-overlay
  10801. (org-delete-overlay org-read-date-overlay))
  10802. (let ((p (point)))
  10803. (end-of-line 1)
  10804. (while (not (equal (buffer-substring
  10805. (max (point-min) (- (point) 4)) (point))
  10806. " "))
  10807. (insert " "))
  10808. (goto-char p))
  10809. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10810. " " (or org-ans1 org-ans2)))
  10811. (org-end-time-was-given nil)
  10812. (f (org-read-date-analyze ans def defdecode))
  10813. (fmts (if org-dcst
  10814. org-time-stamp-custom-formats
  10815. org-time-stamp-formats))
  10816. (fmt (if (or with-time
  10817. (and (boundp 'org-time-was-given) org-time-was-given))
  10818. (cdr fmts)
  10819. (car fmts)))
  10820. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10821. (when (and org-end-time-was-given
  10822. (string-match org-plain-time-of-day-regexp txt))
  10823. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10824. org-end-time-was-given
  10825. (substring txt (match-end 0)))))
  10826. (setq org-read-date-overlay
  10827. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10828. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10829. (defun org-read-date-analyze (ans def defdecode)
  10830. "Analyse the combined answer of the date prompt."
  10831. ;; FIXME: cleanup and comment
  10832. (let (delta deltan deltaw deltadef year month day
  10833. hour minute second wday pm h2 m2 tl wday1
  10834. iso-year iso-weekday iso-week iso-year iso-date)
  10835. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10836. (setq ans "+0"))
  10837. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10838. (setq ans (replace-match "" t t ans)
  10839. deltan (car delta)
  10840. deltaw (nth 1 delta)
  10841. deltadef (nth 2 delta)))
  10842. ;; Check if there is an iso week date in there
  10843. ;; If yes, sore the info and postpone interpreting it until the rest
  10844. ;; of the parsing is done
  10845. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10846. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10847. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10848. iso-week (string-to-number (match-string 2 ans)))
  10849. (setq ans (replace-match "" t t ans)))
  10850. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10851. (when (string-match
  10852. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10853. (setq year (if (match-end 2)
  10854. (string-to-number (match-string 2 ans))
  10855. (string-to-number (format-time-string "%Y")))
  10856. month (string-to-number (match-string 3 ans))
  10857. day (string-to-number (match-string 4 ans)))
  10858. (if (< year 100) (setq year (+ 2000 year)))
  10859. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10860. t nil ans)))
  10861. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10862. ;; If there is a time with am/pm, and *no* time without it, we convert
  10863. ;; so that matching will be successful.
  10864. (loop for i from 1 to 2 do ; twice, for end time as well
  10865. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10866. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10867. (setq hour (string-to-number (match-string 1 ans))
  10868. minute (if (match-end 3)
  10869. (string-to-number (match-string 3 ans))
  10870. 0)
  10871. pm (equal ?p
  10872. (string-to-char (downcase (match-string 4 ans)))))
  10873. (if (and (= hour 12) (not pm))
  10874. (setq hour 0)
  10875. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10876. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10877. t t ans))))
  10878. ;; Check if a time range is given as a duration
  10879. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10880. (setq hour (string-to-number (match-string 1 ans))
  10881. h2 (+ hour (string-to-number (match-string 3 ans)))
  10882. minute (string-to-number (match-string 2 ans))
  10883. m2 (+ minute (if (match-end 5) (string-to-number
  10884. (match-string 5 ans))0)))
  10885. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10886. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10887. t t ans)))
  10888. ;; Check if there is a time range
  10889. (when (boundp 'org-end-time-was-given)
  10890. (setq org-time-was-given nil)
  10891. (when (and (string-match org-plain-time-of-day-regexp ans)
  10892. (match-end 8))
  10893. (setq org-end-time-was-given (match-string 8 ans))
  10894. (setq ans (concat (substring ans 0 (match-beginning 7))
  10895. (substring ans (match-end 7))))))
  10896. (setq tl (parse-time-string ans)
  10897. day (or (nth 3 tl) (nth 3 defdecode))
  10898. month (or (nth 4 tl)
  10899. (if (and org-read-date-prefer-future
  10900. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10901. (1+ (nth 4 defdecode))
  10902. (nth 4 defdecode)))
  10903. year (or (nth 5 tl)
  10904. (if (and org-read-date-prefer-future
  10905. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10906. (1+ (nth 5 defdecode))
  10907. (nth 5 defdecode)))
  10908. hour (or (nth 2 tl) (nth 2 defdecode))
  10909. minute (or (nth 1 tl) (nth 1 defdecode))
  10910. second (or (nth 0 tl) 0)
  10911. wday (nth 6 tl))
  10912. ;; Special date definitions below
  10913. (cond
  10914. (iso-week
  10915. ;; There was an iso week
  10916. (setq year (or iso-year year)
  10917. day (or iso-weekday wday 1)
  10918. wday nil ; to make sure that the trigger below does not match
  10919. iso-date (calendar-gregorian-from-absolute
  10920. (calendar-absolute-from-iso
  10921. (list iso-week day year))))
  10922. ; FIXME: Should we also push ISO weeks into the future?
  10923. ; (when (and org-read-date-prefer-future
  10924. ; (not iso-year)
  10925. ; (< (calendar-absolute-from-gregorian iso-date)
  10926. ; (time-to-days (current-time))))
  10927. ; (setq year (1+ year)
  10928. ; iso-date (calendar-gregorian-from-absolute
  10929. ; (calendar-absolute-from-iso
  10930. ; (list iso-week day year)))))
  10931. (setq month (car iso-date)
  10932. year (nth 2 iso-date)
  10933. day (nth 1 iso-date)))
  10934. (deltan
  10935. (unless deltadef
  10936. (let ((now (decode-time (current-time))))
  10937. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10938. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10939. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10940. ((equal deltaw "m") (setq month (+ month deltan)))
  10941. ((equal deltaw "y") (setq year (+ year deltan)))))
  10942. ((and wday (not (nth 3 tl)))
  10943. ;; Weekday was given, but no day, so pick that day in the week
  10944. ;; on or after the derived date.
  10945. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10946. (unless (equal wday wday1)
  10947. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10948. (if (and (boundp 'org-time-was-given)
  10949. (nth 2 tl))
  10950. (setq org-time-was-given t))
  10951. (if (< year 100) (setq year (+ 2000 year)))
  10952. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10953. (list second minute hour day month year)))
  10954. (defvar parse-time-weekdays)
  10955. (defun org-read-date-get-relative (s today default)
  10956. "Check string S for special relative date string.
  10957. TODAY and DEFAULT are internal times, for today and for a default.
  10958. Return shift list (N what def-flag)
  10959. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10960. N is the number of WHATs to shift.
  10961. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10962. the DEFAULT date rather than TODAY."
  10963. (when (and
  10964. (string-match
  10965. (concat
  10966. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10967. "\\([0-9]+\\)?"
  10968. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10969. "\\([ \t]\\|$\\)") s)
  10970. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10971. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10972. (string-to-char (substring (match-string 1 s) -1))
  10973. ?+))
  10974. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10975. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10976. (what (if (match-end 3) (match-string 3 s) "d"))
  10977. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10978. (date (if rel default today))
  10979. (wday (nth 6 (decode-time date)))
  10980. delta)
  10981. (if wday1
  10982. (progn
  10983. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10984. (if (= dir ?-) (setq delta (- delta 7)))
  10985. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10986. (list delta "d" rel))
  10987. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10988. (defun org-eval-in-calendar (form &optional keepdate)
  10989. "Eval FORM in the calendar window and return to current window.
  10990. Also, store the cursor date in variable org-ans2."
  10991. (let ((sw (selected-window)))
  10992. (select-window (get-buffer-window "*Calendar*"))
  10993. (eval form)
  10994. (when (and (not keepdate) (calendar-cursor-to-date))
  10995. (let* ((date (calendar-cursor-to-date))
  10996. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10997. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10998. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10999. (select-window sw)))
  11000. (defun org-calendar-select ()
  11001. "Return to `org-read-date' with the date currently selected.
  11002. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11003. (interactive)
  11004. (when (calendar-cursor-to-date)
  11005. (let* ((date (calendar-cursor-to-date))
  11006. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11007. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11008. (if (active-minibuffer-window) (exit-minibuffer))))
  11009. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11010. "Insert a date stamp for the date given by the internal TIME.
  11011. WITH-HM means, use the stamp format that includes the time of the day.
  11012. INACTIVE means use square brackets instead of angular ones, so that the
  11013. stamp will not contribute to the agenda.
  11014. PRE and POST are optional strings to be inserted before and after the
  11015. stamp.
  11016. The command returns the inserted time stamp."
  11017. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11018. stamp)
  11019. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11020. (insert-before-markers (or pre ""))
  11021. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11022. (when (listp extra)
  11023. (setq extra (car extra))
  11024. (if (and (stringp extra)
  11025. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11026. (setq extra (format "-%02d:%02d"
  11027. (string-to-number (match-string 1 extra))
  11028. (string-to-number (match-string 2 extra))))
  11029. (setq extra nil)))
  11030. (when extra
  11031. (backward-char 1)
  11032. (insert-before-markers extra)
  11033. (forward-char 1))
  11034. (insert-before-markers (or post ""))
  11035. (setq org-last-inserted-timestamp stamp)))
  11036. (defun org-toggle-time-stamp-overlays ()
  11037. "Toggle the use of custom time stamp formats."
  11038. (interactive)
  11039. (setq org-display-custom-times (not org-display-custom-times))
  11040. (unless org-display-custom-times
  11041. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11042. (while (setq p (next-single-property-change p 'display))
  11043. (if (and (get-text-property p 'display)
  11044. (eq (get-text-property p 'face) 'org-date))
  11045. (remove-text-properties
  11046. p (setq p (next-single-property-change p 'display))
  11047. '(display t))))
  11048. (set-buffer-modified-p bmp)))
  11049. (if (featurep 'xemacs)
  11050. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11051. (org-restart-font-lock)
  11052. (setq org-table-may-need-update t)
  11053. (if org-display-custom-times
  11054. (message "Time stamps are overlayed with custom format")
  11055. (message "Time stamp overlays removed")))
  11056. (defun org-display-custom-time (beg end)
  11057. "Overlay modified time stamp format over timestamp between BEG and END."
  11058. (let* ((ts (buffer-substring beg end))
  11059. t1 w1 with-hm tf time str w2 (off 0))
  11060. (save-match-data
  11061. (setq t1 (org-parse-time-string ts t))
  11062. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11063. (setq off (- (match-end 0) (match-beginning 0)))))
  11064. (setq end (- end off))
  11065. (setq w1 (- end beg)
  11066. with-hm (and (nth 1 t1) (nth 2 t1))
  11067. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11068. time (org-fix-decoded-time t1)
  11069. str (org-add-props
  11070. (format-time-string
  11071. (substring tf 1 -1) (apply 'encode-time time))
  11072. nil 'mouse-face 'highlight)
  11073. w2 (length str))
  11074. (if (not (= w2 w1))
  11075. (add-text-properties (1+ beg) (+ 2 beg)
  11076. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11077. (if (featurep 'xemacs)
  11078. (progn
  11079. (put-text-property beg end 'invisible t)
  11080. (put-text-property beg end 'end-glyph (make-glyph str)))
  11081. (put-text-property beg end 'display str))))
  11082. (defun org-translate-time (string)
  11083. "Translate all timestamps in STRING to custom format.
  11084. But do this only if the variable `org-display-custom-times' is set."
  11085. (when org-display-custom-times
  11086. (save-match-data
  11087. (let* ((start 0)
  11088. (re org-ts-regexp-both)
  11089. t1 with-hm inactive tf time str beg end)
  11090. (while (setq start (string-match re string start))
  11091. (setq beg (match-beginning 0)
  11092. end (match-end 0)
  11093. t1 (save-match-data
  11094. (org-parse-time-string (substring string beg end) t))
  11095. with-hm (and (nth 1 t1) (nth 2 t1))
  11096. inactive (equal (substring string beg (1+ beg)) "[")
  11097. tf (funcall (if with-hm 'cdr 'car)
  11098. org-time-stamp-custom-formats)
  11099. time (org-fix-decoded-time t1)
  11100. str (format-time-string
  11101. (concat
  11102. (if inactive "[" "<") (substring tf 1 -1)
  11103. (if inactive "]" ">"))
  11104. (apply 'encode-time time))
  11105. string (replace-match str t t string)
  11106. start (+ start (length str)))))))
  11107. string)
  11108. (defun org-fix-decoded-time (time)
  11109. "Set 0 instead of nil for the first 6 elements of time.
  11110. Don't touch the rest."
  11111. (let ((n 0))
  11112. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11113. (defun org-days-to-time (timestamp-string)
  11114. "Difference between TIMESTAMP-STRING and now in days."
  11115. (- (time-to-days (org-time-string-to-time timestamp-string))
  11116. (time-to-days (current-time))))
  11117. (defun org-deadline-close (timestamp-string &optional ndays)
  11118. "Is the time in TIMESTAMP-STRING close to the current date?"
  11119. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11120. (and (< (org-days-to-time timestamp-string) ndays)
  11121. (not (org-entry-is-done-p))))
  11122. (defun org-get-wdays (ts)
  11123. "Get the deadline lead time appropriate for timestring TS."
  11124. (cond
  11125. ((<= org-deadline-warning-days 0)
  11126. ;; 0 or negative, enforce this value no matter what
  11127. (- org-deadline-warning-days))
  11128. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11129. ;; lead time is specified.
  11130. (floor (* (string-to-number (match-string 1 ts))
  11131. (cdr (assoc (match-string 2 ts)
  11132. '(("d" . 1) ("w" . 7)
  11133. ("m" . 30.4) ("y" . 365.25)))))))
  11134. ;; go for the default.
  11135. (t org-deadline-warning-days)))
  11136. (defun org-calendar-select-mouse (ev)
  11137. "Return to `org-read-date' with the date currently selected.
  11138. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11139. (interactive "e")
  11140. (mouse-set-point ev)
  11141. (when (calendar-cursor-to-date)
  11142. (let* ((date (calendar-cursor-to-date))
  11143. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11144. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11145. (if (active-minibuffer-window) (exit-minibuffer))))
  11146. (defun org-check-deadlines (ndays)
  11147. "Check if there are any deadlines due or past due.
  11148. A deadline is considered due if it happens within `org-deadline-warning-days'
  11149. days from today's date. If the deadline appears in an entry marked DONE,
  11150. it is not shown. The prefix arg NDAYS can be used to test that many
  11151. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11152. (interactive "P")
  11153. (let* ((org-warn-days
  11154. (cond
  11155. ((equal ndays '(4)) 100000)
  11156. (ndays (prefix-numeric-value ndays))
  11157. (t (abs org-deadline-warning-days))))
  11158. (case-fold-search nil)
  11159. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11160. (callback
  11161. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11162. (message "%d deadlines past-due or due within %d days"
  11163. (org-occur regexp nil callback)
  11164. org-warn-days)))
  11165. (defun org-check-before-date (date)
  11166. "Check if there are deadlines or scheduled entries before DATE."
  11167. (interactive (list (org-read-date)))
  11168. (let ((case-fold-search nil)
  11169. (regexp (concat "\\<\\(" org-deadline-string
  11170. "\\|" org-scheduled-string
  11171. "\\) *<\\([^>]+\\)>"))
  11172. (callback
  11173. (lambda () (time-less-p
  11174. (org-time-string-to-time (match-string 2))
  11175. (org-time-string-to-time date)))))
  11176. (message "%d entries before %s"
  11177. (org-occur regexp nil callback) date)))
  11178. (defun org-check-after-date (date)
  11179. "Check if there are deadlines or scheduled entries after DATE."
  11180. (interactive (list (org-read-date)))
  11181. (let ((case-fold-search nil)
  11182. (regexp (concat "\\<\\(" org-deadline-string
  11183. "\\|" org-scheduled-string
  11184. "\\) *<\\([^>]+\\)>"))
  11185. (callback
  11186. (lambda () (not
  11187. (time-less-p
  11188. (org-time-string-to-time (match-string 2))
  11189. (org-time-string-to-time date))))))
  11190. (message "%d entries after %s"
  11191. (org-occur regexp nil callback) date)))
  11192. (defun org-evaluate-time-range (&optional to-buffer)
  11193. "Evaluate a time range by computing the difference between start and end.
  11194. Normally the result is just printed in the echo area, but with prefix arg
  11195. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11196. If the time range is actually in a table, the result is inserted into the
  11197. next column.
  11198. For time difference computation, a year is assumed to be exactly 365
  11199. days in order to avoid rounding problems."
  11200. (interactive "P")
  11201. (or
  11202. (org-clock-update-time-maybe)
  11203. (save-excursion
  11204. (unless (org-at-date-range-p t)
  11205. (goto-char (point-at-bol))
  11206. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11207. (if (not (org-at-date-range-p t))
  11208. (error "Not at a time-stamp range, and none found in current line")))
  11209. (let* ((ts1 (match-string 1))
  11210. (ts2 (match-string 2))
  11211. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11212. (match-end (match-end 0))
  11213. (time1 (org-time-string-to-time ts1))
  11214. (time2 (org-time-string-to-time ts2))
  11215. (t1 (time-to-seconds time1))
  11216. (t2 (time-to-seconds time2))
  11217. (diff (abs (- t2 t1)))
  11218. (negative (< (- t2 t1) 0))
  11219. ;; (ys (floor (* 365 24 60 60)))
  11220. (ds (* 24 60 60))
  11221. (hs (* 60 60))
  11222. (fy "%dy %dd %02d:%02d")
  11223. (fy1 "%dy %dd")
  11224. (fd "%dd %02d:%02d")
  11225. (fd1 "%dd")
  11226. (fh "%02d:%02d")
  11227. y d h m align)
  11228. (if havetime
  11229. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11230. y 0
  11231. d (floor (/ diff ds)) diff (mod diff ds)
  11232. h (floor (/ diff hs)) diff (mod diff hs)
  11233. m (floor (/ diff 60)))
  11234. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11235. y 0
  11236. d (floor (+ (/ diff ds) 0.5))
  11237. h 0 m 0))
  11238. (if (not to-buffer)
  11239. (message "%s" (org-make-tdiff-string y d h m))
  11240. (if (org-at-table-p)
  11241. (progn
  11242. (goto-char match-end)
  11243. (setq align t)
  11244. (and (looking-at " *|") (goto-char (match-end 0))))
  11245. (goto-char match-end))
  11246. (if (looking-at
  11247. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11248. (replace-match ""))
  11249. (if negative (insert " -"))
  11250. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11251. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11252. (insert " " (format fh h m))))
  11253. (if align (org-table-align))
  11254. (message "Time difference inserted")))))
  11255. (defun org-make-tdiff-string (y d h m)
  11256. (let ((fmt "")
  11257. (l nil))
  11258. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11259. l (push y l)))
  11260. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11261. l (push d l)))
  11262. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11263. l (push h l)))
  11264. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11265. l (push m l)))
  11266. (apply 'format fmt (nreverse l))))
  11267. (defun org-time-string-to-time (s)
  11268. (apply 'encode-time (org-parse-time-string s)))
  11269. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11270. "Convert a time stamp to an absolute day number.
  11271. If there is a specifyer for a cyclic time stamp, get the closest date to
  11272. DAYNR.
  11273. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  11274. (cond
  11275. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11276. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11277. daynr
  11278. (+ daynr 1000)))
  11279. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11280. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11281. (time-to-days (current-time))) (match-string 0 s)
  11282. prefer show-all))
  11283. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11284. (defun org-days-to-iso-week (days)
  11285. "Return the iso week number."
  11286. (require 'cal-iso)
  11287. (car (calendar-iso-from-absolute days)))
  11288. (defun org-small-year-to-year (year)
  11289. "Convert 2-digit years into 4-digit years.
  11290. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11291. The year 2000 cannot be abbreviated. Any year larger than 99
  11292. is returned unchanged."
  11293. (if (< year 38)
  11294. (setq year (+ 2000 year))
  11295. (if (< year 100)
  11296. (setq year (+ 1900 year))))
  11297. year)
  11298. (defun org-time-from-absolute (d)
  11299. "Return the time corresponding to date D.
  11300. D may be an absolute day number, or a calendar-type list (month day year)."
  11301. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11302. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11303. (defun org-calendar-holiday ()
  11304. "List of holidays, for Diary display in Org-mode."
  11305. (require 'holidays)
  11306. (let ((hl (funcall
  11307. (if (fboundp 'calendar-check-holidays)
  11308. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11309. (if hl (mapconcat 'identity hl "; "))))
  11310. (defun org-diary-sexp-entry (sexp entry date)
  11311. "Process a SEXP diary ENTRY for DATE."
  11312. (require 'diary-lib)
  11313. (let ((result (if calendar-debug-sexp
  11314. (let ((stack-trace-on-error t))
  11315. (eval (car (read-from-string sexp))))
  11316. (condition-case nil
  11317. (eval (car (read-from-string sexp)))
  11318. (error
  11319. (beep)
  11320. (message "Bad sexp at line %d in %s: %s"
  11321. (org-current-line)
  11322. (buffer-file-name) sexp)
  11323. (sleep-for 2))))))
  11324. (cond ((stringp result) result)
  11325. ((and (consp result)
  11326. (stringp (cdr result))) (cdr result))
  11327. (result entry)
  11328. (t nil))))
  11329. (defun org-diary-to-ical-string (frombuf)
  11330. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11331. This uses the icalendar.el library."
  11332. (let* ((tmpdir (if (featurep 'xemacs)
  11333. (temp-directory)
  11334. temporary-file-directory))
  11335. (tmpfile (make-temp-name
  11336. (expand-file-name "orgics" tmpdir)))
  11337. buf rtn b e)
  11338. (save-excursion
  11339. (set-buffer frombuf)
  11340. (icalendar-export-region (point-min) (point-max) tmpfile)
  11341. (setq buf (find-buffer-visiting tmpfile))
  11342. (set-buffer buf)
  11343. (goto-char (point-min))
  11344. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11345. (setq b (match-beginning 0)))
  11346. (goto-char (point-max))
  11347. (if (re-search-backward "^END:VEVENT" nil t)
  11348. (setq e (match-end 0)))
  11349. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11350. (kill-buffer buf)
  11351. (delete-file tmpfile)
  11352. rtn))
  11353. (defun org-closest-date (start current change prefer show-all)
  11354. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11355. When PREFER is `past' return a date that is either CURRENT or past.
  11356. When PREFER is `future', return a date that is either CURRENT or future.
  11357. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11358. ;; Make the proper lists from the dates
  11359. (catch 'exit
  11360. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11361. dn dw sday cday n1 n2 n0
  11362. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11363. (setq start (org-date-to-gregorian start)
  11364. current (org-date-to-gregorian
  11365. (if show-all
  11366. current
  11367. (time-to-days (current-time))))
  11368. sday (calendar-absolute-from-gregorian start)
  11369. cday (calendar-absolute-from-gregorian current))
  11370. (if (<= cday sday) (throw 'exit sday))
  11371. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11372. (setq dn (string-to-number (match-string 1 change))
  11373. dw (cdr (assoc (match-string 2 change) a1)))
  11374. (error "Invalid change specifyer: %s" change))
  11375. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11376. (cond
  11377. ((eq dw 'day)
  11378. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11379. n2 (+ n1 dn)))
  11380. ((eq dw 'year)
  11381. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11382. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11383. (setq date1 (list m d y1)
  11384. n1 (calendar-absolute-from-gregorian date1)
  11385. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11386. n2 (calendar-absolute-from-gregorian date2)))
  11387. ((eq dw 'month)
  11388. ;; approx number of month between the two dates
  11389. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11390. ;; How often does dn fit in there?
  11391. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11392. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11393. m (+ m nm)
  11394. ny (floor (/ m 12))
  11395. y (+ y ny)
  11396. m (- m (* ny 12)))
  11397. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11398. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11399. (setq m2 (+ m dn) y2 y)
  11400. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11401. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11402. (while (<= n2 cday)
  11403. (setq n1 n2 m m2 y y2)
  11404. (setq m2 (+ m dn) y2 y)
  11405. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11406. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11407. ;; Make sure n1 is the earlier date
  11408. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11409. (if show-all
  11410. (cond
  11411. ((eq prefer 'past) n1)
  11412. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11413. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11414. (cond
  11415. ((eq prefer 'past) n1)
  11416. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11417. (t (if (= cday n1) n1 n2)))))))
  11418. (defun org-date-to-gregorian (date)
  11419. "Turn any specification of DATE into a gregorian date for the calendar."
  11420. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11421. ((and (listp date) (= (length date) 3)) date)
  11422. ((stringp date)
  11423. (setq date (org-parse-time-string date))
  11424. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11425. ((listp date)
  11426. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11427. (defun org-parse-time-string (s &optional nodefault)
  11428. "Parse the standard Org-mode time string.
  11429. This should be a lot faster than the normal `parse-time-string'.
  11430. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11431. hour and minute fields will be nil if not given."
  11432. (if (string-match org-ts-regexp0 s)
  11433. (list 0
  11434. (if (or (match-beginning 8) (not nodefault))
  11435. (string-to-number (or (match-string 8 s) "0")))
  11436. (if (or (match-beginning 7) (not nodefault))
  11437. (string-to-number (or (match-string 7 s) "0")))
  11438. (string-to-number (match-string 4 s))
  11439. (string-to-number (match-string 3 s))
  11440. (string-to-number (match-string 2 s))
  11441. nil nil nil)
  11442. (make-list 9 0)))
  11443. (defun org-timestamp-up (&optional arg)
  11444. "Increase the date item at the cursor by one.
  11445. If the cursor is on the year, change the year. If it is on the month or
  11446. the day, change that.
  11447. With prefix ARG, change by that many units."
  11448. (interactive "p")
  11449. (org-timestamp-change (prefix-numeric-value arg)))
  11450. (defun org-timestamp-down (&optional arg)
  11451. "Decrease the date item at the cursor by one.
  11452. If the cursor is on the year, change the year. If it is on the month or
  11453. the day, change that.
  11454. With prefix ARG, change by that many units."
  11455. (interactive "p")
  11456. (org-timestamp-change (- (prefix-numeric-value arg))))
  11457. (defun org-timestamp-up-day (&optional arg)
  11458. "Increase the date in the time stamp by one day.
  11459. With prefix ARG, change that many days."
  11460. (interactive "p")
  11461. (if (and (not (org-at-timestamp-p t))
  11462. (org-on-heading-p))
  11463. (org-todo 'up)
  11464. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11465. (defun org-timestamp-down-day (&optional arg)
  11466. "Decrease the date in the time stamp by one day.
  11467. With prefix ARG, change that many days."
  11468. (interactive "p")
  11469. (if (and (not (org-at-timestamp-p t))
  11470. (org-on-heading-p))
  11471. (org-todo 'down)
  11472. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11473. (defun org-at-timestamp-p (&optional inactive-ok)
  11474. "Determine if the cursor is in or at a timestamp."
  11475. (interactive)
  11476. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11477. (pos (point))
  11478. (ans (or (looking-at tsr)
  11479. (save-excursion
  11480. (skip-chars-backward "^[<\n\r\t")
  11481. (if (> (point) (point-min)) (backward-char 1))
  11482. (and (looking-at tsr)
  11483. (> (- (match-end 0) pos) -1))))))
  11484. (and ans
  11485. (boundp 'org-ts-what)
  11486. (setq org-ts-what
  11487. (cond
  11488. ((= pos (match-beginning 0)) 'bracket)
  11489. ((= pos (1- (match-end 0))) 'bracket)
  11490. ((org-pos-in-match-range pos 2) 'year)
  11491. ((org-pos-in-match-range pos 3) 'month)
  11492. ((org-pos-in-match-range pos 7) 'hour)
  11493. ((org-pos-in-match-range pos 8) 'minute)
  11494. ((or (org-pos-in-match-range pos 4)
  11495. (org-pos-in-match-range pos 5)) 'day)
  11496. ((and (> pos (or (match-end 8) (match-end 5)))
  11497. (< pos (match-end 0)))
  11498. (- pos (or (match-end 8) (match-end 5))))
  11499. (t 'day))))
  11500. ans))
  11501. (defun org-toggle-timestamp-type ()
  11502. "Toggle the type (<active> or [inactive]) of a time stamp."
  11503. (interactive)
  11504. (when (org-at-timestamp-p t)
  11505. (let ((beg (match-beginning 0)) (end (match-end 0))
  11506. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11507. (save-excursion
  11508. (goto-char beg)
  11509. (while (re-search-forward "[][<>]" end t)
  11510. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11511. t t)))
  11512. (message "Timestamp is now %sactive"
  11513. (if (equal (char-after beg) ?<) "" "in")))))
  11514. (defun org-timestamp-change (n &optional what)
  11515. "Change the date in the time stamp at point.
  11516. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11517. `year', `minute', `second'. If WHAT is not given, the cursor position
  11518. in the timestamp determines what will be changed."
  11519. (let ((pos (point))
  11520. with-hm inactive
  11521. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11522. org-ts-what
  11523. extra rem
  11524. ts time time0)
  11525. (if (not (org-at-timestamp-p t))
  11526. (error "Not at a timestamp"))
  11527. (if (and (not what) (eq org-ts-what 'bracket))
  11528. (org-toggle-timestamp-type)
  11529. (if (and (not what) (not (eq org-ts-what 'day))
  11530. org-display-custom-times
  11531. (get-text-property (point) 'display)
  11532. (not (get-text-property (1- (point)) 'display)))
  11533. (setq org-ts-what 'day))
  11534. (setq org-ts-what (or what org-ts-what)
  11535. inactive (= (char-after (match-beginning 0)) ?\[)
  11536. ts (match-string 0))
  11537. (replace-match "")
  11538. (if (string-match
  11539. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11540. ts)
  11541. (setq extra (match-string 1 ts)))
  11542. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11543. (setq with-hm t))
  11544. (setq time0 (org-parse-time-string ts))
  11545. (when (and (eq org-ts-what 'minute)
  11546. (eq current-prefix-arg nil))
  11547. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11548. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11549. (setcar (cdr time0) (+ (nth 1 time0)
  11550. (if (> n 0) (- rem) (- dm rem))))))
  11551. (setq time
  11552. (encode-time (or (car time0) 0)
  11553. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11554. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11555. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11556. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11557. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11558. (nthcdr 6 time0)))
  11559. (when (and (member org-ts-what '(hour minute))
  11560. extra
  11561. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  11562. (setq extra (org-modify-ts-extra
  11563. extra
  11564. (if (eq org-ts-what 'hour) 2 5)
  11565. n dm)))
  11566. (when (integerp org-ts-what)
  11567. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11568. (if (eq what 'calendar)
  11569. (let ((cal-date (org-get-date-from-calendar)))
  11570. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11571. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11572. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11573. (setcar time0 (or (car time0) 0))
  11574. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11575. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11576. (setq time (apply 'encode-time time0))))
  11577. (setq org-last-changed-timestamp
  11578. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11579. (org-clock-update-time-maybe)
  11580. (goto-char pos)
  11581. ;; Try to recenter the calendar window, if any
  11582. (if (and org-calendar-follow-timestamp-change
  11583. (get-buffer-window "*Calendar*" t)
  11584. (memq org-ts-what '(day month year)))
  11585. (org-recenter-calendar (time-to-days time))))))
  11586. (defun org-modify-ts-extra (s pos n dm)
  11587. "Change the different parts of the lead-time and repeat fields in timestamp."
  11588. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11589. ng h m new rem)
  11590. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11591. (cond
  11592. ((or (org-pos-in-match-range pos 2)
  11593. (org-pos-in-match-range pos 3))
  11594. (setq m (string-to-number (match-string 3 s))
  11595. h (string-to-number (match-string 2 s)))
  11596. (if (org-pos-in-match-range pos 2)
  11597. (setq h (+ h n))
  11598. (setq n (* dm (org-no-warnings (signum n))))
  11599. (when (not (= 0 (setq rem (% m dm))))
  11600. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11601. (setq m (+ m n)))
  11602. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11603. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11604. (setq h (min 24 (max 0 h)))
  11605. (setq ng 1 new (format "-%02d:%02d" h m)))
  11606. ((org-pos-in-match-range pos 6)
  11607. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11608. ((org-pos-in-match-range pos 5)
  11609. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11610. ((org-pos-in-match-range pos 9)
  11611. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11612. ((org-pos-in-match-range pos 8)
  11613. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11614. (when ng
  11615. (setq s (concat
  11616. (substring s 0 (match-beginning ng))
  11617. new
  11618. (substring s (match-end ng))))))
  11619. s))
  11620. (defun org-recenter-calendar (date)
  11621. "If the calendar is visible, recenter it to DATE."
  11622. (let* ((win (selected-window))
  11623. (cwin (get-buffer-window "*Calendar*" t))
  11624. (calendar-move-hook nil))
  11625. (when cwin
  11626. (select-window cwin)
  11627. (calendar-goto-date (if (listp date) date
  11628. (calendar-gregorian-from-absolute date)))
  11629. (select-window win))))
  11630. (defun org-goto-calendar (&optional arg)
  11631. "Go to the Emacs calendar at the current date.
  11632. If there is a time stamp in the current line, go to that date.
  11633. A prefix ARG can be used to force the current date."
  11634. (interactive "P")
  11635. (let ((tsr org-ts-regexp) diff
  11636. (calendar-move-hook nil)
  11637. (calendar-view-holidays-initially-flag nil)
  11638. (view-calendar-holidays-initially nil)
  11639. (calendar-view-diary-initially-flag nil)
  11640. (view-diary-entries-initially nil))
  11641. (if (or (org-at-timestamp-p)
  11642. (save-excursion
  11643. (beginning-of-line 1)
  11644. (looking-at (concat ".*" tsr))))
  11645. (let ((d1 (time-to-days (current-time)))
  11646. (d2 (time-to-days
  11647. (org-time-string-to-time (match-string 1)))))
  11648. (setq diff (- d2 d1))))
  11649. (calendar)
  11650. (calendar-goto-today)
  11651. (if (and diff (not arg)) (calendar-forward-day diff))))
  11652. (defun org-get-date-from-calendar ()
  11653. "Return a list (month day year) of date at point in calendar."
  11654. (with-current-buffer "*Calendar*"
  11655. (save-match-data
  11656. (calendar-cursor-to-date))))
  11657. (defun org-date-from-calendar ()
  11658. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11659. If there is already a time stamp at the cursor position, update it."
  11660. (interactive)
  11661. (if (org-at-timestamp-p t)
  11662. (org-timestamp-change 0 'calendar)
  11663. (let ((cal-date (org-get-date-from-calendar)))
  11664. (org-insert-time-stamp
  11665. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11666. (defun org-minutes-to-hh:mm-string (m)
  11667. "Compute H:MM from a number of minutes."
  11668. (let ((h (/ m 60)))
  11669. (setq m (- m (* 60 h)))
  11670. (format org-time-clocksum-format h m)))
  11671. (defun org-hh:mm-string-to-minutes (s)
  11672. "Convert a string H:MM to a number of minutes."
  11673. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11674. (+ (* (string-to-number (match-string 1 s)) 60)
  11675. (string-to-number (match-string 2 s)))
  11676. 0))
  11677. ;;;; Files
  11678. (defun org-save-all-org-buffers ()
  11679. "Save all Org-mode buffers without user confirmation."
  11680. (interactive)
  11681. (message "Saving all Org-mode buffers...")
  11682. (save-some-buffers t 'org-mode-p)
  11683. (when (featurep 'org-id) (org-id-locations-save))
  11684. (message "Saving all Org-mode buffers... done"))
  11685. (defun org-revert-all-org-buffers ()
  11686. "Revert all Org-mode buffers.
  11687. Prompt for confirmation when there are unsaved changes.
  11688. Be sure you know what you are doing before letting this function
  11689. overwrite your changes.
  11690. This function is useful in a setup where one tracks org files
  11691. with a version control system, to revert on one machine after pulling
  11692. changes from another. I believe the procedure must be like this:
  11693. 1. M-x org-save-all-org-buffers
  11694. 2. Pull changes from the other machine, resolve conflicts
  11695. 3. M-x org-revert-all-org-buffers"
  11696. (interactive)
  11697. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11698. (error "Abort"))
  11699. (save-excursion
  11700. (save-window-excursion
  11701. (mapc
  11702. (lambda (b)
  11703. (when (and (with-current-buffer b (org-mode-p))
  11704. (with-current-buffer b buffer-file-name))
  11705. (switch-to-buffer b)
  11706. (revert-buffer t 'no-confirm)))
  11707. (buffer-list))
  11708. (when (and (featurep 'org-id) org-id-track-globally)
  11709. (org-id-locations-load)))))
  11710. ;;;; Agenda files
  11711. ;;;###autoload
  11712. (defun org-iswitchb (&optional arg)
  11713. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11714. With a prefix argument, restrict available to files.
  11715. With two prefix arguments, restrict available buffers to agenda files.
  11716. Due to some yet unresolved reason, the global function
  11717. `iswitchb-mode' needs to be active for this function to work."
  11718. (interactive "P")
  11719. (require 'iswitchb)
  11720. (let ((enabled iswitchb-mode) blist)
  11721. (or enabled (iswitchb-mode 1))
  11722. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11723. ((equal arg '(16)) (org-buffer-list 'agenda))
  11724. (t (org-buffer-list))))
  11725. (unwind-protect
  11726. (let ((iswitchb-make-buflist-hook
  11727. (lambda ()
  11728. (setq iswitchb-temp-buflist
  11729. (mapcar 'buffer-name blist)))))
  11730. (switch-to-buffer
  11731. (iswitchb-read-buffer
  11732. "Switch-to: " nil t))
  11733. (or enabled (iswitchb-mode -1))))))
  11734. ;;;###autoload
  11735. (defun org-ido-switchb (&optional arg)
  11736. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11737. With a prefix argument, restrict available to files.
  11738. With two prefix arguments, restrict available buffers to agenda files."
  11739. (interactive "P")
  11740. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11741. ((equal arg '(16)) (org-buffer-list 'agenda))
  11742. (t (org-buffer-list)))))
  11743. (switch-to-buffer
  11744. (org-ido-completing-read "Org buffer: "
  11745. (mapcar 'list (mapcar 'buffer-name blist))
  11746. nil t))))
  11747. (defun org-buffer-list (&optional predicate exclude-tmp)
  11748. "Return a list of Org buffers.
  11749. PREDICATE can be `export', `files' or `agenda'.
  11750. export restrict the list to Export buffers.
  11751. files restrict the list to buffers visiting Org files.
  11752. agenda restrict the list to buffers visiting agenda files.
  11753. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11754. (let* ((bfn nil)
  11755. (agenda-files (and (eq predicate 'agenda)
  11756. (mapcar 'file-truename (org-agenda-files t))))
  11757. (filter
  11758. (cond
  11759. ((eq predicate 'files)
  11760. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11761. ((eq predicate 'export)
  11762. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11763. ((eq predicate 'agenda)
  11764. (lambda (b)
  11765. (with-current-buffer b
  11766. (and (eq major-mode 'org-mode)
  11767. (setq bfn (buffer-file-name b))
  11768. (member (file-truename bfn) agenda-files)))))
  11769. (t (lambda (b) (with-current-buffer b
  11770. (or (eq major-mode 'org-mode)
  11771. (string-match "\*Org .*Export"
  11772. (buffer-name b)))))))))
  11773. (delq nil
  11774. (mapcar
  11775. (lambda(b)
  11776. (if (and (funcall filter b)
  11777. (or (not exclude-tmp)
  11778. (not (string-match "tmp" (buffer-name b)))))
  11779. b
  11780. nil))
  11781. (buffer-list)))))
  11782. (defun org-agenda-files (&optional unrestricted archives)
  11783. "Get the list of agenda files.
  11784. Optional UNRESTRICTED means return the full list even if a restriction
  11785. is currently in place.
  11786. When ARCHIVES is t, include all archive files hat are really being
  11787. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11788. `org-agenda-archives-mode' is t."
  11789. (let ((files
  11790. (cond
  11791. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11792. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11793. ((listp org-agenda-files) org-agenda-files)
  11794. (t (error "Invalid value of `org-agenda-files'")))))
  11795. (setq files (apply 'append
  11796. (mapcar (lambda (f)
  11797. (if (file-directory-p f)
  11798. (directory-files
  11799. f t org-agenda-file-regexp)
  11800. (list f)))
  11801. files)))
  11802. (when org-agenda-skip-unavailable-files
  11803. (setq files (delq nil
  11804. (mapcar (function
  11805. (lambda (file)
  11806. (and (file-readable-p file) file)))
  11807. files))))
  11808. (when (or (eq archives t)
  11809. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11810. (setq files (org-add-archive-files files)))
  11811. files))
  11812. (defun org-edit-agenda-file-list ()
  11813. "Edit the list of agenda files.
  11814. Depending on setup, this either uses customize to edit the variable
  11815. `org-agenda-files', or it visits the file that is holding the list. In the
  11816. latter case, the buffer is set up in a way that saving it automatically kills
  11817. the buffer and restores the previous window configuration."
  11818. (interactive)
  11819. (if (stringp org-agenda-files)
  11820. (let ((cw (current-window-configuration)))
  11821. (find-file org-agenda-files)
  11822. (org-set-local 'org-window-configuration cw)
  11823. (org-add-hook 'after-save-hook
  11824. (lambda ()
  11825. (set-window-configuration
  11826. (prog1 org-window-configuration
  11827. (kill-buffer (current-buffer))))
  11828. (org-install-agenda-files-menu)
  11829. (message "New agenda file list installed"))
  11830. nil 'local)
  11831. (message "%s" (substitute-command-keys
  11832. "Edit list and finish with \\[save-buffer]")))
  11833. (customize-variable 'org-agenda-files)))
  11834. (defun org-store-new-agenda-file-list (list)
  11835. "Set new value for the agenda file list and save it correctly."
  11836. (if (stringp org-agenda-files)
  11837. (let ((f org-agenda-files) b)
  11838. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11839. (with-temp-file f
  11840. (insert (mapconcat 'identity list "\n") "\n")))
  11841. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11842. (setq org-agenda-files list)
  11843. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11844. (defun org-read-agenda-file-list ()
  11845. "Read the list of agenda files from a file."
  11846. (when (file-directory-p org-agenda-files)
  11847. (error "`org-agenda-files' cannot be a single directory"))
  11848. (when (stringp org-agenda-files)
  11849. (with-temp-buffer
  11850. (insert-file-contents org-agenda-files)
  11851. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11852. ;;;###autoload
  11853. (defun org-cycle-agenda-files ()
  11854. "Cycle through the files in `org-agenda-files'.
  11855. If the current buffer visits an agenda file, find the next one in the list.
  11856. If the current buffer does not, find the first agenda file."
  11857. (interactive)
  11858. (let* ((fs (org-agenda-files t))
  11859. (files (append fs (list (car fs))))
  11860. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11861. file)
  11862. (unless files (error "No agenda files"))
  11863. (catch 'exit
  11864. (while (setq file (pop files))
  11865. (if (equal (file-truename file) tcf)
  11866. (when (car files)
  11867. (find-file (car files))
  11868. (throw 'exit t))))
  11869. (find-file (car fs)))
  11870. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11871. (defun org-agenda-file-to-front (&optional to-end)
  11872. "Move/add the current file to the top of the agenda file list.
  11873. If the file is not present in the list, it is added to the front. If it is
  11874. present, it is moved there. With optional argument TO-END, add/move to the
  11875. end of the list."
  11876. (interactive "P")
  11877. (let ((org-agenda-skip-unavailable-files nil)
  11878. (file-alist (mapcar (lambda (x)
  11879. (cons (file-truename x) x))
  11880. (org-agenda-files t)))
  11881. (ctf (file-truename buffer-file-name))
  11882. x had)
  11883. (setq x (assoc ctf file-alist) had x)
  11884. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11885. (if to-end
  11886. (setq file-alist (append (delq x file-alist) (list x)))
  11887. (setq file-alist (cons x (delq x file-alist))))
  11888. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11889. (org-install-agenda-files-menu)
  11890. (message "File %s to %s of agenda file list"
  11891. (if had "moved" "added") (if to-end "end" "front"))))
  11892. (defun org-remove-file (&optional file)
  11893. "Remove current file from the list of files in variable `org-agenda-files'.
  11894. These are the files which are being checked for agenda entries.
  11895. Optional argument FILE means, use this file instead of the current."
  11896. (interactive)
  11897. (let* ((org-agenda-skip-unavailable-files nil)
  11898. (file (or file buffer-file-name))
  11899. (true-file (file-truename file))
  11900. (afile (abbreviate-file-name file))
  11901. (files (delq nil (mapcar
  11902. (lambda (x)
  11903. (if (equal true-file
  11904. (file-truename x))
  11905. nil x))
  11906. (org-agenda-files t)))))
  11907. (if (not (= (length files) (length (org-agenda-files t))))
  11908. (progn
  11909. (org-store-new-agenda-file-list files)
  11910. (org-install-agenda-files-menu)
  11911. (message "Removed file: %s" afile))
  11912. (message "File was not in list: %s (not removed)" afile))))
  11913. (defun org-file-menu-entry (file)
  11914. (vector file (list 'find-file file) t))
  11915. (defun org-check-agenda-file (file)
  11916. "Make sure FILE exists. If not, ask user what to do."
  11917. (when (not (file-exists-p file))
  11918. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11919. (abbreviate-file-name file))
  11920. (let ((r (downcase (read-char-exclusive))))
  11921. (cond
  11922. ((equal r ?r)
  11923. (org-remove-file file)
  11924. (throw 'nextfile t))
  11925. (t (error "Abort"))))))
  11926. (defun org-get-agenda-file-buffer (file)
  11927. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11928. it to the list of buffers which might be released later."
  11929. (let ((buf (org-find-base-buffer-visiting file)))
  11930. (if buf
  11931. buf ; just return it
  11932. ;; Make a new buffer and remember it
  11933. (setq buf (find-file-noselect file))
  11934. (if buf (push buf org-agenda-new-buffers))
  11935. buf)))
  11936. (defun org-release-buffers (blist)
  11937. "Release all buffers in list, asking the user for confirmation when needed.
  11938. When a buffer is unmodified, it is just killed. When modified, it is saved
  11939. \(if the user agrees) and then killed."
  11940. (let (buf file)
  11941. (while (setq buf (pop blist))
  11942. (setq file (buffer-file-name buf))
  11943. (when (and (buffer-modified-p buf)
  11944. file
  11945. (y-or-n-p (format "Save file %s? " file)))
  11946. (with-current-buffer buf (save-buffer)))
  11947. (kill-buffer buf))))
  11948. (defun org-prepare-agenda-buffers (files)
  11949. "Create buffers for all agenda files, protect archived trees and comments."
  11950. (interactive)
  11951. (let ((pa '(:org-archived t))
  11952. (pc '(:org-comment t))
  11953. (pall '(:org-archived t :org-comment t))
  11954. (inhibit-read-only t)
  11955. (rea (concat ":" org-archive-tag ":"))
  11956. bmp file re)
  11957. (save-excursion
  11958. (save-restriction
  11959. (while (setq file (pop files))
  11960. (if (bufferp file)
  11961. (set-buffer file)
  11962. (org-check-agenda-file file)
  11963. (set-buffer (org-get-agenda-file-buffer file)))
  11964. (widen)
  11965. (setq bmp (buffer-modified-p))
  11966. (org-refresh-category-properties)
  11967. (setq org-todo-keywords-for-agenda
  11968. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11969. (setq org-done-keywords-for-agenda
  11970. (append org-done-keywords-for-agenda org-done-keywords))
  11971. (setq org-todo-keyword-alist-for-agenda
  11972. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11973. (setq org-tag-alist-for-agenda
  11974. (append org-tag-alist-for-agenda org-tag-alist))
  11975. (save-excursion
  11976. (remove-text-properties (point-min) (point-max) pall)
  11977. (when org-agenda-skip-archived-trees
  11978. (goto-char (point-min))
  11979. (while (re-search-forward rea nil t)
  11980. (if (org-on-heading-p t)
  11981. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11982. (goto-char (point-min))
  11983. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11984. (while (re-search-forward re nil t)
  11985. (add-text-properties
  11986. (match-beginning 0) (org-end-of-subtree t) pc)))
  11987. (set-buffer-modified-p bmp))))
  11988. (setq org-todo-keyword-alist-for-agenda
  11989. (org-uniquify org-todo-keyword-alist-for-agenda)
  11990. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11991. ;;;; Embedded LaTeX
  11992. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11993. "Keymap for the minor `org-cdlatex-mode'.")
  11994. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11995. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11996. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11997. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11998. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11999. (defvar org-cdlatex-texmathp-advice-is-done nil
  12000. "Flag remembering if we have applied the advice to texmathp already.")
  12001. (define-minor-mode org-cdlatex-mode
  12002. "Toggle the minor `org-cdlatex-mode'.
  12003. This mode supports entering LaTeX environment and math in LaTeX fragments
  12004. in Org-mode.
  12005. \\{org-cdlatex-mode-map}"
  12006. nil " OCDL" nil
  12007. (when org-cdlatex-mode (require 'cdlatex))
  12008. (unless org-cdlatex-texmathp-advice-is-done
  12009. (setq org-cdlatex-texmathp-advice-is-done t)
  12010. (defadvice texmathp (around org-math-always-on activate)
  12011. "Always return t in org-mode buffers.
  12012. This is because we want to insert math symbols without dollars even outside
  12013. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12014. an embedded LaTeX fragment, let texmathp do its job.
  12015. \\[org-cdlatex-mode-map]"
  12016. (interactive)
  12017. (let (p)
  12018. (cond
  12019. ((not (org-mode-p)) ad-do-it)
  12020. ((eq this-command 'cdlatex-math-symbol)
  12021. (setq ad-return-value t
  12022. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12023. (t
  12024. (let ((p (org-inside-LaTeX-fragment-p)))
  12025. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12026. (setq ad-return-value t
  12027. texmathp-why '("Org-mode embedded math" . 0))
  12028. (if p ad-do-it)))))))))
  12029. (defun turn-on-org-cdlatex ()
  12030. "Unconditionally turn on `org-cdlatex-mode'."
  12031. (org-cdlatex-mode 1))
  12032. (defun org-inside-LaTeX-fragment-p ()
  12033. "Test if point is inside a LaTeX fragment.
  12034. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12035. sequence appearing also before point.
  12036. Even though the matchers for math are configurable, this function assumes
  12037. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12038. delimiters are skipped when they have been removed by customization.
  12039. The return value is nil, or a cons cell with the delimiter and
  12040. and the position of this delimiter.
  12041. This function does a reasonably good job, but can locally be fooled by
  12042. for example currency specifications. For example it will assume being in
  12043. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12044. fragments that are properly closed, but during editing, we have to live
  12045. with the uncertainty caused by missing closing delimiters. This function
  12046. looks only before point, not after."
  12047. (catch 'exit
  12048. (let ((pos (point))
  12049. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12050. (lim (progn
  12051. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12052. (point)))
  12053. dd-on str (start 0) m re)
  12054. (goto-char pos)
  12055. (when dodollar
  12056. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12057. re (nth 1 (assoc "$" org-latex-regexps)))
  12058. (while (string-match re str start)
  12059. (cond
  12060. ((= (match-end 0) (length str))
  12061. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12062. ((= (match-end 0) (- (length str) 5))
  12063. (throw 'exit nil))
  12064. (t (setq start (match-end 0))))))
  12065. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12066. (goto-char pos)
  12067. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12068. (and (match-beginning 2) (throw 'exit nil))
  12069. ;; count $$
  12070. (while (re-search-backward "\\$\\$" lim t)
  12071. (setq dd-on (not dd-on)))
  12072. (goto-char pos)
  12073. (if dd-on (cons "$$" m))))))
  12074. (defun org-try-cdlatex-tab ()
  12075. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12076. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12077. - inside a LaTeX fragment, or
  12078. - after the first word in a line, where an abbreviation expansion could
  12079. insert a LaTeX environment."
  12080. (when org-cdlatex-mode
  12081. (cond
  12082. ((save-excursion
  12083. (skip-chars-backward "a-zA-Z0-9*")
  12084. (skip-chars-backward " \t")
  12085. (bolp))
  12086. (cdlatex-tab) t)
  12087. ((org-inside-LaTeX-fragment-p)
  12088. (cdlatex-tab) t)
  12089. (t nil))))
  12090. (defun org-cdlatex-underscore-caret (&optional arg)
  12091. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12092. Revert to the normal definition outside of these fragments."
  12093. (interactive "P")
  12094. (if (org-inside-LaTeX-fragment-p)
  12095. (call-interactively 'cdlatex-sub-superscript)
  12096. (let (org-cdlatex-mode)
  12097. (call-interactively (key-binding (vector last-input-event))))))
  12098. (defun org-cdlatex-math-modify (&optional arg)
  12099. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12100. Revert to the normal definition outside of these fragments."
  12101. (interactive "P")
  12102. (if (org-inside-LaTeX-fragment-p)
  12103. (call-interactively 'cdlatex-math-modify)
  12104. (let (org-cdlatex-mode)
  12105. (call-interactively (key-binding (vector last-input-event))))))
  12106. (defvar org-latex-fragment-image-overlays nil
  12107. "List of overlays carrying the images of latex fragments.")
  12108. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12109. (defun org-remove-latex-fragment-image-overlays ()
  12110. "Remove all overlays with LaTeX fragment images in current buffer."
  12111. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12112. (setq org-latex-fragment-image-overlays nil))
  12113. (defun org-preview-latex-fragment (&optional subtree)
  12114. "Preview the LaTeX fragment at point, or all locally or globally.
  12115. If the cursor is in a LaTeX fragment, create the image and overlay
  12116. it over the source code. If there is no fragment at point, display
  12117. all fragments in the current text, from one headline to the next. With
  12118. prefix SUBTREE, display all fragments in the current subtree. With a
  12119. double prefix `C-u C-u', or when the cursor is before the first headline,
  12120. display all fragments in the buffer.
  12121. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12122. (interactive "P")
  12123. (org-remove-latex-fragment-image-overlays)
  12124. (save-excursion
  12125. (save-restriction
  12126. (let (beg end at msg)
  12127. (cond
  12128. ((or (equal subtree '(16))
  12129. (not (save-excursion
  12130. (re-search-backward (concat "^" outline-regexp) nil t))))
  12131. (setq beg (point-min) end (point-max)
  12132. msg "Creating images for buffer...%s"))
  12133. ((equal subtree '(4))
  12134. (org-back-to-heading)
  12135. (setq beg (point) end (org-end-of-subtree t)
  12136. msg "Creating images for subtree...%s"))
  12137. (t
  12138. (if (setq at (org-inside-LaTeX-fragment-p))
  12139. (goto-char (max (point-min) (- (cdr at) 2)))
  12140. (org-back-to-heading))
  12141. (setq beg (point) end (progn (outline-next-heading) (point))
  12142. msg (if at "Creating image...%s"
  12143. "Creating images for entry...%s"))))
  12144. (message msg "")
  12145. (narrow-to-region beg end)
  12146. (goto-char beg)
  12147. (org-format-latex
  12148. (concat "ltxpng/" (file-name-sans-extension
  12149. (file-name-nondirectory
  12150. buffer-file-name)))
  12151. default-directory 'overlays msg at 'forbuffer)
  12152. (message msg "done. Use `C-c C-c' to remove images.")))))
  12153. (defvar org-latex-regexps
  12154. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12155. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12156. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12157. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12158. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12159. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12160. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  12161. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  12162. "Regular expressions for matching embedded LaTeX.")
  12163. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12164. "Replace LaTeX fragments with links to an image, and produce images."
  12165. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12166. (let* ((prefixnodir (file-name-nondirectory prefix))
  12167. (absprefix (expand-file-name prefix dir))
  12168. (todir (file-name-directory absprefix))
  12169. (opt org-format-latex-options)
  12170. (matchers (plist-get opt :matchers))
  12171. (re-list org-latex-regexps)
  12172. (cnt 0) txt link beg end re e checkdir
  12173. m n block linkfile movefile ov)
  12174. ;; Check if there are old images files with this prefix, and remove them
  12175. (when (file-directory-p todir)
  12176. (mapc 'delete-file
  12177. (directory-files
  12178. todir 'full
  12179. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12180. ;; Check the different regular expressions
  12181. (while (setq e (pop re-list))
  12182. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12183. block (if (nth 3 e) "\n\n" ""))
  12184. (when (member m matchers)
  12185. (goto-char (point-min))
  12186. (while (re-search-forward re nil t)
  12187. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12188. (not (get-text-property (match-beginning n)
  12189. 'org-protected)))
  12190. (setq txt (match-string n)
  12191. beg (match-beginning n) end (match-end n)
  12192. cnt (1+ cnt)
  12193. linkfile (format "%s_%04d.png" prefix cnt)
  12194. movefile (format "%s_%04d.png" absprefix cnt)
  12195. link (concat block "[[file:" linkfile "]]" block))
  12196. (if msg (message msg cnt))
  12197. (goto-char beg)
  12198. (unless checkdir ; make sure the directory exists
  12199. (setq checkdir t)
  12200. (or (file-directory-p todir) (make-directory todir)))
  12201. (org-create-formula-image
  12202. txt movefile opt forbuffer)
  12203. (if overlays
  12204. (progn
  12205. (setq ov (org-make-overlay beg end))
  12206. (if (featurep 'xemacs)
  12207. (progn
  12208. (org-overlay-put ov 'invisible t)
  12209. (org-overlay-put
  12210. ov 'end-glyph
  12211. (make-glyph (vector 'png :file movefile))))
  12212. (org-overlay-put
  12213. ov 'display
  12214. (list 'image :type 'png :file movefile :ascent 'center)))
  12215. (push ov org-latex-fragment-image-overlays)
  12216. (goto-char end))
  12217. (delete-region beg end)
  12218. (insert link))))))))
  12219. ;; This function borrows from Ganesh Swami's latex2png.el
  12220. (defun org-create-formula-image (string tofile options buffer)
  12221. (let* ((tmpdir (if (featurep 'xemacs)
  12222. (temp-directory)
  12223. temporary-file-directory))
  12224. (texfilebase (make-temp-name
  12225. (expand-file-name "orgtex" tmpdir)))
  12226. (texfile (concat texfilebase ".tex"))
  12227. (dvifile (concat texfilebase ".dvi"))
  12228. (pngfile (concat texfilebase ".png"))
  12229. (fnh (if (featurep 'xemacs)
  12230. (font-height (get-face-font 'default))
  12231. (face-attribute 'default :height nil)))
  12232. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12233. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12234. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12235. "Black"))
  12236. (bg (or (plist-get options (if buffer :background :html-background))
  12237. "Transparent")))
  12238. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12239. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12240. (with-temp-file texfile
  12241. (insert org-format-latex-header
  12242. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12243. (let ((dir default-directory))
  12244. (condition-case nil
  12245. (progn
  12246. (cd tmpdir)
  12247. (call-process "latex" nil nil nil texfile))
  12248. (error nil))
  12249. (cd dir))
  12250. (if (not (file-exists-p dvifile))
  12251. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12252. (condition-case nil
  12253. (call-process "dvipng" nil nil nil
  12254. "-E" "-fg" fg "-bg" bg
  12255. "-D" dpi
  12256. ;;"-x" scale "-y" scale
  12257. "-T" "tight"
  12258. "-o" pngfile
  12259. dvifile)
  12260. (error nil))
  12261. (if (not (file-exists-p pngfile))
  12262. (progn (message "Failed to create png file from %s" texfile) nil)
  12263. ;; Use the requested file name and clean up
  12264. (copy-file pngfile tofile 'replace)
  12265. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12266. (delete-file (concat texfilebase e)))
  12267. pngfile))))
  12268. (defun org-dvipng-color (attr)
  12269. "Return an rgb color specification for dvipng."
  12270. (apply 'format "rgb %s %s %s"
  12271. (mapcar 'org-normalize-color
  12272. (color-values (face-attribute 'default attr nil)))))
  12273. (defun org-normalize-color (value)
  12274. "Return string to be used as color value for an RGB component."
  12275. (format "%g" (/ value 65535.0)))
  12276. ;;;; Key bindings
  12277. ;; Make `C-c C-x' a prefix key
  12278. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12279. ;; TAB key with modifiers
  12280. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12281. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12282. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12283. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12284. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12285. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12286. ;; The following line is necessary under Suse GNU/Linux
  12287. (unless (featurep 'xemacs)
  12288. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12289. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12290. (define-key org-mode-map [backtab] 'org-shifttab)
  12291. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12292. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12293. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12294. ;; Cursor keys with modifiers
  12295. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12296. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12297. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12298. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12299. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12300. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12301. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12302. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12303. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12304. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12305. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12306. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12307. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12308. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12309. ;;; Extra keys for tty access.
  12310. ;; We only set them when really needed because otherwise the
  12311. ;; menus don't show the simple keys
  12312. (when (or org-use-extra-keys
  12313. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12314. (not window-system))
  12315. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12316. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12317. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12318. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12319. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12320. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12321. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12322. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12323. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12324. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12325. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12326. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12327. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12328. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12329. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12330. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12331. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12332. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12333. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12334. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12335. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12336. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12337. ;; All the other keys
  12338. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12339. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12340. (if (boundp 'narrow-map)
  12341. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12342. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12343. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12344. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12345. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12346. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12347. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12348. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12349. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12350. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12351. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12352. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12353. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12354. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12355. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12356. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12357. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12358. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  12359. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12360. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12361. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  12362. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12363. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12364. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12365. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12366. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12367. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12368. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12369. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12370. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12371. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12372. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12373. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12374. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12375. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12376. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12377. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12378. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12379. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12380. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12381. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12382. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12383. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12384. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12385. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12386. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12387. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12388. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12389. (org-defkey org-mode-map "\C-m" 'org-return)
  12390. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12391. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12392. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12393. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12394. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12395. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12396. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12397. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12398. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12399. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12400. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12401. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12402. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12403. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12404. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12405. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12406. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12407. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12408. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12409. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12410. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12411. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12412. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12413. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12414. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12415. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12416. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12417. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12418. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12419. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12420. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12421. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12422. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12423. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12424. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12425. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12426. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12427. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12428. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12429. (define-key org-mode-map "\C-c\C-xr" 'org-reload)
  12430. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  12431. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  12432. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  12433. (when (featurep 'xemacs)
  12434. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12435. (defvar org-self-insert-command-undo-counter 0)
  12436. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12437. (defun org-self-insert-command (N)
  12438. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12439. If the cursor is in a table looking at whitespace, the whitespace is
  12440. overwritten, and the table is not marked as requiring realignment."
  12441. (interactive "p")
  12442. (if (and
  12443. (org-table-p)
  12444. (progn
  12445. ;; check if we blank the field, and if that triggers align
  12446. (and (featurep 'org-table) org-table-auto-blank-field
  12447. (member last-command
  12448. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12449. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12450. ;; got extra space, this field does not determine column width
  12451. (let (org-table-may-need-update) (org-table-blank-field))
  12452. ;; no extra space, this field may determine column width
  12453. (org-table-blank-field)))
  12454. t)
  12455. (eq N 1)
  12456. (looking-at "[^|\n]* |"))
  12457. (let (org-table-may-need-update)
  12458. (goto-char (1- (match-end 0)))
  12459. (delete-backward-char 1)
  12460. (goto-char (match-beginning 0))
  12461. (self-insert-command N))
  12462. (setq org-table-may-need-update t)
  12463. (self-insert-command N)
  12464. (org-fix-tags-on-the-fly)
  12465. (if org-self-insert-cluster-for-undo
  12466. (if (not (eq last-command 'org-self-insert-command))
  12467. (setq org-self-insert-command-undo-counter 1)
  12468. (if (>= org-self-insert-command-undo-counter 20)
  12469. (setq org-self-insert-command-undo-counter 1)
  12470. (and (> org-self-insert-command-undo-counter 0)
  12471. buffer-undo-list
  12472. (not (cadr buffer-undo-list)) ; remove nil entry
  12473. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  12474. (setq org-self-insert-command-undo-counter
  12475. (1+ org-self-insert-command-undo-counter)))))))
  12476. (defun org-fix-tags-on-the-fly ()
  12477. (when (and (equal (char-after (point-at-bol)) ?*)
  12478. (org-on-heading-p))
  12479. (org-align-tags-here org-tags-column)))
  12480. (defun org-delete-backward-char (N)
  12481. "Like `delete-backward-char', insert whitespace at field end in tables.
  12482. When deleting backwards, in tables this function will insert whitespace in
  12483. front of the next \"|\" separator, to keep the table aligned. The table will
  12484. still be marked for re-alignment if the field did fill the entire column,
  12485. because, in this case the deletion might narrow the column."
  12486. (interactive "p")
  12487. (if (and (org-table-p)
  12488. (eq N 1)
  12489. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12490. (looking-at ".*?|"))
  12491. (let ((pos (point))
  12492. (noalign (looking-at "[^|\n\r]* |"))
  12493. (c org-table-may-need-update))
  12494. (backward-delete-char N)
  12495. (skip-chars-forward "^|")
  12496. (insert " ")
  12497. (goto-char (1- pos))
  12498. ;; noalign: if there were two spaces at the end, this field
  12499. ;; does not determine the width of the column.
  12500. (if noalign (setq org-table-may-need-update c)))
  12501. (backward-delete-char N)
  12502. (org-fix-tags-on-the-fly)))
  12503. (defun org-delete-char (N)
  12504. "Like `delete-char', but insert whitespace at field end in tables.
  12505. When deleting characters, in tables this function will insert whitespace in
  12506. front of the next \"|\" separator, to keep the table aligned. The table will
  12507. still be marked for re-alignment if the field did fill the entire column,
  12508. because, in this case the deletion might narrow the column."
  12509. (interactive "p")
  12510. (if (and (org-table-p)
  12511. (not (bolp))
  12512. (not (= (char-after) ?|))
  12513. (eq N 1))
  12514. (if (looking-at ".*?|")
  12515. (let ((pos (point))
  12516. (noalign (looking-at "[^|\n\r]* |"))
  12517. (c org-table-may-need-update))
  12518. (replace-match (concat
  12519. (substring (match-string 0) 1 -1)
  12520. " |"))
  12521. (goto-char pos)
  12522. ;; noalign: if there were two spaces at the end, this field
  12523. ;; does not determine the width of the column.
  12524. (if noalign (setq org-table-may-need-update c)))
  12525. (delete-char N))
  12526. (delete-char N)
  12527. (org-fix-tags-on-the-fly)))
  12528. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12529. (put 'org-self-insert-command 'delete-selection t)
  12530. (put 'orgtbl-self-insert-command 'delete-selection t)
  12531. (put 'org-delete-char 'delete-selection 'supersede)
  12532. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12533. (put 'org-yank 'delete-selection 'yank)
  12534. ;; Make `flyspell-mode' delay after some commands
  12535. (put 'org-self-insert-command 'flyspell-delayed t)
  12536. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12537. (put 'org-delete-char 'flyspell-delayed t)
  12538. (put 'org-delete-backward-char 'flyspell-delayed t)
  12539. ;; Make pabbrev-mode expand after org-mode commands
  12540. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12541. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12542. ;; How to do this: Measure non-white length of current string
  12543. ;; If equal to column width, we should realign.
  12544. (defun org-remap (map &rest commands)
  12545. "In MAP, remap the functions given in COMMANDS.
  12546. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12547. (let (new old)
  12548. (while commands
  12549. (setq old (pop commands) new (pop commands))
  12550. (if (fboundp 'command-remapping)
  12551. (org-defkey map (vector 'remap old) new)
  12552. (substitute-key-definition old new map global-map)))))
  12553. (when (eq org-enable-table-editor 'optimized)
  12554. ;; If the user wants maximum table support, we need to hijack
  12555. ;; some standard editing functions
  12556. (org-remap org-mode-map
  12557. 'self-insert-command 'org-self-insert-command
  12558. 'delete-char 'org-delete-char
  12559. 'delete-backward-char 'org-delete-backward-char)
  12560. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12561. (defvar org-ctrl-c-ctrl-c-hook nil
  12562. "Hook for functions attaching themselves to `C-c C-c'.
  12563. This can be used to add additional functionality to the C-c C-c key which
  12564. executes context-dependent commands.
  12565. Each function will be called with no arguments. The function must check
  12566. if the context is appropriate for it to act. If yes, it should do its
  12567. thing and then return a non-nil value. If the context is wrong,
  12568. just do nothing.")
  12569. (defvar org-metaleft-hook nil
  12570. "Hook for functions attaching themselves to `M-left'.
  12571. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12572. (defvar org-metaright-hook nil
  12573. "Hook for functions attaching themselves to `M-right'.
  12574. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12575. (defvar org-metaup-hook nil
  12576. "Hook for functions attaching themselves to `M-up'.
  12577. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12578. (defvar org-metadown-hook nil
  12579. "Hook for functions attaching themselves to `M-down'.
  12580. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12581. (defvar org-shiftmetaleft-hook nil
  12582. "Hook for functions attaching themselves to `M-S-left'.
  12583. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12584. (defvar org-shiftmetaright-hook nil
  12585. "Hook for functions attaching themselves to `M-S-right'.
  12586. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12587. (defvar org-shiftmetaup-hook nil
  12588. "Hook for functions attaching themselves to `M-S-up'.
  12589. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12590. (defvar org-shiftmetadown-hook nil
  12591. "Hook for functions attaching themselves to `M-S-down'.
  12592. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12593. (defvar org-metareturn-hook nil
  12594. "Hook for functions attaching themselves to `M-RET'.
  12595. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12596. (defun org-modifier-cursor-error ()
  12597. "Throw an error, a modified cursor command was applied in wrong context."
  12598. (error "This command is active in special context like tables, headlines or items"))
  12599. (defun org-shiftselect-error ()
  12600. "Throw an error because Shift-Cursor command was applied in wrong context."
  12601. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12602. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12603. (error "This command works only in special context like headlines or timestamps.")))
  12604. (defun org-call-for-shift-select (cmd)
  12605. (let ((this-command-keys-shift-translated t))
  12606. (call-interactively cmd)))
  12607. (defun org-shifttab (&optional arg)
  12608. "Global visibility cycling or move to previous table field.
  12609. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12610. on context.
  12611. See the individual commands for more information."
  12612. (interactive "P")
  12613. (cond
  12614. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12615. ((integerp arg)
  12616. (message "Content view to level: %d" arg)
  12617. (org-content (prefix-numeric-value arg))
  12618. (setq org-cycle-global-status 'overview))
  12619. (t (call-interactively 'org-global-cycle))))
  12620. (defun org-shiftmetaleft ()
  12621. "Promote subtree or delete table column.
  12622. Calls `org-promote-subtree', `org-outdent-item',
  12623. or `org-table-delete-column', depending on context.
  12624. See the individual commands for more information."
  12625. (interactive)
  12626. (cond
  12627. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12628. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12629. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12630. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12631. (t (org-modifier-cursor-error))))
  12632. (defun org-shiftmetaright ()
  12633. "Demote subtree or insert table column.
  12634. Calls `org-demote-subtree', `org-indent-item',
  12635. or `org-table-insert-column', depending on context.
  12636. See the individual commands for more information."
  12637. (interactive)
  12638. (cond
  12639. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12640. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12641. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12642. ((org-at-item-p) (call-interactively 'org-indent-item))
  12643. (t (org-modifier-cursor-error))))
  12644. (defun org-shiftmetaup (&optional arg)
  12645. "Move subtree up or kill table row.
  12646. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12647. `org-move-item-up' depending on context. See the individual commands
  12648. for more information."
  12649. (interactive "P")
  12650. (cond
  12651. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12652. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12653. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12654. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12655. (t (org-modifier-cursor-error))))
  12656. (defun org-shiftmetadown (&optional arg)
  12657. "Move subtree down or insert table row.
  12658. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12659. `org-move-item-down', depending on context. See the individual
  12660. commands for more information."
  12661. (interactive "P")
  12662. (cond
  12663. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12664. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12665. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12666. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12667. (t (org-modifier-cursor-error))))
  12668. (defun org-metaleft (&optional arg)
  12669. "Promote heading or move table column to left.
  12670. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12671. With no specific context, calls the Emacs default `backward-word'.
  12672. See the individual commands for more information."
  12673. (interactive "P")
  12674. (cond
  12675. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12676. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12677. ((or (org-on-heading-p)
  12678. (and (org-region-active-p)
  12679. (save-excursion
  12680. (goto-char (region-beginning))
  12681. (org-on-heading-p))))
  12682. (call-interactively 'org-do-promote))
  12683. ((or (org-at-item-p)
  12684. (and (org-region-active-p)
  12685. (save-excursion
  12686. (goto-char (region-beginning))
  12687. (org-at-item-p))))
  12688. (call-interactively 'org-outdent-item))
  12689. (t (call-interactively 'backward-word))))
  12690. (defun org-metaright (&optional arg)
  12691. "Demote subtree or move table column to right.
  12692. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12693. With no specific context, calls the Emacs default `forward-word'.
  12694. See the individual commands for more information."
  12695. (interactive "P")
  12696. (cond
  12697. ((run-hook-with-args-until-success 'org-metaright-hook))
  12698. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12699. ((or (org-on-heading-p)
  12700. (and (org-region-active-p)
  12701. (save-excursion
  12702. (goto-char (region-beginning))
  12703. (org-on-heading-p))))
  12704. (call-interactively 'org-do-demote))
  12705. ((or (org-at-item-p)
  12706. (and (org-region-active-p)
  12707. (save-excursion
  12708. (goto-char (region-beginning))
  12709. (org-at-item-p))))
  12710. (call-interactively 'org-indent-item))
  12711. (t (call-interactively 'forward-word))))
  12712. (defun org-metaup (&optional arg)
  12713. "Move subtree up or move table row up.
  12714. Calls `org-move-subtree-up' or `org-table-move-row' or
  12715. `org-move-item-up', depending on context. See the individual commands
  12716. for more information."
  12717. (interactive "P")
  12718. (cond
  12719. ((run-hook-with-args-until-success 'org-metaup-hook))
  12720. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12721. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12722. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12723. (t (transpose-lines 1) (beginning-of-line -1))))
  12724. (defun org-metadown (&optional arg)
  12725. "Move subtree down or move table row down.
  12726. Calls `org-move-subtree-down' or `org-table-move-row' or
  12727. `org-move-item-down', depending on context. See the individual
  12728. commands for more information."
  12729. (interactive "P")
  12730. (cond
  12731. ((run-hook-with-args-until-success 'org-metadown-hook))
  12732. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12733. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12734. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12735. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12736. (defun org-shiftup (&optional arg)
  12737. "Increase item in timestamp or increase priority of current headline.
  12738. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12739. depending on context. See the individual commands for more information."
  12740. (interactive "P")
  12741. (cond
  12742. ((and org-support-shift-select (org-region-active-p))
  12743. (org-call-for-shift-select 'previous-line))
  12744. ((org-at-timestamp-p t)
  12745. (call-interactively (if org-edit-timestamp-down-means-later
  12746. 'org-timestamp-down 'org-timestamp-up)))
  12747. ((and (not (eq org-support-shift-select 'always))
  12748. (org-on-heading-p))
  12749. (call-interactively 'org-priority-up))
  12750. ((and (not org-support-shift-select) (org-at-item-p))
  12751. (call-interactively 'org-previous-item))
  12752. ((org-clocktable-try-shift 'up arg))
  12753. (org-support-shift-select
  12754. (org-call-for-shift-select 'previous-line))
  12755. (t (org-shiftselect-error))))
  12756. (defun org-shiftdown (&optional arg)
  12757. "Decrease item in timestamp or decrease priority of current headline.
  12758. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12759. depending on context. See the individual commands for more information."
  12760. (interactive "P")
  12761. (cond
  12762. ((and org-support-shift-select (org-region-active-p))
  12763. (org-call-for-shift-select 'next-line))
  12764. ((org-at-timestamp-p t)
  12765. (call-interactively (if org-edit-timestamp-down-means-later
  12766. 'org-timestamp-up 'org-timestamp-down)))
  12767. ((and (not (eq org-support-shift-select 'always))
  12768. (org-on-heading-p))
  12769. (call-interactively 'org-priority-down))
  12770. ((and (not org-support-shift-select) (org-at-item-p))
  12771. (call-interactively 'org-next-item))
  12772. ((org-clocktable-try-shift 'down arg))
  12773. (org-support-shift-select
  12774. (org-call-for-shift-select 'next-line))
  12775. (t (org-shiftselect-error))))
  12776. (defun org-shiftright (&optional arg)
  12777. "Cycle the thing at point or in the current line, depending on context.
  12778. Depending on context, this does one of the following:
  12779. - switch a timestamp at point one day into the future
  12780. - on a headline, switch to the next TODO keyword.
  12781. - on an item, switch entire list to the next bullet type
  12782. - on a property line, switch to the next allowed value
  12783. - on a clocktable definition line, move time block into the future"
  12784. (interactive "P")
  12785. (cond
  12786. ((and org-support-shift-select (org-region-active-p))
  12787. (org-call-for-shift-select 'forward-char))
  12788. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12789. ((and (not (eq org-support-shift-select 'always))
  12790. (org-on-heading-p))
  12791. (org-call-with-arg 'org-todo 'right))
  12792. ((or (and org-support-shift-select
  12793. (not (eq org-support-shift-select 'always))
  12794. (org-at-item-bullet-p))
  12795. (and (not org-support-shift-select) (org-at-item-p)))
  12796. (org-call-with-arg 'org-cycle-list-bullet nil))
  12797. ((and (not (eq org-support-shift-select 'always))
  12798. (org-at-property-p))
  12799. (call-interactively 'org-property-next-allowed-value))
  12800. ((org-clocktable-try-shift 'right arg))
  12801. (org-support-shift-select
  12802. (org-call-for-shift-select 'forward-char))
  12803. (t (org-shiftselect-error))))
  12804. (defun org-shiftleft (&optional arg)
  12805. "Cycle the thing at point or in the current line, depending on context.
  12806. Depending on context, this does one of the following:
  12807. - switch a timestamp at point one day into the past
  12808. - on a headline, switch to the previous TODO keyword.
  12809. - on an item, switch entire list to the previous bullet type
  12810. - on a property line, switch to the previous allowed value
  12811. - on a clocktable definition line, move time block into the past"
  12812. (interactive "P")
  12813. (cond
  12814. ((and org-support-shift-select (org-region-active-p))
  12815. (org-call-for-shift-select 'backward-char))
  12816. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12817. ((and (not (eq org-support-shift-select 'always))
  12818. (org-on-heading-p))
  12819. (org-call-with-arg 'org-todo 'left))
  12820. ((or (and org-support-shift-select
  12821. (not (eq org-support-shift-select 'always))
  12822. (org-at-item-bullet-p))
  12823. (and (not org-support-shift-select) (org-at-item-p)))
  12824. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12825. ((and (not (eq org-support-shift-select 'always))
  12826. (org-at-property-p))
  12827. (call-interactively 'org-property-previous-allowed-value))
  12828. ((org-clocktable-try-shift 'left arg))
  12829. (org-support-shift-select
  12830. (org-call-for-shift-select 'backward-char))
  12831. (t (org-shiftselect-error))))
  12832. (defun org-shiftcontrolright ()
  12833. "Switch to next TODO set."
  12834. (interactive)
  12835. (cond
  12836. ((and org-support-shift-select (org-region-active-p))
  12837. (org-call-for-shift-select 'forward-word))
  12838. ((and (not (eq org-support-shift-select 'always))
  12839. (org-on-heading-p))
  12840. (org-call-with-arg 'org-todo 'nextset))
  12841. (org-support-shift-select
  12842. (org-call-for-shift-select 'forward-word))
  12843. (t (org-shiftselect-error))))
  12844. (defun org-shiftcontrolleft ()
  12845. "Switch to previous TODO set."
  12846. (interactive)
  12847. (cond
  12848. ((and org-support-shift-select (org-region-active-p))
  12849. (org-call-for-shift-select 'backward-word))
  12850. ((and (not (eq org-support-shift-select 'always))
  12851. (org-on-heading-p))
  12852. (org-call-with-arg 'org-todo 'previousset))
  12853. (org-support-shift-select
  12854. (org-call-for-shift-select 'backward-word))
  12855. (t (org-shiftselect-error))))
  12856. (defun org-ctrl-c-ret ()
  12857. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12858. (interactive)
  12859. (cond
  12860. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12861. (t (call-interactively 'org-insert-heading))))
  12862. (defun org-copy-special ()
  12863. "Copy region in table or copy current subtree.
  12864. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12865. See the individual commands for more information."
  12866. (interactive)
  12867. (call-interactively
  12868. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12869. (defun org-cut-special ()
  12870. "Cut region in table or cut current subtree.
  12871. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12872. See the individual commands for more information."
  12873. (interactive)
  12874. (call-interactively
  12875. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12876. (defun org-paste-special (arg)
  12877. "Paste rectangular region into table, or past subtree relative to level.
  12878. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12879. See the individual commands for more information."
  12880. (interactive "P")
  12881. (if (org-at-table-p)
  12882. (org-table-paste-rectangle)
  12883. (org-paste-subtree arg)))
  12884. (defun org-edit-special ()
  12885. "Call a special editor for the stuff at point.
  12886. When at a table, call the formula editor with `org-table-edit-formulas'.
  12887. When at the first line of an src example, call `org-edit-src-code'.
  12888. When in an #+include line, visit the include file. Otherwise call
  12889. `ffap' to visit the file at point."
  12890. (interactive)
  12891. (cond
  12892. ((org-at-table-p)
  12893. (call-interactively 'org-table-edit-formulas))
  12894. ((save-excursion
  12895. (beginning-of-line 1)
  12896. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12897. (find-file (org-trim (match-string 1))))
  12898. ((org-edit-src-code))
  12899. ((org-edit-fixed-width-region))
  12900. (t (call-interactively 'ffap))))
  12901. (defun org-ctrl-c-ctrl-c (&optional arg)
  12902. "Set tags in headline, or update according to changed information at point.
  12903. This command does many different things, depending on context:
  12904. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12905. this is what we do.
  12906. - If the cursor is in a headline, prompt for tags and insert them
  12907. into the current line, aligned to `org-tags-column'. When called
  12908. with prefix arg, realign all tags in the current buffer.
  12909. - If the cursor is in one of the special #+KEYWORD lines, this
  12910. triggers scanning the buffer for these lines and updating the
  12911. information.
  12912. - If the cursor is inside a table, realign the table. This command
  12913. works even if the automatic table editor has been turned off.
  12914. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12915. the entire table.
  12916. - If the cursor is at a footnote reference or definition, jump to
  12917. the corresponding definition or references, respectively.
  12918. - If the cursor is a the beginning of a dynamic block, update it.
  12919. - If the cursor is inside a table created by the table.el package,
  12920. activate that table.
  12921. - If the current buffer is a remember buffer, close note and file
  12922. it. A prefix argument of 1 files to the default location
  12923. without further interaction. A prefix argument of 2 files to
  12924. the currently clocking task.
  12925. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12926. links in this buffer.
  12927. - If the cursor is on a numbered item in a plain list, renumber the
  12928. ordered list.
  12929. - If the cursor is on a checkbox, toggle it."
  12930. (interactive "P")
  12931. (let ((org-enable-table-editor t))
  12932. (cond
  12933. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12934. org-occur-highlights
  12935. org-latex-fragment-image-overlays)
  12936. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12937. (org-remove-occur-highlights)
  12938. (org-remove-latex-fragment-image-overlays)
  12939. (message "Temporary highlights/overlays removed from current buffer"))
  12940. ((and (local-variable-p 'org-finish-function (current-buffer))
  12941. (fboundp org-finish-function))
  12942. (funcall org-finish-function))
  12943. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12944. ((org-at-property-p)
  12945. (call-interactively 'org-property-action))
  12946. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12947. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12948. ((org-at-table.el-p)
  12949. (require 'table)
  12950. (beginning-of-line 1)
  12951. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12952. (call-interactively 'table-recognize-table))
  12953. ((org-at-table-p)
  12954. (org-table-maybe-eval-formula)
  12955. (if arg
  12956. (call-interactively 'org-table-recalculate)
  12957. (org-table-maybe-recalculate-line))
  12958. (call-interactively 'org-table-align))
  12959. ((or (org-footnote-at-reference-p)
  12960. (org-footnote-at-definition-p))
  12961. (call-interactively 'org-footnote-action))
  12962. ((org-at-item-checkbox-p)
  12963. (call-interactively 'org-toggle-checkbox))
  12964. ((org-at-item-p)
  12965. (if arg
  12966. (call-interactively 'org-toggle-checkbox)
  12967. (call-interactively 'org-maybe-renumber-ordered-list)))
  12968. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12969. ;; Dynamic block
  12970. (beginning-of-line 1)
  12971. (save-excursion (org-update-dblock)))
  12972. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12973. (cond
  12974. ((equal (match-string 1) "TBLFM")
  12975. ;; Recalculate the table before this line
  12976. (save-excursion
  12977. (beginning-of-line 1)
  12978. (skip-chars-backward " \r\n\t")
  12979. (if (org-at-table-p)
  12980. (org-call-with-arg 'org-table-recalculate t))))
  12981. (t
  12982. ; (org-set-regexps-and-options)
  12983. ; (org-restart-font-lock)
  12984. (let ((org-inhibit-startup t)) (org-mode-restart))
  12985. (message "Local setup has been refreshed"))))
  12986. (t (error "C-c C-c can do nothing useful at this location.")))))
  12987. (defun org-mode-restart ()
  12988. "Restart Org-mode, to scan again for special lines.
  12989. Also updates the keyword regular expressions."
  12990. (interactive)
  12991. (org-mode)
  12992. (message "Org-mode restarted"))
  12993. (defun org-kill-note-or-show-branches ()
  12994. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12995. (interactive)
  12996. (if (not org-finish-function)
  12997. (call-interactively 'show-branches)
  12998. (let ((org-note-abort t))
  12999. (funcall org-finish-function))))
  13000. (defun org-return (&optional indent)
  13001. "Goto next table row or insert a newline.
  13002. Calls `org-table-next-row' or `newline', depending on context.
  13003. See the individual commands for more information."
  13004. (interactive)
  13005. (cond
  13006. ((bobp) (if indent (newline-and-indent) (newline)))
  13007. ((org-at-table-p)
  13008. (org-table-justify-field-maybe)
  13009. (call-interactively 'org-table-next-row))
  13010. ((and org-return-follows-link
  13011. (eq (get-text-property (point) 'face) 'org-link))
  13012. (call-interactively 'org-open-at-point))
  13013. ((and (org-at-heading-p)
  13014. (looking-at
  13015. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13016. (org-show-entry)
  13017. (end-of-line 1)
  13018. (newline))
  13019. (t (if indent (newline-and-indent) (newline)))))
  13020. (defun org-return-indent ()
  13021. "Goto next table row or insert a newline and indent.
  13022. Calls `org-table-next-row' or `newline-and-indent', depending on
  13023. context. See the individual commands for more information."
  13024. (interactive)
  13025. (org-return t))
  13026. (defun org-ctrl-c-star ()
  13027. "Compute table, or change heading status of lines.
  13028. Calls `org-table-recalculate' or `org-toggle-heading',
  13029. depending on context."
  13030. (interactive)
  13031. (cond
  13032. ((org-at-table-p)
  13033. (call-interactively 'org-table-recalculate))
  13034. (t
  13035. ;; Convert all lines in region to list items
  13036. (call-interactively 'org-toggle-heading))))
  13037. (defun org-ctrl-c-minus ()
  13038. "Insert separator line in table or modify bullet status of line.
  13039. Also turns a plain line or a region of lines into list items.
  13040. Calls `org-table-insert-hline', `org-toggle-item', or
  13041. `org-cycle-list-bullet', depending on context."
  13042. (interactive)
  13043. (cond
  13044. ((org-at-table-p)
  13045. (call-interactively 'org-table-insert-hline))
  13046. ((org-region-active-p)
  13047. (call-interactively 'org-toggle-item))
  13048. ((org-in-item-p)
  13049. (call-interactively 'org-cycle-list-bullet))
  13050. (t
  13051. (call-interactively 'org-toggle-item))))
  13052. (defun org-toggle-item ()
  13053. "Convert headings or normal lines to items, items to normal lines.
  13054. If there is no active region, only the current line is considered.
  13055. If the first line in the region is a headline, convert all headlines to items.
  13056. If the first line in the region is an item, convert all items to normal lines.
  13057. If the first line is normal text, add an item bullet to each line."
  13058. (interactive)
  13059. (let (l2 l beg end)
  13060. (if (org-region-active-p)
  13061. (setq beg (region-beginning) end (region-end))
  13062. (setq beg (point-at-bol)
  13063. end (min (1+ (point-at-eol)) (point-max))))
  13064. (save-excursion
  13065. (goto-char end)
  13066. (setq l2 (org-current-line))
  13067. (goto-char beg)
  13068. (beginning-of-line 1)
  13069. (setq l (1- (org-current-line)))
  13070. (if (org-at-item-p)
  13071. ;; We already have items, de-itemize
  13072. (while (< (setq l (1+ l)) l2)
  13073. (when (org-at-item-p)
  13074. (goto-char (match-beginning 2))
  13075. (delete-region (match-beginning 2) (match-end 2))
  13076. (and (looking-at "[ \t]+") (replace-match "")))
  13077. (beginning-of-line 2))
  13078. (if (org-on-heading-p)
  13079. ;; Headings, convert to items
  13080. (while (< (setq l (1+ l)) l2)
  13081. (if (looking-at org-outline-regexp)
  13082. (replace-match "- " t t))
  13083. (beginning-of-line 2))
  13084. ;; normal lines, turn them into items
  13085. (while (< (setq l (1+ l)) l2)
  13086. (unless (org-at-item-p)
  13087. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13088. (replace-match "\\1- \\2")))
  13089. (beginning-of-line 2)))))))
  13090. (defun org-toggle-heading (&optional nstars)
  13091. "Convert headings to normal text, or items or text to headings.
  13092. If there is no active region, only the current line is considered.
  13093. If the first line is a heading, remove the stars from all headlines
  13094. in the region.
  13095. If the first line is a plain list item, turn all plain list items into
  13096. headings.
  13097. If the first line is a normal line, turn each and every line in the region
  13098. into a heading.
  13099. When converting a line into a heading, the number of stars is chosen
  13100. such that the lines become children of the current entry. However, when
  13101. a prefix argument is given, its value determines the number of stars to add."
  13102. (interactive "P")
  13103. (let (l2 l itemp beg end)
  13104. (if (org-region-active-p)
  13105. (setq beg (region-beginning) end (region-end))
  13106. (setq beg (point-at-bol)
  13107. end (min (1+ (point-at-eol)) (point-max))))
  13108. (save-excursion
  13109. (goto-char end)
  13110. (setq l2 (org-current-line))
  13111. (goto-char beg)
  13112. (beginning-of-line 1)
  13113. (setq l (1- (org-current-line)))
  13114. (if (org-on-heading-p)
  13115. ;; We already have headlines, de-star them
  13116. (while (< (setq l (1+ l)) l2)
  13117. (when (org-on-heading-p t)
  13118. (and (looking-at outline-regexp) (replace-match "")))
  13119. (beginning-of-line 2))
  13120. (setq itemp (org-at-item-p))
  13121. (let* ((stars
  13122. (if nstars
  13123. (make-string (prefix-numeric-value current-prefix-arg)
  13124. ?*)
  13125. (save-excursion
  13126. (re-search-backward org-complex-heading-regexp nil t)
  13127. (or (match-string 1) ""))))
  13128. (add-stars (cond (nstars "")
  13129. ((equal stars "") "*")
  13130. (org-odd-levels-only "**")
  13131. (t "*")))
  13132. (rpl (concat stars add-stars " ")))
  13133. (while (< (setq l (1+ l)) l2)
  13134. (if itemp
  13135. (and (org-at-item-p) (replace-match rpl t t))
  13136. (unless (org-on-heading-p)
  13137. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13138. (replace-match (concat rpl (match-string 2))))))
  13139. (beginning-of-line 2)))))))
  13140. (defun org-meta-return (&optional arg)
  13141. "Insert a new heading or wrap a region in a table.
  13142. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13143. See the individual commands for more information."
  13144. (interactive "P")
  13145. (cond
  13146. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13147. ((org-at-table-p)
  13148. (call-interactively 'org-table-wrap-region))
  13149. (t (call-interactively 'org-insert-heading))))
  13150. ;;; Menu entries
  13151. ;; Define the Org-mode menus
  13152. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13153. '("Tbl"
  13154. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13155. ["Next Field" org-cycle (org-at-table-p)]
  13156. ["Previous Field" org-shifttab (org-at-table-p)]
  13157. ["Next Row" org-return (org-at-table-p)]
  13158. "--"
  13159. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13160. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13161. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13162. "--"
  13163. ("Column"
  13164. ["Move Column Left" org-metaleft (org-at-table-p)]
  13165. ["Move Column Right" org-metaright (org-at-table-p)]
  13166. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13167. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13168. ("Row"
  13169. ["Move Row Up" org-metaup (org-at-table-p)]
  13170. ["Move Row Down" org-metadown (org-at-table-p)]
  13171. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13172. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13173. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13174. "--"
  13175. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13176. ("Rectangle"
  13177. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13178. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13179. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13180. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13181. "--"
  13182. ("Calculate"
  13183. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13184. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13185. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13186. "--"
  13187. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13188. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13189. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13190. "--"
  13191. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13192. "--"
  13193. ["Sum Column/Rectangle" org-table-sum
  13194. (or (org-at-table-p) (org-region-active-p))]
  13195. ["Which Column?" org-table-current-column (org-at-table-p)])
  13196. ["Debug Formulas"
  13197. org-table-toggle-formula-debugger
  13198. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13199. ["Show Col/Row Numbers"
  13200. org-table-toggle-coordinate-overlays
  13201. :style toggle
  13202. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13203. "--"
  13204. ["Create" org-table-create (and (not (org-at-table-p))
  13205. org-enable-table-editor)]
  13206. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13207. ["Import from File" org-table-import (not (org-at-table-p))]
  13208. ["Export to File" org-table-export (org-at-table-p)]
  13209. "--"
  13210. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13211. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13212. '("Org"
  13213. ("Show/Hide"
  13214. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13215. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13216. ["Sparse Tree..." org-sparse-tree t]
  13217. ["Reveal Context" org-reveal t]
  13218. ["Show All" show-all t]
  13219. "--"
  13220. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13221. "--"
  13222. ["New Heading" org-insert-heading t]
  13223. ("Navigate Headings"
  13224. ["Up" outline-up-heading t]
  13225. ["Next" outline-next-visible-heading t]
  13226. ["Previous" outline-previous-visible-heading t]
  13227. ["Next Same Level" outline-forward-same-level t]
  13228. ["Previous Same Level" outline-backward-same-level t]
  13229. "--"
  13230. ["Jump" org-goto t])
  13231. ("Edit Structure"
  13232. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13233. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13234. "--"
  13235. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13236. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13237. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13238. "--"
  13239. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13240. "--"
  13241. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13242. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13243. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13244. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13245. "--"
  13246. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13247. "--"
  13248. ["Convert to odd levels" org-convert-to-odd-levels t]
  13249. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13250. ("Editing"
  13251. ["Emphasis..." org-emphasize t]
  13252. ["Edit Source Example" org-edit-special t]
  13253. "--"
  13254. ["Footnote new/jump" org-footnote-action t]
  13255. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13256. ("Archive"
  13257. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13258. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13259. ; :active t :keys "C-u C-c C-x C-a"]
  13260. ["Sparse trees open ARCHIVE trees"
  13261. (setq org-sparse-tree-open-archived-trees
  13262. (not org-sparse-tree-open-archived-trees))
  13263. :style toggle :selected org-sparse-tree-open-archived-trees]
  13264. ["Cycling opens ARCHIVE trees"
  13265. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13266. :style toggle :selected org-cycle-open-archived-trees]
  13267. "--"
  13268. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13269. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13270. ; ["Check and Move Children" (org-archive-subtree '(4))
  13271. ; :active t :keys "C-u C-c C-x C-s"]
  13272. )
  13273. "--"
  13274. ("Hyperlinks"
  13275. ["Store Link (Global)" org-store-link t]
  13276. ["Insert Link" org-insert-link t]
  13277. ["Follow Link" org-open-at-point t]
  13278. "--"
  13279. ["Next link" org-next-link t]
  13280. ["Previous link" org-previous-link t]
  13281. "--"
  13282. ["Descriptive Links"
  13283. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13284. :style radio
  13285. :selected (member '(org-link) buffer-invisibility-spec)]
  13286. ["Literal Links"
  13287. (progn
  13288. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13289. :style radio
  13290. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13291. "--"
  13292. ("TODO Lists"
  13293. ["TODO/DONE/-" org-todo t]
  13294. ("Select keyword"
  13295. ["Next keyword" org-shiftright (org-on-heading-p)]
  13296. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  13297. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  13298. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  13299. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  13300. ["Show TODO Tree" org-show-todo-tree t]
  13301. ["Global TODO list" org-todo-list t]
  13302. "--"
  13303. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  13304. :selected org-enforce-todo-dependencies :style toggle :active t]
  13305. "Settings for tree at point"
  13306. ["Do Children sequentially" org-toggle-ordered-property :style radio
  13307. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  13308. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13309. ["Do Children parallel" org-toggle-ordered-property :style radio
  13310. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  13311. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  13312. "--"
  13313. ["Set Priority" org-priority t]
  13314. ["Priority Up" org-shiftup t]
  13315. ["Priority Down" org-shiftdown t]
  13316. "--"
  13317. ["Get news from all feeds" org-feed-update-all t]
  13318. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  13319. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  13320. ("TAGS and Properties"
  13321. ["Set Tags" org-set-tags-command t]
  13322. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  13323. "--"
  13324. ["Set property" org-set-property t]
  13325. ["Column view of properties" org-columns t]
  13326. ["Insert Column View DBlock" org-insert-columns-dblock t])
  13327. ("Dates and Scheduling"
  13328. ["Timestamp" org-time-stamp t]
  13329. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13330. ("Change Date"
  13331. ["1 Day Later" org-shiftright t]
  13332. ["1 Day Earlier" org-shiftleft t]
  13333. ["1 ... Later" org-shiftup t]
  13334. ["1 ... Earlier" org-shiftdown t])
  13335. ["Compute Time Range" org-evaluate-time-range t]
  13336. ["Schedule Item" org-schedule t]
  13337. ["Deadline" org-deadline t]
  13338. "--"
  13339. ["Custom time format" org-toggle-time-stamp-overlays
  13340. :style radio :selected org-display-custom-times]
  13341. "--"
  13342. ["Goto Calendar" org-goto-calendar t]
  13343. ["Date from Calendar" org-date-from-calendar t]
  13344. "--"
  13345. ["Start/Restart Timer" org-timer-start t]
  13346. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13347. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13348. ["Insert Timer String" org-timer t]
  13349. ["Insert Timer Item" org-timer-item t])
  13350. ("Logging work"
  13351. ["Clock in" org-clock-in t]
  13352. ["Clock out" org-clock-out t]
  13353. ["Clock cancel" org-clock-cancel t]
  13354. ["Goto running clock" org-clock-goto t]
  13355. ["Display times" org-clock-display t]
  13356. ["Create clock table" org-clock-report t]
  13357. "--"
  13358. ["Record DONE time"
  13359. (progn (setq org-log-done (not org-log-done))
  13360. (message "Switching to %s will %s record a timestamp"
  13361. (car org-done-keywords)
  13362. (if org-log-done "automatically" "not")))
  13363. :style toggle :selected org-log-done])
  13364. "--"
  13365. ["Agenda Command..." org-agenda t]
  13366. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13367. ("File List for Agenda")
  13368. ("Special views current file"
  13369. ["TODO Tree" org-show-todo-tree t]
  13370. ["Check Deadlines" org-check-deadlines t]
  13371. ["Timeline" org-timeline t]
  13372. ["Tags/Property tree" org-match-sparse-tree t])
  13373. "--"
  13374. ["Export/Publish..." org-export t]
  13375. ("LaTeX"
  13376. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13377. :selected org-cdlatex-mode]
  13378. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13379. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13380. ["Modify math symbol" org-cdlatex-math-modify
  13381. (org-inside-LaTeX-fragment-p)]
  13382. ["Insert citation" org-reftex-citation t]
  13383. "--"
  13384. ["Export LaTeX fragments as images"
  13385. (if (featurep 'org-exp)
  13386. (setq org-export-with-LaTeX-fragments
  13387. (not org-export-with-LaTeX-fragments))
  13388. (require 'org-exp))
  13389. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13390. org-export-with-LaTeX-fragments)])
  13391. "--"
  13392. ("Documentation"
  13393. ["Show Version" org-version t]
  13394. ["Info Documentation" org-info t])
  13395. ("Customize"
  13396. ["Browse Org Group" org-customize t]
  13397. "--"
  13398. ["Expand This Menu" org-create-customize-menu
  13399. (fboundp 'customize-menu-create)])
  13400. "--"
  13401. ("Refresh/Reload"
  13402. ["Refresh setup current buffer" org-mode-restart t]
  13403. ["Reload Org (after update)" org-reload t]
  13404. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  13405. ))
  13406. (defun org-info (&optional node)
  13407. "Read documentation for Org-mode in the info system.
  13408. With optional NODE, go directly to that node."
  13409. (interactive)
  13410. (info (format "(org)%s" (or node ""))))
  13411. (defun org-install-agenda-files-menu ()
  13412. (let ((bl (buffer-list)))
  13413. (save-excursion
  13414. (while bl
  13415. (set-buffer (pop bl))
  13416. (if (org-mode-p) (setq bl nil)))
  13417. (when (org-mode-p)
  13418. (easy-menu-change
  13419. '("Org") "File List for Agenda"
  13420. (append
  13421. (list
  13422. ["Edit File List" (org-edit-agenda-file-list) t]
  13423. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13424. ["Remove Current File from List" org-remove-file t]
  13425. ["Cycle through agenda files" org-cycle-agenda-files t]
  13426. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13427. "--")
  13428. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13429. ;;;; Documentation
  13430. ;;;###autoload
  13431. (defun org-require-autoloaded-modules ()
  13432. (interactive)
  13433. (mapc 'require
  13434. '(org-agenda org-archive org-attach org-clock org-colview
  13435. org-exp org-id org-export-latex org-docbook
  13436. org-publish org-remember org-table org-timer)))
  13437. ;;;###autoload
  13438. (defun org-reload (&optional uncompiled)
  13439. "Reload all org lisp files.
  13440. With prefix arg UNCOMPILED, load the uncompiled versions."
  13441. (interactive "P")
  13442. (require 'find-func)
  13443. (let* ((dir (file-name-directory (find-library-name "org")))
  13444. (files (directory-files dir t "\\.el\\'"))
  13445. (remove-re (concat (if (featurep 'xemacs)
  13446. "org-colview" "org-colview-xemacs")
  13447. "\\'")))
  13448. (setq files (mapcar 'file-name-sans-extension files))
  13449. (setq files (mapcar
  13450. (lambda (x) (if (string-match remove-re x) nil x))
  13451. files))
  13452. (setq files (delq nil files))
  13453. (mapc
  13454. (lambda (f)
  13455. (if (and (not uncompiled)
  13456. (file-exists-p (concat f ".elc")))
  13457. (load (concat f ".elc") nil nil t)
  13458. (load (concat f ".el") nil nil t)))
  13459. files)))
  13460. ;;;###autoload
  13461. (defun org-customize ()
  13462. "Call the customize function with org as argument."
  13463. (interactive)
  13464. (org-load-modules-maybe)
  13465. (org-require-autoloaded-modules)
  13466. (customize-browse 'org))
  13467. (defun org-create-customize-menu ()
  13468. "Create a full customization menu for Org-mode, insert it into the menu."
  13469. (interactive)
  13470. (org-load-modules-maybe)
  13471. (org-require-autoloaded-modules)
  13472. (if (fboundp 'customize-menu-create)
  13473. (progn
  13474. (easy-menu-change
  13475. '("Org") "Customize"
  13476. `(["Browse Org group" org-customize t]
  13477. "--"
  13478. ,(customize-menu-create 'org)
  13479. ["Set" Custom-set t]
  13480. ["Save" Custom-save t]
  13481. ["Reset to Current" Custom-reset-current t]
  13482. ["Reset to Saved" Custom-reset-saved t]
  13483. ["Reset to Standard Settings" Custom-reset-standard t]))
  13484. (message "\"Org\"-menu now contains full customization menu"))
  13485. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13486. ;;;; Miscellaneous stuff
  13487. ;;; Generally useful functions
  13488. (defun org-find-text-property-in-string (prop s)
  13489. "Return the first non-nil value of property PROP in string S."
  13490. (or (get-text-property 0 prop s)
  13491. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13492. prop s)))
  13493. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13494. "Display the given MESSAGE as a warning."
  13495. (if (fboundp 'display-warning)
  13496. (display-warning 'org message
  13497. (if (featurep 'xemacs)
  13498. 'warning
  13499. :warning))
  13500. (let ((buf (get-buffer-create "*Org warnings*")))
  13501. (with-current-buffer buf
  13502. (goto-char (point-max))
  13503. (insert "Warning (Org): " message)
  13504. (unless (bolp)
  13505. (newline)))
  13506. (display-buffer buf)
  13507. (sit-for 0))))
  13508. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13509. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13510. (if (and marker (marker-buffer marker)
  13511. (buffer-live-p (marker-buffer marker)))
  13512. (progn
  13513. (switch-to-buffer (marker-buffer marker))
  13514. (if (or (> marker (point-max)) (< marker (point-min)))
  13515. (widen))
  13516. (goto-char marker)
  13517. (org-show-context 'org-goto))
  13518. (if bookmark
  13519. (bookmark-jump bookmark)
  13520. (error "Cannot find location"))))
  13521. (defun org-quote-csv-field (s)
  13522. "Quote field for inclusion in CSV material."
  13523. (if (string-match "[\",]" s)
  13524. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13525. s))
  13526. (defun org-plist-delete (plist property)
  13527. "Delete PROPERTY from PLIST.
  13528. This is in contrast to merely setting it to 0."
  13529. (let (p)
  13530. (while plist
  13531. (if (not (eq property (car plist)))
  13532. (setq p (plist-put p (car plist) (nth 1 plist))))
  13533. (setq plist (cddr plist)))
  13534. p))
  13535. (defun org-force-self-insert (N)
  13536. "Needed to enforce self-insert under remapping."
  13537. (interactive "p")
  13538. (self-insert-command N))
  13539. (defun org-string-width (s)
  13540. "Compute width of string, ignoring invisible characters.
  13541. This ignores character with invisibility property `org-link', and also
  13542. characters with property `org-cwidth', because these will become invisible
  13543. upon the next fontification round."
  13544. (let (b l)
  13545. (when (or (eq t buffer-invisibility-spec)
  13546. (assq 'org-link buffer-invisibility-spec))
  13547. (while (setq b (text-property-any 0 (length s)
  13548. 'invisible 'org-link s))
  13549. (setq s (concat (substring s 0 b)
  13550. (substring s (or (next-single-property-change
  13551. b 'invisible s) (length s)))))))
  13552. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13553. (setq s (concat (substring s 0 b)
  13554. (substring s (or (next-single-property-change
  13555. b 'org-cwidth s) (length s))))))
  13556. (setq l (string-width s) b -1)
  13557. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13558. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13559. l))
  13560. (defun org-get-indentation (&optional line)
  13561. "Get the indentation of the current line, interpreting tabs.
  13562. When LINE is given, assume it represents a line and compute its indentation."
  13563. (if line
  13564. (if (string-match "^ *" (org-remove-tabs line))
  13565. (match-end 0))
  13566. (save-excursion
  13567. (beginning-of-line 1)
  13568. (skip-chars-forward " \t")
  13569. (current-column))))
  13570. (defun org-remove-tabs (s &optional width)
  13571. "Replace tabulators in S with spaces.
  13572. Assumes that s is a single line, starting in column 0."
  13573. (setq width (or width tab-width))
  13574. (while (string-match "\t" s)
  13575. (setq s (replace-match
  13576. (make-string
  13577. (- (* width (/ (+ (match-beginning 0) width) width))
  13578. (match-beginning 0)) ?\ )
  13579. t t s)))
  13580. s)
  13581. (defun org-fix-indentation (line ind)
  13582. "Fix indentation in LINE.
  13583. IND is a cons cell with target and minimum indentation.
  13584. If the current indentation in LINE is smaller than the minimum,
  13585. leave it alone. If it is larger than ind, set it to the target."
  13586. (let* ((l (org-remove-tabs line))
  13587. (i (org-get-indentation l))
  13588. (i1 (car ind)) (i2 (cdr ind)))
  13589. (if (>= i i2) (setq l (substring line i2)))
  13590. (if (> i1 0)
  13591. (concat (make-string i1 ?\ ) l)
  13592. l)))
  13593. (defun org-base-buffer (buffer)
  13594. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13595. (if (not buffer)
  13596. buffer
  13597. (or (buffer-base-buffer buffer)
  13598. buffer)))
  13599. (defun org-trim (s)
  13600. "Remove whitespace at beginning and end of string."
  13601. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13602. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13603. s)
  13604. (defun org-wrap (string &optional width lines)
  13605. "Wrap string to either a number of lines, or a width in characters.
  13606. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13607. that costs. If there is a word longer than WIDTH, the text is actually
  13608. wrapped to the length of that word.
  13609. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13610. many lines, whatever width that takes.
  13611. The return value is a list of lines, without newlines at the end."
  13612. (let* ((words (org-split-string string "[ \t\n]+"))
  13613. (maxword (apply 'max (mapcar 'org-string-width words)))
  13614. w ll)
  13615. (cond (width
  13616. (org-do-wrap words (max maxword width)))
  13617. (lines
  13618. (setq w maxword)
  13619. (setq ll (org-do-wrap words maxword))
  13620. (if (<= (length ll) lines)
  13621. ll
  13622. (setq ll words)
  13623. (while (> (length ll) lines)
  13624. (setq w (1+ w))
  13625. (setq ll (org-do-wrap words w)))
  13626. ll))
  13627. (t (error "Cannot wrap this")))))
  13628. (defun org-do-wrap (words width)
  13629. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13630. (let (lines line)
  13631. (while words
  13632. (setq line (pop words))
  13633. (while (and words (< (+ (length line) (length (car words))) width))
  13634. (setq line (concat line " " (pop words))))
  13635. (setq lines (push line lines)))
  13636. (nreverse lines)))
  13637. (defun org-split-string (string &optional separators)
  13638. "Splits STRING into substrings at SEPARATORS.
  13639. No empty strings are returned if there are matches at the beginning
  13640. and end of string."
  13641. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13642. (start 0)
  13643. notfirst
  13644. (list nil))
  13645. (while (and (string-match rexp string
  13646. (if (and notfirst
  13647. (= start (match-beginning 0))
  13648. (< start (length string)))
  13649. (1+ start) start))
  13650. (< (match-beginning 0) (length string)))
  13651. (setq notfirst t)
  13652. (or (eq (match-beginning 0) 0)
  13653. (and (eq (match-beginning 0) (match-end 0))
  13654. (eq (match-beginning 0) start))
  13655. (setq list
  13656. (cons (substring string start (match-beginning 0))
  13657. list)))
  13658. (setq start (match-end 0)))
  13659. (or (eq start (length string))
  13660. (setq list
  13661. (cons (substring string start)
  13662. list)))
  13663. (nreverse list)))
  13664. (defun org-uuidgen-p (s)
  13665. "Is S an ID created by UUIDGEN?"
  13666. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  13667. (defun org-context ()
  13668. "Return a list of contexts of the current cursor position.
  13669. If several contexts apply, all are returned.
  13670. Each context entry is a list with a symbol naming the context, and
  13671. two positions indicating start and end of the context. Possible
  13672. contexts are:
  13673. :headline anywhere in a headline
  13674. :headline-stars on the leading stars in a headline
  13675. :todo-keyword on a TODO keyword (including DONE) in a headline
  13676. :tags on the TAGS in a headline
  13677. :priority on the priority cookie in a headline
  13678. :item on the first line of a plain list item
  13679. :item-bullet on the bullet/number of a plain list item
  13680. :checkbox on the checkbox in a plain list item
  13681. :table in an org-mode table
  13682. :table-special on a special filed in a table
  13683. :table-table in a table.el table
  13684. :link on a hyperlink
  13685. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13686. :target on a <<target>>
  13687. :radio-target on a <<<radio-target>>>
  13688. :latex-fragment on a LaTeX fragment
  13689. :latex-preview on a LaTeX fragment with overlayed preview image
  13690. This function expects the position to be visible because it uses font-lock
  13691. faces as a help to recognize the following contexts: :table-special, :link,
  13692. and :keyword."
  13693. (let* ((f (get-text-property (point) 'face))
  13694. (faces (if (listp f) f (list f)))
  13695. (p (point)) clist o)
  13696. ;; First the large context
  13697. (cond
  13698. ((org-on-heading-p t)
  13699. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13700. (when (progn
  13701. (beginning-of-line 1)
  13702. (looking-at org-todo-line-tags-regexp))
  13703. (push (org-point-in-group p 1 :headline-stars) clist)
  13704. (push (org-point-in-group p 2 :todo-keyword) clist)
  13705. (push (org-point-in-group p 4 :tags) clist))
  13706. (goto-char p)
  13707. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13708. (if (looking-at "\\[#[A-Z0-9]\\]")
  13709. (push (org-point-in-group p 0 :priority) clist)))
  13710. ((org-at-item-p)
  13711. (push (org-point-in-group p 2 :item-bullet) clist)
  13712. (push (list :item (point-at-bol)
  13713. (save-excursion (org-end-of-item) (point)))
  13714. clist)
  13715. (and (org-at-item-checkbox-p)
  13716. (push (org-point-in-group p 0 :checkbox) clist)))
  13717. ((org-at-table-p)
  13718. (push (list :table (org-table-begin) (org-table-end)) clist)
  13719. (if (memq 'org-formula faces)
  13720. (push (list :table-special
  13721. (previous-single-property-change p 'face)
  13722. (next-single-property-change p 'face)) clist)))
  13723. ((org-at-table-p 'any)
  13724. (push (list :table-table) clist)))
  13725. (goto-char p)
  13726. ;; Now the small context
  13727. (cond
  13728. ((org-at-timestamp-p)
  13729. (push (org-point-in-group p 0 :timestamp) clist))
  13730. ((memq 'org-link faces)
  13731. (push (list :link
  13732. (previous-single-property-change p 'face)
  13733. (next-single-property-change p 'face)) clist))
  13734. ((memq 'org-special-keyword faces)
  13735. (push (list :keyword
  13736. (previous-single-property-change p 'face)
  13737. (next-single-property-change p 'face)) clist))
  13738. ((org-on-target-p)
  13739. (push (org-point-in-group p 0 :target) clist)
  13740. (goto-char (1- (match-beginning 0)))
  13741. (if (looking-at org-radio-target-regexp)
  13742. (push (org-point-in-group p 0 :radio-target) clist))
  13743. (goto-char p))
  13744. ((setq o (car (delq nil
  13745. (mapcar
  13746. (lambda (x)
  13747. (if (memq x org-latex-fragment-image-overlays) x))
  13748. (org-overlays-at (point))))))
  13749. (push (list :latex-fragment
  13750. (org-overlay-start o) (org-overlay-end o)) clist)
  13751. (push (list :latex-preview
  13752. (org-overlay-start o) (org-overlay-end o)) clist))
  13753. ((org-inside-LaTeX-fragment-p)
  13754. ;; FIXME: positions wrong.
  13755. (push (list :latex-fragment (point) (point)) clist)))
  13756. (setq clist (nreverse (delq nil clist)))
  13757. clist))
  13758. ;; FIXME: Compare with at-regexp-p Do we need both?
  13759. (defun org-in-regexp (re &optional nlines visually)
  13760. "Check if point is inside a match of regexp.
  13761. Normally only the current line is checked, but you can include NLINES extra
  13762. lines both before and after point into the search.
  13763. If VISUALLY is set, require that the cursor is not after the match but
  13764. really on, so that the block visually is on the match."
  13765. (catch 'exit
  13766. (let ((pos (point))
  13767. (eol (point-at-eol (+ 1 (or nlines 0))))
  13768. (inc (if visually 1 0)))
  13769. (save-excursion
  13770. (beginning-of-line (- 1 (or nlines 0)))
  13771. (while (re-search-forward re eol t)
  13772. (if (and (<= (match-beginning 0) pos)
  13773. (>= (+ inc (match-end 0)) pos))
  13774. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13775. (defun org-at-regexp-p (regexp)
  13776. "Is point inside a match of REGEXP in the current line?"
  13777. (catch 'exit
  13778. (save-excursion
  13779. (let ((pos (point)) (end (point-at-eol)))
  13780. (beginning-of-line 1)
  13781. (while (re-search-forward regexp end t)
  13782. (if (and (<= (match-beginning 0) pos)
  13783. (>= (match-end 0) pos))
  13784. (throw 'exit t)))
  13785. nil))))
  13786. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13787. "Call `multi-occur' with buffers for all agenda files."
  13788. (interactive "sOrg-files matching: \np")
  13789. (let* ((files (org-agenda-files))
  13790. (tnames (mapcar 'file-truename files))
  13791. (extra org-agenda-text-search-extra-files)
  13792. f)
  13793. (when (eq (car extra) 'agenda-archives)
  13794. (setq extra (cdr extra))
  13795. (setq files (org-add-archive-files files)))
  13796. (while (setq f (pop extra))
  13797. (unless (member (file-truename f) tnames)
  13798. (add-to-list 'files f 'append)
  13799. (add-to-list 'tnames (file-truename f) 'append)))
  13800. (multi-occur
  13801. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13802. regexp)))
  13803. (if (boundp 'occur-mode-find-occurrence-hook)
  13804. ;; Emacs 23
  13805. (add-hook 'occur-mode-find-occurrence-hook
  13806. (lambda ()
  13807. (when (org-mode-p)
  13808. (org-reveal))))
  13809. ;; Emacs 22
  13810. (defadvice occur-mode-goto-occurrence
  13811. (after org-occur-reveal activate)
  13812. (and (org-mode-p) (org-reveal)))
  13813. (defadvice occur-mode-goto-occurrence-other-window
  13814. (after org-occur-reveal activate)
  13815. (and (org-mode-p) (org-reveal)))
  13816. (defadvice occur-mode-display-occurrence
  13817. (after org-occur-reveal activate)
  13818. (when (org-mode-p)
  13819. (let ((pos (occur-mode-find-occurrence)))
  13820. (with-current-buffer (marker-buffer pos)
  13821. (save-excursion
  13822. (goto-char pos)
  13823. (org-reveal)))))))
  13824. (defun org-uniquify (list)
  13825. "Remove duplicate elements from LIST."
  13826. (let (res)
  13827. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13828. res))
  13829. (defun org-delete-all (elts list)
  13830. "Remove all elements in ELTS from LIST."
  13831. (while elts
  13832. (setq list (delete (pop elts) list)))
  13833. list)
  13834. (defun org-back-over-empty-lines ()
  13835. "Move backwards over whitespace, to the beginning of the first empty line.
  13836. Returns the number of empty lines passed."
  13837. (let ((pos (point)))
  13838. (skip-chars-backward " \t\n\r")
  13839. (beginning-of-line 2)
  13840. (goto-char (min (point) pos))
  13841. (count-lines (point) pos)))
  13842. (defun org-skip-whitespace ()
  13843. (skip-chars-forward " \t\n\r"))
  13844. (defun org-point-in-group (point group &optional context)
  13845. "Check if POINT is in match-group GROUP.
  13846. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13847. match. If the match group does ot exist or point is not inside it,
  13848. return nil."
  13849. (and (match-beginning group)
  13850. (>= point (match-beginning group))
  13851. (<= point (match-end group))
  13852. (if context
  13853. (list context (match-beginning group) (match-end group))
  13854. t)))
  13855. (defun org-switch-to-buffer-other-window (&rest args)
  13856. "Switch to buffer in a second window on the current frame.
  13857. In particular, do not allow pop-up frames."
  13858. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13859. special-display-function)
  13860. (apply 'switch-to-buffer-other-window args)))
  13861. (defun org-combine-plists (&rest plists)
  13862. "Create a single property list from all plists in PLISTS.
  13863. The process starts by copying the first list, and then setting properties
  13864. from the other lists. Settings in the last list are the most significant
  13865. ones and overrule settings in the other lists."
  13866. (let ((rtn (copy-sequence (pop plists)))
  13867. p v ls)
  13868. (while plists
  13869. (setq ls (pop plists))
  13870. (while ls
  13871. (setq p (pop ls) v (pop ls))
  13872. (setq rtn (plist-put rtn p v))))
  13873. rtn))
  13874. (defun org-move-line-down (arg)
  13875. "Move the current line down. With prefix argument, move it past ARG lines."
  13876. (interactive "p")
  13877. (let ((col (current-column))
  13878. beg end pos)
  13879. (beginning-of-line 1) (setq beg (point))
  13880. (beginning-of-line 2) (setq end (point))
  13881. (beginning-of-line (+ 1 arg))
  13882. (setq pos (move-marker (make-marker) (point)))
  13883. (insert (delete-and-extract-region beg end))
  13884. (goto-char pos)
  13885. (org-move-to-column col)))
  13886. (defun org-move-line-up (arg)
  13887. "Move the current line up. With prefix argument, move it past ARG lines."
  13888. (interactive "p")
  13889. (let ((col (current-column))
  13890. beg end pos)
  13891. (beginning-of-line 1) (setq beg (point))
  13892. (beginning-of-line 2) (setq end (point))
  13893. (beginning-of-line (- arg))
  13894. (setq pos (move-marker (make-marker) (point)))
  13895. (insert (delete-and-extract-region beg end))
  13896. (goto-char pos)
  13897. (org-move-to-column col)))
  13898. (defun org-replace-escapes (string table)
  13899. "Replace %-escapes in STRING with values in TABLE.
  13900. TABLE is an association list with keys like \"%a\" and string values.
  13901. The sequences in STRING may contain normal field width and padding information,
  13902. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13903. so values can contain further %-escapes if they are define later in TABLE."
  13904. (let ((case-fold-search nil)
  13905. e re rpl)
  13906. (while (setq e (pop table))
  13907. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13908. (while (string-match re string)
  13909. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13910. (cdr e)))
  13911. (setq string (replace-match rpl t t string))))
  13912. string))
  13913. (defun org-sublist (list start end)
  13914. "Return a section of LIST, from START to END.
  13915. Counting starts at 1."
  13916. (let (rtn (c start))
  13917. (setq list (nthcdr (1- start) list))
  13918. (while (and list (<= c end))
  13919. (push (pop list) rtn)
  13920. (setq c (1+ c)))
  13921. (nreverse rtn)))
  13922. (defun org-find-base-buffer-visiting (file)
  13923. "Like `find-buffer-visiting' but alway return the base buffer and
  13924. not an indirect buffer."
  13925. (let ((buf (find-buffer-visiting file)))
  13926. (if buf
  13927. (or (buffer-base-buffer buf) buf)
  13928. nil)))
  13929. (defun org-image-file-name-regexp (&optional extensions)
  13930. "Return regexp matching the file names of images.
  13931. If EXTENSIONS is given, only match these."
  13932. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13933. (image-file-name-regexp)
  13934. (let ((image-file-name-extensions
  13935. (or extensions
  13936. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13937. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13938. (concat "\\."
  13939. (regexp-opt (nconc (mapcar 'upcase
  13940. image-file-name-extensions)
  13941. image-file-name-extensions)
  13942. t)
  13943. "\\'"))))
  13944. (defun org-file-image-p (file &optional extensions)
  13945. "Return non-nil if FILE is an image."
  13946. (save-match-data
  13947. (string-match (org-image-file-name-regexp extensions) file)))
  13948. (defun org-get-cursor-date ()
  13949. "Return the date at cursor in as a time.
  13950. This works in the calendar and in the agenda, anywhere else it just
  13951. returns the current time."
  13952. (let (date day defd)
  13953. (cond
  13954. ((eq major-mode 'calendar-mode)
  13955. (setq date (calendar-cursor-to-date)
  13956. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13957. ((eq major-mode 'org-agenda-mode)
  13958. (setq day (get-text-property (point) 'day))
  13959. (if day
  13960. (setq date (calendar-gregorian-from-absolute day)
  13961. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13962. (nth 2 date))))))
  13963. (or defd (current-time))))
  13964. (defvar org-agenda-action-marker (make-marker)
  13965. "Marker pointing to the entry for the next agenda action.")
  13966. (defun org-mark-entry-for-agenda-action ()
  13967. "Mark the current entry as target of an agenda action.
  13968. Agenda actions are actions executed from the agenda with the key `k',
  13969. which make use of the date at the cursor."
  13970. (interactive)
  13971. (move-marker org-agenda-action-marker
  13972. (save-excursion (org-back-to-heading t) (point))
  13973. (current-buffer))
  13974. (message
  13975. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13976. ;;; Paragraph filling stuff.
  13977. ;; We want this to be just right, so use the full arsenal.
  13978. (defun org-indent-line-function ()
  13979. "Indent line like previous, but further if previous was headline or item."
  13980. (interactive)
  13981. (let* ((pos (point))
  13982. (itemp (org-at-item-p))
  13983. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13984. column bpos bcol tpos tcol bullet btype bullet-type)
  13985. ;; Find the previous relevant line
  13986. (beginning-of-line 1)
  13987. (cond
  13988. ((looking-at "#") (setq column 0))
  13989. ((looking-at "\\*+ ") (setq column 0))
  13990. ((and (looking-at "[ \t]*:END:")
  13991. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13992. (save-excursion
  13993. (goto-char (1- (match-beginning 1)))
  13994. (setq column (current-column))))
  13995. (t
  13996. (beginning-of-line 0)
  13997. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13998. (not (looking-at "[ \t]*:END:"))
  13999. (not (looking-at org-drawer-regexp)))
  14000. (beginning-of-line 0))
  14001. (cond
  14002. ((looking-at "\\*+[ \t]+")
  14003. (if (not org-adapt-indentation)
  14004. (setq column 0)
  14005. (goto-char (match-end 0))
  14006. (setq column (current-column))))
  14007. ((looking-at org-drawer-regexp)
  14008. (goto-char (1- (match-beginning 1)))
  14009. (setq column (current-column)))
  14010. ((looking-at "\\([ \t]*\\):END:")
  14011. (goto-char (match-end 1))
  14012. (setq column (current-column)))
  14013. ((org-in-item-p)
  14014. (org-beginning-of-item)
  14015. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14016. (setq bpos (match-beginning 1) tpos (match-end 0)
  14017. bcol (progn (goto-char bpos) (current-column))
  14018. tcol (progn (goto-char tpos) (current-column))
  14019. bullet (match-string 1)
  14020. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14021. (if (> tcol (+ bcol org-description-max-indent))
  14022. (setq tcol (+ bcol 5)))
  14023. (if (not itemp)
  14024. (setq column tcol)
  14025. (goto-char pos)
  14026. (beginning-of-line 1)
  14027. (if (looking-at "\\S-")
  14028. (progn
  14029. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14030. (setq bullet (match-string 1)
  14031. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14032. (setq column (if (equal btype bullet-type) bcol tcol)))
  14033. (setq column (org-get-indentation)))))
  14034. (t (setq column (org-get-indentation))))))
  14035. (goto-char pos)
  14036. (if (<= (current-column) (current-indentation))
  14037. (org-indent-line-to column)
  14038. (save-excursion (org-indent-line-to column)))
  14039. (setq column (current-column))
  14040. (beginning-of-line 1)
  14041. (if (looking-at
  14042. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14043. (replace-match (concat "\\1" (format org-property-format
  14044. (match-string 2) (match-string 3)))
  14045. t nil))
  14046. (org-move-to-column column)))
  14047. (defun org-set-autofill-regexps ()
  14048. (interactive)
  14049. ;; In the paragraph separator we include headlines, because filling
  14050. ;; text in a line directly attached to a headline would otherwise
  14051. ;; fill the headline as well.
  14052. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14053. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14054. ;; The paragraph starter includes hand-formatted lists.
  14055. (org-set-local 'paragraph-start
  14056. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  14057. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14058. ;; But only if the user has not turned off tables or fixed-width regions
  14059. (org-set-local
  14060. 'auto-fill-inhibit-regexp
  14061. (concat "\\*+ \\|#\\+"
  14062. "\\|[ \t]*" org-keyword-time-regexp
  14063. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14064. (concat
  14065. "\\|[ \t]*["
  14066. (if org-enable-table-editor "|" "")
  14067. (if org-enable-fixed-width-editor ":" "")
  14068. "]"))))
  14069. ;; We use our own fill-paragraph function, to make sure that tables
  14070. ;; and fixed-width regions are not wrapped. That function will pass
  14071. ;; through to `fill-paragraph' when appropriate.
  14072. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14073. ; Adaptive filling: To get full control, first make sure that
  14074. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14075. (org-set-local 'adaptive-fill-regexp "\000")
  14076. (org-set-local 'adaptive-fill-function
  14077. 'org-adaptive-fill-function)
  14078. (org-set-local
  14079. 'align-mode-rules-list
  14080. '((org-in-buffer-settings
  14081. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14082. (modes . '(org-mode))))))
  14083. (defun org-fill-paragraph (&optional justify)
  14084. "Re-align a table, pass through to fill-paragraph if no table."
  14085. (let ((table-p (org-at-table-p))
  14086. (table.el-p (org-at-table.el-p)))
  14087. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14088. (save-excursion (goto-char (point-at-bol))
  14089. (looking-at outline-regexp)))
  14090. t) ; skip headlines
  14091. (table.el-p t) ; skip table.el tables
  14092. (table-p (org-table-align) t) ; align org-mode tables
  14093. (t nil)))) ; call paragraph-fill
  14094. ;; For reference, this is the default value of adaptive-fill-regexp
  14095. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14096. (defun org-adaptive-fill-function ()
  14097. "Return a fill prefix for org-mode files.
  14098. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14099. work correctly."
  14100. (cond ((looking-at "#[ \t]+")
  14101. (match-string 0))
  14102. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14103. (save-excursion
  14104. (if (> (match-end 1) (+ (match-beginning 1)
  14105. org-description-max-indent))
  14106. (goto-char (+ (match-beginning 1) 5))
  14107. (goto-char (match-end 0)))
  14108. (make-string (current-column) ?\ )))
  14109. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14110. (save-excursion
  14111. (goto-char (match-end 0))
  14112. (make-string (current-column) ?\ )))
  14113. (t nil)))
  14114. ;;; Other stuff.
  14115. (defun org-toggle-fixed-width-section (arg)
  14116. "Toggle the fixed-width export.
  14117. If there is no active region, the QUOTE keyword at the current headline is
  14118. inserted or removed. When present, it causes the text between this headline
  14119. and the next to be exported as fixed-width text, and unmodified.
  14120. If there is an active region, this command adds or removes a colon as the
  14121. first character of this line. If the first character of a line is a colon,
  14122. this line is also exported in fixed-width font."
  14123. (interactive "P")
  14124. (let* ((cc 0)
  14125. (regionp (org-region-active-p))
  14126. (beg (if regionp (region-beginning) (point)))
  14127. (end (if regionp (region-end)))
  14128. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14129. (case-fold-search nil)
  14130. (re "[ \t]*\\(: \\)")
  14131. off)
  14132. (if regionp
  14133. (save-excursion
  14134. (goto-char beg)
  14135. (setq cc (current-column))
  14136. (beginning-of-line 1)
  14137. (setq off (looking-at re))
  14138. (while (> nlines 0)
  14139. (setq nlines (1- nlines))
  14140. (beginning-of-line 1)
  14141. (cond
  14142. (arg
  14143. (org-move-to-column cc t)
  14144. (insert ": \n")
  14145. (forward-line -1))
  14146. ((and off (looking-at re))
  14147. (replace-match "" t t nil 1))
  14148. ((not off) (org-move-to-column cc t) (insert ": ")))
  14149. (forward-line 1)))
  14150. (save-excursion
  14151. (org-back-to-heading)
  14152. (if (looking-at (concat outline-regexp
  14153. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14154. (replace-match "" t t nil 1)
  14155. (if (looking-at outline-regexp)
  14156. (progn
  14157. (goto-char (match-end 0))
  14158. (insert org-quote-string " "))))))))
  14159. (defun org-reftex-citation ()
  14160. "Use reftex-citation to insert a citation into the buffer.
  14161. This looks for a line like
  14162. #+BIBLIOGRAPHY: foo plain option:-d
  14163. and derives from it that foo.bib is the bbliography file relevant
  14164. for this document. It then installs the necessary environment for RefTeX
  14165. to work in this buffer and calls `reftex-citation' to insert a citation
  14166. into the buffer.
  14167. Export of such citations to both LaTeX and HTML is handled by the contributed
  14168. package org-exp-bibtex by Taru Karttunen."
  14169. (interactive)
  14170. (let ((reftex-docstruct-symbol 'rds)
  14171. (reftex-cite-format "\\cite{%l}")
  14172. rds bib)
  14173. (save-excursion
  14174. (save-restriction
  14175. (widen)
  14176. (let ((case-fold-search t)
  14177. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  14178. (if (not (save-excursion
  14179. (or (re-search-forward re nil t)
  14180. (re-search-backward re nil t))))
  14181. (error "No bibliography defined in file")
  14182. (setq bib (concat (match-string 1) ".bib")
  14183. rds (list (list 'bib bib)))))))
  14184. (call-interactively 'reftex-citation)))
  14185. ;;;; Functions extending outline functionality
  14186. (defun org-beginning-of-line (&optional arg)
  14187. "Go to the beginning of the current line. If that is invisible, continue
  14188. to a visible line beginning. This makes the function of C-a more intuitive.
  14189. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14190. first attempt, and only move to after the tags when the cursor is already
  14191. beyond the end of the headline."
  14192. (interactive "P")
  14193. (let ((pos (point))
  14194. (special (if (consp org-special-ctrl-a/e)
  14195. (car org-special-ctrl-a/e)
  14196. org-special-ctrl-a/e))
  14197. refpos)
  14198. (beginning-of-line 1)
  14199. (if (and arg (fboundp 'move-beginning-of-line))
  14200. (call-interactively 'move-beginning-of-line)
  14201. (if (bobp)
  14202. nil
  14203. (backward-char 1)
  14204. (if (org-invisible-p)
  14205. (while (and (not (bobp)) (org-invisible-p))
  14206. (backward-char 1)
  14207. (beginning-of-line 1))
  14208. (forward-char 1))))
  14209. (when special
  14210. (cond
  14211. ((and (looking-at org-complex-heading-regexp)
  14212. (= (char-after (match-end 1)) ?\ ))
  14213. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  14214. (point-at-eol)))
  14215. (goto-char
  14216. (if (eq special t)
  14217. (cond ((> pos refpos) refpos)
  14218. ((= pos (point)) refpos)
  14219. (t (point)))
  14220. (cond ((> pos (point)) (point))
  14221. ((not (eq last-command this-command)) (point))
  14222. (t refpos)))))
  14223. ((org-at-item-p)
  14224. (goto-char
  14225. (if (eq special t)
  14226. (cond ((> pos (match-end 4)) (match-end 4))
  14227. ((= pos (point)) (match-end 4))
  14228. (t (point)))
  14229. (cond ((> pos (point)) (point))
  14230. ((not (eq last-command this-command)) (point))
  14231. (t (match-end 4))))))))
  14232. (org-no-warnings
  14233. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14234. (defun org-end-of-line (&optional arg)
  14235. "Go to the end of the line.
  14236. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  14237. first attempt, and only move to after the tags when the cursor is already
  14238. beyond the end of the headline."
  14239. (interactive "P")
  14240. (let ((special (if (consp org-special-ctrl-a/e)
  14241. (cdr org-special-ctrl-a/e)
  14242. org-special-ctrl-a/e)))
  14243. (if (or (not special)
  14244. (not (org-on-heading-p))
  14245. arg)
  14246. (call-interactively (if (fboundp 'move-end-of-line)
  14247. 'move-end-of-line
  14248. 'end-of-line))
  14249. (let ((pos (point)))
  14250. (beginning-of-line 1)
  14251. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  14252. (if (eq special t)
  14253. (if (or (< pos (match-beginning 1))
  14254. (= pos (match-end 0)))
  14255. (goto-char (match-beginning 1))
  14256. (goto-char (match-end 0)))
  14257. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  14258. (goto-char (match-end 0))
  14259. (goto-char (match-beginning 1))))
  14260. (call-interactively (if (fboundp 'move-end-of-line)
  14261. 'move-end-of-line
  14262. 'end-of-line)))))
  14263. (org-no-warnings
  14264. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  14265. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  14266. (define-key org-mode-map "\C-e" 'org-end-of-line)
  14267. (defun org-backward-sentence (&optional arg)
  14268. "Go to beginning of sentence, or beginning of table field.
  14269. This will call `backward-sentence' or `org-table-beginning-of-field',
  14270. depending on context."
  14271. (interactive "P")
  14272. (cond
  14273. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  14274. (t (call-interactively 'backward-sentence))))
  14275. (defun org-forward-sentence (&optional arg)
  14276. "Go to end of sentence, or end of table field.
  14277. This will call `forward-sentence' or `org-table-end-of-field',
  14278. depending on context."
  14279. (interactive "P")
  14280. (cond
  14281. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  14282. (t (call-interactively 'forward-sentence))))
  14283. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  14284. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  14285. (defun org-kill-line (&optional arg)
  14286. "Kill line, to tags or end of line."
  14287. (interactive "P")
  14288. (cond
  14289. ((or (not org-special-ctrl-k)
  14290. (bolp)
  14291. (not (org-on-heading-p)))
  14292. (call-interactively 'kill-line))
  14293. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  14294. (kill-region (point) (match-beginning 1))
  14295. (org-set-tags nil t))
  14296. (t (kill-region (point) (point-at-eol)))))
  14297. (define-key org-mode-map "\C-k" 'org-kill-line)
  14298. (defun org-yank (&optional arg)
  14299. "Yank. If the kill is a subtree, treat it specially.
  14300. This command will look at the current kill and check if is a single
  14301. subtree, or a series of subtrees[1]. If it passes the test, and if the
  14302. cursor is at the beginning of a line or after the stars of a currently
  14303. empty headline, then the yank is handled specially. How exactly depends
  14304. on the value of the following variables, both set by default.
  14305. org-yank-folded-subtrees
  14306. When set, the subtree(s) will be folded after insertion, but only
  14307. if doing so would now swallow text after the yanked text.
  14308. org-yank-adjusted-subtrees
  14309. When set, the subtree will be promoted or demoted in order to
  14310. fit into the local outline tree structure, which means that the level
  14311. will be adjusted so that it becomes the smaller one of the two
  14312. *visible* surrounding headings.
  14313. Any prefix to this command will cause `yank' to be called directly with
  14314. no special treatment. In particular, a simple `C-u' prefix will just
  14315. plainly yank the text as it is.
  14316. \[1] Basically, the test checks if the first non-white line is a heading
  14317. and if there are no other headings with fewer stars."
  14318. (interactive "P")
  14319. (setq this-command 'yank)
  14320. (if arg
  14321. (call-interactively 'yank)
  14322. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  14323. (and (org-kill-is-subtree-p)
  14324. (or (bolp)
  14325. (and (looking-at "[ \t]*$")
  14326. (string-match
  14327. "\\`\\*+\\'"
  14328. (buffer-substring (point-at-bol) (point)))))))
  14329. swallowp)
  14330. (cond
  14331. ((and subtreep org-yank-folded-subtrees)
  14332. (let ((beg (point))
  14333. end)
  14334. (if (and subtreep org-yank-adjusted-subtrees)
  14335. (org-paste-subtree nil nil 'for-yank)
  14336. (call-interactively 'yank))
  14337. (setq end (point))
  14338. (goto-char beg)
  14339. (when (and (bolp) subtreep
  14340. (not (setq swallowp
  14341. (org-yank-folding-would-swallow-text beg end))))
  14342. (or (looking-at outline-regexp)
  14343. (re-search-forward (concat "^" outline-regexp) end t))
  14344. (while (and (< (point) end) (looking-at outline-regexp))
  14345. (hide-subtree)
  14346. (org-cycle-show-empty-lines 'folded)
  14347. (condition-case nil
  14348. (outline-forward-same-level 1)
  14349. (error (goto-char end)))))
  14350. (when swallowp
  14351. (message
  14352. "Yanked text not folded because that would swallow text"))
  14353. (goto-char end)
  14354. (skip-chars-forward " \t\n\r")
  14355. (beginning-of-line 1)
  14356. (push-mark beg 'nomsg)))
  14357. ((and subtreep org-yank-adjusted-subtrees)
  14358. (let ((beg (point-at-bol)))
  14359. (org-paste-subtree nil nil 'for-yank)
  14360. (push-mark beg 'nomsg)))
  14361. (t
  14362. (call-interactively 'yank))))))
  14363. (defun org-yank-folding-would-swallow-text (beg end)
  14364. "Would hide-subtree at BEG swallow any text after END?"
  14365. (let (level)
  14366. (save-excursion
  14367. (goto-char beg)
  14368. (when (or (looking-at outline-regexp)
  14369. (re-search-forward (concat "^" outline-regexp) end t))
  14370. (setq level (org-outline-level)))
  14371. (goto-char end)
  14372. (skip-chars-forward " \t\r\n\v\f")
  14373. (if (or (eobp)
  14374. (and (bolp) (looking-at org-outline-regexp)
  14375. (<= (org-outline-level) level)))
  14376. nil ; Nothing would be swallowed
  14377. t)))) ; something would swallow
  14378. (define-key org-mode-map "\C-y" 'org-yank)
  14379. (defun org-invisible-p ()
  14380. "Check if point is at a character currently not visible."
  14381. ;; Early versions of noutline don't have `outline-invisible-p'.
  14382. (if (fboundp 'outline-invisible-p)
  14383. (outline-invisible-p)
  14384. (get-char-property (point) 'invisible)))
  14385. (defun org-invisible-p2 ()
  14386. "Check if point is at a character currently not visible."
  14387. (save-excursion
  14388. (if (and (eolp) (not (bobp))) (backward-char 1))
  14389. ;; Early versions of noutline don't have `outline-invisible-p'.
  14390. (if (fboundp 'outline-invisible-p)
  14391. (outline-invisible-p)
  14392. (get-char-property (point) 'invisible))))
  14393. (defun org-back-to-heading (&optional invisible-ok)
  14394. "Call `outline-back-to-heading', but provide a better error message."
  14395. (condition-case nil
  14396. (outline-back-to-heading invisible-ok)
  14397. (error (error "Before first headline at position %d in buffer %s"
  14398. (point) (current-buffer)))))
  14399. (defun org-before-first-heading-p ()
  14400. "Before first heading?"
  14401. (save-excursion
  14402. (null (re-search-backward "^\\*+ " nil t))))
  14403. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14404. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14405. (defun org-at-heading-or-item-p ()
  14406. (or (org-on-heading-p) (org-at-item-p)))
  14407. (defun org-on-target-p ()
  14408. (or (org-in-regexp org-radio-target-regexp)
  14409. (org-in-regexp org-target-regexp)))
  14410. (defun org-up-heading-all (arg)
  14411. "Move to the heading line of which the present line is a subheading.
  14412. This function considers both visible and invisible heading lines.
  14413. With argument, move up ARG levels."
  14414. (if (fboundp 'outline-up-heading-all)
  14415. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14416. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14417. (defun org-up-heading-safe ()
  14418. "Move to the heading line of which the present line is a subheading.
  14419. This version will not throw an error. It will return the level of the
  14420. headline found, or nil if no higher level is found.
  14421. Also, this function will be a lot faster than `outline-up-heading',
  14422. because it relies on stars being the outline starters. This can really
  14423. make a significant difference in outlines with very many siblings."
  14424. (let (start-level re)
  14425. (org-back-to-heading t)
  14426. (setq start-level (funcall outline-level))
  14427. (if (equal start-level 1)
  14428. nil
  14429. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14430. (if (re-search-backward re nil t)
  14431. (funcall outline-level)))))
  14432. (defun org-first-sibling-p ()
  14433. "Is this heading the first child of its parents?"
  14434. (interactive)
  14435. (let ((re (concat "^" outline-regexp))
  14436. level l)
  14437. (unless (org-at-heading-p t)
  14438. (error "Not at a heading"))
  14439. (setq level (funcall outline-level))
  14440. (save-excursion
  14441. (if (not (re-search-backward re nil t))
  14442. t
  14443. (setq l (funcall outline-level))
  14444. (< l level)))))
  14445. (defun org-goto-sibling (&optional previous)
  14446. "Goto the next sibling, even if it is invisible.
  14447. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14448. when a sibling was found. When none is found, return nil and don't
  14449. move point."
  14450. (let ((fun (if previous 're-search-backward 're-search-forward))
  14451. (pos (point))
  14452. (re (concat "^" outline-regexp))
  14453. level l)
  14454. (when (condition-case nil (org-back-to-heading t) (error nil))
  14455. (setq level (funcall outline-level))
  14456. (catch 'exit
  14457. (or previous (forward-char 1))
  14458. (while (funcall fun re nil t)
  14459. (setq l (funcall outline-level))
  14460. (when (< l level) (goto-char pos) (throw 'exit nil))
  14461. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14462. (goto-char pos)
  14463. nil))))
  14464. (defun org-show-siblings ()
  14465. "Show all siblings of the current headline."
  14466. (save-excursion
  14467. (while (org-goto-sibling) (org-flag-heading nil)))
  14468. (save-excursion
  14469. (while (org-goto-sibling 'previous)
  14470. (org-flag-heading nil))))
  14471. (defun org-show-hidden-entry ()
  14472. "Show an entry where even the heading is hidden."
  14473. (save-excursion
  14474. (org-show-entry)))
  14475. (defun org-flag-heading (flag &optional entry)
  14476. "Flag the current heading. FLAG non-nil means make invisible.
  14477. When ENTRY is non-nil, show the entire entry."
  14478. (save-excursion
  14479. (org-back-to-heading t)
  14480. ;; Check if we should show the entire entry
  14481. (if entry
  14482. (progn
  14483. (org-show-entry)
  14484. (save-excursion
  14485. (and (outline-next-heading)
  14486. (org-flag-heading nil))))
  14487. (outline-flag-region (max (point-min) (1- (point)))
  14488. (save-excursion (outline-end-of-heading) (point))
  14489. flag))))
  14490. (defun org-forward-same-level (arg)
  14491. "Move forward to the ARG'th subheading at same level as this one.
  14492. Stop at the first and last subheadings of a superior heading.
  14493. This is like outline-forward-same-level, but invisible headings are ok."
  14494. (interactive "p")
  14495. (org-back-to-heading t)
  14496. (while (> arg 0)
  14497. (let ((point-to-move-to (save-excursion
  14498. (org-get-next-sibling))))
  14499. (if point-to-move-to
  14500. (progn
  14501. (goto-char point-to-move-to)
  14502. (setq arg (1- arg)))
  14503. (progn
  14504. (setq arg 0)
  14505. (error "No following same-level heading"))))))
  14506. (defun org-get-next-sibling ()
  14507. "Move to next heading of the same level, and return point.
  14508. If there is no such heading, return nil.
  14509. This is like outline-next-sibling, but invisible headings are ok."
  14510. (let ((level (funcall outline-level)))
  14511. (outline-next-heading)
  14512. (while (and (not (eobp)) (> (funcall outline-level) level))
  14513. (outline-next-heading))
  14514. (if (or (eobp) (< (funcall outline-level) level))
  14515. nil
  14516. (point))))
  14517. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14518. ;; This is an exact copy of the original function, but it uses
  14519. ;; `org-back-to-heading', to make it work also in invisible
  14520. ;; trees. And is uses an invisible-OK argument.
  14521. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14522. (org-back-to-heading invisible-OK)
  14523. (let ((first t)
  14524. (level (funcall outline-level)))
  14525. (while (and (not (eobp))
  14526. (or first (> (funcall outline-level) level)))
  14527. (setq first nil)
  14528. (outline-next-heading))
  14529. (unless to-heading
  14530. (if (memq (preceding-char) '(?\n ?\^M))
  14531. (progn
  14532. ;; Go to end of line before heading
  14533. (forward-char -1)
  14534. (if (memq (preceding-char) '(?\n ?\^M))
  14535. ;; leave blank line before heading
  14536. (forward-char -1))))))
  14537. (point))
  14538. (defun org-show-subtree ()
  14539. "Show everything after this heading at deeper levels."
  14540. (outline-flag-region
  14541. (point)
  14542. (save-excursion
  14543. (outline-end-of-subtree) (outline-next-heading) (point))
  14544. nil))
  14545. (defun org-show-entry ()
  14546. "Show the body directly following this heading.
  14547. Show the heading too, if it is currently invisible."
  14548. (interactive)
  14549. (save-excursion
  14550. (condition-case nil
  14551. (progn
  14552. (org-back-to-heading t)
  14553. (outline-flag-region
  14554. (max (point-min) (1- (point)))
  14555. (save-excursion
  14556. (if (re-search-forward
  14557. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14558. (match-beginning 1)
  14559. (point-max)))
  14560. nil))
  14561. (error nil))))
  14562. (defun org-make-options-regexp (kwds &optional extra)
  14563. "Make a regular expression for keyword lines."
  14564. (concat
  14565. "^"
  14566. "#?[ \t]*\\+\\("
  14567. (mapconcat 'regexp-quote kwds "\\|")
  14568. (if extra (concat "\\|" extra))
  14569. "\\):[ \t]*"
  14570. "\\(.+\\)"))
  14571. ;; Make isearch reveal the necessary context
  14572. (defun org-isearch-end ()
  14573. "Reveal context after isearch exits."
  14574. (when isearch-success ; only if search was successful
  14575. (if (featurep 'xemacs)
  14576. ;; Under XEmacs, the hook is run in the correct place,
  14577. ;; we directly show the context.
  14578. (org-show-context 'isearch)
  14579. ;; In Emacs the hook runs *before* restoring the overlays.
  14580. ;; So we have to use a one-time post-command-hook to do this.
  14581. ;; (Emacs 22 has a special variable, see function `org-mode')
  14582. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14583. isearch-mode-end-hook-quit)
  14584. ;; Only when the isearch was not quitted.
  14585. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14586. 'append 'local)))))
  14587. (defun org-isearch-post-command ()
  14588. "Remove self from hook, and show context."
  14589. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14590. (org-show-context 'isearch))
  14591. ;;;; Integration with and fixes for other packages
  14592. ;;; Imenu support
  14593. (defvar org-imenu-markers nil
  14594. "All markers currently used by Imenu.")
  14595. (make-variable-buffer-local 'org-imenu-markers)
  14596. (defun org-imenu-new-marker (&optional pos)
  14597. "Return a new marker for use by Imenu, and remember the marker."
  14598. (let ((m (make-marker)))
  14599. (move-marker m (or pos (point)))
  14600. (push m org-imenu-markers)
  14601. m))
  14602. (defun org-imenu-get-tree ()
  14603. "Produce the index for Imenu."
  14604. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14605. (setq org-imenu-markers nil)
  14606. (let* ((n org-imenu-depth)
  14607. (re (concat "^" outline-regexp))
  14608. (subs (make-vector (1+ n) nil))
  14609. (last-level 0)
  14610. m level head)
  14611. (save-excursion
  14612. (save-restriction
  14613. (widen)
  14614. (goto-char (point-max))
  14615. (while (re-search-backward re nil t)
  14616. (setq level (org-reduced-level (funcall outline-level)))
  14617. (when (<= level n)
  14618. (looking-at org-complex-heading-regexp)
  14619. (setq head (org-link-display-format
  14620. (org-match-string-no-properties 4))
  14621. m (org-imenu-new-marker))
  14622. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14623. (if (>= level last-level)
  14624. (push (cons head m) (aref subs level))
  14625. (push (cons head (aref subs (1+ level))) (aref subs level))
  14626. (loop for i from (1+ level) to n do (aset subs i nil)))
  14627. (setq last-level level)))))
  14628. (aref subs 1)))
  14629. (eval-after-load "imenu"
  14630. '(progn
  14631. (add-hook 'imenu-after-jump-hook
  14632. (lambda ()
  14633. (if (eq major-mode 'org-mode)
  14634. (org-show-context 'org-goto))))))
  14635. (defun org-link-display-format (link)
  14636. "Replace a link with either the description, or the link target
  14637. if no description is present"
  14638. (save-match-data
  14639. (if (string-match org-bracket-link-analytic-regexp link)
  14640. (replace-match (or (match-string 5 link)
  14641. (concat (match-string 1 link)
  14642. (match-string 3 link)))
  14643. nil nil link)
  14644. link)))
  14645. ;; Speedbar support
  14646. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14647. "Overlay marking the agenda restriction line in speedbar.")
  14648. (org-overlay-put org-speedbar-restriction-lock-overlay
  14649. 'face 'org-agenda-restriction-lock)
  14650. (org-overlay-put org-speedbar-restriction-lock-overlay
  14651. 'help-echo "Agendas are currently limited to this item.")
  14652. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14653. (defun org-speedbar-set-agenda-restriction ()
  14654. "Restrict future agenda commands to the location at point in speedbar.
  14655. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14656. (interactive)
  14657. (require 'org-agenda)
  14658. (let (p m tp np dir txt)
  14659. (cond
  14660. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14661. 'org-imenu t))
  14662. (setq m (get-text-property p 'org-imenu-marker))
  14663. (save-excursion
  14664. (save-restriction
  14665. (set-buffer (marker-buffer m))
  14666. (goto-char m)
  14667. (org-agenda-set-restriction-lock 'subtree))))
  14668. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14669. 'speedbar-function 'speedbar-find-file))
  14670. (setq tp (previous-single-property-change
  14671. (1+ p) 'speedbar-function)
  14672. np (next-single-property-change
  14673. tp 'speedbar-function)
  14674. dir (speedbar-line-directory)
  14675. txt (buffer-substring-no-properties (or tp (point-min))
  14676. (or np (point-max))))
  14677. (save-excursion
  14678. (save-restriction
  14679. (set-buffer (find-file-noselect
  14680. (let ((default-directory dir))
  14681. (expand-file-name txt))))
  14682. (unless (org-mode-p)
  14683. (error "Cannot restrict to non-Org-mode file"))
  14684. (org-agenda-set-restriction-lock 'file))))
  14685. (t (error "Don't know how to restrict Org-mode's agenda")))
  14686. (org-move-overlay org-speedbar-restriction-lock-overlay
  14687. (point-at-bol) (point-at-eol))
  14688. (setq current-prefix-arg nil)
  14689. (org-agenda-maybe-redo)))
  14690. (eval-after-load "speedbar"
  14691. '(progn
  14692. (speedbar-add-supported-extension ".org")
  14693. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14694. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14695. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14696. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14697. (add-hook 'speedbar-visiting-tag-hook
  14698. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14699. ;;; Fixes and Hacks for problems with other packages
  14700. ;; Make flyspell not check words in links, to not mess up our keymap
  14701. (defun org-mode-flyspell-verify ()
  14702. "Don't let flyspell put overlays at active buttons."
  14703. (not (get-text-property (point) 'keymap)))
  14704. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14705. (eval-after-load "bookmark"
  14706. '(if (boundp 'bookmark-after-jump-hook)
  14707. ;; We can use the hook
  14708. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14709. ;; Hook not available, use advice
  14710. (defadvice bookmark-jump (after org-make-visible activate)
  14711. "Make the position visible."
  14712. (org-bookmark-jump-unhide))))
  14713. ;; Make sure saveplace show the location if it was hidden
  14714. (eval-after-load "saveplace"
  14715. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14716. "Make the position visible."
  14717. (org-bookmark-jump-unhide)))
  14718. (defun org-bookmark-jump-unhide ()
  14719. "Unhide the current position, to show the bookmark location."
  14720. (and (org-mode-p)
  14721. (or (org-invisible-p)
  14722. (save-excursion (goto-char (max (point-min) (1- (point))))
  14723. (org-invisible-p)))
  14724. (org-show-context 'bookmark-jump)))
  14725. ;; Make session.el ignore our circular variable
  14726. (eval-after-load "session"
  14727. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14728. ;;;; Experimental code
  14729. (defun org-closed-in-range ()
  14730. "Sparse tree of items closed in a certain time range.
  14731. Still experimental, may disappear in the future."
  14732. (interactive)
  14733. ;; Get the time interval from the user.
  14734. (let* ((time1 (time-to-seconds
  14735. (org-read-date nil 'to-time nil "Starting date: ")))
  14736. (time2 (time-to-seconds
  14737. (org-read-date nil 'to-time nil "End date:")))
  14738. ;; callback function
  14739. (callback (lambda ()
  14740. (let ((time
  14741. (time-to-seconds
  14742. (apply 'encode-time
  14743. (org-parse-time-string
  14744. (match-string 1))))))
  14745. ;; check if time in interval
  14746. (and (>= time time1) (<= time time2))))))
  14747. ;; make tree, check each match with the callback
  14748. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14749. ;;;; Finish up
  14750. (provide 'org)
  14751. (run-hooks 'org-load-hook)
  14752. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14753. ;;; org.el ends here