org.el 562 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198
  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 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.00pre-3
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 3, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  65. (defvar org-table-formula-constants-local nil
  66. "Local version of `org-table-formula-constants'.")
  67. (make-variable-buffer-local 'org-table-formula-constants-local)
  68. ;;;; Require other packages
  69. (eval-when-compile
  70. (require 'cl)
  71. (require 'gnus-sum)
  72. (require 'calendar))
  73. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  74. ;; the file noutline.el being loaded.
  75. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  76. ;; We require noutline, which might be provided in outline.el
  77. (require 'outline) (require 'noutline)
  78. ;; Other stuff we need.
  79. (require 'time-date)
  80. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  81. (require 'easymenu)
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.00pre-3"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-default-extensions'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-infojs org-irc org-mew org-mhe org-rmail org-vm org-wl)
  125. "Modules that should always be loaded together with org.el.
  126. If a description starts with <C>, the file is not part of emacs
  127. and loading it will require that you have downloaded and properly installed
  128. the org-mode distribution.
  129. You can also use this system to load external packages (i.e. neither Org
  130. core modules, not modules from the CONTRIB directory). Just add symbols
  131. to the end of the list. If the package is called org-xyz.e, then you need
  132. to add the symbol `xyz', and the package must have a call to
  133. (provide 'org-xyz)"
  134. :group 'org
  135. :set 'org-set-modules
  136. :type
  137. '(set :greedy t
  138. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  139. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  140. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  141. (const :tag " info: Links to Info nodes" org-info)
  142. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  143. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  144. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  145. (const :tag " mew Links to Mew folders/messages" org-mew)
  146. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  147. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  148. (const :tag " vm: Links to VM folders/messages" org-vm)
  149. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  150. (const :tag " mouse: Additional mouse support" org-mouse)
  151. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  152. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  153. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  154. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  155. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  156. (const :tag "C id: Global id's for identifying entries" org-id)
  157. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  158. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  159. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  160. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  161. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  162. (const :tag "C registry: A registry for Org links" org-registry)
  163. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  164. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  165. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  166. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  167. (defgroup org-startup nil
  168. "Options concerning startup of Org-mode."
  169. :tag "Org Startup"
  170. :group 'org)
  171. (defcustom org-startup-folded t
  172. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  173. This can also be configured on a per-file basis by adding one of
  174. the following lines anywhere in the buffer:
  175. #+STARTUP: fold
  176. #+STARTUP: nofold
  177. #+STARTUP: content"
  178. :group 'org-startup
  179. :type '(choice
  180. (const :tag "nofold: show all" nil)
  181. (const :tag "fold: overview" t)
  182. (const :tag "content: all headlines" content)))
  183. (defcustom org-startup-truncated t
  184. "Non-nil means, entering Org-mode will set `truncate-lines'.
  185. This is useful since some lines containing links can be very long and
  186. uninteresting. Also tables look terrible when wrapped."
  187. :group 'org-startup
  188. :type 'boolean)
  189. (defcustom org-startup-align-all-tables nil
  190. "Non-nil means, align all tables when visiting a file.
  191. This is useful when the column width in tables is forced with <N> cookies
  192. in table fields. Such tables will look correct only after the first re-align.
  193. This can also be configured on a per-file basis by adding one of
  194. the following lines anywhere in the buffer:
  195. #+STARTUP: align
  196. #+STARTUP: noalign"
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-insert-mode-line-in-empty-file nil
  200. "Non-nil means insert the first line setting Org-mode in empty files.
  201. When the function `org-mode' is called interactively in an empty file, this
  202. normally means that the file name does not automatically trigger Org-mode.
  203. To ensure that the file will always be in Org-mode in the future, a
  204. line enforcing Org-mode will be inserted into the buffer, if this option
  205. has been set."
  206. :group 'org-startup
  207. :type 'boolean)
  208. (defcustom org-replace-disputed-keys nil
  209. "Non-nil means use alternative key bindings for some keys.
  210. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  211. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  212. If you want to use Org-mode together with one of these other modes,
  213. or more generally if you would like to move some Org-mode commands to
  214. other keys, set this variable and configure the keys with the variable
  215. `org-disputed-keys'.
  216. This option is only relevant at load-time of Org-mode, and must be set
  217. *before* org.el is loaded. Changing it requires a restart of Emacs to
  218. become effective."
  219. :group 'org-startup
  220. :type 'boolean)
  221. (if (fboundp 'defvaralias)
  222. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  223. (defcustom org-disputed-keys
  224. '(([(shift up)] . [(meta p)])
  225. ([(shift down)] . [(meta n)])
  226. ([(shift left)] . [(meta -)])
  227. ([(shift right)] . [(meta +)])
  228. ([(control shift right)] . [(meta shift +)])
  229. ([(control shift left)] . [(meta shift -)]))
  230. "Keys for which Org-mode and other modes compete.
  231. This is an alist, cars are the default keys, second element specifies
  232. the alternative to use when `org-replace-disputed-keys' is t.
  233. Keys can be specified in any syntax supported by `define-key'.
  234. The value of this option takes effect only at Org-mode's startup,
  235. therefore you'll have to restart Emacs to apply it after changing."
  236. :group 'org-startup
  237. :type 'alist)
  238. (defun org-key (key)
  239. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  240. Or return the original if not disputed."
  241. (if org-replace-disputed-keys
  242. (let* ((nkey (key-description key))
  243. (x (org-find-if (lambda (x)
  244. (equal (key-description (car x)) nkey))
  245. org-disputed-keys)))
  246. (if x (cdr x) key))
  247. key))
  248. (defun org-find-if (predicate seq)
  249. (catch 'exit
  250. (while seq
  251. (if (funcall predicate (car seq))
  252. (throw 'exit (car seq))
  253. (pop seq)))))
  254. (defun org-defkey (keymap key def)
  255. "Define a key, possibly translated, as returned by `org-key'."
  256. (define-key keymap (org-key key) def))
  257. (defcustom org-ellipsis nil
  258. "The ellipsis to use in the Org-mode outline.
  259. When nil, just use the standard three dots. When a string, use that instead,
  260. When a face, use the standart 3 dots, but with the specified face.
  261. The change affects only Org-mode (which will then use its own display table).
  262. Changing this requires executing `M-x org-mode' in a buffer to become
  263. effective."
  264. :group 'org-startup
  265. :type '(choice (const :tag "Default" nil)
  266. (face :tag "Face" :value org-warning)
  267. (string :tag "String" :value "...#")))
  268. (defvar org-display-table nil
  269. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  270. (defgroup org-keywords nil
  271. "Keywords in Org-mode."
  272. :tag "Org Keywords"
  273. :group 'org)
  274. (defcustom org-deadline-string "DEADLINE:"
  275. "String to mark deadline entries.
  276. A deadline is this string, followed by a time stamp. Should be a word,
  277. terminated by a colon. You can insert a schedule keyword and
  278. a timestamp with \\[org-deadline].
  279. Changes become only effective after restarting Emacs."
  280. :group 'org-keywords
  281. :type 'string)
  282. (defcustom org-scheduled-string "SCHEDULED:"
  283. "String to mark scheduled TODO entries.
  284. A schedule is this string, followed by a time stamp. Should be a word,
  285. terminated by a colon. You can insert a schedule keyword and
  286. a timestamp with \\[org-schedule].
  287. Changes become only effective after restarting Emacs."
  288. :group 'org-keywords
  289. :type 'string)
  290. (defcustom org-closed-string "CLOSED:"
  291. "String used as the prefix for timestamps logging closing a TODO entry."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-clock-string "CLOCK:"
  295. "String used as prefix for timestamps clocking work hours on an item."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-comment-string "COMMENT"
  299. "Entries starting with this keyword will never be exported.
  300. An entry can be toggled between COMMENT and normal with
  301. \\[org-toggle-comment].
  302. Changes become only effective after restarting Emacs."
  303. :group 'org-keywords
  304. :type 'string)
  305. (defcustom org-quote-string "QUOTE"
  306. "Entries starting with this keyword will be exported in fixed-width font.
  307. Quoting applies only to the text in the entry following the headline, and does
  308. not extend beyond the next headline, even if that is lower level.
  309. An entry can be toggled between QUOTE and normal with
  310. \\[org-toggle-fixed-width-section]."
  311. :group 'org-keywords
  312. :type 'string)
  313. (defconst org-repeat-re
  314. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  315. "Regular expression for specifying repeated events.
  316. After a match, group 1 contains the repeat expression.")
  317. (defgroup org-structure nil
  318. "Options concerning the general structure of Org-mode files."
  319. :tag "Org Structure"
  320. :group 'org)
  321. (defgroup org-reveal-location nil
  322. "Options about how to make context of a location visible."
  323. :tag "Org Reveal Location"
  324. :group 'org-structure)
  325. (defconst org-context-choice
  326. '(choice
  327. (const :tag "Always" t)
  328. (const :tag "Never" nil)
  329. (repeat :greedy t :tag "Individual contexts"
  330. (cons
  331. (choice :tag "Context"
  332. (const agenda)
  333. (const org-goto)
  334. (const occur-tree)
  335. (const tags-tree)
  336. (const link-search)
  337. (const mark-goto)
  338. (const bookmark-jump)
  339. (const isearch)
  340. (const default))
  341. (boolean))))
  342. "Contexts for the reveal options.")
  343. (defcustom org-show-hierarchy-above '((default . t))
  344. "Non-nil means, show full hierarchy when revealing a location.
  345. Org-mode often shows locations in an org-mode file which might have
  346. been invisible before. When this is set, the hierarchy of headings
  347. above the exposed location is shown.
  348. Turning this off for example for sparse trees makes them very compact.
  349. Instead of t, this can also be an alist specifying this option for different
  350. contexts. Valid contexts are
  351. agenda when exposing an entry from the agenda
  352. org-goto when using the command `org-goto' on key C-c C-j
  353. occur-tree when using the command `org-occur' on key C-c /
  354. tags-tree when constructing a sparse tree based on tags matches
  355. link-search when exposing search matches associated with a link
  356. mark-goto when exposing the jump goal of a mark
  357. bookmark-jump when exposing a bookmark location
  358. isearch when exiting from an incremental search
  359. default default for all contexts not set explicitly"
  360. :group 'org-reveal-location
  361. :type org-context-choice)
  362. (defcustom org-show-following-heading '((default . nil))
  363. "Non-nil means, show following heading when revealing a location.
  364. Org-mode often shows locations in an org-mode file which might have
  365. been invisible before. When this is set, the heading following the
  366. match is shown.
  367. Turning this off for example for sparse trees makes them very compact,
  368. but makes it harder to edit the location of the match. In such a case,
  369. use the command \\[org-reveal] to show more context.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. See `org-show-hierarchy-above' for valid contexts."
  372. :group 'org-reveal-location
  373. :type org-context-choice)
  374. (defcustom org-show-siblings '((default . nil) (isearch t))
  375. "Non-nil means, show all sibling heading when revealing a location.
  376. Org-mode often shows locations in an org-mode file which might have
  377. been invisible before. When this is set, the sibling of the current entry
  378. heading are all made visible. If `org-show-hierarchy-above' is t,
  379. the same happens on each level of the hierarchy above the current entry.
  380. By default this is on for the isearch context, off for all other contexts.
  381. Turning this off for example for sparse trees makes them very compact,
  382. but makes it harder to edit the location of the match. In such a case,
  383. use the command \\[org-reveal] to show more context.
  384. Instead of t, this can also be an alist specifying this option for different
  385. contexts. See `org-show-hierarchy-above' for valid contexts."
  386. :group 'org-reveal-location
  387. :type org-context-choice)
  388. (defcustom org-show-entry-below '((default . nil))
  389. "Non-nil means, show the entry below a headline when revealing a location.
  390. Org-mode often shows locations in an org-mode file which might have
  391. been invisible before. When this is set, the text below the headline that is
  392. exposed is also shown.
  393. By default this is off for all contexts.
  394. Instead of t, this can also be an alist specifying this option for different
  395. contexts. See `org-show-hierarchy-above' for valid contexts."
  396. :group 'org-reveal-location
  397. :type org-context-choice)
  398. (defcustom org-indirect-buffer-display 'other-window
  399. "How should indirect tree buffers be displayed?
  400. This applies to indirect buffers created with the commands
  401. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  402. Valid values are:
  403. current-window Display in the current window
  404. other-window Just display in another window.
  405. dedicated-frame Create one new frame, and re-use it each time.
  406. new-frame Make a new frame each time. Note that in this case
  407. previously-made indirect buffers are kept, and you need to
  408. kill these buffers yourself."
  409. :group 'org-structure
  410. :group 'org-agenda-windows
  411. :type '(choice
  412. (const :tag "In current window" current-window)
  413. (const :tag "In current frame, other window" other-window)
  414. (const :tag "Each time a new frame" new-frame)
  415. (const :tag "One dedicated frame" dedicated-frame)))
  416. (defgroup org-cycle nil
  417. "Options concerning visibility cycling in Org-mode."
  418. :tag "Org Cycle"
  419. :group 'org-structure)
  420. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  421. "Names of drawers. Drawers are not opened by cycling on the headline above.
  422. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  423. this:
  424. :DRAWERNAME:
  425. .....
  426. :END:
  427. The drawer \"PROPERTIES\" is special for capturing properties through
  428. the property API.
  429. Drawers can be defined on the per-file basis with a line like:
  430. #+DRAWERS: HIDDEN STATE PROPERTIES"
  431. :group 'org-structure
  432. :type '(repeat (string :tag "Drawer Name")))
  433. (defcustom org-cycle-global-at-bob nil
  434. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  435. This makes it possible to do global cycling without having to use S-TAB or
  436. C-u TAB. For this special case to work, the first line of the buffer
  437. must not be a headline - it may be empty ot some other text. When used in
  438. this way, `org-cycle-hook' is disables temporarily, to make sure the
  439. cursor stays at the beginning of the buffer.
  440. When this option is nil, don't do anything special at the beginning
  441. of the buffer."
  442. :group 'org-cycle
  443. :type 'boolean)
  444. (defcustom org-cycle-emulate-tab t
  445. "Where should `org-cycle' emulate TAB.
  446. nil Never
  447. white Only in completely white lines
  448. whitestart Only at the beginning of lines, before the first non-white char
  449. t Everywhere except in headlines
  450. exc-hl-bol Everywhere except at the start of a headline
  451. If TAB is used in a place where it does not emulate TAB, the current subtree
  452. visibility is cycled."
  453. :group 'org-cycle
  454. :type '(choice (const :tag "Never" nil)
  455. (const :tag "Only in completely white lines" white)
  456. (const :tag "Before first char in a line" whitestart)
  457. (const :tag "Everywhere except in headlines" t)
  458. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  459. ))
  460. (defcustom org-cycle-separator-lines 2
  461. "Number of empty lines needed to keep an empty line between collapsed trees.
  462. If you leave an empty line between the end of a subtree and the following
  463. headline, this empty line is hidden when the subtree is folded.
  464. Org-mode will leave (exactly) one empty line visible if the number of
  465. empty lines is equal or larger to the number given in this variable.
  466. So the default 2 means, at least 2 empty lines after the end of a subtree
  467. are needed to produce free space between a collapsed subtree and the
  468. following headline.
  469. Special case: when 0, never leave empty lines in collapsed view."
  470. :group 'org-cycle
  471. :type 'integer)
  472. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  473. org-cycle-hide-drawers
  474. org-cycle-show-empty-lines
  475. org-optimize-window-after-visibility-change)
  476. "Hook that is run after `org-cycle' has changed the buffer visibility.
  477. The function(s) in this hook must accept a single argument which indicates
  478. the new state that was set by the most recent `org-cycle' command. The
  479. argument is a symbol. After a global state change, it can have the values
  480. `overview', `content', or `all'. After a local state change, it can have
  481. the values `folded', `children', or `subtree'."
  482. :group 'org-cycle
  483. :type 'hook)
  484. (defgroup org-edit-structure nil
  485. "Options concerning structure editing in Org-mode."
  486. :tag "Org Edit Structure"
  487. :group 'org-structure)
  488. (defcustom org-odd-levels-only nil
  489. "Non-nil means, skip even levels and only use odd levels for the outline.
  490. This has the effect that two stars are being added/taken away in
  491. promotion/demotion commands. It also influences how levels are
  492. handled by the exporters.
  493. Changing it requires restart of `font-lock-mode' to become effective
  494. for fontification also in regions already fontified.
  495. You may also set this on a per-file basis by adding one of the following
  496. lines to the buffer:
  497. #+STARTUP: odd
  498. #+STARTUP: oddeven"
  499. :group 'org-edit-structure
  500. :group 'org-font-lock
  501. :type 'boolean)
  502. (defcustom org-adapt-indentation t
  503. "Non-nil means, adapt indentation when promoting and demoting.
  504. When this is set and the *entire* text in an entry is indented, the
  505. indentation is increased by one space in a demotion command, and
  506. decreased by one in a promotion command. If any line in the entry
  507. body starts at column 0, indentation is not changed at all."
  508. :group 'org-edit-structure
  509. :type 'boolean)
  510. (defcustom org-special-ctrl-a/e nil
  511. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  512. When t, `C-a' will bring back the cursor to the beginning of the
  513. headline text, i.e. after the stars and after a possible TODO keyword.
  514. In an item, this will be the position after the bullet.
  515. When the cursor is already at that position, another `C-a' will bring
  516. it to the beginning of the line.
  517. `C-e' will jump to the end of the headline, ignoring the presence of tags
  518. in the headline. A second `C-e' will then jump to the true end of the
  519. line, after any tags.
  520. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  521. and only a directly following, identical keypress will bring the cursor
  522. to the special positions."
  523. :group 'org-edit-structure
  524. :type '(choice
  525. (const :tag "off" nil)
  526. (const :tag "after bullet first" t)
  527. (const :tag "border first" reversed)))
  528. (if (fboundp 'defvaralias)
  529. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  530. (defcustom org-special-ctrl-k nil
  531. "Non-nil means `C-k' will behave specially in headlines.
  532. When nil, `C-k' will call the default `kill-line' command.
  533. When t, the following will happen while the cursor is in the headline:
  534. - When the cursor is at the beginning of a headline, kill the entire
  535. line and possible the folded subtree below the line.
  536. - When in the middle of the headline text, kill the headline up to the tags.
  537. - When after the headline text, kill the tags."
  538. :group 'org-edit-structure
  539. :type 'boolean)
  540. (defcustom org-M-RET-may-split-line '((default . t))
  541. "Non-nil means, M-RET will split the line at the cursor position.
  542. When nil, it will go to the end of the line before making a
  543. new line.
  544. You may also set this option in a different way for different
  545. contexts. Valid contexts are:
  546. headline when creating a new headline
  547. item when creating a new item
  548. table in a table field
  549. default the value to be used for all contexts not explicitly
  550. customized"
  551. :group 'org-structure
  552. :group 'org-table
  553. :type '(choice
  554. (const :tag "Always" t)
  555. (const :tag "Never" nil)
  556. (repeat :greedy t :tag "Individual contexts"
  557. (cons
  558. (choice :tag "Context"
  559. (const headline)
  560. (const item)
  561. (const table)
  562. (const default))
  563. (boolean)))))
  564. (defcustom org-blank-before-new-entry '((heading . nil)
  565. (plain-list-item . nil))
  566. "Should `org-insert-heading' leave a blank line before new heading/item?
  567. The value is an alist, with `heading' and `plain-list-item' as car,
  568. and a boolean flag as cdr."
  569. :group 'org-edit-structure
  570. :type '(list
  571. (cons (const heading) (boolean))
  572. (cons (const plain-list-item) (boolean))))
  573. (defcustom org-insert-heading-hook nil
  574. "Hook being run after inserting a new heading."
  575. :group 'org-edit-structure
  576. :type 'hook)
  577. (defcustom org-enable-fixed-width-editor t
  578. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  579. This currently only means, they are never auto-wrapped.
  580. When nil, such lines will be treated like ordinary lines.
  581. See also the QUOTE keyword."
  582. :group 'org-edit-structure
  583. :type 'boolean)
  584. (defcustom org-goto-auto-isearch t
  585. "Non-nil means, typing characters in org-goto starts incremental search."
  586. :group 'org-edit-structure
  587. :type 'boolean)
  588. (defgroup org-sparse-trees nil
  589. "Options concerning sparse trees in Org-mode."
  590. :tag "Org Sparse Trees"
  591. :group 'org-structure)
  592. (defcustom org-highlight-sparse-tree-matches t
  593. "Non-nil means, highlight all matches that define a sparse tree.
  594. The highlights will automatically disappear the next time the buffer is
  595. changed by an edit command."
  596. :group 'org-sparse-trees
  597. :type 'boolean)
  598. (defcustom org-remove-highlights-with-change t
  599. "Non-nil means, any change to the buffer will remove temporary highlights.
  600. Such highlights are created by `org-occur' and `org-clock-display'.
  601. When nil, `C-c C-c needs to be used to get rid of the highlights.
  602. The highlights created by `org-preview-latex-fragment' always need
  603. `C-c C-c' to be removed."
  604. :group 'org-sparse-trees
  605. :group 'org-time
  606. :type 'boolean)
  607. (defcustom org-occur-hook '(org-first-headline-recenter)
  608. "Hook that is run after `org-occur' has constructed a sparse tree.
  609. This can be used to recenter the window to show as much of the structure
  610. as possible."
  611. :group 'org-sparse-trees
  612. :type 'hook)
  613. (defgroup org-plain-lists nil
  614. "Options concerning plain lists in Org-mode."
  615. :tag "Org Plain lists"
  616. :group 'org-structure)
  617. (defcustom org-cycle-include-plain-lists nil
  618. "Non-nil means, include plain lists into visibility cycling.
  619. This means that during cycling, plain list items will *temporarily* be
  620. interpreted as outline headlines with a level given by 1000+i where i is the
  621. indentation of the bullet. In all other operations, plain list items are
  622. not seen as headlines. For example, you cannot assign a TODO keyword to
  623. such an item."
  624. :group 'org-plain-lists
  625. :type 'boolean)
  626. (defcustom org-plain-list-ordered-item-terminator t
  627. "The character that makes a line with leading number an ordered list item.
  628. Valid values are ?. and ?\). To get both terminators, use t. While
  629. ?. may look nicer, it creates the danger that a line with leading
  630. number may be incorrectly interpreted as an item. ?\) therefore is
  631. the safe choice."
  632. :group 'org-plain-lists
  633. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  634. (const :tag "paren like in \"2)\"" ?\))
  635. (const :tab "both" t)))
  636. (defcustom org-auto-renumber-ordered-lists t
  637. "Non-nil means, automatically renumber ordered plain lists.
  638. Renumbering happens when the sequence have been changed with
  639. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  640. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  641. :group 'org-plain-lists
  642. :type 'boolean)
  643. (defcustom org-provide-checkbox-statistics t
  644. "Non-nil means, update checkbox statistics after insert and toggle.
  645. When this is set, checkbox statistics is updated each time you either insert
  646. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  647. with \\[org-ctrl-c-ctrl-c\\]."
  648. :group 'org-plain-lists
  649. :type 'boolean)
  650. (defgroup org-archive nil
  651. "Options concerning archiving in Org-mode."
  652. :tag "Org Archive"
  653. :group 'org-structure)
  654. (defcustom org-archive-tag "ARCHIVE"
  655. "The tag that marks a subtree as archived.
  656. An archived subtree does not open during visibility cycling, and does
  657. not contribute to the agenda listings.
  658. After changing this, font-lock must be restarted in the relevant buffers to
  659. get the proper fontification."
  660. :group 'org-archive
  661. :group 'org-keywords
  662. :type 'string)
  663. (defcustom org-agenda-skip-archived-trees t
  664. "Non-nil means, the agenda will skip any items located in archived trees.
  665. An archived tree is a tree marked with the tag ARCHIVE."
  666. :group 'org-archive
  667. :group 'org-agenda-skip
  668. :type 'boolean)
  669. (defcustom org-cycle-open-archived-trees nil
  670. "Non-nil means, `org-cycle' will open archived trees.
  671. An archived tree is a tree marked with the tag ARCHIVE.
  672. When nil, archived trees will stay folded. You can still open them with
  673. normal outline commands like `show-all', but not with the cycling commands."
  674. :group 'org-archive
  675. :group 'org-cycle
  676. :type 'boolean)
  677. (defcustom org-sparse-tree-open-archived-trees nil
  678. "Non-nil means sparse tree construction shows matches in archived trees.
  679. When nil, matches in these trees are highlighted, but the trees are kept in
  680. collapsed state."
  681. :group 'org-archive
  682. :group 'org-sparse-trees
  683. :type 'boolean)
  684. (defcustom org-archive-location "%s_archive::"
  685. "The location where subtrees should be archived.
  686. This string consists of two parts, separated by a double-colon.
  687. The first part is a file name - when omitted, archiving happens in the same
  688. file. %s will be replaced by the current file name (without directory part).
  689. Archiving to a different file is useful to keep archived entries from
  690. contributing to the Org-mode Agenda.
  691. The part after the double colon is a headline. The archived entries will be
  692. filed under that headline. When omitted, the subtrees are simply filed away
  693. at the end of the file, as top-level entries.
  694. Here are a few examples:
  695. \"%s_archive::\"
  696. If the current file is Projects.org, archive in file
  697. Projects.org_archive, as top-level trees. This is the default.
  698. \"::* Archived Tasks\"
  699. Archive in the current file, under the top-level headline
  700. \"* Archived Tasks\".
  701. \"~/org/archive.org::\"
  702. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  703. \"basement::** Finished Tasks\"
  704. Archive in file ./basement (relative path), as level 3 trees
  705. below the level 2 heading \"** Finished Tasks\".
  706. You may set this option on a per-file basis by adding to the buffer a
  707. line like
  708. #+ARCHIVE: basement::** Finished Tasks"
  709. :group 'org-archive
  710. :type 'string)
  711. (defcustom org-archive-mark-done t
  712. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  713. This can be a string to set the keyword to use. When t, Org-mode will
  714. use the first keyword in its list that means done."
  715. :group 'org-archive
  716. :type '(choice
  717. (const :tag "No" nil)
  718. (const :tag "Yes" t)
  719. (string :tag "Use this keyword")))
  720. (defcustom org-archive-stamp-time t
  721. "Non-nil means, add a time stamp to entries moved to an archive file.
  722. This variable is obsolete and has no effect anymore, instead add ot remove
  723. `time' from the variablle `org-archive-save-context-info'."
  724. :group 'org-archive
  725. :type 'boolean)
  726. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  727. "Parts of context info that should be stored as properties when archiving.
  728. When a subtree is moved to an archive file, it looses information given by
  729. context, like inherited tags, the category, and possibly also the TODO
  730. state (depending on the variable `org-archive-mark-done').
  731. This variable can be a list of any of the following symbols:
  732. time The time of archiving.
  733. file The file where the entry originates.
  734. itags The local tags, in the headline of the subtree.
  735. ltags The tags the subtree inherits from further up the hierarchy.
  736. todo The pre-archive TODO state.
  737. category The category, taken from file name or #+CATEGORY lines.
  738. olpath The outline path to the item. These are all headlines above
  739. the current item, separated by /, like a file path.
  740. For each symbol present in the list, a property will be created in
  741. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  742. information."
  743. :group 'org-archive
  744. :type '(set :greedy t
  745. (const :tag "Time" time)
  746. (const :tag "File" file)
  747. (const :tag "Category" category)
  748. (const :tag "TODO state" todo)
  749. (const :tag "TODO state" priority)
  750. (const :tag "Inherited tags" itags)
  751. (const :tag "Outline path" olpath)
  752. (const :tag "Local tags" ltags)))
  753. (defgroup org-imenu-and-speedbar nil
  754. "Options concerning imenu and speedbar in Org-mode."
  755. :tag "Org Imenu and Speedbar"
  756. :group 'org-structure)
  757. (defcustom org-imenu-depth 2
  758. "The maximum level for Imenu access to Org-mode headlines.
  759. This also applied for speedbar access."
  760. :group 'org-imenu-and-speedbar
  761. :type 'number)
  762. (defgroup org-table nil
  763. "Options concerning tables in Org-mode."
  764. :tag "Org Table"
  765. :group 'org)
  766. (defcustom org-enable-table-editor 'optimized
  767. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  768. When nil, such lines will be treated like ordinary lines.
  769. When equal to the symbol `optimized', the table editor will be optimized to
  770. do the following:
  771. - Automatic overwrite mode in front of whitespace in table fields.
  772. This makes the structure of the table stay in tact as long as the edited
  773. field does not exceed the column width.
  774. - Minimize the number of realigns. Normally, the table is aligned each time
  775. TAB or RET are pressed to move to another field. With optimization this
  776. happens only if changes to a field might have changed the column width.
  777. Optimization requires replacing the functions `self-insert-command',
  778. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  779. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  780. very good at guessing when a re-align will be necessary, but you can always
  781. force one with \\[org-ctrl-c-ctrl-c].
  782. If you would like to use the optimized version in Org-mode, but the
  783. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  784. This variable can be used to turn on and off the table editor during a session,
  785. but in order to toggle optimization, a restart is required.
  786. See also the variable `org-table-auto-blank-field'."
  787. :group 'org-table
  788. :type '(choice
  789. (const :tag "off" nil)
  790. (const :tag "on" t)
  791. (const :tag "on, optimized" optimized)))
  792. (defcustom org-table-tab-recognizes-table.el t
  793. "Non-nil means, TAB will automatically notice a table.el table.
  794. When it sees such a table, it moves point into it and - if necessary -
  795. calls `table-recognize-table'."
  796. :group 'org-table-editing
  797. :type 'boolean)
  798. (defgroup org-link nil
  799. "Options concerning links in Org-mode."
  800. :tag "Org Link"
  801. :group 'org)
  802. (defvar org-link-abbrev-alist-local nil
  803. "Buffer-local version of `org-link-abbrev-alist', which see.
  804. The value of this is taken from the #+LINK lines.")
  805. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  806. (defcustom org-link-abbrev-alist nil
  807. "Alist of link abbreviations.
  808. The car of each element is a string, to be replaced at the start of a link.
  809. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  810. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  811. [[linkkey:tag][description]]
  812. If REPLACE is a string, the tag will simply be appended to create the link.
  813. If the string contains \"%s\", the tag will be inserted there.
  814. REPLACE may also be a function that will be called with the tag as the
  815. only argument to create the link, which should be returned as a string.
  816. See the manual for examples."
  817. :group 'org-link
  818. :type 'alist)
  819. (defcustom org-descriptive-links t
  820. "Non-nil means, hide link part and only show description of bracket links.
  821. Bracket links are like [[link][descritpion]]. This variable sets the initial
  822. state in new org-mode buffers. The setting can then be toggled on a
  823. per-buffer basis from the Org->Hyperlinks menu."
  824. :group 'org-link
  825. :type 'boolean)
  826. (defcustom org-link-file-path-type 'adaptive
  827. "How the path name in file links should be stored.
  828. Valid values are:
  829. relative Relative to the current directory, i.e. the directory of the file
  830. into which the link is being inserted.
  831. absolute Absolute path, if possible with ~ for home directory.
  832. noabbrev Absolute path, no abbreviation of home directory.
  833. adaptive Use relative path for files in the current directory and sub-
  834. directories of it. For other files, use an absolute path."
  835. :group 'org-link
  836. :type '(choice
  837. (const relative)
  838. (const absolute)
  839. (const noabbrev)
  840. (const adaptive)))
  841. (defcustom org-activate-links '(bracket angle plain radio tag date)
  842. "Types of links that should be activated in Org-mode files.
  843. This is a list of symbols, each leading to the activation of a certain link
  844. type. In principle, it does not hurt to turn on most link types - there may
  845. be a small gain when turning off unused link types. The types are:
  846. bracket The recommended [[link][description]] or [[link]] links with hiding.
  847. angular Links in angular brackes that may contain whitespace like
  848. <bbdb:Carsten Dominik>.
  849. plain Plain links in normal text, no whitespace, like http://google.com.
  850. radio Text that is matched by a radio target, see manual for details.
  851. tag Tag settings in a headline (link to tag search).
  852. date Time stamps (link to calendar).
  853. Changing this variable requires a restart of Emacs to become effective."
  854. :group 'org-link
  855. :type '(set (const :tag "Double bracket links (new style)" bracket)
  856. (const :tag "Angular bracket links (old style)" angular)
  857. (const :tag "Plain text links" plain)
  858. (const :tag "Radio target matches" radio)
  859. (const :tag "Tags" tag)
  860. (const :tag "Timestamps" date)))
  861. (defgroup org-link-store nil
  862. "Options concerning storing links in Org-mode."
  863. :tag "Org Store Link"
  864. :group 'org-link)
  865. (defcustom org-email-link-description-format "Email %c: %.30s"
  866. "Format of the description part of a link to an email or usenet message.
  867. The following %-excapes will be replaced by corresponding information:
  868. %F full \"From\" field
  869. %f name, taken from \"From\" field, address if no name
  870. %T full \"To\" field
  871. %t first name in \"To\" field, address if no name
  872. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  873. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  874. %s subject
  875. %m message-id.
  876. You may use normal field width specification between the % and the letter.
  877. This is for example useful to limit the length of the subject.
  878. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  879. :group 'org-link-store
  880. :type 'string)
  881. (defcustom org-from-is-user-regexp
  882. (let (r1 r2)
  883. (when (and user-mail-address (not (string= user-mail-address "")))
  884. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  885. (when (and user-full-name (not (string= user-full-name "")))
  886. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  887. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  888. "Regexp mached against the \"From:\" header of an email or usenet message.
  889. It should match if the message is from the user him/herself."
  890. :group 'org-link-store
  891. :type 'regexp)
  892. (defcustom org-context-in-file-links t
  893. "Non-nil means, file links from `org-store-link' contain context.
  894. A search string will be added to the file name with :: as separator and
  895. used to find the context when the link is activated by the command
  896. `org-open-at-point'.
  897. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  898. negates this setting for the duration of the command."
  899. :group 'org-link-store
  900. :type 'boolean)
  901. (defcustom org-keep-stored-link-after-insertion nil
  902. "Non-nil means, keep link in list for entire session.
  903. The command `org-store-link' adds a link pointing to the current
  904. location to an internal list. These links accumulate during a session.
  905. The command `org-insert-link' can be used to insert links into any
  906. Org-mode file (offering completion for all stored links). When this
  907. option is nil, every link which has been inserted once using \\[org-insert-link]
  908. will be removed from the list, to make completing the unused links
  909. more efficient."
  910. :group 'org-link-store
  911. :type 'boolean)
  912. (defgroup org-link-follow nil
  913. "Options concerning following links in Org-mode."
  914. :tag "Org Follow Link"
  915. :group 'org-link)
  916. (defcustom org-follow-link-hook nil
  917. "Hook that is run after a link has been followed."
  918. :group 'org-link-follow
  919. :type 'hook)
  920. (defcustom org-tab-follows-link nil
  921. "Non-nil means, on links TAB will follow the link.
  922. Needs to be set before org.el is loaded."
  923. :group 'org-link-follow
  924. :type 'boolean)
  925. (defcustom org-return-follows-link nil
  926. "Non-nil means, on links RET will follow the link.
  927. Needs to be set before org.el is loaded."
  928. :group 'org-link-follow
  929. :type 'boolean)
  930. (defcustom org-mouse-1-follows-link
  931. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  932. "Non-nil means, mouse-1 on a link will follow the link.
  933. A longer mouse click will still set point. Does not work on XEmacs.
  934. Needs to be set before org.el is loaded."
  935. :group 'org-link-follow
  936. :type 'boolean)
  937. (defcustom org-mark-ring-length 4
  938. "Number of different positions to be recorded in the ring
  939. Changing this requires a restart of Emacs to work correctly."
  940. :group 'org-link-follow
  941. :type 'interger)
  942. (defcustom org-link-frame-setup
  943. '((vm . vm-visit-folder-other-frame)
  944. (gnus . gnus-other-frame)
  945. (file . find-file-other-window))
  946. "Setup the frame configuration for following links.
  947. When following a link with Emacs, it may often be useful to display
  948. this link in another window or frame. This variable can be used to
  949. set this up for the different types of links.
  950. For VM, use any of
  951. `vm-visit-folder'
  952. `vm-visit-folder-other-frame'
  953. For Gnus, use any of
  954. `gnus'
  955. `gnus-other-frame'
  956. For FILE, use any of
  957. `find-file'
  958. `find-file-other-window'
  959. `find-file-other-frame'
  960. For the calendar, use the variable `calendar-setup'.
  961. For BBDB, it is currently only possible to display the matches in
  962. another window."
  963. :group 'org-link-follow
  964. :type '(list
  965. (cons (const vm)
  966. (choice
  967. (const vm-visit-folder)
  968. (const vm-visit-folder-other-window)
  969. (const vm-visit-folder-other-frame)))
  970. (cons (const gnus)
  971. (choice
  972. (const gnus)
  973. (const gnus-other-frame)))
  974. (cons (const file)
  975. (choice
  976. (const find-file)
  977. (const find-file-other-window)
  978. (const find-file-other-frame)))))
  979. (defcustom org-display-internal-link-with-indirect-buffer nil
  980. "Non-nil means, use indirect buffer to display infile links.
  981. Activating internal links (from one location in a file to another location
  982. in the same file) normally just jumps to the location. When the link is
  983. activated with a C-u prefix (or with mouse-3), the link is displayed in
  984. another window. When this option is set, the other window actually displays
  985. an indirect buffer clone of the current buffer, to avoid any visibility
  986. changes to the current buffer."
  987. :group 'org-link-follow
  988. :type 'boolean)
  989. (defcustom org-open-non-existing-files nil
  990. "Non-nil means, `org-open-file' will open non-existing files.
  991. When nil, an error will be generated."
  992. :group 'org-link-follow
  993. :type 'boolean)
  994. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  995. "Function and arguments to call for following mailto links.
  996. This is a list with the first element being a lisp function, and the
  997. remaining elements being arguments to the function. In string arguments,
  998. %a will be replaced by the address, and %s will be replaced by the subject
  999. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1000. :group 'org-link-follow
  1001. :type '(choice
  1002. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1003. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1004. (const :tag "message-mail" (message-mail "%a" "%s"))
  1005. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1006. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1007. "Non-nil means, ask for confirmation before executing shell links.
  1008. Shell links can be dangerous: just think about a link
  1009. [[shell:rm -rf ~/*][Google Search]]
  1010. This link would show up in your Org-mode document as \"Google Search\",
  1011. but really it would remove your entire home directory.
  1012. Therefore we advise against setting this variable to nil.
  1013. Just change it to `y-or-n-p' of you want to confirm with a
  1014. single keystroke rather than having to type \"yes\"."
  1015. :group 'org-link-follow
  1016. :type '(choice
  1017. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1018. (const :tag "with y-or-n (faster)" y-or-n-p)
  1019. (const :tag "no confirmation (dangerous)" nil)))
  1020. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1021. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1022. Elisp links can be dangerous: just think about a link
  1023. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1024. This link would show up in your Org-mode document as \"Google Search\",
  1025. but really it would remove your entire home directory.
  1026. Therefore we advise against setting this variable to nil.
  1027. Just change it to `y-or-n-p' of you want to confirm with a
  1028. single keystroke rather than having to type \"yes\"."
  1029. :group 'org-link-follow
  1030. :type '(choice
  1031. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1032. (const :tag "with y-or-n (faster)" y-or-n-p)
  1033. (const :tag "no confirmation (dangerous)" nil)))
  1034. (defconst org-file-apps-defaults-gnu
  1035. '((remote . emacs)
  1036. (t . mailcap))
  1037. "Default file applications on a UNIX or GNU/Linux system.
  1038. See `org-file-apps'.")
  1039. (defconst org-file-apps-defaults-macosx
  1040. '((remote . emacs)
  1041. (t . "open %s")
  1042. ("ps" . "gv %s")
  1043. ("ps.gz" . "gv %s")
  1044. ("eps" . "gv %s")
  1045. ("eps.gz" . "gv %s")
  1046. ("dvi" . "xdvi %s")
  1047. ("fig" . "xfig %s"))
  1048. "Default file applications on a MacOS X system.
  1049. The system \"open\" is known as a default, but we use X11 applications
  1050. for some files for which the OS does not have a good default.
  1051. See `org-file-apps'.")
  1052. (defconst org-file-apps-defaults-windowsnt
  1053. (list
  1054. '(remote . emacs)
  1055. (cons t
  1056. (list (if (featurep 'xemacs)
  1057. 'mswindows-shell-execute
  1058. 'w32-shell-execute)
  1059. "open" 'file)))
  1060. "Default file applications on a Windows NT system.
  1061. The system \"open\" is used for most files.
  1062. See `org-file-apps'.")
  1063. (defcustom org-file-apps
  1064. '(
  1065. ("txt" . emacs)
  1066. ("tex" . emacs)
  1067. ("ltx" . emacs)
  1068. ("org" . emacs)
  1069. ("el" . emacs)
  1070. ("bib" . emacs)
  1071. )
  1072. "External applications for opening `file:path' items in a document.
  1073. Org-mode uses system defaults for different file types, but
  1074. you can use this variable to set the application for a given file
  1075. extension. The entries in this list are cons cells where the car identifies
  1076. files and the cdr the corresponding command. Possible values for the
  1077. file identifier are
  1078. \"ext\" A string identifying an extension
  1079. `directory' Matches a directory
  1080. `remote' Matches a remote file, accessible through tramp or efs.
  1081. Remote files most likely should be visited through Emacs
  1082. because external applications cannot handle such paths.
  1083. t Default for all remaining files
  1084. Possible values for the command are:
  1085. `emacs' The file will be visited by the current Emacs process.
  1086. `default' Use the default application for this file type.
  1087. string A command to be executed by a shell; %s will be replaced
  1088. by the path to the file.
  1089. sexp A Lisp form which will be evaluated. The file path will
  1090. be available in the Lisp variable `file'.
  1091. For more examples, see the system specific constants
  1092. `org-file-apps-defaults-macosx'
  1093. `org-file-apps-defaults-windowsnt'
  1094. `org-file-apps-defaults-gnu'."
  1095. :group 'org-link-follow
  1096. :type '(repeat
  1097. (cons (choice :value ""
  1098. (string :tag "Extension")
  1099. (const :tag "Default for unrecognized files" t)
  1100. (const :tag "Remote file" remote)
  1101. (const :tag "Links to a directory" directory))
  1102. (choice :value ""
  1103. (const :tag "Visit with Emacs" emacs)
  1104. (const :tag "Use system default" default)
  1105. (string :tag "Command")
  1106. (sexp :tag "Lisp form")))))
  1107. (defgroup org-refile nil
  1108. "Options concerning refiling entries in Org-mode."
  1109. :tag "Org Remember"
  1110. :group 'org)
  1111. (defcustom org-directory "~/org"
  1112. "Directory with org files.
  1113. This directory will be used as default to prompt for org files.
  1114. Used by the hooks for remember.el."
  1115. :group 'org-refile
  1116. :group 'org-remember
  1117. :type 'directory)
  1118. (defcustom org-default-notes-file "~/.notes"
  1119. "Default target for storing notes.
  1120. Used by the hooks for remember.el. This can be a string, or nil to mean
  1121. the value of `remember-data-file'.
  1122. You can set this on a per-template basis with the variable
  1123. `org-remember-templates'."
  1124. :group 'org-refile
  1125. :group 'org-remember
  1126. :type '(choice
  1127. (const :tag "Default from remember-data-file" nil)
  1128. file))
  1129. (defcustom org-goto-interface 'outline
  1130. "The default interface to be used for `org-goto'.
  1131. Allowed vaues are:
  1132. outline The interface shows an outline of the relevant file
  1133. and the correct heading is found by moving through
  1134. the outline or by searching with incremental search.
  1135. outline-path-completion Headlines in the current buffer are offered via
  1136. completion."
  1137. :group 'org-refile
  1138. :type '(choice
  1139. (const :tag "Outline" outline)
  1140. (const :tag "Outline-path-completion" outline-path-completion)))
  1141. (defcustom org-reverse-note-order nil
  1142. "Non-nil means, store new notes at the beginning of a file or entry.
  1143. When nil, new notes will be filed to the end of a file or entry.
  1144. This can also be a list with cons cells of regular expressions that
  1145. are matched against file names, and values."
  1146. :group 'org-remember
  1147. :type '(choice
  1148. (const :tag "Reverse always" t)
  1149. (const :tag "Reverse never" nil)
  1150. (repeat :tag "By file name regexp"
  1151. (cons regexp boolean))))
  1152. (defcustom org-refile-targets nil
  1153. "Targets for refiling entries with \\[org-refile].
  1154. This is list of cons cells. Each cell contains:
  1155. - a specification of the files to be considered, either a list of files,
  1156. or a symbol whose function or variable value will be used to retrieve
  1157. a file name or a list of file names. Nil means, refile to a different
  1158. heading in the current buffer.
  1159. - A specification of how to find candidate refile targets. This may be
  1160. any of
  1161. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1162. This tag has to be present in all target headlines, inheritance will
  1163. not be considered.
  1164. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1165. todo keyword.
  1166. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1167. headlines that are refiling targets.
  1168. - a cons cell (:level . N). Any headline of level N is considered a target.
  1169. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1170. :group 'org-remember
  1171. :type '(repeat
  1172. (cons
  1173. (choice :value org-agenda-files
  1174. (const :tag "All agenda files" org-agenda-files)
  1175. (const :tag "Current buffer" nil)
  1176. (function) (variable) (file))
  1177. (choice :tag "Identify target headline by"
  1178. (cons :tag "Specific tag" (const :tag) (string))
  1179. (cons :tag "TODO keyword" (const :todo) (string))
  1180. (cons :tag "Regular expression" (const :regexp) (regexp))
  1181. (cons :tag "Level number" (const :level) (integer))
  1182. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1183. (defcustom org-refile-use-outline-path nil
  1184. "Non-nil means, provide refile targets as paths.
  1185. So a level 3 headline will be available as level1/level2/level3.
  1186. When the value is `file', also include the file name (without directory)
  1187. into the path. When `full-file-path', include the full file path."
  1188. :group 'org-remember
  1189. :type '(choice
  1190. (const :tag "Not" nil)
  1191. (const :tag "Yes" t)
  1192. (const :tag "Start with file name" file)
  1193. (const :tag "Start with full file path" full-file-path)))
  1194. (defgroup org-todo nil
  1195. "Options concerning TODO items in Org-mode."
  1196. :tag "Org TODO"
  1197. :group 'org)
  1198. (defgroup org-progress nil
  1199. "Options concerning Progress logging in Org-mode."
  1200. :tag "Org Progress"
  1201. :group 'org-time)
  1202. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1203. "List of TODO entry keyword sequences and their interpretation.
  1204. \\<org-mode-map>This is a list of sequences.
  1205. Each sequence starts with a symbol, either `sequence' or `type',
  1206. indicating if the keywords should be interpreted as a sequence of
  1207. action steps, or as different types of TODO items. The first
  1208. keywords are states requiring action - these states will select a headline
  1209. for inclusion into the global TODO list Org-mode produces. If one of
  1210. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1211. signify that no further action is necessary. If \"|\" is not found,
  1212. the last keyword is treated as the only DONE state of the sequence.
  1213. The command \\[org-todo] cycles an entry through these states, and one
  1214. additional state where no keyword is present. For details about this
  1215. cycling, see the manual.
  1216. TODO keywords and interpretation can also be set on a per-file basis with
  1217. the special #+SEQ_TODO and #+TYP_TODO lines.
  1218. Each keyword can optionally specify a character for fast state selection
  1219. \(in combination with the variable `org-use-fast-todo-selection')
  1220. and specifiers for state change logging, using the same syntax
  1221. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1222. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1223. indicates to record a time stamp each time this state is selected.
  1224. Each keyword may also specify if a timestamp or a note should be
  1225. recorded when entering or leaving the state, by adding additional
  1226. characters in the parenthesis after the keyword. This looks like this:
  1227. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1228. record only the time of the state change. With X and Y being either
  1229. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1230. Y when leaving the state if and only if the *target* state does not
  1231. define X. You may omit any of the fast-selection key or X or /Y,
  1232. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1233. For backward compatibility, this variable may also be just a list
  1234. of keywords - in this case the interptetation (sequence or type) will be
  1235. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1236. :group 'org-todo
  1237. :group 'org-keywords
  1238. :type '(choice
  1239. (repeat :tag "Old syntax, just keywords"
  1240. (string :tag "Keyword"))
  1241. (repeat :tag "New syntax"
  1242. (cons
  1243. (choice
  1244. :tag "Interpretation"
  1245. (const :tag "Sequence (cycling hits every state)" sequence)
  1246. (const :tag "Type (cycling directly to DONE)" type))
  1247. (repeat
  1248. (string :tag "Keyword"))))))
  1249. (defvar org-todo-keywords-1 nil
  1250. "All TODO and DONE keywords active in a buffer.")
  1251. (make-variable-buffer-local 'org-todo-keywords-1)
  1252. (defvar org-todo-keywords-for-agenda nil)
  1253. (defvar org-done-keywords-for-agenda nil)
  1254. (defvar org-not-done-keywords nil)
  1255. (make-variable-buffer-local 'org-not-done-keywords)
  1256. (defvar org-done-keywords nil)
  1257. (make-variable-buffer-local 'org-done-keywords)
  1258. (defvar org-todo-heads nil)
  1259. (make-variable-buffer-local 'org-todo-heads)
  1260. (defvar org-todo-sets nil)
  1261. (make-variable-buffer-local 'org-todo-sets)
  1262. (defvar org-todo-log-states nil)
  1263. (make-variable-buffer-local 'org-todo-log-states)
  1264. (defvar org-todo-kwd-alist nil)
  1265. (make-variable-buffer-local 'org-todo-kwd-alist)
  1266. (defvar org-todo-key-alist nil)
  1267. (make-variable-buffer-local 'org-todo-key-alist)
  1268. (defvar org-todo-key-trigger nil)
  1269. (make-variable-buffer-local 'org-todo-key-trigger)
  1270. (defcustom org-todo-interpretation 'sequence
  1271. "Controls how TODO keywords are interpreted.
  1272. This variable is in principle obsolete and is only used for
  1273. backward compatibility, if the interpretation of todo keywords is
  1274. not given already in `org-todo-keywords'. See that variable for
  1275. more information."
  1276. :group 'org-todo
  1277. :group 'org-keywords
  1278. :type '(choice (const sequence)
  1279. (const type)))
  1280. (defcustom org-use-fast-todo-selection 'prefix
  1281. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1282. This variable describes if and under what circumstances the cycling
  1283. mechanism for TODO keywords will be replaced by a single-key, direct
  1284. selection scheme.
  1285. When nil, fast selection is never used.
  1286. When the symbol `prefix', it will be used when `org-todo' is called with
  1287. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1288. in an agenda buffer.
  1289. When t, fast selection is used by default. In this case, the prefix
  1290. argument forces cycling instead.
  1291. In all cases, the special interface is only used if access keys have actually
  1292. been assigned by the user, i.e. if keywords in the configuration are followed
  1293. by a letter in parenthesis, like TODO(t)."
  1294. :group 'org-todo
  1295. :type '(choice
  1296. (const :tag "Never" nil)
  1297. (const :tag "By default" t)
  1298. (const :tag "Only with C-u C-c C-t" prefix)))
  1299. (defcustom org-after-todo-state-change-hook nil
  1300. "Hook which is run after the state of a TODO item was changed.
  1301. The new state (a string with a TODO keyword, or nil) is available in the
  1302. Lisp variable `state'."
  1303. :group 'org-todo
  1304. :type 'hook)
  1305. (defcustom org-log-done nil
  1306. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1307. When equal to the list (done), also prompt for a closing note.
  1308. This can also be configured on a per-file basis by adding one of
  1309. the following lines anywhere in the buffer:
  1310. #+STARTUP: logdone
  1311. #+STARTUP: lognotedone
  1312. #+STARTUP: nologdone"
  1313. :group 'org-todo
  1314. :group 'org-progress
  1315. :type '(choice
  1316. (const :tag "No logging" nil)
  1317. (const :tag "Record CLOSED timestamp" time)
  1318. (const :tag "Record CLOSED timestamp with closing note." note)))
  1319. ;; Normalize old uses of org-log-done.
  1320. (cond
  1321. ((eq org-log-done t) (setq org-log-done 'time))
  1322. ((and (listp org-log-done) (memq 'done org-log-done))
  1323. (setq org-log-done 'note)))
  1324. (defcustom org-log-note-clock-out nil
  1325. "Non-nil means, recored a note when clocking out of an item.
  1326. This can also be configured on a per-file basis by adding one of
  1327. the following lines anywhere in the buffer:
  1328. #+STARTUP: lognoteclock-out
  1329. #+STARTUP: nolognoteclock-out"
  1330. :group 'org-todo
  1331. :group 'org-progress
  1332. :type 'boolean)
  1333. (defcustom org-log-done-with-time t
  1334. "Non-nil means, the CLOSED time stamp will contain date and time.
  1335. When nil, only the date will be recorded."
  1336. :group 'org-progress
  1337. :type 'boolean)
  1338. (defcustom org-log-note-headings
  1339. '((done . "CLOSING NOTE %t")
  1340. (state . "State %-12s %t")
  1341. (note . "Note taken on %t")
  1342. (clock-out . ""))
  1343. "Headings for notes added to entries.
  1344. The value is an alist, with the car being a symbol indicating the note
  1345. context, and the cdr is the heading to be used. The heading may also be the
  1346. empty string.
  1347. %t in the heading will be replaced by a time stamp.
  1348. %s will be replaced by the new TODO state, in double quotes.
  1349. %u will be replaced by the user name.
  1350. %U will be replaced by the full user name."
  1351. :group 'org-todo
  1352. :group 'org-progress
  1353. :type '(list :greedy t
  1354. (cons (const :tag "Heading when closing an item" done) string)
  1355. (cons (const :tag
  1356. "Heading when changing todo state (todo sequence only)"
  1357. state) string)
  1358. (cons (const :tag "Heading when just taking a note" note) string)
  1359. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1360. (unless (assq 'note org-log-note-headings)
  1361. (push '(note . "%t") org-log-note-headings))
  1362. (defcustom org-log-states-order-reversed t
  1363. "Non-nil means, the latest state change note will be directly after heading.
  1364. When nil, the notes will be orderer according to time."
  1365. :group 'org-todo
  1366. :group 'org-progress
  1367. :type 'boolean)
  1368. (defcustom org-log-repeat 'time
  1369. "Non-nil means, record moving through the DONE state when triggering repeat.
  1370. An auto-repeating tasks is immediately switched back to TODO when marked
  1371. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1372. the TODO keyword definition, or recording a cloing note by setting
  1373. `org-log-done', there will be no record of the task moving trhough DONE.
  1374. This variable forces taking a note anyway. Possible values are:
  1375. nil Don't force a record
  1376. time Record a time stamp
  1377. note Record a note
  1378. This option can also be set with on a per-file-basis with
  1379. #+STARTUP: logrepeat
  1380. #+STARTUP: lognoterepeat
  1381. #+STARTUP: nologrepeat
  1382. You can have local logging settings for a subtree by setting the LOGGING
  1383. property to one or more of these keywords."
  1384. :group 'org-todo
  1385. :group 'org-progress
  1386. :type '(choice
  1387. (const :tag "Don't force a record" nil)
  1388. (const :tag "Force recording the DONE state" time)
  1389. (const :tag "Force recording a note with the DONE state" note)))
  1390. (defgroup org-priorities nil
  1391. "Priorities in Org-mode."
  1392. :tag "Org Priorities"
  1393. :group 'org-todo)
  1394. (defcustom org-highest-priority ?A
  1395. "The highest priority of TODO items. A character like ?A, ?B etc.
  1396. Must have a smaller ASCII number than `org-lowest-priority'."
  1397. :group 'org-priorities
  1398. :type 'character)
  1399. (defcustom org-lowest-priority ?C
  1400. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1401. Must have a larger ASCII number than `org-highest-priority'."
  1402. :group 'org-priorities
  1403. :type 'character)
  1404. (defcustom org-default-priority ?B
  1405. "The default priority of TODO items.
  1406. This is the priority an item get if no explicit priority is given."
  1407. :group 'org-priorities
  1408. :type 'character)
  1409. (defcustom org-priority-start-cycle-with-default t
  1410. "Non-nil means, start with default priority when starting to cycle.
  1411. When this is nil, the first step in the cycle will be (depending on the
  1412. command used) one higher or lower that the default priority."
  1413. :group 'org-priorities
  1414. :type 'boolean)
  1415. (defgroup org-time nil
  1416. "Options concerning time stamps and deadlines in Org-mode."
  1417. :tag "Org Time"
  1418. :group 'org)
  1419. (defcustom org-insert-labeled-timestamps-at-point nil
  1420. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1421. When nil, these labeled time stamps are forces into the second line of an
  1422. entry, just after the headline. When scheduling from the global TODO list,
  1423. the time stamp will always be forced into the second line."
  1424. :group 'org-time
  1425. :type 'boolean)
  1426. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1427. "Formats for `format-time-string' which are used for time stamps.
  1428. It is not recommended to change this constant.")
  1429. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1430. "Number of minutes to round time stamps to.
  1431. These are two values, the first applies when first creating a time stamp.
  1432. The second applies when changing it with the commands `S-up' and `S-down'.
  1433. When changing the time stamp, this means that it will change in steps
  1434. of N minutes, as given by the second value.
  1435. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1436. numbers should be factors of 60, so for example 5, 10, 15.
  1437. When this is larger than 1, you can still force an exact time-stamp by using
  1438. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1439. and by using a prefix arg to `S-up/down' to specify the exact number
  1440. of minutes to shift."
  1441. :group 'org-time
  1442. :get '(lambda (var) ; Make sure all entries have 5 elements
  1443. (if (integerp (default-value var))
  1444. (list (default-value var) 5)
  1445. (default-value var)))
  1446. :type '(list
  1447. (integer :tag "when inserting times")
  1448. (integer :tag "when modifying times")))
  1449. ;; Normalize old customizations of this variable.
  1450. (when (integerp org-time-stamp-rounding-minutes)
  1451. (setq org-time-stamp-rounding-minutes
  1452. (list org-time-stamp-rounding-minutes
  1453. org-time-stamp-rounding-minutes)))
  1454. (defcustom org-display-custom-times nil
  1455. "Non-nil means, overlay custom formats over all time stamps.
  1456. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1457. To turn this on on a per-file basis, insert anywhere in the file:
  1458. #+STARTUP: customtime"
  1459. :group 'org-time
  1460. :set 'set-default
  1461. :type 'sexp)
  1462. (make-variable-buffer-local 'org-display-custom-times)
  1463. (defcustom org-time-stamp-custom-formats
  1464. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1465. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1466. These are overlayed over the default ISO format if the variable
  1467. `org-display-custom-times' is set. Time like %H:%M should be at the
  1468. end of the second format."
  1469. :group 'org-time
  1470. :type 'sexp)
  1471. (defun org-time-stamp-format (&optional long inactive)
  1472. "Get the right format for a time string."
  1473. (let ((f (if long (cdr org-time-stamp-formats)
  1474. (car org-time-stamp-formats))))
  1475. (if inactive
  1476. (concat "[" (substring f 1 -1) "]")
  1477. f)))
  1478. (defcustom org-deadline-warning-days 14
  1479. "No. of days before expiration during which a deadline becomes active.
  1480. This variable governs the display in sparse trees and in the agenda.
  1481. When 0 or negative, it means use this number (the absolute value of it)
  1482. even if a deadline has a different individual lead time specified."
  1483. :group 'org-time
  1484. :group 'org-agenda-daily/weekly
  1485. :type 'number)
  1486. (defcustom org-read-date-prefer-future t
  1487. "Non-nil means, assume future for incomplete date input from user.
  1488. This affects the following situations:
  1489. 1. The user gives a day, but no month.
  1490. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1491. read this as the third of *next* month. However, if you enter \"17\",
  1492. it will be considered as *this* month.
  1493. 2. The user gives a month but not a year.
  1494. For example, if it is april and you enter \"feb 2\", this will be read
  1495. as feb 2, *next* year. \"May 5\", however, will be this year.
  1496. Currently this does not work for ISO week specifications.
  1497. When this option is nil, the current month and year will always be used
  1498. as defaults."
  1499. :group 'org-time
  1500. :type 'boolean)
  1501. (defcustom org-read-date-display-live t
  1502. "Non-nil means, display current interpretation of date prompt live.
  1503. This display will be in an overlay, in the minibuffer."
  1504. :group 'org-time
  1505. :type 'boolean)
  1506. (defcustom org-read-date-popup-calendar t
  1507. "Non-nil means, pop up a calendar when prompting for a date.
  1508. In the calendar, the date can be selected with mouse-1. However, the
  1509. minibuffer will also be active, and you can simply enter the date as well.
  1510. When nil, only the minibuffer will be available."
  1511. :group 'org-time
  1512. :type 'boolean)
  1513. (if (fboundp 'defvaralias)
  1514. (defvaralias 'org-popup-calendar-for-date-prompt
  1515. 'org-read-date-popup-calendar))
  1516. (defcustom org-extend-today-until 0
  1517. "The hour when your day really ends.
  1518. This has influence for the following applications:
  1519. - When switching the agenda to \"today\". It it is still earlier than
  1520. the time given here, the day recognized as TODAY is actually yesterday.
  1521. - When a date is read from the user and it is still before the time given
  1522. here, the current date and time will be assumed to be yesterday, 23:59.
  1523. FIXME:
  1524. IMPORTANT: This is still a very experimental feature, it may disappear
  1525. again or it may be extended to mean more things."
  1526. :group 'org-time
  1527. :type 'number)
  1528. (defcustom org-edit-timestamp-down-means-later nil
  1529. "Non-nil means, S-down will increase the time in a time stamp.
  1530. When nil, S-up will increase."
  1531. :group 'org-time
  1532. :type 'boolean)
  1533. (defcustom org-calendar-follow-timestamp-change t
  1534. "Non-nil means, make the calendar window follow timestamp changes.
  1535. When a timestamp is modified and the calendar window is visible, it will be
  1536. moved to the new date."
  1537. :group 'org-time
  1538. :type 'boolean)
  1539. (defgroup org-tags nil
  1540. "Options concerning tags in Org-mode."
  1541. :tag "Org Tags"
  1542. :group 'org)
  1543. (defcustom org-tag-alist nil
  1544. "List of tags allowed in Org-mode files.
  1545. When this list is nil, Org-mode will base TAG input on what is already in the
  1546. buffer.
  1547. The value of this variable is an alist, the car of each entry must be a
  1548. keyword as a string, the cdr may be a character that is used to select
  1549. that tag through the fast-tag-selection interface.
  1550. See the manual for details."
  1551. :group 'org-tags
  1552. :type '(repeat
  1553. (choice
  1554. (cons (string :tag "Tag name")
  1555. (character :tag "Access char"))
  1556. (const :tag "Start radio group" (:startgroup))
  1557. (const :tag "End radio group" (:endgroup)))))
  1558. (defcustom org-use-fast-tag-selection 'auto
  1559. "Non-nil means, use fast tag selection scheme.
  1560. This is a special interface to select and deselect tags with single keys.
  1561. When nil, fast selection is never used.
  1562. When the symbol `auto', fast selection is used if and only if selection
  1563. characters for tags have been configured, either through the variable
  1564. `org-tag-alist' or through a #+TAGS line in the buffer.
  1565. When t, fast selection is always used and selection keys are assigned
  1566. automatically if necessary."
  1567. :group 'org-tags
  1568. :type '(choice
  1569. (const :tag "Always" t)
  1570. (const :tag "Never" nil)
  1571. (const :tag "When selection characters are configured" 'auto)))
  1572. (defcustom org-fast-tag-selection-single-key nil
  1573. "Non-nil means, fast tag selection exits after first change.
  1574. When nil, you have to press RET to exit it.
  1575. During fast tag selection, you can toggle this flag with `C-c'.
  1576. This variable can also have the value `expert'. In this case, the window
  1577. displaying the tags menu is not even shown, until you press C-c again."
  1578. :group 'org-tags
  1579. :type '(choice
  1580. (const :tag "No" nil)
  1581. (const :tag "Yes" t)
  1582. (const :tag "Expert" expert)))
  1583. (defvar org-fast-tag-selection-include-todo nil
  1584. "Non-nil means, fast tags selection interface will also offer TODO states.
  1585. This is an undocumented feature, you should not rely on it.")
  1586. (defcustom org-tags-column -80
  1587. "The column to which tags should be indented in a headline.
  1588. If this number is positive, it specifies the column. If it is negative,
  1589. it means that the tags should be flushright to that column. For example,
  1590. -80 works well for a normal 80 character screen."
  1591. :group 'org-tags
  1592. :type 'integer)
  1593. (defcustom org-auto-align-tags t
  1594. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1595. These operations change the length of a headline and therefore shift
  1596. the tags around. With this options turned on, after each such operation
  1597. the tags are again aligned to `org-tags-column'."
  1598. :group 'org-tags
  1599. :type 'boolean)
  1600. (defcustom org-use-tag-inheritance t
  1601. "Non-nil means, tags in levels apply also for sublevels.
  1602. When nil, only the tags directly given in a specific line apply there.
  1603. If you turn off this option, you very likely want to turn on the
  1604. companion option `org-tags-match-list-sublevels'.
  1605. This may also be a list of tags that should be inherited, or a regexp that
  1606. matches tags that should be inherited."
  1607. :group 'org-tags
  1608. :type '(choice
  1609. (const :tag "Not" nil)
  1610. (const :tag "Always" t)
  1611. (repeat :tag "Specific tags" (string :tag "Tag"))
  1612. (regexp :tag "Tags matched by regexp")))
  1613. (defun org-tag-inherit-p (tag)
  1614. "Check if TAG is one that should be inherited."
  1615. (cond
  1616. ((eq org-use-tag-inheritance t) t)
  1617. ((not org-use-tag-inheritance) nil)
  1618. ((stringp org-use-tag-inheritance)
  1619. (string-match org-use-tag-inheritance tag))
  1620. ((listp org-use-tag-inheritance)
  1621. (member tag org-use-tag-inheritance))
  1622. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1623. (defcustom org-tags-match-list-sublevels nil
  1624. "Non-nil means list also sublevels of headlines matching tag search.
  1625. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1626. the sublevels of a headline matching a tag search often also match
  1627. the same search. Listing all of them can create very long lists.
  1628. Setting this variable to nil causes subtrees of a match to be skipped.
  1629. This option is off by default, because inheritance in on. If you turn
  1630. inheritance off, you very likely want to turn this option on.
  1631. As a special case, if the tag search is restricted to TODO items, the
  1632. value of this variable is ignored and sublevels are always checked, to
  1633. make sure all corresponding TODO items find their way into the list."
  1634. :group 'org-tags
  1635. :type 'boolean)
  1636. (defvar org-tags-history nil
  1637. "History of minibuffer reads for tags.")
  1638. (defvar org-last-tags-completion-table nil
  1639. "The last used completion table for tags.")
  1640. (defvar org-after-tags-change-hook nil
  1641. "Hook that is run after the tags in a line have changed.")
  1642. (defgroup org-properties nil
  1643. "Options concerning properties in Org-mode."
  1644. :tag "Org Properties"
  1645. :group 'org)
  1646. (defcustom org-property-format "%-10s %s"
  1647. "How property key/value pairs should be formatted by `indent-line'.
  1648. When `indent-line' hits a property definition, it will format the line
  1649. according to this format, mainly to make sure that the values are
  1650. lined-up with respect to each other."
  1651. :group 'org-properties
  1652. :type 'string)
  1653. (defcustom org-use-property-inheritance nil
  1654. "Non-nil means, properties apply also for sublevels.
  1655. This setting is chiefly used during property searches. Turning it on can
  1656. cause significant overhead when doing a search, which is why it is not
  1657. on by default.
  1658. When nil, only the properties directly given in the current entry count.
  1659. When t, every property is inherited. The value may also be a list of
  1660. properties that should have inheritance, or a regular expression matching
  1661. properties that should be inherited.
  1662. However, note that some special properties use inheritance under special
  1663. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1664. and the properties ending in \"_ALL\" when they are used as descriptor
  1665. for valid values of a property.
  1666. Note for programmers:
  1667. When querying an entry with `org-entry-get', you can control if inheritance
  1668. should be used. By default, `org-entry-get' looks only at the local
  1669. properties. You can request inheritance by setting the inherit argument
  1670. to t (to force inheritance) or to `selective' (to respect the setting
  1671. in this variable)."
  1672. :group 'org-properties
  1673. :type '(choice
  1674. (const :tag "Not" nil)
  1675. (const :tag "Always" t)
  1676. (repeat :tag "Specific properties" (string :tag "Property"))
  1677. (regexp :tag "Properties matched by regexp")))
  1678. (defun org-property-inherit-p (property)
  1679. "Check if PROPERTY is one that should be inherited."
  1680. (cond
  1681. ((eq org-use-property-inheritance t) t)
  1682. ((not org-use-property-inheritance) nil)
  1683. ((stringp org-use-property-inheritance)
  1684. (string-match org-use-property-inheritance property))
  1685. ((listp org-use-property-inheritance)
  1686. (member property org-use-property-inheritance))
  1687. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1688. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1689. "The default column format, if no other format has been defined.
  1690. This variable can be set on the per-file basis by inserting a line
  1691. #+COLUMNS: %25ITEM ....."
  1692. :group 'org-properties
  1693. :type 'string)
  1694. (defcustom org-global-properties nil
  1695. "List of property/value pairs that can be inherited by any entry.
  1696. You can set buffer-local values for this by adding lines like
  1697. #+PROPERTY: NAME VALUE"
  1698. :group 'org-properties
  1699. :type '(repeat
  1700. (cons (string :tag "Property")
  1701. (string :tag "Value"))))
  1702. (defvar org-local-properties nil
  1703. "List of property/value pairs that can be inherited by any entry.
  1704. Valid for the current buffer.
  1705. This variable is populated from #+PROPERTY lines.")
  1706. (defgroup org-agenda nil
  1707. "Options concerning agenda views in Org-mode."
  1708. :tag "Org Agenda"
  1709. :group 'org)
  1710. (defvar org-category nil
  1711. "Variable used by org files to set a category for agenda display.
  1712. Such files should use a file variable to set it, for example
  1713. # -*- mode: org; org-category: \"ELisp\"
  1714. or contain a special line
  1715. #+CATEGORY: ELisp
  1716. If the file does not specify a category, then file's base name
  1717. is used instead.")
  1718. (make-variable-buffer-local 'org-category)
  1719. (defcustom org-agenda-files nil
  1720. "The files to be used for agenda display.
  1721. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1722. \\[org-remove-file]. You can also use customize to edit the list.
  1723. If an entry is a directory, all files in that directory that are matched by
  1724. `org-agenda-file-regexp' will be part of the file list.
  1725. If the value of the variable is not a list but a single file name, then
  1726. the list of agenda files is actually stored and maintained in that file, one
  1727. agenda file per line."
  1728. :group 'org-agenda
  1729. :type '(choice
  1730. (repeat :tag "List of files and directories" file)
  1731. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1732. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1733. "Regular expression to match files for `org-agenda-files'.
  1734. If any element in the list in that variable contains a directory instead
  1735. of a normal file, all files in that directory that are matched by this
  1736. regular expression will be included."
  1737. :group 'org-agenda
  1738. :type 'regexp)
  1739. (defcustom org-agenda-text-search-extra-files nil
  1740. "List of extra files to be searched by text search commands.
  1741. These files will be search in addition to the agenda files bu the
  1742. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1743. Note that these files will only be searched for text search commands,
  1744. not for the other agenda views like todo lists, tag earches or the weekly
  1745. agenda. This variable is intended to list notes and possibly archive files
  1746. that should also be searched by these two commands."
  1747. :group 'org-agenda
  1748. :type '(repeat file))
  1749. (if (fboundp 'defvaralias)
  1750. (defvaralias 'org-agenda-multi-occur-extra-files
  1751. 'org-agenda-text-search-extra-files))
  1752. (defcustom org-agenda-skip-unavailable-files nil
  1753. "t means to just skip non-reachable files in `org-agenda-files'.
  1754. Nil means to remove them, after a query, from the list."
  1755. :group 'org-agenda
  1756. :type 'boolean)
  1757. (defcustom org-calendar-to-agenda-key [?c]
  1758. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1759. The command `org-calendar-goto-agenda' will be bound to this key. The
  1760. default is the character `c' because then `c' can be used to switch back and
  1761. forth between agenda and calendar."
  1762. :group 'org-agenda
  1763. :type 'sexp)
  1764. (eval-after-load "calendar"
  1765. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1766. 'org-calendar-goto-agenda))
  1767. (defgroup org-latex nil
  1768. "Options for embedding LaTeX code into Org-mode."
  1769. :tag "Org LaTeX"
  1770. :group 'org)
  1771. (defcustom org-format-latex-options
  1772. '(:foreground default :background default :scale 1.0
  1773. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1774. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1775. "Options for creating images from LaTeX fragments.
  1776. This is a property list with the following properties:
  1777. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1778. `default' means use the forground of the default face.
  1779. :background the background color, or \"Transparent\".
  1780. `default' means use the background of the default face.
  1781. :scale a scaling factor for the size of the images
  1782. :html-foreground, :html-background, :html-scale
  1783. The same numbers for HTML export.
  1784. :matchers a list indicating which matchers should be used to
  1785. find LaTeX fragments. Valid members of this list are:
  1786. \"begin\" find environments
  1787. \"$\" find math expressions surrounded by $...$
  1788. \"$$\" find math expressions surrounded by $$....$$
  1789. \"\\(\" find math expressions surrounded by \\(...\\)
  1790. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1791. :group 'org-latex
  1792. :type 'plist)
  1793. (defcustom org-format-latex-header "\\documentclass{article}
  1794. \\usepackage{fullpage} % do not remove
  1795. \\usepackage{amssymb}
  1796. \\usepackage[usenames]{color}
  1797. \\usepackage{amsmath}
  1798. \\usepackage{latexsym}
  1799. \\usepackage[mathscr]{eucal}
  1800. \\pagestyle{empty} % do not remove"
  1801. "The document header used for processing LaTeX fragments."
  1802. :group 'org-latex
  1803. :type 'string)
  1804. (defgroup org-font-lock nil
  1805. "Font-lock settings for highlighting in Org-mode."
  1806. :tag "Org Font Lock"
  1807. :group 'org)
  1808. (defcustom org-level-color-stars-only nil
  1809. "Non-nil means fontify only the stars in each headline.
  1810. When nil, the entire headline is fontified.
  1811. Changing it requires restart of `font-lock-mode' to become effective
  1812. also in regions already fontified."
  1813. :group 'org-font-lock
  1814. :type 'boolean)
  1815. (defcustom org-hide-leading-stars nil
  1816. "Non-nil means, hide the first N-1 stars in a headline.
  1817. This works by using the face `org-hide' for these stars. This
  1818. face is white for a light background, and black for a dark
  1819. background. You may have to customize the face `org-hide' to
  1820. make this work.
  1821. Changing it requires restart of `font-lock-mode' to become effective
  1822. also in regions already fontified.
  1823. You may also set this on a per-file basis by adding one of the following
  1824. lines to the buffer:
  1825. #+STARTUP: hidestars
  1826. #+STARTUP: showstars"
  1827. :group 'org-font-lock
  1828. :type 'boolean)
  1829. (defcustom org-fontify-done-headline nil
  1830. "Non-nil means, change the face of a headline if it is marked DONE.
  1831. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1832. When this is non-nil, the headline after the keyword is set to the
  1833. `org-headline-done' as an additional indication."
  1834. :group 'org-font-lock
  1835. :type 'boolean)
  1836. (defcustom org-fontify-emphasized-text t
  1837. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1838. Changing this variable requires a restart of Emacs to take effect."
  1839. :group 'org-font-lock
  1840. :type 'boolean)
  1841. (defcustom org-highlight-latex-fragments-and-specials nil
  1842. "Non-nil means, fontify what is treated specially by the exporters."
  1843. :group 'org-font-lock
  1844. :type 'boolean)
  1845. (defcustom org-hide-emphasis-markers nil
  1846. "Non-nil mean font-lock should hide the emphasis marker characters."
  1847. :group 'org-font-lock
  1848. :type 'boolean)
  1849. (defvar org-emph-re nil
  1850. "Regular expression for matching emphasis.")
  1851. (defvar org-verbatim-re nil
  1852. "Regular expression for matching verbatim text.")
  1853. (defvar org-emphasis-regexp-components) ; defined just below
  1854. (defvar org-emphasis-alist) ; defined just below
  1855. (defun org-set-emph-re (var val)
  1856. "Set variable and compute the emphasis regular expression."
  1857. (set var val)
  1858. (when (and (boundp 'org-emphasis-alist)
  1859. (boundp 'org-emphasis-regexp-components)
  1860. org-emphasis-alist org-emphasis-regexp-components)
  1861. (let* ((e org-emphasis-regexp-components)
  1862. (pre (car e))
  1863. (post (nth 1 e))
  1864. (border (nth 2 e))
  1865. (body (nth 3 e))
  1866. (nl (nth 4 e))
  1867. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1868. (body1 (concat body "*?"))
  1869. (markers (mapconcat 'car org-emphasis-alist ""))
  1870. (vmarkers (mapconcat
  1871. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1872. org-emphasis-alist "")))
  1873. ;; make sure special characters appear at the right position in the class
  1874. (if (string-match "\\^" markers)
  1875. (setq markers (concat (replace-match "" t t markers) "^")))
  1876. (if (string-match "-" markers)
  1877. (setq markers (concat (replace-match "" t t markers) "-")))
  1878. (if (string-match "\\^" vmarkers)
  1879. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1880. (if (string-match "-" vmarkers)
  1881. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1882. (if (> nl 0)
  1883. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1884. (int-to-string nl) "\\}")))
  1885. ;; Make the regexp
  1886. (setq org-emph-re
  1887. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1888. "\\("
  1889. "\\([" markers "]\\)"
  1890. "\\("
  1891. "[^" border "]\\|"
  1892. "[^" border (if (and nil stacked) markers) "]"
  1893. body1
  1894. "[^" border (if (and nil stacked) markers) "]"
  1895. "\\)"
  1896. "\\3\\)"
  1897. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1898. (setq org-verbatim-re
  1899. (concat "\\([" pre "]\\|^\\)"
  1900. "\\("
  1901. "\\([" vmarkers "]\\)"
  1902. "\\("
  1903. "[^" border "]\\|"
  1904. "[^" border "]"
  1905. body1
  1906. "[^" border "]"
  1907. "\\)"
  1908. "\\3\\)"
  1909. "\\([" post "]\\|$\\)")))))
  1910. (defcustom org-emphasis-regexp-components
  1911. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1912. "Components used to build the regular expression for emphasis.
  1913. This is a list with 6 entries. Terminology: In an emphasis string
  1914. like \" *strong word* \", we call the initial space PREMATCH, the final
  1915. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1916. and \"trong wor\" is the body. The different components in this variable
  1917. specify what is allowed/forbidden in each part:
  1918. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1919. post Chars allowed as postmatch. End of line will be allowed too.
  1920. border The chars *forbidden* as border characters.
  1921. body-regexp A regexp like \".\" to match a body character. Don't use
  1922. non-shy groups here, and don't allow newline here.
  1923. newline The maximum number of newlines allowed in an emphasis exp.
  1924. Use customize to modify this, or restart Emacs after changing it."
  1925. :group 'org-font-lock
  1926. :set 'org-set-emph-re
  1927. :type '(list
  1928. (sexp :tag "Allowed chars in pre ")
  1929. (sexp :tag "Allowed chars in post ")
  1930. (sexp :tag "Forbidden chars in border ")
  1931. (sexp :tag "Regexp for body ")
  1932. (integer :tag "number of newlines allowed")
  1933. (option (boolean :tag "Stacking (DISABLED) "))))
  1934. (defcustom org-emphasis-alist
  1935. '(("*" bold "<b>" "</b>")
  1936. ("/" italic "<i>" "</i>")
  1937. ("_" underline "<u>" "</u>")
  1938. ("=" org-code "<code>" "</code>" verbatim)
  1939. ("~" org-verbatim "" "" verbatim)
  1940. ("+" (:strike-through t) "<del>" "</del>")
  1941. )
  1942. "Special syntax for emphasized text.
  1943. Text starting and ending with a special character will be emphasized, for
  1944. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1945. characters, the face to be used by font-lock for highlighting in Org-mode
  1946. Emacs buffers, and the HTML tags to be used for this.
  1947. Use customize to modify this, or restart Emacs after changing it."
  1948. :group 'org-font-lock
  1949. :set 'org-set-emph-re
  1950. :type '(repeat
  1951. (list
  1952. (string :tag "Marker character")
  1953. (choice
  1954. (face :tag "Font-lock-face")
  1955. (plist :tag "Face property list"))
  1956. (string :tag "HTML start tag")
  1957. (string :tag "HTML end tag")
  1958. (option (const verbatim)))))
  1959. ;;; Miscellaneous options
  1960. (defgroup org-completion nil
  1961. "Completion in Org-mode."
  1962. :tag "Org Completion"
  1963. :group 'org)
  1964. (defcustom org-completion-fallback-command 'hippie-expand
  1965. "The expansion command called by \\[org-complete] in normal context.
  1966. Normal means, no org-mode-specific context."
  1967. :group 'org-completion
  1968. :type 'function)
  1969. ;;; The faces
  1970. (defgroup org-faces nil
  1971. "Faces in Org-mode."
  1972. :tag "Org Faces"
  1973. :group 'org-font-lock)
  1974. (defface org-hide
  1975. '((((background light)) (:foreground "white"))
  1976. (((background dark)) (:foreground "black")))
  1977. "Face used to hide leading stars in headlines.
  1978. The forground color of this face should be equal to the background
  1979. color of the frame."
  1980. :group 'org-faces)
  1981. (defface org-level-1 ;; font-lock-function-name-face
  1982. (org-compatible-face 'outline-1
  1983. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1984. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1985. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1986. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1987. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  1988. (t (:bold t))))
  1989. "Face used for level 1 headlines."
  1990. :group 'org-faces)
  1991. (defface org-level-2 ;; font-lock-variable-name-face
  1992. (org-compatible-face 'outline-2
  1993. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  1994. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  1995. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  1996. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  1997. (t (:bold t))))
  1998. "Face used for level 2 headlines."
  1999. :group 'org-faces)
  2000. (defface org-level-3 ;; font-lock-keyword-face
  2001. (org-compatible-face 'outline-3
  2002. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2003. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2004. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2005. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2006. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2007. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2008. (t (:bold t))))
  2009. "Face used for level 3 headlines."
  2010. :group 'org-faces)
  2011. (defface org-level-4 ;; font-lock-comment-face
  2012. (org-compatible-face 'outline-4
  2013. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2014. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2015. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2016. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2017. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2018. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2019. (t (:bold t))))
  2020. "Face used for level 4 headlines."
  2021. :group 'org-faces)
  2022. (defface org-level-5 ;; font-lock-type-face
  2023. (org-compatible-face 'outline-5
  2024. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2025. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2026. (((class color) (min-colors 8)) (:foreground "green"))))
  2027. "Face used for level 5 headlines."
  2028. :group 'org-faces)
  2029. (defface org-level-6 ;; font-lock-constant-face
  2030. (org-compatible-face 'outline-6
  2031. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2032. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2033. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2034. "Face used for level 6 headlines."
  2035. :group 'org-faces)
  2036. (defface org-level-7 ;; font-lock-builtin-face
  2037. (org-compatible-face 'outline-7
  2038. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2039. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2040. (((class color) (min-colors 8)) (:foreground "blue"))))
  2041. "Face used for level 7 headlines."
  2042. :group 'org-faces)
  2043. (defface org-level-8 ;; font-lock-string-face
  2044. (org-compatible-face 'outline-8
  2045. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2046. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2047. (((class color) (min-colors 8)) (:foreground "green"))))
  2048. "Face used for level 8 headlines."
  2049. :group 'org-faces)
  2050. (defface org-special-keyword ;; font-lock-string-face
  2051. (org-compatible-face nil
  2052. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2053. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2054. (t (:italic t))))
  2055. "Face used for special keywords."
  2056. :group 'org-faces)
  2057. (defface org-drawer ;; font-lock-function-name-face
  2058. (org-compatible-face nil
  2059. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2060. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2061. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2062. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2063. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2064. (t (:bold t))))
  2065. "Face used for drawers."
  2066. :group 'org-faces)
  2067. (defface org-property-value nil
  2068. "Face used for the value of a property."
  2069. :group 'org-faces)
  2070. (defface org-column
  2071. (org-compatible-face nil
  2072. '((((class color) (min-colors 16) (background light))
  2073. (:background "grey90"))
  2074. (((class color) (min-colors 16) (background dark))
  2075. (:background "grey30"))
  2076. (((class color) (min-colors 8))
  2077. (:background "cyan" :foreground "black"))
  2078. (t (:inverse-video t))))
  2079. "Face for column display of entry properties."
  2080. :group 'org-faces)
  2081. (when (fboundp 'set-face-attribute)
  2082. ;; Make sure that a fixed-width face is used when we have a column table.
  2083. (set-face-attribute 'org-column nil
  2084. :height (face-attribute 'default :height)
  2085. :family (face-attribute 'default :family)))
  2086. (defface org-warning
  2087. (org-compatible-face 'font-lock-warning-face
  2088. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2089. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2090. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2091. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2092. (t (:bold t))))
  2093. "Face for deadlines and TODO keywords."
  2094. :group 'org-faces)
  2095. (defface org-archived ; similar to shadow
  2096. (org-compatible-face 'shadow
  2097. '((((class color grayscale) (min-colors 88) (background light))
  2098. (:foreground "grey50"))
  2099. (((class color grayscale) (min-colors 88) (background dark))
  2100. (:foreground "grey70"))
  2101. (((class color) (min-colors 8) (background light))
  2102. (:foreground "green"))
  2103. (((class color) (min-colors 8) (background dark))
  2104. (:foreground "yellow"))))
  2105. "Face for headline with the ARCHIVE tag."
  2106. :group 'org-faces)
  2107. (defface org-link
  2108. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2109. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2110. (t (:underline t)))
  2111. "Face for links."
  2112. :group 'org-faces)
  2113. (defface org-ellipsis
  2114. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  2115. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  2116. (t (:strike-through t)))
  2117. "Face for the ellipsis in folded text."
  2118. :group 'org-faces)
  2119. (defface org-target
  2120. '((((class color) (background light)) (:underline t))
  2121. (((class color) (background dark)) (:underline t))
  2122. (t (:underline t)))
  2123. "Face for links."
  2124. :group 'org-faces)
  2125. (defface org-date
  2126. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2127. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2128. (t (:underline t)))
  2129. "Face for links."
  2130. :group 'org-faces)
  2131. (defface org-sexp-date
  2132. '((((class color) (background light)) (:foreground "Purple"))
  2133. (((class color) (background dark)) (:foreground "Cyan"))
  2134. (t (:underline t)))
  2135. "Face for links."
  2136. :group 'org-faces)
  2137. (defface org-tag
  2138. '((t (:bold t)))
  2139. "Face for tags."
  2140. :group 'org-faces)
  2141. (defface org-todo ; font-lock-warning-face
  2142. (org-compatible-face nil
  2143. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2144. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2145. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2146. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2147. (t (:inverse-video t :bold t))))
  2148. "Face for TODO keywords."
  2149. :group 'org-faces)
  2150. (defface org-done ;; font-lock-type-face
  2151. (org-compatible-face nil
  2152. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2153. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2154. (((class color) (min-colors 8)) (:foreground "green"))
  2155. (t (:bold t))))
  2156. "Face used for todo keywords that indicate DONE items."
  2157. :group 'org-faces)
  2158. (defface org-headline-done ;; font-lock-string-face
  2159. (org-compatible-face nil
  2160. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2161. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2162. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2163. "Face used to indicate that a headline is DONE.
  2164. This face is only used if `org-fontify-done-headline' is set. If applies
  2165. to the part of the headline after the DONE keyword."
  2166. :group 'org-faces)
  2167. (defcustom org-todo-keyword-faces nil
  2168. "Faces for specific TODO keywords.
  2169. This is a list of cons cells, with TODO keywords in the car
  2170. and faces in the cdr. The face can be a symbol, or a property
  2171. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  2172. :group 'org-faces
  2173. :group 'org-todo
  2174. :type '(repeat
  2175. (cons
  2176. (string :tag "keyword")
  2177. (sexp :tag "face"))))
  2178. (defface org-table ;; font-lock-function-name-face
  2179. (org-compatible-face nil
  2180. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2181. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2182. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2183. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2184. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2185. (((class color) (min-colors 8) (background dark)))))
  2186. "Face used for tables."
  2187. :group 'org-faces)
  2188. (defface org-formula
  2189. (org-compatible-face nil
  2190. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2191. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2192. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2193. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2194. (t (:bold t :italic t))))
  2195. "Face for formulas."
  2196. :group 'org-faces)
  2197. (defface org-code
  2198. (org-compatible-face nil
  2199. '((((class color grayscale) (min-colors 88) (background light))
  2200. (:foreground "grey50"))
  2201. (((class color grayscale) (min-colors 88) (background dark))
  2202. (:foreground "grey70"))
  2203. (((class color) (min-colors 8) (background light))
  2204. (:foreground "green"))
  2205. (((class color) (min-colors 8) (background dark))
  2206. (:foreground "yellow"))))
  2207. "Face for fixed-with text like code snippets."
  2208. :group 'org-faces
  2209. :version "22.1")
  2210. (defface org-verbatim
  2211. (org-compatible-face nil
  2212. '((((class color grayscale) (min-colors 88) (background light))
  2213. (:foreground "grey50" :underline t))
  2214. (((class color grayscale) (min-colors 88) (background dark))
  2215. (:foreground "grey70" :underline t))
  2216. (((class color) (min-colors 8) (background light))
  2217. (:foreground "green" :underline t))
  2218. (((class color) (min-colors 8) (background dark))
  2219. (:foreground "yellow" :underline t))))
  2220. "Face for fixed-with text like code snippets."
  2221. :group 'org-faces
  2222. :version "22.1")
  2223. (defface org-agenda-structure ;; font-lock-function-name-face
  2224. (org-compatible-face nil
  2225. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2226. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2227. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2228. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2229. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2230. (t (:bold t))))
  2231. "Face used in agenda for captions and dates."
  2232. :group 'org-faces)
  2233. (defface org-scheduled-today
  2234. (org-compatible-face nil
  2235. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2236. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2237. (((class color) (min-colors 8)) (:foreground "green"))
  2238. (t (:bold t :italic t))))
  2239. "Face for items scheduled for a certain day."
  2240. :group 'org-faces)
  2241. (defface org-scheduled-previously
  2242. (org-compatible-face nil
  2243. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2244. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2245. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2246. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2247. (t (:bold t))))
  2248. "Face for items scheduled previously, and not yet done."
  2249. :group 'org-faces)
  2250. (defface org-upcoming-deadline
  2251. (org-compatible-face nil
  2252. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2253. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2254. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2255. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2256. (t (:bold t))))
  2257. "Face for items scheduled previously, and not yet done."
  2258. :group 'org-faces)
  2259. (defcustom org-agenda-deadline-faces
  2260. '((1.0 . org-warning)
  2261. (0.5 . org-upcoming-deadline)
  2262. (0.0 . default))
  2263. "Faces for showing deadlines in the agenda.
  2264. This is a list of cons cells. The cdr of each cell is a face to be used,
  2265. and it can also just be like '(:foreground \"yellow\").
  2266. Each car is a fraction of the head-warning time that must have passed for
  2267. this the face in the cdr to be used for display. The numbers must be
  2268. given in descending order. The head-warning time is normally taken
  2269. from `org-deadline-warning-days', but can also be specified in the deadline
  2270. timestamp itself, like this:
  2271. DEADLINE: <2007-08-13 Mon -8d>
  2272. You may use d for days, w for weeks, m for months and y for years. Months
  2273. and years will only be treated in an approximate fashion (30.4 days for a
  2274. month and 365.24 days for a year)."
  2275. :group 'org-faces
  2276. :group 'org-agenda-daily/weekly
  2277. :type '(repeat
  2278. (cons
  2279. (number :tag "Fraction of head-warning time passed")
  2280. (sexp :tag "Face"))))
  2281. (defface org-agenda-restriction-lock
  2282. (org-compatible-face nil
  2283. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  2284. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  2285. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  2286. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  2287. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  2288. (t (:inverse-video t))))
  2289. "Face for showing the agenda restriction lock."
  2290. :group 'org-faces)
  2291. (defface org-time-grid ;; font-lock-variable-name-face
  2292. (org-compatible-face nil
  2293. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2294. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2295. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2296. "Face used for time grids."
  2297. :group 'org-faces)
  2298. (defconst org-level-faces
  2299. '(org-level-1 org-level-2 org-level-3 org-level-4
  2300. org-level-5 org-level-6 org-level-7 org-level-8
  2301. ))
  2302. (defcustom org-n-level-faces (length org-level-faces)
  2303. "The number of different faces to be used for headlines.
  2304. Org-mode defines 8 different headline faces, so this can be at most 8.
  2305. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  2306. :type 'number
  2307. :group 'org-faces)
  2308. ;;; Functions and variables from ther packages
  2309. ;; Declared here to avoid compiler warnings
  2310. ;; XEmacs only
  2311. (defvar outline-mode-menu-heading)
  2312. (defvar outline-mode-menu-show)
  2313. (defvar outline-mode-menu-hide)
  2314. (defvar zmacs-regions) ; XEmacs regions
  2315. ;; Emacs only
  2316. (defvar mark-active)
  2317. ;; Various packages
  2318. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  2319. (declare-function calendar-forward-day "cal-move" (arg))
  2320. (declare-function calendar-goto-date "cal-move" (date))
  2321. (declare-function calendar-goto-today "cal-move" ())
  2322. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  2323. (defvar calc-embedded-close-formula)
  2324. (defvar calc-embedded-open-formula)
  2325. (declare-function cdlatex-tab "ext:cdlatex" ())
  2326. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2327. (defvar font-lock-unfontify-region-function)
  2328. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2329. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2330. (defvar iswitchb-temp-buflist)
  2331. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2332. (declare-function org-agenda-skip "org-agenda" ())
  2333. (declare-function org-format-agenda-item "org-agenda"
  2334. (extra txt &optional category tags dotime noprefix remove-re))
  2335. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2336. (declare-function org-agenda-change-all-lines "org-agenda"
  2337. (newhead hdmarker &optional fixface))
  2338. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2339. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2340. (declare-function parse-time-string "parse-time" (string))
  2341. (declare-function remember "remember" (&optional initial))
  2342. (declare-function remember-buffer-desc "remember" ())
  2343. (declare-function remember-finalize "remember" ())
  2344. (defvar remember-save-after-remembering)
  2345. (defvar remember-data-file)
  2346. (defvar remember-register)
  2347. (defvar remember-buffer)
  2348. (defvar remember-handler-functions)
  2349. (defvar remember-annotation-functions)
  2350. (defvar texmathp-why)
  2351. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2352. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2353. (defvar w3m-current-url)
  2354. (defvar w3m-current-title)
  2355. (defvar org-latex-regexps)
  2356. ;;; Autoload and prepare some org modules
  2357. ;; Some table stuff that needs to be defined here, because it is used
  2358. ;; by the functions setting up org-mode or checking for table context.
  2359. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2360. "Detects an org-type or table-type table.")
  2361. (defconst org-table-line-regexp "^[ \t]*|"
  2362. "Detects an org-type table line.")
  2363. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2364. "Detects an org-type table line.")
  2365. (defconst org-table-hline-regexp "^[ \t]*|-"
  2366. "Detects an org-type table hline.")
  2367. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2368. "Detects a table-type table hline.")
  2369. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2370. "Searching from within a table (any type) this finds the first line
  2371. outside the table.")
  2372. ;; Autoload the functions in org-table.el that are needed by functions here.
  2373. (eval-and-compile
  2374. (org-autoload "org-table"
  2375. '(org-table-align org-table-begin org-table-blank-field
  2376. org-table-convert org-table-convert-region org-table-copy-down
  2377. org-table-copy-region org-table-create
  2378. org-table-create-or-convert-from-region
  2379. org-table-create-with-table.el org-table-current-dline
  2380. org-table-cut-region org-table-delete-column org-table-edit-field
  2381. org-table-edit-formulas org-table-end org-table-eval-formula
  2382. org-table-export org-table-field-info
  2383. org-table-get-stored-formulas org-table-goto-column
  2384. org-table-hline-and-move org-table-import org-table-insert-column
  2385. org-table-insert-hline org-table-insert-row org-table-iterate
  2386. org-table-justify-field-maybe org-table-kill-row
  2387. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2388. org-table-move-column org-table-move-column-left
  2389. org-table-move-column-right org-table-move-row
  2390. org-table-move-row-down org-table-move-row-up
  2391. org-table-next-field org-table-next-row org-table-paste-rectangle
  2392. org-table-previous-field org-table-recalculate
  2393. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2394. org-table-toggle-coordinate-overlays
  2395. org-table-toggle-formula-debugger org-table-wrap-region
  2396. orgtbl-mode turn-on-orgtbl)))
  2397. (defun org-at-table-p (&optional table-type)
  2398. "Return t if the cursor is inside an org-type table.
  2399. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2400. (if org-enable-table-editor
  2401. (save-excursion
  2402. (beginning-of-line 1)
  2403. (looking-at (if table-type org-table-any-line-regexp
  2404. org-table-line-regexp)))
  2405. nil))
  2406. (defsubst org-table-p () (org-at-table-p))
  2407. (defun org-at-table.el-p ()
  2408. "Return t if and only if we are at a table.el table."
  2409. (and (org-at-table-p 'any)
  2410. (save-excursion
  2411. (goto-char (org-table-begin 'any))
  2412. (looking-at org-table1-hline-regexp))))
  2413. (defun org-table-recognize-table.el ()
  2414. "If there is a table.el table nearby, recognize it and move into it."
  2415. (if org-table-tab-recognizes-table.el
  2416. (if (org-at-table.el-p)
  2417. (progn
  2418. (beginning-of-line 1)
  2419. (if (looking-at org-table-dataline-regexp)
  2420. nil
  2421. (if (looking-at org-table1-hline-regexp)
  2422. (progn
  2423. (beginning-of-line 2)
  2424. (if (looking-at org-table-any-border-regexp)
  2425. (beginning-of-line -1)))))
  2426. (if (re-search-forward "|" (org-table-end t) t)
  2427. (progn
  2428. (require 'table)
  2429. (if (table--at-cell-p (point))
  2430. t
  2431. (message "recognizing table.el table...")
  2432. (table-recognize-table)
  2433. (message "recognizing table.el table...done")))
  2434. (error "This should not happen..."))
  2435. t)
  2436. nil)
  2437. nil))
  2438. (defun org-at-table-hline-p ()
  2439. "Return t if the cursor is inside a hline in a table."
  2440. (if org-enable-table-editor
  2441. (save-excursion
  2442. (beginning-of-line 1)
  2443. (looking-at org-table-hline-regexp))
  2444. nil))
  2445. (defvar org-table-clean-did-remove-column nil)
  2446. (defun org-table-map-tables (function)
  2447. "Apply FUNCTION to the start of all tables in the buffer."
  2448. (save-excursion
  2449. (save-restriction
  2450. (widen)
  2451. (goto-char (point-min))
  2452. (while (re-search-forward org-table-any-line-regexp nil t)
  2453. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2454. (beginning-of-line 1)
  2455. (if (looking-at org-table-line-regexp)
  2456. (save-excursion (funcall function)))
  2457. (re-search-forward org-table-any-border-regexp nil 1))))
  2458. (message "Mapping tables: done"))
  2459. ;; Declare and autoload functions from org-exp.el
  2460. (declare-function org-default-export-plist "org-exp")
  2461. (declare-function org-infile-export-plist "org-exp")
  2462. (declare-function org-get-current-options "org-exp")
  2463. (eval-and-compile
  2464. (org-autoload "org-exp"
  2465. '(org-export org-export-as-ascii org-export-visible
  2466. org-insert-export-options-template org-export-as-html-and-open
  2467. org-export-as-html-batch org-export-as-html-to-buffer
  2468. org-replace-region-by-html org-export-region-as-html
  2469. org-export-as-html org-export-icalendar-this-file
  2470. org-export-icalendar-all-agenda-files
  2471. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2472. ;; Autoload org-remember
  2473. (eval-and-compile
  2474. (org-autoload "org-remember"
  2475. '(org-remember-insinuate org-remember-annotation
  2476. org-remember-apply-template org-remember org-remember-handler)))
  2477. ;; Autoload org-clock.el
  2478. (defvar org-clock-marker (make-marker)
  2479. "Marker recording the last clock-in.")
  2480. (eval-and-compile
  2481. (org-autoload
  2482. "org-clock"
  2483. '(org-clock-in org-clock-out org-clock-cancel
  2484. org-clock-goto org-clock-sum org-clock-display
  2485. org-remove-clock-overlays org-clock-report
  2486. org-clocktable-shift org-dblock-write:clocktable
  2487. org-get-clocktable)))
  2488. (defun org-clock-update-time-maybe ()
  2489. "If this is a CLOCK line, update it and return t.
  2490. Otherwise, return nil."
  2491. (interactive)
  2492. (save-excursion
  2493. (beginning-of-line 1)
  2494. (skip-chars-forward " \t")
  2495. (when (looking-at org-clock-string)
  2496. (let ((re (concat "[ \t]*" org-clock-string
  2497. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2498. "\\([ \t]*=>.*\\)?"))
  2499. ts te h m s)
  2500. (if (not (looking-at re))
  2501. nil
  2502. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2503. (end-of-line 1)
  2504. (setq ts (match-string 1)
  2505. te (match-string 2))
  2506. (setq s (- (time-to-seconds
  2507. (apply 'encode-time (org-parse-time-string te)))
  2508. (time-to-seconds
  2509. (apply 'encode-time (org-parse-time-string ts))))
  2510. h (floor (/ s 3600))
  2511. s (- s (* 3600 h))
  2512. m (floor (/ s 60))
  2513. s (- s (* 60 s)))
  2514. (insert " => " (format "%2d:%02d" h m))
  2515. t)))))
  2516. (defun org-check-running-clock ()
  2517. "Check if the current buffer contains the running clock.
  2518. If yes, offer to stop it and to save the buffer with the changes."
  2519. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2520. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2521. (buffer-name))))
  2522. (org-clock-out)
  2523. (when (y-or-n-p "Save changed buffer?")
  2524. (save-buffer))))
  2525. (defun org-clocktable-try-shift (dir n)
  2526. "Check if this line starts a clock table, if yes, shift the time block."
  2527. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2528. (org-clocktable-shift dir n)))
  2529. ;;; Variables for pre-computed regular expressions, all buffer local
  2530. (defvar org-drawer-regexp nil
  2531. "Matches first line of a hidden block.")
  2532. (make-variable-buffer-local 'org-drawer-regexp)
  2533. (defvar org-todo-regexp nil
  2534. "Matches any of the TODO state keywords.")
  2535. (make-variable-buffer-local 'org-todo-regexp)
  2536. (defvar org-not-done-regexp nil
  2537. "Matches any of the TODO state keywords except the last one.")
  2538. (make-variable-buffer-local 'org-not-done-regexp)
  2539. (defvar org-todo-line-regexp nil
  2540. "Matches a headline and puts TODO state into group 2 if present.")
  2541. (make-variable-buffer-local 'org-todo-line-regexp)
  2542. (defvar org-complex-heading-regexp nil
  2543. "Matches a headline and puts everything into groups:
  2544. group 1: the stars
  2545. group 2: The todo keyword, maybe
  2546. group 3: Priority cookie
  2547. group 4: True headline
  2548. group 5: Tags")
  2549. (make-variable-buffer-local 'org-complex-heading-regexp)
  2550. (defvar org-todo-line-tags-regexp nil
  2551. "Matches a headline and puts TODO state into group 2 if present.
  2552. Also put tags into group 4 if tags are present.")
  2553. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2554. (defvar org-nl-done-regexp nil
  2555. "Matches newline followed by a headline with the DONE keyword.")
  2556. (make-variable-buffer-local 'org-nl-done-regexp)
  2557. (defvar org-looking-at-done-regexp nil
  2558. "Matches the DONE keyword a point.")
  2559. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2560. (defvar org-ds-keyword-length 12
  2561. "Maximum length of the Deadline and SCHEDULED keywords.")
  2562. (make-variable-buffer-local 'org-ds-keyword-length)
  2563. (defvar org-deadline-regexp nil
  2564. "Matches the DEADLINE keyword.")
  2565. (make-variable-buffer-local 'org-deadline-regexp)
  2566. (defvar org-deadline-time-regexp nil
  2567. "Matches the DEADLINE keyword together with a time stamp.")
  2568. (make-variable-buffer-local 'org-deadline-time-regexp)
  2569. (defvar org-deadline-line-regexp nil
  2570. "Matches the DEADLINE keyword and the rest of the line.")
  2571. (make-variable-buffer-local 'org-deadline-line-regexp)
  2572. (defvar org-scheduled-regexp nil
  2573. "Matches the SCHEDULED keyword.")
  2574. (make-variable-buffer-local 'org-scheduled-regexp)
  2575. (defvar org-scheduled-time-regexp nil
  2576. "Matches the SCHEDULED keyword together with a time stamp.")
  2577. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2578. (defvar org-closed-time-regexp nil
  2579. "Matches the CLOSED keyword together with a time stamp.")
  2580. (make-variable-buffer-local 'org-closed-time-regexp)
  2581. (defvar org-keyword-time-regexp nil
  2582. "Matches any of the 4 keywords, together with the time stamp.")
  2583. (make-variable-buffer-local 'org-keyword-time-regexp)
  2584. (defvar org-keyword-time-not-clock-regexp nil
  2585. "Matches any of the 3 keywords, together with the time stamp.")
  2586. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2587. (defvar org-maybe-keyword-time-regexp nil
  2588. "Matches a timestamp, possibly preceeded by a keyword.")
  2589. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2590. (defvar org-planning-or-clock-line-re nil
  2591. "Matches a line with planning or clock info.")
  2592. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2593. (defconst org-plain-time-of-day-regexp
  2594. (concat
  2595. "\\(\\<[012]?[0-9]"
  2596. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2597. "\\(--?"
  2598. "\\(\\<[012]?[0-9]"
  2599. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2600. "\\)?")
  2601. "Regular expression to match a plain time or time range.
  2602. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2603. groups carry important information:
  2604. 0 the full match
  2605. 1 the first time, range or not
  2606. 8 the second time, if it is a range.")
  2607. (defconst org-plain-time-extension-regexp
  2608. (concat
  2609. "\\(\\<[012]?[0-9]"
  2610. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2611. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2612. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2613. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2614. groups carry important information:
  2615. 0 the full match
  2616. 7 hours of duration
  2617. 9 minutes of duration")
  2618. (defconst org-stamp-time-of-day-regexp
  2619. (concat
  2620. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2621. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2622. "\\(--?"
  2623. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2624. "Regular expression to match a timestamp time or time range.
  2625. After a match, the following groups carry important information:
  2626. 0 the full match
  2627. 1 date plus weekday, for backreferencing to make sure both times on same day
  2628. 2 the first time, range or not
  2629. 4 the second time, if it is a range.")
  2630. (defconst org-startup-options
  2631. '(("fold" org-startup-folded t)
  2632. ("overview" org-startup-folded t)
  2633. ("nofold" org-startup-folded nil)
  2634. ("showall" org-startup-folded nil)
  2635. ("content" org-startup-folded content)
  2636. ("hidestars" org-hide-leading-stars t)
  2637. ("showstars" org-hide-leading-stars nil)
  2638. ("odd" org-odd-levels-only t)
  2639. ("oddeven" org-odd-levels-only nil)
  2640. ("align" org-startup-align-all-tables t)
  2641. ("noalign" org-startup-align-all-tables nil)
  2642. ("customtime" org-display-custom-times t)
  2643. ("logdone" org-log-done time)
  2644. ("lognotedone" org-log-done note)
  2645. ("nologdone" org-log-done nil)
  2646. ("lognoteclock-out" org-log-note-clock-out t)
  2647. ("nolognoteclock-out" org-log-note-clock-out nil)
  2648. ("logrepeat" org-log-repeat state)
  2649. ("lognoterepeat" org-log-repeat note)
  2650. ("nologrepeat" org-log-repeat nil)
  2651. ("constcgs" constants-unit-system cgs)
  2652. ("constSI" constants-unit-system SI))
  2653. "Variable associated with STARTUP options for org-mode.
  2654. Each element is a list of three items: The startup options as written
  2655. in the #+STARTUP line, the corresponding variable, and the value to
  2656. set this variable to if the option is found. An optional forth element PUSH
  2657. means to push this value onto the list in the variable.")
  2658. (defun org-set-regexps-and-options ()
  2659. "Precompute regular expressions for current buffer."
  2660. (when (org-mode-p)
  2661. (org-set-local 'org-todo-kwd-alist nil)
  2662. (org-set-local 'org-todo-key-alist nil)
  2663. (org-set-local 'org-todo-key-trigger nil)
  2664. (org-set-local 'org-todo-keywords-1 nil)
  2665. (org-set-local 'org-done-keywords nil)
  2666. (org-set-local 'org-todo-heads nil)
  2667. (org-set-local 'org-todo-sets nil)
  2668. (org-set-local 'org-todo-log-states nil)
  2669. (let ((re (org-make-options-regexp
  2670. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2671. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2672. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2673. (splitre "[ \t]+")
  2674. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2675. tail sep kws1 prio props drawers)
  2676. (save-excursion
  2677. (save-restriction
  2678. (widen)
  2679. (goto-char (point-min))
  2680. (while (re-search-forward re nil t)
  2681. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2682. (cond
  2683. ((equal key "CATEGORY")
  2684. (if (string-match "[ \t]+$" value)
  2685. (setq value (replace-match "" t t value)))
  2686. (setq cat value))
  2687. ((member key '("SEQ_TODO" "TODO"))
  2688. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2689. ((equal key "TYP_TODO")
  2690. (push (cons 'type (org-split-string value splitre)) kwds))
  2691. ((equal key "TAGS")
  2692. (setq tags (append tags (org-split-string value splitre))))
  2693. ((equal key "COLUMNS")
  2694. (org-set-local 'org-columns-default-format value))
  2695. ((equal key "LINK")
  2696. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2697. (push (cons (match-string 1 value)
  2698. (org-trim (match-string 2 value)))
  2699. links)))
  2700. ((equal key "PRIORITIES")
  2701. (setq prio (org-split-string value " +")))
  2702. ((equal key "PROPERTY")
  2703. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2704. (push (cons (match-string 1 value) (match-string 2 value))
  2705. props)))
  2706. ((equal key "DRAWERS")
  2707. (setq drawers (org-split-string value splitre)))
  2708. ((equal key "CONSTANTS")
  2709. (setq const (append const (org-split-string value splitre))))
  2710. ((equal key "STARTUP")
  2711. (let ((opts (org-split-string value splitre))
  2712. l var val)
  2713. (while (setq l (pop opts))
  2714. (when (setq l (assoc l org-startup-options))
  2715. (setq var (nth 1 l) val (nth 2 l))
  2716. (if (not (nth 3 l))
  2717. (set (make-local-variable var) val)
  2718. (if (not (listp (symbol-value var)))
  2719. (set (make-local-variable var) nil))
  2720. (set (make-local-variable var) (symbol-value var))
  2721. (add-to-list var val))))))
  2722. ((equal key "ARCHIVE")
  2723. (string-match " *$" value)
  2724. (setq arch (replace-match "" t t value))
  2725. (remove-text-properties 0 (length arch)
  2726. '(face t fontified t) arch)))
  2727. )))
  2728. (when cat
  2729. (org-set-local 'org-category (intern cat))
  2730. (push (cons "CATEGORY" cat) props))
  2731. (when prio
  2732. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2733. (setq prio (mapcar 'string-to-char prio))
  2734. (org-set-local 'org-highest-priority (nth 0 prio))
  2735. (org-set-local 'org-lowest-priority (nth 1 prio))
  2736. (org-set-local 'org-default-priority (nth 2 prio)))
  2737. (and props (org-set-local 'org-local-properties (nreverse props)))
  2738. (and drawers (org-set-local 'org-drawers drawers))
  2739. (and arch (org-set-local 'org-archive-location arch))
  2740. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2741. ;; Process the TODO keywords
  2742. (unless kwds
  2743. ;; Use the global values as if they had been given locally.
  2744. (setq kwds (default-value 'org-todo-keywords))
  2745. (if (stringp (car kwds))
  2746. (setq kwds (list (cons org-todo-interpretation
  2747. (default-value 'org-todo-keywords)))))
  2748. (setq kwds (reverse kwds)))
  2749. (setq kwds (nreverse kwds))
  2750. (let (inter kws kw)
  2751. (while (setq kws (pop kwds))
  2752. (setq inter (pop kws) sep (member "|" kws)
  2753. kws0 (delete "|" (copy-sequence kws))
  2754. kwsa nil
  2755. kws1 (mapcar
  2756. (lambda (x)
  2757. ;; 1 2
  2758. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2759. (progn
  2760. (setq kw (match-string 1 x)
  2761. key (and (match-end 2) (match-string 2 x))
  2762. log (org-extract-log-state-settings x))
  2763. (push (cons kw (and key (string-to-char key))) kwsa)
  2764. (and log (push log org-todo-log-states))
  2765. kw)
  2766. (error "Invalid TODO keyword %s" x)))
  2767. kws0)
  2768. kwsa (if kwsa (append '((:startgroup))
  2769. (nreverse kwsa)
  2770. '((:endgroup))))
  2771. hw (car kws1)
  2772. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2773. tail (list inter hw (car dws) (org-last dws)))
  2774. (add-to-list 'org-todo-heads hw 'append)
  2775. (push kws1 org-todo-sets)
  2776. (setq org-done-keywords (append org-done-keywords dws nil))
  2777. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2778. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2779. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2780. (setq org-todo-sets (nreverse org-todo-sets)
  2781. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2782. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2783. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2784. ;; Process the constants
  2785. (when const
  2786. (let (e cst)
  2787. (while (setq e (pop const))
  2788. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2789. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2790. (setq org-table-formula-constants-local cst)))
  2791. ;; Process the tags.
  2792. (when tags
  2793. (let (e tgs)
  2794. (while (setq e (pop tags))
  2795. (cond
  2796. ((equal e "{") (push '(:startgroup) tgs))
  2797. ((equal e "}") (push '(:endgroup) tgs))
  2798. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2799. (push (cons (match-string 1 e)
  2800. (string-to-char (match-string 2 e)))
  2801. tgs))
  2802. (t (push (list e) tgs))))
  2803. (org-set-local 'org-tag-alist nil)
  2804. (while (setq e (pop tgs))
  2805. (or (and (stringp (car e))
  2806. (assoc (car e) org-tag-alist))
  2807. (push e org-tag-alist))))))
  2808. ;; Compute the regular expressions and other local variables
  2809. (if (not org-done-keywords)
  2810. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2811. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2812. (length org-scheduled-string)))
  2813. org-drawer-regexp
  2814. (concat "^[ \t]*:\\("
  2815. (mapconcat 'regexp-quote org-drawers "\\|")
  2816. "\\):[ \t]*$")
  2817. org-not-done-keywords
  2818. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2819. org-todo-regexp
  2820. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2821. "\\|") "\\)\\>")
  2822. org-not-done-regexp
  2823. (concat "\\<\\("
  2824. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2825. "\\)\\>")
  2826. org-todo-line-regexp
  2827. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2828. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2829. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2830. org-complex-heading-regexp
  2831. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2832. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2833. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2834. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2835. org-nl-done-regexp
  2836. (concat "\n\\*+[ \t]+"
  2837. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2838. "\\)" "\\>")
  2839. org-todo-line-tags-regexp
  2840. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2841. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2842. (org-re
  2843. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2844. org-looking-at-done-regexp
  2845. (concat "^" "\\(?:"
  2846. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2847. "\\>")
  2848. org-deadline-regexp (concat "\\<" org-deadline-string)
  2849. org-deadline-time-regexp
  2850. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2851. org-deadline-line-regexp
  2852. (concat "\\<\\(" org-deadline-string "\\).*")
  2853. org-scheduled-regexp
  2854. (concat "\\<" org-scheduled-string)
  2855. org-scheduled-time-regexp
  2856. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2857. org-closed-time-regexp
  2858. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2859. org-keyword-time-regexp
  2860. (concat "\\<\\(" org-scheduled-string
  2861. "\\|" org-deadline-string
  2862. "\\|" org-closed-string
  2863. "\\|" org-clock-string "\\)"
  2864. " *[[<]\\([^]>]+\\)[]>]")
  2865. org-keyword-time-not-clock-regexp
  2866. (concat "\\<\\(" org-scheduled-string
  2867. "\\|" org-deadline-string
  2868. "\\|" org-closed-string
  2869. "\\)"
  2870. " *[[<]\\([^]>]+\\)[]>]")
  2871. org-maybe-keyword-time-regexp
  2872. (concat "\\(\\<\\(" org-scheduled-string
  2873. "\\|" org-deadline-string
  2874. "\\|" org-closed-string
  2875. "\\|" org-clock-string "\\)\\)?"
  2876. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2877. org-planning-or-clock-line-re
  2878. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2879. "\\|" org-deadline-string
  2880. "\\|" org-closed-string "\\|" org-clock-string
  2881. "\\)\\>\\)")
  2882. )
  2883. (org-compute-latex-and-specials-regexp)
  2884. (org-set-font-lock-defaults)))
  2885. (defun org-extract-log-state-settings (x)
  2886. "Extract the log state setting from a TODO keyword string.
  2887. This will extract info from a string like \"WAIT(w@/!)\"."
  2888. (let (kw key log1 log2)
  2889. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2890. (setq kw (match-string 1 x)
  2891. key (and (match-end 2) (match-string 2 x))
  2892. log1 (and (match-end 3) (match-string 3 x))
  2893. log2 (and (match-end 4) (match-string 4 x)))
  2894. (and (or log1 log2)
  2895. (list kw
  2896. (and log1 (if (equal log1 "!") 'time 'note))
  2897. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2898. (defun org-remove-keyword-keys (list)
  2899. "Remove a pair of parenthesis at the end of each string in LIST."
  2900. (mapcar (lambda (x)
  2901. (if (string-match "(.*)$" x)
  2902. (substring x 0 (match-beginning 0))
  2903. x))
  2904. list))
  2905. ;; FIXME: this could be done much better, using second characters etc.
  2906. (defun org-assign-fast-keys (alist)
  2907. "Assign fast keys to a keyword-key alist.
  2908. Respect keys that are already there."
  2909. (let (new e k c c1 c2 (char ?a))
  2910. (while (setq e (pop alist))
  2911. (cond
  2912. ((equal e '(:startgroup)) (push e new))
  2913. ((equal e '(:endgroup)) (push e new))
  2914. (t
  2915. (setq k (car e) c2 nil)
  2916. (if (cdr e)
  2917. (setq c (cdr e))
  2918. ;; automatically assign a character.
  2919. (setq c1 (string-to-char
  2920. (downcase (substring
  2921. k (if (= (string-to-char k) ?@) 1 0)))))
  2922. (if (or (rassoc c1 new) (rassoc c1 alist))
  2923. (while (or (rassoc char new) (rassoc char alist))
  2924. (setq char (1+ char)))
  2925. (setq c2 c1))
  2926. (setq c (or c2 char)))
  2927. (push (cons k c) new))))
  2928. (nreverse new)))
  2929. ;;; Some variables used in various places
  2930. (defvar org-window-configuration nil
  2931. "Used in various places to store a window configuration.")
  2932. (defvar org-finish-function nil
  2933. "Function to be called when `C-c C-c' is used.
  2934. This is for getting out of special buffers like remember.")
  2935. ;; FIXME: Occasionally check by commenting these, to make sure
  2936. ;; no other functions uses these, forgetting to let-bind them.
  2937. (defvar entry)
  2938. (defvar state)
  2939. (defvar last-state)
  2940. (defvar date)
  2941. (defvar description)
  2942. ;; Defined somewhere in this file, but used before definition.
  2943. (defvar org-html-entities)
  2944. (defvar org-struct-menu)
  2945. (defvar org-org-menu)
  2946. (defvar org-tbl-menu)
  2947. (defvar org-agenda-keymap)
  2948. ;;;; Define the Org-mode
  2949. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2950. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  2951. ;; We use a before-change function to check if a table might need
  2952. ;; an update.
  2953. (defvar org-table-may-need-update t
  2954. "Indicates that a table might need an update.
  2955. This variable is set by `org-before-change-function'.
  2956. `org-table-align' sets it back to nil.")
  2957. (defun org-before-change-function (beg end)
  2958. "Every change indicates that a table might need an update."
  2959. (setq org-table-may-need-update t))
  2960. (defvar org-mode-map)
  2961. (defvar org-mode-hook nil)
  2962. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2963. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2964. (defvar org-table-buffer-is-an nil)
  2965. (defconst org-outline-regexp "\\*+ ")
  2966. ;;;###autoload
  2967. (define-derived-mode org-mode outline-mode "Org"
  2968. "Outline-based notes management and organizer, alias
  2969. \"Carsten's outline-mode for keeping track of everything.\"
  2970. Org-mode develops organizational tasks around a NOTES file which
  2971. contains information about projects as plain text. Org-mode is
  2972. implemented on top of outline-mode, which is ideal to keep the content
  2973. of large files well structured. It supports ToDo items, deadlines and
  2974. time stamps, which magically appear in the diary listing of the Emacs
  2975. calendar. Tables are easily created with a built-in table editor.
  2976. Plain text URL-like links connect to websites, emails (VM), Usenet
  2977. messages (Gnus), BBDB entries, and any files related to the project.
  2978. For printing and sharing of notes, an Org-mode file (or a part of it)
  2979. can be exported as a structured ASCII or HTML file.
  2980. The following commands are available:
  2981. \\{org-mode-map}"
  2982. ;; Get rid of Outline menus, they are not needed
  2983. ;; Need to do this here because define-derived-mode sets up
  2984. ;; the keymap so late. Still, it is a waste to call this each time
  2985. ;; we switch another buffer into org-mode.
  2986. (if (featurep 'xemacs)
  2987. (when (boundp 'outline-mode-menu-heading)
  2988. ;; Assume this is Greg's port, it used easymenu
  2989. (easy-menu-remove outline-mode-menu-heading)
  2990. (easy-menu-remove outline-mode-menu-show)
  2991. (easy-menu-remove outline-mode-menu-hide))
  2992. (define-key org-mode-map [menu-bar headings] 'undefined)
  2993. (define-key org-mode-map [menu-bar hide] 'undefined)
  2994. (define-key org-mode-map [menu-bar show] 'undefined))
  2995. (org-load-modules-maybe)
  2996. (easy-menu-add org-org-menu)
  2997. (easy-menu-add org-tbl-menu)
  2998. (org-install-agenda-files-menu)
  2999. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3000. (org-add-to-invisibility-spec '(org-cwidth))
  3001. (when (featurep 'xemacs)
  3002. (org-set-local 'line-move-ignore-invisible t))
  3003. (org-set-local 'outline-regexp org-outline-regexp)
  3004. (org-set-local 'outline-level 'org-outline-level)
  3005. (when (and org-ellipsis
  3006. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3007. (fboundp 'make-glyph-code))
  3008. (unless org-display-table
  3009. (setq org-display-table (make-display-table)))
  3010. (set-display-table-slot
  3011. org-display-table 4
  3012. (vconcat (mapcar
  3013. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3014. org-ellipsis)))
  3015. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3016. (setq buffer-display-table org-display-table))
  3017. (org-set-regexps-and-options)
  3018. ;; Calc embedded
  3019. (org-set-local 'calc-embedded-open-mode "# ")
  3020. (modify-syntax-entry ?# "<")
  3021. (modify-syntax-entry ?@ "w")
  3022. (if org-startup-truncated (setq truncate-lines t))
  3023. (org-set-local 'font-lock-unfontify-region-function
  3024. 'org-unfontify-region)
  3025. ;; Activate before-change-function
  3026. (org-set-local 'org-table-may-need-update t)
  3027. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3028. 'local)
  3029. ;; Check for running clock before killing a buffer
  3030. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3031. ;; Paragraphs and auto-filling
  3032. (org-set-autofill-regexps)
  3033. (setq indent-line-function 'org-indent-line-function)
  3034. (org-update-radio-target-regexp)
  3035. ;; Comment characters
  3036. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3037. (org-set-local 'comment-padding " ")
  3038. ;; Align options lines
  3039. (org-set-local
  3040. 'align-mode-rules-list
  3041. '((org-in-buffer-settings
  3042. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3043. (modes . '(org-mode)))))
  3044. ;; Imenu
  3045. (org-set-local 'imenu-create-index-function
  3046. 'org-imenu-get-tree)
  3047. ;; Make isearch reveal context
  3048. (if (or (featurep 'xemacs)
  3049. (not (boundp 'outline-isearch-open-invisible-function)))
  3050. ;; Emacs 21 and XEmacs make use of the hook
  3051. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3052. ;; Emacs 22 deals with this through a special variable
  3053. (org-set-local 'outline-isearch-open-invisible-function
  3054. (lambda (&rest ignore) (org-show-context 'isearch))))
  3055. ;; If empty file that did not turn on org-mode automatically, make it to.
  3056. (if (and org-insert-mode-line-in-empty-file
  3057. (interactive-p)
  3058. (= (point-min) (point-max)))
  3059. (insert "# -*- mode: org -*-\n\n"))
  3060. (unless org-inhibit-startup
  3061. (when org-startup-align-all-tables
  3062. (let ((bmp (buffer-modified-p)))
  3063. (org-table-map-tables 'org-table-align)
  3064. (set-buffer-modified-p bmp)))
  3065. (org-cycle-hide-drawers 'all)
  3066. (cond
  3067. ((eq org-startup-folded t)
  3068. (org-cycle '(4)))
  3069. ((eq org-startup-folded 'content)
  3070. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3071. (org-cycle '(4)) (org-cycle '(4)))))))
  3072. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3073. (defun org-current-time ()
  3074. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3075. (if (> (car org-time-stamp-rounding-minutes) 1)
  3076. (let ((r (car org-time-stamp-rounding-minutes))
  3077. (time (decode-time)))
  3078. (apply 'encode-time
  3079. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3080. (nthcdr 2 time))))
  3081. (current-time)))
  3082. ;;;; Font-Lock stuff, including the activators
  3083. (defvar org-mouse-map (make-sparse-keymap))
  3084. (org-defkey org-mouse-map
  3085. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3086. (org-defkey org-mouse-map
  3087. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3088. (when org-mouse-1-follows-link
  3089. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3090. (when org-tab-follows-link
  3091. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3092. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3093. (when org-return-follows-link
  3094. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3095. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3096. (require 'font-lock)
  3097. (defconst org-non-link-chars "]\t\n\r<>")
  3098. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3099. "shell" "elisp"))
  3100. (defvar org-link-re-with-space nil
  3101. "Matches a link with spaces, optional angular brackets around it.")
  3102. (defvar org-link-re-with-space2 nil
  3103. "Matches a link with spaces, optional angular brackets around it.")
  3104. (defvar org-angle-link-re nil
  3105. "Matches link with angular brackets, spaces are allowed.")
  3106. (defvar org-plain-link-re nil
  3107. "Matches plain link, without spaces.")
  3108. (defvar org-bracket-link-regexp nil
  3109. "Matches a link in double brackets.")
  3110. (defvar org-bracket-link-analytic-regexp nil
  3111. "Regular expression used to analyze links.
  3112. Here is what the match groups contain after a match:
  3113. 1: http:
  3114. 2: http
  3115. 3: path
  3116. 4: [desc]
  3117. 5: desc")
  3118. (defvar org-any-link-re nil
  3119. "Regular expression matching any link.")
  3120. (defun org-make-link-regexps ()
  3121. "Update the link regular expressions.
  3122. This should be called after the variable `org-link-types' has changed."
  3123. (setq org-link-re-with-space
  3124. (concat
  3125. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3126. "\\([^" org-non-link-chars " ]"
  3127. "[^" org-non-link-chars "]*"
  3128. "[^" org-non-link-chars " ]\\)>?")
  3129. org-link-re-with-space2
  3130. (concat
  3131. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3132. "\\([^" org-non-link-chars " ]"
  3133. "[^]\t\n\r]*"
  3134. "[^" org-non-link-chars " ]\\)>?")
  3135. org-angle-link-re
  3136. (concat
  3137. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3138. "\\([^" org-non-link-chars " ]"
  3139. "[^" org-non-link-chars "]*"
  3140. "\\)>")
  3141. org-plain-link-re
  3142. (concat
  3143. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3144. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3145. org-bracket-link-regexp
  3146. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3147. org-bracket-link-analytic-regexp
  3148. (concat
  3149. "\\[\\["
  3150. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3151. "\\([^]]+\\)"
  3152. "\\]"
  3153. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3154. "\\]")
  3155. org-any-link-re
  3156. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3157. org-angle-link-re "\\)\\|\\("
  3158. org-plain-link-re "\\)")))
  3159. (org-make-link-regexps)
  3160. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3161. "Regular expression for fast time stamp matching.")
  3162. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3163. "Regular expression for fast time stamp matching.")
  3164. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3165. "Regular expression matching time strings for analysis.
  3166. This one does not require the space after the date, so it can be used
  3167. on a string that terminates immediately after the date.")
  3168. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3169. "Regular expression matching time strings for analysis.")
  3170. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3171. "Regular expression matching time stamps, with groups.")
  3172. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3173. "Regular expression matching time stamps (also [..]), with groups.")
  3174. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3175. "Regular expression matching a time stamp range.")
  3176. (defconst org-tr-regexp-both
  3177. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3178. "Regular expression matching a time stamp range.")
  3179. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3180. org-ts-regexp "\\)?")
  3181. "Regular expression matching a time stamp or time stamp range.")
  3182. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3183. org-ts-regexp-both "\\)?")
  3184. "Regular expression matching a time stamp or time stamp range.
  3185. The time stamps may be either active or inactive.")
  3186. (defvar org-emph-face nil)
  3187. (defun org-do-emphasis-faces (limit)
  3188. "Run through the buffer and add overlays to links."
  3189. (let (rtn)
  3190. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3191. (if (not (= (char-after (match-beginning 3))
  3192. (char-after (match-beginning 4))))
  3193. (progn
  3194. (setq rtn t)
  3195. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3196. 'face
  3197. (nth 1 (assoc (match-string 3)
  3198. org-emphasis-alist)))
  3199. (add-text-properties (match-beginning 2) (match-end 2)
  3200. '(font-lock-multiline t))
  3201. (when org-hide-emphasis-markers
  3202. (add-text-properties (match-end 4) (match-beginning 5)
  3203. '(invisible org-link))
  3204. (add-text-properties (match-beginning 3) (match-end 3)
  3205. '(invisible org-link)))))
  3206. (backward-char 1))
  3207. rtn))
  3208. (defun org-emphasize (&optional char)
  3209. "Insert or change an emphasis, i.e. a font like bold or italic.
  3210. If there is an active region, change that region to a new emphasis.
  3211. If there is no region, just insert the marker characters and position
  3212. the cursor between them.
  3213. CHAR should be either the marker character, or the first character of the
  3214. HTML tag associated with that emphasis. If CHAR is a space, the means
  3215. to remove the emphasis of the selected region.
  3216. If char is not given (for example in an interactive call) it
  3217. will be prompted for."
  3218. (interactive)
  3219. (let ((eal org-emphasis-alist) e det
  3220. (erc org-emphasis-regexp-components)
  3221. (prompt "")
  3222. (string "") beg end move tag c s)
  3223. (if (org-region-active-p)
  3224. (setq beg (region-beginning) end (region-end)
  3225. string (buffer-substring beg end))
  3226. (setq move t))
  3227. (while (setq e (pop eal))
  3228. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3229. c (aref tag 0))
  3230. (push (cons c (string-to-char (car e))) det)
  3231. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3232. (substring tag 1)))))
  3233. (unless char
  3234. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3235. (setq char (read-char-exclusive)))
  3236. (setq char (or (cdr (assoc char det)) char))
  3237. (if (equal char ?\ )
  3238. (setq s "" move nil)
  3239. (unless (assoc (char-to-string char) org-emphasis-alist)
  3240. (error "No such emphasis marker: \"%c\"" char))
  3241. (setq s (char-to-string char)))
  3242. (while (and (> (length string) 1)
  3243. (equal (substring string 0 1) (substring string -1))
  3244. (assoc (substring string 0 1) org-emphasis-alist))
  3245. (setq string (substring string 1 -1)))
  3246. (setq string (concat s string s))
  3247. (if beg (delete-region beg end))
  3248. (unless (or (bolp)
  3249. (string-match (concat "[" (nth 0 erc) "\n]")
  3250. (char-to-string (char-before (point)))))
  3251. (insert " "))
  3252. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3253. (char-to-string (char-after (point))))
  3254. (insert " ") (backward-char 1))
  3255. (insert string)
  3256. (and move (backward-char 1))))
  3257. (defconst org-nonsticky-props
  3258. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3259. (defun org-activate-plain-links (limit)
  3260. "Run through the buffer and add overlays to links."
  3261. (catch 'exit
  3262. (let (f)
  3263. (while (re-search-forward org-plain-link-re limit t)
  3264. (setq f (get-text-property (match-beginning 0) 'face))
  3265. (if (or (eq f 'org-tag)
  3266. (and (listp f) (memq 'org-tag f)))
  3267. nil
  3268. (add-text-properties (match-beginning 0) (match-end 0)
  3269. (list 'mouse-face 'highlight
  3270. 'rear-nonsticky org-nonsticky-props
  3271. 'keymap org-mouse-map
  3272. ))
  3273. (throw 'exit t))))))
  3274. (defun org-activate-code (limit)
  3275. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3276. (unless (get-text-property (match-beginning 1) 'face)
  3277. (remove-text-properties (match-beginning 0) (match-end 0)
  3278. '(display t invisible t intangible t))
  3279. t)))
  3280. (defun org-activate-angle-links (limit)
  3281. "Run through the buffer and add overlays to links."
  3282. (if (re-search-forward org-angle-link-re limit t)
  3283. (progn
  3284. (add-text-properties (match-beginning 0) (match-end 0)
  3285. (list 'mouse-face 'highlight
  3286. 'rear-nonsticky org-nonsticky-props
  3287. 'keymap org-mouse-map
  3288. ))
  3289. t)))
  3290. (defun org-activate-bracket-links (limit)
  3291. "Run through the buffer and add overlays to bracketed links."
  3292. (if (re-search-forward org-bracket-link-regexp limit t)
  3293. (let* ((help (concat "LINK: "
  3294. (org-match-string-no-properties 1)))
  3295. ;; FIXME: above we should remove the escapes.
  3296. ;; but that requires another match, protecting match data,
  3297. ;; a lot of overhead for font-lock.
  3298. (ip (org-maybe-intangible
  3299. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3300. 'keymap org-mouse-map 'mouse-face 'highlight
  3301. 'font-lock-multiline t 'help-echo help)))
  3302. (vp (list 'rear-nonsticky org-nonsticky-props
  3303. 'keymap org-mouse-map 'mouse-face 'highlight
  3304. ' font-lock-multiline t 'help-echo help)))
  3305. ;; We need to remove the invisible property here. Table narrowing
  3306. ;; may have made some of this invisible.
  3307. (remove-text-properties (match-beginning 0) (match-end 0)
  3308. '(invisible nil))
  3309. (if (match-end 3)
  3310. (progn
  3311. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3312. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3313. (add-text-properties (match-end 3) (match-end 0) ip))
  3314. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3315. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3316. (add-text-properties (match-end 1) (match-end 0) ip))
  3317. t)))
  3318. (defun org-activate-dates (limit)
  3319. "Run through the buffer and add overlays to dates."
  3320. (if (re-search-forward org-tsr-regexp-both limit t)
  3321. (progn
  3322. (add-text-properties (match-beginning 0) (match-end 0)
  3323. (list 'mouse-face 'highlight
  3324. 'rear-nonsticky org-nonsticky-props
  3325. 'keymap org-mouse-map))
  3326. (when org-display-custom-times
  3327. (if (match-end 3)
  3328. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3329. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3330. t)))
  3331. (defvar org-target-link-regexp nil
  3332. "Regular expression matching radio targets in plain text.")
  3333. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3334. "Regular expression matching a link target.")
  3335. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3336. "Regular expression matching a radio target.")
  3337. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3338. "Regular expression matching any target.")
  3339. (defun org-activate-target-links (limit)
  3340. "Run through the buffer and add overlays to target matches."
  3341. (when org-target-link-regexp
  3342. (let ((case-fold-search t))
  3343. (if (re-search-forward org-target-link-regexp limit t)
  3344. (progn
  3345. (add-text-properties (match-beginning 0) (match-end 0)
  3346. (list 'mouse-face 'highlight
  3347. 'rear-nonsticky org-nonsticky-props
  3348. 'keymap org-mouse-map
  3349. 'help-echo "Radio target link"
  3350. 'org-linked-text t))
  3351. t)))))
  3352. (defun org-update-radio-target-regexp ()
  3353. "Find all radio targets in this file and update the regular expression."
  3354. (interactive)
  3355. (when (memq 'radio org-activate-links)
  3356. (setq org-target-link-regexp
  3357. (org-make-target-link-regexp (org-all-targets 'radio)))
  3358. (org-restart-font-lock)))
  3359. (defun org-hide-wide-columns (limit)
  3360. (let (s e)
  3361. (setq s (text-property-any (point) (or limit (point-max))
  3362. 'org-cwidth t))
  3363. (when s
  3364. (setq e (next-single-property-change s 'org-cwidth))
  3365. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3366. (goto-char e)
  3367. t)))
  3368. (defvar org-latex-and-specials-regexp nil
  3369. "Regular expression for highlighting export special stuff.")
  3370. (defvar org-match-substring-regexp)
  3371. (defvar org-match-substring-with-braces-regexp)
  3372. (defvar org-export-html-special-string-regexps)
  3373. (defun org-compute-latex-and-specials-regexp ()
  3374. "Compute regular expression for stuff treated specially by exporters."
  3375. (if (not org-highlight-latex-fragments-and-specials)
  3376. (org-set-local 'org-latex-and-specials-regexp nil)
  3377. (require 'org-exp)
  3378. (let*
  3379. ((matchers (plist-get org-format-latex-options :matchers))
  3380. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3381. org-latex-regexps)))
  3382. (options (org-combine-plists (org-default-export-plist)
  3383. (org-infile-export-plist)))
  3384. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3385. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3386. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3387. (org-export-html-expand (plist-get options :expand-quoted-html))
  3388. (org-export-with-special-strings (plist-get options :special-strings))
  3389. (re-sub
  3390. (cond
  3391. ((equal org-export-with-sub-superscripts '{})
  3392. (list org-match-substring-with-braces-regexp))
  3393. (org-export-with-sub-superscripts
  3394. (list org-match-substring-regexp))
  3395. (t nil)))
  3396. (re-latex
  3397. (if org-export-with-LaTeX-fragments
  3398. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3399. (re-macros
  3400. (if org-export-with-TeX-macros
  3401. (list (concat "\\\\"
  3402. (regexp-opt
  3403. (append (mapcar 'car org-html-entities)
  3404. (if (boundp 'org-latex-entities)
  3405. org-latex-entities nil))
  3406. 'words))) ; FIXME
  3407. ))
  3408. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3409. (re-special (if org-export-with-special-strings
  3410. (mapcar (lambda (x) (car x))
  3411. org-export-html-special-string-regexps)))
  3412. (re-rest
  3413. (delq nil
  3414. (list
  3415. (if org-export-html-expand "@<[^>\n]+>")
  3416. ))))
  3417. (org-set-local
  3418. 'org-latex-and-specials-regexp
  3419. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3420. re-rest) "\\|")))))
  3421. (defface org-latex-and-export-specials
  3422. (let ((font (cond ((assq :inherit custom-face-attributes)
  3423. '(:inherit underline))
  3424. (t '(:underline t)))))
  3425. `((((class grayscale) (background light))
  3426. (:foreground "DimGray" ,@font))
  3427. (((class grayscale) (background dark))
  3428. (:foreground "LightGray" ,@font))
  3429. (((class color) (background light))
  3430. (:foreground "SaddleBrown"))
  3431. (((class color) (background dark))
  3432. (:foreground "burlywood"))
  3433. (t (,@font))))
  3434. "Face used to highlight math latex and other special exporter stuff."
  3435. :group 'org-faces)
  3436. (defun org-do-latex-and-special-faces (limit)
  3437. "Run through the buffer and add overlays to links."
  3438. (when org-latex-and-specials-regexp
  3439. (let (rtn d)
  3440. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3441. limit t))
  3442. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3443. 'face))
  3444. '(org-code org-verbatim underline)))
  3445. (progn
  3446. (setq rtn t
  3447. d (cond ((member (char-after (1+ (match-beginning 0)))
  3448. '(?_ ?^)) 1)
  3449. (t 0)))
  3450. (font-lock-prepend-text-property
  3451. (+ d (match-beginning 0)) (match-end 0)
  3452. 'face 'org-latex-and-export-specials)
  3453. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3454. '(font-lock-multiline t)))))
  3455. rtn)))
  3456. (defun org-restart-font-lock ()
  3457. "Restart font-lock-mode, to force refontification."
  3458. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3459. (font-lock-mode -1)
  3460. (font-lock-mode 1)))
  3461. (defun org-all-targets (&optional radio)
  3462. "Return a list of all targets in this file.
  3463. With optional argument RADIO, only find radio targets."
  3464. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3465. rtn)
  3466. (save-excursion
  3467. (goto-char (point-min))
  3468. (while (re-search-forward re nil t)
  3469. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3470. rtn)))
  3471. (defun org-make-target-link-regexp (targets)
  3472. "Make regular expression matching all strings in TARGETS.
  3473. The regular expression finds the targets also if there is a line break
  3474. between words."
  3475. (and targets
  3476. (concat
  3477. "\\<\\("
  3478. (mapconcat
  3479. (lambda (x)
  3480. (while (string-match " +" x)
  3481. (setq x (replace-match "\\s-+" t t x)))
  3482. x)
  3483. targets
  3484. "\\|")
  3485. "\\)\\>")))
  3486. (defun org-activate-tags (limit)
  3487. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3488. (progn
  3489. (add-text-properties (match-beginning 1) (match-end 1)
  3490. (list 'mouse-face 'highlight
  3491. 'rear-nonsticky org-nonsticky-props
  3492. 'keymap org-mouse-map))
  3493. t)))
  3494. (defun org-outline-level ()
  3495. (save-excursion
  3496. (looking-at outline-regexp)
  3497. (if (match-beginning 1)
  3498. (+ (org-get-string-indentation (match-string 1)) 1000)
  3499. (1- (- (match-end 0) (match-beginning 0))))))
  3500. (defvar org-font-lock-keywords nil)
  3501. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  3502. "Regular expression matching a property line.")
  3503. (defun org-set-font-lock-defaults ()
  3504. (let* ((em org-fontify-emphasized-text)
  3505. (lk org-activate-links)
  3506. (org-font-lock-extra-keywords
  3507. (list
  3508. ;; Headlines
  3509. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3510. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3511. ;; Table lines
  3512. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3513. (1 'org-table t))
  3514. ;; Table internals
  3515. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3516. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3517. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3518. ;; Drawers
  3519. (list org-drawer-regexp '(0 'org-special-keyword t))
  3520. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3521. ;; Properties
  3522. (list org-property-re
  3523. '(1 'org-special-keyword t)
  3524. '(3 'org-property-value t))
  3525. (if org-format-transports-properties-p
  3526. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3527. ;; Links
  3528. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3529. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3530. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3531. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3532. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3533. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3534. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3535. '(org-hide-wide-columns (0 nil append))
  3536. ;; TODO lines
  3537. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3538. '(1 (org-get-todo-face 1) t))
  3539. ;; DONE
  3540. (if org-fontify-done-headline
  3541. (list (concat "^[*]+ +\\<\\("
  3542. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3543. "\\)\\(.*\\)")
  3544. '(2 'org-headline-done t))
  3545. nil)
  3546. ;; Priorities
  3547. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3548. ;; Special keywords
  3549. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3550. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3551. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3552. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3553. ;; Emphasis
  3554. (if em
  3555. (if (featurep 'xemacs)
  3556. '(org-do-emphasis-faces (0 nil append))
  3557. '(org-do-emphasis-faces)))
  3558. ;; Checkboxes
  3559. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3560. 2 'bold prepend)
  3561. (if org-provide-checkbox-statistics
  3562. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3563. (0 (org-get-checkbox-statistics-face) t)))
  3564. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3565. '(1 'org-archived prepend))
  3566. ;; Specials
  3567. '(org-do-latex-and-special-faces)
  3568. ;; Code
  3569. '(org-activate-code (1 'org-code t))
  3570. ;; COMMENT
  3571. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3572. "\\|" org-quote-string "\\)\\>")
  3573. '(1 'org-special-keyword t))
  3574. '("^#.*" (0 'font-lock-comment-face t))
  3575. )))
  3576. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3577. ;; Now set the full font-lock-keywords
  3578. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3579. (org-set-local 'font-lock-defaults
  3580. '(org-font-lock-keywords t nil nil backward-paragraph))
  3581. (kill-local-variable 'font-lock-keywords) nil))
  3582. (defvar org-m nil)
  3583. (defvar org-l nil)
  3584. (defvar org-f nil)
  3585. (defun org-get-level-face (n)
  3586. "Get the right face for match N in font-lock matching of healdines."
  3587. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3588. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3589. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3590. (cond
  3591. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3592. ((eq n 2) org-f)
  3593. (t (if org-level-color-stars-only nil org-f))))
  3594. (defun org-get-todo-face (kwd)
  3595. "Get the right face for a TODO keyword KWD.
  3596. If KWD is a number, get the corresponding match group."
  3597. (if (numberp kwd) (setq kwd (match-string kwd)))
  3598. (or (cdr (assoc kwd org-todo-keyword-faces))
  3599. (and (member kwd org-done-keywords) 'org-done)
  3600. 'org-todo))
  3601. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3602. "Remove fontification and activation overlays from links."
  3603. (font-lock-default-unfontify-region beg end)
  3604. (let* ((buffer-undo-list t)
  3605. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3606. (inhibit-modification-hooks t)
  3607. deactivate-mark buffer-file-name buffer-file-truename)
  3608. (remove-text-properties beg end
  3609. '(mouse-face t keymap t org-linked-text t
  3610. invisible t intangible t))))
  3611. ;;;; Visibility cycling, including org-goto and indirect buffer
  3612. ;;; Cycling
  3613. (defvar org-cycle-global-status nil)
  3614. (make-variable-buffer-local 'org-cycle-global-status)
  3615. (defvar org-cycle-subtree-status nil)
  3616. (make-variable-buffer-local 'org-cycle-subtree-status)
  3617. ;;;###autoload
  3618. (defun org-cycle (&optional arg)
  3619. "Visibility cycling for Org-mode.
  3620. - When this function is called with a prefix argument, rotate the entire
  3621. buffer through 3 states (global cycling)
  3622. 1. OVERVIEW: Show only top-level headlines.
  3623. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3624. 3. SHOW ALL: Show everything.
  3625. - When point is at the beginning of a headline, rotate the subtree started
  3626. by this line through 3 different states (local cycling)
  3627. 1. FOLDED: Only the main headline is shown.
  3628. 2. CHILDREN: The main headline and the direct children are shown.
  3629. From this state, you can move to one of the children
  3630. and zoom in further.
  3631. 3. SUBTREE: Show the entire subtree, including body text.
  3632. - When there is a numeric prefix, go up to a heading with level ARG, do
  3633. a `show-subtree' and return to the previous cursor position. If ARG
  3634. is negative, go up that many levels.
  3635. - When point is not at the beginning of a headline, execute
  3636. `indent-relative', like TAB normally does. See the option
  3637. `org-cycle-emulate-tab' for details.
  3638. - Special case: if point is at the beginning of the buffer and there is
  3639. no headline in line 1, this function will act as if called with prefix arg.
  3640. But only if also the variable `org-cycle-global-at-bob' is t."
  3641. (interactive "P")
  3642. (org-load-modules-maybe)
  3643. (let* ((outline-regexp
  3644. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3645. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3646. outline-regexp))
  3647. (bob-special (and org-cycle-global-at-bob (bobp)
  3648. (not (looking-at outline-regexp))))
  3649. (org-cycle-hook
  3650. (if bob-special
  3651. (delq 'org-optimize-window-after-visibility-change
  3652. (copy-sequence org-cycle-hook))
  3653. org-cycle-hook))
  3654. (pos (point)))
  3655. (if (or bob-special (equal arg '(4)))
  3656. ;; special case: use global cycling
  3657. (setq arg t))
  3658. (cond
  3659. ((org-at-table-p 'any)
  3660. ;; Enter the table or move to the next field in the table
  3661. (or (org-table-recognize-table.el)
  3662. (progn
  3663. (if arg (org-table-edit-field t)
  3664. (org-table-justify-field-maybe)
  3665. (call-interactively 'org-table-next-field)))))
  3666. ((eq arg t) ;; Global cycling
  3667. (cond
  3668. ((and (eq last-command this-command)
  3669. (eq org-cycle-global-status 'overview))
  3670. ;; We just created the overview - now do table of contents
  3671. ;; This can be slow in very large buffers, so indicate action
  3672. (message "CONTENTS...")
  3673. (org-content)
  3674. (message "CONTENTS...done")
  3675. (setq org-cycle-global-status 'contents)
  3676. (run-hook-with-args 'org-cycle-hook 'contents))
  3677. ((and (eq last-command this-command)
  3678. (eq org-cycle-global-status 'contents))
  3679. ;; We just showed the table of contents - now show everything
  3680. (show-all)
  3681. (message "SHOW ALL")
  3682. (setq org-cycle-global-status 'all)
  3683. (run-hook-with-args 'org-cycle-hook 'all))
  3684. (t
  3685. ;; Default action: go to overview
  3686. (org-overview)
  3687. (message "OVERVIEW")
  3688. (setq org-cycle-global-status 'overview)
  3689. (run-hook-with-args 'org-cycle-hook 'overview))))
  3690. ((and org-drawers org-drawer-regexp
  3691. (save-excursion
  3692. (beginning-of-line 1)
  3693. (looking-at org-drawer-regexp)))
  3694. ;; Toggle block visibility
  3695. (org-flag-drawer
  3696. (not (get-char-property (match-end 0) 'invisible))))
  3697. ((integerp arg)
  3698. ;; Show-subtree, ARG levels up from here.
  3699. (save-excursion
  3700. (org-back-to-heading)
  3701. (outline-up-heading (if (< arg 0) (- arg)
  3702. (- (funcall outline-level) arg)))
  3703. (org-show-subtree)))
  3704. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3705. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3706. ;; At a heading: rotate between three different views
  3707. (org-back-to-heading)
  3708. (let ((goal-column 0) eoh eol eos)
  3709. ;; First, some boundaries
  3710. (save-excursion
  3711. (org-back-to-heading)
  3712. (save-excursion
  3713. (beginning-of-line 2)
  3714. (while (and (not (eobp)) ;; this is like `next-line'
  3715. (get-char-property (1- (point)) 'invisible))
  3716. (beginning-of-line 2)) (setq eol (point)))
  3717. (outline-end-of-heading) (setq eoh (point))
  3718. (org-end-of-subtree t)
  3719. (unless (eobp)
  3720. (skip-chars-forward " \t\n")
  3721. (beginning-of-line 1) ; in case this is an item
  3722. )
  3723. (setq eos (1- (point))))
  3724. ;; Find out what to do next and set `this-command'
  3725. (cond
  3726. ((= eos eoh)
  3727. ;; Nothing is hidden behind this heading
  3728. (message "EMPTY ENTRY")
  3729. (setq org-cycle-subtree-status nil)
  3730. (save-excursion
  3731. (goto-char eos)
  3732. (outline-next-heading)
  3733. (if (org-invisible-p) (org-flag-heading nil))))
  3734. ((or (>= eol eos)
  3735. (not (string-match "\\S-" (buffer-substring eol eos))))
  3736. ;; Entire subtree is hidden in one line: open it
  3737. (org-show-entry)
  3738. (show-children)
  3739. (message "CHILDREN")
  3740. (save-excursion
  3741. (goto-char eos)
  3742. (outline-next-heading)
  3743. (if (org-invisible-p) (org-flag-heading nil)))
  3744. (setq org-cycle-subtree-status 'children)
  3745. (run-hook-with-args 'org-cycle-hook 'children))
  3746. ((and (eq last-command this-command)
  3747. (eq org-cycle-subtree-status 'children))
  3748. ;; We just showed the children, now show everything.
  3749. (org-show-subtree)
  3750. (message "SUBTREE")
  3751. (setq org-cycle-subtree-status 'subtree)
  3752. (run-hook-with-args 'org-cycle-hook 'subtree))
  3753. (t
  3754. ;; Default action: hide the subtree.
  3755. (hide-subtree)
  3756. (message "FOLDED")
  3757. (setq org-cycle-subtree-status 'folded)
  3758. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3759. ;; TAB emulation
  3760. (buffer-read-only (org-back-to-heading))
  3761. ((org-try-cdlatex-tab))
  3762. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3763. (or (not (bolp))
  3764. (not (looking-at outline-regexp))))
  3765. (call-interactively (global-key-binding "\t")))
  3766. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3767. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3768. (or (and (eq org-cycle-emulate-tab 'white)
  3769. (= (match-end 0) (point-at-eol)))
  3770. (and (eq org-cycle-emulate-tab 'whitestart)
  3771. (>= (match-end 0) pos))))
  3772. t
  3773. (eq org-cycle-emulate-tab t))
  3774. (call-interactively (global-key-binding "\t")))
  3775. (t (save-excursion
  3776. (org-back-to-heading)
  3777. (org-cycle))))))
  3778. ;;;###autoload
  3779. (defun org-global-cycle (&optional arg)
  3780. "Cycle the global visibility. For details see `org-cycle'."
  3781. (interactive "P")
  3782. (let ((org-cycle-include-plain-lists
  3783. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3784. (if (integerp arg)
  3785. (progn
  3786. (show-all)
  3787. (hide-sublevels arg)
  3788. (setq org-cycle-global-status 'contents))
  3789. (org-cycle '(4)))))
  3790. (defun org-overview ()
  3791. "Switch to overview mode, shoing only top-level headlines.
  3792. Really, this shows all headlines with level equal or greater than the level
  3793. of the first headline in the buffer. This is important, because if the
  3794. first headline is not level one, then (hide-sublevels 1) gives confusing
  3795. results."
  3796. (interactive)
  3797. (let ((level (save-excursion
  3798. (goto-char (point-min))
  3799. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3800. (progn
  3801. (goto-char (match-beginning 0))
  3802. (funcall outline-level))))))
  3803. (and level (hide-sublevels level))))
  3804. (defun org-content (&optional arg)
  3805. "Show all headlines in the buffer, like a table of contents.
  3806. With numerical argument N, show content up to level N."
  3807. (interactive "P")
  3808. (save-excursion
  3809. ;; Visit all headings and show their offspring
  3810. (and (integerp arg) (org-overview))
  3811. (goto-char (point-max))
  3812. (catch 'exit
  3813. (while (and (progn (condition-case nil
  3814. (outline-previous-visible-heading 1)
  3815. (error (goto-char (point-min))))
  3816. t)
  3817. (looking-at outline-regexp))
  3818. (if (integerp arg)
  3819. (show-children (1- arg))
  3820. (show-branches))
  3821. (if (bobp) (throw 'exit nil))))))
  3822. (defun org-optimize-window-after-visibility-change (state)
  3823. "Adjust the window after a change in outline visibility.
  3824. This function is the default value of the hook `org-cycle-hook'."
  3825. (when (get-buffer-window (current-buffer))
  3826. (cond
  3827. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3828. ; ((eq state 'overview) (org-beginning-of-line))
  3829. ((eq state 'content) nil)
  3830. ((eq state 'all) nil)
  3831. ((eq state 'folded) nil)
  3832. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3833. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3834. (defun org-compact-display-after-subtree-move ()
  3835. (let (beg end)
  3836. (save-excursion
  3837. (if (org-up-heading-safe)
  3838. (progn
  3839. (hide-subtree)
  3840. (show-entry)
  3841. (show-children)
  3842. (org-cycle-show-empty-lines 'children)
  3843. (org-cycle-hide-drawers 'children))
  3844. (org-overview)))))
  3845. (defun org-cycle-show-empty-lines (state)
  3846. "Show empty lines above all visible headlines.
  3847. The region to be covered depends on STATE when called through
  3848. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3849. entire buffer covered. Note that an empty line is only shown if there
  3850. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3851. (when (> org-cycle-separator-lines 0)
  3852. (save-excursion
  3853. (let* ((n org-cycle-separator-lines)
  3854. (re (cond
  3855. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3856. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3857. (t (let ((ns (number-to-string (- n 2))))
  3858. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3859. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3860. beg end)
  3861. (cond
  3862. ((memq state '(overview contents t))
  3863. (setq beg (point-min) end (point-max)))
  3864. ((memq state '(children folded))
  3865. (setq beg (point) end (progn (org-end-of-subtree t t)
  3866. (beginning-of-line 2)
  3867. (point)))))
  3868. (when beg
  3869. (goto-char beg)
  3870. (while (re-search-forward re end t)
  3871. (if (not (get-char-property (match-end 1) 'invisible))
  3872. (outline-flag-region
  3873. (match-beginning 1) (match-end 1) nil)))))))
  3874. ;; Never hide empty lines at the end of the file.
  3875. (save-excursion
  3876. (goto-char (point-max))
  3877. (outline-previous-heading)
  3878. (outline-end-of-heading)
  3879. (if (and (looking-at "[ \t\n]+")
  3880. (= (match-end 0) (point-max)))
  3881. (outline-flag-region (point) (match-end 0) nil))))
  3882. (defun org-subtree-end-visible-p ()
  3883. "Is the end of the current subtree visible?"
  3884. (pos-visible-in-window-p
  3885. (save-excursion (org-end-of-subtree t) (point))))
  3886. (defun org-first-headline-recenter (&optional N)
  3887. "Move cursor to the first headline and recenter the headline.
  3888. Optional argument N means, put the headline into the Nth line of the window."
  3889. (goto-char (point-min))
  3890. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3891. (beginning-of-line)
  3892. (recenter (prefix-numeric-value N))))
  3893. ;;; Org-goto
  3894. (defvar org-goto-window-configuration nil)
  3895. (defvar org-goto-marker nil)
  3896. (defvar org-goto-map
  3897. (let ((map (make-sparse-keymap)))
  3898. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3899. (while (setq cmd (pop cmds))
  3900. (substitute-key-definition cmd cmd map global-map)))
  3901. (suppress-keymap map)
  3902. (org-defkey map "\C-m" 'org-goto-ret)
  3903. (org-defkey map [(return)] 'org-goto-ret)
  3904. (org-defkey map [(left)] 'org-goto-left)
  3905. (org-defkey map [(right)] 'org-goto-right)
  3906. (org-defkey map [(control ?g)] 'org-goto-quit)
  3907. (org-defkey map "\C-i" 'org-cycle)
  3908. (org-defkey map [(tab)] 'org-cycle)
  3909. (org-defkey map [(down)] 'outline-next-visible-heading)
  3910. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3911. (if org-goto-auto-isearch
  3912. (if (fboundp 'define-key-after)
  3913. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3914. nil)
  3915. (org-defkey map "q" 'org-goto-quit)
  3916. (org-defkey map "n" 'outline-next-visible-heading)
  3917. (org-defkey map "p" 'outline-previous-visible-heading)
  3918. (org-defkey map "f" 'outline-forward-same-level)
  3919. (org-defkey map "b" 'outline-backward-same-level)
  3920. (org-defkey map "u" 'outline-up-heading))
  3921. (org-defkey map "/" 'org-occur)
  3922. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3923. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3924. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3925. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3926. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3927. map))
  3928. (defconst org-goto-help
  3929. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3930. RET=jump to location [Q]uit and return to previous location
  3931. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3932. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3933. (defun org-goto (&optional alternative-interface)
  3934. "Look up a different location in the current file, keeping current visibility.
  3935. When you want look-up or go to a different location in a document, the
  3936. fastest way is often to fold the entire buffer and then dive into the tree.
  3937. This method has the disadvantage, that the previous location will be folded,
  3938. which may not be what you want.
  3939. This command works around this by showing a copy of the current buffer
  3940. in an indirect buffer, in overview mode. You can dive into the tree in
  3941. that copy, use org-occur and incremental search to find a location.
  3942. When pressing RET or `Q', the command returns to the original buffer in
  3943. which the visibility is still unchanged. After RET is will also jump to
  3944. the location selected in the indirect buffer and expose the
  3945. the headline hierarchy above."
  3946. (interactive "P")
  3947. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3948. (org-refile-use-outline-path t)
  3949. (interface
  3950. (if (not alternative-interface)
  3951. org-goto-interface
  3952. (if (eq org-goto-interface 'outline)
  3953. 'outline-path-completion
  3954. 'outline)))
  3955. (org-goto-start-pos (point))
  3956. (selected-point
  3957. (if (eq interface 'outline)
  3958. (car (org-get-location (current-buffer) org-goto-help))
  3959. (nth 3 (org-refile-get-location "Goto: ")))))
  3960. (if selected-point
  3961. (progn
  3962. (org-mark-ring-push org-goto-start-pos)
  3963. (goto-char selected-point)
  3964. (if (or (org-invisible-p) (org-invisible-p2))
  3965. (org-show-context 'org-goto)))
  3966. (message "Quit"))))
  3967. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3968. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3969. (defvar org-goto-local-auto-isearch-map) ; defined below
  3970. (defun org-get-location (buf help)
  3971. "Let the user select a location in the Org-mode buffer BUF.
  3972. This function uses a recursive edit. It returns the selected position
  3973. or nil."
  3974. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3975. (isearch-hide-immediately nil)
  3976. (isearch-search-fun-function
  3977. (lambda () 'org-goto-local-search-forward-headings))
  3978. (org-goto-selected-point org-goto-exit-command))
  3979. (save-excursion
  3980. (save-window-excursion
  3981. (delete-other-windows)
  3982. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3983. (switch-to-buffer
  3984. (condition-case nil
  3985. (make-indirect-buffer (current-buffer) "*org-goto*")
  3986. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3987. (with-output-to-temp-buffer "*Help*"
  3988. (princ help))
  3989. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3990. (setq buffer-read-only nil)
  3991. (let ((org-startup-truncated t)
  3992. (org-startup-folded nil)
  3993. (org-startup-align-all-tables nil))
  3994. (org-mode)
  3995. (org-overview))
  3996. (setq buffer-read-only t)
  3997. (if (and (boundp 'org-goto-start-pos)
  3998. (integer-or-marker-p org-goto-start-pos))
  3999. (let ((org-show-hierarchy-above t)
  4000. (org-show-siblings t)
  4001. (org-show-following-heading t))
  4002. (goto-char org-goto-start-pos)
  4003. (and (org-invisible-p) (org-show-context)))
  4004. (goto-char (point-min)))
  4005. (org-beginning-of-line)
  4006. (message "Select location and press RET")
  4007. (use-local-map org-goto-map)
  4008. (recursive-edit)
  4009. ))
  4010. (kill-buffer "*org-goto*")
  4011. (cons org-goto-selected-point org-goto-exit-command)))
  4012. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4013. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4014. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4015. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4016. (defun org-goto-local-search-forward-headings (string bound noerror)
  4017. "Search and make sure that anu matches are in headlines."
  4018. (catch 'return
  4019. (while (search-forward string bound noerror)
  4020. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4021. (and (member :headline context)
  4022. (not (member :tags context))))
  4023. (throw 'return (point))))))
  4024. (defun org-goto-local-auto-isearch ()
  4025. "Start isearch."
  4026. (interactive)
  4027. (goto-char (point-min))
  4028. (let ((keys (this-command-keys)))
  4029. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4030. (isearch-mode t)
  4031. (isearch-process-search-char (string-to-char keys)))))
  4032. (defun org-goto-ret (&optional arg)
  4033. "Finish `org-goto' by going to the new location."
  4034. (interactive "P")
  4035. (setq org-goto-selected-point (point)
  4036. org-goto-exit-command 'return)
  4037. (throw 'exit nil))
  4038. (defun org-goto-left ()
  4039. "Finish `org-goto' by going to the new location."
  4040. (interactive)
  4041. (if (org-on-heading-p)
  4042. (progn
  4043. (beginning-of-line 1)
  4044. (setq org-goto-selected-point (point)
  4045. org-goto-exit-command 'left)
  4046. (throw 'exit nil))
  4047. (error "Not on a heading")))
  4048. (defun org-goto-right ()
  4049. "Finish `org-goto' by going to the new location."
  4050. (interactive)
  4051. (if (org-on-heading-p)
  4052. (progn
  4053. (setq org-goto-selected-point (point)
  4054. org-goto-exit-command 'right)
  4055. (throw 'exit nil))
  4056. (error "Not on a heading")))
  4057. (defun org-goto-quit ()
  4058. "Finish `org-goto' without cursor motion."
  4059. (interactive)
  4060. (setq org-goto-selected-point nil)
  4061. (setq org-goto-exit-command 'quit)
  4062. (throw 'exit nil))
  4063. ;;; Indirect buffer display of subtrees
  4064. (defvar org-indirect-dedicated-frame nil
  4065. "This is the frame being used for indirect tree display.")
  4066. (defvar org-last-indirect-buffer nil)
  4067. (defun org-tree-to-indirect-buffer (&optional arg)
  4068. "Create indirect buffer and narrow it to current subtree.
  4069. With numerical prefix ARG, go up to this level and then take that tree.
  4070. If ARG is negative, go up that many levels.
  4071. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4072. indirect buffer previously made with this command, to avoid proliferation of
  4073. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4074. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4075. is kept so that you can work with several indirect buffers at the same time.
  4076. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4077. requests that a new frame be made for the new buffer, so that the dedicated
  4078. frame is not changed."
  4079. (interactive "P")
  4080. (let ((cbuf (current-buffer))
  4081. (cwin (selected-window))
  4082. (pos (point))
  4083. beg end level heading ibuf)
  4084. (save-excursion
  4085. (org-back-to-heading t)
  4086. (when (numberp arg)
  4087. (setq level (org-outline-level))
  4088. (if (< arg 0) (setq arg (+ level arg)))
  4089. (while (> (setq level (org-outline-level)) arg)
  4090. (outline-up-heading 1 t)))
  4091. (setq beg (point)
  4092. heading (org-get-heading))
  4093. (org-end-of-subtree t) (setq end (point)))
  4094. (if (and (buffer-live-p org-last-indirect-buffer)
  4095. (not (eq org-indirect-buffer-display 'new-frame))
  4096. (not arg))
  4097. (kill-buffer org-last-indirect-buffer))
  4098. (setq ibuf (org-get-indirect-buffer cbuf)
  4099. org-last-indirect-buffer ibuf)
  4100. (cond
  4101. ((or (eq org-indirect-buffer-display 'new-frame)
  4102. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4103. (select-frame (make-frame))
  4104. (delete-other-windows)
  4105. (switch-to-buffer ibuf)
  4106. (org-set-frame-title heading))
  4107. ((eq org-indirect-buffer-display 'dedicated-frame)
  4108. (raise-frame
  4109. (select-frame (or (and org-indirect-dedicated-frame
  4110. (frame-live-p org-indirect-dedicated-frame)
  4111. org-indirect-dedicated-frame)
  4112. (setq org-indirect-dedicated-frame (make-frame)))))
  4113. (delete-other-windows)
  4114. (switch-to-buffer ibuf)
  4115. (org-set-frame-title (concat "Indirect: " heading)))
  4116. ((eq org-indirect-buffer-display 'current-window)
  4117. (switch-to-buffer ibuf))
  4118. ((eq org-indirect-buffer-display 'other-window)
  4119. (pop-to-buffer ibuf))
  4120. (t (error "Invalid value.")))
  4121. (if (featurep 'xemacs)
  4122. (save-excursion (org-mode) (turn-on-font-lock)))
  4123. (narrow-to-region beg end)
  4124. (show-all)
  4125. (goto-char pos)
  4126. (and (window-live-p cwin) (select-window cwin))))
  4127. (defun org-get-indirect-buffer (&optional buffer)
  4128. (setq buffer (or buffer (current-buffer)))
  4129. (let ((n 1) (base (buffer-name buffer)) bname)
  4130. (while (buffer-live-p
  4131. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4132. (setq n (1+ n)))
  4133. (condition-case nil
  4134. (make-indirect-buffer buffer bname 'clone)
  4135. (error (make-indirect-buffer buffer bname)))))
  4136. (defun org-set-frame-title (title)
  4137. "Set the title of the current frame to the string TITLE."
  4138. ;; FIXME: how to name a single frame in XEmacs???
  4139. (unless (featurep 'xemacs)
  4140. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4141. ;;;; Structure editing
  4142. ;;; Inserting headlines
  4143. (defun org-insert-heading (&optional force-heading)
  4144. "Insert a new heading or item with same depth at point.
  4145. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4146. If point is at the beginning of a headline, insert a sibling before the
  4147. current headline. If point is not at the beginning, do not split the line,
  4148. but create the new hedline after the current line."
  4149. (interactive "P")
  4150. (if (= (buffer-size) 0)
  4151. (insert "\n* ")
  4152. (when (or force-heading (not (org-insert-item)))
  4153. (let* ((head (save-excursion
  4154. (condition-case nil
  4155. (progn
  4156. (org-back-to-heading)
  4157. (match-string 0))
  4158. (error "*"))))
  4159. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4160. pos)
  4161. (cond
  4162. ((and (org-on-heading-p) (bolp)
  4163. (or (bobp)
  4164. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4165. ;; insert before the current line
  4166. (open-line (if blank 2 1)))
  4167. ((and (bolp)
  4168. (or (bobp)
  4169. (save-excursion
  4170. (backward-char 1) (not (org-invisible-p)))))
  4171. ;; insert right here
  4172. nil)
  4173. (t
  4174. ;; in the middle of the line
  4175. (org-show-entry)
  4176. (let ((split
  4177. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4178. tags pos)
  4179. (if (org-on-heading-p)
  4180. (progn
  4181. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4182. (setq tags (and (match-end 2) (match-string 2)))
  4183. (and (match-end 1)
  4184. (delete-region (match-beginning 1) (match-end 1)))
  4185. (setq pos (point-at-bol))
  4186. (or split (end-of-line 1))
  4187. (delete-horizontal-space)
  4188. (newline (if blank 2 1))
  4189. (when tags
  4190. (save-excursion
  4191. (goto-char pos)
  4192. (end-of-line 1)
  4193. (insert " " tags)
  4194. (org-set-tags nil 'align))))
  4195. (or split (end-of-line 1))
  4196. (newline (if blank 2 1))))))
  4197. (insert head) (just-one-space)
  4198. (setq pos (point))
  4199. (end-of-line 1)
  4200. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4201. (run-hooks 'org-insert-heading-hook)))))
  4202. (defun org-get-heading (&optional no-tags)
  4203. "Return the heading of the current entry, without the stars."
  4204. (save-excursion
  4205. (org-back-to-heading t)
  4206. (if (looking-at
  4207. (if no-tags
  4208. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4209. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4210. (match-string 1) "")))
  4211. (defun org-insert-heading-after-current ()
  4212. "Insert a new heading with same level as current, after current subtree."
  4213. (interactive)
  4214. (org-back-to-heading)
  4215. (org-insert-heading)
  4216. (org-move-subtree-down)
  4217. (end-of-line 1))
  4218. (defun org-insert-todo-heading (arg)
  4219. "Insert a new heading with the same level and TODO state as current heading.
  4220. If the heading has no TODO state, or if the state is DONE, use the first
  4221. state (TODO by default). Also with prefix arg, force first state."
  4222. (interactive "P")
  4223. (when (not (org-insert-item 'checkbox))
  4224. (org-insert-heading)
  4225. (save-excursion
  4226. (org-back-to-heading)
  4227. (outline-previous-heading)
  4228. (looking-at org-todo-line-regexp))
  4229. (if (or arg
  4230. (not (match-beginning 2))
  4231. (member (match-string 2) org-done-keywords))
  4232. (insert (car org-todo-keywords-1) " ")
  4233. (insert (match-string 2) " "))))
  4234. (defun org-insert-subheading (arg)
  4235. "Insert a new subheading and demote it.
  4236. Works for outline headings and for plain lists alike."
  4237. (interactive "P")
  4238. (org-insert-heading arg)
  4239. (cond
  4240. ((org-on-heading-p) (org-do-demote))
  4241. ((org-at-item-p) (org-indent-item 1))))
  4242. (defun org-insert-todo-subheading (arg)
  4243. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4244. Works for outline headings and for plain lists alike."
  4245. (interactive "P")
  4246. (org-insert-todo-heading arg)
  4247. (cond
  4248. ((org-on-heading-p) (org-do-demote))
  4249. ((org-at-item-p) (org-indent-item 1))))
  4250. ;;; Promotion and Demotion
  4251. (defun org-promote-subtree ()
  4252. "Promote the entire subtree.
  4253. See also `org-promote'."
  4254. (interactive)
  4255. (save-excursion
  4256. (org-map-tree 'org-promote))
  4257. (org-fix-position-after-promote))
  4258. (defun org-demote-subtree ()
  4259. "Demote the entire subtree. See `org-demote'.
  4260. See also `org-promote'."
  4261. (interactive)
  4262. (save-excursion
  4263. (org-map-tree 'org-demote))
  4264. (org-fix-position-after-promote))
  4265. (defun org-do-promote ()
  4266. "Promote the current heading higher up the tree.
  4267. If the region is active in `transient-mark-mode', promote all headings
  4268. in the region."
  4269. (interactive)
  4270. (save-excursion
  4271. (if (org-region-active-p)
  4272. (org-map-region 'org-promote (region-beginning) (region-end))
  4273. (org-promote)))
  4274. (org-fix-position-after-promote))
  4275. (defun org-do-demote ()
  4276. "Demote the current heading lower down the tree.
  4277. If the region is active in `transient-mark-mode', demote all headings
  4278. in the region."
  4279. (interactive)
  4280. (save-excursion
  4281. (if (org-region-active-p)
  4282. (org-map-region 'org-demote (region-beginning) (region-end))
  4283. (org-demote)))
  4284. (org-fix-position-after-promote))
  4285. (defun org-fix-position-after-promote ()
  4286. "Make sure that after pro/demotion cursor position is right."
  4287. (let ((pos (point)))
  4288. (when (save-excursion
  4289. (beginning-of-line 1)
  4290. (looking-at org-todo-line-regexp)
  4291. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4292. (cond ((eobp) (insert " "))
  4293. ((eolp) (insert " "))
  4294. ((equal (char-after) ?\ ) (forward-char 1))))))
  4295. (defun org-reduced-level (l)
  4296. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4297. (defun org-get-valid-level (level &optional change)
  4298. "Rectify a level change under the influence of `org-odd-levels-only'
  4299. LEVEL is a current level, CHANGE is by how much the level should be
  4300. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4301. even level numbers will become the next higher odd number."
  4302. (if org-odd-levels-only
  4303. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4304. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4305. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4306. (max 1 (+ level change))))
  4307. (if (boundp 'define-obsolete-function-alias)
  4308. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4309. (define-obsolete-function-alias 'org-get-legal-level
  4310. 'org-get-valid-level)
  4311. (define-obsolete-function-alias 'org-get-legal-level
  4312. 'org-get-valid-level "23.1")))
  4313. (defun org-promote ()
  4314. "Promote the current heading higher up the tree.
  4315. If the region is active in `transient-mark-mode', promote all headings
  4316. in the region."
  4317. (org-back-to-heading t)
  4318. (let* ((level (save-match-data (funcall outline-level)))
  4319. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4320. (diff (abs (- level (length up-head) -1))))
  4321. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4322. (replace-match up-head nil t)
  4323. ;; Fixup tag positioning
  4324. (and org-auto-align-tags (org-set-tags nil t))
  4325. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4326. (defun org-demote ()
  4327. "Demote the current heading lower down the tree.
  4328. If the region is active in `transient-mark-mode', demote all headings
  4329. in the region."
  4330. (org-back-to-heading t)
  4331. (let* ((level (save-match-data (funcall outline-level)))
  4332. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4333. (diff (abs (- level (length down-head) -1))))
  4334. (replace-match down-head nil t)
  4335. ;; Fixup tag positioning
  4336. (and org-auto-align-tags (org-set-tags nil t))
  4337. (if org-adapt-indentation (org-fixup-indentation diff))))
  4338. (defun org-map-tree (fun)
  4339. "Call FUN for every heading underneath the current one."
  4340. (org-back-to-heading)
  4341. (let ((level (funcall outline-level)))
  4342. (save-excursion
  4343. (funcall fun)
  4344. (while (and (progn
  4345. (outline-next-heading)
  4346. (> (funcall outline-level) level))
  4347. (not (eobp)))
  4348. (funcall fun)))))
  4349. (defun org-map-region (fun beg end)
  4350. "Call FUN for every heading between BEG and END."
  4351. (let ((org-ignore-region t))
  4352. (save-excursion
  4353. (setq end (copy-marker end))
  4354. (goto-char beg)
  4355. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4356. (< (point) end))
  4357. (funcall fun))
  4358. (while (and (progn
  4359. (outline-next-heading)
  4360. (< (point) end))
  4361. (not (eobp)))
  4362. (funcall fun)))))
  4363. (defun org-fixup-indentation (diff)
  4364. "Change the indentation in the current entry by DIFF
  4365. However, if any line in the current entry has no indentation, or if it
  4366. would end up with no indentation after the change, nothing at all is done."
  4367. (save-excursion
  4368. (let ((end (save-excursion (outline-next-heading)
  4369. (point-marker)))
  4370. (prohibit (if (> diff 0)
  4371. "^\\S-"
  4372. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4373. col)
  4374. (unless (save-excursion (end-of-line 1)
  4375. (re-search-forward prohibit end t))
  4376. (while (and (< (point) end)
  4377. (re-search-forward "^[ \t]+" end t))
  4378. (goto-char (match-end 0))
  4379. (setq col (current-column))
  4380. (if (< diff 0) (replace-match ""))
  4381. (indent-to (+ diff col))))
  4382. (move-marker end nil))))
  4383. (defun org-convert-to-odd-levels ()
  4384. "Convert an org-mode file with all levels allowed to one with odd levels.
  4385. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4386. level 5 etc."
  4387. (interactive)
  4388. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4389. (let ((org-odd-levels-only nil) n)
  4390. (save-excursion
  4391. (goto-char (point-min))
  4392. (while (re-search-forward "^\\*\\*+ " nil t)
  4393. (setq n (- (length (match-string 0)) 2))
  4394. (while (>= (setq n (1- n)) 0)
  4395. (org-demote))
  4396. (end-of-line 1))))))
  4397. (defun org-convert-to-oddeven-levels ()
  4398. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4399. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4400. section with an even level, conversion would destroy the structure of the file. An error
  4401. is signaled in this case."
  4402. (interactive)
  4403. (goto-char (point-min))
  4404. ;; First check if there are no even levels
  4405. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4406. (org-show-context t)
  4407. (error "Not all levels are odd in this file. Conversion not possible."))
  4408. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4409. (let ((org-odd-levels-only nil) n)
  4410. (save-excursion
  4411. (goto-char (point-min))
  4412. (while (re-search-forward "^\\*\\*+ " nil t)
  4413. (setq n (/ (1- (length (match-string 0))) 2))
  4414. (while (>= (setq n (1- n)) 0)
  4415. (org-promote))
  4416. (end-of-line 1))))))
  4417. (defun org-tr-level (n)
  4418. "Make N odd if required."
  4419. (if org-odd-levels-only (1+ (/ n 2)) n))
  4420. ;;; Vertical tree motion, cutting and pasting of subtrees
  4421. (defun org-move-subtree-up (&optional arg)
  4422. "Move the current subtree up past ARG headlines of the same level."
  4423. (interactive "p")
  4424. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4425. (defun org-move-subtree-down (&optional arg)
  4426. "Move the current subtree down past ARG headlines of the same level."
  4427. (interactive "p")
  4428. (setq arg (prefix-numeric-value arg))
  4429. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4430. 'outline-get-last-sibling))
  4431. (ins-point (make-marker))
  4432. (cnt (abs arg))
  4433. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4434. ;; Select the tree
  4435. (org-back-to-heading)
  4436. (setq beg0 (point))
  4437. (save-excursion
  4438. (setq ne-beg (org-back-over-empty-lines))
  4439. (setq beg (point)))
  4440. (save-match-data
  4441. (save-excursion (outline-end-of-heading)
  4442. (setq folded (org-invisible-p)))
  4443. (outline-end-of-subtree))
  4444. (outline-next-heading)
  4445. (setq ne-end (org-back-over-empty-lines))
  4446. (setq end (point))
  4447. (goto-char beg0)
  4448. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4449. ;; include less whitespace
  4450. (save-excursion
  4451. (goto-char beg)
  4452. (forward-line (- ne-beg ne-end))
  4453. (setq beg (point))))
  4454. ;; Find insertion point, with error handling
  4455. (while (> cnt 0)
  4456. (or (and (funcall movfunc) (looking-at outline-regexp))
  4457. (progn (goto-char beg0)
  4458. (error "Cannot move past superior level or buffer limit")))
  4459. (setq cnt (1- cnt)))
  4460. (if (> arg 0)
  4461. ;; Moving forward - still need to move over subtree
  4462. (progn (org-end-of-subtree t t)
  4463. (save-excursion
  4464. (org-back-over-empty-lines)
  4465. (or (bolp) (newline)))))
  4466. (setq ne-ins (org-back-over-empty-lines))
  4467. (move-marker ins-point (point))
  4468. (setq txt (buffer-substring beg end))
  4469. (delete-region beg end)
  4470. (outline-flag-region (1- beg) beg nil)
  4471. (outline-flag-region (1- (point)) (point) nil)
  4472. (insert txt)
  4473. (or (bolp) (insert "\n"))
  4474. (setq ins-end (point))
  4475. (goto-char ins-point)
  4476. (org-skip-whitespace)
  4477. (when (and (< arg 0)
  4478. (org-first-sibling-p)
  4479. (> ne-ins ne-beg))
  4480. ;; Move whitespace back to beginning
  4481. (save-excursion
  4482. (goto-char ins-end)
  4483. (let ((kill-whole-line t))
  4484. (kill-line (- ne-ins ne-beg)) (point)))
  4485. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4486. (move-marker ins-point nil)
  4487. (org-compact-display-after-subtree-move)
  4488. (unless folded
  4489. (org-show-entry)
  4490. (show-children)
  4491. (org-cycle-hide-drawers 'children))))
  4492. (defvar org-subtree-clip ""
  4493. "Clipboard for cut and paste of subtrees.
  4494. This is actually only a copy of the kill, because we use the normal kill
  4495. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4496. (defvar org-subtree-clip-folded nil
  4497. "Was the last copied subtree folded?
  4498. This is used to fold the tree back after pasting.")
  4499. (defun org-cut-subtree (&optional n)
  4500. "Cut the current subtree into the clipboard.
  4501. With prefix arg N, cut this many sequential subtrees.
  4502. This is a short-hand for marking the subtree and then cutting it."
  4503. (interactive "p")
  4504. (org-copy-subtree n 'cut))
  4505. (defun org-copy-subtree (&optional n cut)
  4506. "Cut the current subtree into the clipboard.
  4507. With prefix arg N, cut this many sequential subtrees.
  4508. This is a short-hand for marking the subtree and then copying it.
  4509. If CUT is non-nil, actually cut the subtree."
  4510. (interactive "p")
  4511. (let (beg end folded (beg0 (point)))
  4512. (if (interactive-p)
  4513. (org-back-to-heading nil) ; take what looks like a subtree
  4514. (org-back-to-heading t)) ; take what is really there
  4515. (org-back-over-empty-lines)
  4516. (setq beg (point))
  4517. (skip-chars-forward " \t\r\n")
  4518. (save-match-data
  4519. (save-excursion (outline-end-of-heading)
  4520. (setq folded (org-invisible-p)))
  4521. (condition-case nil
  4522. (outline-forward-same-level (1- n))
  4523. (error nil))
  4524. (org-end-of-subtree t t))
  4525. (org-back-over-empty-lines)
  4526. (setq end (point))
  4527. (goto-char beg0)
  4528. (when (> end beg)
  4529. (setq org-subtree-clip-folded folded)
  4530. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4531. (setq org-subtree-clip (current-kill 0))
  4532. (message "%s: Subtree(s) with %d characters"
  4533. (if cut "Cut" "Copied")
  4534. (length org-subtree-clip)))))
  4535. (defun org-paste-subtree (&optional level tree)
  4536. "Paste the clipboard as a subtree, with modification of headline level.
  4537. The entire subtree is promoted or demoted in order to match a new headline
  4538. level. By default, the new level is derived from the visible headings
  4539. before and after the insertion point, and taken to be the inferior headline
  4540. level of the two. So if the previous visible heading is level 3 and the
  4541. next is level 4 (or vice versa), level 4 will be used for insertion.
  4542. This makes sure that the subtree remains an independent subtree and does
  4543. not swallow low level entries.
  4544. You can also force a different level, either by using a numeric prefix
  4545. argument, or by inserting the heading marker by hand. For example, if the
  4546. cursor is after \"*****\", then the tree will be shifted to level 5.
  4547. If you want to insert the tree as is, just use \\[yank].
  4548. If optional TREE is given, use this text instead of the kill ring."
  4549. (interactive "P")
  4550. (unless (org-kill-is-subtree-p tree)
  4551. (error "%s"
  4552. (substitute-command-keys
  4553. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4554. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4555. (^re (concat "^\\(" outline-regexp "\\)"))
  4556. (re (concat "\\(" outline-regexp "\\)"))
  4557. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4558. (old-level (if (string-match ^re txt)
  4559. (- (match-end 0) (match-beginning 0) 1)
  4560. -1))
  4561. (force-level (cond (level (prefix-numeric-value level))
  4562. ((string-match
  4563. ^re_ (buffer-substring (point-at-bol) (point)))
  4564. (- (match-end 1) (match-beginning 1)))
  4565. (t nil)))
  4566. (previous-level (save-excursion
  4567. (condition-case nil
  4568. (progn
  4569. (outline-previous-visible-heading 1)
  4570. (if (looking-at re)
  4571. (- (match-end 0) (match-beginning 0) 1)
  4572. 1))
  4573. (error 1))))
  4574. (next-level (save-excursion
  4575. (condition-case nil
  4576. (progn
  4577. (or (looking-at outline-regexp)
  4578. (outline-next-visible-heading 1))
  4579. (if (looking-at re)
  4580. (- (match-end 0) (match-beginning 0) 1)
  4581. 1))
  4582. (error 1))))
  4583. (new-level (or force-level (max previous-level next-level)))
  4584. (shift (if (or (= old-level -1)
  4585. (= new-level -1)
  4586. (= old-level new-level))
  4587. 0
  4588. (- new-level old-level)))
  4589. (delta (if (> shift 0) -1 1))
  4590. (func (if (> shift 0) 'org-demote 'org-promote))
  4591. (org-odd-levels-only nil)
  4592. beg end)
  4593. ;; Remove the forced level indicator
  4594. (if force-level
  4595. (delete-region (point-at-bol) (point)))
  4596. ;; Paste
  4597. (beginning-of-line 1)
  4598. (org-back-over-empty-lines)
  4599. (setq beg (point))
  4600. (insert-before-markers txt)
  4601. (unless (string-match "\n\\'" txt) (insert "\n"))
  4602. (setq end (point))
  4603. (goto-char beg)
  4604. (skip-chars-forward " \t\n\r")
  4605. (setq beg (point))
  4606. ;; Shift if necessary
  4607. (unless (= shift 0)
  4608. (save-restriction
  4609. (narrow-to-region beg end)
  4610. (while (not (= shift 0))
  4611. (org-map-region func (point-min) (point-max))
  4612. (setq shift (+ delta shift)))
  4613. (goto-char (point-min))))
  4614. (when (interactive-p)
  4615. (message "Clipboard pasted as level %d subtree" new-level))
  4616. (if (and kill-ring
  4617. (eq org-subtree-clip (current-kill 0))
  4618. org-subtree-clip-folded)
  4619. ;; The tree was folded before it was killed/copied
  4620. (hide-subtree))))
  4621. (defun org-kill-is-subtree-p (&optional txt)
  4622. "Check if the current kill is an outline subtree, or a set of trees.
  4623. Returns nil if kill does not start with a headline, or if the first
  4624. headline level is not the largest headline level in the tree.
  4625. So this will actually accept several entries of equal levels as well,
  4626. which is OK for `org-paste-subtree'.
  4627. If optional TXT is given, check this string instead of the current kill."
  4628. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4629. (start-level (and kill
  4630. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4631. org-outline-regexp "\\)")
  4632. kill)
  4633. (- (match-end 2) (match-beginning 2) 1)))
  4634. (re (concat "^" org-outline-regexp))
  4635. (start (1+ (match-beginning 2))))
  4636. (if (not start-level)
  4637. (progn
  4638. nil) ;; does not even start with a heading
  4639. (catch 'exit
  4640. (while (setq start (string-match re kill (1+ start)))
  4641. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4642. (throw 'exit nil)))
  4643. t))))
  4644. (defun org-narrow-to-subtree ()
  4645. "Narrow buffer to the current subtree."
  4646. (interactive)
  4647. (save-excursion
  4648. (save-match-data
  4649. (narrow-to-region
  4650. (progn (org-back-to-heading) (point))
  4651. (progn (org-end-of-subtree t t) (point))))))
  4652. ;;; Outline Sorting
  4653. (defun org-sort (with-case)
  4654. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4655. Optional argument WITH-CASE means sort case-sensitively."
  4656. (interactive "P")
  4657. (if (org-at-table-p)
  4658. (org-call-with-arg 'org-table-sort-lines with-case)
  4659. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4660. (defun org-sort-remove-invisible (s)
  4661. (remove-text-properties 0 (length s) org-rm-props s)
  4662. (while (string-match org-bracket-link-regexp s)
  4663. (setq s (replace-match (if (match-end 2)
  4664. (match-string 3 s)
  4665. (match-string 1 s)) t t s)))
  4666. s)
  4667. (defvar org-priority-regexp) ; defined later in the file
  4668. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4669. "Sort entries on a certain level of an outline tree.
  4670. If there is an active region, the entries in the region are sorted.
  4671. Else, if the cursor is before the first entry, sort the top-level items.
  4672. Else, the children of the entry at point are sorted.
  4673. Sorting can be alphabetically, numerically, and by date/time as given by
  4674. the first time stamp in the entry. The command prompts for the sorting
  4675. type unless it has been given to the function through the SORTING-TYPE
  4676. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4677. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4678. called with point at the beginning of the record. It must return either
  4679. a string or a number that should serve as the sorting key for that record.
  4680. Comparing entries ignores case by default. However, with an optional argument
  4681. WITH-CASE, the sorting considers case as well."
  4682. (interactive "P")
  4683. (let ((case-func (if with-case 'identity 'downcase))
  4684. start beg end stars re re2
  4685. txt what tmp plain-list-p)
  4686. ;; Find beginning and end of region to sort
  4687. (cond
  4688. ((org-region-active-p)
  4689. ;; we will sort the region
  4690. (setq end (region-end)
  4691. what "region")
  4692. (goto-char (region-beginning))
  4693. (if (not (org-on-heading-p)) (outline-next-heading))
  4694. (setq start (point)))
  4695. ((org-at-item-p)
  4696. ;; we will sort this plain list
  4697. (org-beginning-of-item-list) (setq start (point))
  4698. (org-end-of-item-list) (setq end (point))
  4699. (goto-char start)
  4700. (setq plain-list-p t
  4701. what "plain list"))
  4702. ((or (org-on-heading-p)
  4703. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4704. ;; we will sort the children of the current headline
  4705. (org-back-to-heading)
  4706. (setq start (point)
  4707. end (progn (org-end-of-subtree t t)
  4708. (org-back-over-empty-lines)
  4709. (point))
  4710. what "children")
  4711. (goto-char start)
  4712. (show-subtree)
  4713. (outline-next-heading))
  4714. (t
  4715. ;; we will sort the top-level entries in this file
  4716. (goto-char (point-min))
  4717. (or (org-on-heading-p) (outline-next-heading))
  4718. (setq start (point) end (point-max) what "top-level")
  4719. (goto-char start)
  4720. (show-all)))
  4721. (setq beg (point))
  4722. (if (>= beg end) (error "Nothing to sort"))
  4723. (unless plain-list-p
  4724. (looking-at "\\(\\*+\\)")
  4725. (setq stars (match-string 1)
  4726. re (concat "^" (regexp-quote stars) " +")
  4727. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4728. txt (buffer-substring beg end))
  4729. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4730. (if (and (not (equal stars "*")) (string-match re2 txt))
  4731. (error "Region to sort contains a level above the first entry")))
  4732. (unless sorting-type
  4733. (message
  4734. (if plain-list-p
  4735. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4736. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  4737. what)
  4738. (setq sorting-type (read-char-exclusive))
  4739. (and (= (downcase sorting-type) ?f)
  4740. (setq getkey-func
  4741. (completing-read "Sort using function: "
  4742. obarray 'fboundp t nil nil))
  4743. (setq getkey-func (intern getkey-func)))
  4744. (and (= (downcase sorting-type) ?r)
  4745. (setq property
  4746. (completing-read "Property: "
  4747. (mapcar 'list (org-buffer-property-keys t))
  4748. nil t))))
  4749. (message "Sorting entries...")
  4750. (save-restriction
  4751. (narrow-to-region start end)
  4752. (let ((dcst (downcase sorting-type))
  4753. (now (current-time)))
  4754. (sort-subr
  4755. (/= dcst sorting-type)
  4756. ;; This function moves to the beginning character of the "record" to
  4757. ;; be sorted.
  4758. (if plain-list-p
  4759. (lambda nil
  4760. (if (org-at-item-p) t (goto-char (point-max))))
  4761. (lambda nil
  4762. (if (re-search-forward re nil t)
  4763. (goto-char (match-beginning 0))
  4764. (goto-char (point-max)))))
  4765. ;; This function moves to the last character of the "record" being
  4766. ;; sorted.
  4767. (if plain-list-p
  4768. 'org-end-of-item
  4769. (lambda nil
  4770. (save-match-data
  4771. (condition-case nil
  4772. (outline-forward-same-level 1)
  4773. (error
  4774. (goto-char (point-max)))))))
  4775. ;; This function returns the value that gets sorted against.
  4776. (if plain-list-p
  4777. (lambda nil
  4778. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4779. (cond
  4780. ((= dcst ?n)
  4781. (string-to-number (buffer-substring (match-end 0)
  4782. (point-at-eol))))
  4783. ((= dcst ?a)
  4784. (buffer-substring (match-end 0) (point-at-eol)))
  4785. ((= dcst ?t)
  4786. (if (re-search-forward org-ts-regexp
  4787. (point-at-eol) t)
  4788. (org-time-string-to-time (match-string 0))
  4789. now))
  4790. ((= dcst ?f)
  4791. (if getkey-func
  4792. (progn
  4793. (setq tmp (funcall getkey-func))
  4794. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4795. tmp)
  4796. (error "Invalid key function `%s'" getkey-func)))
  4797. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4798. (lambda nil
  4799. (cond
  4800. ((= dcst ?n)
  4801. (if (looking-at outline-regexp)
  4802. (string-to-number (buffer-substring (match-end 0)
  4803. (point-at-eol)))
  4804. nil))
  4805. ((= dcst ?a)
  4806. (funcall case-func (buffer-substring (point-at-bol)
  4807. (point-at-eol))))
  4808. ((= dcst ?t)
  4809. (if (re-search-forward org-ts-regexp
  4810. (save-excursion
  4811. (forward-line 2)
  4812. (point)) t)
  4813. (org-time-string-to-time (match-string 0))
  4814. now))
  4815. ((= dcst ?p)
  4816. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4817. (string-to-char (match-string 2))
  4818. org-default-priority))
  4819. ((= dcst ?r)
  4820. (or (org-entry-get nil property) ""))
  4821. ((= dcst ?f)
  4822. (if getkey-func
  4823. (progn
  4824. (setq tmp (funcall getkey-func))
  4825. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4826. tmp)
  4827. (error "Invalid key function `%s'" getkey-func)))
  4828. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4829. nil
  4830. (cond
  4831. ((= dcst ?a) 'string<)
  4832. ((= dcst ?t) 'time-less-p)
  4833. (t nil)))))
  4834. (message "Sorting entries...done")))
  4835. (defun org-do-sort (table what &optional with-case sorting-type)
  4836. "Sort TABLE of WHAT according to SORTING-TYPE.
  4837. The user will be prompted for the SORTING-TYPE if the call to this
  4838. function does not specify it. WHAT is only for the prompt, to indicate
  4839. what is being sorted. The sorting key will be extracted from
  4840. the car of the elements of the table.
  4841. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4842. (unless sorting-type
  4843. (message
  4844. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4845. what)
  4846. (setq sorting-type (read-char-exclusive)))
  4847. (let ((dcst (downcase sorting-type))
  4848. extractfun comparefun)
  4849. ;; Define the appropriate functions
  4850. (cond
  4851. ((= dcst ?n)
  4852. (setq extractfun 'string-to-number
  4853. comparefun (if (= dcst sorting-type) '< '>)))
  4854. ((= dcst ?a)
  4855. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4856. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4857. comparefun (if (= dcst sorting-type)
  4858. 'string<
  4859. (lambda (a b) (and (not (string< a b))
  4860. (not (string= a b)))))))
  4861. ((= dcst ?t)
  4862. (setq extractfun
  4863. (lambda (x)
  4864. (if (string-match org-ts-regexp x)
  4865. (time-to-seconds
  4866. (org-time-string-to-time (match-string 0 x)))
  4867. 0))
  4868. comparefun (if (= dcst sorting-type) '< '>)))
  4869. (t (error "Invalid sorting type `%c'" sorting-type)))
  4870. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4871. table)
  4872. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4873. ;;;; Plain list items, including checkboxes
  4874. ;;; Plain list items
  4875. (defun org-at-item-p ()
  4876. "Is point in a line starting a hand-formatted item?"
  4877. (let ((llt org-plain-list-ordered-item-terminator))
  4878. (save-excursion
  4879. (goto-char (point-at-bol))
  4880. (looking-at
  4881. (cond
  4882. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4883. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4884. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4885. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4886. (defun org-in-item-p ()
  4887. "It the cursor inside a plain list item.
  4888. Does not have to be the first line."
  4889. (save-excursion
  4890. (condition-case nil
  4891. (progn
  4892. (org-beginning-of-item)
  4893. (org-at-item-p)
  4894. t)
  4895. (error nil))))
  4896. (defun org-insert-item (&optional checkbox)
  4897. "Insert a new item at the current level.
  4898. Return t when things worked, nil when we are not in an item."
  4899. (when (save-excursion
  4900. (condition-case nil
  4901. (progn
  4902. (org-beginning-of-item)
  4903. (org-at-item-p)
  4904. (if (org-invisible-p) (error "Invisible item"))
  4905. t)
  4906. (error nil)))
  4907. (let* ((bul (match-string 0))
  4908. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4909. (match-end 0)))
  4910. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4911. pos)
  4912. (cond
  4913. ((and (org-at-item-p) (<= (point) eow))
  4914. ;; before the bullet
  4915. (beginning-of-line 1)
  4916. (open-line (if blank 2 1)))
  4917. ((<= (point) eow)
  4918. (beginning-of-line 1))
  4919. (t
  4920. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4921. (end-of-line 1)
  4922. (delete-horizontal-space))
  4923. (newline (if blank 2 1))))
  4924. (insert bul (if checkbox "[ ]" ""))
  4925. (just-one-space)
  4926. (setq pos (point))
  4927. (end-of-line 1)
  4928. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4929. (org-maybe-renumber-ordered-list)
  4930. (and checkbox (org-update-checkbox-count-maybe))
  4931. t))
  4932. ;;; Checkboxes
  4933. (defun org-at-item-checkbox-p ()
  4934. "Is point at a line starting a plain-list item with a checklet?"
  4935. (and (org-at-item-p)
  4936. (save-excursion
  4937. (goto-char (match-end 0))
  4938. (skip-chars-forward " \t")
  4939. (looking-at "\\[[- X]\\]"))))
  4940. (defun org-toggle-checkbox (&optional arg)
  4941. "Toggle the checkbox in the current line."
  4942. (interactive "P")
  4943. (catch 'exit
  4944. (let (beg end status (firstnew 'unknown))
  4945. (cond
  4946. ((org-region-active-p)
  4947. (setq beg (region-beginning) end (region-end)))
  4948. ((org-on-heading-p)
  4949. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4950. ((org-at-item-checkbox-p)
  4951. (let ((pos (point)))
  4952. (replace-match
  4953. (cond (arg "[-]")
  4954. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4955. (t "[ ]"))
  4956. t t)
  4957. (goto-char pos))
  4958. (throw 'exit t))
  4959. (t (error "Not at a checkbox or heading, and no active region")))
  4960. (save-excursion
  4961. (goto-char beg)
  4962. (while (< (point) end)
  4963. (when (org-at-item-checkbox-p)
  4964. (setq status (equal (match-string 0) "[X]"))
  4965. (when (eq firstnew 'unknown)
  4966. (setq firstnew (not status)))
  4967. (replace-match
  4968. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4969. (beginning-of-line 2)))))
  4970. (org-update-checkbox-count-maybe))
  4971. (defun org-update-checkbox-count-maybe ()
  4972. "Update checkbox statistics unless turned off by user."
  4973. (when org-provide-checkbox-statistics
  4974. (org-update-checkbox-count)))
  4975. (defun org-update-checkbox-count (&optional all)
  4976. "Update the checkbox statistics in the current section.
  4977. This will find all statistic cookies like [57%] and [6/12] and update them
  4978. with the current numbers. With optional prefix argument ALL, do this for
  4979. the whole buffer."
  4980. (interactive "P")
  4981. (save-excursion
  4982. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4983. (beg (condition-case nil
  4984. (progn (outline-back-to-heading) (point))
  4985. (error (point-min))))
  4986. (end (move-marker (make-marker)
  4987. (progn (outline-next-heading) (point))))
  4988. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4989. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4990. (re-find (concat re "\\|" re-box))
  4991. beg-cookie end-cookie is-percent c-on c-off lim
  4992. eline curr-ind next-ind continue-from startsearch
  4993. (cstat 0)
  4994. )
  4995. (when all
  4996. (goto-char (point-min))
  4997. (outline-next-heading)
  4998. (setq beg (point) end (point-max)))
  4999. (goto-char end)
  5000. ;; find each statistic cookie
  5001. (while (re-search-backward re-find beg t)
  5002. (setq beg-cookie (match-beginning 1)
  5003. end-cookie (match-end 1)
  5004. cstat (+ cstat (if end-cookie 1 0))
  5005. startsearch (point-at-eol)
  5006. continue-from (point-at-bol)
  5007. is-percent (match-beginning 2)
  5008. lim (cond
  5009. ((org-on-heading-p) (outline-next-heading) (point))
  5010. ((org-at-item-p) (org-end-of-item) (point))
  5011. (t nil))
  5012. c-on 0
  5013. c-off 0)
  5014. (when lim
  5015. ;; find first checkbox for this cookie and gather
  5016. ;; statistics from all that are at this indentation level
  5017. (goto-char startsearch)
  5018. (if (re-search-forward re-box lim t)
  5019. (progn
  5020. (org-beginning-of-item)
  5021. (setq curr-ind (org-get-indentation))
  5022. (setq next-ind curr-ind)
  5023. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5024. (save-excursion (end-of-line) (setq eline (point)))
  5025. (if (re-search-forward re-box eline t)
  5026. (if (member (match-string 2) '("[ ]" "[-]"))
  5027. (setq c-off (1+ c-off))
  5028. (setq c-on (1+ c-on))
  5029. )
  5030. )
  5031. (org-end-of-item)
  5032. (setq next-ind (org-get-indentation))
  5033. )))
  5034. (goto-char continue-from)
  5035. ;; update cookie
  5036. (when end-cookie
  5037. (delete-region beg-cookie end-cookie)
  5038. (goto-char beg-cookie)
  5039. (insert
  5040. (if is-percent
  5041. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5042. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5043. ;; update items checkbox if it has one
  5044. (when (org-at-item-p)
  5045. (org-beginning-of-item)
  5046. (when (and (> (+ c-on c-off) 0)
  5047. (re-search-forward re-box (point-at-eol) t))
  5048. (setq beg-cookie (match-beginning 2)
  5049. end-cookie (match-end 2))
  5050. (delete-region beg-cookie end-cookie)
  5051. (goto-char beg-cookie)
  5052. (cond ((= c-off 0) (insert "[X]"))
  5053. ((= c-on 0) (insert "[ ]"))
  5054. (t (insert "[-]")))
  5055. )))
  5056. (goto-char continue-from))
  5057. (when (interactive-p)
  5058. (message "Checkbox satistics updated %s (%d places)"
  5059. (if all "in entire file" "in current outline entry") cstat)))))
  5060. (defun org-get-checkbox-statistics-face ()
  5061. "Select the face for checkbox statistics.
  5062. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5063. it will be `org-todo'."
  5064. (if (match-end 1)
  5065. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5066. (if (and (> (match-end 2) (match-beginning 2))
  5067. (equal (match-string 2) (match-string 3)))
  5068. 'org-done
  5069. 'org-todo)))
  5070. (defun org-get-indentation (&optional line)
  5071. "Get the indentation of the current line, interpreting tabs.
  5072. When LINE is given, assume it represents a line and compute its indentation."
  5073. (if line
  5074. (if (string-match "^ *" (org-remove-tabs line))
  5075. (match-end 0))
  5076. (save-excursion
  5077. (beginning-of-line 1)
  5078. (skip-chars-forward " \t")
  5079. (current-column))))
  5080. (defun org-remove-tabs (s &optional width)
  5081. "Replace tabulators in S with spaces.
  5082. Assumes that s is a single line, starting in column 0."
  5083. (setq width (or width tab-width))
  5084. (while (string-match "\t" s)
  5085. (setq s (replace-match
  5086. (make-string
  5087. (- (* width (/ (+ (match-beginning 0) width) width))
  5088. (match-beginning 0)) ?\ )
  5089. t t s)))
  5090. s)
  5091. (defun org-fix-indentation (line ind)
  5092. "Fix indentation in LINE.
  5093. IND is a cons cell with target and minimum indentation.
  5094. If the current indenation in LINE is smaller than the minimum,
  5095. leave it alone. If it is larger than ind, set it to the target."
  5096. (let* ((l (org-remove-tabs line))
  5097. (i (org-get-indentation l))
  5098. (i1 (car ind)) (i2 (cdr ind)))
  5099. (if (>= i i2) (setq l (substring line i2)))
  5100. (if (> i1 0)
  5101. (concat (make-string i1 ?\ ) l)
  5102. l)))
  5103. (defcustom org-empty-line-terminates-plain-lists nil
  5104. "Non-nil means, an empty line ends all plain list levels.
  5105. When nil, empty lines are part of the preceeding item."
  5106. :group 'org-plain-lists
  5107. :type 'boolean)
  5108. (defun org-beginning-of-item ()
  5109. "Go to the beginning of the current hand-formatted item.
  5110. If the cursor is not in an item, throw an error."
  5111. (interactive)
  5112. (let ((pos (point))
  5113. (limit (save-excursion
  5114. (condition-case nil
  5115. (progn
  5116. (org-back-to-heading)
  5117. (beginning-of-line 2) (point))
  5118. (error (point-min)))))
  5119. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5120. ind ind1)
  5121. (if (org-at-item-p)
  5122. (beginning-of-line 1)
  5123. (beginning-of-line 1)
  5124. (skip-chars-forward " \t")
  5125. (setq ind (current-column))
  5126. (if (catch 'exit
  5127. (while t
  5128. (beginning-of-line 0)
  5129. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5130. (if (looking-at "[ \t]*$")
  5131. (setq ind1 ind-empty)
  5132. (skip-chars-forward " \t")
  5133. (setq ind1 (current-column)))
  5134. (if (< ind1 ind)
  5135. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5136. nil
  5137. (goto-char pos)
  5138. (error "Not in an item")))))
  5139. (defun org-end-of-item ()
  5140. "Go to the end of the current hand-formatted item.
  5141. If the cursor is not in an item, throw an error."
  5142. (interactive)
  5143. (let* ((pos (point))
  5144. ind1
  5145. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5146. (limit (save-excursion (outline-next-heading) (point)))
  5147. (ind (save-excursion
  5148. (org-beginning-of-item)
  5149. (skip-chars-forward " \t")
  5150. (current-column)))
  5151. (end (catch 'exit
  5152. (while t
  5153. (beginning-of-line 2)
  5154. (if (eobp) (throw 'exit (point)))
  5155. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5156. (if (looking-at "[ \t]*$")
  5157. (setq ind1 ind-empty)
  5158. (skip-chars-forward " \t")
  5159. (setq ind1 (current-column)))
  5160. (if (<= ind1 ind)
  5161. (throw 'exit (point-at-bol)))))))
  5162. (if end
  5163. (goto-char end)
  5164. (goto-char pos)
  5165. (error "Not in an item"))))
  5166. (defun org-next-item ()
  5167. "Move to the beginning of the next item in the current plain list.
  5168. Error if not at a plain list, or if this is the last item in the list."
  5169. (interactive)
  5170. (let (ind ind1 (pos (point)))
  5171. (org-beginning-of-item)
  5172. (setq ind (org-get-indentation))
  5173. (org-end-of-item)
  5174. (setq ind1 (org-get-indentation))
  5175. (unless (and (org-at-item-p) (= ind ind1))
  5176. (goto-char pos)
  5177. (error "On last item"))))
  5178. (defun org-previous-item ()
  5179. "Move to the beginning of the previous item in the current plain list.
  5180. Error if not at a plain list, or if this is the first item in the list."
  5181. (interactive)
  5182. (let (beg ind ind1 (pos (point)))
  5183. (org-beginning-of-item)
  5184. (setq beg (point))
  5185. (setq ind (org-get-indentation))
  5186. (goto-char beg)
  5187. (catch 'exit
  5188. (while t
  5189. (beginning-of-line 0)
  5190. (if (looking-at "[ \t]*$")
  5191. nil
  5192. (if (<= (setq ind1 (org-get-indentation)) ind)
  5193. (throw 'exit t)))))
  5194. (condition-case nil
  5195. (if (or (not (org-at-item-p))
  5196. (< ind1 (1- ind)))
  5197. (error "")
  5198. (org-beginning-of-item))
  5199. (error (goto-char pos)
  5200. (error "On first item")))))
  5201. (defun org-first-list-item-p ()
  5202. "Is this heading the item in a plain list?"
  5203. (unless (org-at-item-p)
  5204. (error "Not at a plain list item"))
  5205. (org-beginning-of-item)
  5206. (= (point) (save-excursion (org-beginning-of-item-list))))
  5207. (defun org-move-item-down ()
  5208. "Move the plain list item at point down, i.e. swap with following item.
  5209. Subitems (items with larger indentation) are considered part of the item,
  5210. so this really moves item trees."
  5211. (interactive)
  5212. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5213. (org-beginning-of-item)
  5214. (setq beg0 (point))
  5215. (save-excursion
  5216. (setq ne-beg (org-back-over-empty-lines))
  5217. (setq beg (point)))
  5218. (goto-char beg0)
  5219. (setq ind (org-get-indentation))
  5220. (org-end-of-item)
  5221. (setq end0 (point))
  5222. (setq ind1 (org-get-indentation))
  5223. (setq ne-end (org-back-over-empty-lines))
  5224. (setq end (point))
  5225. (goto-char beg0)
  5226. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5227. ;; include less whitespace
  5228. (save-excursion
  5229. (goto-char beg)
  5230. (forward-line (- ne-beg ne-end))
  5231. (setq beg (point))))
  5232. (goto-char end0)
  5233. (if (and (org-at-item-p) (= ind ind1))
  5234. (progn
  5235. (org-end-of-item)
  5236. (org-back-over-empty-lines)
  5237. (setq txt (buffer-substring beg end))
  5238. (save-excursion
  5239. (delete-region beg end))
  5240. (setq pos (point))
  5241. (insert txt)
  5242. (goto-char pos) (org-skip-whitespace)
  5243. (org-maybe-renumber-ordered-list))
  5244. (goto-char pos)
  5245. (error "Cannot move this item further down"))))
  5246. (defun org-move-item-up (arg)
  5247. "Move the plain list item at point up, i.e. swap with previous item.
  5248. Subitems (items with larger indentation) are considered part of the item,
  5249. so this really moves item trees."
  5250. (interactive "p")
  5251. (let (beg beg0 end ind ind1 (pos (point)) txt
  5252. ne-beg ne-ins ins-end)
  5253. (org-beginning-of-item)
  5254. (setq beg0 (point))
  5255. (setq ind (org-get-indentation))
  5256. (save-excursion
  5257. (setq ne-beg (org-back-over-empty-lines))
  5258. (setq beg (point)))
  5259. (goto-char beg0)
  5260. (org-end-of-item)
  5261. (setq end (point))
  5262. (goto-char beg0)
  5263. (catch 'exit
  5264. (while t
  5265. (beginning-of-line 0)
  5266. (if (looking-at "[ \t]*$")
  5267. (if org-empty-line-terminates-plain-lists
  5268. (progn
  5269. (goto-char pos)
  5270. (error "Cannot move this item further up"))
  5271. nil)
  5272. (if (<= (setq ind1 (org-get-indentation)) ind)
  5273. (throw 'exit t)))))
  5274. (condition-case nil
  5275. (org-beginning-of-item)
  5276. (error (goto-char beg)
  5277. (error "Cannot move this item further up")))
  5278. (setq ind1 (org-get-indentation))
  5279. (if (and (org-at-item-p) (= ind ind1))
  5280. (progn
  5281. (setq ne-ins (org-back-over-empty-lines))
  5282. (setq txt (buffer-substring beg end))
  5283. (save-excursion
  5284. (delete-region beg end))
  5285. (setq pos (point))
  5286. (insert txt)
  5287. (setq ins-end (point))
  5288. (goto-char pos) (org-skip-whitespace)
  5289. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5290. ;; Move whitespace back to beginning
  5291. (save-excursion
  5292. (goto-char ins-end)
  5293. (let ((kill-whole-line t))
  5294. (kill-line (- ne-ins ne-beg)) (point)))
  5295. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5296. (org-maybe-renumber-ordered-list))
  5297. (goto-char pos)
  5298. (error "Cannot move this item further up"))))
  5299. (defun org-maybe-renumber-ordered-list ()
  5300. "Renumber the ordered list at point if setup allows it.
  5301. This tests the user option `org-auto-renumber-ordered-lists' before
  5302. doing the renumbering."
  5303. (interactive)
  5304. (when (and org-auto-renumber-ordered-lists
  5305. (org-at-item-p))
  5306. (if (match-beginning 3)
  5307. (org-renumber-ordered-list 1)
  5308. (org-fix-bullet-type))))
  5309. (defun org-maybe-renumber-ordered-list-safe ()
  5310. (condition-case nil
  5311. (save-excursion
  5312. (org-maybe-renumber-ordered-list))
  5313. (error nil)))
  5314. (defun org-cycle-list-bullet (&optional which)
  5315. "Cycle through the different itemize/enumerate bullets.
  5316. This cycle the entire list level through the sequence:
  5317. `-' -> `+' -> `*' -> `1.' -> `1)'
  5318. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5319. 0 meand `-', 1 means `+' etc."
  5320. (interactive "P")
  5321. (org-preserve-lc
  5322. (org-beginning-of-item-list)
  5323. (org-at-item-p)
  5324. (beginning-of-line 1)
  5325. (let ((current (match-string 0))
  5326. (prevp (eq which 'previous))
  5327. new)
  5328. (setq new (cond
  5329. ((and (numberp which)
  5330. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5331. ((string-match "-" current) (if prevp "1)" "+"))
  5332. ((string-match "\\+" current)
  5333. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5334. ((string-match "\\*" current) (if prevp "+" "1."))
  5335. ((string-match "\\." current) (if prevp "*" "1)"))
  5336. ((string-match ")" current) (if prevp "1." "-"))
  5337. (t (error "This should not happen"))))
  5338. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5339. (org-fix-bullet-type)
  5340. (org-maybe-renumber-ordered-list))))
  5341. (defun org-get-string-indentation (s)
  5342. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5343. (let ((n -1) (i 0) (w tab-width) c)
  5344. (catch 'exit
  5345. (while (< (setq n (1+ n)) (length s))
  5346. (setq c (aref s n))
  5347. (cond ((= c ?\ ) (setq i (1+ i)))
  5348. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5349. (t (throw 'exit t)))))
  5350. i))
  5351. (defun org-renumber-ordered-list (arg)
  5352. "Renumber an ordered plain list.
  5353. Cursor needs to be in the first line of an item, the line that starts
  5354. with something like \"1.\" or \"2)\"."
  5355. (interactive "p")
  5356. (unless (and (org-at-item-p)
  5357. (match-beginning 3))
  5358. (error "This is not an ordered list"))
  5359. (let ((line (org-current-line))
  5360. (col (current-column))
  5361. (ind (org-get-string-indentation
  5362. (buffer-substring (point-at-bol) (match-beginning 3))))
  5363. ;; (term (substring (match-string 3) -1))
  5364. ind1 (n (1- arg))
  5365. fmt)
  5366. ;; find where this list begins
  5367. (org-beginning-of-item-list)
  5368. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5369. (setq fmt (concat "%d" (match-string 1)))
  5370. (beginning-of-line 0)
  5371. ;; walk forward and replace these numbers
  5372. (catch 'exit
  5373. (while t
  5374. (catch 'next
  5375. (beginning-of-line 2)
  5376. (if (eobp) (throw 'exit nil))
  5377. (if (looking-at "[ \t]*$") (throw 'next nil))
  5378. (skip-chars-forward " \t") (setq ind1 (current-column))
  5379. (if (> ind1 ind) (throw 'next t))
  5380. (if (< ind1 ind) (throw 'exit t))
  5381. (if (not (org-at-item-p)) (throw 'exit nil))
  5382. (delete-region (match-beginning 2) (match-end 2))
  5383. (goto-char (match-beginning 2))
  5384. (insert (format fmt (setq n (1+ n)))))))
  5385. (goto-line line)
  5386. (move-to-column col)))
  5387. (defun org-fix-bullet-type ()
  5388. "Make sure all items in this list have the same bullet as the firsst item."
  5389. (interactive)
  5390. (unless (org-at-item-p) (error "This is not a list"))
  5391. (let ((line (org-current-line))
  5392. (col (current-column))
  5393. (ind (current-indentation))
  5394. ind1 bullet)
  5395. ;; find where this list begins
  5396. (org-beginning-of-item-list)
  5397. (beginning-of-line 1)
  5398. ;; find out what the bullet type is
  5399. (looking-at "[ \t]*\\(\\S-+\\)")
  5400. (setq bullet (match-string 1))
  5401. ;; walk forward and replace these numbers
  5402. (beginning-of-line 0)
  5403. (catch 'exit
  5404. (while t
  5405. (catch 'next
  5406. (beginning-of-line 2)
  5407. (if (eobp) (throw 'exit nil))
  5408. (if (looking-at "[ \t]*$") (throw 'next nil))
  5409. (skip-chars-forward " \t") (setq ind1 (current-column))
  5410. (if (> ind1 ind) (throw 'next t))
  5411. (if (< ind1 ind) (throw 'exit t))
  5412. (if (not (org-at-item-p)) (throw 'exit nil))
  5413. (skip-chars-forward " \t")
  5414. (looking-at "\\S-+")
  5415. (replace-match bullet))))
  5416. (goto-line line)
  5417. (move-to-column col)
  5418. (if (string-match "[0-9]" bullet)
  5419. (org-renumber-ordered-list 1))))
  5420. (defun org-beginning-of-item-list ()
  5421. "Go to the beginning of the current item list.
  5422. I.e. to the first item in this list."
  5423. (interactive)
  5424. (org-beginning-of-item)
  5425. (let ((pos (point-at-bol))
  5426. (ind (org-get-indentation))
  5427. ind1)
  5428. ;; find where this list begins
  5429. (catch 'exit
  5430. (while t
  5431. (catch 'next
  5432. (beginning-of-line 0)
  5433. (if (looking-at "[ \t]*$")
  5434. (throw (if (bobp) 'exit 'next) t))
  5435. (skip-chars-forward " \t") (setq ind1 (current-column))
  5436. (if (or (< ind1 ind)
  5437. (and (= ind1 ind)
  5438. (not (org-at-item-p)))
  5439. (bobp))
  5440. (throw 'exit t)
  5441. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5442. (goto-char pos)))
  5443. (defun org-end-of-item-list ()
  5444. "Go to the end of the current item list.
  5445. I.e. to the text after the last item."
  5446. (interactive)
  5447. (org-beginning-of-item)
  5448. (let ((pos (point-at-bol))
  5449. (ind (org-get-indentation))
  5450. ind1)
  5451. ;; find where this list begins
  5452. (catch 'exit
  5453. (while t
  5454. (catch 'next
  5455. (beginning-of-line 2)
  5456. (if (looking-at "[ \t]*$")
  5457. (throw (if (eobp) 'exit 'next) t))
  5458. (skip-chars-forward " \t") (setq ind1 (current-column))
  5459. (if (or (< ind1 ind)
  5460. (and (= ind1 ind)
  5461. (not (org-at-item-p)))
  5462. (eobp))
  5463. (progn
  5464. (setq pos (point-at-bol))
  5465. (throw 'exit t))))))
  5466. (goto-char pos)))
  5467. (defvar org-last-indent-begin-marker (make-marker))
  5468. (defvar org-last-indent-end-marker (make-marker))
  5469. (defun org-outdent-item (arg)
  5470. "Outdent a local list item."
  5471. (interactive "p")
  5472. (org-indent-item (- arg)))
  5473. (defun org-indent-item (arg)
  5474. "Indent a local list item."
  5475. (interactive "p")
  5476. (unless (org-at-item-p)
  5477. (error "Not on an item"))
  5478. (save-excursion
  5479. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5480. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5481. (setq beg org-last-indent-begin-marker
  5482. end org-last-indent-end-marker)
  5483. (org-beginning-of-item)
  5484. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5485. (org-end-of-item)
  5486. (setq end (move-marker org-last-indent-end-marker (point))))
  5487. (goto-char beg)
  5488. (setq tmp (org-item-indent-positions)
  5489. ind (car tmp)
  5490. ind-down (nth 2 tmp)
  5491. ind-up (nth 1 tmp)
  5492. delta (if (> arg 0)
  5493. (if ind-down (- ind-down ind) 2)
  5494. (if ind-up (- ind-up ind) -2)))
  5495. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5496. (while (< (point) end)
  5497. (beginning-of-line 1)
  5498. (skip-chars-forward " \t") (setq ind1 (current-column))
  5499. (delete-region (point-at-bol) (point))
  5500. (or (eolp) (indent-to-column (+ ind1 delta)))
  5501. (beginning-of-line 2))))
  5502. (org-fix-bullet-type)
  5503. (org-maybe-renumber-ordered-list-safe)
  5504. (save-excursion
  5505. (beginning-of-line 0)
  5506. (condition-case nil (org-beginning-of-item) (error nil))
  5507. (org-maybe-renumber-ordered-list-safe)))
  5508. (defun org-item-indent-positions ()
  5509. "Return indentation for plain list items.
  5510. This returns a list with three values: The current indentation, the
  5511. parent indentation and the indentation a child should habe.
  5512. Assumes cursor in item line."
  5513. (let* ((bolpos (point-at-bol))
  5514. (ind (org-get-indentation))
  5515. ind-down ind-up pos)
  5516. (save-excursion
  5517. (org-beginning-of-item-list)
  5518. (skip-chars-backward "\n\r \t")
  5519. (when (org-in-item-p)
  5520. (org-beginning-of-item)
  5521. (setq ind-up (org-get-indentation))))
  5522. (setq pos (point))
  5523. (save-excursion
  5524. (cond
  5525. ((and (condition-case nil (progn (org-previous-item) t)
  5526. (error nil))
  5527. (or (forward-char 1) t)
  5528. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5529. (setq ind-down (org-get-indentation)))
  5530. ((and (goto-char pos)
  5531. (org-at-item-p))
  5532. (goto-char (match-end 0))
  5533. (skip-chars-forward " \t")
  5534. (setq ind-down (current-column)))))
  5535. (list ind ind-up ind-down)))
  5536. ;;; The orgstruct minor mode
  5537. ;; Define a minor mode which can be used in other modes in order to
  5538. ;; integrate the org-mode structure editing commands.
  5539. ;; This is really a hack, because the org-mode structure commands use
  5540. ;; keys which normally belong to the major mode. Here is how it
  5541. ;; works: The minor mode defines all the keys necessary to operate the
  5542. ;; structure commands, but wraps the commands into a function which
  5543. ;; tests if the cursor is currently at a headline or a plain list
  5544. ;; item. If that is the case, the structure command is used,
  5545. ;; temporarily setting many Org-mode variables like regular
  5546. ;; expressions for filling etc. However, when any of those keys is
  5547. ;; used at a different location, function uses `key-binding' to look
  5548. ;; up if the key has an associated command in another currently active
  5549. ;; keymap (minor modes, major mode, global), and executes that
  5550. ;; command. There might be problems if any of the keys is otherwise
  5551. ;; used as a prefix key.
  5552. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5553. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5554. ;; addresses this by checking explicitly for both bindings.
  5555. (defvar orgstruct-mode-map (make-sparse-keymap)
  5556. "Keymap for the minor `orgstruct-mode'.")
  5557. (defvar org-local-vars nil
  5558. "List of local variables, for use by `orgstruct-mode'")
  5559. ;;;###autoload
  5560. (define-minor-mode orgstruct-mode
  5561. "Toggle the minor more `orgstruct-mode'.
  5562. This mode is for using Org-mode structure commands in other modes.
  5563. The following key behave as if Org-mode was active, if the cursor
  5564. is on a headline, or on a plain list item (both in the definition
  5565. of Org-mode).
  5566. M-up Move entry/item up
  5567. M-down Move entry/item down
  5568. M-left Promote
  5569. M-right Demote
  5570. M-S-up Move entry/item up
  5571. M-S-down Move entry/item down
  5572. M-S-left Promote subtree
  5573. M-S-right Demote subtree
  5574. M-q Fill paragraph and items like in Org-mode
  5575. C-c ^ Sort entries
  5576. C-c - Cycle list bullet
  5577. TAB Cycle item visibility
  5578. M-RET Insert new heading/item
  5579. S-M-RET Insert new TODO heading / Chekbox item
  5580. C-c C-c Set tags / toggle checkbox"
  5581. nil " OrgStruct" nil
  5582. (org-load-modules-maybe)
  5583. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5584. ;;;###autoload
  5585. (defun turn-on-orgstruct ()
  5586. "Unconditionally turn on `orgstruct-mode'."
  5587. (orgstruct-mode 1))
  5588. ;;;###autoload
  5589. (defun turn-on-orgstruct++ ()
  5590. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5591. In addition to setting orgstruct-mode, this also exports all indentation and
  5592. autofilling variables from org-mode into the buffer. Note that turning
  5593. off orgstruct-mode will *not* remove these additional settings."
  5594. (orgstruct-mode 1)
  5595. (let (var val)
  5596. (mapc
  5597. (lambda (x)
  5598. (when (string-match
  5599. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5600. (symbol-name (car x)))
  5601. (setq var (car x) val (nth 1 x))
  5602. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5603. org-local-vars)))
  5604. (defun orgstruct-error ()
  5605. "Error when there is no default binding for a structure key."
  5606. (interactive)
  5607. (error "This key has no function outside structure elements"))
  5608. (defun orgstruct-setup ()
  5609. "Setup orgstruct keymaps."
  5610. (let ((nfunc 0)
  5611. (bindings
  5612. (list
  5613. '([(meta up)] org-metaup)
  5614. '([(meta down)] org-metadown)
  5615. '([(meta left)] org-metaleft)
  5616. '([(meta right)] org-metaright)
  5617. '([(meta shift up)] org-shiftmetaup)
  5618. '([(meta shift down)] org-shiftmetadown)
  5619. '([(meta shift left)] org-shiftmetaleft)
  5620. '([(meta shift right)] org-shiftmetaright)
  5621. '([(shift up)] org-shiftup)
  5622. '([(shift down)] org-shiftdown)
  5623. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5624. '("\M-q" fill-paragraph)
  5625. '("\C-c^" org-sort)
  5626. '("\C-c-" org-cycle-list-bullet)))
  5627. elt key fun cmd)
  5628. (while (setq elt (pop bindings))
  5629. (setq nfunc (1+ nfunc))
  5630. (setq key (org-key (car elt))
  5631. fun (nth 1 elt)
  5632. cmd (orgstruct-make-binding fun nfunc key))
  5633. (org-defkey orgstruct-mode-map key cmd))
  5634. ;; Special treatment needed for TAB and RET
  5635. (org-defkey orgstruct-mode-map [(tab)]
  5636. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5637. (org-defkey orgstruct-mode-map "\C-i"
  5638. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5639. (org-defkey orgstruct-mode-map "\M-\C-m"
  5640. (orgstruct-make-binding 'org-insert-heading 105
  5641. "\M-\C-m" [(meta return)]))
  5642. (org-defkey orgstruct-mode-map [(meta return)]
  5643. (orgstruct-make-binding 'org-insert-heading 106
  5644. [(meta return)] "\M-\C-m"))
  5645. (org-defkey orgstruct-mode-map [(shift meta return)]
  5646. (orgstruct-make-binding 'org-insert-todo-heading 107
  5647. [(meta return)] "\M-\C-m"))
  5648. (unless org-local-vars
  5649. (setq org-local-vars (org-get-local-variables)))
  5650. t))
  5651. (defun orgstruct-make-binding (fun n &rest keys)
  5652. "Create a function for binding in the structure minor mode.
  5653. FUN is the command to call inside a table. N is used to create a unique
  5654. command name. KEYS are keys that should be checked in for a command
  5655. to execute outside of tables."
  5656. (eval
  5657. (list 'defun
  5658. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5659. '(arg)
  5660. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5661. "Outside of structure, run the binding of `"
  5662. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5663. "'.")
  5664. '(interactive "p")
  5665. (list 'if
  5666. '(org-context-p 'headline 'item)
  5667. (list 'org-run-like-in-org-mode (list 'quote fun))
  5668. (list 'let '(orgstruct-mode)
  5669. (list 'call-interactively
  5670. (append '(or)
  5671. (mapcar (lambda (k)
  5672. (list 'key-binding k))
  5673. keys)
  5674. '('orgstruct-error))))))))
  5675. (defun org-context-p (&rest contexts)
  5676. "Check if local context is and of CONTEXTS.
  5677. Possible values in the list of contexts are `table', `headline', and `item'."
  5678. (let ((pos (point)))
  5679. (goto-char (point-at-bol))
  5680. (prog1 (or (and (memq 'table contexts)
  5681. (looking-at "[ \t]*|"))
  5682. (and (memq 'headline contexts)
  5683. (looking-at "\\*+"))
  5684. (and (memq 'item contexts)
  5685. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5686. (goto-char pos))))
  5687. (defun org-get-local-variables ()
  5688. "Return a list of all local variables in an org-mode buffer."
  5689. (let (varlist)
  5690. (with-current-buffer (get-buffer-create "*Org tmp*")
  5691. (erase-buffer)
  5692. (org-mode)
  5693. (setq varlist (buffer-local-variables)))
  5694. (kill-buffer "*Org tmp*")
  5695. (delq nil
  5696. (mapcar
  5697. (lambda (x)
  5698. (setq x
  5699. (if (symbolp x)
  5700. (list x)
  5701. (list (car x) (list 'quote (cdr x)))))
  5702. (if (string-match
  5703. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5704. (symbol-name (car x)))
  5705. x nil))
  5706. varlist))))
  5707. ;;;###autoload
  5708. (defun org-run-like-in-org-mode (cmd)
  5709. (org-load-modules-maybe)
  5710. (unless org-local-vars
  5711. (setq org-local-vars (org-get-local-variables)))
  5712. (eval (list 'let org-local-vars
  5713. (list 'call-interactively (list 'quote cmd)))))
  5714. ;;;; Archiving
  5715. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5716. (defun org-archive-subtree (&optional find-done)
  5717. "Move the current subtree to the archive.
  5718. The archive can be a certain top-level heading in the current file, or in
  5719. a different file. The tree will be moved to that location, the subtree
  5720. heading be marked DONE, and the current time will be added.
  5721. When called with prefix argument FIND-DONE, find whole trees without any
  5722. open TODO items and archive them (after getting confirmation from the user).
  5723. If the cursor is not at a headline when this comand is called, try all level
  5724. 1 trees. If the cursor is on a headline, only try the direct children of
  5725. this heading."
  5726. (interactive "P")
  5727. (if find-done
  5728. (org-archive-all-done)
  5729. ;; Save all relevant TODO keyword-relatex variables
  5730. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5731. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5732. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5733. (tr-org-done-keywords org-done-keywords)
  5734. (tr-org-todo-regexp org-todo-regexp)
  5735. (tr-org-todo-line-regexp org-todo-line-regexp)
  5736. (tr-org-odd-levels-only org-odd-levels-only)
  5737. (this-buffer (current-buffer))
  5738. (org-archive-location org-archive-location)
  5739. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5740. ;; start of variables that will be used for saving context
  5741. ;; The compiler complains about them - keep them anyway!
  5742. (file (abbreviate-file-name (buffer-file-name)))
  5743. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  5744. (time (format-time-string
  5745. (substring (cdr org-time-stamp-formats) 1 -1)
  5746. (current-time)))
  5747. afile heading buffer level newfile-p
  5748. category todo priority
  5749. ;; start of variables that will be used for savind context
  5750. ltags itags prop)
  5751. ;; Try to find a local archive location
  5752. (save-excursion
  5753. (save-restriction
  5754. (widen)
  5755. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  5756. (if (and prop (string-match "\\S-" prop))
  5757. (setq org-archive-location prop)
  5758. (if (or (re-search-backward re nil t)
  5759. (re-search-forward re nil t))
  5760. (setq org-archive-location (match-string 1))))))
  5761. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5762. (progn
  5763. (setq afile (format (match-string 1 org-archive-location)
  5764. (file-name-nondirectory buffer-file-name))
  5765. heading (match-string 2 org-archive-location)))
  5766. (error "Invalid `org-archive-location'"))
  5767. (if (> (length afile) 0)
  5768. (setq newfile-p (not (file-exists-p afile))
  5769. buffer (find-file-noselect afile))
  5770. (setq buffer (current-buffer)))
  5771. (unless buffer
  5772. (error "Cannot access file \"%s\"" afile))
  5773. (if (and (> (length heading) 0)
  5774. (string-match "^\\*+" heading))
  5775. (setq level (match-end 0))
  5776. (setq heading nil level 0))
  5777. (save-excursion
  5778. (org-back-to-heading t)
  5779. ;; Get context information that will be lost by moving the tree
  5780. (org-refresh-category-properties)
  5781. (setq category (org-get-category)
  5782. todo (and (looking-at org-todo-line-regexp)
  5783. (match-string 2))
  5784. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  5785. ltags (org-get-tags)
  5786. itags (org-delete-all ltags (org-get-tags-at)))
  5787. (setq ltags (mapconcat 'identity ltags " ")
  5788. itags (mapconcat 'identity itags " "))
  5789. ;; We first only copy, in case something goes wrong
  5790. ;; we need to protect this-command, to avoid kill-region sets it,
  5791. ;; which would lead to duplication of subtrees
  5792. (let (this-command) (org-copy-subtree))
  5793. (set-buffer buffer)
  5794. ;; Enforce org-mode for the archive buffer
  5795. (if (not (org-mode-p))
  5796. ;; Force the mode for future visits.
  5797. (let ((org-insert-mode-line-in-empty-file t)
  5798. (org-inhibit-startup t))
  5799. (call-interactively 'org-mode)))
  5800. (when newfile-p
  5801. (goto-char (point-max))
  5802. (insert (format "\nArchived entries from file %s\n\n"
  5803. (buffer-file-name this-buffer))))
  5804. ;; Force the TODO keywords of the original buffer
  5805. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5806. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5807. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5808. (org-done-keywords tr-org-done-keywords)
  5809. (org-todo-regexp tr-org-todo-regexp)
  5810. (org-todo-line-regexp tr-org-todo-line-regexp)
  5811. (org-odd-levels-only
  5812. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5813. org-odd-levels-only
  5814. tr-org-odd-levels-only)))
  5815. (goto-char (point-min))
  5816. (show-all)
  5817. (if heading
  5818. (progn
  5819. (if (re-search-forward
  5820. (concat "^" (regexp-quote heading)
  5821. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5822. nil t)
  5823. (goto-char (match-end 0))
  5824. ;; Heading not found, just insert it at the end
  5825. (goto-char (point-max))
  5826. (or (bolp) (insert "\n"))
  5827. (insert "\n" heading "\n")
  5828. (end-of-line 0))
  5829. ;; Make the subtree visible
  5830. (show-subtree)
  5831. (org-end-of-subtree t)
  5832. (skip-chars-backward " \t\r\n")
  5833. (and (looking-at "[ \t\r\n]*")
  5834. (replace-match "\n\n")))
  5835. ;; No specific heading, just go to end of file.
  5836. (goto-char (point-max)) (insert "\n"))
  5837. ;; Paste
  5838. (org-paste-subtree (org-get-valid-level level 1))
  5839. ;; Mark the entry as done
  5840. (when (and org-archive-mark-done
  5841. (looking-at org-todo-line-regexp)
  5842. (or (not (match-end 2))
  5843. (not (member (match-string 2) org-done-keywords))))
  5844. (let (org-log-done org-todo-log-states)
  5845. (org-todo
  5846. (car (or (member org-archive-mark-done org-done-keywords)
  5847. org-done-keywords)))))
  5848. ;; Add the context info
  5849. (when org-archive-save-context-info
  5850. (let ((l org-archive-save-context-info) e n v)
  5851. (while (setq e (pop l))
  5852. (when (and (setq v (symbol-value e))
  5853. (stringp v) (string-match "\\S-" v))
  5854. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  5855. (org-entry-put (point) n v)))))
  5856. ;; Save and kill the buffer, if it is not the same buffer.
  5857. (if (not (eq this-buffer buffer))
  5858. (progn (save-buffer) (kill-buffer buffer)))))
  5859. ;; Here we are back in the original buffer. Everything seems to have
  5860. ;; worked. So now cut the tree and finish up.
  5861. (let (this-command) (org-cut-subtree))
  5862. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5863. (message "Subtree archived %s"
  5864. (if (eq this-buffer buffer)
  5865. (concat "under heading: " heading)
  5866. (concat "in file: " (abbreviate-file-name afile)))))))
  5867. (defun org-get-category (&optional pos)
  5868. "Get the category applying to position POS."
  5869. (get-text-property (or pos (point)) 'org-category))
  5870. (defun org-refresh-category-properties ()
  5871. "Refresh category text properties in the buffer."
  5872. (let ((def-cat (cond
  5873. ((null org-category)
  5874. (if buffer-file-name
  5875. (file-name-sans-extension
  5876. (file-name-nondirectory buffer-file-name))
  5877. "???"))
  5878. ((symbolp org-category) (symbol-name org-category))
  5879. (t org-category)))
  5880. beg end cat pos optionp)
  5881. (org-unmodified
  5882. (save-excursion
  5883. (save-restriction
  5884. (widen)
  5885. (goto-char (point-min))
  5886. (put-text-property (point) (point-max) 'org-category def-cat)
  5887. (while (re-search-forward
  5888. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5889. (setq pos (match-end 0)
  5890. optionp (equal (char-after (match-beginning 0)) ?#)
  5891. cat (org-trim (match-string 2)))
  5892. (if optionp
  5893. (setq beg (point-at-bol) end (point-max))
  5894. (org-back-to-heading t)
  5895. (setq beg (point) end (org-end-of-subtree t t)))
  5896. (put-text-property beg end 'org-category cat)
  5897. (goto-char pos)))))))
  5898. (defun org-archive-all-done (&optional tag)
  5899. "Archive sublevels of the current tree without open TODO items.
  5900. If the cursor is not on a headline, try all level 1 trees. If
  5901. it is on a headline, try all direct children.
  5902. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5903. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5904. (rea (concat ".*:" org-archive-tag ":"))
  5905. (begm (make-marker))
  5906. (endm (make-marker))
  5907. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5908. "Move subtree to archive (no open TODO items)? "))
  5909. beg end (cntarch 0))
  5910. (if (org-on-heading-p)
  5911. (progn
  5912. (setq re1 (concat "^" (regexp-quote
  5913. (make-string
  5914. (1+ (- (match-end 0) (match-beginning 0) 1))
  5915. ?*))
  5916. " "))
  5917. (move-marker begm (point))
  5918. (move-marker endm (org-end-of-subtree t)))
  5919. (setq re1 "^* ")
  5920. (move-marker begm (point-min))
  5921. (move-marker endm (point-max)))
  5922. (save-excursion
  5923. (goto-char begm)
  5924. (while (re-search-forward re1 endm t)
  5925. (setq beg (match-beginning 0)
  5926. end (save-excursion (org-end-of-subtree t) (point)))
  5927. (goto-char beg)
  5928. (if (re-search-forward re end t)
  5929. (goto-char end)
  5930. (goto-char beg)
  5931. (if (and (or (not tag) (not (looking-at rea)))
  5932. (y-or-n-p question))
  5933. (progn
  5934. (if tag
  5935. (org-toggle-tag org-archive-tag 'on)
  5936. (org-archive-subtree))
  5937. (setq cntarch (1+ cntarch)))
  5938. (goto-char end)))))
  5939. (message "%d trees archived" cntarch)))
  5940. (defun org-cycle-hide-drawers (state)
  5941. "Re-hide all drawers after a visibility state change."
  5942. (when (and (org-mode-p)
  5943. (not (memq state '(overview folded))))
  5944. (save-excursion
  5945. (let* ((globalp (memq state '(contents all)))
  5946. (beg (if globalp (point-min) (point)))
  5947. (end (if globalp (point-max) (org-end-of-subtree t))))
  5948. (goto-char beg)
  5949. (while (re-search-forward org-drawer-regexp end t)
  5950. (org-flag-drawer t))))))
  5951. (defun org-flag-drawer (flag)
  5952. (save-excursion
  5953. (beginning-of-line 1)
  5954. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5955. (let ((b (match-end 0))
  5956. (outline-regexp org-outline-regexp))
  5957. (if (re-search-forward
  5958. "^[ \t]*:END:"
  5959. (save-excursion (outline-next-heading) (point)) t)
  5960. (outline-flag-region b (point-at-eol) flag)
  5961. (error ":END: line missing"))))))
  5962. (defun org-cycle-hide-archived-subtrees (state)
  5963. "Re-hide all archived subtrees after a visibility state change."
  5964. (when (and (not org-cycle-open-archived-trees)
  5965. (not (memq state '(overview folded))))
  5966. (save-excursion
  5967. (let* ((globalp (memq state '(contents all)))
  5968. (beg (if globalp (point-min) (point)))
  5969. (end (if globalp (point-max) (org-end-of-subtree t))))
  5970. (org-hide-archived-subtrees beg end)
  5971. (goto-char beg)
  5972. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5973. (message "%s" (substitute-command-keys
  5974. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5975. (defun org-force-cycle-archived ()
  5976. "Cycle subtree even if it is archived."
  5977. (interactive)
  5978. (setq this-command 'org-cycle)
  5979. (let ((org-cycle-open-archived-trees t))
  5980. (call-interactively 'org-cycle)))
  5981. (defun org-hide-archived-subtrees (beg end)
  5982. "Re-hide all archived subtrees after a visibility state change."
  5983. (save-excursion
  5984. (let* ((re (concat ":" org-archive-tag ":")))
  5985. (goto-char beg)
  5986. (while (re-search-forward re end t)
  5987. (and (org-on-heading-p) (hide-subtree))
  5988. (org-end-of-subtree t)))))
  5989. (defun org-toggle-tag (tag &optional onoff)
  5990. "Toggle the tag TAG for the current line.
  5991. If ONOFF is `on' or `off', don't toggle but set to this state."
  5992. (unless (org-on-heading-p t) (error "Not on headling"))
  5993. (let (res current)
  5994. (save-excursion
  5995. (beginning-of-line)
  5996. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  5997. (point-at-eol) t)
  5998. (progn
  5999. (setq current (match-string 1))
  6000. (replace-match ""))
  6001. (setq current ""))
  6002. (setq current (nreverse (org-split-string current ":")))
  6003. (cond
  6004. ((eq onoff 'on)
  6005. (setq res t)
  6006. (or (member tag current) (push tag current)))
  6007. ((eq onoff 'off)
  6008. (or (not (member tag current)) (setq current (delete tag current))))
  6009. (t (if (member tag current)
  6010. (setq current (delete tag current))
  6011. (setq res t)
  6012. (push tag current))))
  6013. (end-of-line 1)
  6014. (if current
  6015. (progn
  6016. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  6017. (org-set-tags nil t))
  6018. (delete-horizontal-space))
  6019. (run-hooks 'org-after-tags-change-hook))
  6020. res))
  6021. (defun org-toggle-archive-tag (&optional arg)
  6022. "Toggle the archive tag for the current headline.
  6023. With prefix ARG, check all children of current headline and offer tagging
  6024. the children that do not contain any open TODO items."
  6025. (interactive "P")
  6026. (if arg
  6027. (org-archive-all-done 'tag)
  6028. (let (set)
  6029. (save-excursion
  6030. (org-back-to-heading t)
  6031. (setq set (org-toggle-tag org-archive-tag))
  6032. (when set (hide-subtree)))
  6033. (and set (beginning-of-line 1))
  6034. (message "Subtree %s" (if set "archived" "unarchived")))))
  6035. ;;;; Link Stuff
  6036. ;;; Link abbreviations
  6037. (defun org-link-expand-abbrev (link)
  6038. "Apply replacements as defined in `org-link-abbrev-alist."
  6039. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6040. (let* ((key (match-string 1 link))
  6041. (as (or (assoc key org-link-abbrev-alist-local)
  6042. (assoc key org-link-abbrev-alist)))
  6043. (tag (and (match-end 2) (match-string 3 link)))
  6044. rpl)
  6045. (if (not as)
  6046. link
  6047. (setq rpl (cdr as))
  6048. (cond
  6049. ((symbolp rpl) (funcall rpl tag))
  6050. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6051. (t (concat rpl tag)))))
  6052. link))
  6053. ;;; Storing and inserting links
  6054. (defvar org-insert-link-history nil
  6055. "Minibuffer history for links inserted with `org-insert-link'.")
  6056. (defvar org-stored-links nil
  6057. "Contains the links stored with `org-store-link'.")
  6058. (defvar org-store-link-plist nil
  6059. "Plist with info about the most recently link created with `org-store-link'.")
  6060. (defvar org-link-protocols nil
  6061. "Link protocols added to Org-mode using `org-add-link-type'.")
  6062. (defvar org-store-link-functions nil
  6063. "List of functions that are called to create and store a link.
  6064. Each function will be called in turn until one returns a non-nil
  6065. value. Each function should check if it is responsible for creating
  6066. this link (for example by looking at the major mode).
  6067. If not, it must exit and return nil.
  6068. If yes, it should return a non-nil value after a calling
  6069. `org-store-link-props' with a list of properties and values.
  6070. Special properties are:
  6071. :type The link prefix. like \"http\". This must be given.
  6072. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6073. This is obligatory as well.
  6074. :description Optional default description for the second pair
  6075. of brackets in an Org-mode link. The user can still change
  6076. this when inserting this link into an Org-mode buffer.
  6077. In addition to these, any additional properties can be specified
  6078. and then used in remember templates.")
  6079. (defun org-add-link-type (type &optional follow export)
  6080. "Add TYPE to the list of `org-link-types'.
  6081. Re-compute all regular expressions depending on `org-link-types'
  6082. FOLLOW and EXPORT are two functions.
  6083. FOLLOW should take the link path as the single argument and do whatever
  6084. is necessary to follow the link, for example find a file or display
  6085. a mail message.
  6086. EXPORT should format the link path for export to one of the export formats.
  6087. It should be a function accepting three arguments:
  6088. path the path of the link, the text after the prefix (like \"http:\")
  6089. desc the description of the link, if any, nil if there was no descripton
  6090. format the export format, a symbol like `html' or `latex'.
  6091. The function may use the FORMAT information to return different values
  6092. depending on the format. The return value will be put literally into
  6093. the exported file.
  6094. Org-mode has a built-in default for exporting links. If you are happy with
  6095. this default, there is no need to define an export function for the link
  6096. type. For a simple example of an export function, see `org-bbdb.el'."
  6097. (add-to-list 'org-link-types type t)
  6098. (org-make-link-regexps)
  6099. (if (assoc type org-link-protocols)
  6100. (setcdr (assoc type org-link-protocols) (list follow export))
  6101. (push (list type follow export) org-link-protocols)))
  6102. ;;;###autoload
  6103. (defun org-store-link (arg)
  6104. "\\<org-mode-map>Store an org-link to the current location.
  6105. This link is added to `org-stored-links' and can later be inserted
  6106. into an org-buffer with \\[org-insert-link].
  6107. For some link types, a prefix arg is interpreted:
  6108. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6109. For file links, arg negates `org-context-in-file-links'."
  6110. (interactive "P")
  6111. (org-load-modules-maybe)
  6112. (setq org-store-link-plist nil) ; reset
  6113. (let (link cpltxt desc description search txt)
  6114. (cond
  6115. ((run-hook-with-args-until-success 'org-store-link-functions)
  6116. (setq link (plist-get org-store-link-plist :link)
  6117. desc (or (plist-get org-store-link-plist :description) link)))
  6118. ((eq major-mode 'calendar-mode)
  6119. (let ((cd (calendar-cursor-to-date)))
  6120. (setq link
  6121. (format-time-string
  6122. (car org-time-stamp-formats)
  6123. (apply 'encode-time
  6124. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6125. nil nil nil))))
  6126. (org-store-link-props :type "calendar" :date cd)))
  6127. ((eq major-mode 'w3-mode)
  6128. (setq cpltxt (url-view-url t)
  6129. link (org-make-link cpltxt))
  6130. (org-store-link-props :type "w3" :url (url-view-url t)))
  6131. ((eq major-mode 'w3m-mode)
  6132. (setq cpltxt (or w3m-current-title w3m-current-url)
  6133. link (org-make-link w3m-current-url))
  6134. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6135. ((setq search (run-hook-with-args-until-success
  6136. 'org-create-file-search-functions))
  6137. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6138. "::" search))
  6139. (setq cpltxt (or description link)))
  6140. ((eq major-mode 'image-mode)
  6141. (setq cpltxt (concat "file:"
  6142. (abbreviate-file-name buffer-file-name))
  6143. link (org-make-link cpltxt))
  6144. (org-store-link-props :type "image" :file buffer-file-name))
  6145. ((eq major-mode 'dired-mode)
  6146. ;; link to the file in the current line
  6147. (setq cpltxt (concat "file:"
  6148. (abbreviate-file-name
  6149. (expand-file-name
  6150. (dired-get-filename nil t))))
  6151. link (org-make-link cpltxt)))
  6152. ((and buffer-file-name (org-mode-p))
  6153. ;; Just link to current headline
  6154. (setq cpltxt (concat "file:"
  6155. (abbreviate-file-name buffer-file-name)))
  6156. ;; Add a context search string
  6157. (when (org-xor org-context-in-file-links arg)
  6158. ;; Check if we are on a target
  6159. (if (org-in-regexp "<<\\(.*?\\)>>")
  6160. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6161. (setq txt (cond
  6162. ((org-on-heading-p) nil)
  6163. ((org-region-active-p)
  6164. (buffer-substring (region-beginning) (region-end)))
  6165. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  6166. (when (or (null txt) (string-match "\\S-" txt))
  6167. (setq cpltxt
  6168. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6169. desc "NONE"))))
  6170. (if (string-match "::\\'" cpltxt)
  6171. (setq cpltxt (substring cpltxt 0 -2)))
  6172. (setq link (org-make-link cpltxt)))
  6173. ((buffer-file-name (buffer-base-buffer))
  6174. ;; Just link to this file here.
  6175. (setq cpltxt (concat "file:"
  6176. (abbreviate-file-name
  6177. (buffer-file-name (buffer-base-buffer)))))
  6178. ;; Add a context string
  6179. (when (org-xor org-context-in-file-links arg)
  6180. (setq txt (if (org-region-active-p)
  6181. (buffer-substring (region-beginning) (region-end))
  6182. (buffer-substring (point-at-bol) (point-at-eol))))
  6183. ;; Only use search option if there is some text.
  6184. (when (string-match "\\S-" txt)
  6185. (setq cpltxt
  6186. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6187. desc "NONE")))
  6188. (setq link (org-make-link cpltxt)))
  6189. ((interactive-p)
  6190. (error "Cannot link to a buffer which is not visiting a file"))
  6191. (t (setq link nil)))
  6192. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6193. (setq link (or link cpltxt)
  6194. desc (or desc cpltxt))
  6195. (if (equal desc "NONE") (setq desc nil))
  6196. (if (and (interactive-p) link)
  6197. (progn
  6198. (setq org-stored-links
  6199. (cons (list link desc) org-stored-links))
  6200. (message "Stored: %s" (or desc link)))
  6201. (and link (org-make-link-string link desc)))))
  6202. (defun org-store-link-props (&rest plist)
  6203. "Store link properties, extract names and addresses."
  6204. (let (x adr)
  6205. (when (setq x (plist-get plist :from))
  6206. (setq adr (mail-extract-address-components x))
  6207. (plist-put plist :fromname (car adr))
  6208. (plist-put plist :fromaddress (nth 1 adr)))
  6209. (when (setq x (plist-get plist :to))
  6210. (setq adr (mail-extract-address-components x))
  6211. (plist-put plist :toname (car adr))
  6212. (plist-put plist :toaddress (nth 1 adr))))
  6213. (let ((from (plist-get plist :from))
  6214. (to (plist-get plist :to)))
  6215. (when (and from to org-from-is-user-regexp)
  6216. (plist-put plist :fromto
  6217. (if (string-match org-from-is-user-regexp from)
  6218. (concat "to %t")
  6219. (concat "from %f")))))
  6220. (setq org-store-link-plist plist))
  6221. (defun org-add-link-props (&rest plist)
  6222. "Add these properties to the link property list."
  6223. (let (key value)
  6224. (while plist
  6225. (setq key (pop plist) value (pop plist))
  6226. (setq org-store-link-plist
  6227. (plist-put org-store-link-plist key value)))))
  6228. (defun org-email-link-description (&optional fmt)
  6229. "Return the description part of an email link.
  6230. This takes information from `org-store-link-plist' and formats it
  6231. according to FMT (default from `org-email-link-description-format')."
  6232. (setq fmt (or fmt org-email-link-description-format))
  6233. (let* ((p org-store-link-plist)
  6234. (to (plist-get p :toaddress))
  6235. (from (plist-get p :fromaddress))
  6236. (table
  6237. (list
  6238. (cons "%c" (plist-get p :fromto))
  6239. (cons "%F" (plist-get p :from))
  6240. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6241. (cons "%T" (plist-get p :to))
  6242. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6243. (cons "%s" (plist-get p :subject))
  6244. (cons "%m" (plist-get p :message-id)))))
  6245. (when (string-match "%c" fmt)
  6246. ;; Check if the user wrote this message
  6247. (if (and org-from-is-user-regexp from to
  6248. (save-match-data (string-match org-from-is-user-regexp from)))
  6249. (setq fmt (replace-match "to %t" t t fmt))
  6250. (setq fmt (replace-match "from %f" t t fmt))))
  6251. (org-replace-escapes fmt table)))
  6252. (defun org-make-org-heading-search-string (&optional string heading)
  6253. "Make search string for STRING or current headline."
  6254. (interactive)
  6255. (let ((s (or string (org-get-heading))))
  6256. (unless (and string (not heading))
  6257. ;; We are using a headline, clean up garbage in there.
  6258. (if (string-match org-todo-regexp s)
  6259. (setq s (replace-match "" t t s)))
  6260. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6261. (setq s (replace-match "" t t s)))
  6262. (setq s (org-trim s))
  6263. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6264. org-comment-string "\\)") s)
  6265. (setq s (replace-match "" t t s)))
  6266. (while (string-match org-ts-regexp s)
  6267. (setq s (replace-match "" t t s))))
  6268. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6269. (setq s (replace-match " " t t s)))
  6270. (or string (setq s (concat "*" s))) ; Add * for headlines
  6271. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6272. (defun org-make-link (&rest strings)
  6273. "Concatenate STRINGS."
  6274. (apply 'concat strings))
  6275. (defun org-make-link-string (link &optional description)
  6276. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6277. (unless (string-match "\\S-" link)
  6278. (error "Empty link"))
  6279. (when (stringp description)
  6280. ;; Remove brackets from the description, they are fatal.
  6281. (while (string-match "\\[" description)
  6282. (setq description (replace-match "{" t t description)))
  6283. (while (string-match "\\]" description)
  6284. (setq description (replace-match "}" t t description))))
  6285. (when (equal (org-link-escape link) description)
  6286. ;; No description needed, it is identical
  6287. (setq description nil))
  6288. (when (and (not description)
  6289. (not (equal link (org-link-escape link))))
  6290. (setq description link))
  6291. (concat "[[" (org-link-escape link) "]"
  6292. (if description (concat "[" description "]") "")
  6293. "]"))
  6294. (defconst org-link-escape-chars
  6295. '((?\ . "%20")
  6296. (?\[ . "%5B")
  6297. (?\] . "%5D")
  6298. (?\340 . "%E0") ; `a
  6299. (?\342 . "%E2") ; ^a
  6300. (?\347 . "%E7") ; ,c
  6301. (?\350 . "%E8") ; `e
  6302. (?\351 . "%E9") ; 'e
  6303. (?\352 . "%EA") ; ^e
  6304. (?\356 . "%EE") ; ^i
  6305. (?\364 . "%F4") ; ^o
  6306. (?\371 . "%F9") ; `u
  6307. (?\373 . "%FB") ; ^u
  6308. (?\; . "%3B")
  6309. (?? . "%3F")
  6310. (?= . "%3D")
  6311. (?+ . "%2B")
  6312. )
  6313. "Association list of escapes for some characters problematic in links.
  6314. This is the list that is used for internal purposes.")
  6315. (defconst org-link-escape-chars-browser
  6316. '((?\ . "%20")) ; 32 for the SPC char
  6317. "Association list of escapes for some characters problematic in links.
  6318. This is the list that is used before handing over to the browser.")
  6319. (defun org-link-escape (text &optional table)
  6320. "Escape charaters in TEXT that are problematic for links."
  6321. (setq table (or table org-link-escape-chars))
  6322. (when text
  6323. (let ((re (mapconcat (lambda (x) (regexp-quote
  6324. (char-to-string (car x))))
  6325. table "\\|")))
  6326. (while (string-match re text)
  6327. (setq text
  6328. (replace-match
  6329. (cdr (assoc (string-to-char (match-string 0 text))
  6330. table))
  6331. t t text)))
  6332. text)))
  6333. (defun org-link-unescape (text &optional table)
  6334. "Reverse the action of `org-link-escape'."
  6335. (setq table (or table org-link-escape-chars))
  6336. (when text
  6337. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6338. table "\\|")))
  6339. (while (string-match re text)
  6340. (setq text
  6341. (replace-match
  6342. (char-to-string (car (rassoc (match-string 0 text) table)))
  6343. t t text)))
  6344. text)))
  6345. (defun org-xor (a b)
  6346. "Exclusive or."
  6347. (if a (not b) b))
  6348. (defun org-get-header (header)
  6349. "Find a header field in the current buffer."
  6350. (save-excursion
  6351. (goto-char (point-min))
  6352. (let ((case-fold-search t) s)
  6353. (cond
  6354. ((eq header 'from)
  6355. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6356. (setq s (match-string 1)))
  6357. (while (string-match "\"" s)
  6358. (setq s (replace-match "" t t s)))
  6359. (if (string-match "[<(].*" s)
  6360. (setq s (replace-match "" t t s))))
  6361. ((eq header 'message-id)
  6362. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6363. (setq s (match-string 1))))
  6364. ((eq header 'subject)
  6365. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6366. (setq s (match-string 1)))))
  6367. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6368. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6369. s)))
  6370. (defun org-fixup-message-id-for-http (s)
  6371. "Replace special characters in a message id, so it can be used in an http query."
  6372. (while (string-match "<" s)
  6373. (setq s (replace-match "%3C" t t s)))
  6374. (while (string-match ">" s)
  6375. (setq s (replace-match "%3E" t t s)))
  6376. (while (string-match "@" s)
  6377. (setq s (replace-match "%40" t t s)))
  6378. s)
  6379. ;;;###autoload
  6380. (defun org-insert-link-global ()
  6381. "Insert a link like Org-mode does.
  6382. This command can be called in any mode to insert a link in Org-mode syntax."
  6383. (interactive)
  6384. (org-load-modules-maybe)
  6385. (org-run-like-in-org-mode 'org-insert-link))
  6386. (defun org-insert-link (&optional complete-file)
  6387. "Insert a link. At the prompt, enter the link.
  6388. Completion can be used to select a link previously stored with
  6389. `org-store-link'. When the empty string is entered (i.e. if you just
  6390. press RET at the prompt), the link defaults to the most recently
  6391. stored link. As SPC triggers completion in the minibuffer, you need to
  6392. use M-SPC or C-q SPC to force the insertion of a space character.
  6393. You will also be prompted for a description, and if one is given, it will
  6394. be displayed in the buffer instead of the link.
  6395. If there is already a link at point, this command will allow you to edit link
  6396. and description parts.
  6397. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  6398. selected using completion. The path to the file will be relative to
  6399. the current directory if the file is in the current directory or a
  6400. subdirectory. Otherwise, the link will be the absolute path as
  6401. completed in the minibuffer (i.e. normally ~/path/to/file).
  6402. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  6403. is in the current directory or below.
  6404. With three \\[universal-argument] prefixes, negate the meaning of
  6405. `org-keep-stored-link-after-insertion'."
  6406. (interactive "P")
  6407. (let* ((wcf (current-window-configuration))
  6408. (region (if (org-region-active-p)
  6409. (buffer-substring (region-beginning) (region-end))))
  6410. (remove (and region (list (region-beginning) (region-end))))
  6411. (desc region)
  6412. tmphist ; byte-compile incorrectly complains about this
  6413. link entry file)
  6414. (cond
  6415. ((org-in-regexp org-bracket-link-regexp 1)
  6416. ;; We do have a link at point, and we are going to edit it.
  6417. (setq remove (list (match-beginning 0) (match-end 0)))
  6418. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6419. (setq link (read-string "Link: "
  6420. (org-link-unescape
  6421. (org-match-string-no-properties 1)))))
  6422. ((or (org-in-regexp org-angle-link-re)
  6423. (org-in-regexp org-plain-link-re))
  6424. ;; Convert to bracket link
  6425. (setq remove (list (match-beginning 0) (match-end 0))
  6426. link (read-string "Link: "
  6427. (org-remove-angle-brackets (match-string 0)))))
  6428. ((equal complete-file '(4))
  6429. ;; Completing read for file names.
  6430. (setq file (read-file-name "File: "))
  6431. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6432. (pwd1 (file-name-as-directory (abbreviate-file-name
  6433. (expand-file-name ".")))))
  6434. (cond
  6435. ((equal complete-file '(16))
  6436. (setq link (org-make-link
  6437. "file:"
  6438. (abbreviate-file-name (expand-file-name file)))))
  6439. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6440. (setq link (org-make-link "file:" (match-string 1 file))))
  6441. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6442. (expand-file-name file))
  6443. (setq link (org-make-link
  6444. "file:" (match-string 1 (expand-file-name file)))))
  6445. (t (setq link (org-make-link "file:" file))))))
  6446. (t
  6447. ;; Read link, with completion for stored links.
  6448. (with-output-to-temp-buffer "*Org Links*"
  6449. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6450. (when org-stored-links
  6451. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6452. (princ (mapconcat
  6453. (lambda (x)
  6454. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6455. (reverse org-stored-links) "\n"))))
  6456. (let ((cw (selected-window)))
  6457. (select-window (get-buffer-window "*Org Links*"))
  6458. (shrink-window-if-larger-than-buffer)
  6459. (setq truncate-lines t)
  6460. (select-window cw))
  6461. ;; Fake a link history, containing the stored links.
  6462. (setq tmphist (append (mapcar 'car org-stored-links)
  6463. org-insert-link-history))
  6464. (unwind-protect
  6465. (setq link (org-completing-read
  6466. "Link: "
  6467. (append
  6468. (mapcar (lambda (x) (list (concat (car x) ":")))
  6469. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6470. (mapcar (lambda (x) (list (concat x ":")))
  6471. org-link-types))
  6472. nil nil nil
  6473. 'tmphist
  6474. (or (car (car org-stored-links)))))
  6475. (set-window-configuration wcf)
  6476. (kill-buffer "*Org Links*"))
  6477. (setq entry (assoc link org-stored-links))
  6478. (or entry (push link org-insert-link-history))
  6479. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6480. (not org-keep-stored-link-after-insertion))
  6481. (setq org-stored-links (delq (assoc link org-stored-links)
  6482. org-stored-links)))
  6483. (setq desc (or desc (nth 1 entry)))))
  6484. (if (string-match org-plain-link-re link)
  6485. ;; URL-like link, normalize the use of angular brackets.
  6486. (setq link (org-make-link (org-remove-angle-brackets link))))
  6487. ;; Check if we are linking to the current file with a search option
  6488. ;; If yes, simplify the link by using only the search option.
  6489. (when (and buffer-file-name
  6490. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6491. (let* ((path (match-string 1 link))
  6492. (case-fold-search nil)
  6493. (search (match-string 2 link)))
  6494. (save-match-data
  6495. (if (equal (file-truename buffer-file-name) (file-truename path))
  6496. ;; We are linking to this same file, with a search option
  6497. (setq link search)))))
  6498. ;; Check if we can/should use a relative path. If yes, simplify the link
  6499. (when (string-match "\\<file:\\(.*\\)" link)
  6500. (let* ((path (match-string 1 link))
  6501. (origpath path)
  6502. (case-fold-search nil))
  6503. (cond
  6504. ((eq org-link-file-path-type 'absolute)
  6505. (setq path (abbreviate-file-name (expand-file-name path))))
  6506. ((eq org-link-file-path-type 'noabbrev)
  6507. (setq path (expand-file-name path)))
  6508. ((eq org-link-file-path-type 'relative)
  6509. (setq path (file-relative-name path)))
  6510. (t
  6511. (save-match-data
  6512. (if (string-match (concat "^" (regexp-quote
  6513. (file-name-as-directory
  6514. (expand-file-name "."))))
  6515. (expand-file-name path))
  6516. ;; We are linking a file with relative path name.
  6517. (setq path (substring (expand-file-name path)
  6518. (match-end 0)))))))
  6519. (setq link (concat "file:" path))
  6520. (if (equal desc origpath)
  6521. (setq desc path))))
  6522. (setq desc (read-string "Description: " desc))
  6523. (unless (string-match "\\S-" desc) (setq desc nil))
  6524. (if remove (apply 'delete-region remove))
  6525. (insert (org-make-link-string link desc))))
  6526. (defun org-completing-read (&rest args)
  6527. (let ((minibuffer-local-completion-map
  6528. (copy-keymap minibuffer-local-completion-map)))
  6529. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6530. (apply 'completing-read args)))
  6531. ;;; Opening/following a link
  6532. (defvar org-link-search-failed nil)
  6533. (defun org-next-link ()
  6534. "Move forward to the next link.
  6535. If the link is in hidden text, expose it."
  6536. (interactive)
  6537. (when (and org-link-search-failed (eq this-command last-command))
  6538. (goto-char (point-min))
  6539. (message "Link search wrapped back to beginning of buffer"))
  6540. (setq org-link-search-failed nil)
  6541. (let* ((pos (point))
  6542. (ct (org-context))
  6543. (a (assoc :link ct)))
  6544. (if a (goto-char (nth 2 a)))
  6545. (if (re-search-forward org-any-link-re nil t)
  6546. (progn
  6547. (goto-char (match-beginning 0))
  6548. (if (org-invisible-p) (org-show-context)))
  6549. (goto-char pos)
  6550. (setq org-link-search-failed t)
  6551. (error "No further link found"))))
  6552. (defun org-previous-link ()
  6553. "Move backward to the previous link.
  6554. If the link is in hidden text, expose it."
  6555. (interactive)
  6556. (when (and org-link-search-failed (eq this-command last-command))
  6557. (goto-char (point-max))
  6558. (message "Link search wrapped back to end of buffer"))
  6559. (setq org-link-search-failed nil)
  6560. (let* ((pos (point))
  6561. (ct (org-context))
  6562. (a (assoc :link ct)))
  6563. (if a (goto-char (nth 1 a)))
  6564. (if (re-search-backward org-any-link-re nil t)
  6565. (progn
  6566. (goto-char (match-beginning 0))
  6567. (if (org-invisible-p) (org-show-context)))
  6568. (goto-char pos)
  6569. (setq org-link-search-failed t)
  6570. (error "No further link found"))))
  6571. (defun org-find-file-at-mouse (ev)
  6572. "Open file link or URL at mouse."
  6573. (interactive "e")
  6574. (mouse-set-point ev)
  6575. (org-open-at-point 'in-emacs))
  6576. (defun org-open-at-mouse (ev)
  6577. "Open file link or URL at mouse."
  6578. (interactive "e")
  6579. (mouse-set-point ev)
  6580. (org-open-at-point))
  6581. (defvar org-window-config-before-follow-link nil
  6582. "The window configuration before following a link.
  6583. This is saved in case the need arises to restore it.")
  6584. (defvar org-open-link-marker (make-marker)
  6585. "Marker pointing to the location where `org-open-at-point; was called.")
  6586. ;;;###autoload
  6587. (defun org-open-at-point-global ()
  6588. "Follow a link like Org-mode does.
  6589. This command can be called in any mode to follow a link that has
  6590. Org-mode syntax."
  6591. (interactive)
  6592. (org-run-like-in-org-mode 'org-open-at-point))
  6593. (defun org-open-at-point (&optional in-emacs)
  6594. "Open link at or after point.
  6595. If there is no link at point, this function will search forward up to
  6596. the end of the current subtree.
  6597. Normally, files will be opened by an appropriate application. If the
  6598. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6599. (interactive "P")
  6600. (org-load-modules-maybe)
  6601. (move-marker org-open-link-marker (point))
  6602. (setq org-window-config-before-follow-link (current-window-configuration))
  6603. (org-remove-occur-highlights nil nil t)
  6604. (if (org-at-timestamp-p t)
  6605. (org-follow-timestamp-link)
  6606. (let (type path link line search (pos (point)))
  6607. (catch 'match
  6608. (save-excursion
  6609. (skip-chars-forward "^]\n\r")
  6610. (when (org-in-regexp org-bracket-link-regexp)
  6611. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6612. (while (string-match " *\n *" link)
  6613. (setq link (replace-match " " t t link)))
  6614. (setq link (org-link-expand-abbrev link))
  6615. (if (string-match org-link-re-with-space2 link)
  6616. (setq type (match-string 1 link) path (match-string 2 link))
  6617. (setq type "thisfile" path link))
  6618. (throw 'match t)))
  6619. (when (get-text-property (point) 'org-linked-text)
  6620. (setq type "thisfile"
  6621. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6622. (1+ (point)) (point))
  6623. path (buffer-substring
  6624. (previous-single-property-change pos 'org-linked-text)
  6625. (next-single-property-change pos 'org-linked-text)))
  6626. (throw 'match t))
  6627. (save-excursion
  6628. (when (or (org-in-regexp org-angle-link-re)
  6629. (org-in-regexp org-plain-link-re))
  6630. (setq type (match-string 1) path (match-string 2))
  6631. (throw 'match t)))
  6632. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6633. (setq type "tree-match"
  6634. path (match-string 1))
  6635. (throw 'match t))
  6636. (save-excursion
  6637. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6638. (setq type "tags"
  6639. path (match-string 1))
  6640. (while (string-match ":" path)
  6641. (setq path (replace-match "+" t t path)))
  6642. (throw 'match t))))
  6643. (unless path
  6644. (error "No link found"))
  6645. ;; Remove any trailing spaces in path
  6646. (if (string-match " +\\'" path)
  6647. (setq path (replace-match "" t t path)))
  6648. (cond
  6649. ((assoc type org-link-protocols)
  6650. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6651. ((equal type "mailto")
  6652. (let ((cmd (car org-link-mailto-program))
  6653. (args (cdr org-link-mailto-program)) args1
  6654. (address path) (subject "") a)
  6655. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6656. (setq address (match-string 1 path)
  6657. subject (org-link-escape (match-string 2 path))))
  6658. (while args
  6659. (cond
  6660. ((not (stringp (car args))) (push (pop args) args1))
  6661. (t (setq a (pop args))
  6662. (if (string-match "%a" a)
  6663. (setq a (replace-match address t t a)))
  6664. (if (string-match "%s" a)
  6665. (setq a (replace-match subject t t a)))
  6666. (push a args1))))
  6667. (apply cmd (nreverse args1))))
  6668. ((member type '("http" "https" "ftp" "news"))
  6669. (browse-url (concat type ":" (org-link-escape
  6670. path org-link-escape-chars-browser))))
  6671. ((member type '("message"))
  6672. (browse-url (concat type ":" path)))
  6673. ((string= type "tags")
  6674. (org-tags-view in-emacs path))
  6675. ((string= type "thisfile")
  6676. (if in-emacs
  6677. (switch-to-buffer-other-window
  6678. (org-get-buffer-for-internal-link (current-buffer)))
  6679. (org-mark-ring-push))
  6680. (let ((cmd `(org-link-search
  6681. ,path
  6682. ,(cond ((equal in-emacs '(4)) 'occur)
  6683. ((equal in-emacs '(16)) 'org-occur)
  6684. (t nil))
  6685. ,pos)))
  6686. (condition-case nil (eval cmd)
  6687. (error (progn (widen) (eval cmd))))))
  6688. ((string= type "tree-match")
  6689. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6690. ((string= type "file")
  6691. (if (string-match "::\\([0-9]+\\)\\'" path)
  6692. (setq line (string-to-number (match-string 1 path))
  6693. path (substring path 0 (match-beginning 0)))
  6694. (if (string-match "::\\(.+\\)\\'" path)
  6695. (setq search (match-string 1 path)
  6696. path (substring path 0 (match-beginning 0)))))
  6697. (if (string-match "[*?{]" (file-name-nondirectory path))
  6698. (dired path)
  6699. (org-open-file path in-emacs line search)))
  6700. ((string= type "news")
  6701. (require 'org-gnus)
  6702. (org-gnus-follow-link path))
  6703. ((string= type "shell")
  6704. (let ((cmd path))
  6705. (if (or (not org-confirm-shell-link-function)
  6706. (funcall org-confirm-shell-link-function
  6707. (format "Execute \"%s\" in shell? "
  6708. (org-add-props cmd nil
  6709. 'face 'org-warning))))
  6710. (progn
  6711. (message "Executing %s" cmd)
  6712. (shell-command cmd))
  6713. (error "Abort"))))
  6714. ((string= type "elisp")
  6715. (let ((cmd path))
  6716. (if (or (not org-confirm-elisp-link-function)
  6717. (funcall org-confirm-elisp-link-function
  6718. (format "Execute \"%s\" as elisp? "
  6719. (org-add-props cmd nil
  6720. 'face 'org-warning))))
  6721. (message "%s => %s" cmd (eval (read cmd)))
  6722. (error "Abort"))))
  6723. (t
  6724. (browse-url-at-point)))))
  6725. (move-marker org-open-link-marker nil)
  6726. (run-hook-with-args 'org-follow-link-hook))
  6727. ;;; File search
  6728. (defvar org-create-file-search-functions nil
  6729. "List of functions to construct the right search string for a file link.
  6730. These functions are called in turn with point at the location to
  6731. which the link should point.
  6732. A function in the hook should first test if it would like to
  6733. handle this file type, for example by checking the major-mode or
  6734. the file extension. If it decides not to handle this file, it
  6735. should just return nil to give other functions a chance. If it
  6736. does handle the file, it must return the search string to be used
  6737. when following the link. The search string will be part of the
  6738. file link, given after a double colon, and `org-open-at-point'
  6739. will automatically search for it. If special measures must be
  6740. taken to make the search successful, another function should be
  6741. added to the companion hook `org-execute-file-search-functions',
  6742. which see.
  6743. A function in this hook may also use `setq' to set the variable
  6744. `description' to provide a suggestion for the descriptive text to
  6745. be used for this link when it gets inserted into an Org-mode
  6746. buffer with \\[org-insert-link].")
  6747. (defvar org-execute-file-search-functions nil
  6748. "List of functions to execute a file search triggered by a link.
  6749. Functions added to this hook must accept a single argument, the
  6750. search string that was part of the file link, the part after the
  6751. double colon. The function must first check if it would like to
  6752. handle this search, for example by checking the major-mode or the
  6753. file extension. If it decides not to handle this search, it
  6754. should just return nil to give other functions a chance. If it
  6755. does handle the search, it must return a non-nil value to keep
  6756. other functions from trying.
  6757. Each function can access the current prefix argument through the
  6758. variable `current-prefix-argument'. Note that a single prefix is
  6759. used to force opening a link in Emacs, so it may be good to only
  6760. use a numeric or double prefix to guide the search function.
  6761. In case this is needed, a function in this hook can also restore
  6762. the window configuration before `org-open-at-point' was called using:
  6763. (set-window-configuration org-window-config-before-follow-link)")
  6764. (defun org-link-search (s &optional type avoid-pos)
  6765. "Search for a link search option.
  6766. If S is surrounded by forward slashes, it is interpreted as a
  6767. regular expression. In org-mode files, this will create an `org-occur'
  6768. sparse tree. In ordinary files, `occur' will be used to list matches.
  6769. If the current buffer is in `dired-mode', grep will be used to search
  6770. in all files. If AVOID-POS is given, ignore matches near that position."
  6771. (let ((case-fold-search t)
  6772. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6773. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6774. (append '(("") (" ") ("\t") ("\n"))
  6775. org-emphasis-alist)
  6776. "\\|") "\\)"))
  6777. (pos (point))
  6778. (pre "") (post "")
  6779. words re0 re1 re2 re3 re4 re5 re2a reall)
  6780. (cond
  6781. ;; First check if there are any special
  6782. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6783. ;; Now try the builtin stuff
  6784. ((save-excursion
  6785. (goto-char (point-min))
  6786. (and
  6787. (re-search-forward
  6788. (concat "<<" (regexp-quote s0) ">>") nil t)
  6789. (setq pos (match-beginning 0))))
  6790. ;; There is an exact target for this
  6791. (goto-char pos))
  6792. ((string-match "^/\\(.*\\)/$" s)
  6793. ;; A regular expression
  6794. (cond
  6795. ((org-mode-p)
  6796. (org-occur (match-string 1 s)))
  6797. ;;((eq major-mode 'dired-mode)
  6798. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6799. (t (org-do-occur (match-string 1 s)))))
  6800. (t
  6801. ;; A normal search strings
  6802. (when (equal (string-to-char s) ?*)
  6803. ;; Anchor on headlines, post may include tags.
  6804. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6805. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6806. s (substring s 1)))
  6807. (remove-text-properties
  6808. 0 (length s)
  6809. '(face nil mouse-face nil keymap nil fontified nil) s)
  6810. ;; Make a series of regular expressions to find a match
  6811. (setq words (org-split-string s "[ \n\r\t]+")
  6812. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6813. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6814. "\\)" markers)
  6815. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6816. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6817. re1 (concat pre re2 post)
  6818. re3 (concat pre re4 post)
  6819. re5 (concat pre ".*" re4)
  6820. re2 (concat pre re2)
  6821. re2a (concat pre re2a)
  6822. re4 (concat pre re4)
  6823. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6824. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6825. re5 "\\)"
  6826. ))
  6827. (cond
  6828. ((eq type 'org-occur) (org-occur reall))
  6829. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6830. (t (goto-char (point-min))
  6831. (if (or (org-search-not-self 1 re0 nil t)
  6832. (org-search-not-self 1 re1 nil t)
  6833. (org-search-not-self 1 re2 nil t)
  6834. (org-search-not-self 1 re2a nil t)
  6835. (org-search-not-self 1 re3 nil t)
  6836. (org-search-not-self 1 re4 nil t)
  6837. (org-search-not-self 1 re5 nil t)
  6838. )
  6839. (goto-char (match-beginning 1))
  6840. (goto-char pos)
  6841. (error "No match")))))
  6842. (t
  6843. ;; Normal string-search
  6844. (goto-char (point-min))
  6845. (if (search-forward s nil t)
  6846. (goto-char (match-beginning 0))
  6847. (error "No match"))))
  6848. (and (org-mode-p) (org-show-context 'link-search))))
  6849. (defun org-search-not-self (group &rest args)
  6850. "Execute `re-search-forward', but only accept matches that do not
  6851. enclose the position of `org-open-link-marker'."
  6852. (let ((m org-open-link-marker))
  6853. (catch 'exit
  6854. (while (apply 're-search-forward args)
  6855. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6856. (goto-char (match-end group))
  6857. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6858. (> (match-beginning 0) (marker-position m))
  6859. (< (match-end 0) (marker-position m)))
  6860. (save-match-data
  6861. (or (not (org-in-regexp
  6862. org-bracket-link-analytic-regexp 1))
  6863. (not (match-end 4)) ; no description
  6864. (and (<= (match-beginning 4) (point))
  6865. (>= (match-end 4) (point))))))
  6866. (throw 'exit (point))))))))
  6867. (defun org-get-buffer-for-internal-link (buffer)
  6868. "Return a buffer to be used for displaying the link target of internal links."
  6869. (cond
  6870. ((not org-display-internal-link-with-indirect-buffer)
  6871. buffer)
  6872. ((string-match "(Clone)$" (buffer-name buffer))
  6873. (message "Buffer is already a clone, not making another one")
  6874. ;; we also do not modify visibility in this case
  6875. buffer)
  6876. (t ; make a new indirect buffer for displaying the link
  6877. (let* ((bn (buffer-name buffer))
  6878. (ibn (concat bn "(Clone)"))
  6879. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6880. (with-current-buffer ib (org-overview))
  6881. ib))))
  6882. (defun org-do-occur (regexp &optional cleanup)
  6883. "Call the Emacs command `occur'.
  6884. If CLEANUP is non-nil, remove the printout of the regular expression
  6885. in the *Occur* buffer. This is useful if the regex is long and not useful
  6886. to read."
  6887. (occur regexp)
  6888. (when cleanup
  6889. (let ((cwin (selected-window)) win beg end)
  6890. (when (setq win (get-buffer-window "*Occur*"))
  6891. (select-window win))
  6892. (goto-char (point-min))
  6893. (when (re-search-forward "match[a-z]+" nil t)
  6894. (setq beg (match-end 0))
  6895. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6896. (setq end (1- (match-beginning 0)))))
  6897. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6898. (goto-char (point-min))
  6899. (select-window cwin))))
  6900. ;;; The mark ring for links jumps
  6901. (defvar org-mark-ring nil
  6902. "Mark ring for positions before jumps in Org-mode.")
  6903. (defvar org-mark-ring-last-goto nil
  6904. "Last position in the mark ring used to go back.")
  6905. ;; Fill and close the ring
  6906. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6907. (loop for i from 1 to org-mark-ring-length do
  6908. (push (make-marker) org-mark-ring))
  6909. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6910. org-mark-ring)
  6911. (defun org-mark-ring-push (&optional pos buffer)
  6912. "Put the current position or POS into the mark ring and rotate it."
  6913. (interactive)
  6914. (setq pos (or pos (point)))
  6915. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6916. (move-marker (car org-mark-ring)
  6917. (or pos (point))
  6918. (or buffer (current-buffer)))
  6919. (message "%s"
  6920. (substitute-command-keys
  6921. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6922. (defun org-mark-ring-goto (&optional n)
  6923. "Jump to the previous position in the mark ring.
  6924. With prefix arg N, jump back that many stored positions. When
  6925. called several times in succession, walk through the entire ring.
  6926. Org-mode commands jumping to a different position in the current file,
  6927. or to another Org-mode file, automatically push the old position
  6928. onto the ring."
  6929. (interactive "p")
  6930. (let (p m)
  6931. (if (eq last-command this-command)
  6932. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6933. (setq p org-mark-ring))
  6934. (setq org-mark-ring-last-goto p)
  6935. (setq m (car p))
  6936. (switch-to-buffer (marker-buffer m))
  6937. (goto-char m)
  6938. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6939. (defun org-remove-angle-brackets (s)
  6940. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6941. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6942. s)
  6943. (defun org-add-angle-brackets (s)
  6944. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6945. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6946. s)
  6947. ;;; Following specific links
  6948. (defun org-follow-timestamp-link ()
  6949. (cond
  6950. ((org-at-date-range-p t)
  6951. (let ((org-agenda-start-on-weekday)
  6952. (t1 (match-string 1))
  6953. (t2 (match-string 2)))
  6954. (setq t1 (time-to-days (org-time-string-to-time t1))
  6955. t2 (time-to-days (org-time-string-to-time t2)))
  6956. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6957. ((org-at-timestamp-p t)
  6958. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6959. (substring (match-string 1) 0 10)))
  6960. 1))
  6961. (t (error "This should not happen"))))
  6962. ;;; Following file links
  6963. (defun org-open-file (path &optional in-emacs line search)
  6964. "Open the file at PATH.
  6965. First, this expands any special file name abbreviations. Then the
  6966. configuration variable `org-file-apps' is checked if it contains an
  6967. entry for this file type, and if yes, the corresponding command is launched.
  6968. If no application is found, Emacs simply visits the file.
  6969. With optional argument IN-EMACS, Emacs will visit the file.
  6970. Optional LINE specifies a line to go to, optional SEARCH a string to
  6971. search for. If LINE or SEARCH is given, the file will always be
  6972. opened in Emacs.
  6973. If the file does not exist, an error is thrown."
  6974. (setq in-emacs (or in-emacs line search))
  6975. (let* ((file (if (equal path "")
  6976. buffer-file-name
  6977. (substitute-in-file-name (expand-file-name path))))
  6978. (apps (append org-file-apps (org-default-apps)))
  6979. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6980. (dirp (if remp nil (file-directory-p file)))
  6981. (dfile (downcase file))
  6982. (old-buffer (current-buffer))
  6983. (old-pos (point))
  6984. (old-mode major-mode)
  6985. ext cmd)
  6986. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6987. (setq ext (match-string 1 dfile))
  6988. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6989. (setq ext (match-string 1 dfile))))
  6990. (if in-emacs
  6991. (setq cmd 'emacs)
  6992. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6993. (and dirp (cdr (assoc 'directory apps)))
  6994. (cdr (assoc ext apps))
  6995. (cdr (assoc t apps)))))
  6996. (when (eq cmd 'mailcap)
  6997. (require 'mailcap)
  6998. (mailcap-parse-mailcaps)
  6999. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7000. (command (mailcap-mime-info mime-type)))
  7001. (if (stringp command)
  7002. (setq cmd command)
  7003. (setq cmd 'emacs))))
  7004. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7005. (not (file-exists-p file))
  7006. (not org-open-non-existing-files))
  7007. (error "No such file: %s" file))
  7008. (cond
  7009. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7010. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7011. (while (string-match "['\"]%s['\"]" cmd)
  7012. (setq cmd (replace-match "%s" t t cmd)))
  7013. (while (string-match "%s" cmd)
  7014. (setq cmd (replace-match
  7015. (save-match-data (shell-quote-argument file))
  7016. t t cmd)))
  7017. (save-window-excursion
  7018. (start-process-shell-command cmd nil cmd)))
  7019. ((or (stringp cmd)
  7020. (eq cmd 'emacs))
  7021. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7022. (widen)
  7023. (if line (goto-line line)
  7024. (if search (org-link-search search))))
  7025. ((consp cmd)
  7026. (eval cmd))
  7027. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7028. (and (org-mode-p) (eq old-mode 'org-mode)
  7029. (or (not (equal old-buffer (current-buffer)))
  7030. (not (equal old-pos (point))))
  7031. (org-mark-ring-push old-pos old-buffer))))
  7032. (defun org-default-apps ()
  7033. "Return the default applications for this operating system."
  7034. (cond
  7035. ((eq system-type 'darwin)
  7036. org-file-apps-defaults-macosx)
  7037. ((eq system-type 'windows-nt)
  7038. org-file-apps-defaults-windowsnt)
  7039. (t org-file-apps-defaults-gnu)))
  7040. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7041. (defun org-file-remote-p (file)
  7042. "Test whether FILE specifies a location on a remote system.
  7043. Return non-nil if the location is indeed remote.
  7044. For example, the filename \"/user@host:/foo\" specifies a location
  7045. on the system \"/user@host:\"."
  7046. (cond ((fboundp 'file-remote-p)
  7047. (file-remote-p file))
  7048. ((fboundp 'tramp-handle-file-remote-p)
  7049. (tramp-handle-file-remote-p file))
  7050. ((and (boundp 'ange-ftp-name-format)
  7051. (string-match (car ange-ftp-name-format) file))
  7052. t)
  7053. (t nil)))
  7054. ;;;; Refiling
  7055. (defun org-get-org-file ()
  7056. "Read a filename, with default directory `org-directory'."
  7057. (let ((default (or org-default-notes-file remember-data-file)))
  7058. (read-file-name (format "File name [%s]: " default)
  7059. (file-name-as-directory org-directory)
  7060. default)))
  7061. (defun org-notes-order-reversed-p ()
  7062. "Check if the current file should receive notes in reversed order."
  7063. (cond
  7064. ((not org-reverse-note-order) nil)
  7065. ((eq t org-reverse-note-order) t)
  7066. ((not (listp org-reverse-note-order)) nil)
  7067. (t (catch 'exit
  7068. (let ((all org-reverse-note-order)
  7069. entry)
  7070. (while (setq entry (pop all))
  7071. (if (string-match (car entry) buffer-file-name)
  7072. (throw 'exit (cdr entry))))
  7073. nil)))))
  7074. (defvar org-refile-target-table nil
  7075. "The list of refile targets, created by `org-refile'.")
  7076. (defvar org-agenda-new-buffers nil
  7077. "Buffers created to visit agenda files.")
  7078. (defun org-get-refile-targets (&optional default-buffer)
  7079. "Produce a table with refile targets."
  7080. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7081. targets txt re files f desc descre)
  7082. (with-current-buffer (or default-buffer (current-buffer))
  7083. (while (setq entry (pop entries))
  7084. (setq files (car entry) desc (cdr entry))
  7085. (cond
  7086. ((null files) (setq files (list (current-buffer))))
  7087. ((eq files 'org-agenda-files)
  7088. (setq files (org-agenda-files 'unrestricted)))
  7089. ((and (symbolp files) (fboundp files))
  7090. (setq files (funcall files)))
  7091. ((and (symbolp files) (boundp files))
  7092. (setq files (symbol-value files))))
  7093. (if (stringp files) (setq files (list files)))
  7094. (cond
  7095. ((eq (car desc) :tag)
  7096. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7097. ((eq (car desc) :todo)
  7098. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7099. ((eq (car desc) :regexp)
  7100. (setq descre (cdr desc)))
  7101. ((eq (car desc) :level)
  7102. (setq descre (concat "^\\*\\{" (number-to-string
  7103. (if org-odd-levels-only
  7104. (1- (* 2 (cdr desc)))
  7105. (cdr desc)))
  7106. "\\}[ \t]")))
  7107. ((eq (car desc) :maxlevel)
  7108. (setq descre (concat "^\\*\\{1," (number-to-string
  7109. (if org-odd-levels-only
  7110. (1- (* 2 (cdr desc)))
  7111. (cdr desc)))
  7112. "\\}[ \t]")))
  7113. (t (error "Bad refiling target description %s" desc)))
  7114. (while (setq f (pop files))
  7115. (save-excursion
  7116. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7117. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7118. (save-excursion
  7119. (save-restriction
  7120. (widen)
  7121. (goto-char (point-min))
  7122. (while (re-search-forward descre nil t)
  7123. (goto-char (point-at-bol))
  7124. (when (looking-at org-complex-heading-regexp)
  7125. (setq txt (match-string 4)
  7126. re (concat "^" (regexp-quote
  7127. (buffer-substring (match-beginning 1)
  7128. (match-end 4)))))
  7129. (if (match-end 5) (setq re (concat re "[ \t]+"
  7130. (regexp-quote
  7131. (match-string 5)))))
  7132. (setq re (concat re "[ \t]*$"))
  7133. (when org-refile-use-outline-path
  7134. (setq txt (mapconcat 'identity
  7135. (append
  7136. (if (eq org-refile-use-outline-path 'file)
  7137. (list (file-name-nondirectory
  7138. (buffer-file-name (buffer-base-buffer))))
  7139. (if (eq org-refile-use-outline-path 'full-file-path)
  7140. (list (buffer-file-name (buffer-base-buffer)))))
  7141. (org-get-outline-path)
  7142. (list txt))
  7143. "/")))
  7144. (push (list txt f re (point)) targets))
  7145. (goto-char (point-at-eol))))))))
  7146. (nreverse targets))))
  7147. (defun org-get-outline-path ()
  7148. "Return the outline path to the current entry, as a list."
  7149. (let (rtn)
  7150. (save-excursion
  7151. (while (org-up-heading-safe)
  7152. (when (looking-at org-complex-heading-regexp)
  7153. (push (org-match-string-no-properties 4) rtn)))
  7154. rtn)))
  7155. (defvar org-refile-history nil
  7156. "History for refiling operations.")
  7157. (defun org-refile (&optional goto default-buffer)
  7158. "Move the entry at point to another heading.
  7159. The list of target headings is compiled using the information in
  7160. `org-refile-targets', which see. This list is created before each use
  7161. and will therefore always be up-to-date.
  7162. At the target location, the entry is filed as a subitem of the target heading.
  7163. Depending on `org-reverse-note-order', the new subitem will either be the
  7164. first of the last subitem.
  7165. With prefix arg GOTO, the command will only visit the target location,
  7166. not actually move anything.
  7167. With a double prefix `C-c C-c', go to the location where the last refiling
  7168. operation has put the subtree."
  7169. (interactive "P")
  7170. (let* ((cbuf (current-buffer))
  7171. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7172. pos it nbuf file re level reversed)
  7173. (if (equal goto '(16))
  7174. (org-refile-goto-last-stored)
  7175. (when (setq it (org-refile-get-location
  7176. (if goto "Goto: " "Refile to: ") default-buffer))
  7177. (setq file (nth 1 it)
  7178. re (nth 2 it)
  7179. pos (nth 3 it))
  7180. (setq nbuf (or (find-buffer-visiting file)
  7181. (find-file-noselect file)))
  7182. (if goto
  7183. (progn
  7184. (switch-to-buffer nbuf)
  7185. (goto-char pos)
  7186. (org-show-context 'org-goto))
  7187. (org-copy-special)
  7188. (save-excursion
  7189. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7190. (find-file-noselect file))))
  7191. (setq reversed (org-notes-order-reversed-p))
  7192. (save-excursion
  7193. (save-restriction
  7194. (widen)
  7195. (goto-char pos)
  7196. (looking-at outline-regexp)
  7197. (setq level (org-get-valid-level (funcall outline-level) 1))
  7198. (goto-char
  7199. (if reversed
  7200. (outline-next-heading)
  7201. (or (save-excursion (outline-get-next-sibling))
  7202. (org-end-of-subtree t t)
  7203. (point-max))))
  7204. (bookmark-set "org-refile-last-stored")
  7205. (org-paste-subtree level))))
  7206. (org-cut-special)
  7207. (message "Entry refiled to \"%s\"" (car it)))))))
  7208. (defun org-refile-goto-last-stored ()
  7209. "Go to the location where the last refile was stored."
  7210. (interactive)
  7211. (bookmark-jump "org-refile-last-stored")
  7212. (message "This is the location of the last refile"))
  7213. (defun org-refile-get-location (&optional prompt default-buffer)
  7214. "Prompt the user for a refile location, using PROMPT."
  7215. (let ((org-refile-targets org-refile-targets)
  7216. (org-refile-use-outline-path org-refile-use-outline-path))
  7217. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7218. (unless org-refile-target-table
  7219. (error "No refile targets"))
  7220. (let* ((cbuf (current-buffer))
  7221. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7222. (fname (and filename (file-truename filename)))
  7223. (tbl (mapcar
  7224. (lambda (x)
  7225. (if (not (equal fname (file-truename (nth 1 x))))
  7226. (cons (concat (car x) " (" (file-name-nondirectory
  7227. (nth 1 x)) ")")
  7228. (cdr x))
  7229. x))
  7230. org-refile-target-table))
  7231. (completion-ignore-case t))
  7232. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  7233. tbl)))
  7234. ;;;; Dynamic blocks
  7235. (defun org-find-dblock (name)
  7236. "Find the first dynamic block with name NAME in the buffer.
  7237. If not found, stay at current position and return nil."
  7238. (let (pos)
  7239. (save-excursion
  7240. (goto-char (point-min))
  7241. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7242. nil t)
  7243. (match-beginning 0))))
  7244. (if pos (goto-char pos))
  7245. pos))
  7246. (defconst org-dblock-start-re
  7247. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7248. "Matches the startline of a dynamic block, with parameters.")
  7249. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7250. "Matches the end of a dyhamic block.")
  7251. (defun org-create-dblock (plist)
  7252. "Create a dynamic block section, with parameters taken from PLIST.
  7253. PLIST must containe a :name entry which is used as name of the block."
  7254. (unless (bolp) (newline))
  7255. (let ((name (plist-get plist :name)))
  7256. (insert "#+BEGIN: " name)
  7257. (while plist
  7258. (if (eq (car plist) :name)
  7259. (setq plist (cddr plist))
  7260. (insert " " (prin1-to-string (pop plist)))))
  7261. (insert "\n\n#+END:\n")
  7262. (beginning-of-line -2)))
  7263. (defun org-prepare-dblock ()
  7264. "Prepare dynamic block for refresh.
  7265. This empties the block, puts the cursor at the insert position and returns
  7266. the property list including an extra property :name with the block name."
  7267. (unless (looking-at org-dblock-start-re)
  7268. (error "Not at a dynamic block"))
  7269. (let* ((begdel (1+ (match-end 0)))
  7270. (name (org-no-properties (match-string 1)))
  7271. (params (append (list :name name)
  7272. (read (concat "(" (match-string 3) ")")))))
  7273. (unless (re-search-forward org-dblock-end-re nil t)
  7274. (error "Dynamic block not terminated"))
  7275. (setq params
  7276. (append params
  7277. (list :content (buffer-substring
  7278. begdel (match-beginning 0)))))
  7279. (delete-region begdel (match-beginning 0))
  7280. (goto-char begdel)
  7281. (open-line 1)
  7282. params))
  7283. (defun org-map-dblocks (&optional command)
  7284. "Apply COMMAND to all dynamic blocks in the current buffer.
  7285. If COMMAND is not given, use `org-update-dblock'."
  7286. (let ((cmd (or command 'org-update-dblock))
  7287. pos)
  7288. (save-excursion
  7289. (goto-char (point-min))
  7290. (while (re-search-forward org-dblock-start-re nil t)
  7291. (goto-char (setq pos (match-beginning 0)))
  7292. (condition-case nil
  7293. (funcall cmd)
  7294. (error (message "Error during update of dynamic block")))
  7295. (goto-char pos)
  7296. (unless (re-search-forward org-dblock-end-re nil t)
  7297. (error "Dynamic block not terminated"))))))
  7298. (defun org-dblock-update (&optional arg)
  7299. "User command for updating dynamic blocks.
  7300. Update the dynamic block at point. With prefix ARG, update all dynamic
  7301. blocks in the buffer."
  7302. (interactive "P")
  7303. (if arg
  7304. (org-update-all-dblocks)
  7305. (or (looking-at org-dblock-start-re)
  7306. (org-beginning-of-dblock))
  7307. (org-update-dblock)))
  7308. (defun org-update-dblock ()
  7309. "Update the dynamic block at point
  7310. This means to empty the block, parse for parameters and then call
  7311. the correct writing function."
  7312. (save-window-excursion
  7313. (let* ((pos (point))
  7314. (line (org-current-line))
  7315. (params (org-prepare-dblock))
  7316. (name (plist-get params :name))
  7317. (cmd (intern (concat "org-dblock-write:" name))))
  7318. (message "Updating dynamic block `%s' at line %d..." name line)
  7319. (funcall cmd params)
  7320. (message "Updating dynamic block `%s' at line %d...done" name line)
  7321. (goto-char pos))))
  7322. (defun org-beginning-of-dblock ()
  7323. "Find the beginning of the dynamic block at point.
  7324. Error if there is no scuh block at point."
  7325. (let ((pos (point))
  7326. beg)
  7327. (end-of-line 1)
  7328. (if (and (re-search-backward org-dblock-start-re nil t)
  7329. (setq beg (match-beginning 0))
  7330. (re-search-forward org-dblock-end-re nil t)
  7331. (> (match-end 0) pos))
  7332. (goto-char beg)
  7333. (goto-char pos)
  7334. (error "Not in a dynamic block"))))
  7335. (defun org-update-all-dblocks ()
  7336. "Update all dynamic blocks in the buffer.
  7337. This function can be used in a hook."
  7338. (when (org-mode-p)
  7339. (org-map-dblocks 'org-update-dblock)))
  7340. ;;;; Completion
  7341. (defconst org-additional-option-like-keywords
  7342. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7343. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7344. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7345. (defun org-complete (&optional arg)
  7346. "Perform completion on word at point.
  7347. At the beginning of a headline, this completes TODO keywords as given in
  7348. `org-todo-keywords'.
  7349. If the current word is preceded by a backslash, completes the TeX symbols
  7350. that are supported for HTML support.
  7351. If the current word is preceded by \"#+\", completes special words for
  7352. setting file options.
  7353. In the line after \"#+STARTUP:, complete valid keywords.\"
  7354. At all other locations, this simply calls the value of
  7355. `org-completion-fallback-command'."
  7356. (interactive "P")
  7357. (org-without-partial-completion
  7358. (catch 'exit
  7359. (let* ((end (point))
  7360. (beg1 (save-excursion
  7361. (skip-chars-backward (org-re "[:alnum:]_@"))
  7362. (point)))
  7363. (beg (save-excursion
  7364. (skip-chars-backward "a-zA-Z0-9_:$")
  7365. (point)))
  7366. (confirm (lambda (x) (stringp (car x))))
  7367. (searchhead (equal (char-before beg) ?*))
  7368. (tag (and (equal (char-before beg1) ?:)
  7369. (equal (char-after (point-at-bol)) ?*)))
  7370. (prop (and (equal (char-before beg1) ?:)
  7371. (not (equal (char-after (point-at-bol)) ?*))))
  7372. (texp (equal (char-before beg) ?\\))
  7373. (link (equal (char-before beg) ?\[))
  7374. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7375. beg)
  7376. "#+"))
  7377. (startup (string-match "^#\\+STARTUP:.*"
  7378. (buffer-substring (point-at-bol) (point))))
  7379. (completion-ignore-case opt)
  7380. (type nil)
  7381. (tbl nil)
  7382. (table (cond
  7383. (opt
  7384. (setq type :opt)
  7385. (require 'org-exp)
  7386. (append
  7387. (mapcar
  7388. (lambda (x)
  7389. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7390. (cons (match-string 2 x) (match-string 1 x)))
  7391. (org-split-string (org-get-current-options) "\n"))
  7392. (mapcar 'list org-additional-option-like-keywords)))
  7393. (startup
  7394. (setq type :startup)
  7395. org-startup-options)
  7396. (link (append org-link-abbrev-alist-local
  7397. org-link-abbrev-alist))
  7398. (texp
  7399. (setq type :tex)
  7400. org-html-entities)
  7401. ((string-match "\\`\\*+[ \t]+\\'"
  7402. (buffer-substring (point-at-bol) beg))
  7403. (setq type :todo)
  7404. (mapcar 'list org-todo-keywords-1))
  7405. (searchhead
  7406. (setq type :searchhead)
  7407. (save-excursion
  7408. (goto-char (point-min))
  7409. (while (re-search-forward org-todo-line-regexp nil t)
  7410. (push (list
  7411. (org-make-org-heading-search-string
  7412. (match-string 3) t))
  7413. tbl)))
  7414. tbl)
  7415. (tag (setq type :tag beg beg1)
  7416. (or org-tag-alist (org-get-buffer-tags)))
  7417. (prop (setq type :prop beg beg1)
  7418. (mapcar 'list (org-buffer-property-keys nil t t)))
  7419. (t (progn
  7420. (call-interactively org-completion-fallback-command)
  7421. (throw 'exit nil)))))
  7422. (pattern (buffer-substring-no-properties beg end))
  7423. (completion (try-completion pattern table confirm)))
  7424. (cond ((eq completion t)
  7425. (if (not (assoc (upcase pattern) table))
  7426. (message "Already complete")
  7427. (if (and (equal type :opt)
  7428. (not (member (car (assoc (upcase pattern) table))
  7429. org-additional-option-like-keywords)))
  7430. (insert (substring (cdr (assoc (upcase pattern) table))
  7431. (length pattern)))
  7432. (if (memq type '(:tag :prop)) (insert ":")))))
  7433. ((null completion)
  7434. (message "Can't find completion for \"%s\"" pattern)
  7435. (ding))
  7436. ((not (string= pattern completion))
  7437. (delete-region beg end)
  7438. (if (string-match " +$" completion)
  7439. (setq completion (replace-match "" t t completion)))
  7440. (insert completion)
  7441. (if (get-buffer-window "*Completions*")
  7442. (delete-window (get-buffer-window "*Completions*")))
  7443. (if (assoc completion table)
  7444. (if (eq type :todo) (insert " ")
  7445. (if (memq type '(:tag :prop)) (insert ":"))))
  7446. (if (and (equal type :opt) (assoc completion table))
  7447. (message "%s" (substitute-command-keys
  7448. "Press \\[org-complete] again to insert example settings"))))
  7449. (t
  7450. (message "Making completion list...")
  7451. (let ((list (sort (all-completions pattern table confirm)
  7452. 'string<)))
  7453. (with-output-to-temp-buffer "*Completions*"
  7454. (condition-case nil
  7455. ;; Protection needed for XEmacs and emacs 21
  7456. (display-completion-list list pattern)
  7457. (error (display-completion-list list)))))
  7458. (message "Making completion list...%s" "done")))))))
  7459. ;;;; TODO, DEADLINE, Comments
  7460. (defun org-toggle-comment ()
  7461. "Change the COMMENT state of an entry."
  7462. (interactive)
  7463. (save-excursion
  7464. (org-back-to-heading)
  7465. (let (case-fold-search)
  7466. (if (looking-at (concat outline-regexp
  7467. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7468. (replace-match "" t t nil 1)
  7469. (if (looking-at outline-regexp)
  7470. (progn
  7471. (goto-char (match-end 0))
  7472. (insert org-comment-string " ")))))))
  7473. (defvar org-last-todo-state-is-todo nil
  7474. "This is non-nil when the last TODO state change led to a TODO state.
  7475. If the last change removed the TODO tag or switched to DONE, then
  7476. this is nil.")
  7477. (defvar org-setting-tags nil) ; dynamically skiped
  7478. (defun org-parse-local-options (string var)
  7479. "Parse STRING for startup setting relevant for variable VAR."
  7480. (let ((rtn (symbol-value var))
  7481. e opts)
  7482. (save-match-data
  7483. (if (or (not string) (not (string-match "\\S-" string)))
  7484. rtn
  7485. (setq opts (delq nil (mapcar (lambda (x)
  7486. (setq e (assoc x org-startup-options))
  7487. (if (eq (nth 1 e) var) e nil))
  7488. (org-split-string string "[ \t]+"))))
  7489. (if (not opts)
  7490. rtn
  7491. (setq rtn nil)
  7492. (while (setq e (pop opts))
  7493. (if (not (nth 3 e))
  7494. (setq rtn (nth 2 e))
  7495. (if (not (listp rtn)) (setq rtn nil))
  7496. (push (nth 2 e) rtn)))
  7497. rtn)))))
  7498. (defvar org-blocker-hook nil
  7499. "Hook for functions that are allowed to block a state change.
  7500. Each function gets as its single argument a property list, see
  7501. `org-trigger-hook' for more information about this list.
  7502. If any of the functions in this hook returns nil, the state change
  7503. is blocked.")
  7504. (defvar org-trigger-hook nil
  7505. "Hook for functions that are triggered by a state change.
  7506. Each function gets as its single argument a property list with at least
  7507. the following elements:
  7508. (:type type-of-change :position pos-at-entry-start
  7509. :from old-state :to new-state)
  7510. Depending on the type, more properties may be present.
  7511. This mechanism is currently implemented for:
  7512. TODO state changes
  7513. ------------------
  7514. :type todo-state-change
  7515. :from previous state (keyword as a string), or nil
  7516. :to new state (keyword as a string), or nil")
  7517. (defun org-todo (&optional arg)
  7518. "Change the TODO state of an item.
  7519. The state of an item is given by a keyword at the start of the heading,
  7520. like
  7521. *** TODO Write paper
  7522. *** DONE Call mom
  7523. The different keywords are specified in the variable `org-todo-keywords'.
  7524. By default the available states are \"TODO\" and \"DONE\".
  7525. So for this example: when the item starts with TODO, it is changed to DONE.
  7526. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7527. DONE are present, add TODO at the beginning of the heading.
  7528. With C-u prefix arg, use completion to determine the new state.
  7529. With numeric prefix arg, switch to that state.
  7530. For calling through lisp, arg is also interpreted in the following way:
  7531. 'none -> empty state
  7532. \"\"(empty string) -> switch to empty state
  7533. 'done -> switch to DONE
  7534. 'nextset -> switch to the next set of keywords
  7535. 'previousset -> switch to the previous set of keywords
  7536. \"WAITING\" -> switch to the specified keyword, but only if it
  7537. really is a member of `org-todo-keywords'."
  7538. (interactive "P")
  7539. (save-excursion
  7540. (catch 'exit
  7541. (org-back-to-heading)
  7542. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7543. (or (looking-at (concat " +" org-todo-regexp " *"))
  7544. (looking-at " *"))
  7545. (let* ((match-data (match-data))
  7546. (startpos (point-at-bol))
  7547. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7548. (org-log-done org-log-done)
  7549. (org-log-repeat org-log-repeat)
  7550. (org-todo-log-states org-todo-log-states)
  7551. (this (match-string 1))
  7552. (hl-pos (match-beginning 0))
  7553. (head (org-get-todo-sequence-head this))
  7554. (ass (assoc head org-todo-kwd-alist))
  7555. (interpret (nth 1 ass))
  7556. (done-word (nth 3 ass))
  7557. (final-done-word (nth 4 ass))
  7558. (last-state (or this ""))
  7559. (completion-ignore-case t)
  7560. (member (member this org-todo-keywords-1))
  7561. (tail (cdr member))
  7562. (state (cond
  7563. ((and org-todo-key-trigger
  7564. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7565. (and (not arg) org-use-fast-todo-selection
  7566. (not (eq org-use-fast-todo-selection 'prefix)))))
  7567. ;; Use fast selection
  7568. (org-fast-todo-selection))
  7569. ((and (equal arg '(4))
  7570. (or (not org-use-fast-todo-selection)
  7571. (not org-todo-key-trigger)))
  7572. ;; Read a state with completion
  7573. (completing-read "State: " (mapcar (lambda(x) (list x))
  7574. org-todo-keywords-1)
  7575. nil t))
  7576. ((eq arg 'right)
  7577. (if this
  7578. (if tail (car tail) nil)
  7579. (car org-todo-keywords-1)))
  7580. ((eq arg 'left)
  7581. (if (equal member org-todo-keywords-1)
  7582. nil
  7583. (if this
  7584. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7585. org-todo-keywords-1)
  7586. (org-last org-todo-keywords-1))))
  7587. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7588. (setq arg nil))) ; hack to fall back to cycling
  7589. (arg
  7590. ;; user or caller requests a specific state
  7591. (cond
  7592. ((equal arg "") nil)
  7593. ((eq arg 'none) nil)
  7594. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7595. ((eq arg 'nextset)
  7596. (or (car (cdr (member head org-todo-heads)))
  7597. (car org-todo-heads)))
  7598. ((eq arg 'previousset)
  7599. (let ((org-todo-heads (reverse org-todo-heads)))
  7600. (or (car (cdr (member head org-todo-heads)))
  7601. (car org-todo-heads))))
  7602. ((car (member arg org-todo-keywords-1)))
  7603. ((nth (1- (prefix-numeric-value arg))
  7604. org-todo-keywords-1))))
  7605. ((null member) (or head (car org-todo-keywords-1)))
  7606. ((equal this final-done-word) nil) ;; -> make empty
  7607. ((null tail) nil) ;; -> first entry
  7608. ((eq interpret 'sequence)
  7609. (car tail))
  7610. ((memq interpret '(type priority))
  7611. (if (eq this-command last-command)
  7612. (car tail)
  7613. (if (> (length tail) 0)
  7614. (or done-word (car org-done-keywords))
  7615. nil)))
  7616. (t nil)))
  7617. (next (if state (concat " " state " ") " "))
  7618. (change-plist (list :type 'todo-state-change :from this :to state
  7619. :position startpos))
  7620. dolog now-done-p)
  7621. (when org-blocker-hook
  7622. (unless (save-excursion
  7623. (save-match-data
  7624. (run-hook-with-args-until-failure
  7625. 'org-blocker-hook change-plist)))
  7626. (if (interactive-p)
  7627. (error "TODO state change from %s to %s blocked" this state)
  7628. ;; fail silently
  7629. (message "TODO state change from %s to %s blocked" this state)
  7630. (throw 'exit nil))))
  7631. (store-match-data match-data)
  7632. (replace-match next t t)
  7633. (unless (pos-visible-in-window-p hl-pos)
  7634. (message "TODO state changed to %s" (org-trim next)))
  7635. (unless head
  7636. (setq head (org-get-todo-sequence-head state)
  7637. ass (assoc head org-todo-kwd-alist)
  7638. interpret (nth 1 ass)
  7639. done-word (nth 3 ass)
  7640. final-done-word (nth 4 ass)))
  7641. (when (memq arg '(nextset previousset))
  7642. (message "Keyword-Set %d/%d: %s"
  7643. (- (length org-todo-sets) -1
  7644. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7645. (length org-todo-sets)
  7646. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7647. (setq org-last-todo-state-is-todo
  7648. (not (member state org-done-keywords)))
  7649. (setq now-done-p (and (member state org-done-keywords)
  7650. (not (member this org-done-keywords))))
  7651. (and logging (org-local-logging logging))
  7652. (when (and (or org-todo-log-states org-log-done)
  7653. (not (memq arg '(nextset previousset))))
  7654. ;; we need to look at recording a time and note
  7655. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7656. (nth 2 (assoc this org-todo-log-states))))
  7657. (when (and state
  7658. (member state org-not-done-keywords)
  7659. (not (member this org-not-done-keywords)))
  7660. ;; This is now a todo state and was not one before
  7661. ;; If there was a CLOSED time stamp, get rid of it.
  7662. (org-add-planning-info nil nil 'closed))
  7663. (when (and now-done-p org-log-done)
  7664. ;; It is now done, and it was not done before
  7665. (org-add-planning-info 'closed (org-current-time))
  7666. (if (and (not dolog) (eq 'note org-log-done))
  7667. (org-add-log-setup 'done state 'findpos 'note)))
  7668. (when (and state dolog)
  7669. ;; This is a non-nil state, and we need to log it
  7670. (org-add-log-setup 'state state 'findpos dolog)))
  7671. ;; Fixup tag positioning
  7672. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7673. (run-hooks 'org-after-todo-state-change-hook)
  7674. (if (and arg (not (member state org-done-keywords)))
  7675. (setq head (org-get-todo-sequence-head state)))
  7676. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7677. ;; Do we need to trigger a repeat?
  7678. (when now-done-p (org-auto-repeat-maybe state))
  7679. ;; Fixup cursor location if close to the keyword
  7680. (if (and (outline-on-heading-p)
  7681. (not (bolp))
  7682. (save-excursion (beginning-of-line 1)
  7683. (looking-at org-todo-line-regexp))
  7684. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7685. (progn
  7686. (goto-char (or (match-end 2) (match-end 1)))
  7687. (just-one-space)))
  7688. (when org-trigger-hook
  7689. (save-excursion
  7690. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7691. (defun org-local-logging (value)
  7692. "Get logging settings from a property VALUE."
  7693. (let* (words w a)
  7694. ;; directly set the variables, they are already local.
  7695. (setq org-log-done nil
  7696. org-log-repeat nil
  7697. org-todo-log-states nil)
  7698. (setq words (org-split-string value))
  7699. (while (setq w (pop words))
  7700. (cond
  7701. ((setq a (assoc w org-startup-options))
  7702. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7703. (set (nth 1 a) (nth 2 a))))
  7704. ((setq a (org-extract-log-state-settings w))
  7705. (and (member (car a) org-todo-keywords-1)
  7706. (push a org-todo-log-states)))))))
  7707. (defun org-get-todo-sequence-head (kwd)
  7708. "Return the head of the TODO sequence to which KWD belongs.
  7709. If KWD is not set, check if there is a text property remembering the
  7710. right sequence."
  7711. (let (p)
  7712. (cond
  7713. ((not kwd)
  7714. (or (get-text-property (point-at-bol) 'org-todo-head)
  7715. (progn
  7716. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7717. nil (point-at-eol)))
  7718. (get-text-property p 'org-todo-head))))
  7719. ((not (member kwd org-todo-keywords-1))
  7720. (car org-todo-keywords-1))
  7721. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7722. (defun org-fast-todo-selection ()
  7723. "Fast TODO keyword selection with single keys.
  7724. Returns the new TODO keyword, or nil if no state change should occur."
  7725. (let* ((fulltable org-todo-key-alist)
  7726. (done-keywords org-done-keywords) ;; needed for the faces.
  7727. (maxlen (apply 'max (mapcar
  7728. (lambda (x)
  7729. (if (stringp (car x)) (string-width (car x)) 0))
  7730. fulltable)))
  7731. (expert nil)
  7732. (fwidth (+ maxlen 3 1 3))
  7733. (ncol (/ (- (window-width) 4) fwidth))
  7734. tg cnt e c tbl
  7735. groups ingroup)
  7736. (save-window-excursion
  7737. (if expert
  7738. (set-buffer (get-buffer-create " *Org todo*"))
  7739. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7740. (erase-buffer)
  7741. (org-set-local 'org-done-keywords done-keywords)
  7742. (setq tbl fulltable cnt 0)
  7743. (while (setq e (pop tbl))
  7744. (cond
  7745. ((equal e '(:startgroup))
  7746. (push '() groups) (setq ingroup t)
  7747. (when (not (= cnt 0))
  7748. (setq cnt 0)
  7749. (insert "\n"))
  7750. (insert "{ "))
  7751. ((equal e '(:endgroup))
  7752. (setq ingroup nil cnt 0)
  7753. (insert "}\n"))
  7754. (t
  7755. (setq tg (car e) c (cdr e))
  7756. (if ingroup (push tg (car groups)))
  7757. (setq tg (org-add-props tg nil 'face
  7758. (org-get-todo-face tg)))
  7759. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7760. (insert "[" c "] " tg (make-string
  7761. (- fwidth 4 (length tg)) ?\ ))
  7762. (when (= (setq cnt (1+ cnt)) ncol)
  7763. (insert "\n")
  7764. (if ingroup (insert " "))
  7765. (setq cnt 0)))))
  7766. (insert "\n")
  7767. (goto-char (point-min))
  7768. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7769. (fit-window-to-buffer))
  7770. (message "[a-z..]:Set [SPC]:clear")
  7771. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7772. (cond
  7773. ((or (= c ?\C-g)
  7774. (and (= c ?q) (not (rassoc c fulltable))))
  7775. (setq quit-flag t))
  7776. ((= c ?\ ) nil)
  7777. ((setq e (rassoc c fulltable) tg (car e))
  7778. tg)
  7779. (t (setq quit-flag t))))))
  7780. (defun org-entry-is-todo-p ()
  7781. (member (org-get-todo-state) org-not-done-keywords))
  7782. (defun org-entry-is-done-p ()
  7783. (member (org-get-todo-state) org-done-keywords))
  7784. (defun org-get-todo-state ()
  7785. (save-excursion
  7786. (org-back-to-heading t)
  7787. (and (looking-at org-todo-line-regexp)
  7788. (match-end 2)
  7789. (match-string 2))))
  7790. (defun org-at-date-range-p (&optional inactive-ok)
  7791. "Is the cursor inside a date range?"
  7792. (interactive)
  7793. (save-excursion
  7794. (catch 'exit
  7795. (let ((pos (point)))
  7796. (skip-chars-backward "^[<\r\n")
  7797. (skip-chars-backward "<[")
  7798. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7799. (>= (match-end 0) pos)
  7800. (throw 'exit t))
  7801. (skip-chars-backward "^<[\r\n")
  7802. (skip-chars-backward "<[")
  7803. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7804. (>= (match-end 0) pos)
  7805. (throw 'exit t)))
  7806. nil)))
  7807. (defun org-get-repeat ()
  7808. "Check if tere is a deadline/schedule with repeater in this entry."
  7809. (save-match-data
  7810. (save-excursion
  7811. (org-back-to-heading t)
  7812. (if (re-search-forward
  7813. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7814. (match-string 1)))))
  7815. (defvar org-last-changed-timestamp)
  7816. (defvar org-log-post-message)
  7817. (defvar org-log-note-purpose)
  7818. (defvar org-log-note-how)
  7819. (defun org-auto-repeat-maybe (done-word)
  7820. "Check if the current headline contains a repeated deadline/schedule.
  7821. If yes, set TODO state back to what it was and change the base date
  7822. of repeating deadline/scheduled time stamps to new date.
  7823. This function is run automatically after each state change to a DONE state."
  7824. ;; last-state is dynamically scoped into this function
  7825. (let* ((repeat (org-get-repeat))
  7826. (aa (assoc last-state org-todo-kwd-alist))
  7827. (interpret (nth 1 aa))
  7828. (head (nth 2 aa))
  7829. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7830. (msg "Entry repeats: ")
  7831. (org-log-done nil)
  7832. (org-todo-log-states nil)
  7833. (nshiftmax 10) (nshift 0)
  7834. re type n what ts mb0 time)
  7835. (when repeat
  7836. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7837. (org-todo (if (eq interpret 'type) last-state head))
  7838. (when org-log-repeat
  7839. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7840. (memq 'org-add-log-note post-command-hook))
  7841. ;; OK, we are already setup for some record
  7842. (if (eq org-log-repeat 'note)
  7843. ;; make sure we take a note, not only a time stamp
  7844. (setq org-log-note-how 'note))
  7845. ;; Set up for taking a record
  7846. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7847. 'findpos org-log-repeat)))
  7848. (org-back-to-heading t)
  7849. (org-add-planning-info nil nil 'closed)
  7850. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7851. org-deadline-time-regexp "\\)\\|\\("
  7852. org-ts-regexp "\\)"))
  7853. (while (re-search-forward
  7854. re (save-excursion (outline-next-heading) (point)) t)
  7855. (setq type (if (match-end 1) org-scheduled-string
  7856. (if (match-end 3) org-deadline-string "Plain:"))
  7857. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7858. mb0 (match-beginning 0))
  7859. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7860. (setq n (string-to-number (match-string 2 ts))
  7861. what (match-string 3 ts))
  7862. (if (equal what "w") (setq n (* n 7) what "d"))
  7863. ;; Preparation, see if we need to modify the start date for the change
  7864. (when (match-end 1)
  7865. (setq time (save-match-data (org-time-string-to-time ts)))
  7866. (cond
  7867. ((equal (match-string 1 ts) ".")
  7868. ;; Shift starting date to today
  7869. (org-timestamp-change
  7870. (- (time-to-days (current-time)) (time-to-days time))
  7871. 'day))
  7872. ((equal (match-string 1 ts) "+")
  7873. (while (or (= nshift 0)
  7874. (<= (time-to-days time) (time-to-days (current-time))))
  7875. (when (= (incf nshift) nshiftmax)
  7876. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7877. (error "Abort")))
  7878. (org-timestamp-change n (cdr (assoc what whata)))
  7879. (org-at-timestamp-p t)
  7880. (setq ts (match-string 1))
  7881. (setq time (save-match-data (org-time-string-to-time ts))))
  7882. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7883. ;; rematch, so that we have everything in place for the real shift
  7884. (org-at-timestamp-p t)
  7885. (setq ts (match-string 1))
  7886. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7887. (org-timestamp-change n (cdr (assoc what whata)))
  7888. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7889. (setq org-log-post-message msg)
  7890. (message "%s" msg))))
  7891. (defun org-show-todo-tree (arg)
  7892. "Make a compact tree which shows all headlines marked with TODO.
  7893. The tree will show the lines where the regexp matches, and all higher
  7894. headlines above the match.
  7895. With a \\[universal-argument] prefix, also show the DONE entries.
  7896. With a numeric prefix N, construct a sparse tree for the Nth element
  7897. of `org-todo-keywords-1'."
  7898. (interactive "P")
  7899. (let ((case-fold-search nil)
  7900. (kwd-re
  7901. (cond ((null arg) org-not-done-regexp)
  7902. ((equal arg '(4))
  7903. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7904. (mapcar 'list org-todo-keywords-1))))
  7905. (concat "\\("
  7906. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7907. "\\)\\>")))
  7908. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7909. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7910. org-todo-keywords-1)))
  7911. (t (error "Invalid prefix argument: %s" arg)))))
  7912. (message "%d TODO entries found"
  7913. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7914. (defun org-deadline (&optional remove)
  7915. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7916. With argument REMOVE, remove any deadline from the item."
  7917. (interactive "P")
  7918. (if remove
  7919. (progn
  7920. (org-remove-timestamp-with-keyword org-deadline-string)
  7921. (message "Item no longer has a deadline."))
  7922. (org-add-planning-info 'deadline nil 'closed)))
  7923. (defun org-schedule (&optional remove)
  7924. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7925. With argument REMOVE, remove any scheduling date from the item."
  7926. (interactive "P")
  7927. (if remove
  7928. (progn
  7929. (org-remove-timestamp-with-keyword org-scheduled-string)
  7930. (message "Item is no longer scheduled."))
  7931. (org-add-planning-info 'scheduled nil 'closed)))
  7932. (defun org-remove-timestamp-with-keyword (keyword)
  7933. "Remove all time stamps with KEYWORD in the current entry."
  7934. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7935. beg)
  7936. (save-excursion
  7937. (org-back-to-heading t)
  7938. (setq beg (point))
  7939. (org-end-of-subtree t t)
  7940. (while (re-search-backward re beg t)
  7941. (replace-match "")
  7942. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7943. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7944. (defun org-add-planning-info (what &optional time &rest remove)
  7945. "Insert new timestamp with keyword in the line directly after the headline.
  7946. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7947. If non is given, the user is prompted for a date.
  7948. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7949. be removed."
  7950. (interactive)
  7951. (let (org-time-was-given org-end-time-was-given ts
  7952. end default-time default-input)
  7953. (when (and (not time) (memq what '(scheduled deadline)))
  7954. ;; Try to get a default date/time from existing timestamp
  7955. (save-excursion
  7956. (org-back-to-heading t)
  7957. (setq end (save-excursion (outline-next-heading) (point)))
  7958. (when (re-search-forward (if (eq what 'scheduled)
  7959. org-scheduled-time-regexp
  7960. org-deadline-time-regexp)
  7961. end t)
  7962. (setq ts (match-string 1)
  7963. default-time
  7964. (apply 'encode-time (org-parse-time-string ts))
  7965. default-input (and ts (org-get-compact-tod ts))))))
  7966. (when what
  7967. ;; If necessary, get the time from the user
  7968. (setq time (or time (org-read-date nil 'to-time nil nil
  7969. default-time default-input))))
  7970. (when (and org-insert-labeled-timestamps-at-point
  7971. (member what '(scheduled deadline)))
  7972. (insert
  7973. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7974. (org-insert-time-stamp time org-time-was-given
  7975. nil nil nil (list org-end-time-was-given))
  7976. (setq what nil))
  7977. (save-excursion
  7978. (save-restriction
  7979. (let (col list elt ts buffer-invisibility-spec)
  7980. (org-back-to-heading t)
  7981. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7982. (goto-char (match-end 1))
  7983. (setq col (current-column))
  7984. (goto-char (match-end 0))
  7985. (if (eobp) (insert "\n") (forward-char 1))
  7986. (if (and (not (looking-at outline-regexp))
  7987. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7988. "[^\r\n]*"))
  7989. (not (equal (match-string 1) org-clock-string)))
  7990. (narrow-to-region (match-beginning 0) (match-end 0))
  7991. (insert-before-markers "\n")
  7992. (backward-char 1)
  7993. (narrow-to-region (point) (point))
  7994. (indent-to-column col))
  7995. ;; Check if we have to remove something.
  7996. (setq list (cons what remove))
  7997. (while list
  7998. (setq elt (pop list))
  7999. (goto-char (point-min))
  8000. (when (or (and (eq elt 'scheduled)
  8001. (re-search-forward org-scheduled-time-regexp nil t))
  8002. (and (eq elt 'deadline)
  8003. (re-search-forward org-deadline-time-regexp nil t))
  8004. (and (eq elt 'closed)
  8005. (re-search-forward org-closed-time-regexp nil t)))
  8006. (replace-match "")
  8007. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8008. (if (looking-at " +") (replace-match ""))))
  8009. (goto-char (point-max))
  8010. (when what
  8011. (insert
  8012. (if (not (equal (char-before) ?\ )) " " "")
  8013. (cond ((eq what 'scheduled) org-scheduled-string)
  8014. ((eq what 'deadline) org-deadline-string)
  8015. ((eq what 'closed) org-closed-string))
  8016. " ")
  8017. (setq ts (org-insert-time-stamp
  8018. time
  8019. (or org-time-was-given
  8020. (and (eq what 'closed) org-log-done-with-time))
  8021. (eq what 'closed)
  8022. nil nil (list org-end-time-was-given)))
  8023. (end-of-line 1))
  8024. (goto-char (point-min))
  8025. (widen)
  8026. (if (looking-at "[ \t]+\r?\n")
  8027. (replace-match ""))
  8028. ts)))))
  8029. (defvar org-log-note-marker (make-marker))
  8030. (defvar org-log-note-purpose nil)
  8031. (defvar org-log-note-state nil)
  8032. (defvar org-log-note-how nil)
  8033. (defvar org-log-note-window-configuration nil)
  8034. (defvar org-log-note-return-to (make-marker))
  8035. (defvar org-log-post-message nil
  8036. "Message to be displayed after a log note has been stored.
  8037. The auto-repeater uses this.")
  8038. (defun org-add-note ()
  8039. "Add a note to the current entry.
  8040. This is done in the same way as adding a state change note."
  8041. (interactive)
  8042. (org-add-log-setup 'note nil t nil))
  8043. (defun org-add-log-setup (&optional purpose state findpos how)
  8044. "Set up the post command hook to take a note.
  8045. If this is about to TODO state change, the new state is expected in STATE.
  8046. When FINDPOS is non-nil, find the correct position for the note in
  8047. the current entry. If not, assume that it can be inserted at point."
  8048. (save-excursion
  8049. (when findpos
  8050. (org-back-to-heading t)
  8051. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8052. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8053. "[^\r\n]*\\)?"))
  8054. (goto-char (match-end 0))
  8055. (unless org-log-states-order-reversed
  8056. (and (= (char-after) ?\n) (forward-char 1))
  8057. (org-skip-over-state-notes)
  8058. (skip-chars-backward " \t\n\r")))
  8059. (move-marker org-log-note-marker (point))
  8060. (setq org-log-note-purpose purpose
  8061. org-log-note-state state
  8062. org-log-note-how how)
  8063. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8064. (defun org-skip-over-state-notes ()
  8065. "Skip past the list of State notes in an entry."
  8066. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8067. (while (looking-at "[ \t]*- State")
  8068. (condition-case nil
  8069. (org-next-item)
  8070. (error (org-end-of-item)))))
  8071. (defun org-add-log-note (&optional purpose)
  8072. "Pop up a window for taking a note, and add this note later at point."
  8073. (remove-hook 'post-command-hook 'org-add-log-note)
  8074. (setq org-log-note-window-configuration (current-window-configuration))
  8075. (delete-other-windows)
  8076. (move-marker org-log-note-return-to (point))
  8077. (switch-to-buffer (marker-buffer org-log-note-marker))
  8078. (goto-char org-log-note-marker)
  8079. (org-switch-to-buffer-other-window "*Org Note*")
  8080. (erase-buffer)
  8081. (if (memq org-log-note-how '(time state))
  8082. (org-store-log-note)
  8083. (let ((org-inhibit-startup t)) (org-mode))
  8084. (insert (format "# Insert note for %s.
  8085. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8086. (cond
  8087. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8088. ((eq org-log-note-purpose 'done) "closed todo item")
  8089. ((eq org-log-note-purpose 'state)
  8090. (format "state change to \"%s\"" org-log-note-state))
  8091. ((eq org-log-note-purpose 'note)
  8092. "this entry")
  8093. (t (error "This should not happen")))))
  8094. (org-set-local 'org-finish-function 'org-store-log-note)))
  8095. (defvar org-note-abort nil) ; dynamically scoped
  8096. (defun org-store-log-note ()
  8097. "Finish taking a log note, and insert it to where it belongs."
  8098. (let ((txt (buffer-string))
  8099. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8100. lines ind)
  8101. (kill-buffer (current-buffer))
  8102. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8103. (setq txt (replace-match "" t t txt)))
  8104. (if (string-match "\\s-+\\'" txt)
  8105. (setq txt (replace-match "" t t txt)))
  8106. (setq lines (org-split-string txt "\n"))
  8107. (when (and note (string-match "\\S-" note))
  8108. (setq note
  8109. (org-replace-escapes
  8110. note
  8111. (list (cons "%u" (user-login-name))
  8112. (cons "%U" user-full-name)
  8113. (cons "%t" (format-time-string
  8114. (org-time-stamp-format 'long 'inactive)
  8115. (current-time)))
  8116. (cons "%s" (if org-log-note-state
  8117. (concat "\"" org-log-note-state "\"")
  8118. "")))))
  8119. (if lines (setq note (concat note " \\\\")))
  8120. (push note lines))
  8121. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8122. (when lines
  8123. (save-excursion
  8124. (set-buffer (marker-buffer org-log-note-marker))
  8125. (save-excursion
  8126. (goto-char org-log-note-marker)
  8127. (move-marker org-log-note-marker nil)
  8128. (end-of-line 1)
  8129. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8130. (indent-relative nil)
  8131. (insert "- " (pop lines))
  8132. (org-indent-line-function)
  8133. (beginning-of-line 1)
  8134. (looking-at "[ \t]*")
  8135. (setq ind (concat (match-string 0) " "))
  8136. (end-of-line 1)
  8137. (while lines (insert "\n" ind (pop lines)))))))
  8138. (set-window-configuration org-log-note-window-configuration)
  8139. (with-current-buffer (marker-buffer org-log-note-return-to)
  8140. (goto-char org-log-note-return-to))
  8141. (move-marker org-log-note-return-to nil)
  8142. (and org-log-post-message (message "%s" org-log-post-message)))
  8143. (defun org-sparse-tree (&optional arg)
  8144. "Create a sparse tree, prompt for the details.
  8145. This command can create sparse trees. You first need to select the type
  8146. of match used to create the tree:
  8147. t Show entries with a specific TODO keyword.
  8148. T Show entries selected by a tags match.
  8149. p Enter a property name and its value (both with completion on existing
  8150. names/values) and show entries with that property.
  8151. r Show entries matching a regular expression
  8152. d Show deadlines due within `org-deadline-warning-days'."
  8153. (interactive "P")
  8154. (let (ans kwd value)
  8155. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8156. (setq ans (read-char-exclusive))
  8157. (cond
  8158. ((equal ans ?d)
  8159. (call-interactively 'org-check-deadlines))
  8160. ((equal ans ?b)
  8161. (call-interactively 'org-check-before-date))
  8162. ((equal ans ?t)
  8163. (org-show-todo-tree '(4)))
  8164. ((equal ans ?T)
  8165. (call-interactively 'org-tags-sparse-tree))
  8166. ((member ans '(?p ?P))
  8167. (setq kwd (completing-read "Property: "
  8168. (mapcar 'list (org-buffer-property-keys))))
  8169. (setq value (completing-read "Value: "
  8170. (mapcar 'list (org-property-values kwd))))
  8171. (unless (string-match "\\`{.*}\\'" value)
  8172. (setq value (concat "\"" value "\"")))
  8173. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8174. ((member ans '(?r ?R ?/))
  8175. (call-interactively 'org-occur))
  8176. (t (error "No such sparse tree command \"%c\"" ans)))))
  8177. (defvar org-occur-highlights nil
  8178. "List of overlays used for occur matches.")
  8179. (make-variable-buffer-local 'org-occur-highlights)
  8180. (defvar org-occur-parameters nil
  8181. "Parameters of the active org-occur calls.
  8182. This is a list, each call to org-occur pushes as cons cell,
  8183. containing the regular expression and the callback, onto the list.
  8184. The list can contain several entries if `org-occur' has been called
  8185. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8186. will only contain one set of parameters. When the highlights are
  8187. removed (for example with `C-c C-c', or with the next edit (depending
  8188. on `org-remove-highlights-with-change'), this variable is emptied
  8189. as well.")
  8190. (make-variable-buffer-local 'org-occur-parameters)
  8191. (defun org-occur (regexp &optional keep-previous callback)
  8192. "Make a compact tree which shows all matches of REGEXP.
  8193. The tree will show the lines where the regexp matches, and all higher
  8194. headlines above the match. It will also show the heading after the match,
  8195. to make sure editing the matching entry is easy.
  8196. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8197. call to `org-occur' will be kept, to allow stacking of calls to this
  8198. command.
  8199. If CALLBACK is non-nil, it is a function which is called to confirm
  8200. that the match should indeed be shown."
  8201. (interactive "sRegexp: \nP")
  8202. (unless keep-previous
  8203. (org-remove-occur-highlights nil nil t))
  8204. (push (cons regexp callback) org-occur-parameters)
  8205. (let ((cnt 0))
  8206. (save-excursion
  8207. (goto-char (point-min))
  8208. (if (or (not keep-previous) ; do not want to keep
  8209. (not org-occur-highlights)) ; no previous matches
  8210. ;; hide everything
  8211. (org-overview))
  8212. (while (re-search-forward regexp nil t)
  8213. (when (or (not callback)
  8214. (save-match-data (funcall callback)))
  8215. (setq cnt (1+ cnt))
  8216. (when org-highlight-sparse-tree-matches
  8217. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8218. (org-show-context 'occur-tree))))
  8219. (when org-remove-highlights-with-change
  8220. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8221. nil 'local))
  8222. (unless org-sparse-tree-open-archived-trees
  8223. (org-hide-archived-subtrees (point-min) (point-max)))
  8224. (run-hooks 'org-occur-hook)
  8225. (if (interactive-p)
  8226. (message "%d match(es) for regexp %s" cnt regexp))
  8227. cnt))
  8228. (defun org-show-context (&optional key)
  8229. "Make sure point and context and visible.
  8230. How much context is shown depends upon the variables
  8231. `org-show-hierarchy-above', `org-show-following-heading'. and
  8232. `org-show-siblings'."
  8233. (let ((heading-p (org-on-heading-p t))
  8234. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8235. (following-p (org-get-alist-option org-show-following-heading key))
  8236. (entry-p (org-get-alist-option org-show-entry-below key))
  8237. (siblings-p (org-get-alist-option org-show-siblings key)))
  8238. (catch 'exit
  8239. ;; Show heading or entry text
  8240. (if (and heading-p (not entry-p))
  8241. (org-flag-heading nil) ; only show the heading
  8242. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8243. (org-show-hidden-entry))) ; show entire entry
  8244. (when following-p
  8245. ;; Show next sibling, or heading below text
  8246. (save-excursion
  8247. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8248. (org-flag-heading nil))))
  8249. (when siblings-p (org-show-siblings))
  8250. (when hierarchy-p
  8251. ;; show all higher headings, possibly with siblings
  8252. (save-excursion
  8253. (while (and (condition-case nil
  8254. (progn (org-up-heading-all 1) t)
  8255. (error nil))
  8256. (not (bobp)))
  8257. (org-flag-heading nil)
  8258. (when siblings-p (org-show-siblings))))))))
  8259. (defun org-reveal (&optional siblings)
  8260. "Show current entry, hierarchy above it, and the following headline.
  8261. This can be used to show a consistent set of context around locations
  8262. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8263. not t for the search context.
  8264. With optional argument SIBLINGS, on each level of the hierarchy all
  8265. siblings are shown. This repairs the tree structure to what it would
  8266. look like when opened with hierarchical calls to `org-cycle'."
  8267. (interactive "P")
  8268. (let ((org-show-hierarchy-above t)
  8269. (org-show-following-heading t)
  8270. (org-show-siblings (if siblings t org-show-siblings)))
  8271. (org-show-context nil)))
  8272. (defun org-highlight-new-match (beg end)
  8273. "Highlight from BEG to END and mark the highlight is an occur headline."
  8274. (let ((ov (org-make-overlay beg end)))
  8275. (org-overlay-put ov 'face 'secondary-selection)
  8276. (push ov org-occur-highlights)))
  8277. (defun org-remove-occur-highlights (&optional beg end noremove)
  8278. "Remove the occur highlights from the buffer.
  8279. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8280. from the `before-change-functions' in the current buffer."
  8281. (interactive)
  8282. (unless org-inhibit-highlight-removal
  8283. (mapc 'org-delete-overlay org-occur-highlights)
  8284. (setq org-occur-highlights nil)
  8285. (setq org-occur-parameters nil)
  8286. (unless noremove
  8287. (remove-hook 'before-change-functions
  8288. 'org-remove-occur-highlights 'local))))
  8289. ;;;; Priorities
  8290. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8291. "Regular expression matching the priority indicator.")
  8292. (defvar org-remove-priority-next-time nil)
  8293. (defun org-priority-up ()
  8294. "Increase the priority of the current item."
  8295. (interactive)
  8296. (org-priority 'up))
  8297. (defun org-priority-down ()
  8298. "Decrease the priority of the current item."
  8299. (interactive)
  8300. (org-priority 'down))
  8301. (defun org-priority (&optional action)
  8302. "Change the priority of an item by ARG.
  8303. ACTION can be `set', `up', `down', or a character."
  8304. (interactive)
  8305. (setq action (or action 'set))
  8306. (let (current new news have remove)
  8307. (save-excursion
  8308. (org-back-to-heading)
  8309. (if (looking-at org-priority-regexp)
  8310. (setq current (string-to-char (match-string 2))
  8311. have t)
  8312. (setq current org-default-priority))
  8313. (cond
  8314. ((or (eq action 'set) (integerp action))
  8315. (if (integerp action)
  8316. (setq new action)
  8317. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  8318. (setq new (read-char-exclusive)))
  8319. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8320. (= (upcase org-lowest-priority) org-lowest-priority))
  8321. (setq new (upcase new)))
  8322. (cond ((equal new ?\ ) (setq remove t))
  8323. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8324. (error "Priority must be between `%c' and `%c'"
  8325. org-highest-priority org-lowest-priority))))
  8326. ((eq action 'up)
  8327. (if (and (not have) (eq last-command this-command))
  8328. (setq new org-lowest-priority)
  8329. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8330. org-default-priority (1- current)))))
  8331. ((eq action 'down)
  8332. (if (and (not have) (eq last-command this-command))
  8333. (setq new org-highest-priority)
  8334. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8335. org-default-priority (1+ current)))))
  8336. (t (error "Invalid action")))
  8337. (if (or (< (upcase new) org-highest-priority)
  8338. (> (upcase new) org-lowest-priority))
  8339. (setq remove t))
  8340. (setq news (format "%c" new))
  8341. (if have
  8342. (if remove
  8343. (replace-match "" t t nil 1)
  8344. (replace-match news t t nil 2))
  8345. (if remove
  8346. (error "No priority cookie found in line")
  8347. (looking-at org-todo-line-regexp)
  8348. (if (match-end 2)
  8349. (progn
  8350. (goto-char (match-end 2))
  8351. (insert " [#" news "]"))
  8352. (goto-char (match-beginning 3))
  8353. (insert "[#" news "] ")))))
  8354. (org-preserve-lc (org-set-tags nil 'align))
  8355. (if remove
  8356. (message "Priority removed")
  8357. (message "Priority of current item set to %s" news))))
  8358. (defun org-get-priority (s)
  8359. "Find priority cookie and return priority."
  8360. (save-match-data
  8361. (if (not (string-match org-priority-regexp s))
  8362. (* 1000 (- org-lowest-priority org-default-priority))
  8363. (* 1000 (- org-lowest-priority
  8364. (string-to-char (match-string 2 s)))))))
  8365. ;;;; Tags
  8366. (defun org-scan-tags (action matcher &optional todo-only)
  8367. "Scan headline tags with inheritance and produce output ACTION.
  8368. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8369. evaluated, testing if a given set of tags qualifies a headline for
  8370. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8371. are included in the output."
  8372. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8373. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8374. (org-re
  8375. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8376. (props (list 'face nil
  8377. 'done-face 'org-done
  8378. 'undone-face nil
  8379. 'mouse-face 'highlight
  8380. 'org-not-done-regexp org-not-done-regexp
  8381. 'org-todo-regexp org-todo-regexp
  8382. 'keymap org-agenda-keymap
  8383. 'help-echo
  8384. (format "mouse-2 or RET jump to org file %s"
  8385. (abbreviate-file-name
  8386. (or (buffer-file-name (buffer-base-buffer))
  8387. (buffer-name (buffer-base-buffer)))))))
  8388. (case-fold-search nil)
  8389. lspos
  8390. tags tags-list tags-alist (llast 0) rtn level category i txt
  8391. todo marker entry priority)
  8392. (save-excursion
  8393. (goto-char (point-min))
  8394. (when (eq action 'sparse-tree)
  8395. (org-overview)
  8396. (org-remove-occur-highlights))
  8397. (while (re-search-forward re nil t)
  8398. (catch :skip
  8399. (setq todo (if (match-end 1) (match-string 2))
  8400. tags (if (match-end 4) (match-string 4)))
  8401. (goto-char (setq lspos (1+ (match-beginning 0))))
  8402. (setq level (org-reduced-level (funcall outline-level))
  8403. category (org-get-category))
  8404. (setq i llast llast level)
  8405. ;; remove tag lists from same and sublevels
  8406. (while (>= i level)
  8407. (when (setq entry (assoc i tags-alist))
  8408. (setq tags-alist (delete entry tags-alist)))
  8409. (setq i (1- i)))
  8410. ;; add the next tags
  8411. (when tags
  8412. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8413. tags-alist
  8414. (cons (cons level tags) tags-alist)))
  8415. ;; compile tags for current headline
  8416. (setq tags-list
  8417. (if org-use-tag-inheritance
  8418. (apply 'append (mapcar 'cdr tags-alist))
  8419. tags))
  8420. (when (and tags org-use-tag-inheritance
  8421. (not (eq t org-use-tag-inheritance)))
  8422. ;; selective inheritance, remove uninherited ones
  8423. (setcdr (car tags-alist)
  8424. (org-remove-uniherited-tags (cdar tags-alist))))
  8425. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8426. (eval matcher)
  8427. (or (not org-agenda-skip-archived-trees)
  8428. (not (member org-archive-tag tags-list))))
  8429. (and (eq action 'agenda) (org-agenda-skip))
  8430. ;; list this headline
  8431. (if (eq action 'sparse-tree)
  8432. (progn
  8433. (and org-highlight-sparse-tree-matches
  8434. (org-get-heading) (match-end 0)
  8435. (org-highlight-new-match
  8436. (match-beginning 0) (match-beginning 1)))
  8437. (org-show-context 'tags-tree))
  8438. (setq txt (org-format-agenda-item
  8439. ""
  8440. (concat
  8441. (if org-tags-match-list-sublevels
  8442. (make-string (1- level) ?.) "")
  8443. (org-get-heading))
  8444. category tags-list)
  8445. priority (org-get-priority txt))
  8446. (goto-char lspos)
  8447. (setq marker (org-agenda-new-marker))
  8448. (org-add-props txt props
  8449. 'org-marker marker 'org-hd-marker marker 'org-category category
  8450. 'priority priority 'type "tagsmatch")
  8451. (push txt rtn))
  8452. ;; if we are to skip sublevels, jump to end of subtree
  8453. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8454. (when (and (eq action 'sparse-tree)
  8455. (not org-sparse-tree-open-archived-trees))
  8456. (org-hide-archived-subtrees (point-min) (point-max)))
  8457. (nreverse rtn)))
  8458. (defun org-remove-uniherited-tags (tags)
  8459. "Remove all tags that are not inherited from the list TAGS."
  8460. (cond
  8461. ((eq org-use-tag-inheritance t) tags)
  8462. ((not org-use-tag-inheritance) nil)
  8463. ((stringp org-use-tag-inheritance)
  8464. (delq nil (mapcar
  8465. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8466. tags)))
  8467. ((listp org-use-tag-inheritance)
  8468. (org-delete-all org-use-tag-inheritance tags))))
  8469. (defvar todo-only) ;; dynamically scoped
  8470. (defun org-tags-sparse-tree (&optional todo-only match)
  8471. "Create a sparse tree according to tags string MATCH.
  8472. MATCH can contain positive and negative selection of tags, like
  8473. \"+WORK+URGENT-WITHBOSS\".
  8474. If optional argument TODO_ONLY is non-nil, only select lines that are
  8475. also TODO lines."
  8476. (interactive "P")
  8477. (org-prepare-agenda-buffers (list (current-buffer)))
  8478. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8479. (defvar org-cached-props nil)
  8480. (defun org-cached-entry-get (pom property)
  8481. (if (or (eq t org-use-property-inheritance)
  8482. (and (stringp org-use-property-inheritance)
  8483. (string-match org-use-property-inheritance property))
  8484. (and (listp org-use-property-inheritance)
  8485. (member property org-use-property-inheritance)))
  8486. ;; Caching is not possible, check it directly
  8487. (org-entry-get pom property 'inherit)
  8488. ;; Get all properties, so that we can do complicated checks easily
  8489. (cdr (assoc property (or org-cached-props
  8490. (setq org-cached-props
  8491. (org-entry-properties pom)))))))
  8492. (defun org-global-tags-completion-table (&optional files)
  8493. "Return the list of all tags in all agenda buffer/files."
  8494. (save-excursion
  8495. (org-uniquify
  8496. (delq nil
  8497. (apply 'append
  8498. (mapcar
  8499. (lambda (file)
  8500. (set-buffer (find-file-noselect file))
  8501. (append (org-get-buffer-tags)
  8502. (mapcar (lambda (x) (if (stringp (car-safe x))
  8503. (list (car-safe x)) nil))
  8504. org-tag-alist)))
  8505. (if (and files (car files))
  8506. files
  8507. (org-agenda-files))))))))
  8508. (defun org-make-tags-matcher (match)
  8509. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8510. ;; todo-only is scoped dynamically into this function, and the function
  8511. ;; may change it it the matcher asksk for it.
  8512. (unless match
  8513. ;; Get a new match request, with completion
  8514. (let ((org-last-tags-completion-table
  8515. (org-global-tags-completion-table)))
  8516. (setq match (completing-read
  8517. "Match: " 'org-tags-completion-function nil nil nil
  8518. 'org-tags-history))))
  8519. ;; Parse the string and create a lisp form
  8520. (let ((match0 match)
  8521. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  8522. minus tag mm
  8523. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8524. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  8525. (if (string-match "/+" match)
  8526. ;; match contains also a todo-matching request
  8527. (progn
  8528. (setq tagsmatch (substring match 0 (match-beginning 0))
  8529. todomatch (substring match (match-end 0)))
  8530. (if (string-match "^!" todomatch)
  8531. (setq todo-only t todomatch (substring todomatch 1)))
  8532. (if (string-match "^\\s-*$" todomatch)
  8533. (setq todomatch nil)))
  8534. ;; only matching tags
  8535. (setq tagsmatch match todomatch nil))
  8536. ;; Make the tags matcher
  8537. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8538. (setq tagsmatcher t)
  8539. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8540. (while (setq term (pop orterms))
  8541. (while (and (equal (substring term -1) "\\") orterms)
  8542. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8543. (while (string-match re term)
  8544. (setq minus (and (match-end 1)
  8545. (equal (match-string 1 term) "-"))
  8546. tag (match-string 2 term)
  8547. re-p (equal (string-to-char tag) ?{)
  8548. level-p (match-end 3)
  8549. prop-p (match-end 4)
  8550. mm (cond
  8551. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8552. (level-p `(= level ,(string-to-number
  8553. (match-string 3 term))))
  8554. (prop-p
  8555. (setq pn (match-string 4 term)
  8556. pv (match-string 5 term)
  8557. cat-p (equal pn "CATEGORY")
  8558. re-p (equal (string-to-char pv) ?{)
  8559. pv (substring pv 1 -1))
  8560. (if (equal pn "CATEGORY")
  8561. (setq gv '(get-text-property (point) 'org-category))
  8562. (setq gv `(org-cached-entry-get nil ,pn)))
  8563. (if re-p
  8564. `(string-match ,pv (or ,gv ""))
  8565. `(equal ,pv (or ,gv ""))))
  8566. (t `(member ,(downcase tag) tags-list)))
  8567. mm (if minus (list 'not mm) mm)
  8568. term (substring term (match-end 0)))
  8569. (push mm tagsmatcher))
  8570. (push (if (> (length tagsmatcher) 1)
  8571. (cons 'and tagsmatcher)
  8572. (car tagsmatcher))
  8573. orlist)
  8574. (setq tagsmatcher nil))
  8575. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8576. (setq tagsmatcher
  8577. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8578. ;; Make the todo matcher
  8579. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8580. (setq todomatcher t)
  8581. (setq orterms (org-split-string todomatch "|") orlist nil)
  8582. (while (setq term (pop orterms))
  8583. (while (string-match re term)
  8584. (setq minus (and (match-end 1)
  8585. (equal (match-string 1 term) "-"))
  8586. kwd (match-string 2 term)
  8587. re-p (equal (string-to-char kwd) ?{)
  8588. term (substring term (match-end 0))
  8589. mm (if re-p
  8590. `(string-match ,(substring kwd 1 -1) todo)
  8591. (list 'equal 'todo kwd))
  8592. mm (if minus (list 'not mm) mm))
  8593. (push mm todomatcher))
  8594. (push (if (> (length todomatcher) 1)
  8595. (cons 'and todomatcher)
  8596. (car todomatcher))
  8597. orlist)
  8598. (setq todomatcher nil))
  8599. (setq todomatcher (if (> (length orlist) 1)
  8600. (cons 'or orlist) (car orlist))))
  8601. ;; Return the string and lisp forms of the matcher
  8602. (setq matcher (if todomatcher
  8603. (list 'and tagsmatcher todomatcher)
  8604. tagsmatcher))
  8605. (cons match0 matcher)))
  8606. (defun org-match-any-p (re list)
  8607. "Does re match any element of list?"
  8608. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8609. (delq nil list))
  8610. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8611. (defvar org-tags-overlay (org-make-overlay 1 1))
  8612. (org-detach-overlay org-tags-overlay)
  8613. (defun org-get-tags-at (&optional pos)
  8614. "Get a list of all headline tags applicable at POS.
  8615. POS defaults to point. If tags are inherited, the list contains
  8616. the targets in the same sequence as the headlines appear, i.e.
  8617. sthe tags of the current headline come last."
  8618. (interactive)
  8619. (let (tags ltags lastpos parent)
  8620. (save-excursion
  8621. (save-restriction
  8622. (widen)
  8623. (goto-char (or pos (point)))
  8624. (save-match-data
  8625. (condition-case nil
  8626. (progn
  8627. (org-back-to-heading t)
  8628. (while (not (equal lastpos (point)))
  8629. (setq lastpos (point))
  8630. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8631. (setq ltags (org-split-string
  8632. (org-match-string-no-properties 1) ":"))
  8633. (setq tags (append (org-remove-uniherited-tags ltags)
  8634. tags)))
  8635. (or org-use-tag-inheritance (error ""))
  8636. (org-up-heading-all 1)
  8637. (setq parent t)))
  8638. (error nil))))
  8639. tags)))
  8640. (defun org-align-tags-here (to-col)
  8641. ;; Assumes that this is a headline
  8642. (let ((pos (point)) (col (current-column)) tags)
  8643. (beginning-of-line 1)
  8644. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8645. (< pos (match-beginning 2)))
  8646. (progn
  8647. (setq tags (match-string 2))
  8648. (goto-char (match-beginning 1))
  8649. (insert " ")
  8650. (delete-region (point) (1+ (match-end 0)))
  8651. (backward-char 1)
  8652. (move-to-column
  8653. (max (1+ (current-column))
  8654. (1+ col)
  8655. (if (> to-col 0)
  8656. to-col
  8657. (- (abs to-col) (length tags))))
  8658. t)
  8659. (insert tags)
  8660. (move-to-column (min (current-column) col) t))
  8661. (goto-char pos))))
  8662. (defun org-set-tags (&optional arg just-align)
  8663. "Set the tags for the current headline.
  8664. With prefix ARG, realign all tags in headings in the current buffer."
  8665. (interactive "P")
  8666. (let* ((re (concat "^" outline-regexp))
  8667. (current (org-get-tags-string))
  8668. (col (current-column))
  8669. (org-setting-tags t)
  8670. table current-tags inherited-tags ; computed below when needed
  8671. tags p0 c0 c1 rpl)
  8672. (if arg
  8673. (save-excursion
  8674. (goto-char (point-min))
  8675. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8676. (while (re-search-forward re nil t)
  8677. (org-set-tags nil t)
  8678. (end-of-line 1)))
  8679. (message "All tags realigned to column %d" org-tags-column))
  8680. (if just-align
  8681. (setq tags current)
  8682. ;; Get a new set of tags from the user
  8683. (save-excursion
  8684. (setq table (or org-tag-alist (org-get-buffer-tags))
  8685. org-last-tags-completion-table table
  8686. current-tags (org-split-string current ":")
  8687. inherited-tags (nreverse
  8688. (nthcdr (length current-tags)
  8689. (nreverse (org-get-tags-at))))
  8690. tags
  8691. (if (or (eq t org-use-fast-tag-selection)
  8692. (and org-use-fast-tag-selection
  8693. (delq nil (mapcar 'cdr table))))
  8694. (org-fast-tag-selection
  8695. current-tags inherited-tags table
  8696. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8697. (let ((org-add-colon-after-tag-completion t))
  8698. (org-trim
  8699. (org-without-partial-completion
  8700. (completing-read "Tags: " 'org-tags-completion-function
  8701. nil nil current 'org-tags-history)))))))
  8702. (while (string-match "[-+&]+" tags)
  8703. ;; No boolean logic, just a list
  8704. (setq tags (replace-match ":" t t tags))))
  8705. (if (string-match "\\`[\t ]*\\'" tags)
  8706. (setq tags "")
  8707. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8708. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8709. ;; Insert new tags at the correct column
  8710. (beginning-of-line 1)
  8711. (cond
  8712. ((and (equal current "") (equal tags "")))
  8713. ((re-search-forward
  8714. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8715. (point-at-eol) t)
  8716. (if (equal tags "")
  8717. (setq rpl "")
  8718. (goto-char (match-beginning 0))
  8719. (setq c0 (current-column) p0 (point)
  8720. c1 (max (1+ c0) (if (> org-tags-column 0)
  8721. org-tags-column
  8722. (- (- org-tags-column) (length tags))))
  8723. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8724. (replace-match rpl t t)
  8725. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8726. tags)
  8727. (t (error "Tags alignment failed")))
  8728. (move-to-column col)
  8729. (unless just-align
  8730. (run-hooks 'org-after-tags-change-hook)))))
  8731. (defun org-change-tag-in-region (beg end tag off)
  8732. "Add or remove TAG for each entry in the region.
  8733. This works in the agenda, and also in an org-mode buffer."
  8734. (interactive
  8735. (list (region-beginning) (region-end)
  8736. (let ((org-last-tags-completion-table
  8737. (if (org-mode-p)
  8738. (org-get-buffer-tags)
  8739. (org-global-tags-completion-table))))
  8740. (completing-read
  8741. "Tag: " 'org-tags-completion-function nil nil nil
  8742. 'org-tags-history))
  8743. (progn
  8744. (message "[s]et or [r]emove? ")
  8745. (equal (read-char-exclusive) ?r))))
  8746. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8747. (let ((agendap (equal major-mode 'org-agenda-mode))
  8748. l1 l2 m buf pos newhead (cnt 0))
  8749. (goto-char end)
  8750. (setq l2 (1- (org-current-line)))
  8751. (goto-char beg)
  8752. (setq l1 (org-current-line))
  8753. (loop for l from l1 to l2 do
  8754. (goto-line l)
  8755. (setq m (get-text-property (point) 'org-hd-marker))
  8756. (when (or (and (org-mode-p) (org-on-heading-p))
  8757. (and agendap m))
  8758. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8759. pos (if agendap m (point)))
  8760. (with-current-buffer buf
  8761. (save-excursion
  8762. (save-restriction
  8763. (goto-char pos)
  8764. (setq cnt (1+ cnt))
  8765. (org-toggle-tag tag (if off 'off 'on))
  8766. (setq newhead (org-get-heading)))))
  8767. (and agendap (org-agenda-change-all-lines newhead m))))
  8768. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8769. (defun org-tags-completion-function (string predicate &optional flag)
  8770. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8771. (confirm (lambda (x) (stringp (car x)))))
  8772. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8773. (setq s1 (match-string 1 string)
  8774. s2 (match-string 2 string))
  8775. (setq s1 "" s2 string))
  8776. (cond
  8777. ((eq flag nil)
  8778. ;; try completion
  8779. (setq rtn (try-completion s2 ctable confirm))
  8780. (if (stringp rtn)
  8781. (setq rtn
  8782. (concat s1 s2 (substring rtn (length s2))
  8783. (if (and org-add-colon-after-tag-completion
  8784. (assoc rtn ctable))
  8785. ":" ""))))
  8786. rtn)
  8787. ((eq flag t)
  8788. ;; all-completions
  8789. (all-completions s2 ctable confirm)
  8790. )
  8791. ((eq flag 'lambda)
  8792. ;; exact match?
  8793. (assoc s2 ctable)))
  8794. ))
  8795. (defun org-fast-tag-insert (kwd tags face &optional end)
  8796. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8797. (insert (format "%-12s" (concat kwd ":"))
  8798. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8799. (or end "")))
  8800. (defun org-fast-tag-show-exit (flag)
  8801. (save-excursion
  8802. (goto-line 3)
  8803. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8804. (replace-match ""))
  8805. (when flag
  8806. (end-of-line 1)
  8807. (move-to-column (- (window-width) 19) t)
  8808. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8809. (defun org-set-current-tags-overlay (current prefix)
  8810. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8811. (if (featurep 'xemacs)
  8812. (org-overlay-display org-tags-overlay (concat prefix s)
  8813. 'secondary-selection)
  8814. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8815. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8816. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8817. "Fast tag selection with single keys.
  8818. CURRENT is the current list of tags in the headline, INHERITED is the
  8819. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8820. possibly with grouping information. TODO-TABLE is a similar table with
  8821. TODO keywords, should these have keys assigned to them.
  8822. If the keys are nil, a-z are automatically assigned.
  8823. Returns the new tags string, or nil to not change the current settings."
  8824. (let* ((fulltable (append table todo-table))
  8825. (maxlen (apply 'max (mapcar
  8826. (lambda (x)
  8827. (if (stringp (car x)) (string-width (car x)) 0))
  8828. fulltable)))
  8829. (buf (current-buffer))
  8830. (expert (eq org-fast-tag-selection-single-key 'expert))
  8831. (buffer-tags nil)
  8832. (fwidth (+ maxlen 3 1 3))
  8833. (ncol (/ (- (window-width) 4) fwidth))
  8834. (i-face 'org-done)
  8835. (c-face 'org-todo)
  8836. tg cnt e c char c1 c2 ntable tbl rtn
  8837. ov-start ov-end ov-prefix
  8838. (exit-after-next org-fast-tag-selection-single-key)
  8839. (done-keywords org-done-keywords)
  8840. groups ingroup)
  8841. (save-excursion
  8842. (beginning-of-line 1)
  8843. (if (looking-at
  8844. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8845. (setq ov-start (match-beginning 1)
  8846. ov-end (match-end 1)
  8847. ov-prefix "")
  8848. (setq ov-start (1- (point-at-eol))
  8849. ov-end (1+ ov-start))
  8850. (skip-chars-forward "^\n\r")
  8851. (setq ov-prefix
  8852. (concat
  8853. (buffer-substring (1- (point)) (point))
  8854. (if (> (current-column) org-tags-column)
  8855. " "
  8856. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8857. (org-move-overlay org-tags-overlay ov-start ov-end)
  8858. (save-window-excursion
  8859. (if expert
  8860. (set-buffer (get-buffer-create " *Org tags*"))
  8861. (delete-other-windows)
  8862. (split-window-vertically)
  8863. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8864. (erase-buffer)
  8865. (org-set-local 'org-done-keywords done-keywords)
  8866. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8867. (org-fast-tag-insert "Current" current c-face "\n\n")
  8868. (org-fast-tag-show-exit exit-after-next)
  8869. (org-set-current-tags-overlay current ov-prefix)
  8870. (setq tbl fulltable char ?a cnt 0)
  8871. (while (setq e (pop tbl))
  8872. (cond
  8873. ((equal e '(:startgroup))
  8874. (push '() groups) (setq ingroup t)
  8875. (when (not (= cnt 0))
  8876. (setq cnt 0)
  8877. (insert "\n"))
  8878. (insert "{ "))
  8879. ((equal e '(:endgroup))
  8880. (setq ingroup nil cnt 0)
  8881. (insert "}\n"))
  8882. (t
  8883. (setq tg (car e) c2 nil)
  8884. (if (cdr e)
  8885. (setq c (cdr e))
  8886. ;; automatically assign a character.
  8887. (setq c1 (string-to-char
  8888. (downcase (substring
  8889. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8890. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8891. (while (or (rassoc char ntable) (rassoc char table))
  8892. (setq char (1+ char)))
  8893. (setq c2 c1))
  8894. (setq c (or c2 char)))
  8895. (if ingroup (push tg (car groups)))
  8896. (setq tg (org-add-props tg nil 'face
  8897. (cond
  8898. ((not (assoc tg table))
  8899. (org-get-todo-face tg))
  8900. ((member tg current) c-face)
  8901. ((member tg inherited) i-face)
  8902. (t nil))))
  8903. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8904. (insert "[" c "] " tg (make-string
  8905. (- fwidth 4 (length tg)) ?\ ))
  8906. (push (cons tg c) ntable)
  8907. (when (= (setq cnt (1+ cnt)) ncol)
  8908. (insert "\n")
  8909. (if ingroup (insert " "))
  8910. (setq cnt 0)))))
  8911. (setq ntable (nreverse ntable))
  8912. (insert "\n")
  8913. (goto-char (point-min))
  8914. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8915. (fit-window-to-buffer))
  8916. (setq rtn
  8917. (catch 'exit
  8918. (while t
  8919. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8920. (if groups " [!] no groups" " [!]groups")
  8921. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8922. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8923. (cond
  8924. ((= c ?\r) (throw 'exit t))
  8925. ((= c ?!)
  8926. (setq groups (not groups))
  8927. (goto-char (point-min))
  8928. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8929. ((= c ?\C-c)
  8930. (if (not expert)
  8931. (org-fast-tag-show-exit
  8932. (setq exit-after-next (not exit-after-next)))
  8933. (setq expert nil)
  8934. (delete-other-windows)
  8935. (split-window-vertically)
  8936. (org-switch-to-buffer-other-window " *Org tags*")
  8937. (and (fboundp 'fit-window-to-buffer)
  8938. (fit-window-to-buffer))))
  8939. ((or (= c ?\C-g)
  8940. (and (= c ?q) (not (rassoc c ntable))))
  8941. (org-detach-overlay org-tags-overlay)
  8942. (setq quit-flag t))
  8943. ((= c ?\ )
  8944. (setq current nil)
  8945. (if exit-after-next (setq exit-after-next 'now)))
  8946. ((= c ?\t)
  8947. (condition-case nil
  8948. (setq tg (completing-read
  8949. "Tag: "
  8950. (or buffer-tags
  8951. (with-current-buffer buf
  8952. (org-get-buffer-tags)))))
  8953. (quit (setq tg "")))
  8954. (when (string-match "\\S-" tg)
  8955. (add-to-list 'buffer-tags (list tg))
  8956. (if (member tg current)
  8957. (setq current (delete tg current))
  8958. (push tg current)))
  8959. (if exit-after-next (setq exit-after-next 'now)))
  8960. ((setq e (rassoc c todo-table) tg (car e))
  8961. (with-current-buffer buf
  8962. (save-excursion (org-todo tg)))
  8963. (if exit-after-next (setq exit-after-next 'now)))
  8964. ((setq e (rassoc c ntable) tg (car e))
  8965. (if (member tg current)
  8966. (setq current (delete tg current))
  8967. (loop for g in groups do
  8968. (if (member tg g)
  8969. (mapc (lambda (x)
  8970. (setq current (delete x current)))
  8971. g)))
  8972. (push tg current))
  8973. (if exit-after-next (setq exit-after-next 'now))))
  8974. ;; Create a sorted list
  8975. (setq current
  8976. (sort current
  8977. (lambda (a b)
  8978. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8979. (if (eq exit-after-next 'now) (throw 'exit t))
  8980. (goto-char (point-min))
  8981. (beginning-of-line 2)
  8982. (delete-region (point) (point-at-eol))
  8983. (org-fast-tag-insert "Current" current c-face)
  8984. (org-set-current-tags-overlay current ov-prefix)
  8985. (while (re-search-forward
  8986. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8987. (setq tg (match-string 1))
  8988. (add-text-properties
  8989. (match-beginning 1) (match-end 1)
  8990. (list 'face
  8991. (cond
  8992. ((member tg current) c-face)
  8993. ((member tg inherited) i-face)
  8994. (t (get-text-property (match-beginning 1) 'face))))))
  8995. (goto-char (point-min)))))
  8996. (org-detach-overlay org-tags-overlay)
  8997. (if rtn
  8998. (mapconcat 'identity current ":")
  8999. nil))))
  9000. (defun org-get-tags-string ()
  9001. "Get the TAGS string in the current headline."
  9002. (unless (org-on-heading-p t)
  9003. (error "Not on a heading"))
  9004. (save-excursion
  9005. (beginning-of-line 1)
  9006. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9007. (org-match-string-no-properties 1)
  9008. "")))
  9009. (defun org-get-tags ()
  9010. "Get the list of tags specified in the current headline."
  9011. (org-split-string (org-get-tags-string) ":"))
  9012. (defun org-get-buffer-tags ()
  9013. "Get a table of all tags used in the buffer, for completion."
  9014. (let (tags)
  9015. (save-excursion
  9016. (goto-char (point-min))
  9017. (while (re-search-forward
  9018. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9019. (when (equal (char-after (point-at-bol 0)) ?*)
  9020. (mapc (lambda (x) (add-to-list 'tags x))
  9021. (org-split-string (org-match-string-no-properties 1) ":")))))
  9022. (mapcar 'list tags)))
  9023. ;;;; Properties
  9024. ;;; Setting and retrieving properties
  9025. (defconst org-special-properties
  9026. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9027. "TIMESTAMP" "TIMESTAMP_IA")
  9028. "The special properties valid in Org-mode.
  9029. These are properties that are not defined in the property drawer,
  9030. but in some other way.")
  9031. (defconst org-default-properties
  9032. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9033. "LOCATION" "LOGGING" "COLUMNS")
  9034. "Some properties that are used by Org-mode for various purposes.
  9035. Being in this list makes sure that they are offered for completion.")
  9036. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9037. "Regular expression matching the first line of a property drawer.")
  9038. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9039. "Regular expression matching the first line of a property drawer.")
  9040. (defun org-property-action ()
  9041. "Do an action on properties."
  9042. (interactive)
  9043. (let (c)
  9044. (org-at-property-p)
  9045. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9046. (setq c (read-char-exclusive))
  9047. (cond
  9048. ((equal c ?s)
  9049. (call-interactively 'org-set-property))
  9050. ((equal c ?d)
  9051. (call-interactively 'org-delete-property))
  9052. ((equal c ?D)
  9053. (call-interactively 'org-delete-property-globally))
  9054. ((equal c ?c)
  9055. (call-interactively 'org-compute-property-at-point))
  9056. (t (error "No such property action %c" c)))))
  9057. (defun org-at-property-p ()
  9058. "Is the cursor in a property line?"
  9059. ;; FIXME: Does not check if we are actually in the drawer.
  9060. ;; FIXME: also returns true on any drawers.....
  9061. ;; This is used by C-c C-c for property action.
  9062. (save-excursion
  9063. (beginning-of-line 1)
  9064. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9065. (defun org-get-property-block (&optional beg end force)
  9066. "Return the (beg . end) range of the body of the property drawer.
  9067. BEG and END can be beginning and end of subtree, if not given
  9068. they will be found.
  9069. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9070. (catch 'exit
  9071. (save-excursion
  9072. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9073. (end (or end (progn (outline-next-heading) (point)))))
  9074. (goto-char beg)
  9075. (if (re-search-forward org-property-start-re end t)
  9076. (setq beg (1+ (match-end 0)))
  9077. (if force
  9078. (save-excursion
  9079. (org-insert-property-drawer)
  9080. (setq end (progn (outline-next-heading) (point))))
  9081. (throw 'exit nil))
  9082. (goto-char beg)
  9083. (if (re-search-forward org-property-start-re end t)
  9084. (setq beg (1+ (match-end 0)))))
  9085. (if (re-search-forward org-property-end-re end t)
  9086. (setq end (match-beginning 0))
  9087. (or force (throw 'exit nil))
  9088. (goto-char beg)
  9089. (setq end beg)
  9090. (org-indent-line-function)
  9091. (insert ":END:\n"))
  9092. (cons beg end)))))
  9093. (defun org-entry-properties (&optional pom which)
  9094. "Get all properties of the entry at point-or-marker POM.
  9095. This includes the TODO keyword, the tags, time strings for deadline,
  9096. scheduled, and clocking, and any additional properties defined in the
  9097. entry. The return value is an alist, keys may occur multiple times
  9098. if the property key was used several times.
  9099. POM may also be nil, in which case the current entry is used.
  9100. If WHICH is nil or `all', get all properties. If WHICH is
  9101. `special' or `standard', only get that subclass."
  9102. (setq which (or which 'all))
  9103. (org-with-point-at pom
  9104. (let ((clockstr (substring org-clock-string 0 -1))
  9105. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9106. beg end range props sum-props key value string clocksum)
  9107. (save-excursion
  9108. (when (condition-case nil (org-back-to-heading t) (error nil))
  9109. (setq beg (point))
  9110. (setq sum-props (get-text-property (point) 'org-summaries))
  9111. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9112. (outline-next-heading)
  9113. (setq end (point))
  9114. (when (memq which '(all special))
  9115. ;; Get the special properties, like TODO and tags
  9116. (goto-char beg)
  9117. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9118. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9119. (when (looking-at org-priority-regexp)
  9120. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9121. (when (and (setq value (org-get-tags-string))
  9122. (string-match "\\S-" value))
  9123. (push (cons "TAGS" value) props))
  9124. (when (setq value (org-get-tags-at))
  9125. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9126. props))
  9127. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9128. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9129. string (if (equal key clockstr)
  9130. (org-no-properties
  9131. (org-trim
  9132. (buffer-substring
  9133. (match-beginning 3) (goto-char (point-at-eol)))))
  9134. (substring (org-match-string-no-properties 3) 1 -1)))
  9135. (unless key
  9136. (if (= (char-after (match-beginning 3)) ?\[)
  9137. (setq key "TIMESTAMP_IA")
  9138. (setq key "TIMESTAMP")))
  9139. (when (or (equal key clockstr) (not (assoc key props)))
  9140. (push (cons key string) props)))
  9141. )
  9142. (when (memq which '(all standard))
  9143. ;; Get the standard properties, like :PORP: ...
  9144. (setq range (org-get-property-block beg end))
  9145. (when range
  9146. (goto-char (car range))
  9147. (while (re-search-forward
  9148. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9149. (cdr range) t)
  9150. (setq key (org-match-string-no-properties 1)
  9151. value (org-trim (or (org-match-string-no-properties 2) "")))
  9152. (unless (member key excluded)
  9153. (push (cons key (or value "")) props)))))
  9154. (if clocksum
  9155. (push (cons "CLOCKSUM"
  9156. (org-column-number-to-string (/ (float clocksum) 60.)
  9157. 'add_times))
  9158. props))
  9159. (append sum-props (nreverse props)))))))
  9160. (defun org-entry-get (pom property &optional inherit)
  9161. "Get value of PROPERTY for entry at point-or-marker POM.
  9162. If INHERIT is non-nil and the entry does not have the property,
  9163. then also check higher levels of the hierarchy.
  9164. If INHERIT is the symbol `selective', use inheritance only if the setting
  9165. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9166. If the property is present but empty, the return value is the empty string.
  9167. If the property is not present at all, nil is returned."
  9168. (org-with-point-at pom
  9169. (if (and inherit (if (eq inherit 'selective)
  9170. (org-property-inherit-p property)
  9171. t))
  9172. (org-entry-get-with-inheritance property)
  9173. (if (member property org-special-properties)
  9174. ;; We need a special property. Use brute force, get all properties.
  9175. (cdr (assoc property (org-entry-properties nil 'special)))
  9176. (let ((range (org-get-property-block)))
  9177. (if (and range
  9178. (goto-char (car range))
  9179. (re-search-forward
  9180. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9181. (cdr range) t))
  9182. ;; Found the property, return it.
  9183. (if (match-end 1)
  9184. (org-match-string-no-properties 1)
  9185. "")))))))
  9186. (defun org-property-or-variable-value (var &optional inherit)
  9187. "Check if there is a property fixing the value of VAR.
  9188. If yes, return this value. If not, return the current value of the variable."
  9189. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9190. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9191. (read prop)
  9192. (symbol-value var))))
  9193. (defun org-entry-delete (pom property)
  9194. "Delete the property PROPERTY from entry at point-or-marker POM."
  9195. (org-with-point-at pom
  9196. (if (member property org-special-properties)
  9197. nil ; cannot delete these properties.
  9198. (let ((range (org-get-property-block)))
  9199. (if (and range
  9200. (goto-char (car range))
  9201. (re-search-forward
  9202. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9203. (cdr range) t))
  9204. (progn
  9205. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9206. t)
  9207. nil)))))
  9208. ;; Multi-values properties are properties that contain multiple values
  9209. ;; These values are assumed to be single words, separated by whitespace.
  9210. (defun org-entry-add-to-multivalued-property (pom property value)
  9211. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9212. (let* ((old (org-entry-get pom property))
  9213. (values (and old (org-split-string old "[ \t]"))))
  9214. (unless (member value values)
  9215. (setq values (cons value values))
  9216. (org-entry-put pom property
  9217. (mapconcat 'identity values " ")))))
  9218. (defun org-entry-remove-from-multivalued-property (pom property value)
  9219. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9220. (let* ((old (org-entry-get pom property))
  9221. (values (and old (org-split-string old "[ \t]"))))
  9222. (when (member value values)
  9223. (setq values (delete value values))
  9224. (org-entry-put pom property
  9225. (mapconcat 'identity values " ")))))
  9226. (defun org-entry-member-in-multivalued-property (pom property value)
  9227. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9228. (let* ((old (org-entry-get pom property))
  9229. (values (and old (org-split-string old "[ \t]"))))
  9230. (member value values)))
  9231. (defvar org-entry-property-inherited-from (make-marker))
  9232. (defun org-entry-get-with-inheritance (property)
  9233. "Get entry property, and search higher levels if not present."
  9234. (let (tmp)
  9235. (save-excursion
  9236. (save-restriction
  9237. (widen)
  9238. (catch 'ex
  9239. (while t
  9240. (when (setq tmp (org-entry-get nil property))
  9241. (org-back-to-heading t)
  9242. (move-marker org-entry-property-inherited-from (point))
  9243. (throw 'ex tmp))
  9244. (or (org-up-heading-safe) (throw 'ex nil)))))
  9245. (or tmp (cdr (assoc property org-local-properties))
  9246. (cdr (assoc property org-global-properties))))))
  9247. (defun org-entry-put (pom property value)
  9248. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9249. (org-with-point-at pom
  9250. (org-back-to-heading t)
  9251. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9252. range)
  9253. (cond
  9254. ((equal property "TODO")
  9255. (when (and (stringp value) (string-match "\\S-" value)
  9256. (not (member value org-todo-keywords-1)))
  9257. (error "\"%s\" is not a valid TODO state" value))
  9258. (if (or (not value)
  9259. (not (string-match "\\S-" value)))
  9260. (setq value 'none))
  9261. (org-todo value)
  9262. (org-set-tags nil 'align))
  9263. ((equal property "PRIORITY")
  9264. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9265. (string-to-char value) ?\ ))
  9266. (org-set-tags nil 'align))
  9267. ((equal property "SCHEDULED")
  9268. (if (re-search-forward org-scheduled-time-regexp end t)
  9269. (cond
  9270. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9271. ((eq value 'later) (org-timestamp-change 1 'day))
  9272. (t (call-interactively 'org-schedule)))
  9273. (call-interactively 'org-schedule)))
  9274. ((equal property "DEADLINE")
  9275. (if (re-search-forward org-deadline-time-regexp end t)
  9276. (cond
  9277. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9278. ((eq value 'later) (org-timestamp-change 1 'day))
  9279. (t (call-interactively 'org-deadline)))
  9280. (call-interactively 'org-deadline)))
  9281. ((member property org-special-properties)
  9282. (error "The %s property can not yet be set with `org-entry-put'"
  9283. property))
  9284. (t ; a non-special property
  9285. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9286. (setq range (org-get-property-block beg end 'force))
  9287. (goto-char (car range))
  9288. (if (re-search-forward
  9289. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9290. (progn
  9291. (delete-region (match-beginning 1) (match-end 1))
  9292. (goto-char (match-beginning 1)))
  9293. (goto-char (cdr range))
  9294. (insert "\n")
  9295. (backward-char 1)
  9296. (org-indent-line-function)
  9297. (insert ":" property ":"))
  9298. (and value (insert " " value))
  9299. (org-indent-line-function)))))))
  9300. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9301. "Get all property keys in the current buffer.
  9302. With INCLUDE-SPECIALS, also list the special properties that relect things
  9303. like tags and TODO state.
  9304. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9305. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9306. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9307. formats in the current buffer."
  9308. (let (rtn range cfmt cols s p)
  9309. (save-excursion
  9310. (save-restriction
  9311. (widen)
  9312. (goto-char (point-min))
  9313. (while (re-search-forward org-property-start-re nil t)
  9314. (setq range (org-get-property-block))
  9315. (goto-char (car range))
  9316. (while (re-search-forward
  9317. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9318. (cdr range) t)
  9319. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9320. (outline-next-heading))))
  9321. (when include-specials
  9322. (setq rtn (append org-special-properties rtn)))
  9323. (when include-defaults
  9324. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9325. (when include-columns
  9326. (save-excursion
  9327. (save-restriction
  9328. (widen)
  9329. (goto-char (point-min))
  9330. (while (re-search-forward
  9331. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9332. nil t)
  9333. (setq cfmt (match-string 2) s 0)
  9334. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9335. cfmt s)
  9336. (setq s (match-end 0)
  9337. p (match-string 1 cfmt))
  9338. (unless (or (equal p "ITEM")
  9339. (member p org-special-properties))
  9340. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9341. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9342. (defun org-property-values (key)
  9343. "Return a list of all values of property KEY."
  9344. (save-excursion
  9345. (save-restriction
  9346. (widen)
  9347. (goto-char (point-min))
  9348. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9349. values)
  9350. (while (re-search-forward re nil t)
  9351. (add-to-list 'values (org-trim (match-string 1))))
  9352. (delete "" values)))))
  9353. (defun org-insert-property-drawer ()
  9354. "Insert a property drawer into the current entry."
  9355. (interactive)
  9356. (org-back-to-heading t)
  9357. (looking-at outline-regexp)
  9358. (let ((indent (- (match-end 0)(match-beginning 0)))
  9359. (beg (point))
  9360. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9361. end hiddenp)
  9362. (outline-next-heading)
  9363. (setq end (point))
  9364. (goto-char beg)
  9365. (while (re-search-forward re end t))
  9366. (setq hiddenp (org-invisible-p))
  9367. (end-of-line 1)
  9368. (and (equal (char-after) ?\n) (forward-char 1))
  9369. (org-skip-over-state-notes)
  9370. (skip-chars-backward " \t\n\r")
  9371. (if (eq (char-before) ?*) (forward-char 1))
  9372. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9373. (beginning-of-line 0)
  9374. (indent-to-column indent)
  9375. (beginning-of-line 2)
  9376. (indent-to-column indent)
  9377. (beginning-of-line 0)
  9378. (if hiddenp
  9379. (save-excursion
  9380. (org-back-to-heading t)
  9381. (hide-entry))
  9382. (org-flag-drawer t))))
  9383. (defun org-set-property (property value)
  9384. "In the current entry, set PROPERTY to VALUE.
  9385. When called interactively, this will prompt for a property name, offering
  9386. completion on existing and default properties. And then it will prompt
  9387. for a value, offering competion either on allowed values (via an inherited
  9388. xxx_ALL property) or on existing values in other instances of this property
  9389. in the current file."
  9390. (interactive
  9391. (let* ((prop (completing-read
  9392. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  9393. (cur (org-entry-get nil prop))
  9394. (allowed (org-property-get-allowed-values nil prop 'table))
  9395. (existing (mapcar 'list (org-property-values prop)))
  9396. (val (if allowed
  9397. (completing-read "Value: " allowed nil 'req-match)
  9398. (completing-read
  9399. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9400. (concat "[" cur "]") "")
  9401. ": ")
  9402. existing nil nil "" nil cur))))
  9403. (list prop (if (equal val "") cur val))))
  9404. (unless (equal (org-entry-get nil property) value)
  9405. (org-entry-put nil property value)))
  9406. (defun org-delete-property (property)
  9407. "In the current entry, delete PROPERTY."
  9408. (interactive
  9409. (let* ((prop (completing-read
  9410. "Property: " (org-entry-properties nil 'standard))))
  9411. (list prop)))
  9412. (message "Property %s %s" property
  9413. (if (org-entry-delete nil property)
  9414. "deleted"
  9415. "was not present in the entry")))
  9416. (defun org-delete-property-globally (property)
  9417. "Remove PROPERTY globally, from all entries."
  9418. (interactive
  9419. (let* ((prop (completing-read
  9420. "Globally remove property: "
  9421. (mapcar 'list (org-buffer-property-keys)))))
  9422. (list prop)))
  9423. (save-excursion
  9424. (save-restriction
  9425. (widen)
  9426. (goto-char (point-min))
  9427. (let ((cnt 0))
  9428. (while (re-search-forward
  9429. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9430. nil t)
  9431. (setq cnt (1+ cnt))
  9432. (replace-match ""))
  9433. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9434. (defvar org-columns-current-fmt-compiled) ; defined below
  9435. (defun org-compute-property-at-point ()
  9436. "Compute the property at point.
  9437. This looks for an enclosing column format, extracts the operator and
  9438. then applies it to the proerty in the column format's scope."
  9439. (interactive)
  9440. (unless (org-at-property-p)
  9441. (error "Not at a property"))
  9442. (let ((prop (org-match-string-no-properties 2)))
  9443. (org-columns-get-format-and-top-level)
  9444. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9445. (error "No operator defined for property %s" prop))
  9446. (org-columns-compute prop)))
  9447. (defun org-property-get-allowed-values (pom property &optional table)
  9448. "Get allowed values for the property PROPERTY.
  9449. When TABLE is non-nil, return an alist that can directly be used for
  9450. completion."
  9451. (let (vals)
  9452. (cond
  9453. ((equal property "TODO")
  9454. (setq vals (org-with-point-at pom
  9455. (append org-todo-keywords-1 '("")))))
  9456. ((equal property "PRIORITY")
  9457. (let ((n org-lowest-priority))
  9458. (while (>= n org-highest-priority)
  9459. (push (char-to-string n) vals)
  9460. (setq n (1- n)))))
  9461. ((member property org-special-properties))
  9462. (t
  9463. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9464. (when (and vals (string-match "\\S-" vals))
  9465. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9466. (setq vals (mapcar (lambda (x)
  9467. (cond ((stringp x) x)
  9468. ((numberp x) (number-to-string x))
  9469. ((symbolp x) (symbol-name x))
  9470. (t "???")))
  9471. vals)))))
  9472. (if table (mapcar 'list vals) vals)))
  9473. (defun org-property-previous-allowed-value (&optional previous)
  9474. "Switch to the next allowed value for this property."
  9475. (interactive)
  9476. (org-property-next-allowed-value t))
  9477. (defun org-property-next-allowed-value (&optional previous)
  9478. "Switch to the next allowed value for this property."
  9479. (interactive)
  9480. (unless (org-at-property-p)
  9481. (error "Not at a property"))
  9482. (let* ((key (match-string 2))
  9483. (value (match-string 3))
  9484. (allowed (or (org-property-get-allowed-values (point) key)
  9485. (and (member value '("[ ]" "[-]" "[X]"))
  9486. '("[ ]" "[X]"))))
  9487. nval)
  9488. (unless allowed
  9489. (error "Allowed values for this property have not been defined"))
  9490. (if previous (setq allowed (reverse allowed)))
  9491. (if (member value allowed)
  9492. (setq nval (car (cdr (member value allowed)))))
  9493. (setq nval (or nval (car allowed)))
  9494. (if (equal nval value)
  9495. (error "Only one allowed value for this property"))
  9496. (org-at-property-p)
  9497. (replace-match (concat " :" key ": " nval) t t)
  9498. (org-indent-line-function)
  9499. (beginning-of-line 1)
  9500. (skip-chars-forward " \t")))
  9501. (defun org-find-entry-with-id (ident)
  9502. "Locate the entry that contains the ID property with exact value IDENT.
  9503. IDENT can be a string, a symbol or a number, this function will search for
  9504. the string representation of it.
  9505. Return the position where this entry starts, or nil if there is no such entry."
  9506. (let ((id (cond
  9507. ((stringp ident) ident)
  9508. ((symbol-name ident) (symbol-name ident))
  9509. ((numberp ident) (number-to-string ident))
  9510. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9511. (case-fold-search nil))
  9512. (save-excursion
  9513. (save-restriction
  9514. (widen)
  9515. (goto-char (point-min))
  9516. (when (re-search-forward
  9517. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9518. nil t)
  9519. (org-back-to-heading)
  9520. (point))))))
  9521. ;;; Column View
  9522. (defvar org-columns-overlays nil
  9523. "Holds the list of current column overlays.")
  9524. (defvar org-columns-current-fmt nil
  9525. "Local variable, holds the currently active column format.")
  9526. (defvar org-columns-current-fmt-compiled nil
  9527. "Local variable, holds the currently active column format.
  9528. This is the compiled version of the format.")
  9529. (defvar org-columns-current-widths nil
  9530. "Loval variable, holds the currently widths of fields.")
  9531. (defvar org-columns-current-maxwidths nil
  9532. "Loval variable, holds the currently active maximum column widths.")
  9533. (defvar org-columns-begin-marker (make-marker)
  9534. "Points to the position where last a column creation command was called.")
  9535. (defvar org-columns-top-level-marker (make-marker)
  9536. "Points to the position where current columns region starts.")
  9537. (defvar org-columns-map (make-sparse-keymap)
  9538. "The keymap valid in column display.")
  9539. (defun org-columns-content ()
  9540. "Switch to contents view while in columns view."
  9541. (interactive)
  9542. (org-overview)
  9543. (org-content))
  9544. (org-defkey org-columns-map "c" 'org-columns-content)
  9545. (org-defkey org-columns-map "o" 'org-overview)
  9546. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  9547. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  9548. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  9549. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  9550. (org-defkey org-columns-map "v" 'org-columns-show-value)
  9551. (org-defkey org-columns-map "q" 'org-columns-quit)
  9552. (org-defkey org-columns-map "r" 'org-columns-redo)
  9553. (org-defkey org-columns-map "g" 'org-columns-redo)
  9554. (org-defkey org-columns-map [left] 'backward-char)
  9555. (org-defkey org-columns-map "\M-b" 'backward-char)
  9556. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  9557. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  9558. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  9559. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  9560. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  9561. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  9562. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  9563. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  9564. (org-defkey org-columns-map "<" 'org-columns-narrow)
  9565. (org-defkey org-columns-map ">" 'org-columns-widen)
  9566. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  9567. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  9568. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  9569. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  9570. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  9571. '("Column"
  9572. ["Edit property" org-columns-edit-value t]
  9573. ["Next allowed value" org-columns-next-allowed-value t]
  9574. ["Previous allowed value" org-columns-previous-allowed-value t]
  9575. ["Show full value" org-columns-show-value t]
  9576. ["Edit allowed values" org-columns-edit-allowed t]
  9577. "--"
  9578. ["Edit column attributes" org-columns-edit-attributes t]
  9579. ["Increase column width" org-columns-widen t]
  9580. ["Decrease column width" org-columns-narrow t]
  9581. "--"
  9582. ["Move column right" org-columns-move-right t]
  9583. ["Move column left" org-columns-move-left t]
  9584. ["Add column" org-columns-new t]
  9585. ["Delete column" org-columns-delete t]
  9586. "--"
  9587. ["CONTENTS" org-columns-content t]
  9588. ["OVERVIEW" org-overview t]
  9589. ["Refresh columns display" org-columns-redo t]
  9590. "--"
  9591. ["Open link" org-columns-open-link t]
  9592. "--"
  9593. ["Quit" org-columns-quit t]))
  9594. (defun org-columns-new-overlay (beg end &optional string face)
  9595. "Create a new column overlay and add it to the list."
  9596. (let ((ov (org-make-overlay beg end)))
  9597. (org-overlay-put ov 'face (or face 'secondary-selection))
  9598. (org-overlay-display ov string face)
  9599. (push ov org-columns-overlays)
  9600. ov))
  9601. (defun org-columns-display-here (&optional props)
  9602. "Overlay the current line with column display."
  9603. (interactive)
  9604. (let* ((fmt org-columns-current-fmt-compiled)
  9605. (beg (point-at-bol))
  9606. (level-face (save-excursion
  9607. (beginning-of-line 1)
  9608. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  9609. (org-get-level-face 2))))
  9610. (color (list :foreground
  9611. (face-attribute (or level-face 'default) :foreground)))
  9612. props pom property ass width f string ov column val modval)
  9613. ;; Check if the entry is in another buffer.
  9614. (unless props
  9615. (if (eq major-mode 'org-agenda-mode)
  9616. (setq pom (or (get-text-property (point) 'org-hd-marker)
  9617. (get-text-property (point) 'org-marker))
  9618. props (if pom (org-entry-properties pom) nil))
  9619. (setq props (org-entry-properties nil))))
  9620. ;; Walk the format
  9621. (while (setq column (pop fmt))
  9622. (setq property (car column)
  9623. ass (if (equal property "ITEM")
  9624. (cons "ITEM"
  9625. (save-match-data
  9626. (org-no-properties
  9627. (org-remove-tabs
  9628. (buffer-substring-no-properties
  9629. (point-at-bol) (point-at-eol))))))
  9630. (assoc property props))
  9631. width (or (cdr (assoc property org-columns-current-maxwidths))
  9632. (nth 2 column)
  9633. (length property))
  9634. f (format "%%-%d.%ds | " width width)
  9635. val (or (cdr ass) "")
  9636. modval (if (equal property "ITEM")
  9637. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  9638. string (format f (or modval val)))
  9639. ;; Create the overlay
  9640. (org-unmodified
  9641. (setq ov (org-columns-new-overlay
  9642. beg (setq beg (1+ beg)) string
  9643. (list color 'org-column)))
  9644. (org-overlay-put ov 'keymap org-columns-map)
  9645. (org-overlay-put ov 'org-columns-key property)
  9646. (org-overlay-put ov 'org-columns-value (cdr ass))
  9647. (org-overlay-put ov 'org-columns-value-modified modval)
  9648. (org-overlay-put ov 'org-columns-pom pom)
  9649. (org-overlay-put ov 'org-columns-format f))
  9650. (if (or (not (char-after beg))
  9651. (equal (char-after beg) ?\n))
  9652. (let ((inhibit-read-only t))
  9653. (save-excursion
  9654. (goto-char beg)
  9655. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  9656. ;; Make the rest of the line disappear.
  9657. (org-unmodified
  9658. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  9659. (org-overlay-put ov 'invisible t)
  9660. (org-overlay-put ov 'keymap org-columns-map)
  9661. (org-overlay-put ov 'intangible t)
  9662. (push ov org-columns-overlays)
  9663. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  9664. (org-overlay-put ov 'keymap org-columns-map)
  9665. (push ov org-columns-overlays)
  9666. (let ((inhibit-read-only t))
  9667. (put-text-property (max (point-min) (1- (point-at-bol)))
  9668. (min (point-max) (1+ (point-at-eol)))
  9669. 'read-only "Type `e' to edit property")))))
  9670. (defvar org-columns-full-header-line-format nil
  9671. "Fthe full header line format, will be shifted by horizontal scrolling." )
  9672. (defvar org-previous-header-line-format nil
  9673. "The header line format before column view was turned on.")
  9674. (defvar org-columns-inhibit-recalculation nil
  9675. "Inhibit recomputing of columns on column view startup.")
  9676. (defvar header-line-format)
  9677. (defvar org-columns-previous-hscroll 0)
  9678. (defun org-columns-display-here-title ()
  9679. "Overlay the newline before the current line with the table title."
  9680. (interactive)
  9681. (let ((fmt org-columns-current-fmt-compiled)
  9682. string (title "")
  9683. property width f column str widths)
  9684. (while (setq column (pop fmt))
  9685. (setq property (car column)
  9686. str (or (nth 1 column) property)
  9687. width (or (cdr (assoc property org-columns-current-maxwidths))
  9688. (nth 2 column)
  9689. (length str))
  9690. widths (push width widths)
  9691. f (format "%%-%d.%ds | " width width)
  9692. string (format f str)
  9693. title (concat title string)))
  9694. (setq title (concat
  9695. (org-add-props " " nil 'display '(space :align-to 0))
  9696. (org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  9697. (org-set-local 'org-previous-header-line-format header-line-format)
  9698. (org-set-local 'org-columns-current-widths (nreverse widths))
  9699. (setq org-columns-full-header-line-format title)
  9700. (setq org-columns-previous-hscroll -1)
  9701. ; (org-columns-hscoll-title)
  9702. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  9703. (defun org-columns-hscoll-title ()
  9704. "Set the header-line-format so that it scrolls along with the table."
  9705. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  9706. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  9707. (setq header-line-format
  9708. (concat (substring org-columns-full-header-line-format 0 1)
  9709. (substring org-columns-full-header-line-format
  9710. (1+ (window-hscroll))))
  9711. org-columns-previous-hscroll (window-hscroll))
  9712. (force-mode-line-update)))
  9713. (defun org-columns-remove-overlays ()
  9714. "Remove all currently active column overlays."
  9715. (interactive)
  9716. (when (marker-buffer org-columns-begin-marker)
  9717. (with-current-buffer (marker-buffer org-columns-begin-marker)
  9718. (when (local-variable-p 'org-previous-header-line-format)
  9719. (setq header-line-format org-previous-header-line-format)
  9720. (kill-local-variable 'org-previous-header-line-format)
  9721. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  9722. (move-marker org-columns-begin-marker nil)
  9723. (move-marker org-columns-top-level-marker nil)
  9724. (org-unmodified
  9725. (mapc 'org-delete-overlay org-columns-overlays)
  9726. (setq org-columns-overlays nil)
  9727. (let ((inhibit-read-only t))
  9728. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  9729. (defun org-columns-cleanup-item (item fmt)
  9730. "Remove from ITEM what is a column in the format FMT."
  9731. (if (not org-complex-heading-regexp)
  9732. item
  9733. (when (string-match org-complex-heading-regexp item)
  9734. (concat
  9735. (org-add-props (concat (match-string 1 item) " ") nil
  9736. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  9737. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  9738. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  9739. " " (match-string 4 item)
  9740. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  9741. (defun org-columns-show-value ()
  9742. "Show the full value of the property."
  9743. (interactive)
  9744. (let ((value (get-char-property (point) 'org-columns-value)))
  9745. (message "Value is: %s" (or value ""))))
  9746. (defun org-columns-quit ()
  9747. "Remove the column overlays and in this way exit column editing."
  9748. (interactive)
  9749. (org-unmodified
  9750. (org-columns-remove-overlays)
  9751. (let ((inhibit-read-only t))
  9752. (remove-text-properties (point-min) (point-max) '(read-only t))))
  9753. (when (eq major-mode 'org-agenda-mode)
  9754. (message
  9755. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  9756. (defun org-columns-check-computed ()
  9757. "Check if this column value is computed.
  9758. If yes, throw an error indicating that changing it does not make sense."
  9759. (let ((val (get-char-property (point) 'org-columns-value)))
  9760. (when (and (stringp val)
  9761. (get-char-property 0 'org-computed val))
  9762. (error "This value is computed from the entry's children"))))
  9763. (defun org-columns-todo (&optional arg)
  9764. "Change the TODO state during column view."
  9765. (interactive "P")
  9766. (org-columns-edit-value "TODO"))
  9767. (defun org-columns-set-tags-or-toggle (&optional arg)
  9768. "Toggle checkbox at point, or set tags for current headline."
  9769. (interactive "P")
  9770. (if (string-match "\\`\\[[ xX-]\\]\\'"
  9771. (get-char-property (point) 'org-columns-value))
  9772. (org-columns-next-allowed-value)
  9773. (org-columns-edit-value "TAGS")))
  9774. (defun org-columns-edit-value (&optional key)
  9775. "Edit the value of the property at point in column view.
  9776. Where possible, use the standard interface for changing this line."
  9777. (interactive)
  9778. (org-columns-check-computed)
  9779. (let* ((external-key key)
  9780. (col (current-column))
  9781. (key (or key (get-char-property (point) 'org-columns-key)))
  9782. (value (get-char-property (point) 'org-columns-value))
  9783. (bol (point-at-bol)) (eol (point-at-eol))
  9784. (pom (or (get-text-property bol 'org-hd-marker)
  9785. (point))) ; keep despite of compiler waring
  9786. (line-overlays
  9787. (delq nil (mapcar (lambda (x)
  9788. (and (eq (overlay-buffer x) (current-buffer))
  9789. (>= (overlay-start x) bol)
  9790. (<= (overlay-start x) eol)
  9791. x))
  9792. org-columns-overlays)))
  9793. nval eval allowed)
  9794. (cond
  9795. ((equal key "CLOCKSUM")
  9796. (error "This special column cannot be edited"))
  9797. ((equal key "ITEM")
  9798. (setq eval '(org-with-point-at pom
  9799. (org-edit-headline))))
  9800. ((equal key "TODO")
  9801. (setq eval '(org-with-point-at pom
  9802. (let ((current-prefix-arg
  9803. (if external-key current-prefix-arg '(4))))
  9804. (call-interactively 'org-todo)))))
  9805. ((equal key "PRIORITY")
  9806. (setq eval '(org-with-point-at pom
  9807. (call-interactively 'org-priority))))
  9808. ((equal key "TAGS")
  9809. (setq eval '(org-with-point-at pom
  9810. (let ((org-fast-tag-selection-single-key
  9811. (if (eq org-fast-tag-selection-single-key 'expert)
  9812. t org-fast-tag-selection-single-key)))
  9813. (call-interactively 'org-set-tags)))))
  9814. ((equal key "DEADLINE")
  9815. (setq eval '(org-with-point-at pom
  9816. (call-interactively 'org-deadline))))
  9817. ((equal key "SCHEDULED")
  9818. (setq eval '(org-with-point-at pom
  9819. (call-interactively 'org-schedule))))
  9820. (t
  9821. (setq allowed (org-property-get-allowed-values pom key 'table))
  9822. (if allowed
  9823. (setq nval (completing-read "Value: " allowed nil t))
  9824. (setq nval (read-string "Edit: " value)))
  9825. (setq nval (org-trim nval))
  9826. (when (not (equal nval value))
  9827. (setq eval '(org-entry-put pom key nval)))))
  9828. (when eval
  9829. (let ((inhibit-read-only t))
  9830. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  9831. (unwind-protect
  9832. (progn
  9833. (setq org-columns-overlays
  9834. (org-delete-all line-overlays org-columns-overlays))
  9835. (mapc 'org-delete-overlay line-overlays)
  9836. (org-columns-eval eval))
  9837. (org-columns-display-here))))
  9838. (move-to-column col)
  9839. (if (and (org-mode-p)
  9840. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9841. (org-columns-update key))))
  9842. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  9843. "Edit the current headline, the part without TODO keyword, TAGS."
  9844. (org-back-to-heading)
  9845. (when (looking-at org-todo-line-regexp)
  9846. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  9847. (txt (match-string 3))
  9848. (post "")
  9849. txt2)
  9850. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  9851. (setq post (match-string 0 txt)
  9852. txt (substring txt 0 (match-beginning 0))))
  9853. (setq txt2 (read-string "Edit: " txt))
  9854. (when (not (equal txt txt2))
  9855. (beginning-of-line 1)
  9856. (insert pre txt2 post)
  9857. (delete-region (point) (point-at-eol))
  9858. (org-set-tags nil t)))))
  9859. (defun org-columns-edit-allowed ()
  9860. "Edit the list of allowed values for the current property."
  9861. (interactive)
  9862. (let* ((key (get-char-property (point) 'org-columns-key))
  9863. (key1 (concat key "_ALL"))
  9864. (allowed (org-entry-get (point) key1 t))
  9865. nval)
  9866. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  9867. (setq nval (read-string "Allowed: " allowed))
  9868. (org-entry-put
  9869. (cond ((marker-position org-entry-property-inherited-from)
  9870. org-entry-property-inherited-from)
  9871. ((marker-position org-columns-top-level-marker)
  9872. org-columns-top-level-marker))
  9873. key1 nval)))
  9874. (defun org-columns-eval (form)
  9875. (let (hidep)
  9876. (save-excursion
  9877. (beginning-of-line 1)
  9878. ;; `next-line' is needed here, because it skips invisible line.
  9879. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  9880. (setq hidep (org-on-heading-p 1)))
  9881. (eval form)
  9882. (and hidep (hide-entry))))
  9883. (defun org-columns-previous-allowed-value ()
  9884. "Switch to the previous allowed value for this column."
  9885. (interactive)
  9886. (org-columns-next-allowed-value t))
  9887. (defun org-columns-next-allowed-value (&optional previous)
  9888. "Switch to the next allowed value for this column."
  9889. (interactive)
  9890. (org-columns-check-computed)
  9891. (let* ((col (current-column))
  9892. (key (get-char-property (point) 'org-columns-key))
  9893. (value (get-char-property (point) 'org-columns-value))
  9894. (bol (point-at-bol)) (eol (point-at-eol))
  9895. (pom (or (get-text-property bol 'org-hd-marker)
  9896. (point))) ; keep despite of compiler waring
  9897. (line-overlays
  9898. (delq nil (mapcar (lambda (x)
  9899. (and (eq (overlay-buffer x) (current-buffer))
  9900. (>= (overlay-start x) bol)
  9901. (<= (overlay-start x) eol)
  9902. x))
  9903. org-columns-overlays)))
  9904. (allowed (or (org-property-get-allowed-values pom key)
  9905. (and (memq
  9906. (nth 4 (assoc key org-columns-current-fmt-compiled))
  9907. '(checkbox checkbox-n-of-m checkbox-percent))
  9908. '("[ ]" "[X]"))))
  9909. nval)
  9910. (when (equal key "ITEM")
  9911. (error "Cannot edit item headline from here"))
  9912. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  9913. (error "Allowed values for this property have not been defined"))
  9914. (if (member key '("SCHEDULED" "DEADLINE"))
  9915. (setq nval (if previous 'earlier 'later))
  9916. (if previous (setq allowed (reverse allowed)))
  9917. (if (member value allowed)
  9918. (setq nval (car (cdr (member value allowed)))))
  9919. (setq nval (or nval (car allowed)))
  9920. (if (equal nval value)
  9921. (error "Only one allowed value for this property")))
  9922. (let ((inhibit-read-only t))
  9923. (remove-text-properties (1- bol) eol '(read-only t))
  9924. (unwind-protect
  9925. (progn
  9926. (setq org-columns-overlays
  9927. (org-delete-all line-overlays org-columns-overlays))
  9928. (mapc 'org-delete-overlay line-overlays)
  9929. (org-columns-eval '(org-entry-put pom key nval)))
  9930. (org-columns-display-here)))
  9931. (move-to-column col)
  9932. (if (and (org-mode-p)
  9933. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9934. (org-columns-update key))))
  9935. (defun org-verify-version (task)
  9936. (cond
  9937. ((eq task 'columns)
  9938. (if (or (featurep 'xemacs)
  9939. (< emacs-major-version 22))
  9940. (error "Emacs 22 is required for the columns feature")))))
  9941. (defun org-columns-open-link (&optional arg)
  9942. (interactive "P")
  9943. (let ((value (get-char-property (point) 'org-columns-value)))
  9944. (org-open-link-from-string value arg)))
  9945. (defun org-open-link-from-string (s &optional arg)
  9946. "Open a link in the string S, as if it was in Org-mode."
  9947. (interactive)
  9948. (with-temp-buffer
  9949. (let ((org-inhibit-startup t))
  9950. (org-mode)
  9951. (insert s)
  9952. (goto-char (point-min))
  9953. (org-open-at-point arg))))
  9954. (defun org-columns-get-format-and-top-level ()
  9955. (let (fmt)
  9956. (when (condition-case nil (org-back-to-heading) (error nil))
  9957. (move-marker org-entry-property-inherited-from nil)
  9958. (setq fmt (org-entry-get nil "COLUMNS" t)))
  9959. (setq fmt (or fmt org-columns-default-format))
  9960. (org-set-local 'org-columns-current-fmt fmt)
  9961. (org-columns-compile-format fmt)
  9962. (if (marker-position org-entry-property-inherited-from)
  9963. (move-marker org-columns-top-level-marker
  9964. org-entry-property-inherited-from)
  9965. (move-marker org-columns-top-level-marker (point)))
  9966. fmt))
  9967. (defun org-columns ()
  9968. "Turn on column view on an org-mode file."
  9969. (interactive)
  9970. (org-verify-version 'columns)
  9971. (org-columns-remove-overlays)
  9972. (move-marker org-columns-begin-marker (point))
  9973. (let (beg end fmt cache maxwidths)
  9974. (setq fmt (org-columns-get-format-and-top-level))
  9975. (save-excursion
  9976. (goto-char org-columns-top-level-marker)
  9977. (setq beg (point))
  9978. (unless org-columns-inhibit-recalculation
  9979. (org-columns-compute-all))
  9980. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  9981. (point-max)))
  9982. ;; Get and cache the properties
  9983. (goto-char beg)
  9984. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  9985. (save-excursion
  9986. (save-restriction
  9987. (narrow-to-region beg end)
  9988. (org-clock-sum))))
  9989. (while (re-search-forward (concat "^" outline-regexp) end t)
  9990. (push (cons (org-current-line) (org-entry-properties)) cache))
  9991. (when cache
  9992. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  9993. (org-set-local 'org-columns-current-maxwidths maxwidths)
  9994. (org-columns-display-here-title)
  9995. (mapc (lambda (x)
  9996. (goto-line (car x))
  9997. (org-columns-display-here (cdr x)))
  9998. cache)))))
  9999. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  10000. "Insert a new column, to the left of the current column."
  10001. (interactive)
  10002. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  10003. cell)
  10004. (setq prop (completing-read
  10005. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  10006. nil nil prop))
  10007. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  10008. (setq width (read-string "Column width: " (if width (number-to-string width))))
  10009. (if (string-match "\\S-" width)
  10010. (setq width (string-to-number width))
  10011. (setq width nil))
  10012. (setq fmt (completing-read "Summary [none]: "
  10013. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  10014. nil t))
  10015. (if (string-match "\\S-" fmt)
  10016. (setq fmt (intern fmt))
  10017. (setq fmt nil))
  10018. (if (eq fmt 'none) (setq fmt nil))
  10019. (if editp
  10020. (progn
  10021. (setcar editp prop)
  10022. (setcdr editp (list title width nil fmt)))
  10023. (setq cell (nthcdr (1- (current-column))
  10024. org-columns-current-fmt-compiled))
  10025. (setcdr cell (cons (list prop title width nil fmt)
  10026. (cdr cell))))
  10027. (org-columns-store-format)
  10028. (org-columns-redo)))
  10029. (defun org-columns-delete ()
  10030. "Delete the column at point from columns view."
  10031. (interactive)
  10032. (let* ((n (current-column))
  10033. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  10034. (when (y-or-n-p
  10035. (format "Are you sure you want to remove column \"%s\"? " title))
  10036. (setq org-columns-current-fmt-compiled
  10037. (delq (nth n org-columns-current-fmt-compiled)
  10038. org-columns-current-fmt-compiled))
  10039. (org-columns-store-format)
  10040. (org-columns-redo)
  10041. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  10042. (backward-char 1)))))
  10043. (defun org-columns-edit-attributes ()
  10044. "Edit the attributes of the current column."
  10045. (interactive)
  10046. (let* ((n (current-column))
  10047. (info (nth n org-columns-current-fmt-compiled)))
  10048. (apply 'org-columns-new info)))
  10049. (defun org-columns-widen (arg)
  10050. "Make the column wider by ARG characters."
  10051. (interactive "p")
  10052. (let* ((n (current-column))
  10053. (entry (nth n org-columns-current-fmt-compiled))
  10054. (width (or (nth 2 entry)
  10055. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  10056. (setq width (max 1 (+ width arg)))
  10057. (setcar (nthcdr 2 entry) width)
  10058. (org-columns-store-format)
  10059. (org-columns-redo)))
  10060. (defun org-columns-narrow (arg)
  10061. "Make the column nrrower by ARG characters."
  10062. (interactive "p")
  10063. (org-columns-widen (- arg)))
  10064. (defun org-columns-move-right ()
  10065. "Swap this column with the one to the right."
  10066. (interactive)
  10067. (let* ((n (current-column))
  10068. (cell (nthcdr n org-columns-current-fmt-compiled))
  10069. e)
  10070. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  10071. (error "Cannot shift this column further to the right"))
  10072. (setq e (car cell))
  10073. (setcar cell (car (cdr cell)))
  10074. (setcdr cell (cons e (cdr (cdr cell))))
  10075. (org-columns-store-format)
  10076. (org-columns-redo)
  10077. (forward-char 1)))
  10078. (defun org-columns-move-left ()
  10079. "Swap this column with the one to the left."
  10080. (interactive)
  10081. (let* ((n (current-column)))
  10082. (when (= n 0)
  10083. (error "Cannot shift this column further to the left"))
  10084. (backward-char 1)
  10085. (org-columns-move-right)
  10086. (backward-char 1)))
  10087. (defun org-columns-store-format ()
  10088. "Store the text version of the current columns format in appropriate place.
  10089. This is either in the COLUMNS property of the node starting the current column
  10090. display, or in the #+COLUMNS line of the current buffer."
  10091. (let (fmt (cnt 0))
  10092. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  10093. (org-set-local 'org-columns-current-fmt fmt)
  10094. (if (marker-position org-columns-top-level-marker)
  10095. (save-excursion
  10096. (goto-char org-columns-top-level-marker)
  10097. (if (and (org-at-heading-p)
  10098. (org-entry-get nil "COLUMNS"))
  10099. (org-entry-put nil "COLUMNS" fmt)
  10100. (goto-char (point-min))
  10101. ;; Overwrite all #+COLUMNS lines....
  10102. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  10103. (setq cnt (1+ cnt))
  10104. (replace-match (concat "#+COLUMNS: " fmt) t t))
  10105. (unless (> cnt 0)
  10106. (goto-char (point-min))
  10107. (or (org-on-heading-p t) (outline-next-heading))
  10108. (let ((inhibit-read-only t))
  10109. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  10110. (org-set-local 'org-columns-default-format fmt))))))
  10111. (defvar org-overriding-columns-format nil
  10112. "When set, overrides any other definition.")
  10113. (defvar org-agenda-view-columns-initially nil
  10114. "When set, switch to columns view immediately after creating the agenda.")
  10115. (defun org-agenda-columns ()
  10116. "Turn on column view in the agenda."
  10117. (interactive)
  10118. (org-verify-version 'columns)
  10119. (org-columns-remove-overlays)
  10120. (move-marker org-columns-begin-marker (point))
  10121. (let (fmt cache maxwidths m)
  10122. (cond
  10123. ((and (local-variable-p 'org-overriding-columns-format)
  10124. org-overriding-columns-format)
  10125. (setq fmt org-overriding-columns-format))
  10126. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  10127. (setq fmt (or (org-entry-get m "COLUMNS" t)
  10128. (with-current-buffer (marker-buffer m)
  10129. org-columns-default-format))))
  10130. ((and (boundp 'org-columns-current-fmt)
  10131. (local-variable-p 'org-columns-current-fmt)
  10132. org-columns-current-fmt)
  10133. (setq fmt org-columns-current-fmt))
  10134. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  10135. (setq m (get-text-property m 'org-hd-marker))
  10136. (setq fmt (or (org-entry-get m "COLUMNS" t)
  10137. (with-current-buffer (marker-buffer m)
  10138. org-columns-default-format)))))
  10139. (setq fmt (or fmt org-columns-default-format))
  10140. (org-set-local 'org-columns-current-fmt fmt)
  10141. (org-columns-compile-format fmt)
  10142. (save-excursion
  10143. ;; Get and cache the properties
  10144. (goto-char (point-min))
  10145. (while (not (eobp))
  10146. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  10147. (get-text-property (point) 'org-marker)))
  10148. (push (cons (org-current-line) (org-entry-properties m)) cache))
  10149. (beginning-of-line 2))
  10150. (when cache
  10151. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  10152. (org-set-local 'org-columns-current-maxwidths maxwidths)
  10153. (org-columns-display-here-title)
  10154. (mapc (lambda (x)
  10155. (goto-line (car x))
  10156. (org-columns-display-here (cdr x)))
  10157. cache)))))
  10158. (defun org-columns-get-autowidth-alist (s cache)
  10159. "Derive the maximum column widths from the format and the cache."
  10160. (let ((start 0) rtn)
  10161. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  10162. (push (cons (match-string 1 s) 1) rtn)
  10163. (setq start (match-end 0)))
  10164. (mapc (lambda (x)
  10165. (setcdr x (apply 'max
  10166. (mapcar
  10167. (lambda (y)
  10168. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  10169. cache))))
  10170. rtn)
  10171. rtn))
  10172. (defun org-columns-compute-all ()
  10173. "Compute all columns that have operators defined."
  10174. (org-unmodified
  10175. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  10176. (let ((columns org-columns-current-fmt-compiled) col)
  10177. (while (setq col (pop columns))
  10178. (when (nth 3 col)
  10179. (save-excursion
  10180. (org-columns-compute (car col)))))))
  10181. (defun org-columns-update (property)
  10182. "Recompute PROPERTY, and update the columns display for it."
  10183. (org-columns-compute property)
  10184. (let (fmt val pos)
  10185. (save-excursion
  10186. (mapc (lambda (ov)
  10187. (when (equal (org-overlay-get ov 'org-columns-key) property)
  10188. (setq pos (org-overlay-start ov))
  10189. (goto-char pos)
  10190. (when (setq val (cdr (assoc property
  10191. (get-text-property
  10192. (point-at-bol) 'org-summaries))))
  10193. (setq fmt (org-overlay-get ov 'org-columns-format))
  10194. (org-overlay-put ov 'org-columns-value val)
  10195. (org-overlay-put ov 'display (format fmt val)))))
  10196. org-columns-overlays))))
  10197. (defun org-columns-compute (property)
  10198. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  10199. (interactive)
  10200. (let* ((re (concat "^" outline-regexp))
  10201. (lmax 30) ; Does anyone use deeper levels???
  10202. (lsum (make-vector lmax 0))
  10203. (lflag (make-vector lmax nil))
  10204. (level 0)
  10205. (ass (assoc property org-columns-current-fmt-compiled))
  10206. (format (nth 4 ass))
  10207. (printf (nth 5 ass))
  10208. (beg org-columns-top-level-marker)
  10209. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  10210. (save-excursion
  10211. ;; Find the region to compute
  10212. (goto-char beg)
  10213. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  10214. (goto-char end)
  10215. ;; Walk the tree from the back and do the computations
  10216. (while (re-search-backward re beg t)
  10217. (setq sumpos (match-beginning 0)
  10218. last-level level
  10219. level (org-outline-level)
  10220. val (org-entry-get nil property)
  10221. valflag (and val (string-match "\\S-" val)))
  10222. (cond
  10223. ((< level last-level)
  10224. ;; put the sum of lower levels here as a property
  10225. (setq sum (aref lsum last-level) ; current sum
  10226. flag (aref lflag last-level) ; any valid entries from children?
  10227. str (org-column-number-to-string sum format printf)
  10228. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  10229. useval (if flag str1 (if valflag val ""))
  10230. sum-alist (get-text-property sumpos 'org-summaries))
  10231. (if (assoc property sum-alist)
  10232. (setcdr (assoc property sum-alist) useval)
  10233. (push (cons property useval) sum-alist)
  10234. (org-unmodified
  10235. (add-text-properties sumpos (1+ sumpos)
  10236. (list 'org-summaries sum-alist))))
  10237. (when val
  10238. (org-entry-put nil property (if flag str val)))
  10239. ;; add current to current level accumulator
  10240. (when (or flag valflag)
  10241. (aset lsum level (+ (aref lsum level)
  10242. (if flag sum (org-column-string-to-number
  10243. (if flag str val) format))))
  10244. (aset lflag level t))
  10245. ;; clear accumulators for deeper levels
  10246. (loop for l from (1+ level) to (1- lmax) do
  10247. (aset lsum l 0)
  10248. (aset lflag l nil)))
  10249. ((>= level last-level)
  10250. ;; add what we have here to the accumulator for this level
  10251. (aset lsum level (+ (aref lsum level)
  10252. (org-column-string-to-number (or val "0") format)))
  10253. (and valflag (aset lflag level t)))
  10254. (t (error "This should not happen")))))))
  10255. (defun org-columns-redo ()
  10256. "Construct the column display again."
  10257. (interactive)
  10258. (message "Recomputing columns...")
  10259. (save-excursion
  10260. (if (marker-position org-columns-begin-marker)
  10261. (goto-char org-columns-begin-marker))
  10262. (org-columns-remove-overlays)
  10263. (if (org-mode-p)
  10264. (call-interactively 'org-columns)
  10265. (call-interactively 'org-agenda-columns)))
  10266. (message "Recomputing columns...done"))
  10267. (defun org-columns-not-in-agenda ()
  10268. (if (eq major-mode 'org-agenda-mode)
  10269. (error "This command is only allowed in Org-mode buffers")))
  10270. (defun org-string-to-number (s)
  10271. "Convert string to number, and interpret hh:mm:ss."
  10272. (if (not (string-match ":" s))
  10273. (string-to-number s)
  10274. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10275. (while l
  10276. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10277. sum)))
  10278. (defun org-column-number-to-string (n fmt &optional printf)
  10279. "Convert a computed column number to a string value, according to FMT."
  10280. (cond
  10281. ((eq fmt 'add_times)
  10282. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  10283. (format "%d:%02d" h m)))
  10284. ((eq fmt 'checkbox)
  10285. (cond ((= n (floor n)) "[X]")
  10286. ((> n 1.) "[-]")
  10287. (t "[ ]")))
  10288. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  10289. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  10290. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  10291. (printf (format printf n))
  10292. ((eq fmt 'currency)
  10293. (format "%.2f" n))
  10294. (t (number-to-string n))))
  10295. (defun org-nofm-to-completion (n m &optional percent)
  10296. (if (not percent)
  10297. (format "[%d/%d]" n m)
  10298. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  10299. (defun org-column-string-to-number (s fmt)
  10300. "Convert a column value to a number that can be used for column computing."
  10301. (cond
  10302. ((string-match ":" s)
  10303. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10304. (while l
  10305. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10306. sum))
  10307. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  10308. (if (equal s "[X]") 1. 0.000001))
  10309. (t (string-to-number s))))
  10310. (defun org-columns-uncompile-format (cfmt)
  10311. "Turn the compiled columns format back into a string representation."
  10312. (let ((rtn "") e s prop title op width fmt printf)
  10313. (while (setq e (pop cfmt))
  10314. (setq prop (car e)
  10315. title (nth 1 e)
  10316. width (nth 2 e)
  10317. op (nth 3 e)
  10318. fmt (nth 4 e)
  10319. printf (nth 5 e))
  10320. (cond
  10321. ((eq fmt 'add_times) (setq op ":"))
  10322. ((eq fmt 'checkbox) (setq op "X"))
  10323. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  10324. ((eq fmt 'checkbox-percent) (setq op "X%"))
  10325. ((eq fmt 'add_numbers) (setq op "+"))
  10326. ((eq fmt 'currency) (setq op "$")))
  10327. (if (and op printf) (setq op (concat op ";" printf)))
  10328. (if (equal title prop) (setq title nil))
  10329. (setq s (concat "%" (if width (number-to-string width))
  10330. prop
  10331. (if title (concat "(" title ")"))
  10332. (if op (concat "{" op "}"))))
  10333. (setq rtn (concat rtn " " s)))
  10334. (org-trim rtn)))
  10335. (defun org-columns-compile-format (fmt)
  10336. "Turn a column format string into an alist of specifications.
  10337. The alist has one entry for each column in the format. The elements of
  10338. that list are:
  10339. property the property
  10340. title the title field for the columns
  10341. width the column width in characters, can be nil for automatic
  10342. operator the operator if any
  10343. format the output format for computed results, derived from operator
  10344. printf a printf format for computed values"
  10345. (let ((start 0) width prop title op f printf)
  10346. (setq org-columns-current-fmt-compiled nil)
  10347. (while (string-match
  10348. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  10349. fmt start)
  10350. (setq start (match-end 0)
  10351. width (match-string 1 fmt)
  10352. prop (match-string 2 fmt)
  10353. title (or (match-string 3 fmt) prop)
  10354. op (match-string 4 fmt)
  10355. f nil
  10356. printf nil)
  10357. (if width (setq width (string-to-number width)))
  10358. (when (and op (string-match ";" op))
  10359. (setq printf (substring op (match-end 0))
  10360. op (substring op 0 (match-beginning 0))))
  10361. (cond
  10362. ((equal op "+") (setq f 'add_numbers))
  10363. ((equal op "$") (setq f 'currency))
  10364. ((equal op ":") (setq f 'add_times))
  10365. ((equal op "X") (setq f 'checkbox))
  10366. ((equal op "X/") (setq f 'checkbox-n-of-m))
  10367. ((equal op "X%") (setq f 'checkbox-percent))
  10368. )
  10369. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  10370. (setq org-columns-current-fmt-compiled
  10371. (nreverse org-columns-current-fmt-compiled))))
  10372. ;;; Dynamic block for Column view
  10373. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  10374. "Get the column view of the current buffer or subtree.
  10375. The first optional argument MAXLEVEL sets the level limit. A
  10376. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  10377. empty rows, an empty row being one where all the column view
  10378. specifiers except ITEM are empty. This function returns a list
  10379. containing the title row and all other rows. Each row is a list
  10380. of fields."
  10381. (save-excursion
  10382. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  10383. (n (length title)) row tbl)
  10384. (goto-char (point-min))
  10385. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  10386. (or (null maxlevel)
  10387. (>= maxlevel
  10388. (if org-odd-levels-only
  10389. (/ (1+ (length (match-string 1))) 2)
  10390. (length (match-string 1))))))
  10391. (when (get-char-property (match-beginning 0) 'org-columns-key)
  10392. (setq row nil)
  10393. (loop for i from 0 to (1- n) do
  10394. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  10395. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  10396. "")
  10397. row))
  10398. (setq row (nreverse row))
  10399. (unless (and skip-empty-rows
  10400. (eq 1 (length (delete "" (delete-dups row)))))
  10401. (push row tbl))))
  10402. (append (list title 'hline) (nreverse tbl)))))
  10403. (defun org-dblock-write:columnview (params)
  10404. "Write the column view table.
  10405. PARAMS is a property list of parameters:
  10406. :width enforce same column widths with <N> specifiers.
  10407. :id the :ID: property of the entry where the columns view
  10408. should be built, as a string. When `local', call locally.
  10409. When `global' call column view with the cursor at the beginning
  10410. of the buffer (usually this means that the whole buffer switches
  10411. to column view).
  10412. :hlines When t, insert a hline before each item. When a number, insert
  10413. a hline before each level <= that number.
  10414. :vlines When t, make each column a colgroup to enforce vertical lines.
  10415. :maxlevel When set to a number, don't capture headlines below this level.
  10416. :skip-empty-rows
  10417. When t, skip rows where all specifiers other than ITEM are empty."
  10418. (let ((pos (move-marker (make-marker) (point)))
  10419. (hlines (plist-get params :hlines))
  10420. (vlines (plist-get params :vlines))
  10421. (maxlevel (plist-get params :maxlevel))
  10422. (skip-empty-rows (plist-get params :skip-empty-rows))
  10423. tbl id idpos nfields tmp)
  10424. (save-excursion
  10425. (save-restriction
  10426. (when (setq id (plist-get params :id))
  10427. (cond ((not id) nil)
  10428. ((eq id 'global) (goto-char (point-min)))
  10429. ((eq id 'local) nil)
  10430. ((setq idpos (org-find-entry-with-id id))
  10431. (goto-char idpos))
  10432. (t (error "Cannot find entry with :ID: %s" id))))
  10433. (org-columns)
  10434. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  10435. (setq nfields (length (car tbl)))
  10436. (org-columns-quit)))
  10437. (goto-char pos)
  10438. (move-marker pos nil)
  10439. (when tbl
  10440. (when (plist-get params :hlines)
  10441. (setq tmp nil)
  10442. (while tbl
  10443. (if (eq (car tbl) 'hline)
  10444. (push (pop tbl) tmp)
  10445. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  10446. (if (and (not (eq (car tmp) 'hline))
  10447. (or (eq hlines t)
  10448. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  10449. (push 'hline tmp)))
  10450. (push (pop tbl) tmp)))
  10451. (setq tbl (nreverse tmp)))
  10452. (when vlines
  10453. (setq tbl (mapcar (lambda (x)
  10454. (if (eq 'hline x) x (cons "" x)))
  10455. tbl))
  10456. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  10457. (setq pos (point))
  10458. (insert (org-listtable-to-string tbl))
  10459. (when (plist-get params :width)
  10460. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  10461. org-columns-current-widths "|")))
  10462. (goto-char pos)
  10463. (org-table-align))))
  10464. (defun org-listtable-to-string (tbl)
  10465. "Convert a listtable TBL to a string that contains the Org-mode table.
  10466. The table still need to be alligned. The resulting string has no leading
  10467. and tailing newline characters."
  10468. (mapconcat
  10469. (lambda (x)
  10470. (cond
  10471. ((listp x)
  10472. (concat "|" (mapconcat 'identity x "|") "|"))
  10473. ((eq x 'hline) "|-|")
  10474. (t (error "Garbage in listtable: %s" x))))
  10475. tbl "\n"))
  10476. (defun org-insert-columns-dblock ()
  10477. "Create a dynamic block capturing a column view table."
  10478. (interactive)
  10479. (let ((defaults '(:name "columnview" :hlines 1))
  10480. (id (completing-read
  10481. "Capture columns (local, global, entry with :ID: property) [local]: "
  10482. (append '(("global") ("local"))
  10483. (mapcar 'list (org-property-values "ID"))))))
  10484. (if (equal id "") (setq id 'local))
  10485. (if (equal id "global") (setq id 'global))
  10486. (setq defaults (append defaults (list :id id)))
  10487. (org-create-dblock defaults)
  10488. (org-update-dblock)))
  10489. ;;;; Timestamps
  10490. (defvar org-last-changed-timestamp nil)
  10491. (defvar org-time-was-given) ; dynamically scoped parameter
  10492. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10493. (defvar org-ts-what) ; dynamically scoped parameter
  10494. (defun org-time-stamp (arg)
  10495. "Prompt for a date/time and insert a time stamp.
  10496. If the user specifies a time like HH:MM, or if this command is called
  10497. with a prefix argument, the time stamp will contain date and time.
  10498. Otherwise, only the date will be included. All parts of a date not
  10499. specified by the user will be filled in from the current date/time.
  10500. So if you press just return without typing anything, the time stamp
  10501. will represent the current date/time. If there is already a timestamp
  10502. at the cursor, it will be modified."
  10503. (interactive "P")
  10504. (let* ((ts nil)
  10505. (default-time
  10506. ;; Default time is either today, or, when entering a range,
  10507. ;; the range start.
  10508. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10509. (save-excursion
  10510. (re-search-backward
  10511. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10512. (- (point) 20) t)))
  10513. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10514. (current-time)))
  10515. (default-input (and ts (org-get-compact-tod ts)))
  10516. org-time-was-given org-end-time-was-given time)
  10517. (cond
  10518. ((and (org-at-timestamp-p)
  10519. (eq last-command 'org-time-stamp)
  10520. (eq this-command 'org-time-stamp))
  10521. (insert "--")
  10522. (setq time (let ((this-command this-command))
  10523. (org-read-date arg 'totime nil nil default-time default-input)))
  10524. (org-insert-time-stamp time (or org-time-was-given arg)))
  10525. ((org-at-timestamp-p)
  10526. (setq time (let ((this-command this-command))
  10527. (org-read-date arg 'totime nil nil default-time default-input)))
  10528. (when (org-at-timestamp-p) ; just to get the match data
  10529. (replace-match "")
  10530. (setq org-last-changed-timestamp
  10531. (org-insert-time-stamp
  10532. time (or org-time-was-given arg)
  10533. nil nil nil (list org-end-time-was-given))))
  10534. (message "Timestamp updated"))
  10535. (t
  10536. (setq time (let ((this-command this-command))
  10537. (org-read-date arg 'totime nil nil default-time default-input)))
  10538. (org-insert-time-stamp time (or org-time-was-given arg)
  10539. nil nil nil (list org-end-time-was-given))))))
  10540. ;; FIXME: can we use this for something else, like computing time differences?
  10541. (defun org-get-compact-tod (s)
  10542. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10543. (let* ((t1 (match-string 1 s))
  10544. (h1 (string-to-number (match-string 2 s)))
  10545. (m1 (string-to-number (match-string 3 s)))
  10546. (t2 (and (match-end 4) (match-string 5 s)))
  10547. (h2 (and t2 (string-to-number (match-string 6 s))))
  10548. (m2 (and t2 (string-to-number (match-string 7 s))))
  10549. dh dm)
  10550. (if (not t2)
  10551. t1
  10552. (setq dh (- h2 h1) dm (- m2 m1))
  10553. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10554. (concat t1 "+" (number-to-string dh)
  10555. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10556. (defun org-time-stamp-inactive (&optional arg)
  10557. "Insert an inactive time stamp.
  10558. An inactive time stamp is enclosed in square brackets instead of angle
  10559. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10560. does not link to the calendar and cannot be changed with the S-cursor keys.
  10561. So these are more for recording a certain time/date."
  10562. (interactive "P")
  10563. (let (org-time-was-given org-end-time-was-given time)
  10564. (setq time (org-read-date arg 'totime))
  10565. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  10566. nil nil (list org-end-time-was-given))))
  10567. (defvar org-date-ovl (org-make-overlay 1 1))
  10568. (org-overlay-put org-date-ovl 'face 'org-warning)
  10569. (org-detach-overlay org-date-ovl)
  10570. (defvar org-ans1) ; dynamically scoped parameter
  10571. (defvar org-ans2) ; dynamically scoped parameter
  10572. (defvar org-plain-time-of-day-regexp) ; defined below
  10573. (defvar org-read-date-overlay nil)
  10574. (defvar org-dcst nil) ; dynamically scoped
  10575. (defun org-read-date (&optional with-time to-time from-string prompt
  10576. default-time default-input)
  10577. "Read a date, possibly a time, and make things smooth for the user.
  10578. The prompt will suggest to enter an ISO date, but you can also enter anything
  10579. which will at least partially be understood by `parse-time-string'.
  10580. Unrecognized parts of the date will default to the current day, month, year,
  10581. hour and minute. If this command is called to replace a timestamp at point,
  10582. of to enter the second timestamp of a range, the default time is taken from the
  10583. existing stamp. For example,
  10584. 3-2-5 --> 2003-02-05
  10585. feb 15 --> currentyear-02-15
  10586. sep 12 9 --> 2009-09-12
  10587. 12:45 --> today 12:45
  10588. 22 sept 0:34 --> currentyear-09-22 0:34
  10589. 12 --> currentyear-currentmonth-12
  10590. Fri --> nearest Friday (today or later)
  10591. etc.
  10592. Furthermore you can specify a relative date by giving, as the *first* thing
  10593. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10594. change in days weeks, months, years.
  10595. With a single plus or minus, the date is relative to today. With a double
  10596. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10597. +4d --> four days from today
  10598. +4 --> same as above
  10599. +2w --> two weeks from today
  10600. ++5 --> five days from default date
  10601. The function understands only English month and weekday abbreviations,
  10602. but this can be configured with the variables `parse-time-months' and
  10603. `parse-time-weekdays'.
  10604. While prompting, a calendar is popped up - you can also select the
  10605. date with the mouse (button 1). The calendar shows a period of three
  10606. months. To scroll it to other months, use the keys `>' and `<'.
  10607. If you don't like the calendar, turn it off with
  10608. \(setq org-read-date-popup-calendar nil)
  10609. With optional argument TO-TIME, the date will immediately be converted
  10610. to an internal time.
  10611. With an optional argument WITH-TIME, the prompt will suggest to also
  10612. insert a time. Note that when WITH-TIME is not set, you can still
  10613. enter a time, and this function will inform the calling routine about
  10614. this change. The calling routine may then choose to change the format
  10615. used to insert the time stamp into the buffer to include the time.
  10616. With optional argument FROM-STRING, read from this string instead from
  10617. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10618. the time/date that is used for everything that is not specified by the
  10619. user."
  10620. (require 'parse-time)
  10621. (let* ((org-time-stamp-rounding-minutes
  10622. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10623. (org-dcst org-display-custom-times)
  10624. (ct (org-current-time))
  10625. (def (or default-time ct))
  10626. (defdecode (decode-time def))
  10627. (dummy (progn
  10628. (when (< (nth 2 defdecode) org-extend-today-until)
  10629. (setcar (nthcdr 2 defdecode) -1)
  10630. (setcar (nthcdr 1 defdecode) 59)
  10631. (setq def (apply 'encode-time defdecode)
  10632. defdecode (decode-time def)))))
  10633. (calendar-move-hook nil)
  10634. (view-diary-entries-initially nil)
  10635. (view-calendar-holidays-initially nil)
  10636. (timestr (format-time-string
  10637. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10638. (prompt (concat (if prompt (concat prompt " ") "")
  10639. (format "Date+time [%s]: " timestr)))
  10640. ans (org-ans0 "") org-ans1 org-ans2 final)
  10641. (cond
  10642. (from-string (setq ans from-string))
  10643. (org-read-date-popup-calendar
  10644. (save-excursion
  10645. (save-window-excursion
  10646. (calendar)
  10647. (calendar-forward-day (- (time-to-days def)
  10648. (calendar-absolute-from-gregorian
  10649. (calendar-current-date))))
  10650. (org-eval-in-calendar nil t)
  10651. (let* ((old-map (current-local-map))
  10652. (map (copy-keymap calendar-mode-map))
  10653. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10654. (org-defkey map (kbd "RET") 'org-calendar-select)
  10655. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10656. 'org-calendar-select-mouse)
  10657. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10658. 'org-calendar-select-mouse)
  10659. (org-defkey minibuffer-local-map [(meta shift left)]
  10660. (lambda () (interactive)
  10661. (org-eval-in-calendar '(calendar-backward-month 1))))
  10662. (org-defkey minibuffer-local-map [(meta shift right)]
  10663. (lambda () (interactive)
  10664. (org-eval-in-calendar '(calendar-forward-month 1))))
  10665. (org-defkey minibuffer-local-map [(meta shift up)]
  10666. (lambda () (interactive)
  10667. (org-eval-in-calendar '(calendar-backward-year 1))))
  10668. (org-defkey minibuffer-local-map [(meta shift down)]
  10669. (lambda () (interactive)
  10670. (org-eval-in-calendar '(calendar-forward-year 1))))
  10671. (org-defkey minibuffer-local-map [(shift up)]
  10672. (lambda () (interactive)
  10673. (org-eval-in-calendar '(calendar-backward-week 1))))
  10674. (org-defkey minibuffer-local-map [(shift down)]
  10675. (lambda () (interactive)
  10676. (org-eval-in-calendar '(calendar-forward-week 1))))
  10677. (org-defkey minibuffer-local-map [(shift left)]
  10678. (lambda () (interactive)
  10679. (org-eval-in-calendar '(calendar-backward-day 1))))
  10680. (org-defkey minibuffer-local-map [(shift right)]
  10681. (lambda () (interactive)
  10682. (org-eval-in-calendar '(calendar-forward-day 1))))
  10683. (org-defkey minibuffer-local-map ">"
  10684. (lambda () (interactive)
  10685. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10686. (org-defkey minibuffer-local-map "<"
  10687. (lambda () (interactive)
  10688. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10689. (unwind-protect
  10690. (progn
  10691. (use-local-map map)
  10692. (add-hook 'post-command-hook 'org-read-date-display)
  10693. (setq org-ans0 (read-string prompt default-input nil nil))
  10694. ;; org-ans0: from prompt
  10695. ;; org-ans1: from mouse click
  10696. ;; org-ans2: from calendar motion
  10697. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10698. (remove-hook 'post-command-hook 'org-read-date-display)
  10699. (use-local-map old-map)
  10700. (when org-read-date-overlay
  10701. (org-delete-overlay org-read-date-overlay)
  10702. (setq org-read-date-overlay nil)))))))
  10703. (t ; Naked prompt only
  10704. (unwind-protect
  10705. (setq ans (read-string prompt default-input nil timestr))
  10706. (when org-read-date-overlay
  10707. (org-delete-overlay org-read-date-overlay)
  10708. (setq org-read-date-overlay nil)))))
  10709. (setq final (org-read-date-analyze ans def defdecode))
  10710. (if to-time
  10711. (apply 'encode-time final)
  10712. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10713. (format "%04d-%02d-%02d %02d:%02d"
  10714. (nth 5 final) (nth 4 final) (nth 3 final)
  10715. (nth 2 final) (nth 1 final))
  10716. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10717. (defvar def)
  10718. (defvar defdecode)
  10719. (defvar with-time)
  10720. (defun org-read-date-display ()
  10721. "Display the currrent date prompt interpretation in the minibuffer."
  10722. (when org-read-date-display-live
  10723. (when org-read-date-overlay
  10724. (org-delete-overlay org-read-date-overlay))
  10725. (let ((p (point)))
  10726. (end-of-line 1)
  10727. (while (not (equal (buffer-substring
  10728. (max (point-min) (- (point) 4)) (point))
  10729. " "))
  10730. (insert " "))
  10731. (goto-char p))
  10732. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10733. " " (or org-ans1 org-ans2)))
  10734. (org-end-time-was-given nil)
  10735. (f (org-read-date-analyze ans def defdecode))
  10736. (fmts (if org-dcst
  10737. org-time-stamp-custom-formats
  10738. org-time-stamp-formats))
  10739. (fmt (if (or with-time
  10740. (and (boundp 'org-time-was-given) org-time-was-given))
  10741. (cdr fmts)
  10742. (car fmts)))
  10743. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10744. (when (and org-end-time-was-given
  10745. (string-match org-plain-time-of-day-regexp txt))
  10746. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10747. org-end-time-was-given
  10748. (substring txt (match-end 0)))))
  10749. (setq org-read-date-overlay
  10750. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  10751. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10752. (defun org-read-date-analyze (ans def defdecode)
  10753. "Analyze the combined answer of the date prompt."
  10754. ;; FIXME: cleanup and comment
  10755. (let (delta deltan deltaw deltadef year month day
  10756. hour minute second wday pm h2 m2 tl wday1
  10757. iso-year iso-weekday iso-week iso-year iso-date)
  10758. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10759. (setq ans (replace-match "" t t ans)
  10760. deltan (car delta)
  10761. deltaw (nth 1 delta)
  10762. deltadef (nth 2 delta)))
  10763. ;; Check if there is an iso week date in there
  10764. ;; If yes, sore the info and ostpone interpreting it until the rest
  10765. ;; of the parsing is done
  10766. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10767. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10768. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10769. iso-week (string-to-number (match-string 2 ans)))
  10770. (setq ans (replace-match "" t t ans)))
  10771. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10772. (when (string-match
  10773. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10774. (setq year (if (match-end 2)
  10775. (string-to-number (match-string 2 ans))
  10776. (string-to-number (format-time-string "%Y")))
  10777. month (string-to-number (match-string 3 ans))
  10778. day (string-to-number (match-string 4 ans)))
  10779. (if (< year 100) (setq year (+ 2000 year)))
  10780. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10781. t nil ans)))
  10782. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10783. ;; If there is a time with am/pm, and *no* time without it, we convert
  10784. ;; so that matching will be successful.
  10785. (loop for i from 1 to 2 do ; twice, for end time as well
  10786. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10787. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10788. (setq hour (string-to-number (match-string 1 ans))
  10789. minute (if (match-end 3)
  10790. (string-to-number (match-string 3 ans))
  10791. 0)
  10792. pm (equal ?p
  10793. (string-to-char (downcase (match-string 4 ans)))))
  10794. (if (and (= hour 12) (not pm))
  10795. (setq hour 0)
  10796. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10797. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10798. t t ans))))
  10799. ;; Check if a time range is given as a duration
  10800. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10801. (setq hour (string-to-number (match-string 1 ans))
  10802. h2 (+ hour (string-to-number (match-string 3 ans)))
  10803. minute (string-to-number (match-string 2 ans))
  10804. m2 (+ minute (if (match-end 5) (string-to-number
  10805. (match-string 5 ans))0)))
  10806. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10807. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10808. t t ans)))
  10809. ;; Check if there is a time range
  10810. (when (boundp 'org-end-time-was-given)
  10811. (setq org-time-was-given nil)
  10812. (when (and (string-match org-plain-time-of-day-regexp ans)
  10813. (match-end 8))
  10814. (setq org-end-time-was-given (match-string 8 ans))
  10815. (setq ans (concat (substring ans 0 (match-beginning 7))
  10816. (substring ans (match-end 7))))))
  10817. (setq tl (parse-time-string ans)
  10818. day (or (nth 3 tl) (nth 3 defdecode))
  10819. month (or (nth 4 tl)
  10820. (if (and org-read-date-prefer-future
  10821. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10822. (1+ (nth 4 defdecode))
  10823. (nth 4 defdecode)))
  10824. year (or (nth 5 tl)
  10825. (if (and org-read-date-prefer-future
  10826. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10827. (1+ (nth 5 defdecode))
  10828. (nth 5 defdecode)))
  10829. hour (or (nth 2 tl) (nth 2 defdecode))
  10830. minute (or (nth 1 tl) (nth 1 defdecode))
  10831. second (or (nth 0 tl) 0)
  10832. wday (nth 6 tl))
  10833. ;; Special date definitions below
  10834. (cond
  10835. (iso-week
  10836. ;; There was an iso week
  10837. (setq year (or iso-year year)
  10838. day (or iso-weekday wday 1)
  10839. wday nil ; to make sure that the trigger below does not match
  10840. iso-date (calendar-gregorian-from-absolute
  10841. (calendar-absolute-from-iso
  10842. (list iso-week day year))))
  10843. ; FIXME: Should we also push ISO weeks into the future?
  10844. ; (when (and org-read-date-prefer-future
  10845. ; (not iso-year)
  10846. ; (< (calendar-absolute-from-gregorian iso-date)
  10847. ; (time-to-days (current-time))))
  10848. ; (setq year (1+ year)
  10849. ; iso-date (calendar-gregorian-from-absolute
  10850. ; (calendar-absolute-from-iso
  10851. ; (list iso-week day year)))))
  10852. (setq month (car iso-date)
  10853. year (nth 2 iso-date)
  10854. day (nth 1 iso-date)))
  10855. (deltan
  10856. (unless deltadef
  10857. (let ((now (decode-time (current-time))))
  10858. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10859. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10860. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10861. ((equal deltaw "m") (setq month (+ month deltan)))
  10862. ((equal deltaw "y") (setq year (+ year deltan)))))
  10863. ((and wday (not (nth 3 tl)))
  10864. ;; Weekday was given, but no day, so pick that day in the week
  10865. ;; on or after the derived date.
  10866. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10867. (unless (equal wday wday1)
  10868. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10869. (if (and (boundp 'org-time-was-given)
  10870. (nth 2 tl))
  10871. (setq org-time-was-given t))
  10872. (if (< year 100) (setq year (+ 2000 year)))
  10873. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10874. (list second minute hour day month year)))
  10875. (defvar parse-time-weekdays)
  10876. (defun org-read-date-get-relative (s today default)
  10877. "Check string S for special relative date string.
  10878. TODAY and DEFAULT are internal times, for today and for a default.
  10879. Return shift list (N what def-flag)
  10880. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10881. N is the number of WHATs to shift.
  10882. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10883. the DEFAULT date rather than TODAY."
  10884. (when (string-match
  10885. (concat
  10886. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  10887. "\\([0-9]+\\)?"
  10888. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10889. "\\([ \t]\\|$\\)") s)
  10890. (let* ((dir (if (match-end 1)
  10891. (string-to-char (substring (match-string 1 s) -1))
  10892. ?+))
  10893. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10894. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10895. (what (if (match-end 3) (match-string 3 s) "d"))
  10896. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10897. (date (if rel default today))
  10898. (wday (nth 6 (decode-time date)))
  10899. delta)
  10900. (if wday1
  10901. (progn
  10902. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10903. (if (= dir ?-) (setq delta (- delta 7)))
  10904. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10905. (list delta "d" rel))
  10906. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10907. (defun org-eval-in-calendar (form &optional keepdate)
  10908. "Eval FORM in the calendar window and return to current window.
  10909. Also, store the cursor date in variable org-ans2."
  10910. (let ((sw (selected-window)))
  10911. (select-window (get-buffer-window "*Calendar*"))
  10912. (eval form)
  10913. (when (and (not keepdate) (calendar-cursor-to-date))
  10914. (let* ((date (calendar-cursor-to-date))
  10915. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10916. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10917. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10918. (select-window sw)))
  10919. ; ;; Update the prompt to show new default date
  10920. ; (save-excursion
  10921. ; (goto-char (point-min))
  10922. ; (when (and org-ans2
  10923. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10924. ; (get-text-property (match-end 0) 'field))
  10925. ; (let ((inhibit-read-only t))
  10926. ; (replace-match (concat "[" org-ans2 "]") t t)
  10927. ; (add-text-properties (point-min) (1+ (match-end 0))
  10928. ; (text-properties-at (1+ (point-min)))))))))
  10929. (defun org-calendar-select ()
  10930. "Return to `org-read-date' with the date currently selected.
  10931. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10932. (interactive)
  10933. (when (calendar-cursor-to-date)
  10934. (let* ((date (calendar-cursor-to-date))
  10935. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10936. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10937. (if (active-minibuffer-window) (exit-minibuffer))))
  10938. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10939. "Insert a date stamp for the date given by the internal TIME.
  10940. WITH-HM means, use the stamp format that includes the time of the day.
  10941. INACTIVE means use square brackets instead of angular ones, so that the
  10942. stamp will not contribute to the agenda.
  10943. PRE and POST are optional strings to be inserted before and after the
  10944. stamp.
  10945. The command returns the inserted time stamp."
  10946. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10947. stamp)
  10948. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10949. (insert-before-markers (or pre ""))
  10950. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10951. (when (listp extra)
  10952. (setq extra (car extra))
  10953. (if (and (stringp extra)
  10954. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10955. (setq extra (format "-%02d:%02d"
  10956. (string-to-number (match-string 1 extra))
  10957. (string-to-number (match-string 2 extra))))
  10958. (setq extra nil)))
  10959. (when extra
  10960. (backward-char 1)
  10961. (insert-before-markers extra)
  10962. (forward-char 1))
  10963. (insert-before-markers (or post ""))
  10964. stamp))
  10965. (defun org-toggle-time-stamp-overlays ()
  10966. "Toggle the use of custom time stamp formats."
  10967. (interactive)
  10968. (setq org-display-custom-times (not org-display-custom-times))
  10969. (unless org-display-custom-times
  10970. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10971. (while (setq p (next-single-property-change p 'display))
  10972. (if (and (get-text-property p 'display)
  10973. (eq (get-text-property p 'face) 'org-date))
  10974. (remove-text-properties
  10975. p (setq p (next-single-property-change p 'display))
  10976. '(display t))))
  10977. (set-buffer-modified-p bmp)))
  10978. (if (featurep 'xemacs)
  10979. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10980. (org-restart-font-lock)
  10981. (setq org-table-may-need-update t)
  10982. (if org-display-custom-times
  10983. (message "Time stamps are overlayed with custom format")
  10984. (message "Time stamp overlays removed")))
  10985. (defun org-display-custom-time (beg end)
  10986. "Overlay modified time stamp format over timestamp between BED and END."
  10987. (let* ((ts (buffer-substring beg end))
  10988. t1 w1 with-hm tf time str w2 (off 0))
  10989. (save-match-data
  10990. (setq t1 (org-parse-time-string ts t))
  10991. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10992. (setq off (- (match-end 0) (match-beginning 0)))))
  10993. (setq end (- end off))
  10994. (setq w1 (- end beg)
  10995. with-hm (and (nth 1 t1) (nth 2 t1))
  10996. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10997. time (org-fix-decoded-time t1)
  10998. str (org-add-props
  10999. (format-time-string
  11000. (substring tf 1 -1) (apply 'encode-time time))
  11001. nil 'mouse-face 'highlight)
  11002. w2 (length str))
  11003. (if (not (= w2 w1))
  11004. (add-text-properties (1+ beg) (+ 2 beg)
  11005. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11006. (if (featurep 'xemacs)
  11007. (progn
  11008. (put-text-property beg end 'invisible t)
  11009. (put-text-property beg end 'end-glyph (make-glyph str)))
  11010. (put-text-property beg end 'display str))))
  11011. (defun org-translate-time (string)
  11012. "Translate all timestamps in STRING to custom format.
  11013. But do this only if the variable `org-display-custom-times' is set."
  11014. (when org-display-custom-times
  11015. (save-match-data
  11016. (let* ((start 0)
  11017. (re org-ts-regexp-both)
  11018. t1 with-hm inactive tf time str beg end)
  11019. (while (setq start (string-match re string start))
  11020. (setq beg (match-beginning 0)
  11021. end (match-end 0)
  11022. t1 (save-match-data
  11023. (org-parse-time-string (substring string beg end) t))
  11024. with-hm (and (nth 1 t1) (nth 2 t1))
  11025. inactive (equal (substring string beg (1+ beg)) "[")
  11026. tf (funcall (if with-hm 'cdr 'car)
  11027. org-time-stamp-custom-formats)
  11028. time (org-fix-decoded-time t1)
  11029. str (format-time-string
  11030. (concat
  11031. (if inactive "[" "<") (substring tf 1 -1)
  11032. (if inactive "]" ">"))
  11033. (apply 'encode-time time))
  11034. string (replace-match str t t string)
  11035. start (+ start (length str)))))))
  11036. string)
  11037. (defun org-fix-decoded-time (time)
  11038. "Set 0 instead of nil for the first 6 elements of time.
  11039. Don't touch the rest."
  11040. (let ((n 0))
  11041. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11042. (defun org-days-to-time (timestamp-string)
  11043. "Difference between TIMESTAMP-STRING and now in days."
  11044. (- (time-to-days (org-time-string-to-time timestamp-string))
  11045. (time-to-days (current-time))))
  11046. (defun org-deadline-close (timestamp-string &optional ndays)
  11047. "Is the time in TIMESTAMP-STRING close to the current date?"
  11048. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11049. (and (< (org-days-to-time timestamp-string) ndays)
  11050. (not (org-entry-is-done-p))))
  11051. (defun org-get-wdays (ts)
  11052. "Get the deadline lead time appropriate for timestring TS."
  11053. (cond
  11054. ((<= org-deadline-warning-days 0)
  11055. ;; 0 or negative, enforce this value no matter what
  11056. (- org-deadline-warning-days))
  11057. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  11058. ;; lead time is specified.
  11059. (floor (* (string-to-number (match-string 1 ts))
  11060. (cdr (assoc (match-string 2 ts)
  11061. '(("d" . 1) ("w" . 7)
  11062. ("m" . 30.4) ("y" . 365.25)))))))
  11063. ;; go for the default.
  11064. (t org-deadline-warning-days)))
  11065. (defun org-calendar-select-mouse (ev)
  11066. "Return to `org-read-date' with the date currently selected.
  11067. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11068. (interactive "e")
  11069. (mouse-set-point ev)
  11070. (when (calendar-cursor-to-date)
  11071. (let* ((date (calendar-cursor-to-date))
  11072. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11073. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11074. (if (active-minibuffer-window) (exit-minibuffer))))
  11075. (defun org-check-deadlines (ndays)
  11076. "Check if there are any deadlines due or past due.
  11077. A deadline is considered due if it happens within `org-deadline-warning-days'
  11078. days from today's date. If the deadline appears in an entry marked DONE,
  11079. it is not shown. The prefix arg NDAYS can be used to test that many
  11080. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11081. (interactive "P")
  11082. (let* ((org-warn-days
  11083. (cond
  11084. ((equal ndays '(4)) 100000)
  11085. (ndays (prefix-numeric-value ndays))
  11086. (t (abs org-deadline-warning-days))))
  11087. (case-fold-search nil)
  11088. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11089. (callback
  11090. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11091. (message "%d deadlines past-due or due within %d days"
  11092. (org-occur regexp nil callback)
  11093. org-warn-days)))
  11094. (defun org-check-before-date (date)
  11095. "Check if there are deadlines or scheduled entries before DATE."
  11096. (interactive (list (org-read-date)))
  11097. (let ((case-fold-search nil)
  11098. (regexp (concat "\\<\\(" org-deadline-string
  11099. "\\|" org-scheduled-string
  11100. "\\) *<\\([^>]+\\)>"))
  11101. (callback
  11102. (lambda () (time-less-p
  11103. (org-time-string-to-time (match-string 2))
  11104. (org-time-string-to-time date)))))
  11105. (message "%d entries before %s"
  11106. (org-occur regexp nil callback) date)))
  11107. (defun org-evaluate-time-range (&optional to-buffer)
  11108. "Evaluate a time range by computing the difference between start and end.
  11109. Normally the result is just printed in the echo area, but with prefix arg
  11110. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11111. If the time range is actually in a table, the result is inserted into the
  11112. next column.
  11113. For time difference computation, a year is assumed to be exactly 365
  11114. days in order to avoid rounding problems."
  11115. (interactive "P")
  11116. (or
  11117. (org-clock-update-time-maybe)
  11118. (save-excursion
  11119. (unless (org-at-date-range-p t)
  11120. (goto-char (point-at-bol))
  11121. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11122. (if (not (org-at-date-range-p t))
  11123. (error "Not at a time-stamp range, and none found in current line")))
  11124. (let* ((ts1 (match-string 1))
  11125. (ts2 (match-string 2))
  11126. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11127. (match-end (match-end 0))
  11128. (time1 (org-time-string-to-time ts1))
  11129. (time2 (org-time-string-to-time ts2))
  11130. (t1 (time-to-seconds time1))
  11131. (t2 (time-to-seconds time2))
  11132. (diff (abs (- t2 t1)))
  11133. (negative (< (- t2 t1) 0))
  11134. ;; (ys (floor (* 365 24 60 60)))
  11135. (ds (* 24 60 60))
  11136. (hs (* 60 60))
  11137. (fy "%dy %dd %02d:%02d")
  11138. (fy1 "%dy %dd")
  11139. (fd "%dd %02d:%02d")
  11140. (fd1 "%dd")
  11141. (fh "%02d:%02d")
  11142. y d h m align)
  11143. (if havetime
  11144. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11145. y 0
  11146. d (floor (/ diff ds)) diff (mod diff ds)
  11147. h (floor (/ diff hs)) diff (mod diff hs)
  11148. m (floor (/ diff 60)))
  11149. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11150. y 0
  11151. d (floor (+ (/ diff ds) 0.5))
  11152. h 0 m 0))
  11153. (if (not to-buffer)
  11154. (message "%s" (org-make-tdiff-string y d h m))
  11155. (if (org-at-table-p)
  11156. (progn
  11157. (goto-char match-end)
  11158. (setq align t)
  11159. (and (looking-at " *|") (goto-char (match-end 0))))
  11160. (goto-char match-end))
  11161. (if (looking-at
  11162. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11163. (replace-match ""))
  11164. (if negative (insert " -"))
  11165. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11166. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11167. (insert " " (format fh h m))))
  11168. (if align (org-table-align))
  11169. (message "Time difference inserted")))))
  11170. (defun org-make-tdiff-string (y d h m)
  11171. (let ((fmt "")
  11172. (l nil))
  11173. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11174. l (push y l)))
  11175. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11176. l (push d l)))
  11177. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11178. l (push h l)))
  11179. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11180. l (push m l)))
  11181. (apply 'format fmt (nreverse l))))
  11182. (defun org-time-string-to-time (s)
  11183. (apply 'encode-time (org-parse-time-string s)))
  11184. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11185. "Convert a time stamp to an absolute day number.
  11186. If there is a specifyer for a cyclic time stamp, get the closest date to
  11187. DAYNR.
  11188. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  11189. (cond
  11190. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11191. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11192. daynr
  11193. (+ daynr 1000)))
  11194. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11195. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11196. (time-to-days (current-time))) (match-string 0 s)
  11197. prefer show-all))
  11198. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11199. (defun org-days-to-iso-week (days)
  11200. "Return the iso week number."
  11201. (require 'cal-iso)
  11202. (car (calendar-iso-from-absolute days)))
  11203. (defun org-small-year-to-year (year)
  11204. "Convert 2-digit years into 4-digit years.
  11205. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11206. The year 2000 cannot be abbreviated. Any year lager than 99
  11207. is retrned unchanged."
  11208. (if (< year 38)
  11209. (setq year (+ 2000 year))
  11210. (if (< year 100)
  11211. (setq year (+ 1900 year))))
  11212. year)
  11213. (defun org-time-from-absolute (d)
  11214. "Return the time corresponding to date D.
  11215. D may be an absolute day number, or a calendar-type list (month day year)."
  11216. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11217. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11218. (defun org-calendar-holiday ()
  11219. "List of holidays, for Diary display in Org-mode."
  11220. (require 'holidays)
  11221. (let ((hl (funcall
  11222. (if (fboundp 'calendar-check-holidays)
  11223. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11224. (if hl (mapconcat 'identity hl "; "))))
  11225. (defun org-diary-sexp-entry (sexp entry date)
  11226. "Process a SEXP diary ENTRY for DATE."
  11227. (require 'diary-lib)
  11228. (let ((result (if calendar-debug-sexp
  11229. (let ((stack-trace-on-error t))
  11230. (eval (car (read-from-string sexp))))
  11231. (condition-case nil
  11232. (eval (car (read-from-string sexp)))
  11233. (error
  11234. (beep)
  11235. (message "Bad sexp at line %d in %s: %s"
  11236. (org-current-line)
  11237. (buffer-file-name) sexp)
  11238. (sleep-for 2))))))
  11239. (cond ((stringp result) result)
  11240. ((and (consp result)
  11241. (stringp (cdr result))) (cdr result))
  11242. (result entry)
  11243. (t nil))))
  11244. (defun org-diary-to-ical-string (frombuf)
  11245. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11246. This uses the icalendar.el library."
  11247. (let* ((tmpdir (if (featurep 'xemacs)
  11248. (temp-directory)
  11249. temporary-file-directory))
  11250. (tmpfile (make-temp-name
  11251. (expand-file-name "orgics" tmpdir)))
  11252. buf rtn b e)
  11253. (save-excursion
  11254. (set-buffer frombuf)
  11255. (icalendar-export-region (point-min) (point-max) tmpfile)
  11256. (setq buf (find-buffer-visiting tmpfile))
  11257. (set-buffer buf)
  11258. (goto-char (point-min))
  11259. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11260. (setq b (match-beginning 0)))
  11261. (goto-char (point-max))
  11262. (if (re-search-backward "^END:VEVENT" nil t)
  11263. (setq e (match-end 0)))
  11264. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11265. (kill-buffer buf)
  11266. (kill-buffer frombuf)
  11267. (delete-file tmpfile)
  11268. rtn))
  11269. (defun org-closest-date (start current change prefer show-all)
  11270. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11271. When PREFER is `past' return a date that is either CURRENT or past.
  11272. When PREFER is `future', return a date that is either CURRENT or future.
  11273. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  11274. ;; Make the proper lists from the dates
  11275. (catch 'exit
  11276. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11277. dn dw sday cday n1 n2
  11278. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11279. (setq start (org-date-to-gregorian start)
  11280. current (org-date-to-gregorian
  11281. (if show-all
  11282. current
  11283. (time-to-days (current-time))))
  11284. sday (calendar-absolute-from-gregorian start)
  11285. cday (calendar-absolute-from-gregorian current))
  11286. (if (<= cday sday) (throw 'exit sday))
  11287. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11288. (setq dn (string-to-number (match-string 1 change))
  11289. dw (cdr (assoc (match-string 2 change) a1)))
  11290. (error "Invalid change specifyer: %s" change))
  11291. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11292. (cond
  11293. ((eq dw 'day)
  11294. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11295. n2 (+ n1 dn)))
  11296. ((eq dw 'year)
  11297. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11298. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11299. (setq date1 (list m d y1)
  11300. n1 (calendar-absolute-from-gregorian date1)
  11301. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11302. n2 (calendar-absolute-from-gregorian date2)))
  11303. ((eq dw 'month)
  11304. ;; approx number of month between the tow dates
  11305. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11306. ;; How often does dn fit in there?
  11307. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11308. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11309. m (+ m nm)
  11310. ny (floor (/ m 12))
  11311. y (+ y ny)
  11312. m (- m (* ny 12)))
  11313. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11314. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11315. (setq m2 (+ m dn) y2 y)
  11316. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11317. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11318. (while (< n2 cday)
  11319. (setq n1 n2 m m2 y y2)
  11320. (setq m2 (+ m dn) y2 y)
  11321. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11322. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11323. (if show-all
  11324. (cond
  11325. ((eq prefer 'past) n1)
  11326. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11327. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11328. (cond
  11329. ((eq prefer 'past) n1)
  11330. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11331. (t (if (= cday n1) n1 n2)))))))
  11332. (defun org-date-to-gregorian (date)
  11333. "Turn any specification of DATE into a gregorian date for the calendar."
  11334. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11335. ((and (listp date) (= (length date) 3)) date)
  11336. ((stringp date)
  11337. (setq date (org-parse-time-string date))
  11338. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11339. ((listp date)
  11340. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11341. (defun org-parse-time-string (s &optional nodefault)
  11342. "Parse the standard Org-mode time string.
  11343. This should be a lot faster than the normal `parse-time-string'.
  11344. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11345. hour and minute fields will be nil if not given."
  11346. (if (string-match org-ts-regexp0 s)
  11347. (list 0
  11348. (if (or (match-beginning 8) (not nodefault))
  11349. (string-to-number (or (match-string 8 s) "0")))
  11350. (if (or (match-beginning 7) (not nodefault))
  11351. (string-to-number (or (match-string 7 s) "0")))
  11352. (string-to-number (match-string 4 s))
  11353. (string-to-number (match-string 3 s))
  11354. (string-to-number (match-string 2 s))
  11355. nil nil nil)
  11356. (make-list 9 0)))
  11357. (defun org-timestamp-up (&optional arg)
  11358. "Increase the date item at the cursor by one.
  11359. If the cursor is on the year, change the year. If it is on the month or
  11360. the day, change that.
  11361. With prefix ARG, change by that many units."
  11362. (interactive "p")
  11363. (org-timestamp-change (prefix-numeric-value arg)))
  11364. (defun org-timestamp-down (&optional arg)
  11365. "Decrease the date item at the cursor by one.
  11366. If the cursor is on the year, change the year. If it is on the month or
  11367. the day, change that.
  11368. With prefix ARG, change by that many units."
  11369. (interactive "p")
  11370. (org-timestamp-change (- (prefix-numeric-value arg))))
  11371. (defun org-timestamp-up-day (&optional arg)
  11372. "Increase the date in the time stamp by one day.
  11373. With prefix ARG, change that many days."
  11374. (interactive "p")
  11375. (if (and (not (org-at-timestamp-p t))
  11376. (org-on-heading-p))
  11377. (org-todo 'up)
  11378. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11379. (defun org-timestamp-down-day (&optional arg)
  11380. "Decrease the date in the time stamp by one day.
  11381. With prefix ARG, change that many days."
  11382. (interactive "p")
  11383. (if (and (not (org-at-timestamp-p t))
  11384. (org-on-heading-p))
  11385. (org-todo 'down)
  11386. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11387. (defun org-at-timestamp-p (&optional inactive-ok)
  11388. "Determine if the cursor is in or at a timestamp."
  11389. (interactive)
  11390. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11391. (pos (point))
  11392. (ans (or (looking-at tsr)
  11393. (save-excursion
  11394. (skip-chars-backward "^[<\n\r\t")
  11395. (if (> (point) (point-min)) (backward-char 1))
  11396. (and (looking-at tsr)
  11397. (> (- (match-end 0) pos) -1))))))
  11398. (and ans
  11399. (boundp 'org-ts-what)
  11400. (setq org-ts-what
  11401. (cond
  11402. ((= pos (match-beginning 0)) 'bracket)
  11403. ((= pos (1- (match-end 0))) 'bracket)
  11404. ((org-pos-in-match-range pos 2) 'year)
  11405. ((org-pos-in-match-range pos 3) 'month)
  11406. ((org-pos-in-match-range pos 7) 'hour)
  11407. ((org-pos-in-match-range pos 8) 'minute)
  11408. ((or (org-pos-in-match-range pos 4)
  11409. (org-pos-in-match-range pos 5)) 'day)
  11410. ((and (> pos (or (match-end 8) (match-end 5)))
  11411. (< pos (match-end 0)))
  11412. (- pos (or (match-end 8) (match-end 5))))
  11413. (t 'day))))
  11414. ans))
  11415. (defun org-toggle-timestamp-type ()
  11416. "Toggle the type (<active> or [inactive]) of a time stamp."
  11417. (interactive)
  11418. (when (org-at-timestamp-p t)
  11419. (save-excursion
  11420. (goto-char (match-beginning 0))
  11421. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  11422. (goto-char (1- (match-end 0)))
  11423. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  11424. (message "Timestamp is now %sactive"
  11425. (if (equal (char-before) ?>) "in" ""))))
  11426. (defun org-timestamp-change (n &optional what)
  11427. "Change the date in the time stamp at point.
  11428. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11429. `year', `minute', `second'. If WHAT is not given, the cursor position
  11430. in the timestamp determines what will be changed."
  11431. (let ((pos (point))
  11432. with-hm inactive
  11433. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11434. org-ts-what
  11435. extra rem
  11436. ts time time0)
  11437. (if (not (org-at-timestamp-p t))
  11438. (error "Not at a timestamp"))
  11439. (if (and (not what) (eq org-ts-what 'bracket))
  11440. (org-toggle-timestamp-type)
  11441. (if (and (not what) (not (eq org-ts-what 'day))
  11442. org-display-custom-times
  11443. (get-text-property (point) 'display)
  11444. (not (get-text-property (1- (point)) 'display)))
  11445. (setq org-ts-what 'day))
  11446. (setq org-ts-what (or what org-ts-what)
  11447. inactive (= (char-after (match-beginning 0)) ?\[)
  11448. ts (match-string 0))
  11449. (replace-match "")
  11450. (if (string-match
  11451. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11452. ts)
  11453. (setq extra (match-string 1 ts)))
  11454. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11455. (setq with-hm t))
  11456. (setq time0 (org-parse-time-string ts))
  11457. (when (and (eq org-ts-what 'minute)
  11458. (eq current-prefix-arg nil))
  11459. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11460. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11461. (setcar (cdr time0) (+ (nth 1 time0)
  11462. (if (> n 0) (- rem) (- dm rem))))))
  11463. (setq time
  11464. (encode-time (or (car time0) 0)
  11465. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11466. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11467. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11468. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11469. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11470. (nthcdr 6 time0)))
  11471. (when (integerp org-ts-what)
  11472. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11473. (if (eq what 'calendar)
  11474. (let ((cal-date (org-get-date-from-calendar)))
  11475. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11476. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11477. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11478. (setcar time0 (or (car time0) 0))
  11479. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11480. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11481. (setq time (apply 'encode-time time0))))
  11482. (setq org-last-changed-timestamp
  11483. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11484. (org-clock-update-time-maybe)
  11485. (goto-char pos)
  11486. ;; Try to recenter the calendar window, if any
  11487. (if (and org-calendar-follow-timestamp-change
  11488. (get-buffer-window "*Calendar*" t)
  11489. (memq org-ts-what '(day month year)))
  11490. (org-recenter-calendar (time-to-days time))))))
  11491. (defun org-modify-ts-extra (s pos n dm)
  11492. "Change the different parts of the lead-time and repeat fields in timestamp."
  11493. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11494. ng h m new rem)
  11495. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11496. (cond
  11497. ((or (org-pos-in-match-range pos 2)
  11498. (org-pos-in-match-range pos 3))
  11499. (setq m (string-to-number (match-string 3 s))
  11500. h (string-to-number (match-string 2 s)))
  11501. (if (org-pos-in-match-range pos 2)
  11502. (setq h (+ h n))
  11503. (setq n (* dm (org-no-warnings (signum n))))
  11504. (when (not (= 0 (setq rem (% m dm))))
  11505. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11506. (setq m (+ m n)))
  11507. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11508. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11509. (setq h (min 24 (max 0 h)))
  11510. (setq ng 1 new (format "-%02d:%02d" h m)))
  11511. ((org-pos-in-match-range pos 6)
  11512. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11513. ((org-pos-in-match-range pos 5)
  11514. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11515. ((org-pos-in-match-range pos 9)
  11516. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11517. ((org-pos-in-match-range pos 8)
  11518. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11519. (when ng
  11520. (setq s (concat
  11521. (substring s 0 (match-beginning ng))
  11522. new
  11523. (substring s (match-end ng))))))
  11524. s))
  11525. (defun org-recenter-calendar (date)
  11526. "If the calendar is visible, recenter it to DATE."
  11527. (let* ((win (selected-window))
  11528. (cwin (get-buffer-window "*Calendar*" t))
  11529. (calendar-move-hook nil))
  11530. (when cwin
  11531. (select-window cwin)
  11532. (calendar-goto-date (if (listp date) date
  11533. (calendar-gregorian-from-absolute date)))
  11534. (select-window win))))
  11535. (defun org-goto-calendar (&optional arg)
  11536. "Go to the Emacs calendar at the current date.
  11537. If there is a time stamp in the current line, go to that date.
  11538. A prefix ARG can be used to force the current date."
  11539. (interactive "P")
  11540. (let ((tsr org-ts-regexp) diff
  11541. (calendar-move-hook nil)
  11542. (view-calendar-holidays-initially nil)
  11543. (view-diary-entries-initially nil))
  11544. (if (or (org-at-timestamp-p)
  11545. (save-excursion
  11546. (beginning-of-line 1)
  11547. (looking-at (concat ".*" tsr))))
  11548. (let ((d1 (time-to-days (current-time)))
  11549. (d2 (time-to-days
  11550. (org-time-string-to-time (match-string 1)))))
  11551. (setq diff (- d2 d1))))
  11552. (calendar)
  11553. (calendar-goto-today)
  11554. (if (and diff (not arg)) (calendar-forward-day diff))))
  11555. (defun org-get-date-from-calendar ()
  11556. "Return a list (month day year) of date at point in calendar."
  11557. (with-current-buffer "*Calendar*"
  11558. (save-match-data
  11559. (calendar-cursor-to-date))))
  11560. (defun org-date-from-calendar ()
  11561. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11562. If there is already a time stamp at the cursor position, update it."
  11563. (interactive)
  11564. (if (org-at-timestamp-p t)
  11565. (org-timestamp-change 0 'calendar)
  11566. (let ((cal-date (org-get-date-from-calendar)))
  11567. (org-insert-time-stamp
  11568. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11569. (defun org-minutes-to-hours (m)
  11570. "Compute H:MM from a number of minutes."
  11571. (let ((h (/ m 60)))
  11572. (setq m (- m (* 60 h)))
  11573. (format "%d:%02d" h m)))
  11574. ;;;; Agenda files
  11575. ;;;###autoload
  11576. (defun org-iswitchb (&optional arg)
  11577. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11578. With a prefix argument, restrict available to files.
  11579. With two prefix arguments, restrict available buffers to agenda files.
  11580. Due to some yet unresolved reason, global function
  11581. `iswitchb-mode' needs to be active for this function to work."
  11582. (interactive "P")
  11583. (require 'iswitchb)
  11584. (let ((enabled iswitchb-mode) blist)
  11585. (or enabled (iswitchb-mode 1))
  11586. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11587. ((equal arg '(16)) (org-buffer-list 'agenda))
  11588. (t (org-buffer-list))))
  11589. (unwind-protect
  11590. (let ((iswitchb-make-buflist-hook
  11591. (lambda ()
  11592. (setq iswitchb-temp-buflist
  11593. (mapcar 'buffer-name blist)))))
  11594. (switch-to-buffer
  11595. (iswitchb-read-buffer
  11596. "Switch-to: " nil t))
  11597. (or enabled (iswitchb-mode -1))))))
  11598. (defun org-buffer-list (&optional predicate tmp)
  11599. "Return a list of Org buffers.
  11600. PREDICATE can be either 'export, 'files or 'agenda.
  11601. 'export restrict the list to Export buffers.
  11602. 'files restrict the list to buffers visiting Org files.
  11603. 'agenda restrict the list to buffers visiting agenda files.
  11604. If TMP is non-nil, don't include temporary buffers."
  11605. (let (filter blist)
  11606. (setq filter
  11607. (cond ((eq predicate 'files) "\.org$")
  11608. ((eq predicate 'export) "\*Org .*Export")
  11609. (t "\*Org \\|\.org$")))
  11610. (setq blist
  11611. (mapcar
  11612. (lambda(b)
  11613. (let ((bname (buffer-name b))
  11614. (bfile (buffer-file-name b)))
  11615. (if (and (string-match filter bname)
  11616. (if (eq predicate 'agenda)
  11617. (member bfile
  11618. (mapcar (lambda(f) (file-truename f))
  11619. org-agenda-files)) t)
  11620. (if tmp (not (string-match "tmp" bname)) t)) b)))
  11621. (buffer-list)))
  11622. (delete nil blist)))
  11623. (defun org-agenda-files (&optional unrestricted)
  11624. "Get the list of agenda files.
  11625. Optional UNRESTRICTED means return the full list even if a restriction
  11626. is currently in place."
  11627. (let ((files
  11628. (cond
  11629. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11630. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11631. ((listp org-agenda-files) org-agenda-files)
  11632. (t (error "Invalid value of `org-agenda-files'")))))
  11633. (setq files (apply 'append
  11634. (mapcar (lambda (f)
  11635. (if (file-directory-p f)
  11636. (directory-files f t
  11637. org-agenda-file-regexp)
  11638. (list f)))
  11639. files)))
  11640. (if org-agenda-skip-unavailable-files
  11641. (delq nil
  11642. (mapcar (function
  11643. (lambda (file)
  11644. (and (file-readable-p file) file)))
  11645. files))
  11646. files))) ; `org-check-agenda-file' will remove them from the list
  11647. (defun org-edit-agenda-file-list ()
  11648. "Edit the list of agenda files.
  11649. Depending on setup, this either uses customize to edit the variable
  11650. `org-agenda-files', or it visits the file that is holding the list. In the
  11651. latter case, the buffer is set up in a way that saving it automatically kills
  11652. the buffer and restores the previous window configuration."
  11653. (interactive)
  11654. (if (stringp org-agenda-files)
  11655. (let ((cw (current-window-configuration)))
  11656. (find-file org-agenda-files)
  11657. (org-set-local 'org-window-configuration cw)
  11658. (org-add-hook 'after-save-hook
  11659. (lambda ()
  11660. (set-window-configuration
  11661. (prog1 org-window-configuration
  11662. (kill-buffer (current-buffer))))
  11663. (org-install-agenda-files-menu)
  11664. (message "New agenda file list installed"))
  11665. nil 'local)
  11666. (message "%s" (substitute-command-keys
  11667. "Edit list and finish with \\[save-buffer]")))
  11668. (customize-variable 'org-agenda-files)))
  11669. (defun org-store-new-agenda-file-list (list)
  11670. "Set new value for the agenda file list and save it correcly."
  11671. (if (stringp org-agenda-files)
  11672. (let ((f org-agenda-files) b)
  11673. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11674. (with-temp-file f
  11675. (insert (mapconcat 'identity list "\n") "\n")))
  11676. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11677. (setq org-agenda-files list)
  11678. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11679. (defun org-read-agenda-file-list ()
  11680. "Read the list of agenda files from a file."
  11681. (when (stringp org-agenda-files)
  11682. (with-temp-buffer
  11683. (insert-file-contents org-agenda-files)
  11684. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11685. ;;;###autoload
  11686. (defun org-cycle-agenda-files ()
  11687. "Cycle through the files in `org-agenda-files'.
  11688. If the current buffer visits an agenda file, find the next one in the list.
  11689. If the current buffer does not, find the first agenda file."
  11690. (interactive)
  11691. (let* ((fs (org-agenda-files t))
  11692. (files (append fs (list (car fs))))
  11693. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11694. file)
  11695. (unless files (error "No agenda files"))
  11696. (catch 'exit
  11697. (while (setq file (pop files))
  11698. (if (equal (file-truename file) tcf)
  11699. (when (car files)
  11700. (find-file (car files))
  11701. (throw 'exit t))))
  11702. (find-file (car fs)))
  11703. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11704. (defun org-agenda-file-to-front (&optional to-end)
  11705. "Move/add the current file to the top of the agenda file list.
  11706. If the file is not present in the list, it is added to the front. If it is
  11707. present, it is moved there. With optional argument TO-END, add/move to the
  11708. end of the list."
  11709. (interactive "P")
  11710. (let ((org-agenda-skip-unavailable-files nil)
  11711. (file-alist (mapcar (lambda (x)
  11712. (cons (file-truename x) x))
  11713. (org-agenda-files t)))
  11714. (ctf (file-truename buffer-file-name))
  11715. x had)
  11716. (setq x (assoc ctf file-alist) had x)
  11717. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11718. (if to-end
  11719. (setq file-alist (append (delq x file-alist) (list x)))
  11720. (setq file-alist (cons x (delq x file-alist))))
  11721. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11722. (org-install-agenda-files-menu)
  11723. (message "File %s to %s of agenda file list"
  11724. (if had "moved" "added") (if to-end "end" "front"))))
  11725. (defun org-remove-file (&optional file)
  11726. "Remove current file from the list of files in variable `org-agenda-files'.
  11727. These are the files which are being checked for agenda entries.
  11728. Optional argument FILE means, use this file instead of the current."
  11729. (interactive)
  11730. (let* ((org-agenda-skip-unavailable-files nil)
  11731. (file (or file buffer-file-name))
  11732. (true-file (file-truename file))
  11733. (afile (abbreviate-file-name file))
  11734. (files (delq nil (mapcar
  11735. (lambda (x)
  11736. (if (equal true-file
  11737. (file-truename x))
  11738. nil x))
  11739. (org-agenda-files t)))))
  11740. (if (not (= (length files) (length (org-agenda-files t))))
  11741. (progn
  11742. (org-store-new-agenda-file-list files)
  11743. (org-install-agenda-files-menu)
  11744. (message "Removed file: %s" afile))
  11745. (message "File was not in list: %s (not removed)" afile))))
  11746. (defun org-file-menu-entry (file)
  11747. (vector file (list 'find-file file) t))
  11748. (defun org-check-agenda-file (file)
  11749. "Make sure FILE exists. If not, ask user what to do."
  11750. (when (not (file-exists-p file))
  11751. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11752. (abbreviate-file-name file))
  11753. (let ((r (downcase (read-char-exclusive))))
  11754. (cond
  11755. ((equal r ?r)
  11756. (org-remove-file file)
  11757. (throw 'nextfile t))
  11758. (t (error "Abort"))))))
  11759. (defun org-get-agenda-file-buffer (file)
  11760. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11761. it to the list of buffers which might be released later."
  11762. (let ((buf (org-find-base-buffer-visiting file)))
  11763. (if buf
  11764. buf ; just return it
  11765. ;; Make a new buffer and remember it
  11766. (setq buf (find-file-noselect file))
  11767. (if buf (push buf org-agenda-new-buffers))
  11768. buf)))
  11769. (defun org-release-buffers (blist)
  11770. "Release all buffers in list, asking the user for confirmation when needed.
  11771. When a buffer is unmodified, it is just killed. When modified, it is saved
  11772. \(if the user agrees) and then killed."
  11773. (let (buf file)
  11774. (while (setq buf (pop blist))
  11775. (setq file (buffer-file-name buf))
  11776. (when (and (buffer-modified-p buf)
  11777. file
  11778. (y-or-n-p (format "Save file %s? " file)))
  11779. (with-current-buffer buf (save-buffer)))
  11780. (kill-buffer buf))))
  11781. (defun org-prepare-agenda-buffers (files)
  11782. "Create buffers for all agenda files, protect archived trees and comments."
  11783. (interactive)
  11784. (let ((pa '(:org-archived t))
  11785. (pc '(:org-comment t))
  11786. (pall '(:org-archived t :org-comment t))
  11787. (inhibit-read-only t)
  11788. (rea (concat ":" org-archive-tag ":"))
  11789. bmp file re)
  11790. (save-excursion
  11791. (save-restriction
  11792. (while (setq file (pop files))
  11793. (if (bufferp file)
  11794. (set-buffer file)
  11795. (org-check-agenda-file file)
  11796. (set-buffer (org-get-agenda-file-buffer file)))
  11797. (widen)
  11798. (setq bmp (buffer-modified-p))
  11799. (org-refresh-category-properties)
  11800. (setq org-todo-keywords-for-agenda
  11801. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11802. (setq org-done-keywords-for-agenda
  11803. (append org-done-keywords-for-agenda org-done-keywords))
  11804. (save-excursion
  11805. (remove-text-properties (point-min) (point-max) pall)
  11806. (when org-agenda-skip-archived-trees
  11807. (goto-char (point-min))
  11808. (while (re-search-forward rea nil t)
  11809. (if (org-on-heading-p t)
  11810. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11811. (goto-char (point-min))
  11812. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11813. (while (re-search-forward re nil t)
  11814. (add-text-properties
  11815. (match-beginning 0) (org-end-of-subtree t) pc)))
  11816. (set-buffer-modified-p bmp))))))
  11817. ;;;; Embedded LaTeX
  11818. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11819. "Keymap for the minor `org-cdlatex-mode'.")
  11820. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11821. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11822. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11823. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11824. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11825. (defvar org-cdlatex-texmathp-advice-is-done nil
  11826. "Flag remembering if we have applied the advice to texmathp already.")
  11827. (define-minor-mode org-cdlatex-mode
  11828. "Toggle the minor `org-cdlatex-mode'.
  11829. This mode supports entering LaTeX environment and math in LaTeX fragments
  11830. in Org-mode.
  11831. \\{org-cdlatex-mode-map}"
  11832. nil " OCDL" nil
  11833. (when org-cdlatex-mode (require 'cdlatex))
  11834. (unless org-cdlatex-texmathp-advice-is-done
  11835. (setq org-cdlatex-texmathp-advice-is-done t)
  11836. (defadvice texmathp (around org-math-always-on activate)
  11837. "Always return t in org-mode buffers.
  11838. This is because we want to insert math symbols without dollars even outside
  11839. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11840. en embedded LaTeX fragement, let texmathp do its job.
  11841. \\[org-cdlatex-mode-map]"
  11842. (interactive)
  11843. (let (p)
  11844. (cond
  11845. ((not (org-mode-p)) ad-do-it)
  11846. ((eq this-command 'cdlatex-math-symbol)
  11847. (setq ad-return-value t
  11848. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11849. (t
  11850. (let ((p (org-inside-LaTeX-fragment-p)))
  11851. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11852. (setq ad-return-value t
  11853. texmathp-why '("Org-mode embedded math" . 0))
  11854. (if p ad-do-it)))))))))
  11855. (defun turn-on-org-cdlatex ()
  11856. "Unconditionally turn on `org-cdlatex-mode'."
  11857. (org-cdlatex-mode 1))
  11858. (defun org-inside-LaTeX-fragment-p ()
  11859. "Test if point is inside a LaTeX fragment.
  11860. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11861. sequence appearing also before point.
  11862. Even though the matchers for math are configurable, this function assumes
  11863. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11864. delimiters are skipped when they have been removed by customization.
  11865. The return value is nil, or a cons cell with the delimiter and
  11866. and the position of this delimiter.
  11867. This function does a reasonably good job, but can locally be fooled by
  11868. for example currency specifications. For example it will assume being in
  11869. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11870. fragments that are properly closed, but during editing, we have to live
  11871. with the uncertainty caused by missing closing delimiters. This function
  11872. looks only before point, not after."
  11873. (catch 'exit
  11874. (let ((pos (point))
  11875. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11876. (lim (progn
  11877. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11878. (point)))
  11879. dd-on str (start 0) m re)
  11880. (goto-char pos)
  11881. (when dodollar
  11882. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11883. re (nth 1 (assoc "$" org-latex-regexps)))
  11884. (while (string-match re str start)
  11885. (cond
  11886. ((= (match-end 0) (length str))
  11887. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11888. ((= (match-end 0) (- (length str) 5))
  11889. (throw 'exit nil))
  11890. (t (setq start (match-end 0))))))
  11891. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11892. (goto-char pos)
  11893. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11894. (and (match-beginning 2) (throw 'exit nil))
  11895. ;; count $$
  11896. (while (re-search-backward "\\$\\$" lim t)
  11897. (setq dd-on (not dd-on)))
  11898. (goto-char pos)
  11899. (if dd-on (cons "$$" m))))))
  11900. (defun org-try-cdlatex-tab ()
  11901. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11902. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11903. - inside a LaTeX fragment, or
  11904. - after the first word in a line, where an abbreviation expansion could
  11905. insert a LaTeX environment."
  11906. (when org-cdlatex-mode
  11907. (cond
  11908. ((save-excursion
  11909. (skip-chars-backward "a-zA-Z0-9*")
  11910. (skip-chars-backward " \t")
  11911. (bolp))
  11912. (cdlatex-tab) t)
  11913. ((org-inside-LaTeX-fragment-p)
  11914. (cdlatex-tab) t)
  11915. (t nil))))
  11916. (defun org-cdlatex-underscore-caret (&optional arg)
  11917. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11918. Revert to the normal definition outside of these fragments."
  11919. (interactive "P")
  11920. (if (org-inside-LaTeX-fragment-p)
  11921. (call-interactively 'cdlatex-sub-superscript)
  11922. (let (org-cdlatex-mode)
  11923. (call-interactively (key-binding (vector last-input-event))))))
  11924. (defun org-cdlatex-math-modify (&optional arg)
  11925. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11926. Revert to the normal definition outside of these fragments."
  11927. (interactive "P")
  11928. (if (org-inside-LaTeX-fragment-p)
  11929. (call-interactively 'cdlatex-math-modify)
  11930. (let (org-cdlatex-mode)
  11931. (call-interactively (key-binding (vector last-input-event))))))
  11932. (defvar org-latex-fragment-image-overlays nil
  11933. "List of overlays carrying the images of latex fragments.")
  11934. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11935. (defun org-remove-latex-fragment-image-overlays ()
  11936. "Remove all overlays with LaTeX fragment images in current buffer."
  11937. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11938. (setq org-latex-fragment-image-overlays nil))
  11939. (defun org-preview-latex-fragment (&optional subtree)
  11940. "Preview the LaTeX fragment at point, or all locally or globally.
  11941. If the cursor is in a LaTeX fragment, create the image and overlay
  11942. it over the source code. If there is no fragment at point, display
  11943. all fragments in the current text, from one headline to the next. With
  11944. prefix SUBTREE, display all fragments in the current subtree. With a
  11945. double prefix `C-u C-u', or when the cursor is before the first headline,
  11946. display all fragments in the buffer.
  11947. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11948. (interactive "P")
  11949. (org-remove-latex-fragment-image-overlays)
  11950. (save-excursion
  11951. (save-restriction
  11952. (let (beg end at msg)
  11953. (cond
  11954. ((or (equal subtree '(16))
  11955. (not (save-excursion
  11956. (re-search-backward (concat "^" outline-regexp) nil t))))
  11957. (setq beg (point-min) end (point-max)
  11958. msg "Creating images for buffer...%s"))
  11959. ((equal subtree '(4))
  11960. (org-back-to-heading)
  11961. (setq beg (point) end (org-end-of-subtree t)
  11962. msg "Creating images for subtree...%s"))
  11963. (t
  11964. (if (setq at (org-inside-LaTeX-fragment-p))
  11965. (goto-char (max (point-min) (- (cdr at) 2)))
  11966. (org-back-to-heading))
  11967. (setq beg (point) end (progn (outline-next-heading) (point))
  11968. msg (if at "Creating image...%s"
  11969. "Creating images for entry...%s"))))
  11970. (message msg "")
  11971. (narrow-to-region beg end)
  11972. (goto-char beg)
  11973. (org-format-latex
  11974. (concat "ltxpng/" (file-name-sans-extension
  11975. (file-name-nondirectory
  11976. buffer-file-name)))
  11977. default-directory 'overlays msg at 'forbuffer)
  11978. (message msg "done. Use `C-c C-c' to remove images.")))))
  11979. (defvar org-latex-regexps
  11980. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11981. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11982. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11983. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11984. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11985. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11986. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11987. "Regular expressions for matching embedded LaTeX.")
  11988. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11989. "Replace LaTeX fragments with links to an image, and produce images."
  11990. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11991. (let* ((prefixnodir (file-name-nondirectory prefix))
  11992. (absprefix (expand-file-name prefix dir))
  11993. (todir (file-name-directory absprefix))
  11994. (opt org-format-latex-options)
  11995. (matchers (plist-get opt :matchers))
  11996. (re-list org-latex-regexps)
  11997. (cnt 0) txt link beg end re e checkdir
  11998. m n block linkfile movefile ov)
  11999. ;; Check if there are old images files with this prefix, and remove them
  12000. (when (file-directory-p todir)
  12001. (mapc 'delete-file
  12002. (directory-files
  12003. todir 'full
  12004. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12005. ;; Check the different regular expressions
  12006. (while (setq e (pop re-list))
  12007. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12008. block (if (nth 3 e) "\n\n" ""))
  12009. (when (member m matchers)
  12010. (goto-char (point-min))
  12011. (while (re-search-forward re nil t)
  12012. (when (or (not at) (equal (cdr at) (match-beginning n)))
  12013. (setq txt (match-string n)
  12014. beg (match-beginning n) end (match-end n)
  12015. cnt (1+ cnt)
  12016. linkfile (format "%s_%04d.png" prefix cnt)
  12017. movefile (format "%s_%04d.png" absprefix cnt)
  12018. link (concat block "[[file:" linkfile "]]" block))
  12019. (if msg (message msg cnt))
  12020. (goto-char beg)
  12021. (unless checkdir ; make sure the directory exists
  12022. (setq checkdir t)
  12023. (or (file-directory-p todir) (make-directory todir)))
  12024. (org-create-formula-image
  12025. txt movefile opt forbuffer)
  12026. (if overlays
  12027. (progn
  12028. (setq ov (org-make-overlay beg end))
  12029. (if (featurep 'xemacs)
  12030. (progn
  12031. (org-overlay-put ov 'invisible t)
  12032. (org-overlay-put
  12033. ov 'end-glyph
  12034. (make-glyph (vector 'png :file movefile))))
  12035. (org-overlay-put
  12036. ov 'display
  12037. (list 'image :type 'png :file movefile :ascent 'center)))
  12038. (push ov org-latex-fragment-image-overlays)
  12039. (goto-char end))
  12040. (delete-region beg end)
  12041. (insert link))))))))
  12042. ;; This function borrows from Ganesh Swami's latex2png.el
  12043. (defun org-create-formula-image (string tofile options buffer)
  12044. (let* ((tmpdir (if (featurep 'xemacs)
  12045. (temp-directory)
  12046. temporary-file-directory))
  12047. (texfilebase (make-temp-name
  12048. (expand-file-name "orgtex" tmpdir)))
  12049. (texfile (concat texfilebase ".tex"))
  12050. (dvifile (concat texfilebase ".dvi"))
  12051. (pngfile (concat texfilebase ".png"))
  12052. (fnh (face-attribute 'default :height nil))
  12053. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12054. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12055. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12056. "Black"))
  12057. (bg (or (plist-get options (if buffer :background :html-background))
  12058. "Transparent")))
  12059. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12060. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12061. (with-temp-file texfile
  12062. (insert org-format-latex-header
  12063. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12064. (let ((dir default-directory))
  12065. (condition-case nil
  12066. (progn
  12067. (cd tmpdir)
  12068. (call-process "latex" nil nil nil texfile))
  12069. (error nil))
  12070. (cd dir))
  12071. (if (not (file-exists-p dvifile))
  12072. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12073. (call-process "dvipng" nil nil nil
  12074. "-E" "-fg" fg "-bg" bg
  12075. "-D" dpi
  12076. ;;"-x" scale "-y" scale
  12077. "-T" "tight"
  12078. "-o" pngfile
  12079. dvifile)
  12080. (if (not (file-exists-p pngfile))
  12081. (progn (message "Failed to create png file from %s" texfile) nil)
  12082. ;; Use the requested file name and clean up
  12083. (copy-file pngfile tofile 'replace)
  12084. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12085. (delete-file (concat texfilebase e)))
  12086. pngfile))))
  12087. (defun org-dvipng-color (attr)
  12088. "Return an rgb color specification for dvipng."
  12089. (apply 'format "rgb %s %s %s"
  12090. (mapcar 'org-normalize-color
  12091. (color-values (face-attribute 'default attr nil)))))
  12092. (defun org-normalize-color (value)
  12093. "Return string to be used as color value for an RGB component."
  12094. (format "%g" (/ value 65535.0)))
  12095. ;;;; Key bindings
  12096. ;; Make `C-c C-x' a prefix key
  12097. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12098. ;; TAB key with modifiers
  12099. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12100. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12101. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12102. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12103. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12104. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12105. ;; The following line is necessary under Suse GNU/Linux
  12106. (unless (featurep 'xemacs)
  12107. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12108. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12109. (define-key org-mode-map [backtab] 'org-shifttab)
  12110. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12111. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12112. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12113. ;; Cursor keys with modifiers
  12114. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12115. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12116. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12117. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12118. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12119. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12120. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12121. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12122. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12123. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12124. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12125. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12126. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12127. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12128. ;;; Extra keys for tty access.
  12129. ;; We only set them when really needed because otherwise the
  12130. ;; menus don't show the simple keys
  12131. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12132. (not window-system))
  12133. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12134. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12135. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12136. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12137. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12138. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12139. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12140. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12141. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12142. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12143. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12144. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12145. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12146. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12147. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12148. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12149. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12150. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12151. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12152. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12153. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12154. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12155. ;; All the other keys
  12156. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12157. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12158. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  12159. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12160. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12161. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12162. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12163. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12164. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12165. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12166. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12167. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12168. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12169. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12170. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12171. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12172. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12173. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12174. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  12175. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12176. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12177. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12178. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12179. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12180. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12181. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12182. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12183. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12184. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12185. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12186. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12187. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12188. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12189. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12190. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12191. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12192. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12193. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12194. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12195. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12196. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12197. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12198. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12199. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12200. (org-defkey org-mode-map "\C-m" 'org-return)
  12201. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12202. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12203. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12204. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12205. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12206. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  12207. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12208. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12209. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12210. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12211. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12212. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12213. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12214. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12215. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12216. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12217. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12218. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12219. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12220. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12221. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12222. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12223. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12224. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12225. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12226. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12227. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12228. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12229. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12230. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12231. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12232. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  12233. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12234. (when (featurep 'xemacs)
  12235. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12236. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12237. (defun org-self-insert-command (N)
  12238. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12239. If the cursor is in a table looking at whitespace, the whitespace is
  12240. overwritten, and the table is not marked as requiring realignment."
  12241. (interactive "p")
  12242. (if (and (org-table-p)
  12243. (progn
  12244. ;; check if we blank the field, and if that triggers align
  12245. (and (featurep 'org-table) org-table-auto-blank-field
  12246. (member last-command
  12247. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12248. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12249. ;; got extra space, this field does not determine column width
  12250. (let (org-table-may-need-update) (org-table-blank-field))
  12251. ;; no extra space, this field may determine column width
  12252. (org-table-blank-field)))
  12253. t)
  12254. (eq N 1)
  12255. (looking-at "[^|\n]* |"))
  12256. (let (org-table-may-need-update)
  12257. (goto-char (1- (match-end 0)))
  12258. (delete-backward-char 1)
  12259. (goto-char (match-beginning 0))
  12260. (self-insert-command N))
  12261. (setq org-table-may-need-update t)
  12262. (self-insert-command N)
  12263. (org-fix-tags-on-the-fly)))
  12264. (defun org-fix-tags-on-the-fly ()
  12265. (when (and (equal (char-after (point-at-bol)) ?*)
  12266. (org-on-heading-p))
  12267. (org-align-tags-here org-tags-column)))
  12268. (defun org-delete-backward-char (N)
  12269. "Like `delete-backward-char', insert whitespace at field end in tables.
  12270. When deleting backwards, in tables this function will insert whitespace in
  12271. front of the next \"|\" separator, to keep the table aligned. The table will
  12272. still be marked for re-alignment if the field did fill the entire column,
  12273. because, in this case the deletion might narrow the column."
  12274. (interactive "p")
  12275. (if (and (org-table-p)
  12276. (eq N 1)
  12277. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12278. (looking-at ".*?|"))
  12279. (let ((pos (point))
  12280. (noalign (looking-at "[^|\n\r]* |"))
  12281. (c org-table-may-need-update))
  12282. (backward-delete-char N)
  12283. (skip-chars-forward "^|")
  12284. (insert " ")
  12285. (goto-char (1- pos))
  12286. ;; noalign: if there were two spaces at the end, this field
  12287. ;; does not determine the width of the column.
  12288. (if noalign (setq org-table-may-need-update c)))
  12289. (backward-delete-char N)
  12290. (org-fix-tags-on-the-fly)))
  12291. (defun org-delete-char (N)
  12292. "Like `delete-char', but insert whitespace at field end in tables.
  12293. When deleting characters, in tables this function will insert whitespace in
  12294. front of the next \"|\" separator, to keep the table aligned. The table will
  12295. still be marked for re-alignment if the field did fill the entire column,
  12296. because, in this case the deletion might narrow the column."
  12297. (interactive "p")
  12298. (if (and (org-table-p)
  12299. (not (bolp))
  12300. (not (= (char-after) ?|))
  12301. (eq N 1))
  12302. (if (looking-at ".*?|")
  12303. (let ((pos (point))
  12304. (noalign (looking-at "[^|\n\r]* |"))
  12305. (c org-table-may-need-update))
  12306. (replace-match (concat
  12307. (substring (match-string 0) 1 -1)
  12308. " |"))
  12309. (goto-char pos)
  12310. ;; noalign: if there were two spaces at the end, this field
  12311. ;; does not determine the width of the column.
  12312. (if noalign (setq org-table-may-need-update c)))
  12313. (delete-char N))
  12314. (delete-char N)
  12315. (org-fix-tags-on-the-fly)))
  12316. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12317. (put 'org-self-insert-command 'delete-selection t)
  12318. (put 'orgtbl-self-insert-command 'delete-selection t)
  12319. (put 'org-delete-char 'delete-selection 'supersede)
  12320. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12321. ;; Make `flyspell-mode' delay after some commands
  12322. (put 'org-self-insert-command 'flyspell-delayed t)
  12323. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12324. (put 'org-delete-char 'flyspell-delayed t)
  12325. (put 'org-delete-backward-char 'flyspell-delayed t)
  12326. ;; Make pabbrev-mode expand after org-mode commands
  12327. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12328. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  12329. ;; How to do this: Measure non-white length of current string
  12330. ;; If equal to column width, we should realign.
  12331. (defun org-remap (map &rest commands)
  12332. "In MAP, remap the functions given in COMMANDS.
  12333. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12334. (let (new old)
  12335. (while commands
  12336. (setq old (pop commands) new (pop commands))
  12337. (if (fboundp 'command-remapping)
  12338. (org-defkey map (vector 'remap old) new)
  12339. (substitute-key-definition old new map global-map)))))
  12340. (when (eq org-enable-table-editor 'optimized)
  12341. ;; If the user wants maximum table support, we need to hijack
  12342. ;; some standard editing functions
  12343. (org-remap org-mode-map
  12344. 'self-insert-command 'org-self-insert-command
  12345. 'delete-char 'org-delete-char
  12346. 'delete-backward-char 'org-delete-backward-char)
  12347. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12348. (defun org-shiftcursor-error ()
  12349. "Throw an error because Shift-Cursor command was applied in wrong context."
  12350. (error "This command is active in special context like tables, headlines or timestamps"))
  12351. (defun org-shifttab (&optional arg)
  12352. "Global visibility cycling or move to previous table field.
  12353. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12354. on context.
  12355. See the individual commands for more information."
  12356. (interactive "P")
  12357. (cond
  12358. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12359. (arg (message "Content view to level: ")
  12360. (org-content (prefix-numeric-value arg))
  12361. (setq org-cycle-global-status 'overview))
  12362. (t (call-interactively 'org-global-cycle))))
  12363. (defun org-shiftmetaleft ()
  12364. "Promote subtree or delete table column.
  12365. Calls `org-promote-subtree', `org-outdent-item',
  12366. or `org-table-delete-column', depending on context.
  12367. See the individual commands for more information."
  12368. (interactive)
  12369. (cond
  12370. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12371. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12372. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12373. (t (org-shiftcursor-error))))
  12374. (defun org-shiftmetaright ()
  12375. "Demote subtree or insert table column.
  12376. Calls `org-demote-subtree', `org-indent-item',
  12377. or `org-table-insert-column', depending on context.
  12378. See the individual commands for more information."
  12379. (interactive)
  12380. (cond
  12381. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12382. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12383. ((org-at-item-p) (call-interactively 'org-indent-item))
  12384. (t (org-shiftcursor-error))))
  12385. (defun org-shiftmetaup (&optional arg)
  12386. "Move subtree up or kill table row.
  12387. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12388. `org-move-item-up' depending on context. See the individual commands
  12389. for more information."
  12390. (interactive "P")
  12391. (cond
  12392. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12393. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12394. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12395. (t (org-shiftcursor-error))))
  12396. (defun org-shiftmetadown (&optional arg)
  12397. "Move subtree down or insert table row.
  12398. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12399. `org-move-item-down', depending on context. See the individual
  12400. commands for more information."
  12401. (interactive "P")
  12402. (cond
  12403. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12404. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12405. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12406. (t (org-shiftcursor-error))))
  12407. (defun org-metaleft (&optional arg)
  12408. "Promote heading or move table column to left.
  12409. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12410. With no specific context, calls the Emacs default `backward-word'.
  12411. See the individual commands for more information."
  12412. (interactive "P")
  12413. (cond
  12414. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12415. ((or (org-on-heading-p) (org-region-active-p))
  12416. (call-interactively 'org-do-promote))
  12417. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12418. (t (call-interactively 'backward-word))))
  12419. (defun org-metaright (&optional arg)
  12420. "Demote subtree or move table column to right.
  12421. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12422. With no specific context, calls the Emacs default `forward-word'.
  12423. See the individual commands for more information."
  12424. (interactive "P")
  12425. (cond
  12426. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12427. ((or (org-on-heading-p) (org-region-active-p))
  12428. (call-interactively 'org-do-demote))
  12429. ((org-at-item-p) (call-interactively 'org-indent-item))
  12430. (t (call-interactively 'forward-word))))
  12431. (defun org-metaup (&optional arg)
  12432. "Move subtree up or move table row up.
  12433. Calls `org-move-subtree-up' or `org-table-move-row' or
  12434. `org-move-item-up', depending on context. See the individual commands
  12435. for more information."
  12436. (interactive "P")
  12437. (cond
  12438. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12439. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12440. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12441. (t (transpose-lines 1) (beginning-of-line -1))))
  12442. (defun org-metadown (&optional arg)
  12443. "Move subtree down or move table row down.
  12444. Calls `org-move-subtree-down' or `org-table-move-row' or
  12445. `org-move-item-down', depending on context. See the individual
  12446. commands for more information."
  12447. (interactive "P")
  12448. (cond
  12449. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12450. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12451. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12452. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12453. (defun org-shiftup (&optional arg)
  12454. "Increase item in timestamp or increase priority of current headline.
  12455. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12456. depending on context. See the individual commands for more information."
  12457. (interactive "P")
  12458. (cond
  12459. ((org-at-timestamp-p t)
  12460. (call-interactively (if org-edit-timestamp-down-means-later
  12461. 'org-timestamp-down 'org-timestamp-up)))
  12462. ((org-on-heading-p) (call-interactively 'org-priority-up))
  12463. ((org-at-item-p) (call-interactively 'org-previous-item))
  12464. ((org-clocktable-try-shift 'up arg))
  12465. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  12466. (defun org-shiftdown (&optional arg)
  12467. "Decrease item in timestamp or decrease priority of current headline.
  12468. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12469. depending on context. See the individual commands for more information."
  12470. (interactive "P")
  12471. (cond
  12472. ((org-at-timestamp-p t)
  12473. (call-interactively (if org-edit-timestamp-down-means-later
  12474. 'org-timestamp-up 'org-timestamp-down)))
  12475. ((org-on-heading-p) (call-interactively 'org-priority-down))
  12476. ((org-clocktable-try-shift 'down arg))
  12477. (t (call-interactively 'org-next-item))))
  12478. (defun org-shiftright (&optional arg)
  12479. "Next TODO keyword or timestamp one day later, depending on context."
  12480. (interactive "P")
  12481. (cond
  12482. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12483. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  12484. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  12485. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  12486. ((org-clocktable-try-shift 'right arg))
  12487. (t (org-shiftcursor-error))))
  12488. (defun org-shiftleft (&optional arg)
  12489. "Previous TODO keyword or timestamp one day earlier, depending on context."
  12490. (interactive "P")
  12491. (cond
  12492. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12493. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  12494. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12495. ((org-at-property-p)
  12496. (call-interactively 'org-property-previous-allowed-value))
  12497. ((org-clocktable-try-shift 'left arg))
  12498. (t (org-shiftcursor-error))))
  12499. (defun org-shiftcontrolright ()
  12500. "Switch to next TODO set."
  12501. (interactive)
  12502. (cond
  12503. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  12504. (t (org-shiftcursor-error))))
  12505. (defun org-shiftcontrolleft ()
  12506. "Switch to previous TODO set."
  12507. (interactive)
  12508. (cond
  12509. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  12510. (t (org-shiftcursor-error))))
  12511. (defun org-ctrl-c-ret ()
  12512. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12513. (interactive)
  12514. (cond
  12515. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12516. (t (call-interactively 'org-insert-heading))))
  12517. (defun org-copy-special ()
  12518. "Copy region in table or copy current subtree.
  12519. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12520. See the individual commands for more information."
  12521. (interactive)
  12522. (call-interactively
  12523. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12524. (defun org-cut-special ()
  12525. "Cut region in table or cut current subtree.
  12526. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12527. See the individual commands for more information."
  12528. (interactive)
  12529. (call-interactively
  12530. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12531. (defun org-paste-special (arg)
  12532. "Paste rectangular region into table, or past subtree relative to level.
  12533. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12534. See the individual commands for more information."
  12535. (interactive "P")
  12536. (if (org-at-table-p)
  12537. (org-table-paste-rectangle)
  12538. (org-paste-subtree arg)))
  12539. (defun org-ctrl-c-ctrl-c (&optional arg)
  12540. "Set tags in headline, or update according to changed information at point.
  12541. This command does many different things, depending on context:
  12542. - If the cursor is in a headline, prompt for tags and insert them
  12543. into the current line, aligned to `org-tags-column'. When called
  12544. with prefix arg, realign all tags in the current buffer.
  12545. - If the cursor is in one of the special #+KEYWORD lines, this
  12546. triggers scanning the buffer for these lines and updating the
  12547. information.
  12548. - If the cursor is inside a table, realign the table. This command
  12549. works even if the automatic table editor has been turned off.
  12550. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12551. the entire table.
  12552. - If the cursor is a the beginning of a dynamic block, update it.
  12553. - If the cursor is inside a table created by the table.el package,
  12554. activate that table.
  12555. - If the current buffer is a remember buffer, close note and file it.
  12556. with a prefix argument, file it without further interaction to the default
  12557. location.
  12558. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12559. links in this buffer.
  12560. - If the cursor is on a numbered item in a plain list, renumber the
  12561. ordered list.
  12562. - If the cursor is on a checkbox, toggle it."
  12563. (interactive "P")
  12564. (let ((org-enable-table-editor t))
  12565. (cond
  12566. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12567. org-occur-highlights
  12568. org-latex-fragment-image-overlays)
  12569. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  12570. (org-remove-occur-highlights)
  12571. (org-remove-latex-fragment-image-overlays)
  12572. (message "Temporary highlights/overlays removed from current buffer"))
  12573. ((and (local-variable-p 'org-finish-function (current-buffer))
  12574. (fboundp org-finish-function))
  12575. (funcall org-finish-function))
  12576. ((org-at-property-p)
  12577. (call-interactively 'org-property-action))
  12578. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12579. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12580. ((org-at-table.el-p)
  12581. (require 'table)
  12582. (beginning-of-line 1)
  12583. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12584. (call-interactively 'table-recognize-table))
  12585. ((org-at-table-p)
  12586. (org-table-maybe-eval-formula)
  12587. (if arg
  12588. (call-interactively 'org-table-recalculate)
  12589. (org-table-maybe-recalculate-line))
  12590. (call-interactively 'org-table-align))
  12591. ((org-at-item-checkbox-p)
  12592. (call-interactively 'org-toggle-checkbox))
  12593. ((org-at-item-p)
  12594. (call-interactively 'org-maybe-renumber-ordered-list))
  12595. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12596. ;; Dynamic block
  12597. (beginning-of-line 1)
  12598. (org-update-dblock))
  12599. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12600. (cond
  12601. ((equal (match-string 1) "TBLFM")
  12602. ;; Recalculate the table before this line
  12603. (save-excursion
  12604. (beginning-of-line 1)
  12605. (skip-chars-backward " \r\n\t")
  12606. (if (org-at-table-p)
  12607. (org-call-with-arg 'org-table-recalculate t))))
  12608. (t
  12609. (call-interactively 'org-mode-restart))))
  12610. (t (error "C-c C-c can do nothing useful at this location.")))))
  12611. (defun org-mode-restart ()
  12612. "Restart Org-mode, to scan again for special lines.
  12613. Also updates the keyword regular expressions."
  12614. (interactive)
  12615. (let ((org-inhibit-startup t)) (org-mode))
  12616. (message "Org-mode restarted to refresh keyword and special line setup"))
  12617. (defun org-kill-note-or-show-branches ()
  12618. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12619. (interactive)
  12620. (if (not org-finish-function)
  12621. (call-interactively 'show-branches)
  12622. (let ((org-note-abort t))
  12623. (funcall org-finish-function))))
  12624. (defun org-return (&optional indent)
  12625. "Goto next table row or insert a newline.
  12626. Calls `org-table-next-row' or `newline', depending on context.
  12627. See the individual commands for more information."
  12628. (interactive)
  12629. (cond
  12630. ((bobp) (if indent (newline-and-indent) (newline)))
  12631. ((and (org-at-heading-p)
  12632. (looking-at
  12633. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12634. (org-show-entry)
  12635. (end-of-line 1)
  12636. (newline))
  12637. ((org-at-table-p)
  12638. (org-table-justify-field-maybe)
  12639. (call-interactively 'org-table-next-row))
  12640. (t (if indent (newline-and-indent) (newline)))))
  12641. (defun org-return-indent ()
  12642. "Goto next table row or insert a newline and indent.
  12643. Calls `org-table-next-row' or `newline-and-indent', depending on
  12644. context. See the individual commands for more information."
  12645. (interactive)
  12646. (org-return t))
  12647. (defun org-ctrl-c-star ()
  12648. "Compute table, or change heading status of lines.
  12649. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  12650. depending on context. This will also turn a plain list item or a normal
  12651. line into a subheading."
  12652. (interactive)
  12653. (cond
  12654. ((org-at-table-p)
  12655. (call-interactively 'org-table-recalculate))
  12656. ((org-region-active-p)
  12657. ;; Convert all lines in region to list items
  12658. (call-interactively 'org-toggle-region-headings))
  12659. ((org-on-heading-p)
  12660. (org-toggle-region-headings (point-at-bol)
  12661. (min (1+ (point-at-eol)) (point-max))))
  12662. ((org-at-item-p)
  12663. ;; Convert to heading
  12664. (let ((level (save-match-data
  12665. (save-excursion
  12666. (condition-case nil
  12667. (progn
  12668. (org-back-to-heading t)
  12669. (funcall outline-level))
  12670. (error 0))))))
  12671. (replace-match
  12672. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12673. (t (org-toggle-region-headings (point-at-bol)
  12674. (min (1+ (point-at-eol)) (point-max))))))
  12675. (defun org-ctrl-c-minus ()
  12676. "Insert separator line in table or modify bullet status of line.
  12677. Also turns a plain line or a region of lines into list items.
  12678. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12679. `org-cycle-list-bullet', depending on context."
  12680. (interactive)
  12681. (cond
  12682. ((org-at-table-p)
  12683. (call-interactively 'org-table-insert-hline))
  12684. ((org-on-heading-p)
  12685. ;; Convert to item
  12686. (save-excursion
  12687. (beginning-of-line 1)
  12688. (if (looking-at "\\*+ ")
  12689. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12690. ((org-region-active-p)
  12691. ;; Convert all lines in region to list items
  12692. (call-interactively 'org-toggle-region-items))
  12693. ((org-in-item-p)
  12694. (call-interactively 'org-cycle-list-bullet))
  12695. (t (org-toggle-region-items (point-at-bol)
  12696. (min (1+ (point-at-eol)) (point-max))))))
  12697. (defun org-toggle-region-items (beg end)
  12698. "Convert all lines in region to list items.
  12699. If the first line is already an item, convert all list items in the region
  12700. to normal lines."
  12701. (interactive "r")
  12702. (let (l2 l)
  12703. (save-excursion
  12704. (goto-char end)
  12705. (setq l2 (org-current-line))
  12706. (goto-char beg)
  12707. (beginning-of-line 1)
  12708. (setq l (1- (org-current-line)))
  12709. (if (org-at-item-p)
  12710. ;; We already have items, de-itemize
  12711. (while (< (setq l (1+ l)) l2)
  12712. (when (org-at-item-p)
  12713. (goto-char (match-beginning 2))
  12714. (delete-region (match-beginning 2) (match-end 2))
  12715. (and (looking-at "[ \t]+") (replace-match "")))
  12716. (beginning-of-line 2))
  12717. (while (< (setq l (1+ l)) l2)
  12718. (unless (org-at-item-p)
  12719. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12720. (replace-match "\\1- \\2")))
  12721. (beginning-of-line 2))))))
  12722. (defun org-toggle-region-headings (beg end)
  12723. "Convert all lines in region to list items.
  12724. If the first line is already an item, convert all list items in the region
  12725. to normal lines."
  12726. (interactive "r")
  12727. (let (l2 l)
  12728. (save-excursion
  12729. (goto-char end)
  12730. (setq l2 (org-current-line))
  12731. (goto-char beg)
  12732. (beginning-of-line 1)
  12733. (setq l (1- (org-current-line)))
  12734. (if (org-on-heading-p)
  12735. ;; We already have headlines, de-star them
  12736. (while (< (setq l (1+ l)) l2)
  12737. (when (org-on-heading-p t)
  12738. (and (looking-at outline-regexp) (replace-match "")))
  12739. (beginning-of-line 2))
  12740. (let* ((stars (save-excursion
  12741. (re-search-backward org-complex-heading-regexp nil t)
  12742. (or (match-string 1) "*")))
  12743. (add-stars (if org-odd-levels-only "**" "*"))
  12744. (rpl (concat stars add-stars " \\2")))
  12745. (while (< (setq l (1+ l)) l2)
  12746. (unless (org-on-heading-p)
  12747. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12748. (replace-match rpl)))
  12749. (beginning-of-line 2)))))))
  12750. (defun org-meta-return (&optional arg)
  12751. "Insert a new heading or wrap a region in a table.
  12752. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12753. See the individual commands for more information."
  12754. (interactive "P")
  12755. (cond
  12756. ((org-at-table-p)
  12757. (call-interactively 'org-table-wrap-region))
  12758. (t (call-interactively 'org-insert-heading))))
  12759. ;;; Menu entries
  12760. ;; Define the Org-mode menus
  12761. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12762. '("Tbl"
  12763. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12764. ["Next Field" org-cycle (org-at-table-p)]
  12765. ["Previous Field" org-shifttab (org-at-table-p)]
  12766. ["Next Row" org-return (org-at-table-p)]
  12767. "--"
  12768. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12769. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12770. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12771. "--"
  12772. ("Column"
  12773. ["Move Column Left" org-metaleft (org-at-table-p)]
  12774. ["Move Column Right" org-metaright (org-at-table-p)]
  12775. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12776. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12777. ("Row"
  12778. ["Move Row Up" org-metaup (org-at-table-p)]
  12779. ["Move Row Down" org-metadown (org-at-table-p)]
  12780. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12781. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12782. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12783. "--"
  12784. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12785. ("Rectangle"
  12786. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12787. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12788. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12789. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12790. "--"
  12791. ("Calculate"
  12792. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12793. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12794. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  12795. "--"
  12796. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12797. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12798. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12799. "--"
  12800. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12801. "--"
  12802. ["Sum Column/Rectangle" org-table-sum
  12803. (or (org-at-table-p) (org-region-active-p))]
  12804. ["Which Column?" org-table-current-column (org-at-table-p)])
  12805. ["Debug Formulas"
  12806. org-table-toggle-formula-debugger
  12807. :style toggle :selected org-table-formula-debug]
  12808. ["Show Col/Row Numbers"
  12809. org-table-toggle-coordinate-overlays
  12810. :style toggle :selected org-table-overlay-coordinates]
  12811. "--"
  12812. ["Create" org-table-create (and (not (org-at-table-p))
  12813. org-enable-table-editor)]
  12814. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12815. ["Import from File" org-table-import (not (org-at-table-p))]
  12816. ["Export to File" org-table-export (org-at-table-p)]
  12817. "--"
  12818. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12819. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12820. '("Org"
  12821. ("Show/Hide"
  12822. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))
  12823. :help "Cycle subtree visibility: folded->children->all->folded"]
  12824. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))
  12825. :help "Cycle global visibility: overview->content->all"]
  12826. ["Sparse Tree..." org-sparse-tree
  12827. :help "Create sparse trees using different search criteria"]
  12828. ["Reveal Context" org-reveal :active t
  12829. :help "Show hidden context around point, including the outline hierarchy"]
  12830. ["Show All" show-all :active t
  12831. :help "Show all text in the buffer, including drawers"]
  12832. "--"
  12833. ["Subtree to indirect buffer" org-tree-to-indirect-buffer :active t
  12834. :help "Open the subtree at point in a separate window, using an indirect buffer"])
  12835. "--"
  12836. ["New Heading" org-insert-heading t]
  12837. ("Navigate Headings"
  12838. ["Up" outline-up-heading t]
  12839. ["Next" outline-next-visible-heading t]
  12840. ["Previous" outline-previous-visible-heading t]
  12841. ["Next Same Level" outline-forward-same-level t]
  12842. ["Previous Same Level" outline-backward-same-level t]
  12843. "--"
  12844. ["Jump" org-goto t])
  12845. ("Edit Structure"
  12846. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12847. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12848. "--"
  12849. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12850. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12851. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12852. "--"
  12853. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12854. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12855. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12856. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12857. "--"
  12858. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12859. "--"
  12860. ["Convert to odd levels" org-convert-to-odd-levels t]
  12861. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12862. ("Editing"
  12863. ["Emphasis..." org-emphasize t])
  12864. ("Archive"
  12865. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12866. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12867. ; :active t :keys "C-u C-c C-x C-a"]
  12868. ["Sparse trees open ARCHIVE trees"
  12869. (setq org-sparse-tree-open-archived-trees
  12870. (not org-sparse-tree-open-archived-trees))
  12871. :style toggle :selected org-sparse-tree-open-archived-trees]
  12872. ["Cycling opens ARCHIVE trees"
  12873. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12874. :style toggle :selected org-cycle-open-archived-trees]
  12875. ["Agenda includes ARCHIVE trees"
  12876. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  12877. :style toggle :selected (not org-agenda-skip-archived-trees)]
  12878. "--"
  12879. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12880. ; ["Check and Move Children" (org-archive-subtree '(4))
  12881. ; :active t :keys "C-u C-c C-x C-s"]
  12882. )
  12883. "--"
  12884. ("TODO Lists"
  12885. ["TODO/DONE/-" org-todo t]
  12886. ("Select keyword"
  12887. ["Next keyword" org-shiftright (org-on-heading-p)]
  12888. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12889. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12890. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12891. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12892. ["Show TODO Tree" org-show-todo-tree t]
  12893. ["Global TODO list" org-todo-list t]
  12894. "--"
  12895. ["Set Priority" org-priority t]
  12896. ["Priority Up" org-shiftup t]
  12897. ["Priority Down" org-shiftdown t])
  12898. ("TAGS and Properties"
  12899. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  12900. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12901. "--"
  12902. ["Set property" 'org-set-property t]
  12903. ["Column view of properties" org-columns t]
  12904. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12905. ("Dates and Scheduling"
  12906. ["Timestamp" org-time-stamp t]
  12907. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12908. ("Change Date"
  12909. ["1 Day Later" org-shiftright t]
  12910. ["1 Day Earlier" org-shiftleft t]
  12911. ["1 ... Later" org-shiftup t]
  12912. ["1 ... Earlier" org-shiftdown t])
  12913. ["Compute Time Range" org-evaluate-time-range t]
  12914. ["Schedule Item" org-schedule t]
  12915. ["Deadline" org-deadline t]
  12916. "--"
  12917. ["Custom time format" org-toggle-time-stamp-overlays
  12918. :style radio :selected org-display-custom-times]
  12919. "--"
  12920. ["Goto Calendar" org-goto-calendar t]
  12921. ["Date from Calendar" org-date-from-calendar t])
  12922. ("Logging work"
  12923. ["Clock in" org-clock-in t]
  12924. ["Clock out" org-clock-out t]
  12925. ["Clock cancel" org-clock-cancel t]
  12926. ["Goto running clock" org-clock-goto t]
  12927. ["Display times" org-clock-display t]
  12928. ["Create clock table" org-clock-report t]
  12929. "--"
  12930. ["Record DONE time"
  12931. (progn (setq org-log-done (not org-log-done))
  12932. (message "Switching to %s will %s record a timestamp"
  12933. (car org-done-keywords)
  12934. (if org-log-done "automatically" "not")))
  12935. :style toggle :selected org-log-done])
  12936. "--"
  12937. ["Agenda Command..." org-agenda t]
  12938. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12939. ("File List for Agenda")
  12940. ("Special views current file"
  12941. ["TODO Tree" org-show-todo-tree t]
  12942. ["Check Deadlines" org-check-deadlines t]
  12943. ["Timeline" org-timeline t]
  12944. ["Tags Tree" org-tags-sparse-tree t])
  12945. "--"
  12946. ("Hyperlinks"
  12947. ["Store Link (Global)" org-store-link t]
  12948. ["Insert Link" org-insert-link t]
  12949. ["Follow Link" org-open-at-point t]
  12950. "--"
  12951. ["Next link" org-next-link :help "Move forward to next link in the buffer"]
  12952. ["Previous link" org-previous-link t]
  12953. "--"
  12954. ["Descriptive Links"
  12955. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12956. :style radio :help "Hide link part of links, only show the description"
  12957. :selected (member '(org-link) buffer-invisibility-spec)]
  12958. ["Literal Links"
  12959. (progn
  12960. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12961. :style radio :help "Show full links without hiding anything"
  12962. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12963. "--"
  12964. ["Export/Publish..." org-export t]
  12965. ("LaTeX"
  12966. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12967. :selected org-cdlatex-mode]
  12968. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12969. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12970. ["Modify math symbol" org-cdlatex-math-modify
  12971. (org-inside-LaTeX-fragment-p)]
  12972. ["Export LaTeX fragments as images"
  12973. (if (featurep 'org-exp)
  12974. (setq org-export-with-LaTeX-fragments
  12975. (not org-export-with-LaTeX-fragments))
  12976. (require 'org-exp))
  12977. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12978. org-export-with-LaTeX-fragments)])
  12979. "--"
  12980. ("Documentation"
  12981. ["Show Version" org-version t]
  12982. ["Info Documentation" org-info t])
  12983. ("Customize"
  12984. ["Browse Org Group" org-customize t]
  12985. "--"
  12986. ["Expand This Menu" org-create-customize-menu
  12987. (fboundp 'customize-menu-create)])
  12988. "--"
  12989. ["Refresh setup" org-mode-restart t]
  12990. ))
  12991. (defun org-info (&optional node)
  12992. "Read documentation for Org-mode in the info system.
  12993. With optional NODE, go directly to that node."
  12994. (interactive)
  12995. (info (format "(org)%s" (or node ""))))
  12996. (defun org-install-agenda-files-menu ()
  12997. (let ((bl (buffer-list)))
  12998. (save-excursion
  12999. (while bl
  13000. (set-buffer (pop bl))
  13001. (if (org-mode-p) (setq bl nil)))
  13002. (when (org-mode-p)
  13003. (easy-menu-change
  13004. '("Org") "File List for Agenda"
  13005. (append
  13006. (list
  13007. ["Edit File List" (org-edit-agenda-file-list) t]
  13008. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13009. ["Remove Current File from List" org-remove-file t]
  13010. ["Cycle through agenda files" org-cycle-agenda-files t]
  13011. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13012. "--")
  13013. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13014. ;;;; Documentation
  13015. (defun org-customize ()
  13016. "Call the customize function with org as argument."
  13017. (interactive)
  13018. (customize-browse 'org))
  13019. (defun org-create-customize-menu ()
  13020. "Create a full customization menu for Org-mode, insert it into the menu."
  13021. (interactive)
  13022. (if (fboundp 'customize-menu-create)
  13023. (progn
  13024. (easy-menu-change
  13025. '("Org") "Customize"
  13026. `(["Browse Org group" org-customize t]
  13027. "--"
  13028. ,(customize-menu-create 'org)
  13029. ["Set" Custom-set t]
  13030. ["Save" Custom-save t]
  13031. ["Reset to Current" Custom-reset-current t]
  13032. ["Reset to Saved" Custom-reset-saved t]
  13033. ["Reset to Standard Settings" Custom-reset-standard t]))
  13034. (message "\"Org\"-menu now contains full customization menu"))
  13035. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13036. ;;;; Miscellaneous stuff
  13037. ;;; Generally useful functions
  13038. (defun org-plist-delete (plist property)
  13039. "Delete PROPERTY from PLIST.
  13040. This is in contrast to merely setting it to 0."
  13041. (let (p)
  13042. (while plist
  13043. (if (not (eq property (car plist)))
  13044. (setq p (plist-put p (car plist) (nth 1 plist))))
  13045. (setq plist (cddr plist)))
  13046. p))
  13047. (defun org-force-self-insert (N)
  13048. "Needed to enforce self-insert under remapping."
  13049. (interactive "p")
  13050. (self-insert-command N))
  13051. (defun org-string-width (s)
  13052. "Compute width of string, ignoring invisible characters.
  13053. This ignores character with invisibility property `org-link', and also
  13054. characters with property `org-cwidth', because these will become invisible
  13055. upon the next fontification round."
  13056. (let (b l)
  13057. (when (or (eq t buffer-invisibility-spec)
  13058. (assq 'org-link buffer-invisibility-spec))
  13059. (while (setq b (text-property-any 0 (length s)
  13060. 'invisible 'org-link s))
  13061. (setq s (concat (substring s 0 b)
  13062. (substring s (or (next-single-property-change
  13063. b 'invisible s) (length s)))))))
  13064. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13065. (setq s (concat (substring s 0 b)
  13066. (substring s (or (next-single-property-change
  13067. b 'org-cwidth s) (length s))))))
  13068. (setq l (string-width s) b -1)
  13069. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13070. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13071. l))
  13072. (defun org-trim (s)
  13073. "Remove whitespace at beginning and end of string."
  13074. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13075. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13076. s)
  13077. (defun org-wrap (string &optional width lines)
  13078. "Wrap string to either a number of lines, or a width in characters.
  13079. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13080. that costs. If there is a word longer than WIDTH, the text is actually
  13081. wrapped to the length of that word.
  13082. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13083. many lines, whatever width that takes.
  13084. The return value is a list of lines, without newlines at the end."
  13085. (let* ((words (org-split-string string "[ \t\n]+"))
  13086. (maxword (apply 'max (mapcar 'org-string-width words)))
  13087. w ll)
  13088. (cond (width
  13089. (org-do-wrap words (max maxword width)))
  13090. (lines
  13091. (setq w maxword)
  13092. (setq ll (org-do-wrap words maxword))
  13093. (if (<= (length ll) lines)
  13094. ll
  13095. (setq ll words)
  13096. (while (> (length ll) lines)
  13097. (setq w (1+ w))
  13098. (setq ll (org-do-wrap words w)))
  13099. ll))
  13100. (t (error "Cannot wrap this")))))
  13101. (defun org-do-wrap (words width)
  13102. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13103. (let (lines line)
  13104. (while words
  13105. (setq line (pop words))
  13106. (while (and words (< (+ (length line) (length (car words))) width))
  13107. (setq line (concat line " " (pop words))))
  13108. (setq lines (push line lines)))
  13109. (nreverse lines)))
  13110. (defun org-split-string (string &optional separators)
  13111. "Splits STRING into substrings at SEPARATORS.
  13112. No empty strings are returned if there are matches at the beginning
  13113. and end of string."
  13114. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13115. (start 0)
  13116. notfirst
  13117. (list nil))
  13118. (while (and (string-match rexp string
  13119. (if (and notfirst
  13120. (= start (match-beginning 0))
  13121. (< start (length string)))
  13122. (1+ start) start))
  13123. (< (match-beginning 0) (length string)))
  13124. (setq notfirst t)
  13125. (or (eq (match-beginning 0) 0)
  13126. (and (eq (match-beginning 0) (match-end 0))
  13127. (eq (match-beginning 0) start))
  13128. (setq list
  13129. (cons (substring string start (match-beginning 0))
  13130. list)))
  13131. (setq start (match-end 0)))
  13132. (or (eq start (length string))
  13133. (setq list
  13134. (cons (substring string start)
  13135. list)))
  13136. (nreverse list)))
  13137. (defun org-context ()
  13138. "Return a list of contexts of the current cursor position.
  13139. If several contexts apply, all are returned.
  13140. Each context entry is a list with a symbol naming the context, and
  13141. two positions indicating start and end of the context. Possible
  13142. contexts are:
  13143. :headline anywhere in a headline
  13144. :headline-stars on the leading stars in a headline
  13145. :todo-keyword on a TODO keyword (including DONE) in a headline
  13146. :tags on the TAGS in a headline
  13147. :priority on the priority cookie in a headline
  13148. :item on the first line of a plain list item
  13149. :item-bullet on the bullet/number of a plain list item
  13150. :checkbox on the checkbox in a plain list item
  13151. :table in an org-mode table
  13152. :table-special on a special filed in a table
  13153. :table-table in a table.el table
  13154. :link on a hyperlink
  13155. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13156. :target on a <<target>>
  13157. :radio-target on a <<<radio-target>>>
  13158. :latex-fragment on a LaTeX fragment
  13159. :latex-preview on a LaTeX fragment with overlayed preview image
  13160. This function expects the position to be visible because it uses font-lock
  13161. faces as a help to recognize the following contexts: :table-special, :link,
  13162. and :keyword."
  13163. (let* ((f (get-text-property (point) 'face))
  13164. (faces (if (listp f) f (list f)))
  13165. (p (point)) clist o)
  13166. ;; First the large context
  13167. (cond
  13168. ((org-on-heading-p t)
  13169. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13170. (when (progn
  13171. (beginning-of-line 1)
  13172. (looking-at org-todo-line-tags-regexp))
  13173. (push (org-point-in-group p 1 :headline-stars) clist)
  13174. (push (org-point-in-group p 2 :todo-keyword) clist)
  13175. (push (org-point-in-group p 4 :tags) clist))
  13176. (goto-char p)
  13177. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13178. (if (looking-at "\\[#[A-Z0-9]\\]")
  13179. (push (org-point-in-group p 0 :priority) clist)))
  13180. ((org-at-item-p)
  13181. (push (org-point-in-group p 2 :item-bullet) clist)
  13182. (push (list :item (point-at-bol)
  13183. (save-excursion (org-end-of-item) (point)))
  13184. clist)
  13185. (and (org-at-item-checkbox-p)
  13186. (push (org-point-in-group p 0 :checkbox) clist)))
  13187. ((org-at-table-p)
  13188. (push (list :table (org-table-begin) (org-table-end)) clist)
  13189. (if (memq 'org-formula faces)
  13190. (push (list :table-special
  13191. (previous-single-property-change p 'face)
  13192. (next-single-property-change p 'face)) clist)))
  13193. ((org-at-table-p 'any)
  13194. (push (list :table-table) clist)))
  13195. (goto-char p)
  13196. ;; Now the small context
  13197. (cond
  13198. ((org-at-timestamp-p)
  13199. (push (org-point-in-group p 0 :timestamp) clist))
  13200. ((memq 'org-link faces)
  13201. (push (list :link
  13202. (previous-single-property-change p 'face)
  13203. (next-single-property-change p 'face)) clist))
  13204. ((memq 'org-special-keyword faces)
  13205. (push (list :keyword
  13206. (previous-single-property-change p 'face)
  13207. (next-single-property-change p 'face)) clist))
  13208. ((org-on-target-p)
  13209. (push (org-point-in-group p 0 :target) clist)
  13210. (goto-char (1- (match-beginning 0)))
  13211. (if (looking-at org-radio-target-regexp)
  13212. (push (org-point-in-group p 0 :radio-target) clist))
  13213. (goto-char p))
  13214. ((setq o (car (delq nil
  13215. (mapcar
  13216. (lambda (x)
  13217. (if (memq x org-latex-fragment-image-overlays) x))
  13218. (org-overlays-at (point))))))
  13219. (push (list :latex-fragment
  13220. (org-overlay-start o) (org-overlay-end o)) clist)
  13221. (push (list :latex-preview
  13222. (org-overlay-start o) (org-overlay-end o)) clist))
  13223. ((org-inside-LaTeX-fragment-p)
  13224. ;; FIXME: positions wrong.
  13225. (push (list :latex-fragment (point) (point)) clist)))
  13226. (setq clist (nreverse (delq nil clist)))
  13227. clist))
  13228. ;; FIXME: Compare with at-regexp-p Do we need both?
  13229. (defun org-in-regexp (re &optional nlines visually)
  13230. "Check if point is inside a match of regexp.
  13231. Normally only the current line is checked, but you can include NLINES extra
  13232. lines both before and after point into the search.
  13233. If VISUALLY is set, require that the cursor is not after the match but
  13234. really on, so that the block visually is on the match."
  13235. (catch 'exit
  13236. (let ((pos (point))
  13237. (eol (point-at-eol (+ 1 (or nlines 0))))
  13238. (inc (if visually 1 0)))
  13239. (save-excursion
  13240. (beginning-of-line (- 1 (or nlines 0)))
  13241. (while (re-search-forward re eol t)
  13242. (if (and (<= (match-beginning 0) pos)
  13243. (>= (+ inc (match-end 0)) pos))
  13244. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13245. (defun org-at-regexp-p (regexp)
  13246. "Is point inside a match of REGEXP in the current line?"
  13247. (catch 'exit
  13248. (save-excursion
  13249. (let ((pos (point)) (end (point-at-eol)))
  13250. (beginning-of-line 1)
  13251. (while (re-search-forward regexp end t)
  13252. (if (and (<= (match-beginning 0) pos)
  13253. (>= (match-end 0) pos))
  13254. (throw 'exit t)))
  13255. nil))))
  13256. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13257. "Call `multi-occur' with buffers for all agenda files."
  13258. (interactive "sOrg-files matching: \np")
  13259. (let* ((files (org-agenda-files))
  13260. (tnames (mapcar 'file-truename files))
  13261. (extra org-agenda-text-search-extra-files)
  13262. f)
  13263. (while (setq f (pop extra))
  13264. (unless (member (file-truename f) tnames)
  13265. (add-to-list 'files f 'append)
  13266. (add-to-list 'tnames (file-truename f) 'append)))
  13267. (multi-occur
  13268. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13269. regexp)))
  13270. (if (boundp 'occur-mode-find-occurrence-hook)
  13271. ;; Emacs 23
  13272. (add-hook 'occur-mode-find-occurrence-hook
  13273. (lambda ()
  13274. (when (org-mode-p)
  13275. (org-reveal))))
  13276. ;; Emacs 22
  13277. (defadvice occur-mode-goto-occurrence
  13278. (after org-occur-reveal activate)
  13279. (and (org-mode-p) (org-reveal)))
  13280. (defadvice occur-mode-goto-occurrence-other-window
  13281. (after org-occur-reveal activate)
  13282. (and (org-mode-p) (org-reveal)))
  13283. (defadvice occur-mode-display-occurrence
  13284. (after org-occur-reveal activate)
  13285. (when (org-mode-p)
  13286. (let ((pos (occur-mode-find-occurrence)))
  13287. (with-current-buffer (marker-buffer pos)
  13288. (save-excursion
  13289. (goto-char pos)
  13290. (org-reveal)))))))
  13291. (defun org-uniquify (list)
  13292. "Remove duplicate elements from LIST."
  13293. (let (res)
  13294. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13295. res))
  13296. (defun org-delete-all (elts list)
  13297. "Remove all elements in ELTS from LIST."
  13298. (while elts
  13299. (setq list (delete (pop elts) list)))
  13300. list)
  13301. (defun org-back-over-empty-lines ()
  13302. "Move backwards over witespace, to the beginning of the first empty line.
  13303. Returns the number of empty lines passed."
  13304. (let ((pos (point)))
  13305. (skip-chars-backward " \t\n\r")
  13306. (beginning-of-line 2)
  13307. (goto-char (min (point) pos))
  13308. (count-lines (point) pos)))
  13309. (defun org-skip-whitespace ()
  13310. (skip-chars-forward " \t\n\r"))
  13311. (defun org-point-in-group (point group &optional context)
  13312. "Check if POINT is in match-group GROUP.
  13313. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13314. match. If the match group does ot exist or point is not inside it,
  13315. return nil."
  13316. (and (match-beginning group)
  13317. (>= point (match-beginning group))
  13318. (<= point (match-end group))
  13319. (if context
  13320. (list context (match-beginning group) (match-end group))
  13321. t)))
  13322. (defun org-switch-to-buffer-other-window (&rest args)
  13323. "Switch to buffer in a second window on the current frame.
  13324. In particular, do not allow pop-up frames."
  13325. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13326. special-display-function)
  13327. (apply 'switch-to-buffer-other-window args)))
  13328. (defun org-combine-plists (&rest plists)
  13329. "Create a single property list from all plists in PLISTS.
  13330. The process starts by copying the first list, and then setting properties
  13331. from the other lists. Settings in the last list are the most significant
  13332. ones and overrule settings in the other lists."
  13333. (let ((rtn (copy-sequence (pop plists)))
  13334. p v ls)
  13335. (while plists
  13336. (setq ls (pop plists))
  13337. (while ls
  13338. (setq p (pop ls) v (pop ls))
  13339. (setq rtn (plist-put rtn p v))))
  13340. rtn))
  13341. (defun org-move-line-down (arg)
  13342. "Move the current line down. With prefix argument, move it past ARG lines."
  13343. (interactive "p")
  13344. (let ((col (current-column))
  13345. beg end pos)
  13346. (beginning-of-line 1) (setq beg (point))
  13347. (beginning-of-line 2) (setq end (point))
  13348. (beginning-of-line (+ 1 arg))
  13349. (setq pos (move-marker (make-marker) (point)))
  13350. (insert (delete-and-extract-region beg end))
  13351. (goto-char pos)
  13352. (move-to-column col)))
  13353. (defun org-move-line-up (arg)
  13354. "Move the current line up. With prefix argument, move it past ARG lines."
  13355. (interactive "p")
  13356. (let ((col (current-column))
  13357. beg end pos)
  13358. (beginning-of-line 1) (setq beg (point))
  13359. (beginning-of-line 2) (setq end (point))
  13360. (beginning-of-line (- arg))
  13361. (setq pos (move-marker (make-marker) (point)))
  13362. (insert (delete-and-extract-region beg end))
  13363. (goto-char pos)
  13364. (move-to-column col)))
  13365. (defun org-replace-escapes (string table)
  13366. "Replace %-escapes in STRING with values in TABLE.
  13367. TABLE is an association list with keys like \"%a\" and string values.
  13368. The sequences in STRING may contain normal field width and padding information,
  13369. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13370. so values can contain further %-escapes if they are define later in TABLE."
  13371. (let ((case-fold-search nil)
  13372. e re rpl)
  13373. (while (setq e (pop table))
  13374. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13375. (while (string-match re string)
  13376. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13377. (cdr e)))
  13378. (setq string (replace-match rpl t t string))))
  13379. string))
  13380. (defun org-sublist (list start end)
  13381. "Return a section of LIST, from START to END.
  13382. Counting starts at 1."
  13383. (let (rtn (c start))
  13384. (setq list (nthcdr (1- start) list))
  13385. (while (and list (<= c end))
  13386. (push (pop list) rtn)
  13387. (setq c (1+ c)))
  13388. (nreverse rtn)))
  13389. (defun org-find-base-buffer-visiting (file)
  13390. "Like `find-buffer-visiting' but alway return the base buffer and
  13391. not an indirect buffer."
  13392. (let ((buf (find-buffer-visiting file)))
  13393. (if buf
  13394. (or (buffer-base-buffer buf) buf)
  13395. nil)))
  13396. (defun org-image-file-name-regexp ()
  13397. "Return regexp matching the file names of images."
  13398. (if (fboundp 'image-file-name-regexp)
  13399. (image-file-name-regexp)
  13400. (let ((image-file-name-extensions
  13401. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13402. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13403. (concat "\\."
  13404. (regexp-opt (nconc (mapcar 'upcase
  13405. image-file-name-extensions)
  13406. image-file-name-extensions)
  13407. t)
  13408. "\\'"))))
  13409. (defun org-file-image-p (file)
  13410. "Return non-nil if FILE is an image."
  13411. (save-match-data
  13412. (string-match (org-image-file-name-regexp) file)))
  13413. ;;; Paragraph filling stuff.
  13414. ;; We want this to be just right, so use the full arsenal.
  13415. (defun org-indent-line-function ()
  13416. "Indent line like previous, but further if previous was headline or item."
  13417. (interactive)
  13418. (let* ((pos (point))
  13419. (itemp (org-at-item-p))
  13420. column bpos bcol tpos tcol bullet btype bullet-type)
  13421. ;; Find the previous relevant line
  13422. (beginning-of-line 1)
  13423. (cond
  13424. ((looking-at "#") (setq column 0))
  13425. ((looking-at "\\*+ ") (setq column 0))
  13426. (t
  13427. (beginning-of-line 0)
  13428. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13429. (beginning-of-line 0))
  13430. (cond
  13431. ((looking-at "\\*+[ \t]+")
  13432. (goto-char (match-end 0))
  13433. (setq column (current-column)))
  13434. ((org-in-item-p)
  13435. (org-beginning-of-item)
  13436. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13437. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  13438. (setq bpos (match-beginning 1) tpos (match-end 0)
  13439. bcol (progn (goto-char bpos) (current-column))
  13440. tcol (progn (goto-char tpos) (current-column))
  13441. bullet (match-string 1)
  13442. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13443. (if (not itemp)
  13444. (setq column tcol)
  13445. (goto-char pos)
  13446. (beginning-of-line 1)
  13447. (if (looking-at "\\S-")
  13448. (progn
  13449. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13450. (setq bullet (match-string 1)
  13451. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13452. (setq column (if (equal btype bullet-type) bcol tcol)))
  13453. (setq column (org-get-indentation)))))
  13454. (t (setq column (org-get-indentation))))))
  13455. (goto-char pos)
  13456. (if (<= (current-column) (current-indentation))
  13457. (indent-line-to column)
  13458. (save-excursion (indent-line-to column)))
  13459. (setq column (current-column))
  13460. (beginning-of-line 1)
  13461. (if (looking-at
  13462. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13463. (replace-match (concat "\\1" (format org-property-format
  13464. (match-string 2) (match-string 3)))
  13465. t nil))
  13466. (move-to-column column)))
  13467. (defun org-set-autofill-regexps ()
  13468. (interactive)
  13469. ;; In the paragraph separator we include headlines, because filling
  13470. ;; text in a line directly attached to a headline would otherwise
  13471. ;; fill the headline as well.
  13472. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13473. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13474. ;; The paragraph starter includes hand-formatted lists.
  13475. (org-set-local 'paragraph-start
  13476. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13477. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13478. ;; But only if the user has not turned off tables or fixed-width regions
  13479. (org-set-local
  13480. 'auto-fill-inhibit-regexp
  13481. (concat "\\*+ \\|#\\+"
  13482. "\\|[ \t]*" org-keyword-time-regexp
  13483. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13484. (concat
  13485. "\\|[ \t]*["
  13486. (if org-enable-table-editor "|" "")
  13487. (if org-enable-fixed-width-editor ":" "")
  13488. "]"))))
  13489. ;; We use our own fill-paragraph function, to make sure that tables
  13490. ;; and fixed-width regions are not wrapped. That function will pass
  13491. ;; through to `fill-paragraph' when appropriate.
  13492. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13493. ; Adaptive filling: To get full control, first make sure that
  13494. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13495. (org-set-local 'adaptive-fill-regexp "\000")
  13496. (org-set-local 'adaptive-fill-function
  13497. 'org-adaptive-fill-function)
  13498. (org-set-local
  13499. 'align-mode-rules-list
  13500. '((org-in-buffer-settings
  13501. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13502. (modes . '(org-mode))))))
  13503. (defun org-fill-paragraph (&optional justify)
  13504. "Re-align a table, pass through to fill-paragraph if no table."
  13505. (let ((table-p (org-at-table-p))
  13506. (table.el-p (org-at-table.el-p)))
  13507. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13508. (save-excursion (goto-char (point-at-bol))
  13509. (looking-at outline-regexp)))
  13510. t) ; skip headlines
  13511. (table.el-p t) ; skip table.el tables
  13512. (table-p (org-table-align) t) ; align org-mode tables
  13513. (t nil)))) ; call paragraph-fill
  13514. ;; For reference, this is the default value of adaptive-fill-regexp
  13515. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13516. (defun org-adaptive-fill-function ()
  13517. "Return a fill prefix for org-mode files.
  13518. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13519. work correctly."
  13520. (cond ((looking-at "#[ \t]+")
  13521. (match-string 0))
  13522. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  13523. (save-excursion
  13524. (goto-char (match-end 0))
  13525. (make-string (current-column) ?\ )))
  13526. (t nil)))
  13527. ;;; Other stuff.
  13528. (defun org-toggle-fixed-width-section (arg)
  13529. "Toggle the fixed-width export.
  13530. If there is no active region, the QUOTE keyword at the current headline is
  13531. inserted or removed. When present, it causes the text between this headline
  13532. and the next to be exported as fixed-width text, and unmodified.
  13533. If there is an active region, this command adds or removes a colon as the
  13534. first character of this line. If the first character of a line is a colon,
  13535. this line is also exported in fixed-width font."
  13536. (interactive "P")
  13537. (let* ((cc 0)
  13538. (regionp (org-region-active-p))
  13539. (beg (if regionp (region-beginning) (point)))
  13540. (end (if regionp (region-end)))
  13541. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13542. (case-fold-search nil)
  13543. (re "[ \t]*\\(:\\)")
  13544. off)
  13545. (if regionp
  13546. (save-excursion
  13547. (goto-char beg)
  13548. (setq cc (current-column))
  13549. (beginning-of-line 1)
  13550. (setq off (looking-at re))
  13551. (while (> nlines 0)
  13552. (setq nlines (1- nlines))
  13553. (beginning-of-line 1)
  13554. (cond
  13555. (arg
  13556. (move-to-column cc t)
  13557. (insert ":\n")
  13558. (forward-line -1))
  13559. ((and off (looking-at re))
  13560. (replace-match "" t t nil 1))
  13561. ((not off) (move-to-column cc t) (insert ":")))
  13562. (forward-line 1)))
  13563. (save-excursion
  13564. (org-back-to-heading)
  13565. (if (looking-at (concat outline-regexp
  13566. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13567. (replace-match "" t t nil 1)
  13568. (if (looking-at outline-regexp)
  13569. (progn
  13570. (goto-char (match-end 0))
  13571. (insert org-quote-string " "))))))))
  13572. ;;;; Functions extending outline functionality
  13573. (defun org-beginning-of-line (&optional arg)
  13574. "Go to the beginning of the current line. If that is invisible, continue
  13575. to a visible line beginning. This makes the function of C-a more intuitive.
  13576. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13577. first attempt, and only move to after the tags when the cursor is already
  13578. beyond the end of the headline."
  13579. (interactive "P")
  13580. (let ((pos (point)))
  13581. (beginning-of-line 1)
  13582. (if (bobp)
  13583. nil
  13584. (backward-char 1)
  13585. (if (org-invisible-p)
  13586. (while (and (not (bobp)) (org-invisible-p))
  13587. (backward-char 1)
  13588. (beginning-of-line 1))
  13589. (forward-char 1)))
  13590. (when org-special-ctrl-a/e
  13591. (cond
  13592. ((and (looking-at org-todo-line-regexp)
  13593. (= (char-after (match-end 1)) ?\ ))
  13594. (goto-char
  13595. (if (eq org-special-ctrl-a/e t)
  13596. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  13597. ((= pos (point)) (match-beginning 3))
  13598. (t (point)))
  13599. (cond ((> pos (point)) (point))
  13600. ((not (eq last-command this-command)) (point))
  13601. (t (match-beginning 3))))))
  13602. ((org-at-item-p)
  13603. (goto-char
  13604. (if (eq org-special-ctrl-a/e t)
  13605. (cond ((> pos (match-end 4)) (match-end 4))
  13606. ((= pos (point)) (match-end 4))
  13607. (t (point)))
  13608. (cond ((> pos (point)) (point))
  13609. ((not (eq last-command this-command)) (point))
  13610. (t (match-end 4))))))))))
  13611. (defun org-end-of-line (&optional arg)
  13612. "Go to the end of the line.
  13613. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13614. first attempt, and only move to after the tags when the cursor is already
  13615. beyond the end of the headline."
  13616. (interactive "P")
  13617. (if (or (not org-special-ctrl-a/e)
  13618. (not (org-on-heading-p)))
  13619. (end-of-line arg)
  13620. (let ((pos (point)))
  13621. (beginning-of-line 1)
  13622. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13623. (if (eq org-special-ctrl-a/e t)
  13624. (if (or (< pos (match-beginning 1))
  13625. (= pos (match-end 0)))
  13626. (goto-char (match-beginning 1))
  13627. (goto-char (match-end 0)))
  13628. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13629. (goto-char (match-end 0))
  13630. (goto-char (match-beginning 1))))
  13631. (end-of-line arg)))))
  13632. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13633. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13634. (defun org-kill-line (&optional arg)
  13635. "Kill line, to tags or end of line."
  13636. (interactive "P")
  13637. (cond
  13638. ((or (not org-special-ctrl-k)
  13639. (bolp)
  13640. (not (org-on-heading-p)))
  13641. (call-interactively 'kill-line))
  13642. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13643. (kill-region (point) (match-beginning 1))
  13644. (org-set-tags nil t))
  13645. (t (kill-region (point) (point-at-eol)))))
  13646. (define-key org-mode-map "\C-k" 'org-kill-line)
  13647. (defun org-invisible-p ()
  13648. "Check if point is at a character currently not visible."
  13649. ;; Early versions of noutline don't have `outline-invisible-p'.
  13650. (if (fboundp 'outline-invisible-p)
  13651. (outline-invisible-p)
  13652. (get-char-property (point) 'invisible)))
  13653. (defun org-invisible-p2 ()
  13654. "Check if point is at a character currently not visible."
  13655. (save-excursion
  13656. (if (and (eolp) (not (bobp))) (backward-char 1))
  13657. ;; Early versions of noutline don't have `outline-invisible-p'.
  13658. (if (fboundp 'outline-invisible-p)
  13659. (outline-invisible-p)
  13660. (get-char-property (point) 'invisible))))
  13661. (defalias 'org-back-to-heading 'outline-back-to-heading)
  13662. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13663. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13664. (defun org-at-heading-or-item-p ()
  13665. (or (org-on-heading-p) (org-at-item-p)))
  13666. (defun org-on-target-p ()
  13667. (or (org-in-regexp org-radio-target-regexp)
  13668. (org-in-regexp org-target-regexp)))
  13669. (defun org-up-heading-all (arg)
  13670. "Move to the heading line of which the present line is a subheading.
  13671. This function considers both visible and invisible heading lines.
  13672. With argument, move up ARG levels."
  13673. (if (fboundp 'outline-up-heading-all)
  13674. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13675. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13676. (defun org-up-heading-safe ()
  13677. "Move to the heading line of which the present line is a subheading.
  13678. This version will not throw an error. It will return the level of the
  13679. headline found, or nil if no higher level is found."
  13680. (let ((pos (point)) start-level level
  13681. (re (concat "^" outline-regexp)))
  13682. (catch 'exit
  13683. (outline-back-to-heading t)
  13684. (setq start-level (funcall outline-level))
  13685. (if (equal start-level 1) (throw 'exit nil))
  13686. (while (re-search-backward re nil t)
  13687. (setq level (funcall outline-level))
  13688. (if (< level start-level) (throw 'exit level)))
  13689. nil)))
  13690. (defun org-first-sibling-p ()
  13691. "Is this heading the first child of its parents?"
  13692. (interactive)
  13693. (let ((re (concat "^" outline-regexp))
  13694. level l)
  13695. (unless (org-at-heading-p t)
  13696. (error "Not at a heading"))
  13697. (setq level (funcall outline-level))
  13698. (save-excursion
  13699. (if (not (re-search-backward re nil t))
  13700. t
  13701. (setq l (funcall outline-level))
  13702. (< l level)))))
  13703. (defun org-goto-sibling (&optional previous)
  13704. "Goto the next sibling, even if it is invisible.
  13705. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13706. when a sibling was found. When none is found, return nil and don't
  13707. move point."
  13708. (let ((fun (if previous 're-search-backward 're-search-forward))
  13709. (pos (point))
  13710. (re (concat "^" outline-regexp))
  13711. level l)
  13712. (when (condition-case nil (org-back-to-heading t) (error nil))
  13713. (setq level (funcall outline-level))
  13714. (catch 'exit
  13715. (or previous (forward-char 1))
  13716. (while (funcall fun re nil t)
  13717. (setq l (funcall outline-level))
  13718. (when (< l level) (goto-char pos) (throw 'exit nil))
  13719. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13720. (goto-char pos)
  13721. nil))))
  13722. (defun org-show-siblings ()
  13723. "Show all siblings of the current headline."
  13724. (save-excursion
  13725. (while (org-goto-sibling) (org-flag-heading nil)))
  13726. (save-excursion
  13727. (while (org-goto-sibling 'previous)
  13728. (org-flag-heading nil))))
  13729. (defun org-show-hidden-entry ()
  13730. "Show an entry where even the heading is hidden."
  13731. (save-excursion
  13732. (org-show-entry)))
  13733. (defun org-flag-heading (flag &optional entry)
  13734. "Flag the current heading. FLAG non-nil means make invisible.
  13735. When ENTRY is non-nil, show the entire entry."
  13736. (save-excursion
  13737. (org-back-to-heading t)
  13738. ;; Check if we should show the entire entry
  13739. (if entry
  13740. (progn
  13741. (org-show-entry)
  13742. (save-excursion
  13743. (and (outline-next-heading)
  13744. (org-flag-heading nil))))
  13745. (outline-flag-region (max (point-min) (1- (point)))
  13746. (save-excursion (outline-end-of-heading) (point))
  13747. flag))))
  13748. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13749. ;; This is an exact copy of the original function, but it uses
  13750. ;; `org-back-to-heading', to make it work also in invisible
  13751. ;; trees. And is uses an invisible-OK argument.
  13752. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13753. (org-back-to-heading invisible-OK)
  13754. (let ((first t)
  13755. (level (funcall outline-level)))
  13756. (while (and (not (eobp))
  13757. (or first (> (funcall outline-level) level)))
  13758. (setq first nil)
  13759. (outline-next-heading))
  13760. (unless to-heading
  13761. (if (memq (preceding-char) '(?\n ?\^M))
  13762. (progn
  13763. ;; Go to end of line before heading
  13764. (forward-char -1)
  13765. (if (memq (preceding-char) '(?\n ?\^M))
  13766. ;; leave blank line before heading
  13767. (forward-char -1))))))
  13768. (point))
  13769. (defun org-show-subtree ()
  13770. "Show everything after this heading at deeper levels."
  13771. (outline-flag-region
  13772. (point)
  13773. (save-excursion
  13774. (outline-end-of-subtree) (outline-next-heading) (point))
  13775. nil))
  13776. (defun org-show-entry ()
  13777. "Show the body directly following this heading.
  13778. Show the heading too, if it is currently invisible."
  13779. (interactive)
  13780. (save-excursion
  13781. (condition-case nil
  13782. (progn
  13783. (org-back-to-heading t)
  13784. (outline-flag-region
  13785. (max (point-min) (1- (point)))
  13786. (save-excursion
  13787. (re-search-forward
  13788. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13789. (or (match-beginning 1) (point-max)))
  13790. nil))
  13791. (error nil))))
  13792. (defun org-make-options-regexp (kwds)
  13793. "Make a regular expression for keyword lines."
  13794. (concat
  13795. "^"
  13796. "#?[ \t]*\\+\\("
  13797. (mapconcat 'regexp-quote kwds "\\|")
  13798. "\\):[ \t]*"
  13799. "\\(.+\\)"))
  13800. ;; Make isearch reveal the necessary context
  13801. (defun org-isearch-end ()
  13802. "Reveal context after isearch exits."
  13803. (when isearch-success ; only if search was successful
  13804. (if (featurep 'xemacs)
  13805. ;; Under XEmacs, the hook is run in the correct place,
  13806. ;; we directly show the context.
  13807. (org-show-context 'isearch)
  13808. ;; In Emacs the hook runs *before* restoring the overlays.
  13809. ;; So we have to use a one-time post-command-hook to do this.
  13810. ;; (Emacs 22 has a special variable, see function `org-mode')
  13811. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13812. isearch-mode-end-hook-quit)
  13813. ;; Only when the isearch was not quitted.
  13814. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13815. 'append 'local)))))
  13816. (defun org-isearch-post-command ()
  13817. "Remove self from hook, and show context."
  13818. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13819. (org-show-context 'isearch))
  13820. ;;;; Integration with and fixes for other packages
  13821. ;;; Imenu support
  13822. (defvar org-imenu-markers nil
  13823. "All markers currently used by Imenu.")
  13824. (make-variable-buffer-local 'org-imenu-markers)
  13825. (defun org-imenu-new-marker (&optional pos)
  13826. "Return a new marker for use by Imenu, and remember the marker."
  13827. (let ((m (make-marker)))
  13828. (move-marker m (or pos (point)))
  13829. (push m org-imenu-markers)
  13830. m))
  13831. (defun org-imenu-get-tree ()
  13832. "Produce the index for Imenu."
  13833. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13834. (setq org-imenu-markers nil)
  13835. (let* ((n org-imenu-depth)
  13836. (re (concat "^" outline-regexp))
  13837. (subs (make-vector (1+ n) nil))
  13838. (last-level 0)
  13839. m tree level head)
  13840. (save-excursion
  13841. (save-restriction
  13842. (widen)
  13843. (goto-char (point-max))
  13844. (while (re-search-backward re nil t)
  13845. (setq level (org-reduced-level (funcall outline-level)))
  13846. (when (<= level n)
  13847. (looking-at org-complex-heading-regexp)
  13848. (setq head (org-match-string-no-properties 4)
  13849. m (org-imenu-new-marker))
  13850. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13851. (if (>= level last-level)
  13852. (push (cons head m) (aref subs level))
  13853. (push (cons head (aref subs (1+ level))) (aref subs level))
  13854. (loop for i from (1+ level) to n do (aset subs i nil)))
  13855. (setq last-level level)))))
  13856. (aref subs 1)))
  13857. (eval-after-load "imenu"
  13858. '(progn
  13859. (add-hook 'imenu-after-jump-hook
  13860. (lambda () (org-show-context 'org-goto)))))
  13861. ;; Speedbar support
  13862. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13863. "Overlay marking the agenda restriction line in speedbar.")
  13864. (org-overlay-put org-speedbar-restriction-lock-overlay
  13865. 'face 'org-agenda-restriction-lock)
  13866. (org-overlay-put org-speedbar-restriction-lock-overlay
  13867. 'help-echo "Agendas are currently limited to this item.")
  13868. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13869. (defun org-speedbar-set-agenda-restriction ()
  13870. "Restrict future agenda commands to the location at point in speedbar.
  13871. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13872. (interactive)
  13873. (require 'org-agenda)
  13874. (let (p m tp np dir txt w)
  13875. (cond
  13876. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13877. 'org-imenu t))
  13878. (setq m (get-text-property p 'org-imenu-marker))
  13879. (save-excursion
  13880. (save-restriction
  13881. (set-buffer (marker-buffer m))
  13882. (goto-char m)
  13883. (org-agenda-set-restriction-lock 'subtree))))
  13884. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13885. 'speedbar-function 'speedbar-find-file))
  13886. (setq tp (previous-single-property-change
  13887. (1+ p) 'speedbar-function)
  13888. np (next-single-property-change
  13889. tp 'speedbar-function)
  13890. dir (speedbar-line-directory)
  13891. txt (buffer-substring-no-properties (or tp (point-min))
  13892. (or np (point-max))))
  13893. (save-excursion
  13894. (save-restriction
  13895. (set-buffer (find-file-noselect
  13896. (let ((default-directory dir))
  13897. (expand-file-name txt))))
  13898. (unless (org-mode-p)
  13899. (error "Cannot restrict to non-Org-mode file"))
  13900. (org-agenda-set-restriction-lock 'file))))
  13901. (t (error "Don't know how to restrict Org-mode's agenda")))
  13902. (org-move-overlay org-speedbar-restriction-lock-overlay
  13903. (point-at-bol) (point-at-eol))
  13904. (setq current-prefix-arg nil)
  13905. (org-agenda-maybe-redo)))
  13906. (eval-after-load "speedbar"
  13907. '(progn
  13908. (speedbar-add-supported-extension ".org")
  13909. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13910. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13911. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13912. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13913. (add-hook 'speedbar-visiting-tag-hook
  13914. (lambda () (org-show-context 'org-goto)))))
  13915. ;;; Fixes and Hacks for problems with other packages
  13916. ;; Make flyspell not check words in links, to not mess up our keymap
  13917. (defun org-mode-flyspell-verify ()
  13918. "Don't let flyspell put overlays at active buttons."
  13919. (not (get-text-property (point) 'keymap)))
  13920. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13921. (eval-after-load "bookmark"
  13922. '(if (boundp 'bookmark-after-jump-hook)
  13923. ;; We can use the hook
  13924. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13925. ;; Hook not available, use advice
  13926. (defadvice bookmark-jump (after org-make-visible activate)
  13927. "Make the position visible."
  13928. (org-bookmark-jump-unhide))))
  13929. (defun org-bookmark-jump-unhide ()
  13930. "Unhide the current position, to show the bookmark location."
  13931. (and (org-mode-p)
  13932. (or (org-invisible-p)
  13933. (save-excursion (goto-char (max (point-min) (1- (point))))
  13934. (org-invisible-p)))
  13935. (org-show-context 'bookmark-jump)))
  13936. ;; Make session.el ignore our circular variable
  13937. (eval-after-load "session"
  13938. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13939. ;;;; Experimental code
  13940. (defun org-closed-in-range ()
  13941. "Sparse tree of items closed in a certain time range.
  13942. Still experimental, may disappear in the future."
  13943. (interactive)
  13944. ;; Get the time interval from the user.
  13945. (let* ((time1 (time-to-seconds
  13946. (org-read-date nil 'to-time nil "Starting date: ")))
  13947. (time2 (time-to-seconds
  13948. (org-read-date nil 'to-time nil "End date:")))
  13949. ;; callback function
  13950. (callback (lambda ()
  13951. (let ((time
  13952. (time-to-seconds
  13953. (apply 'encode-time
  13954. (org-parse-time-string
  13955. (match-string 1))))))
  13956. ;; check if time in interval
  13957. (and (>= time time1) (<= time time2))))))
  13958. ;; make tree, check each match with the callback
  13959. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13960. ;;;; Finish up
  13961. (provide 'org)
  13962. (run-hooks 'org-load-hook)
  13963. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13964. ;;; org.el ends here