org.el 559 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117
  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-2
  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-2"
  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-irc 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 " irc: Links to IRC/ERC chat sessions" org-irc)
  143. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  144. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  145. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  146. (const :tag " vm: Links to VM folders/messages" org-vm)
  147. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  148. (const :tag " mouse: Additional mouse support" org-mouse)
  149. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  150. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  151. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  152. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  153. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  154. (const :tag "C id: Global id's for identifying entries" org-id)
  155. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  156. (const :tag "C iswitchb: Use iswitchb to select Org buffer" org-iswitchb)
  157. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  158. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  159. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  160. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  161. (const :tag "C registry: A registry for Org links" org-registry)
  162. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  163. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  164. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  165. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  166. (defgroup org-startup nil
  167. "Options concerning startup of Org-mode."
  168. :tag "Org Startup"
  169. :group 'org)
  170. (defcustom org-startup-folded t
  171. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  172. This can also be configured on a per-file basis by adding one of
  173. the following lines anywhere in the buffer:
  174. #+STARTUP: fold
  175. #+STARTUP: nofold
  176. #+STARTUP: content"
  177. :group 'org-startup
  178. :type '(choice
  179. (const :tag "nofold: show all" nil)
  180. (const :tag "fold: overview" t)
  181. (const :tag "content: all headlines" content)))
  182. (defcustom org-startup-truncated t
  183. "Non-nil means, entering Org-mode will set `truncate-lines'.
  184. This is useful since some lines containing links can be very long and
  185. uninteresting. Also tables look terrible when wrapped."
  186. :group 'org-startup
  187. :type 'boolean)
  188. (defcustom org-startup-align-all-tables nil
  189. "Non-nil means, align all tables when visiting a file.
  190. This is useful when the column width in tables is forced with <N> cookies
  191. in table fields. Such tables will look correct only after the first re-align.
  192. This can also be configured on a per-file basis by adding one of
  193. the following lines anywhere in the buffer:
  194. #+STARTUP: align
  195. #+STARTUP: noalign"
  196. :group 'org-startup
  197. :type 'boolean)
  198. (defcustom org-insert-mode-line-in-empty-file nil
  199. "Non-nil means insert the first line setting Org-mode in empty files.
  200. When the function `org-mode' is called interactively in an empty file, this
  201. normally means that the file name does not automatically trigger Org-mode.
  202. To ensure that the file will always be in Org-mode in the future, a
  203. line enforcing Org-mode will be inserted into the buffer, if this option
  204. has been set."
  205. :group 'org-startup
  206. :type 'boolean)
  207. (defcustom org-replace-disputed-keys nil
  208. "Non-nil means use alternative key bindings for some keys.
  209. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  210. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  211. If you want to use Org-mode together with one of these other modes,
  212. or more generally if you would like to move some Org-mode commands to
  213. other keys, set this variable and configure the keys with the variable
  214. `org-disputed-keys'.
  215. This option is only relevant at load-time of Org-mode, and must be set
  216. *before* org.el is loaded. Changing it requires a restart of Emacs to
  217. become effective."
  218. :group 'org-startup
  219. :type 'boolean)
  220. (if (fboundp 'defvaralias)
  221. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  222. (defcustom org-disputed-keys
  223. '(([(shift up)] . [(meta p)])
  224. ([(shift down)] . [(meta n)])
  225. ([(shift left)] . [(meta -)])
  226. ([(shift right)] . [(meta +)])
  227. ([(control shift right)] . [(meta shift +)])
  228. ([(control shift left)] . [(meta shift -)]))
  229. "Keys for which Org-mode and other modes compete.
  230. This is an alist, cars are the default keys, second element specifies
  231. the alternative to use when `org-replace-disputed-keys' is t.
  232. Keys can be specified in any syntax supported by `define-key'.
  233. The value of this option takes effect only at Org-mode's startup,
  234. therefore you'll have to restart Emacs to apply it after changing."
  235. :group 'org-startup
  236. :type 'alist)
  237. (defun org-key (key)
  238. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  239. Or return the original if not disputed."
  240. (if org-replace-disputed-keys
  241. (let* ((nkey (key-description key))
  242. (x (org-find-if (lambda (x)
  243. (equal (key-description (car x)) nkey))
  244. org-disputed-keys)))
  245. (if x (cdr x) key))
  246. key))
  247. (defun org-find-if (predicate seq)
  248. (catch 'exit
  249. (while seq
  250. (if (funcall predicate (car seq))
  251. (throw 'exit (car seq))
  252. (pop seq)))))
  253. (defun org-defkey (keymap key def)
  254. "Define a key, possibly translated, as returned by `org-key'."
  255. (define-key keymap (org-key key) def))
  256. (defcustom org-ellipsis nil
  257. "The ellipsis to use in the Org-mode outline.
  258. When nil, just use the standard three dots. When a string, use that instead,
  259. When a face, use the standart 3 dots, but with the specified face.
  260. The change affects only Org-mode (which will then use its own display table).
  261. Changing this requires executing `M-x org-mode' in a buffer to become
  262. effective."
  263. :group 'org-startup
  264. :type '(choice (const :tag "Default" nil)
  265. (face :tag "Face" :value org-warning)
  266. (string :tag "String" :value "...#")))
  267. (defvar org-display-table nil
  268. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  269. (defgroup org-keywords nil
  270. "Keywords in Org-mode."
  271. :tag "Org Keywords"
  272. :group 'org)
  273. (defcustom org-deadline-string "DEADLINE:"
  274. "String to mark deadline entries.
  275. A deadline is this string, followed by a time stamp. Should be a word,
  276. terminated by a colon. You can insert a schedule keyword and
  277. a timestamp with \\[org-deadline].
  278. Changes become only effective after restarting Emacs."
  279. :group 'org-keywords
  280. :type 'string)
  281. (defcustom org-scheduled-string "SCHEDULED:"
  282. "String to mark scheduled TODO entries.
  283. A schedule is this string, followed by a time stamp. Should be a word,
  284. terminated by a colon. You can insert a schedule keyword and
  285. a timestamp with \\[org-schedule].
  286. Changes become only effective after restarting Emacs."
  287. :group 'org-keywords
  288. :type 'string)
  289. (defcustom org-closed-string "CLOSED:"
  290. "String used as the prefix for timestamps logging closing a TODO entry."
  291. :group 'org-keywords
  292. :type 'string)
  293. (defcustom org-clock-string "CLOCK:"
  294. "String used as prefix for timestamps clocking work hours on an item."
  295. :group 'org-keywords
  296. :type 'string)
  297. (defcustom org-comment-string "COMMENT"
  298. "Entries starting with this keyword will never be exported.
  299. An entry can be toggled between COMMENT and normal with
  300. \\[org-toggle-comment].
  301. Changes become only effective after restarting Emacs."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-quote-string "QUOTE"
  305. "Entries starting with this keyword will be exported in fixed-width font.
  306. Quoting applies only to the text in the entry following the headline, and does
  307. not extend beyond the next headline, even if that is lower level.
  308. An entry can be toggled between QUOTE and normal with
  309. \\[org-toggle-fixed-width-section]."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defconst org-repeat-re
  313. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  314. "Regular expression for specifying repeated events.
  315. After a match, group 1 contains the repeat expression.")
  316. (defgroup org-structure nil
  317. "Options concerning the general structure of Org-mode files."
  318. :tag "Org Structure"
  319. :group 'org)
  320. (defgroup org-reveal-location nil
  321. "Options about how to make context of a location visible."
  322. :tag "Org Reveal Location"
  323. :group 'org-structure)
  324. (defconst org-context-choice
  325. '(choice
  326. (const :tag "Always" t)
  327. (const :tag "Never" nil)
  328. (repeat :greedy t :tag "Individual contexts"
  329. (cons
  330. (choice :tag "Context"
  331. (const agenda)
  332. (const org-goto)
  333. (const occur-tree)
  334. (const tags-tree)
  335. (const link-search)
  336. (const mark-goto)
  337. (const bookmark-jump)
  338. (const isearch)
  339. (const default))
  340. (boolean))))
  341. "Contexts for the reveal options.")
  342. (defcustom org-show-hierarchy-above '((default . t))
  343. "Non-nil means, show full hierarchy when revealing a location.
  344. Org-mode often shows locations in an org-mode file which might have
  345. been invisible before. When this is set, the hierarchy of headings
  346. above the exposed location is shown.
  347. Turning this off for example for sparse trees makes them very compact.
  348. Instead of t, this can also be an alist specifying this option for different
  349. contexts. Valid contexts are
  350. agenda when exposing an entry from the agenda
  351. org-goto when using the command `org-goto' on key C-c C-j
  352. occur-tree when using the command `org-occur' on key C-c /
  353. tags-tree when constructing a sparse tree based on tags matches
  354. link-search when exposing search matches associated with a link
  355. mark-goto when exposing the jump goal of a mark
  356. bookmark-jump when exposing a bookmark location
  357. isearch when exiting from an incremental search
  358. default default for all contexts not set explicitly"
  359. :group 'org-reveal-location
  360. :type org-context-choice)
  361. (defcustom org-show-following-heading '((default . nil))
  362. "Non-nil means, show following heading when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the heading following the
  365. match is shown.
  366. Turning this off for example for sparse trees makes them very compact,
  367. but makes it harder to edit the location of the match. In such a case,
  368. use the command \\[org-reveal] to show more context.
  369. Instead of t, this can also be an alist specifying this option for different
  370. contexts. See `org-show-hierarchy-above' for valid contexts."
  371. :group 'org-reveal-location
  372. :type org-context-choice)
  373. (defcustom org-show-siblings '((default . nil) (isearch t))
  374. "Non-nil means, show all sibling heading when revealing a location.
  375. Org-mode often shows locations in an org-mode file which might have
  376. been invisible before. When this is set, the sibling of the current entry
  377. heading are all made visible. If `org-show-hierarchy-above' is t,
  378. the same happens on each level of the hierarchy above the current entry.
  379. By default this is on for the isearch context, off for all other contexts.
  380. Turning this off for example for sparse trees makes them very compact,
  381. but makes it harder to edit the location of the match. In such a case,
  382. use the command \\[org-reveal] to show more context.
  383. Instead of t, this can also be an alist specifying this option for different
  384. contexts. See `org-show-hierarchy-above' for valid contexts."
  385. :group 'org-reveal-location
  386. :type org-context-choice)
  387. (defcustom org-show-entry-below '((default . nil))
  388. "Non-nil means, show the entry below a headline when revealing a location.
  389. Org-mode often shows locations in an org-mode file which might have
  390. been invisible before. When this is set, the text below the headline that is
  391. exposed is also shown.
  392. By default this is off for all contexts.
  393. Instead of t, this can also be an alist specifying this option for different
  394. contexts. See `org-show-hierarchy-above' for valid contexts."
  395. :group 'org-reveal-location
  396. :type org-context-choice)
  397. (defcustom org-indirect-buffer-display 'other-window
  398. "How should indirect tree buffers be displayed?
  399. This applies to indirect buffers created with the commands
  400. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  401. Valid values are:
  402. current-window Display in the current window
  403. other-window Just display in another window.
  404. dedicated-frame Create one new frame, and re-use it each time.
  405. new-frame Make a new frame each time. Note that in this case
  406. previously-made indirect buffers are kept, and you need to
  407. kill these buffers yourself."
  408. :group 'org-structure
  409. :group 'org-agenda-windows
  410. :type '(choice
  411. (const :tag "In current window" current-window)
  412. (const :tag "In current frame, other window" other-window)
  413. (const :tag "Each time a new frame" new-frame)
  414. (const :tag "One dedicated frame" dedicated-frame)))
  415. (defgroup org-cycle nil
  416. "Options concerning visibility cycling in Org-mode."
  417. :tag "Org Cycle"
  418. :group 'org-structure)
  419. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  420. "Names of drawers. Drawers are not opened by cycling on the headline above.
  421. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  422. this:
  423. :DRAWERNAME:
  424. .....
  425. :END:
  426. The drawer \"PROPERTIES\" is special for capturing properties through
  427. the property API.
  428. Drawers can be defined on the per-file basis with a line like:
  429. #+DRAWERS: HIDDEN STATE PROPERTIES"
  430. :group 'org-structure
  431. :type '(repeat (string :tag "Drawer Name")))
  432. (defcustom org-cycle-global-at-bob nil
  433. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  434. This makes it possible to do global cycling without having to use S-TAB or
  435. C-u TAB. For this special case to work, the first line of the buffer
  436. must not be a headline - it may be empty ot some other text. When used in
  437. this way, `org-cycle-hook' is disables temporarily, to make sure the
  438. cursor stays at the beginning of the buffer.
  439. When this option is nil, don't do anything special at the beginning
  440. of the buffer."
  441. :group 'org-cycle
  442. :type 'boolean)
  443. (defcustom org-cycle-emulate-tab t
  444. "Where should `org-cycle' emulate TAB.
  445. nil Never
  446. white Only in completely white lines
  447. whitestart Only at the beginning of lines, before the first non-white char
  448. t Everywhere except in headlines
  449. exc-hl-bol Everywhere except at the start of a headline
  450. If TAB is used in a place where it does not emulate TAB, the current subtree
  451. visibility is cycled."
  452. :group 'org-cycle
  453. :type '(choice (const :tag "Never" nil)
  454. (const :tag "Only in completely white lines" white)
  455. (const :tag "Before first char in a line" whitestart)
  456. (const :tag "Everywhere except in headlines" t)
  457. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  458. ))
  459. (defcustom org-cycle-separator-lines 2
  460. "Number of empty lines needed to keep an empty line between collapsed trees.
  461. If you leave an empty line between the end of a subtree and the following
  462. headline, this empty line is hidden when the subtree is folded.
  463. Org-mode will leave (exactly) one empty line visible if the number of
  464. empty lines is equal or larger to the number given in this variable.
  465. So the default 2 means, at least 2 empty lines after the end of a subtree
  466. are needed to produce free space between a collapsed subtree and the
  467. following headline.
  468. Special case: when 0, never leave empty lines in collapsed view."
  469. :group 'org-cycle
  470. :type 'integer)
  471. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  472. org-cycle-hide-drawers
  473. org-cycle-show-empty-lines
  474. org-optimize-window-after-visibility-change)
  475. "Hook that is run after `org-cycle' has changed the buffer visibility.
  476. The function(s) in this hook must accept a single argument which indicates
  477. the new state that was set by the most recent `org-cycle' command. The
  478. argument is a symbol. After a global state change, it can have the values
  479. `overview', `content', or `all'. After a local state change, it can have
  480. the values `folded', `children', or `subtree'."
  481. :group 'org-cycle
  482. :type 'hook)
  483. (defgroup org-edit-structure nil
  484. "Options concerning structure editing in Org-mode."
  485. :tag "Org Edit Structure"
  486. :group 'org-structure)
  487. (defcustom org-odd-levels-only nil
  488. "Non-nil means, skip even levels and only use odd levels for the outline.
  489. This has the effect that two stars are being added/taken away in
  490. promotion/demotion commands. It also influences how levels are
  491. handled by the exporters.
  492. Changing it requires restart of `font-lock-mode' to become effective
  493. for fontification also in regions already fontified.
  494. You may also set this on a per-file basis by adding one of the following
  495. lines to the buffer:
  496. #+STARTUP: odd
  497. #+STARTUP: oddeven"
  498. :group 'org-edit-structure
  499. :group 'org-font-lock
  500. :type 'boolean)
  501. (defcustom org-adapt-indentation t
  502. "Non-nil means, adapt indentation when promoting and demoting.
  503. When this is set and the *entire* text in an entry is indented, the
  504. indentation is increased by one space in a demotion command, and
  505. decreased by one in a promotion command. If any line in the entry
  506. body starts at column 0, indentation is not changed at all."
  507. :group 'org-edit-structure
  508. :type 'boolean)
  509. (defcustom org-special-ctrl-a/e nil
  510. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  511. When t, `C-a' will bring back the cursor to the beginning of the
  512. headline text, i.e. after the stars and after a possible TODO keyword.
  513. In an item, this will be the position after the bullet.
  514. When the cursor is already at that position, another `C-a' will bring
  515. it to the beginning of the line.
  516. `C-e' will jump to the end of the headline, ignoring the presence of tags
  517. in the headline. A second `C-e' will then jump to the true end of the
  518. line, after any tags.
  519. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  520. and only a directly following, identical keypress will bring the cursor
  521. to the special positions."
  522. :group 'org-edit-structure
  523. :type '(choice
  524. (const :tag "off" nil)
  525. (const :tag "after bullet first" t)
  526. (const :tag "border first" reversed)))
  527. (if (fboundp 'defvaralias)
  528. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  529. (defcustom org-special-ctrl-k nil
  530. "Non-nil means `C-k' will behave specially in headlines.
  531. When nil, `C-k' will call the default `kill-line' command.
  532. When t, the following will happen while the cursor is in the headline:
  533. - When the cursor is at the beginning of a headline, kill the entire
  534. line and possible the folded subtree below the line.
  535. - When in the middle of the headline text, kill the headline up to the tags.
  536. - When after the headline text, kill the tags."
  537. :group 'org-edit-structure
  538. :type 'boolean)
  539. (defcustom org-M-RET-may-split-line '((default . t))
  540. "Non-nil means, M-RET will split the line at the cursor position.
  541. When nil, it will go to the end of the line before making a
  542. new line.
  543. You may also set this option in a different way for different
  544. contexts. Valid contexts are:
  545. headline when creating a new headline
  546. item when creating a new item
  547. table in a table field
  548. default the value to be used for all contexts not explicitly
  549. customized"
  550. :group 'org-structure
  551. :group 'org-table
  552. :type '(choice
  553. (const :tag "Always" t)
  554. (const :tag "Never" nil)
  555. (repeat :greedy t :tag "Individual contexts"
  556. (cons
  557. (choice :tag "Context"
  558. (const headline)
  559. (const item)
  560. (const table)
  561. (const default))
  562. (boolean)))))
  563. (defcustom org-blank-before-new-entry '((heading . nil)
  564. (plain-list-item . nil))
  565. "Should `org-insert-heading' leave a blank line before new heading/item?
  566. The value is an alist, with `heading' and `plain-list-item' as car,
  567. and a boolean flag as cdr."
  568. :group 'org-edit-structure
  569. :type '(list
  570. (cons (const heading) (boolean))
  571. (cons (const plain-list-item) (boolean))))
  572. (defcustom org-insert-heading-hook nil
  573. "Hook being run after inserting a new heading."
  574. :group 'org-edit-structure
  575. :type 'hook)
  576. (defcustom org-enable-fixed-width-editor t
  577. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  578. This currently only means, they are never auto-wrapped.
  579. When nil, such lines will be treated like ordinary lines.
  580. See also the QUOTE keyword."
  581. :group 'org-edit-structure
  582. :type 'boolean)
  583. (defcustom org-goto-auto-isearch t
  584. "Non-nil means, typing characters in org-goto starts incremental search."
  585. :group 'org-edit-structure
  586. :type 'boolean)
  587. (defgroup org-sparse-trees nil
  588. "Options concerning sparse trees in Org-mode."
  589. :tag "Org Sparse Trees"
  590. :group 'org-structure)
  591. (defcustom org-highlight-sparse-tree-matches t
  592. "Non-nil means, highlight all matches that define a sparse tree.
  593. The highlights will automatically disappear the next time the buffer is
  594. changed by an edit command."
  595. :group 'org-sparse-trees
  596. :type 'boolean)
  597. (defcustom org-remove-highlights-with-change t
  598. "Non-nil means, any change to the buffer will remove temporary highlights.
  599. Such highlights are created by `org-occur' and `org-clock-display'.
  600. When nil, `C-c C-c needs to be used to get rid of the highlights.
  601. The highlights created by `org-preview-latex-fragment' always need
  602. `C-c C-c' to be removed."
  603. :group 'org-sparse-trees
  604. :group 'org-time
  605. :type 'boolean)
  606. (defcustom org-occur-hook '(org-first-headline-recenter)
  607. "Hook that is run after `org-occur' has constructed a sparse tree.
  608. This can be used to recenter the window to show as much of the structure
  609. as possible."
  610. :group 'org-sparse-trees
  611. :type 'hook)
  612. (defgroup org-plain-lists nil
  613. "Options concerning plain lists in Org-mode."
  614. :tag "Org Plain lists"
  615. :group 'org-structure)
  616. (defcustom org-cycle-include-plain-lists nil
  617. "Non-nil means, include plain lists into visibility cycling.
  618. This means that during cycling, plain list items will *temporarily* be
  619. interpreted as outline headlines with a level given by 1000+i where i is the
  620. indentation of the bullet. In all other operations, plain list items are
  621. not seen as headlines. For example, you cannot assign a TODO keyword to
  622. such an item."
  623. :group 'org-plain-lists
  624. :type 'boolean)
  625. (defcustom org-plain-list-ordered-item-terminator t
  626. "The character that makes a line with leading number an ordered list item.
  627. Valid values are ?. and ?\). To get both terminators, use t. While
  628. ?. may look nicer, it creates the danger that a line with leading
  629. number may be incorrectly interpreted as an item. ?\) therefore is
  630. the safe choice."
  631. :group 'org-plain-lists
  632. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  633. (const :tag "paren like in \"2)\"" ?\))
  634. (const :tab "both" t)))
  635. (defcustom org-auto-renumber-ordered-lists t
  636. "Non-nil means, automatically renumber ordered plain lists.
  637. Renumbering happens when the sequence have been changed with
  638. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  639. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  640. :group 'org-plain-lists
  641. :type 'boolean)
  642. (defcustom org-provide-checkbox-statistics t
  643. "Non-nil means, update checkbox statistics after insert and toggle.
  644. When this is set, checkbox statistics is updated each time you either insert
  645. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  646. with \\[org-ctrl-c-ctrl-c\\]."
  647. :group 'org-plain-lists
  648. :type 'boolean)
  649. (defgroup org-archive nil
  650. "Options concerning archiving in Org-mode."
  651. :tag "Org Archive"
  652. :group 'org-structure)
  653. (defcustom org-archive-tag "ARCHIVE"
  654. "The tag that marks a subtree as archived.
  655. An archived subtree does not open during visibility cycling, and does
  656. not contribute to the agenda listings.
  657. After changing this, font-lock must be restarted in the relevant buffers to
  658. get the proper fontification."
  659. :group 'org-archive
  660. :group 'org-keywords
  661. :type 'string)
  662. (defcustom org-agenda-skip-archived-trees t
  663. "Non-nil means, the agenda will skip any items located in archived trees.
  664. An archived tree is a tree marked with the tag ARCHIVE."
  665. :group 'org-archive
  666. :group 'org-agenda-skip
  667. :type 'boolean)
  668. (defcustom org-cycle-open-archived-trees nil
  669. "Non-nil means, `org-cycle' will open archived trees.
  670. An archived tree is a tree marked with the tag ARCHIVE.
  671. When nil, archived trees will stay folded. You can still open them with
  672. normal outline commands like `show-all', but not with the cycling commands."
  673. :group 'org-archive
  674. :group 'org-cycle
  675. :type 'boolean)
  676. (defcustom org-sparse-tree-open-archived-trees nil
  677. "Non-nil means sparse tree construction shows matches in archived trees.
  678. When nil, matches in these trees are highlighted, but the trees are kept in
  679. collapsed state."
  680. :group 'org-archive
  681. :group 'org-sparse-trees
  682. :type 'boolean)
  683. (defcustom org-archive-location "%s_archive::"
  684. "The location where subtrees should be archived.
  685. This string consists of two parts, separated by a double-colon.
  686. The first part is a file name - when omitted, archiving happens in the same
  687. file. %s will be replaced by the current file name (without directory part).
  688. Archiving to a different file is useful to keep archived entries from
  689. contributing to the Org-mode Agenda.
  690. The part after the double colon is a headline. The archived entries will be
  691. filed under that headline. When omitted, the subtrees are simply filed away
  692. at the end of the file, as top-level entries.
  693. Here are a few examples:
  694. \"%s_archive::\"
  695. If the current file is Projects.org, archive in file
  696. Projects.org_archive, as top-level trees. This is the default.
  697. \"::* Archived Tasks\"
  698. Archive in the current file, under the top-level headline
  699. \"* Archived Tasks\".
  700. \"~/org/archive.org::\"
  701. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  702. \"basement::** Finished Tasks\"
  703. Archive in file ./basement (relative path), as level 3 trees
  704. below the level 2 heading \"** Finished Tasks\".
  705. You may set this option on a per-file basis by adding to the buffer a
  706. line like
  707. #+ARCHIVE: basement::** Finished Tasks"
  708. :group 'org-archive
  709. :type 'string)
  710. (defcustom org-archive-mark-done t
  711. "Non-nil means, mark entries as DONE when they are moved to the archive file.
  712. This can be a string to set the keyword to use. When t, Org-mode will
  713. use the first keyword in its list that means done."
  714. :group 'org-archive
  715. :type '(choice
  716. (const :tag "No" nil)
  717. (const :tag "Yes" t)
  718. (string :tag "Use this keyword")))
  719. (defcustom org-archive-stamp-time t
  720. "Non-nil means, add a time stamp to entries moved to an archive file.
  721. This variable is obsolete and has no effect anymore, instead add ot remove
  722. `time' from the variablle `org-archive-save-context-info'."
  723. :group 'org-archive
  724. :type 'boolean)
  725. (defcustom org-archive-save-context-info '(time file olpath category todo itags)
  726. "Parts of context info that should be stored as properties when archiving.
  727. When a subtree is moved to an archive file, it looses information given by
  728. context, like inherited tags, the category, and possibly also the TODO
  729. state (depending on the variable `org-archive-mark-done').
  730. This variable can be a list of any of the following symbols:
  731. time The time of archiving.
  732. file The file where the entry originates.
  733. itags The local tags, in the headline of the subtree.
  734. ltags The tags the subtree inherits from further up the hierarchy.
  735. todo The pre-archive TODO state.
  736. category The category, taken from file name or #+CATEGORY lines.
  737. olpath The outline path to the item. These are all headlines above
  738. the current item, separated by /, like a file path.
  739. For each symbol present in the list, a property will be created in
  740. the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
  741. information."
  742. :group 'org-archive
  743. :type '(set :greedy t
  744. (const :tag "Time" time)
  745. (const :tag "File" file)
  746. (const :tag "Category" category)
  747. (const :tag "TODO state" todo)
  748. (const :tag "TODO state" priority)
  749. (const :tag "Inherited tags" itags)
  750. (const :tag "Outline path" olpath)
  751. (const :tag "Local tags" ltags)))
  752. (defgroup org-imenu-and-speedbar nil
  753. "Options concerning imenu and speedbar in Org-mode."
  754. :tag "Org Imenu and Speedbar"
  755. :group 'org-structure)
  756. (defcustom org-imenu-depth 2
  757. "The maximum level for Imenu access to Org-mode headlines.
  758. This also applied for speedbar access."
  759. :group 'org-imenu-and-speedbar
  760. :type 'number)
  761. (defgroup org-table nil
  762. "Options concerning tables in Org-mode."
  763. :tag "Org Table"
  764. :group 'org)
  765. (defcustom org-enable-table-editor 'optimized
  766. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  767. When nil, such lines will be treated like ordinary lines.
  768. When equal to the symbol `optimized', the table editor will be optimized to
  769. do the following:
  770. - Automatic overwrite mode in front of whitespace in table fields.
  771. This makes the structure of the table stay in tact as long as the edited
  772. field does not exceed the column width.
  773. - Minimize the number of realigns. Normally, the table is aligned each time
  774. TAB or RET are pressed to move to another field. With optimization this
  775. happens only if changes to a field might have changed the column width.
  776. Optimization requires replacing the functions `self-insert-command',
  777. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  778. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  779. very good at guessing when a re-align will be necessary, but you can always
  780. force one with \\[org-ctrl-c-ctrl-c].
  781. If you would like to use the optimized version in Org-mode, but the
  782. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  783. This variable can be used to turn on and off the table editor during a session,
  784. but in order to toggle optimization, a restart is required.
  785. See also the variable `org-table-auto-blank-field'."
  786. :group 'org-table
  787. :type '(choice
  788. (const :tag "off" nil)
  789. (const :tag "on" t)
  790. (const :tag "on, optimized" optimized)))
  791. (defcustom org-table-tab-recognizes-table.el t
  792. "Non-nil means, TAB will automatically notice a table.el table.
  793. When it sees such a table, it moves point into it and - if necessary -
  794. calls `table-recognize-table'."
  795. :group 'org-table-editing
  796. :type 'boolean)
  797. (defgroup org-link nil
  798. "Options concerning links in Org-mode."
  799. :tag "Org Link"
  800. :group 'org)
  801. (defvar org-link-abbrev-alist-local nil
  802. "Buffer-local version of `org-link-abbrev-alist', which see.
  803. The value of this is taken from the #+LINK lines.")
  804. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  805. (defcustom org-link-abbrev-alist nil
  806. "Alist of link abbreviations.
  807. The car of each element is a string, to be replaced at the start of a link.
  808. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  809. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  810. [[linkkey:tag][description]]
  811. If REPLACE is a string, the tag will simply be appended to create the link.
  812. If the string contains \"%s\", the tag will be inserted there.
  813. REPLACE may also be a function that will be called with the tag as the
  814. only argument to create the link, which should be returned as a string.
  815. See the manual for examples."
  816. :group 'org-link
  817. :type 'alist)
  818. (defcustom org-descriptive-links t
  819. "Non-nil means, hide link part and only show description of bracket links.
  820. Bracket links are like [[link][descritpion]]. This variable sets the initial
  821. state in new org-mode buffers. The setting can then be toggled on a
  822. per-buffer basis from the Org->Hyperlinks menu."
  823. :group 'org-link
  824. :type 'boolean)
  825. (defcustom org-link-file-path-type 'adaptive
  826. "How the path name in file links should be stored.
  827. Valid values are:
  828. relative Relative to the current directory, i.e. the directory of the file
  829. into which the link is being inserted.
  830. absolute Absolute path, if possible with ~ for home directory.
  831. noabbrev Absolute path, no abbreviation of home directory.
  832. adaptive Use relative path for files in the current directory and sub-
  833. directories of it. For other files, use an absolute path."
  834. :group 'org-link
  835. :type '(choice
  836. (const relative)
  837. (const absolute)
  838. (const noabbrev)
  839. (const adaptive)))
  840. (defcustom org-activate-links '(bracket angle plain radio tag date)
  841. "Types of links that should be activated in Org-mode files.
  842. This is a list of symbols, each leading to the activation of a certain link
  843. type. In principle, it does not hurt to turn on most link types - there may
  844. be a small gain when turning off unused link types. The types are:
  845. bracket The recommended [[link][description]] or [[link]] links with hiding.
  846. angular Links in angular brackes that may contain whitespace like
  847. <bbdb:Carsten Dominik>.
  848. plain Plain links in normal text, no whitespace, like http://google.com.
  849. radio Text that is matched by a radio target, see manual for details.
  850. tag Tag settings in a headline (link to tag search).
  851. date Time stamps (link to calendar).
  852. Changing this variable requires a restart of Emacs to become effective."
  853. :group 'org-link
  854. :type '(set (const :tag "Double bracket links (new style)" bracket)
  855. (const :tag "Angular bracket links (old style)" angular)
  856. (const :tag "Plain text links" plain)
  857. (const :tag "Radio target matches" radio)
  858. (const :tag "Tags" tag)
  859. (const :tag "Timestamps" date)))
  860. (defgroup org-link-store nil
  861. "Options concerning storing links in Org-mode."
  862. :tag "Org Store Link"
  863. :group 'org-link)
  864. (defcustom org-email-link-description-format "Email %c: %.30s"
  865. "Format of the description part of a link to an email or usenet message.
  866. The following %-excapes will be replaced by corresponding information:
  867. %F full \"From\" field
  868. %f name, taken from \"From\" field, address if no name
  869. %T full \"To\" field
  870. %t first name in \"To\" field, address if no name
  871. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  872. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  873. %s subject
  874. %m message-id.
  875. You may use normal field width specification between the % and the letter.
  876. This is for example useful to limit the length of the subject.
  877. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  878. :group 'org-link-store
  879. :type 'string)
  880. (defcustom org-from-is-user-regexp
  881. (let (r1 r2)
  882. (when (and user-mail-address (not (string= user-mail-address "")))
  883. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  884. (when (and user-full-name (not (string= user-full-name "")))
  885. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  886. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  887. "Regexp mached against the \"From:\" header of an email or usenet message.
  888. It should match if the message is from the user him/herself."
  889. :group 'org-link-store
  890. :type 'regexp)
  891. (defcustom org-context-in-file-links t
  892. "Non-nil means, file links from `org-store-link' contain context.
  893. A search string will be added to the file name with :: as separator and
  894. used to find the context when the link is activated by the command
  895. `org-open-at-point'.
  896. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  897. negates this setting for the duration of the command."
  898. :group 'org-link-store
  899. :type 'boolean)
  900. (defcustom org-keep-stored-link-after-insertion nil
  901. "Non-nil means, keep link in list for entire session.
  902. The command `org-store-link' adds a link pointing to the current
  903. location to an internal list. These links accumulate during a session.
  904. The command `org-insert-link' can be used to insert links into any
  905. Org-mode file (offering completion for all stored links). When this
  906. option is nil, every link which has been inserted once using \\[org-insert-link]
  907. will be removed from the list, to make completing the unused links
  908. more efficient."
  909. :group 'org-link-store
  910. :type 'boolean)
  911. (defgroup org-link-follow nil
  912. "Options concerning following links in Org-mode."
  913. :tag "Org Follow Link"
  914. :group 'org-link)
  915. (defcustom org-follow-link-hook nil
  916. "Hook that is run after a link has been followed."
  917. :group 'org-link-follow
  918. :type 'hook)
  919. (defcustom org-tab-follows-link nil
  920. "Non-nil means, on links TAB will follow the link.
  921. Needs to be set before org.el is loaded."
  922. :group 'org-link-follow
  923. :type 'boolean)
  924. (defcustom org-return-follows-link nil
  925. "Non-nil means, on links RET will follow the link.
  926. Needs to be set before org.el is loaded."
  927. :group 'org-link-follow
  928. :type 'boolean)
  929. (defcustom org-mouse-1-follows-link
  930. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  931. "Non-nil means, mouse-1 on a link will follow the link.
  932. A longer mouse click will still set point. Does not work on XEmacs.
  933. Needs to be set before org.el is loaded."
  934. :group 'org-link-follow
  935. :type 'boolean)
  936. (defcustom org-mark-ring-length 4
  937. "Number of different positions to be recorded in the ring
  938. Changing this requires a restart of Emacs to work correctly."
  939. :group 'org-link-follow
  940. :type 'interger)
  941. (defcustom org-link-frame-setup
  942. '((vm . vm-visit-folder-other-frame)
  943. (gnus . gnus-other-frame)
  944. (file . find-file-other-window))
  945. "Setup the frame configuration for following links.
  946. When following a link with Emacs, it may often be useful to display
  947. this link in another window or frame. This variable can be used to
  948. set this up for the different types of links.
  949. For VM, use any of
  950. `vm-visit-folder'
  951. `vm-visit-folder-other-frame'
  952. For Gnus, use any of
  953. `gnus'
  954. `gnus-other-frame'
  955. For FILE, use any of
  956. `find-file'
  957. `find-file-other-window'
  958. `find-file-other-frame'
  959. For the calendar, use the variable `calendar-setup'.
  960. For BBDB, it is currently only possible to display the matches in
  961. another window."
  962. :group 'org-link-follow
  963. :type '(list
  964. (cons (const vm)
  965. (choice
  966. (const vm-visit-folder)
  967. (const vm-visit-folder-other-window)
  968. (const vm-visit-folder-other-frame)))
  969. (cons (const gnus)
  970. (choice
  971. (const gnus)
  972. (const gnus-other-frame)))
  973. (cons (const file)
  974. (choice
  975. (const find-file)
  976. (const find-file-other-window)
  977. (const find-file-other-frame)))))
  978. (defcustom org-display-internal-link-with-indirect-buffer nil
  979. "Non-nil means, use indirect buffer to display infile links.
  980. Activating internal links (from one location in a file to another location
  981. in the same file) normally just jumps to the location. When the link is
  982. activated with a C-u prefix (or with mouse-3), the link is displayed in
  983. another window. When this option is set, the other window actually displays
  984. an indirect buffer clone of the current buffer, to avoid any visibility
  985. changes to the current buffer."
  986. :group 'org-link-follow
  987. :type 'boolean)
  988. (defcustom org-open-non-existing-files nil
  989. "Non-nil means, `org-open-file' will open non-existing files.
  990. When nil, an error will be generated."
  991. :group 'org-link-follow
  992. :type 'boolean)
  993. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  994. "Function and arguments to call for following mailto links.
  995. This is a list with the first element being a lisp function, and the
  996. remaining elements being arguments to the function. In string arguments,
  997. %a will be replaced by the address, and %s will be replaced by the subject
  998. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  999. :group 'org-link-follow
  1000. :type '(choice
  1001. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1002. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1003. (const :tag "message-mail" (message-mail "%a" "%s"))
  1004. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1005. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1006. "Non-nil means, ask for confirmation before executing shell links.
  1007. Shell links can be dangerous: just think about a link
  1008. [[shell:rm -rf ~/*][Google Search]]
  1009. This link would show up in your Org-mode document as \"Google Search\",
  1010. but really it would remove your entire home directory.
  1011. Therefore we advise against setting this variable to nil.
  1012. Just change it to `y-or-n-p' of you want to confirm with a
  1013. single keystroke rather than having to type \"yes\"."
  1014. :group 'org-link-follow
  1015. :type '(choice
  1016. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1017. (const :tag "with y-or-n (faster)" y-or-n-p)
  1018. (const :tag "no confirmation (dangerous)" nil)))
  1019. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1020. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1021. Elisp links can be dangerous: just think about a link
  1022. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1023. This link would show up in your Org-mode document as \"Google Search\",
  1024. but really it would remove your entire home directory.
  1025. Therefore we advise against setting this variable to nil.
  1026. Just change it to `y-or-n-p' of you want to confirm with a
  1027. single keystroke rather than having to type \"yes\"."
  1028. :group 'org-link-follow
  1029. :type '(choice
  1030. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1031. (const :tag "with y-or-n (faster)" y-or-n-p)
  1032. (const :tag "no confirmation (dangerous)" nil)))
  1033. (defconst org-file-apps-defaults-gnu
  1034. '((remote . emacs)
  1035. (t . mailcap))
  1036. "Default file applications on a UNIX or GNU/Linux system.
  1037. See `org-file-apps'.")
  1038. (defconst org-file-apps-defaults-macosx
  1039. '((remote . emacs)
  1040. (t . "open %s")
  1041. ("ps" . "gv %s")
  1042. ("ps.gz" . "gv %s")
  1043. ("eps" . "gv %s")
  1044. ("eps.gz" . "gv %s")
  1045. ("dvi" . "xdvi %s")
  1046. ("fig" . "xfig %s"))
  1047. "Default file applications on a MacOS X system.
  1048. The system \"open\" is known as a default, but we use X11 applications
  1049. for some files for which the OS does not have a good default.
  1050. See `org-file-apps'.")
  1051. (defconst org-file-apps-defaults-windowsnt
  1052. (list
  1053. '(remote . emacs)
  1054. (cons t
  1055. (list (if (featurep 'xemacs)
  1056. 'mswindows-shell-execute
  1057. 'w32-shell-execute)
  1058. "open" 'file)))
  1059. "Default file applications on a Windows NT system.
  1060. The system \"open\" is used for most files.
  1061. See `org-file-apps'.")
  1062. (defcustom org-file-apps
  1063. '(
  1064. ("txt" . emacs)
  1065. ("tex" . emacs)
  1066. ("ltx" . emacs)
  1067. ("org" . emacs)
  1068. ("el" . emacs)
  1069. ("bib" . emacs)
  1070. )
  1071. "External applications for opening `file:path' items in a document.
  1072. Org-mode uses system defaults for different file types, but
  1073. you can use this variable to set the application for a given file
  1074. extension. The entries in this list are cons cells where the car identifies
  1075. files and the cdr the corresponding command. Possible values for the
  1076. file identifier are
  1077. \"ext\" A string identifying an extension
  1078. `directory' Matches a directory
  1079. `remote' Matches a remote file, accessible through tramp or efs.
  1080. Remote files most likely should be visited through Emacs
  1081. because external applications cannot handle such paths.
  1082. t Default for all remaining files
  1083. Possible values for the command are:
  1084. `emacs' The file will be visited by the current Emacs process.
  1085. `default' Use the default application for this file type.
  1086. string A command to be executed by a shell; %s will be replaced
  1087. by the path to the file.
  1088. sexp A Lisp form which will be evaluated. The file path will
  1089. be available in the Lisp variable `file'.
  1090. For more examples, see the system specific constants
  1091. `org-file-apps-defaults-macosx'
  1092. `org-file-apps-defaults-windowsnt'
  1093. `org-file-apps-defaults-gnu'."
  1094. :group 'org-link-follow
  1095. :type '(repeat
  1096. (cons (choice :value ""
  1097. (string :tag "Extension")
  1098. (const :tag "Default for unrecognized files" t)
  1099. (const :tag "Remote file" remote)
  1100. (const :tag "Links to a directory" directory))
  1101. (choice :value ""
  1102. (const :tag "Visit with Emacs" emacs)
  1103. (const :tag "Use system default" default)
  1104. (string :tag "Command")
  1105. (sexp :tag "Lisp form")))))
  1106. (defgroup org-refile nil
  1107. "Options concerning refiling entries in Org-mode."
  1108. :tag "Org Remember"
  1109. :group 'org)
  1110. (defcustom org-directory "~/org"
  1111. "Directory with org files.
  1112. This directory will be used as default to prompt for org files.
  1113. Used by the hooks for remember.el."
  1114. :group 'org-refile
  1115. :group 'org-remember
  1116. :type 'directory)
  1117. (defcustom org-default-notes-file "~/.notes"
  1118. "Default target for storing notes.
  1119. Used by the hooks for remember.el. This can be a string, or nil to mean
  1120. the value of `remember-data-file'.
  1121. You can set this on a per-template basis with the variable
  1122. `org-remember-templates'."
  1123. :group 'org-refile
  1124. :group 'org-remember
  1125. :type '(choice
  1126. (const :tag "Default from remember-data-file" nil)
  1127. file))
  1128. (defcustom org-goto-interface 'outline
  1129. "The default interface to be used for `org-goto'.
  1130. Allowed vaues are:
  1131. outline The interface shows an outline of the relevant file
  1132. and the correct heading is found by moving through
  1133. the outline or by searching with incremental search.
  1134. outline-path-completion Headlines in the current buffer are offered via
  1135. completion."
  1136. :group 'org-refile
  1137. :type '(choice
  1138. (const :tag "Outline" outline)
  1139. (const :tag "Outline-path-completion" outline-path-completion)))
  1140. (defcustom org-reverse-note-order nil
  1141. "Non-nil means, store new notes at the beginning of a file or entry.
  1142. When nil, new notes will be filed to the end of a file or entry.
  1143. This can also be a list with cons cells of regular expressions that
  1144. are matched against file names, and values."
  1145. :group 'org-remember
  1146. :type '(choice
  1147. (const :tag "Reverse always" t)
  1148. (const :tag "Reverse never" nil)
  1149. (repeat :tag "By file name regexp"
  1150. (cons regexp boolean))))
  1151. (defcustom org-refile-targets nil
  1152. "Targets for refiling entries with \\[org-refile].
  1153. This is list of cons cells. Each cell contains:
  1154. - a specification of the files to be considered, either a list of files,
  1155. or a symbol whose function or variable value will be used to retrieve
  1156. a file name or a list of file names. Nil means, refile to a different
  1157. heading in the current buffer.
  1158. - A specification of how to find candidate refile targets. This may be
  1159. any of
  1160. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1161. This tag has to be present in all target headlines, inheritance will
  1162. not be considered.
  1163. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1164. todo keyword.
  1165. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1166. headlines that are refiling targets.
  1167. - a cons cell (:level . N). Any headline of level N is considered a target.
  1168. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1169. :group 'org-remember
  1170. :type '(repeat
  1171. (cons
  1172. (choice :value org-agenda-files
  1173. (const :tag "All agenda files" org-agenda-files)
  1174. (const :tag "Current buffer" nil)
  1175. (function) (variable) (file))
  1176. (choice :tag "Identify target headline by"
  1177. (cons :tag "Specific tag" (const :tag) (string))
  1178. (cons :tag "TODO keyword" (const :todo) (string))
  1179. (cons :tag "Regular expression" (const :regexp) (regexp))
  1180. (cons :tag "Level number" (const :level) (integer))
  1181. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1182. (defcustom org-refile-use-outline-path nil
  1183. "Non-nil means, provide refile targets as paths.
  1184. So a level 3 headline will be available as level1/level2/level3.
  1185. When the value is `file', also include the file name (without directory)
  1186. into the path. When `full-file-path', include the full file path."
  1187. :group 'org-remember
  1188. :type '(choice
  1189. (const :tag "Not" nil)
  1190. (const :tag "Yes" t)
  1191. (const :tag "Start with file name" file)
  1192. (const :tag "Start with full file path" full-file-path)))
  1193. (defgroup org-todo nil
  1194. "Options concerning TODO items in Org-mode."
  1195. :tag "Org TODO"
  1196. :group 'org)
  1197. (defgroup org-progress nil
  1198. "Options concerning Progress logging in Org-mode."
  1199. :tag "Org Progress"
  1200. :group 'org-time)
  1201. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1202. "List of TODO entry keyword sequences and their interpretation.
  1203. \\<org-mode-map>This is a list of sequences.
  1204. Each sequence starts with a symbol, either `sequence' or `type',
  1205. indicating if the keywords should be interpreted as a sequence of
  1206. action steps, or as different types of TODO items. The first
  1207. keywords are states requiring action - these states will select a headline
  1208. for inclusion into the global TODO list Org-mode produces. If one of
  1209. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1210. signify that no further action is necessary. If \"|\" is not found,
  1211. the last keyword is treated as the only DONE state of the sequence.
  1212. The command \\[org-todo] cycles an entry through these states, and one
  1213. additional state where no keyword is present. For details about this
  1214. cycling, see the manual.
  1215. TODO keywords and interpretation can also be set on a per-file basis with
  1216. the special #+SEQ_TODO and #+TYP_TODO lines.
  1217. Each keyword can optionally specify a character for fast state selection
  1218. \(in combination with the variable `org-use-fast-todo-selection')
  1219. and specifiers for state change logging, using the same syntax
  1220. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1221. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1222. indicates to record a time stamp each time this state is selected.
  1223. Each keyword may also specify if a timestamp or a note should be
  1224. recorded when entering or leaving the state, by adding additional
  1225. characters in the parenthesis after the keyword. This looks like this:
  1226. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1227. record only the time of the state change. With X and Y being either
  1228. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1229. Y when leaving the state if and only if the *target* state does not
  1230. define X. You may omit any of the fast-selection key or X or /Y,
  1231. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1232. For backward compatibility, this variable may also be just a list
  1233. of keywords - in this case the interptetation (sequence or type) will be
  1234. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1235. :group 'org-todo
  1236. :group 'org-keywords
  1237. :type '(choice
  1238. (repeat :tag "Old syntax, just keywords"
  1239. (string :tag "Keyword"))
  1240. (repeat :tag "New syntax"
  1241. (cons
  1242. (choice
  1243. :tag "Interpretation"
  1244. (const :tag "Sequence (cycling hits every state)" sequence)
  1245. (const :tag "Type (cycling directly to DONE)" type))
  1246. (repeat
  1247. (string :tag "Keyword"))))))
  1248. (defvar org-todo-keywords-1 nil
  1249. "All TODO and DONE keywords active in a buffer.")
  1250. (make-variable-buffer-local 'org-todo-keywords-1)
  1251. (defvar org-todo-keywords-for-agenda nil)
  1252. (defvar org-done-keywords-for-agenda nil)
  1253. (defvar org-not-done-keywords nil)
  1254. (make-variable-buffer-local 'org-not-done-keywords)
  1255. (defvar org-done-keywords nil)
  1256. (make-variable-buffer-local 'org-done-keywords)
  1257. (defvar org-todo-heads nil)
  1258. (make-variable-buffer-local 'org-todo-heads)
  1259. (defvar org-todo-sets nil)
  1260. (make-variable-buffer-local 'org-todo-sets)
  1261. (defvar org-todo-log-states nil)
  1262. (make-variable-buffer-local 'org-todo-log-states)
  1263. (defvar org-todo-kwd-alist nil)
  1264. (make-variable-buffer-local 'org-todo-kwd-alist)
  1265. (defvar org-todo-key-alist nil)
  1266. (make-variable-buffer-local 'org-todo-key-alist)
  1267. (defvar org-todo-key-trigger nil)
  1268. (make-variable-buffer-local 'org-todo-key-trigger)
  1269. (defcustom org-todo-interpretation 'sequence
  1270. "Controls how TODO keywords are interpreted.
  1271. This variable is in principle obsolete and is only used for
  1272. backward compatibility, if the interpretation of todo keywords is
  1273. not given already in `org-todo-keywords'. See that variable for
  1274. more information."
  1275. :group 'org-todo
  1276. :group 'org-keywords
  1277. :type '(choice (const sequence)
  1278. (const type)))
  1279. (defcustom org-use-fast-todo-selection 'prefix
  1280. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1281. This variable describes if and under what circumstances the cycling
  1282. mechanism for TODO keywords will be replaced by a single-key, direct
  1283. selection scheme.
  1284. When nil, fast selection is never used.
  1285. When the symbol `prefix', it will be used when `org-todo' is called with
  1286. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1287. in an agenda buffer.
  1288. When t, fast selection is used by default. In this case, the prefix
  1289. argument forces cycling instead.
  1290. In all cases, the special interface is only used if access keys have actually
  1291. been assigned by the user, i.e. if keywords in the configuration are followed
  1292. by a letter in parenthesis, like TODO(t)."
  1293. :group 'org-todo
  1294. :type '(choice
  1295. (const :tag "Never" nil)
  1296. (const :tag "By default" t)
  1297. (const :tag "Only with C-u C-c C-t" prefix)))
  1298. (defcustom org-after-todo-state-change-hook nil
  1299. "Hook which is run after the state of a TODO item was changed.
  1300. The new state (a string with a TODO keyword, or nil) is available in the
  1301. Lisp variable `state'."
  1302. :group 'org-todo
  1303. :type 'hook)
  1304. (defcustom org-log-done nil
  1305. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1306. When equal to the list (done), also prompt for a closing note.
  1307. This can also be configured on a per-file basis by adding one of
  1308. the following lines anywhere in the buffer:
  1309. #+STARTUP: logdone
  1310. #+STARTUP: lognotedone
  1311. #+STARTUP: nologdone"
  1312. :group 'org-todo
  1313. :group 'org-progress
  1314. :type '(choice
  1315. (const :tag "No logging" nil)
  1316. (const :tag "Record CLOSED timestamp" time)
  1317. (const :tag "Record CLOSED timestamp with closing note." note)))
  1318. ;; Normalize old uses of org-log-done.
  1319. (cond
  1320. ((eq org-log-done t) (setq org-log-done 'time))
  1321. ((and (listp org-log-done) (memq 'done org-log-done))
  1322. (setq org-log-done 'note)))
  1323. (defcustom org-log-note-clock-out nil
  1324. "Non-nil means, recored a note when clocking out of an item.
  1325. This can also be configured on a per-file basis by adding one of
  1326. the following lines anywhere in the buffer:
  1327. #+STARTUP: lognoteclock-out
  1328. #+STARTUP: nolognoteclock-out"
  1329. :group 'org-todo
  1330. :group 'org-progress
  1331. :type 'boolean)
  1332. (defcustom org-log-done-with-time t
  1333. "Non-nil means, the CLOSED time stamp will contain date and time.
  1334. When nil, only the date will be recorded."
  1335. :group 'org-progress
  1336. :type 'boolean)
  1337. (defcustom org-log-note-headings
  1338. '((done . "CLOSING NOTE %t")
  1339. (state . "State %-12s %t")
  1340. (note . "Note taken on %t")
  1341. (clock-out . ""))
  1342. "Headings for notes added to entries.
  1343. The value is an alist, with the car being a symbol indicating the note
  1344. context, and the cdr is the heading to be used. The heading may also be the
  1345. empty string.
  1346. %t in the heading will be replaced by a time stamp.
  1347. %s will be replaced by the new TODO state, in double quotes.
  1348. %u will be replaced by the user name.
  1349. %U will be replaced by the full user name."
  1350. :group 'org-todo
  1351. :group 'org-progress
  1352. :type '(list :greedy t
  1353. (cons (const :tag "Heading when closing an item" done) string)
  1354. (cons (const :tag
  1355. "Heading when changing todo state (todo sequence only)"
  1356. state) string)
  1357. (cons (const :tag "Heading when just taking a note" note) string)
  1358. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1359. (unless (assq 'note org-log-note-headings)
  1360. (push '(note . "%t") org-log-note-headings))
  1361. (defcustom org-log-states-order-reversed t
  1362. "Non-nil means, the latest state change note will be directly after heading.
  1363. When nil, the notes will be orderer according to time."
  1364. :group 'org-todo
  1365. :group 'org-progress
  1366. :type 'boolean)
  1367. (defcustom org-log-repeat 'time
  1368. "Non-nil means, record moving through the DONE state when triggering repeat.
  1369. An auto-repeating tasks is immediately switched back to TODO when marked
  1370. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1371. the TODO keyword definition, or recording a cloing note by setting
  1372. `org-log-done', there will be no record of the task moving trhough DONE.
  1373. This variable forces taking a note anyway. Possible values are:
  1374. nil Don't force a record
  1375. time Record a time stamp
  1376. note Record a note
  1377. This option can also be set with on a per-file-basis with
  1378. #+STARTUP: logrepeat
  1379. #+STARTUP: lognoterepeat
  1380. #+STARTUP: nologrepeat
  1381. You can have local logging settings for a subtree by setting the LOGGING
  1382. property to one or more of these keywords."
  1383. :group 'org-todo
  1384. :group 'org-progress
  1385. :type '(choice
  1386. (const :tag "Don't force a record" nil)
  1387. (const :tag "Force recording the DONE state" time)
  1388. (const :tag "Force recording a note with the DONE state" note)))
  1389. (defgroup org-priorities nil
  1390. "Priorities in Org-mode."
  1391. :tag "Org Priorities"
  1392. :group 'org-todo)
  1393. (defcustom org-highest-priority ?A
  1394. "The highest priority of TODO items. A character like ?A, ?B etc.
  1395. Must have a smaller ASCII number than `org-lowest-priority'."
  1396. :group 'org-priorities
  1397. :type 'character)
  1398. (defcustom org-lowest-priority ?C
  1399. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1400. Must have a larger ASCII number than `org-highest-priority'."
  1401. :group 'org-priorities
  1402. :type 'character)
  1403. (defcustom org-default-priority ?B
  1404. "The default priority of TODO items.
  1405. This is the priority an item get if no explicit priority is given."
  1406. :group 'org-priorities
  1407. :type 'character)
  1408. (defcustom org-priority-start-cycle-with-default t
  1409. "Non-nil means, start with default priority when starting to cycle.
  1410. When this is nil, the first step in the cycle will be (depending on the
  1411. command used) one higher or lower that the default priority."
  1412. :group 'org-priorities
  1413. :type 'boolean)
  1414. (defgroup org-time nil
  1415. "Options concerning time stamps and deadlines in Org-mode."
  1416. :tag "Org Time"
  1417. :group 'org)
  1418. (defcustom org-insert-labeled-timestamps-at-point nil
  1419. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1420. When nil, these labeled time stamps are forces into the second line of an
  1421. entry, just after the headline. When scheduling from the global TODO list,
  1422. the time stamp will always be forced into the second line."
  1423. :group 'org-time
  1424. :type 'boolean)
  1425. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1426. "Formats for `format-time-string' which are used for time stamps.
  1427. It is not recommended to change this constant.")
  1428. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1429. "Number of minutes to round time stamps to.
  1430. These are two values, the first applies when first creating a time stamp.
  1431. The second applies when changing it with the commands `S-up' and `S-down'.
  1432. When changing the time stamp, this means that it will change in steps
  1433. of N minutes, as given by the second value.
  1434. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1435. numbers should be factors of 60, so for example 5, 10, 15.
  1436. When this is larger than 1, you can still force an exact time-stamp by using
  1437. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1438. and by using a prefix arg to `S-up/down' to specify the exact number
  1439. of minutes to shift."
  1440. :group 'org-time
  1441. :get '(lambda (var) ; Make sure all entries have 5 elements
  1442. (if (integerp (default-value var))
  1443. (list (default-value var) 5)
  1444. (default-value var)))
  1445. :type '(list
  1446. (integer :tag "when inserting times")
  1447. (integer :tag "when modifying times")))
  1448. ;; Normalize old customizations of this variable.
  1449. (when (integerp org-time-stamp-rounding-minutes)
  1450. (setq org-time-stamp-rounding-minutes
  1451. (list org-time-stamp-rounding-minutes
  1452. org-time-stamp-rounding-minutes)))
  1453. (defcustom org-display-custom-times nil
  1454. "Non-nil means, overlay custom formats over all time stamps.
  1455. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1456. To turn this on on a per-file basis, insert anywhere in the file:
  1457. #+STARTUP: customtime"
  1458. :group 'org-time
  1459. :set 'set-default
  1460. :type 'sexp)
  1461. (make-variable-buffer-local 'org-display-custom-times)
  1462. (defcustom org-time-stamp-custom-formats
  1463. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1464. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1465. These are overlayed over the default ISO format if the variable
  1466. `org-display-custom-times' is set. Time like %H:%M should be at the
  1467. end of the second format."
  1468. :group 'org-time
  1469. :type 'sexp)
  1470. (defun org-time-stamp-format (&optional long inactive)
  1471. "Get the right format for a time string."
  1472. (let ((f (if long (cdr org-time-stamp-formats)
  1473. (car org-time-stamp-formats))))
  1474. (if inactive
  1475. (concat "[" (substring f 1 -1) "]")
  1476. f)))
  1477. (defcustom org-deadline-warning-days 14
  1478. "No. of days before expiration during which a deadline becomes active.
  1479. This variable governs the display in sparse trees and in the agenda.
  1480. When 0 or negative, it means use this number (the absolute value of it)
  1481. even if a deadline has a different individual lead time specified."
  1482. :group 'org-time
  1483. :group 'org-agenda-daily/weekly
  1484. :type 'number)
  1485. (defcustom org-read-date-prefer-future t
  1486. "Non-nil means, assume future for incomplete date input from user.
  1487. This affects the following situations:
  1488. 1. The user gives a day, but no month.
  1489. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1490. read this as the third of *next* month. However, if you enter \"17\",
  1491. it will be considered as *this* month.
  1492. 2. The user gives a month but not a year.
  1493. For example, if it is april and you enter \"feb 2\", this will be read
  1494. as feb 2, *next* year. \"May 5\", however, will be this year.
  1495. Currently this does not work for ISO week specifications.
  1496. When this option is nil, the current month and year will always be used
  1497. as defaults."
  1498. :group 'org-time
  1499. :type 'boolean)
  1500. (defcustom org-read-date-display-live t
  1501. "Non-nil means, display current interpretation of date prompt live.
  1502. This display will be in an overlay, in the minibuffer."
  1503. :group 'org-time
  1504. :type 'boolean)
  1505. (defcustom org-read-date-popup-calendar t
  1506. "Non-nil means, pop up a calendar when prompting for a date.
  1507. In the calendar, the date can be selected with mouse-1. However, the
  1508. minibuffer will also be active, and you can simply enter the date as well.
  1509. When nil, only the minibuffer will be available."
  1510. :group 'org-time
  1511. :type 'boolean)
  1512. (if (fboundp 'defvaralias)
  1513. (defvaralias 'org-popup-calendar-for-date-prompt
  1514. 'org-read-date-popup-calendar))
  1515. (defcustom org-extend-today-until 0
  1516. "The hour when your day really ends.
  1517. This has influence for the following applications:
  1518. - When switching the agenda to \"today\". It it is still earlier than
  1519. the time given here, the day recognized as TODAY is actually yesterday.
  1520. - When a date is read from the user and it is still before the time given
  1521. here, the current date and time will be assumed to be yesterday, 23:59.
  1522. FIXME:
  1523. IMPORTANT: This is still a very experimental feature, it may disappear
  1524. again or it may be extended to mean more things."
  1525. :group 'org-time
  1526. :type 'number)
  1527. (defcustom org-edit-timestamp-down-means-later nil
  1528. "Non-nil means, S-down will increase the time in a time stamp.
  1529. When nil, S-up will increase."
  1530. :group 'org-time
  1531. :type 'boolean)
  1532. (defcustom org-calendar-follow-timestamp-change t
  1533. "Non-nil means, make the calendar window follow timestamp changes.
  1534. When a timestamp is modified and the calendar window is visible, it will be
  1535. moved to the new date."
  1536. :group 'org-time
  1537. :type 'boolean)
  1538. (defgroup org-tags nil
  1539. "Options concerning tags in Org-mode."
  1540. :tag "Org Tags"
  1541. :group 'org)
  1542. (defcustom org-tag-alist nil
  1543. "List of tags allowed in Org-mode files.
  1544. When this list is nil, Org-mode will base TAG input on what is already in the
  1545. buffer.
  1546. The value of this variable is an alist, the car of each entry must be a
  1547. keyword as a string, the cdr may be a character that is used to select
  1548. that tag through the fast-tag-selection interface.
  1549. See the manual for details."
  1550. :group 'org-tags
  1551. :type '(repeat
  1552. (choice
  1553. (cons (string :tag "Tag name")
  1554. (character :tag "Access char"))
  1555. (const :tag "Start radio group" (:startgroup))
  1556. (const :tag "End radio group" (:endgroup)))))
  1557. (defcustom org-use-fast-tag-selection 'auto
  1558. "Non-nil means, use fast tag selection scheme.
  1559. This is a special interface to select and deselect tags with single keys.
  1560. When nil, fast selection is never used.
  1561. When the symbol `auto', fast selection is used if and only if selection
  1562. characters for tags have been configured, either through the variable
  1563. `org-tag-alist' or through a #+TAGS line in the buffer.
  1564. When t, fast selection is always used and selection keys are assigned
  1565. automatically if necessary."
  1566. :group 'org-tags
  1567. :type '(choice
  1568. (const :tag "Always" t)
  1569. (const :tag "Never" nil)
  1570. (const :tag "When selection characters are configured" 'auto)))
  1571. (defcustom org-fast-tag-selection-single-key nil
  1572. "Non-nil means, fast tag selection exits after first change.
  1573. When nil, you have to press RET to exit it.
  1574. During fast tag selection, you can toggle this flag with `C-c'.
  1575. This variable can also have the value `expert'. In this case, the window
  1576. displaying the tags menu is not even shown, until you press C-c again."
  1577. :group 'org-tags
  1578. :type '(choice
  1579. (const :tag "No" nil)
  1580. (const :tag "Yes" t)
  1581. (const :tag "Expert" expert)))
  1582. (defvar org-fast-tag-selection-include-todo nil
  1583. "Non-nil means, fast tags selection interface will also offer TODO states.
  1584. This is an undocumented feature, you should not rely on it.")
  1585. (defcustom org-tags-column -80
  1586. "The column to which tags should be indented in a headline.
  1587. If this number is positive, it specifies the column. If it is negative,
  1588. it means that the tags should be flushright to that column. For example,
  1589. -80 works well for a normal 80 character screen."
  1590. :group 'org-tags
  1591. :type 'integer)
  1592. (defcustom org-auto-align-tags t
  1593. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1594. These operations change the length of a headline and therefore shift
  1595. the tags around. With this options turned on, after each such operation
  1596. the tags are again aligned to `org-tags-column'."
  1597. :group 'org-tags
  1598. :type 'boolean)
  1599. (defcustom org-use-tag-inheritance t
  1600. "Non-nil means, tags in levels apply also for sublevels.
  1601. When nil, only the tags directly given in a specific line apply there.
  1602. If you turn off this option, you very likely want to turn on the
  1603. companion option `org-tags-match-list-sublevels'.
  1604. This may also be a list of tags that should be inherited, or a regexp that
  1605. matches tags that should be inherited."
  1606. :group 'org-tags
  1607. :type '(choice
  1608. (const :tag "Not" nil)
  1609. (const :tag "Always" t)
  1610. (repeat :tag "Specific tags" (string :tag "Tag"))
  1611. (regexp :tag "Tags matched by regexp")))
  1612. (defun org-tag-inherit-p (tag)
  1613. "Check if TAG is one that should be inherited."
  1614. (cond
  1615. ((eq org-use-tag-inheritance t) t)
  1616. ((not org-use-tag-inheritance) nil)
  1617. ((stringp org-use-tag-inheritance)
  1618. (string-match org-use-tag-inheritance tag))
  1619. ((listp org-use-tag-inheritance)
  1620. (member tag org-use-tag-inheritance))
  1621. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1622. (defcustom org-tags-match-list-sublevels nil
  1623. "Non-nil means list also sublevels of headlines matching tag search.
  1624. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1625. the sublevels of a headline matching a tag search often also match
  1626. the same search. Listing all of them can create very long lists.
  1627. Setting this variable to nil causes subtrees of a match to be skipped.
  1628. This option is off by default, because inheritance in on. If you turn
  1629. inheritance off, you very likely want to turn this option on.
  1630. As a special case, if the tag search is restricted to TODO items, the
  1631. value of this variable is ignored and sublevels are always checked, to
  1632. make sure all corresponding TODO items find their way into the list."
  1633. :group 'org-tags
  1634. :type 'boolean)
  1635. (defvar org-tags-history nil
  1636. "History of minibuffer reads for tags.")
  1637. (defvar org-last-tags-completion-table nil
  1638. "The last used completion table for tags.")
  1639. (defvar org-after-tags-change-hook nil
  1640. "Hook that is run after the tags in a line have changed.")
  1641. (defgroup org-properties nil
  1642. "Options concerning properties in Org-mode."
  1643. :tag "Org Properties"
  1644. :group 'org)
  1645. (defcustom org-property-format "%-10s %s"
  1646. "How property key/value pairs should be formatted by `indent-line'.
  1647. When `indent-line' hits a property definition, it will format the line
  1648. according to this format, mainly to make sure that the values are
  1649. lined-up with respect to each other."
  1650. :group 'org-properties
  1651. :type 'string)
  1652. (defcustom org-use-property-inheritance nil
  1653. "Non-nil means, properties apply also for sublevels.
  1654. This setting is chiefly used during property searches. Turning it on can
  1655. cause significant overhead when doing a search, which is why it is not
  1656. on by default.
  1657. When nil, only the properties directly given in the current entry count.
  1658. When t, every property is inherited. The value may also be a list of
  1659. properties that should have inheritance, or a regular expression matching
  1660. properties that should be inherited.
  1661. However, note that some special properties use inheritance under special
  1662. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1663. and the properties ending in \"_ALL\" when they are used as descriptor
  1664. for valid values of a property.
  1665. Note for programmers:
  1666. When querying an entry with `org-entry-get', you can control if inheritance
  1667. should be used. By default, `org-entry-get' looks only at the local
  1668. properties. You can request inheritance by setting the inherit argument
  1669. to t (to force inheritance) or to `selective' (to respect the setting
  1670. in this variable)."
  1671. :group 'org-properties
  1672. :type '(choice
  1673. (const :tag "Not" nil)
  1674. (const :tag "Always" t)
  1675. (repeat :tag "Specific properties" (string :tag "Property"))
  1676. (regexp :tag "Properties matched by regexp")))
  1677. (defun org-property-inherit-p (property)
  1678. "Check if PROPERTY is one that should be inherited."
  1679. (cond
  1680. ((eq org-use-property-inheritance t) t)
  1681. ((not org-use-property-inheritance) nil)
  1682. ((stringp org-use-property-inheritance)
  1683. (string-match org-use-property-inheritance property))
  1684. ((listp org-use-property-inheritance)
  1685. (member property org-use-property-inheritance))
  1686. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1687. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1688. "The default column format, if no other format has been defined.
  1689. This variable can be set on the per-file basis by inserting a line
  1690. #+COLUMNS: %25ITEM ....."
  1691. :group 'org-properties
  1692. :type 'string)
  1693. (defcustom org-global-properties nil
  1694. "List of property/value pairs that can be inherited by any entry.
  1695. You can set buffer-local values for this by adding lines like
  1696. #+PROPERTY: NAME VALUE"
  1697. :group 'org-properties
  1698. :type '(repeat
  1699. (cons (string :tag "Property")
  1700. (string :tag "Value"))))
  1701. (defvar org-local-properties nil
  1702. "List of property/value pairs that can be inherited by any entry.
  1703. Valid for the current buffer.
  1704. This variable is populated from #+PROPERTY lines.")
  1705. (defgroup org-agenda nil
  1706. "Options concerning agenda views in Org-mode."
  1707. :tag "Org Agenda"
  1708. :group 'org)
  1709. (defvar org-category nil
  1710. "Variable used by org files to set a category for agenda display.
  1711. Such files should use a file variable to set it, for example
  1712. # -*- mode: org; org-category: \"ELisp\"
  1713. or contain a special line
  1714. #+CATEGORY: ELisp
  1715. If the file does not specify a category, then file's base name
  1716. is used instead.")
  1717. (make-variable-buffer-local 'org-category)
  1718. (defcustom org-agenda-files nil
  1719. "The files to be used for agenda display.
  1720. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1721. \\[org-remove-file]. You can also use customize to edit the list.
  1722. If an entry is a directory, all files in that directory that are matched by
  1723. `org-agenda-file-regexp' will be part of the file list.
  1724. If the value of the variable is not a list but a single file name, then
  1725. the list of agenda files is actually stored and maintained in that file, one
  1726. agenda file per line."
  1727. :group 'org-agenda
  1728. :type '(choice
  1729. (repeat :tag "List of files and directories" file)
  1730. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1731. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1732. "Regular expression to match files for `org-agenda-files'.
  1733. If any element in the list in that variable contains a directory instead
  1734. of a normal file, all files in that directory that are matched by this
  1735. regular expression will be included."
  1736. :group 'org-agenda
  1737. :type 'regexp)
  1738. (defcustom org-agenda-text-search-extra-files nil
  1739. "List of extra files to be searched by text search commands.
  1740. These files will be search in addition to the agenda files bu the
  1741. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1742. Note that these files will only be searched for text search commands,
  1743. not for the other agenda views like todo lists, tag earches or the weekly
  1744. agenda. This variable is intended to list notes and possibly archive files
  1745. that should also be searched by these two commands."
  1746. :group 'org-agenda
  1747. :type '(repeat file))
  1748. (if (fboundp 'defvaralias)
  1749. (defvaralias 'org-agenda-multi-occur-extra-files
  1750. 'org-agenda-text-search-extra-files))
  1751. (defcustom org-agenda-skip-unavailable-files nil
  1752. "t means to just skip non-reachable files in `org-agenda-files'.
  1753. Nil means to remove them, after a query, from the list."
  1754. :group 'org-agenda
  1755. :type 'boolean)
  1756. (defcustom org-calendar-to-agenda-key [?c]
  1757. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1758. The command `org-calendar-goto-agenda' will be bound to this key. The
  1759. default is the character `c' because then `c' can be used to switch back and
  1760. forth between agenda and calendar."
  1761. :group 'org-agenda
  1762. :type 'sexp)
  1763. (eval-after-load "calendar"
  1764. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1765. 'org-calendar-goto-agenda))
  1766. (defgroup org-latex nil
  1767. "Options for embedding LaTeX code into Org-mode."
  1768. :tag "Org LaTeX"
  1769. :group 'org)
  1770. (defcustom org-format-latex-options
  1771. '(:foreground default :background default :scale 1.0
  1772. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1773. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1774. "Options for creating images from LaTeX fragments.
  1775. This is a property list with the following properties:
  1776. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1777. `default' means use the forground of the default face.
  1778. :background the background color, or \"Transparent\".
  1779. `default' means use the background of the default face.
  1780. :scale a scaling factor for the size of the images
  1781. :html-foreground, :html-background, :html-scale
  1782. The same numbers for HTML export.
  1783. :matchers a list indicating which matchers should be used to
  1784. find LaTeX fragments. Valid members of this list are:
  1785. \"begin\" find environments
  1786. \"$\" find math expressions surrounded by $...$
  1787. \"$$\" find math expressions surrounded by $$....$$
  1788. \"\\(\" find math expressions surrounded by \\(...\\)
  1789. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1790. :group 'org-latex
  1791. :type 'plist)
  1792. (defcustom org-format-latex-header "\\documentclass{article}
  1793. \\usepackage{fullpage} % do not remove
  1794. \\usepackage{amssymb}
  1795. \\usepackage[usenames]{color}
  1796. \\usepackage{amsmath}
  1797. \\usepackage{latexsym}
  1798. \\usepackage[mathscr]{eucal}
  1799. \\pagestyle{empty} % do not remove"
  1800. "The document header used for processing LaTeX fragments."
  1801. :group 'org-latex
  1802. :type 'string)
  1803. (defgroup org-font-lock nil
  1804. "Font-lock settings for highlighting in Org-mode."
  1805. :tag "Org Font Lock"
  1806. :group 'org)
  1807. (defcustom org-level-color-stars-only nil
  1808. "Non-nil means fontify only the stars in each headline.
  1809. When nil, the entire headline is fontified.
  1810. Changing it requires restart of `font-lock-mode' to become effective
  1811. also in regions already fontified."
  1812. :group 'org-font-lock
  1813. :type 'boolean)
  1814. (defcustom org-hide-leading-stars nil
  1815. "Non-nil means, hide the first N-1 stars in a headline.
  1816. This works by using the face `org-hide' for these stars. This
  1817. face is white for a light background, and black for a dark
  1818. background. You may have to customize the face `org-hide' to
  1819. make this work.
  1820. Changing it requires restart of `font-lock-mode' to become effective
  1821. also in regions already fontified.
  1822. You may also set this on a per-file basis by adding one of the following
  1823. lines to the buffer:
  1824. #+STARTUP: hidestars
  1825. #+STARTUP: showstars"
  1826. :group 'org-font-lock
  1827. :type 'boolean)
  1828. (defcustom org-fontify-done-headline nil
  1829. "Non-nil means, change the face of a headline if it is marked DONE.
  1830. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1831. When this is non-nil, the headline after the keyword is set to the
  1832. `org-headline-done' as an additional indication."
  1833. :group 'org-font-lock
  1834. :type 'boolean)
  1835. (defcustom org-fontify-emphasized-text t
  1836. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1837. Changing this variable requires a restart of Emacs to take effect."
  1838. :group 'org-font-lock
  1839. :type 'boolean)
  1840. (defcustom org-highlight-latex-fragments-and-specials nil
  1841. "Non-nil means, fontify what is treated specially by the exporters."
  1842. :group 'org-font-lock
  1843. :type 'boolean)
  1844. (defcustom org-hide-emphasis-markers nil
  1845. "Non-nil mean font-lock should hide the emphasis marker characters."
  1846. :group 'org-font-lock
  1847. :type 'boolean)
  1848. (defvar org-emph-re nil
  1849. "Regular expression for matching emphasis.")
  1850. (defvar org-verbatim-re nil
  1851. "Regular expression for matching verbatim text.")
  1852. (defvar org-emphasis-regexp-components) ; defined just below
  1853. (defvar org-emphasis-alist) ; defined just below
  1854. (defun org-set-emph-re (var val)
  1855. "Set variable and compute the emphasis regular expression."
  1856. (set var val)
  1857. (when (and (boundp 'org-emphasis-alist)
  1858. (boundp 'org-emphasis-regexp-components)
  1859. org-emphasis-alist org-emphasis-regexp-components)
  1860. (let* ((e org-emphasis-regexp-components)
  1861. (pre (car e))
  1862. (post (nth 1 e))
  1863. (border (nth 2 e))
  1864. (body (nth 3 e))
  1865. (nl (nth 4 e))
  1866. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1867. (body1 (concat body "*?"))
  1868. (markers (mapconcat 'car org-emphasis-alist ""))
  1869. (vmarkers (mapconcat
  1870. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1871. org-emphasis-alist "")))
  1872. ;; make sure special characters appear at the right position in the class
  1873. (if (string-match "\\^" markers)
  1874. (setq markers (concat (replace-match "" t t markers) "^")))
  1875. (if (string-match "-" markers)
  1876. (setq markers (concat (replace-match "" t t markers) "-")))
  1877. (if (string-match "\\^" vmarkers)
  1878. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1879. (if (string-match "-" vmarkers)
  1880. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1881. (if (> nl 0)
  1882. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1883. (int-to-string nl) "\\}")))
  1884. ;; Make the regexp
  1885. (setq org-emph-re
  1886. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1887. "\\("
  1888. "\\([" markers "]\\)"
  1889. "\\("
  1890. "[^" border "]\\|"
  1891. "[^" border (if (and nil stacked) markers) "]"
  1892. body1
  1893. "[^" border (if (and nil stacked) markers) "]"
  1894. "\\)"
  1895. "\\3\\)"
  1896. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1897. (setq org-verbatim-re
  1898. (concat "\\([" pre "]\\|^\\)"
  1899. "\\("
  1900. "\\([" vmarkers "]\\)"
  1901. "\\("
  1902. "[^" border "]\\|"
  1903. "[^" border "]"
  1904. body1
  1905. "[^" border "]"
  1906. "\\)"
  1907. "\\3\\)"
  1908. "\\([" post "]\\|$\\)")))))
  1909. (defcustom org-emphasis-regexp-components
  1910. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1911. "Components used to build the regular expression for emphasis.
  1912. This is a list with 6 entries. Terminology: In an emphasis string
  1913. like \" *strong word* \", we call the initial space PREMATCH, the final
  1914. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1915. and \"trong wor\" is the body. The different components in this variable
  1916. specify what is allowed/forbidden in each part:
  1917. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1918. post Chars allowed as postmatch. End of line will be allowed too.
  1919. border The chars *forbidden* as border characters.
  1920. body-regexp A regexp like \".\" to match a body character. Don't use
  1921. non-shy groups here, and don't allow newline here.
  1922. newline The maximum number of newlines allowed in an emphasis exp.
  1923. Use customize to modify this, or restart Emacs after changing it."
  1924. :group 'org-font-lock
  1925. :set 'org-set-emph-re
  1926. :type '(list
  1927. (sexp :tag "Allowed chars in pre ")
  1928. (sexp :tag "Allowed chars in post ")
  1929. (sexp :tag "Forbidden chars in border ")
  1930. (sexp :tag "Regexp for body ")
  1931. (integer :tag "number of newlines allowed")
  1932. (option (boolean :tag "Stacking (DISABLED) "))))
  1933. (defcustom org-emphasis-alist
  1934. '(("*" bold "<b>" "</b>")
  1935. ("/" italic "<i>" "</i>")
  1936. ("_" underline "<u>" "</u>")
  1937. ("=" org-code "<code>" "</code>" verbatim)
  1938. ("~" org-verbatim "" "" verbatim)
  1939. ("+" (:strike-through t) "<del>" "</del>")
  1940. )
  1941. "Special syntax for emphasized text.
  1942. Text starting and ending with a special character will be emphasized, for
  1943. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1944. characters, the face to be used by font-lock for highlighting in Org-mode
  1945. Emacs buffers, and the HTML tags to be used for this.
  1946. Use customize to modify this, or restart Emacs after changing it."
  1947. :group 'org-font-lock
  1948. :set 'org-set-emph-re
  1949. :type '(repeat
  1950. (list
  1951. (string :tag "Marker character")
  1952. (choice
  1953. (face :tag "Font-lock-face")
  1954. (plist :tag "Face property list"))
  1955. (string :tag "HTML start tag")
  1956. (string :tag "HTML end tag")
  1957. (option (const verbatim)))))
  1958. ;;; Miscellaneous options
  1959. (defgroup org-completion nil
  1960. "Completion in Org-mode."
  1961. :tag "Org Completion"
  1962. :group 'org)
  1963. (defcustom org-completion-fallback-command 'hippie-expand
  1964. "The expansion command called by \\[org-complete] in normal context.
  1965. Normal means, no org-mode-specific context."
  1966. :group 'org-completion
  1967. :type 'function)
  1968. ;;; The faces
  1969. (defgroup org-faces nil
  1970. "Faces in Org-mode."
  1971. :tag "Org Faces"
  1972. :group 'org-font-lock)
  1973. (defface org-hide
  1974. '((((background light)) (:foreground "white"))
  1975. (((background dark)) (:foreground "black")))
  1976. "Face used to hide leading stars in headlines.
  1977. The forground color of this face should be equal to the background
  1978. color of the frame."
  1979. :group 'org-faces)
  1980. (defface org-level-1 ;; font-lock-function-name-face
  1981. (org-compatible-face 'outline-1
  1982. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1983. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1984. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1985. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1986. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  1987. (t (:bold t))))
  1988. "Face used for level 1 headlines."
  1989. :group 'org-faces)
  1990. (defface org-level-2 ;; font-lock-variable-name-face
  1991. (org-compatible-face 'outline-2
  1992. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  1993. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  1994. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  1995. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  1996. (t (:bold t))))
  1997. "Face used for level 2 headlines."
  1998. :group 'org-faces)
  1999. (defface org-level-3 ;; font-lock-keyword-face
  2000. (org-compatible-face 'outline-3
  2001. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2002. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2003. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2004. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2005. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2006. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2007. (t (:bold t))))
  2008. "Face used for level 3 headlines."
  2009. :group 'org-faces)
  2010. (defface org-level-4 ;; font-lock-comment-face
  2011. (org-compatible-face 'outline-4
  2012. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2013. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2014. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2015. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2016. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2017. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2018. (t (:bold t))))
  2019. "Face used for level 4 headlines."
  2020. :group 'org-faces)
  2021. (defface org-level-5 ;; font-lock-type-face
  2022. (org-compatible-face 'outline-5
  2023. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2024. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2025. (((class color) (min-colors 8)) (:foreground "green"))))
  2026. "Face used for level 5 headlines."
  2027. :group 'org-faces)
  2028. (defface org-level-6 ;; font-lock-constant-face
  2029. (org-compatible-face 'outline-6
  2030. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2031. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2032. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2033. "Face used for level 6 headlines."
  2034. :group 'org-faces)
  2035. (defface org-level-7 ;; font-lock-builtin-face
  2036. (org-compatible-face 'outline-7
  2037. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2038. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2039. (((class color) (min-colors 8)) (:foreground "blue"))))
  2040. "Face used for level 7 headlines."
  2041. :group 'org-faces)
  2042. (defface org-level-8 ;; font-lock-string-face
  2043. (org-compatible-face 'outline-8
  2044. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2045. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2046. (((class color) (min-colors 8)) (:foreground "green"))))
  2047. "Face used for level 8 headlines."
  2048. :group 'org-faces)
  2049. (defface org-special-keyword ;; font-lock-string-face
  2050. (org-compatible-face nil
  2051. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2052. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2053. (t (:italic t))))
  2054. "Face used for special keywords."
  2055. :group 'org-faces)
  2056. (defface org-drawer ;; font-lock-function-name-face
  2057. (org-compatible-face nil
  2058. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2059. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2060. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2061. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2062. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2063. (t (:bold t))))
  2064. "Face used for drawers."
  2065. :group 'org-faces)
  2066. (defface org-property-value nil
  2067. "Face used for the value of a property."
  2068. :group 'org-faces)
  2069. (defface org-column
  2070. (org-compatible-face nil
  2071. '((((class color) (min-colors 16) (background light))
  2072. (:background "grey90"))
  2073. (((class color) (min-colors 16) (background dark))
  2074. (:background "grey30"))
  2075. (((class color) (min-colors 8))
  2076. (:background "cyan" :foreground "black"))
  2077. (t (:inverse-video t))))
  2078. "Face for column display of entry properties."
  2079. :group 'org-faces)
  2080. (when (fboundp 'set-face-attribute)
  2081. ;; Make sure that a fixed-width face is used when we have a column table.
  2082. (set-face-attribute 'org-column nil
  2083. :height (face-attribute 'default :height)
  2084. :family (face-attribute 'default :family)))
  2085. (defface org-warning
  2086. (org-compatible-face 'font-lock-warning-face
  2087. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2088. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2089. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2090. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2091. (t (:bold t))))
  2092. "Face for deadlines and TODO keywords."
  2093. :group 'org-faces)
  2094. (defface org-archived ; similar to shadow
  2095. (org-compatible-face 'shadow
  2096. '((((class color grayscale) (min-colors 88) (background light))
  2097. (:foreground "grey50"))
  2098. (((class color grayscale) (min-colors 88) (background dark))
  2099. (:foreground "grey70"))
  2100. (((class color) (min-colors 8) (background light))
  2101. (:foreground "green"))
  2102. (((class color) (min-colors 8) (background dark))
  2103. (:foreground "yellow"))))
  2104. "Face for headline with the ARCHIVE tag."
  2105. :group 'org-faces)
  2106. (defface org-link
  2107. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2108. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2109. (t (:underline t)))
  2110. "Face for links."
  2111. :group 'org-faces)
  2112. (defface org-ellipsis
  2113. '((((class color) (background light)) (:foreground "DarkGoldenrod" :underline t))
  2114. (((class color) (background dark)) (:foreground "LightGoldenrod" :underline t))
  2115. (t (:strike-through t)))
  2116. "Face for the ellipsis in folded text."
  2117. :group 'org-faces)
  2118. (defface org-target
  2119. '((((class color) (background light)) (:underline t))
  2120. (((class color) (background dark)) (:underline t))
  2121. (t (:underline t)))
  2122. "Face for links."
  2123. :group 'org-faces)
  2124. (defface org-date
  2125. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2126. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2127. (t (:underline t)))
  2128. "Face for links."
  2129. :group 'org-faces)
  2130. (defface org-sexp-date
  2131. '((((class color) (background light)) (:foreground "Purple"))
  2132. (((class color) (background dark)) (:foreground "Cyan"))
  2133. (t (:underline t)))
  2134. "Face for links."
  2135. :group 'org-faces)
  2136. (defface org-tag
  2137. '((t (:bold t)))
  2138. "Face for tags."
  2139. :group 'org-faces)
  2140. (defface org-todo ; font-lock-warning-face
  2141. (org-compatible-face nil
  2142. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2143. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2144. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2145. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2146. (t (:inverse-video t :bold t))))
  2147. "Face for TODO keywords."
  2148. :group 'org-faces)
  2149. (defface org-done ;; font-lock-type-face
  2150. (org-compatible-face nil
  2151. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2152. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2153. (((class color) (min-colors 8)) (:foreground "green"))
  2154. (t (:bold t))))
  2155. "Face used for todo keywords that indicate DONE items."
  2156. :group 'org-faces)
  2157. (defface org-headline-done ;; font-lock-string-face
  2158. (org-compatible-face nil
  2159. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2160. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2161. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2162. "Face used to indicate that a headline is DONE.
  2163. This face is only used if `org-fontify-done-headline' is set. If applies
  2164. to the part of the headline after the DONE keyword."
  2165. :group 'org-faces)
  2166. (defcustom org-todo-keyword-faces nil
  2167. "Faces for specific TODO keywords.
  2168. This is a list of cons cells, with TODO keywords in the car
  2169. and faces in the cdr. The face can be a symbol, or a property
  2170. list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
  2171. :group 'org-faces
  2172. :group 'org-todo
  2173. :type '(repeat
  2174. (cons
  2175. (string :tag "keyword")
  2176. (sexp :tag "face"))))
  2177. (defface org-table ;; font-lock-function-name-face
  2178. (org-compatible-face nil
  2179. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2180. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2181. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2182. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2183. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2184. (((class color) (min-colors 8) (background dark)))))
  2185. "Face used for tables."
  2186. :group 'org-faces)
  2187. (defface org-formula
  2188. (org-compatible-face nil
  2189. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2190. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2191. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2192. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2193. (t (:bold t :italic t))))
  2194. "Face for formulas."
  2195. :group 'org-faces)
  2196. (defface org-code
  2197. (org-compatible-face nil
  2198. '((((class color grayscale) (min-colors 88) (background light))
  2199. (:foreground "grey50"))
  2200. (((class color grayscale) (min-colors 88) (background dark))
  2201. (:foreground "grey70"))
  2202. (((class color) (min-colors 8) (background light))
  2203. (:foreground "green"))
  2204. (((class color) (min-colors 8) (background dark))
  2205. (:foreground "yellow"))))
  2206. "Face for fixed-with text like code snippets."
  2207. :group 'org-faces
  2208. :version "22.1")
  2209. (defface org-verbatim
  2210. (org-compatible-face nil
  2211. '((((class color grayscale) (min-colors 88) (background light))
  2212. (:foreground "grey50" :underline t))
  2213. (((class color grayscale) (min-colors 88) (background dark))
  2214. (:foreground "grey70" :underline t))
  2215. (((class color) (min-colors 8) (background light))
  2216. (:foreground "green" :underline t))
  2217. (((class color) (min-colors 8) (background dark))
  2218. (:foreground "yellow" :underline t))))
  2219. "Face for fixed-with text like code snippets."
  2220. :group 'org-faces
  2221. :version "22.1")
  2222. (defface org-agenda-structure ;; font-lock-function-name-face
  2223. (org-compatible-face nil
  2224. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2225. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2226. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2227. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2228. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2229. (t (:bold t))))
  2230. "Face used in agenda for captions and dates."
  2231. :group 'org-faces)
  2232. (defface org-scheduled-today
  2233. (org-compatible-face nil
  2234. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2235. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2236. (((class color) (min-colors 8)) (:foreground "green"))
  2237. (t (:bold t :italic t))))
  2238. "Face for items scheduled for a certain day."
  2239. :group 'org-faces)
  2240. (defface org-scheduled-previously
  2241. (org-compatible-face nil
  2242. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2243. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2244. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2245. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2246. (t (:bold t))))
  2247. "Face for items scheduled previously, and not yet done."
  2248. :group 'org-faces)
  2249. (defface org-upcoming-deadline
  2250. (org-compatible-face nil
  2251. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2252. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2253. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2254. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2255. (t (:bold t))))
  2256. "Face for items scheduled previously, and not yet done."
  2257. :group 'org-faces)
  2258. (defcustom org-agenda-deadline-faces
  2259. '((1.0 . org-warning)
  2260. (0.5 . org-upcoming-deadline)
  2261. (0.0 . default))
  2262. "Faces for showing deadlines in the agenda.
  2263. This is a list of cons cells. The cdr of each cell is a face to be used,
  2264. and it can also just be like '(:foreground \"yellow\").
  2265. Each car is a fraction of the head-warning time that must have passed for
  2266. this the face in the cdr to be used for display. The numbers must be
  2267. given in descending order. The head-warning time is normally taken
  2268. from `org-deadline-warning-days', but can also be specified in the deadline
  2269. timestamp itself, like this:
  2270. DEADLINE: <2007-08-13 Mon -8d>
  2271. You may use d for days, w for weeks, m for months and y for years. Months
  2272. and years will only be treated in an approximate fashion (30.4 days for a
  2273. month and 365.24 days for a year)."
  2274. :group 'org-faces
  2275. :group 'org-agenda-daily/weekly
  2276. :type '(repeat
  2277. (cons
  2278. (number :tag "Fraction of head-warning time passed")
  2279. (sexp :tag "Face"))))
  2280. (defface org-agenda-restriction-lock
  2281. (org-compatible-face nil
  2282. '((((class color) (min-colors 88) (background light)) (:background "yellow1"))
  2283. (((class color) (min-colors 88) (background dark)) (:background "skyblue4"))
  2284. (((class color) (min-colors 16) (background light)) (:background "yellow1"))
  2285. (((class color) (min-colors 16) (background dark)) (:background "skyblue4"))
  2286. (((class color) (min-colors 8)) (:background "cyan" :foreground "black"))
  2287. (t (:inverse-video t))))
  2288. "Face for showing the agenda restriction lock."
  2289. :group 'org-faces)
  2290. (defface org-time-grid ;; font-lock-variable-name-face
  2291. (org-compatible-face nil
  2292. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2293. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2294. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2295. "Face used for time grids."
  2296. :group 'org-faces)
  2297. (defconst org-level-faces
  2298. '(org-level-1 org-level-2 org-level-3 org-level-4
  2299. org-level-5 org-level-6 org-level-7 org-level-8
  2300. ))
  2301. (defcustom org-n-level-faces (length org-level-faces)
  2302. "The number of different faces to be used for headlines.
  2303. Org-mode defines 8 different headline faces, so this can be at most 8.
  2304. If it is less than 8, the level-1 face gets re-used for level N+1 etc."
  2305. :type 'number
  2306. :group 'org-faces)
  2307. ;;; Functions and variables from ther packages
  2308. ;; Declared here to avoid compiler warnings
  2309. ;; XEmacs only
  2310. (defvar outline-mode-menu-heading)
  2311. (defvar outline-mode-menu-show)
  2312. (defvar outline-mode-menu-hide)
  2313. (defvar zmacs-regions) ; XEmacs regions
  2314. ;; Emacs only
  2315. (defvar mark-active)
  2316. ;; Various packages
  2317. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  2318. (declare-function calendar-forward-day "cal-move" (arg))
  2319. (declare-function calendar-goto-date "cal-move" (date))
  2320. (declare-function calendar-goto-today "cal-move" ())
  2321. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  2322. (defvar calc-embedded-close-formula)
  2323. (defvar calc-embedded-open-formula)
  2324. (declare-function cdlatex-tab "ext:cdlatex" ())
  2325. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2326. (defvar font-lock-unfontify-region-function)
  2327. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2328. (declare-function org-agenda-skip "org-agenda" ())
  2329. (declare-function org-format-agenda-item "org-agenda"
  2330. (extra txt &optional category tags dotime noprefix remove-re))
  2331. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2332. (declare-function org-agenda-change-all-lines "org-agenda"
  2333. (newhead hdmarker &optional fixface))
  2334. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2335. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2336. (declare-function parse-time-string "parse-time" (string))
  2337. (declare-function remember "remember" (&optional initial))
  2338. (declare-function remember-buffer-desc "remember" ())
  2339. (declare-function remember-finalize "remember" ())
  2340. (defvar remember-save-after-remembering)
  2341. (defvar remember-data-file)
  2342. (defvar remember-register)
  2343. (defvar remember-buffer)
  2344. (defvar remember-handler-functions)
  2345. (defvar remember-annotation-functions)
  2346. (defvar texmathp-why)
  2347. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2348. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2349. (defvar w3m-current-url)
  2350. (defvar w3m-current-title)
  2351. (defvar org-latex-regexps)
  2352. ;;; Autoload and prepare some org modules
  2353. ;; Some table stuff that needs to be defined here, because it is used
  2354. ;; by the functions setting up org-mode or checking for table context.
  2355. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2356. "Detects an org-type or table-type table.")
  2357. (defconst org-table-line-regexp "^[ \t]*|"
  2358. "Detects an org-type table line.")
  2359. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2360. "Detects an org-type table line.")
  2361. (defconst org-table-hline-regexp "^[ \t]*|-"
  2362. "Detects an org-type table hline.")
  2363. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2364. "Detects a table-type table hline.")
  2365. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2366. "Searching from within a table (any type) this finds the first line
  2367. outside the table.")
  2368. ;; Autoload the functions in org-table.el that are needed by functions here.
  2369. (eval-and-compile
  2370. (org-autoload "org-table"
  2371. '(org-table-align org-table-begin org-table-blank-field
  2372. org-table-convert org-table-convert-region org-table-copy-down
  2373. org-table-copy-region org-table-create
  2374. org-table-create-or-convert-from-region
  2375. org-table-create-with-table.el org-table-current-dline
  2376. org-table-cut-region org-table-delete-column org-table-edit-field
  2377. org-table-edit-formulas org-table-end org-table-eval-formula
  2378. org-table-export org-table-field-info
  2379. org-table-get-stored-formulas org-table-goto-column
  2380. org-table-hline-and-move org-table-import org-table-insert-column
  2381. org-table-insert-hline org-table-insert-row org-table-iterate
  2382. org-table-justify-field-maybe org-table-kill-row
  2383. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2384. org-table-move-column org-table-move-column-left
  2385. org-table-move-column-right org-table-move-row
  2386. org-table-move-row-down org-table-move-row-up
  2387. org-table-next-field org-table-next-row org-table-paste-rectangle
  2388. org-table-previous-field org-table-recalculate
  2389. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2390. org-table-toggle-coordinate-overlays
  2391. org-table-toggle-formula-debugger org-table-wrap-region
  2392. orgtbl-mode turn-on-orgtbl)))
  2393. (defun org-at-table-p (&optional table-type)
  2394. "Return t if the cursor is inside an org-type table.
  2395. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2396. (if org-enable-table-editor
  2397. (save-excursion
  2398. (beginning-of-line 1)
  2399. (looking-at (if table-type org-table-any-line-regexp
  2400. org-table-line-regexp)))
  2401. nil))
  2402. (defsubst org-table-p () (org-at-table-p))
  2403. (defun org-at-table.el-p ()
  2404. "Return t if and only if we are at a table.el table."
  2405. (and (org-at-table-p 'any)
  2406. (save-excursion
  2407. (goto-char (org-table-begin 'any))
  2408. (looking-at org-table1-hline-regexp))))
  2409. (defun org-table-recognize-table.el ()
  2410. "If there is a table.el table nearby, recognize it and move into it."
  2411. (if org-table-tab-recognizes-table.el
  2412. (if (org-at-table.el-p)
  2413. (progn
  2414. (beginning-of-line 1)
  2415. (if (looking-at org-table-dataline-regexp)
  2416. nil
  2417. (if (looking-at org-table1-hline-regexp)
  2418. (progn
  2419. (beginning-of-line 2)
  2420. (if (looking-at org-table-any-border-regexp)
  2421. (beginning-of-line -1)))))
  2422. (if (re-search-forward "|" (org-table-end t) t)
  2423. (progn
  2424. (require 'table)
  2425. (if (table--at-cell-p (point))
  2426. t
  2427. (message "recognizing table.el table...")
  2428. (table-recognize-table)
  2429. (message "recognizing table.el table...done")))
  2430. (error "This should not happen..."))
  2431. t)
  2432. nil)
  2433. nil))
  2434. (defun org-at-table-hline-p ()
  2435. "Return t if the cursor is inside a hline in a table."
  2436. (if org-enable-table-editor
  2437. (save-excursion
  2438. (beginning-of-line 1)
  2439. (looking-at org-table-hline-regexp))
  2440. nil))
  2441. (defvar org-table-clean-did-remove-column nil)
  2442. (defun org-table-map-tables (function)
  2443. "Apply FUNCTION to the start of all tables in the buffer."
  2444. (save-excursion
  2445. (save-restriction
  2446. (widen)
  2447. (goto-char (point-min))
  2448. (while (re-search-forward org-table-any-line-regexp nil t)
  2449. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2450. (beginning-of-line 1)
  2451. (if (looking-at org-table-line-regexp)
  2452. (save-excursion (funcall function)))
  2453. (re-search-forward org-table-any-border-regexp nil 1))))
  2454. (message "Mapping tables: done"))
  2455. ;; Declare and autoload functions from org-exp.el
  2456. (declare-function org-default-export-plist "org-exp")
  2457. (declare-function org-infile-export-plist "org-exp")
  2458. (declare-function org-get-current-options "org-exp")
  2459. (eval-and-compile
  2460. (org-autoload "org-exp"
  2461. '(org-export org-export-as-ascii org-export-visible
  2462. org-insert-export-options-template org-export-as-html-and-open
  2463. org-export-as-html-batch org-export-as-html-to-buffer
  2464. org-replace-region-by-html org-export-region-as-html
  2465. org-export-as-html org-export-icalendar-this-file
  2466. org-export-icalendar-all-agenda-files
  2467. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2468. ;; Autoload org-remember
  2469. (eval-and-compile
  2470. (org-autoload "org-remember"
  2471. '(org-remember-insinuate org-remember-annotation
  2472. org-remember-apply-template org-remember org-remember-handler)))
  2473. ;; Autoload org-clock.el
  2474. (defvar org-clock-marker (make-marker)
  2475. "Marker recording the last clock-in.")
  2476. (eval-and-compile
  2477. (org-autoload
  2478. "org-clock"
  2479. '(org-clock-in org-clock-out org-clock-cancel
  2480. org-clock-goto org-clock-sum org-clock-display
  2481. org-remove-clock-overlays org-clock-report
  2482. org-clocktable-shift org-dblock-write:clocktable)))
  2483. (defun org-clock-update-time-maybe ()
  2484. "If this is a CLOCK line, update it and return t.
  2485. Otherwise, return nil."
  2486. (interactive)
  2487. (save-excursion
  2488. (beginning-of-line 1)
  2489. (skip-chars-forward " \t")
  2490. (when (looking-at org-clock-string)
  2491. (let ((re (concat "[ \t]*" org-clock-string
  2492. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2493. "\\([ \t]*=>.*\\)?"))
  2494. ts te h m s)
  2495. (if (not (looking-at re))
  2496. nil
  2497. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2498. (end-of-line 1)
  2499. (setq ts (match-string 1)
  2500. te (match-string 2))
  2501. (setq s (- (time-to-seconds
  2502. (apply 'encode-time (org-parse-time-string te)))
  2503. (time-to-seconds
  2504. (apply 'encode-time (org-parse-time-string ts))))
  2505. h (floor (/ s 3600))
  2506. s (- s (* 3600 h))
  2507. m (floor (/ s 60))
  2508. s (- s (* 60 s)))
  2509. (insert " => " (format "%2d:%02d" h m))
  2510. t)))))
  2511. (defun org-check-running-clock ()
  2512. "Check if the current buffer contains the running clock.
  2513. If yes, offer to stop it and to save the buffer with the changes."
  2514. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2515. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2516. (buffer-name))))
  2517. (org-clock-out)
  2518. (when (y-or-n-p "Save changed buffer?")
  2519. (save-buffer))))
  2520. (defun org-clocktable-try-shift (dir n)
  2521. "Check if this line starts a clock table, if yes, shift the time block."
  2522. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2523. (org-clocktable-shift dir n)))
  2524. ;;; Variables for pre-computed regular expressions, all buffer local
  2525. (defvar org-drawer-regexp nil
  2526. "Matches first line of a hidden block.")
  2527. (make-variable-buffer-local 'org-drawer-regexp)
  2528. (defvar org-todo-regexp nil
  2529. "Matches any of the TODO state keywords.")
  2530. (make-variable-buffer-local 'org-todo-regexp)
  2531. (defvar org-not-done-regexp nil
  2532. "Matches any of the TODO state keywords except the last one.")
  2533. (make-variable-buffer-local 'org-not-done-regexp)
  2534. (defvar org-todo-line-regexp nil
  2535. "Matches a headline and puts TODO state into group 2 if present.")
  2536. (make-variable-buffer-local 'org-todo-line-regexp)
  2537. (defvar org-complex-heading-regexp nil
  2538. "Matches a headline and puts everything into groups:
  2539. group 1: the stars
  2540. group 2: The todo keyword, maybe
  2541. group 3: Priority cookie
  2542. group 4: True headline
  2543. group 5: Tags")
  2544. (make-variable-buffer-local 'org-complex-heading-regexp)
  2545. (defvar org-todo-line-tags-regexp nil
  2546. "Matches a headline and puts TODO state into group 2 if present.
  2547. Also put tags into group 4 if tags are present.")
  2548. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2549. (defvar org-nl-done-regexp nil
  2550. "Matches newline followed by a headline with the DONE keyword.")
  2551. (make-variable-buffer-local 'org-nl-done-regexp)
  2552. (defvar org-looking-at-done-regexp nil
  2553. "Matches the DONE keyword a point.")
  2554. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2555. (defvar org-ds-keyword-length 12
  2556. "Maximum length of the Deadline and SCHEDULED keywords.")
  2557. (make-variable-buffer-local 'org-ds-keyword-length)
  2558. (defvar org-deadline-regexp nil
  2559. "Matches the DEADLINE keyword.")
  2560. (make-variable-buffer-local 'org-deadline-regexp)
  2561. (defvar org-deadline-time-regexp nil
  2562. "Matches the DEADLINE keyword together with a time stamp.")
  2563. (make-variable-buffer-local 'org-deadline-time-regexp)
  2564. (defvar org-deadline-line-regexp nil
  2565. "Matches the DEADLINE keyword and the rest of the line.")
  2566. (make-variable-buffer-local 'org-deadline-line-regexp)
  2567. (defvar org-scheduled-regexp nil
  2568. "Matches the SCHEDULED keyword.")
  2569. (make-variable-buffer-local 'org-scheduled-regexp)
  2570. (defvar org-scheduled-time-regexp nil
  2571. "Matches the SCHEDULED keyword together with a time stamp.")
  2572. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2573. (defvar org-closed-time-regexp nil
  2574. "Matches the CLOSED keyword together with a time stamp.")
  2575. (make-variable-buffer-local 'org-closed-time-regexp)
  2576. (defvar org-keyword-time-regexp nil
  2577. "Matches any of the 4 keywords, together with the time stamp.")
  2578. (make-variable-buffer-local 'org-keyword-time-regexp)
  2579. (defvar org-keyword-time-not-clock-regexp nil
  2580. "Matches any of the 3 keywords, together with the time stamp.")
  2581. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2582. (defvar org-maybe-keyword-time-regexp nil
  2583. "Matches a timestamp, possibly preceeded by a keyword.")
  2584. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2585. (defvar org-planning-or-clock-line-re nil
  2586. "Matches a line with planning or clock info.")
  2587. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2588. (defconst org-plain-time-of-day-regexp
  2589. (concat
  2590. "\\(\\<[012]?[0-9]"
  2591. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2592. "\\(--?"
  2593. "\\(\\<[012]?[0-9]"
  2594. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2595. "\\)?")
  2596. "Regular expression to match a plain time or time range.
  2597. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2598. groups carry important information:
  2599. 0 the full match
  2600. 1 the first time, range or not
  2601. 8 the second time, if it is a range.")
  2602. (defconst org-plain-time-extension-regexp
  2603. (concat
  2604. "\\(\\<[012]?[0-9]"
  2605. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2606. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2607. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2608. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2609. groups carry important information:
  2610. 0 the full match
  2611. 7 hours of duration
  2612. 9 minutes of duration")
  2613. (defconst org-stamp-time-of-day-regexp
  2614. (concat
  2615. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2616. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2617. "\\(--?"
  2618. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2619. "Regular expression to match a timestamp time or time range.
  2620. After a match, the following groups carry important information:
  2621. 0 the full match
  2622. 1 date plus weekday, for backreferencing to make sure both times on same day
  2623. 2 the first time, range or not
  2624. 4 the second time, if it is a range.")
  2625. (defconst org-startup-options
  2626. '(("fold" org-startup-folded t)
  2627. ("overview" org-startup-folded t)
  2628. ("nofold" org-startup-folded nil)
  2629. ("showall" org-startup-folded nil)
  2630. ("content" org-startup-folded content)
  2631. ("hidestars" org-hide-leading-stars t)
  2632. ("showstars" org-hide-leading-stars nil)
  2633. ("odd" org-odd-levels-only t)
  2634. ("oddeven" org-odd-levels-only nil)
  2635. ("align" org-startup-align-all-tables t)
  2636. ("noalign" org-startup-align-all-tables nil)
  2637. ("customtime" org-display-custom-times t)
  2638. ("logdone" org-log-done time)
  2639. ("lognotedone" org-log-done note)
  2640. ("nologdone" org-log-done nil)
  2641. ("lognoteclock-out" org-log-note-clock-out t)
  2642. ("nolognoteclock-out" org-log-note-clock-out nil)
  2643. ("logrepeat" org-log-repeat state)
  2644. ("lognoterepeat" org-log-repeat note)
  2645. ("nologrepeat" org-log-repeat nil)
  2646. ("constcgs" constants-unit-system cgs)
  2647. ("constSI" constants-unit-system SI))
  2648. "Variable associated with STARTUP options for org-mode.
  2649. Each element is a list of three items: The startup options as written
  2650. in the #+STARTUP line, the corresponding variable, and the value to
  2651. set this variable to if the option is found. An optional forth element PUSH
  2652. means to push this value onto the list in the variable.")
  2653. (defun org-set-regexps-and-options ()
  2654. "Precompute regular expressions for current buffer."
  2655. (when (org-mode-p)
  2656. (org-set-local 'org-todo-kwd-alist nil)
  2657. (org-set-local 'org-todo-key-alist nil)
  2658. (org-set-local 'org-todo-key-trigger nil)
  2659. (org-set-local 'org-todo-keywords-1 nil)
  2660. (org-set-local 'org-done-keywords nil)
  2661. (org-set-local 'org-todo-heads nil)
  2662. (org-set-local 'org-todo-sets nil)
  2663. (org-set-local 'org-todo-log-states nil)
  2664. (let ((re (org-make-options-regexp
  2665. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2666. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2667. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2668. (splitre "[ \t]+")
  2669. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2670. tail sep kws1 prio props drawers)
  2671. (save-excursion
  2672. (save-restriction
  2673. (widen)
  2674. (goto-char (point-min))
  2675. (while (re-search-forward re nil t)
  2676. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2677. (cond
  2678. ((equal key "CATEGORY")
  2679. (if (string-match "[ \t]+$" value)
  2680. (setq value (replace-match "" t t value)))
  2681. (setq cat value))
  2682. ((member key '("SEQ_TODO" "TODO"))
  2683. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2684. ((equal key "TYP_TODO")
  2685. (push (cons 'type (org-split-string value splitre)) kwds))
  2686. ((equal key "TAGS")
  2687. (setq tags (append tags (org-split-string value splitre))))
  2688. ((equal key "COLUMNS")
  2689. (org-set-local 'org-columns-default-format value))
  2690. ((equal key "LINK")
  2691. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2692. (push (cons (match-string 1 value)
  2693. (org-trim (match-string 2 value)))
  2694. links)))
  2695. ((equal key "PRIORITIES")
  2696. (setq prio (org-split-string value " +")))
  2697. ((equal key "PROPERTY")
  2698. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2699. (push (cons (match-string 1 value) (match-string 2 value))
  2700. props)))
  2701. ((equal key "DRAWERS")
  2702. (setq drawers (org-split-string value splitre)))
  2703. ((equal key "CONSTANTS")
  2704. (setq const (append const (org-split-string value splitre))))
  2705. ((equal key "STARTUP")
  2706. (let ((opts (org-split-string value splitre))
  2707. l var val)
  2708. (while (setq l (pop opts))
  2709. (when (setq l (assoc l org-startup-options))
  2710. (setq var (nth 1 l) val (nth 2 l))
  2711. (if (not (nth 3 l))
  2712. (set (make-local-variable var) val)
  2713. (if (not (listp (symbol-value var)))
  2714. (set (make-local-variable var) nil))
  2715. (set (make-local-variable var) (symbol-value var))
  2716. (add-to-list var val))))))
  2717. ((equal key "ARCHIVE")
  2718. (string-match " *$" value)
  2719. (setq arch (replace-match "" t t value))
  2720. (remove-text-properties 0 (length arch)
  2721. '(face t fontified t) arch)))
  2722. )))
  2723. (when cat
  2724. (org-set-local 'org-category (intern cat))
  2725. (push (cons "CATEGORY" cat) props))
  2726. (when prio
  2727. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2728. (setq prio (mapcar 'string-to-char prio))
  2729. (org-set-local 'org-highest-priority (nth 0 prio))
  2730. (org-set-local 'org-lowest-priority (nth 1 prio))
  2731. (org-set-local 'org-default-priority (nth 2 prio)))
  2732. (and props (org-set-local 'org-local-properties (nreverse props)))
  2733. (and drawers (org-set-local 'org-drawers drawers))
  2734. (and arch (org-set-local 'org-archive-location arch))
  2735. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2736. ;; Process the TODO keywords
  2737. (unless kwds
  2738. ;; Use the global values as if they had been given locally.
  2739. (setq kwds (default-value 'org-todo-keywords))
  2740. (if (stringp (car kwds))
  2741. (setq kwds (list (cons org-todo-interpretation
  2742. (default-value 'org-todo-keywords)))))
  2743. (setq kwds (reverse kwds)))
  2744. (setq kwds (nreverse kwds))
  2745. (let (inter kws kw)
  2746. (while (setq kws (pop kwds))
  2747. (setq inter (pop kws) sep (member "|" kws)
  2748. kws0 (delete "|" (copy-sequence kws))
  2749. kwsa nil
  2750. kws1 (mapcar
  2751. (lambda (x)
  2752. ;; 1 2
  2753. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2754. (progn
  2755. (setq kw (match-string 1 x)
  2756. key (and (match-end 2) (match-string 2 x))
  2757. log (org-extract-log-state-settings x))
  2758. (push (cons kw (and key (string-to-char key))) kwsa)
  2759. (and log (push log org-todo-log-states))
  2760. kw)
  2761. (error "Invalid TODO keyword %s" x)))
  2762. kws0)
  2763. kwsa (if kwsa (append '((:startgroup))
  2764. (nreverse kwsa)
  2765. '((:endgroup))))
  2766. hw (car kws1)
  2767. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2768. tail (list inter hw (car dws) (org-last dws)))
  2769. (add-to-list 'org-todo-heads hw 'append)
  2770. (push kws1 org-todo-sets)
  2771. (setq org-done-keywords (append org-done-keywords dws nil))
  2772. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2773. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2774. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2775. (setq org-todo-sets (nreverse org-todo-sets)
  2776. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2777. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2778. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2779. ;; Process the constants
  2780. (when const
  2781. (let (e cst)
  2782. (while (setq e (pop const))
  2783. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2784. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2785. (setq org-table-formula-constants-local cst)))
  2786. ;; Process the tags.
  2787. (when tags
  2788. (let (e tgs)
  2789. (while (setq e (pop tags))
  2790. (cond
  2791. ((equal e "{") (push '(:startgroup) tgs))
  2792. ((equal e "}") (push '(:endgroup) tgs))
  2793. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2794. (push (cons (match-string 1 e)
  2795. (string-to-char (match-string 2 e)))
  2796. tgs))
  2797. (t (push (list e) tgs))))
  2798. (org-set-local 'org-tag-alist nil)
  2799. (while (setq e (pop tgs))
  2800. (or (and (stringp (car e))
  2801. (assoc (car e) org-tag-alist))
  2802. (push e org-tag-alist))))))
  2803. ;; Compute the regular expressions and other local variables
  2804. (if (not org-done-keywords)
  2805. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2806. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2807. (length org-scheduled-string)))
  2808. org-drawer-regexp
  2809. (concat "^[ \t]*:\\("
  2810. (mapconcat 'regexp-quote org-drawers "\\|")
  2811. "\\):[ \t]*$")
  2812. org-not-done-keywords
  2813. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2814. org-todo-regexp
  2815. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2816. "\\|") "\\)\\>")
  2817. org-not-done-regexp
  2818. (concat "\\<\\("
  2819. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2820. "\\)\\>")
  2821. org-todo-line-regexp
  2822. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2823. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2824. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2825. org-complex-heading-regexp
  2826. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2827. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2828. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2829. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2830. org-nl-done-regexp
  2831. (concat "\n\\*+[ \t]+"
  2832. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2833. "\\)" "\\>")
  2834. org-todo-line-tags-regexp
  2835. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2836. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2837. (org-re
  2838. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2839. org-looking-at-done-regexp
  2840. (concat "^" "\\(?:"
  2841. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2842. "\\>")
  2843. org-deadline-regexp (concat "\\<" org-deadline-string)
  2844. org-deadline-time-regexp
  2845. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2846. org-deadline-line-regexp
  2847. (concat "\\<\\(" org-deadline-string "\\).*")
  2848. org-scheduled-regexp
  2849. (concat "\\<" org-scheduled-string)
  2850. org-scheduled-time-regexp
  2851. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2852. org-closed-time-regexp
  2853. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2854. org-keyword-time-regexp
  2855. (concat "\\<\\(" org-scheduled-string
  2856. "\\|" org-deadline-string
  2857. "\\|" org-closed-string
  2858. "\\|" org-clock-string "\\)"
  2859. " *[[<]\\([^]>]+\\)[]>]")
  2860. org-keyword-time-not-clock-regexp
  2861. (concat "\\<\\(" org-scheduled-string
  2862. "\\|" org-deadline-string
  2863. "\\|" org-closed-string
  2864. "\\)"
  2865. " *[[<]\\([^]>]+\\)[]>]")
  2866. org-maybe-keyword-time-regexp
  2867. (concat "\\(\\<\\(" org-scheduled-string
  2868. "\\|" org-deadline-string
  2869. "\\|" org-closed-string
  2870. "\\|" org-clock-string "\\)\\)?"
  2871. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2872. org-planning-or-clock-line-re
  2873. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2874. "\\|" org-deadline-string
  2875. "\\|" org-closed-string "\\|" org-clock-string
  2876. "\\)\\>\\)")
  2877. )
  2878. (org-compute-latex-and-specials-regexp)
  2879. (org-set-font-lock-defaults)))
  2880. (defun org-extract-log-state-settings (x)
  2881. "Extract the log state setting from a TODO keyword string.
  2882. This will extract info from a string like \"WAIT(w@/!)\"."
  2883. (let (kw key log1 log2)
  2884. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2885. (setq kw (match-string 1 x)
  2886. key (and (match-end 2) (match-string 2 x))
  2887. log1 (and (match-end 3) (match-string 3 x))
  2888. log2 (and (match-end 4) (match-string 4 x)))
  2889. (and (or log1 log2)
  2890. (list kw
  2891. (and log1 (if (equal log1 "!") 'time 'note))
  2892. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2893. (defun org-remove-keyword-keys (list)
  2894. "Remove a pair of parenthesis at the end of each string in LIST."
  2895. (mapcar (lambda (x)
  2896. (if (string-match "(.*)$" x)
  2897. (substring x 0 (match-beginning 0))
  2898. x))
  2899. list))
  2900. ;; FIXME: this could be done much better, using second characters etc.
  2901. (defun org-assign-fast-keys (alist)
  2902. "Assign fast keys to a keyword-key alist.
  2903. Respect keys that are already there."
  2904. (let (new e k c c1 c2 (char ?a))
  2905. (while (setq e (pop alist))
  2906. (cond
  2907. ((equal e '(:startgroup)) (push e new))
  2908. ((equal e '(:endgroup)) (push e new))
  2909. (t
  2910. (setq k (car e) c2 nil)
  2911. (if (cdr e)
  2912. (setq c (cdr e))
  2913. ;; automatically assign a character.
  2914. (setq c1 (string-to-char
  2915. (downcase (substring
  2916. k (if (= (string-to-char k) ?@) 1 0)))))
  2917. (if (or (rassoc c1 new) (rassoc c1 alist))
  2918. (while (or (rassoc char new) (rassoc char alist))
  2919. (setq char (1+ char)))
  2920. (setq c2 c1))
  2921. (setq c (or c2 char)))
  2922. (push (cons k c) new))))
  2923. (nreverse new)))
  2924. ;;; Some variables used in various places
  2925. (defvar org-window-configuration nil
  2926. "Used in various places to store a window configuration.")
  2927. (defvar org-finish-function nil
  2928. "Function to be called when `C-c C-c' is used.
  2929. This is for getting out of special buffers like remember.")
  2930. ;; FIXME: Occasionally check by commenting these, to make sure
  2931. ;; no other functions uses these, forgetting to let-bind them.
  2932. (defvar entry)
  2933. (defvar state)
  2934. (defvar last-state)
  2935. (defvar date)
  2936. (defvar description)
  2937. ;; Defined somewhere in this file, but used before definition.
  2938. (defvar org-html-entities)
  2939. (defvar org-struct-menu)
  2940. (defvar org-org-menu)
  2941. (defvar org-tbl-menu)
  2942. (defvar org-agenda-keymap)
  2943. ;;;; Define the Org-mode
  2944. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2945. (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."))
  2946. ;; We use a before-change function to check if a table might need
  2947. ;; an update.
  2948. (defvar org-table-may-need-update t
  2949. "Indicates that a table might need an update.
  2950. This variable is set by `org-before-change-function'.
  2951. `org-table-align' sets it back to nil.")
  2952. (defun org-before-change-function (beg end)
  2953. "Every change indicates that a table might need an update."
  2954. (setq org-table-may-need-update t))
  2955. (defvar org-mode-map)
  2956. (defvar org-mode-hook nil)
  2957. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2958. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2959. (defvar org-table-buffer-is-an nil)
  2960. (defconst org-outline-regexp "\\*+ ")
  2961. ;;;###autoload
  2962. (define-derived-mode org-mode outline-mode "Org"
  2963. "Outline-based notes management and organizer, alias
  2964. \"Carsten's outline-mode for keeping track of everything.\"
  2965. Org-mode develops organizational tasks around a NOTES file which
  2966. contains information about projects as plain text. Org-mode is
  2967. implemented on top of outline-mode, which is ideal to keep the content
  2968. of large files well structured. It supports ToDo items, deadlines and
  2969. time stamps, which magically appear in the diary listing of the Emacs
  2970. calendar. Tables are easily created with a built-in table editor.
  2971. Plain text URL-like links connect to websites, emails (VM), Usenet
  2972. messages (Gnus), BBDB entries, and any files related to the project.
  2973. For printing and sharing of notes, an Org-mode file (or a part of it)
  2974. can be exported as a structured ASCII or HTML file.
  2975. The following commands are available:
  2976. \\{org-mode-map}"
  2977. ;; Get rid of Outline menus, they are not needed
  2978. ;; Need to do this here because define-derived-mode sets up
  2979. ;; the keymap so late. Still, it is a waste to call this each time
  2980. ;; we switch another buffer into org-mode.
  2981. (if (featurep 'xemacs)
  2982. (when (boundp 'outline-mode-menu-heading)
  2983. ;; Assume this is Greg's port, it used easymenu
  2984. (easy-menu-remove outline-mode-menu-heading)
  2985. (easy-menu-remove outline-mode-menu-show)
  2986. (easy-menu-remove outline-mode-menu-hide))
  2987. (define-key org-mode-map [menu-bar headings] 'undefined)
  2988. (define-key org-mode-map [menu-bar hide] 'undefined)
  2989. (define-key org-mode-map [menu-bar show] 'undefined))
  2990. (org-load-modules-maybe)
  2991. (easy-menu-add org-org-menu)
  2992. (easy-menu-add org-tbl-menu)
  2993. (org-install-agenda-files-menu)
  2994. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2995. (org-add-to-invisibility-spec '(org-cwidth))
  2996. (when (featurep 'xemacs)
  2997. (org-set-local 'line-move-ignore-invisible t))
  2998. (org-set-local 'outline-regexp org-outline-regexp)
  2999. (org-set-local 'outline-level 'org-outline-level)
  3000. (when (and org-ellipsis
  3001. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3002. (fboundp 'make-glyph-code))
  3003. (unless org-display-table
  3004. (setq org-display-table (make-display-table)))
  3005. (set-display-table-slot
  3006. org-display-table 4
  3007. (vconcat (mapcar
  3008. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3009. org-ellipsis)))
  3010. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3011. (setq buffer-display-table org-display-table))
  3012. (org-set-regexps-and-options)
  3013. ;; Calc embedded
  3014. (org-set-local 'calc-embedded-open-mode "# ")
  3015. (modify-syntax-entry ?# "<")
  3016. (modify-syntax-entry ?@ "w")
  3017. (if org-startup-truncated (setq truncate-lines t))
  3018. (org-set-local 'font-lock-unfontify-region-function
  3019. 'org-unfontify-region)
  3020. ;; Activate before-change-function
  3021. (org-set-local 'org-table-may-need-update t)
  3022. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3023. 'local)
  3024. ;; Check for running clock before killing a buffer
  3025. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3026. ;; Paragraphs and auto-filling
  3027. (org-set-autofill-regexps)
  3028. (setq indent-line-function 'org-indent-line-function)
  3029. (org-update-radio-target-regexp)
  3030. ;; Comment characters
  3031. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3032. (org-set-local 'comment-padding " ")
  3033. ;; Align options lines
  3034. (org-set-local
  3035. 'align-mode-rules-list
  3036. '((org-in-buffer-settings
  3037. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3038. (modes . '(org-mode)))))
  3039. ;; Imenu
  3040. (org-set-local 'imenu-create-index-function
  3041. 'org-imenu-get-tree)
  3042. ;; Make isearch reveal context
  3043. (if (or (featurep 'xemacs)
  3044. (not (boundp 'outline-isearch-open-invisible-function)))
  3045. ;; Emacs 21 and XEmacs make use of the hook
  3046. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3047. ;; Emacs 22 deals with this through a special variable
  3048. (org-set-local 'outline-isearch-open-invisible-function
  3049. (lambda (&rest ignore) (org-show-context 'isearch))))
  3050. ;; If empty file that did not turn on org-mode automatically, make it to.
  3051. (if (and org-insert-mode-line-in-empty-file
  3052. (interactive-p)
  3053. (= (point-min) (point-max)))
  3054. (insert "# -*- mode: org -*-\n\n"))
  3055. (unless org-inhibit-startup
  3056. (when org-startup-align-all-tables
  3057. (let ((bmp (buffer-modified-p)))
  3058. (org-table-map-tables 'org-table-align)
  3059. (set-buffer-modified-p bmp)))
  3060. (org-cycle-hide-drawers 'all)
  3061. (cond
  3062. ((eq org-startup-folded t)
  3063. (org-cycle '(4)))
  3064. ((eq org-startup-folded 'content)
  3065. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3066. (org-cycle '(4)) (org-cycle '(4)))))))
  3067. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3068. (defun org-current-time ()
  3069. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3070. (if (> (car org-time-stamp-rounding-minutes) 1)
  3071. (let ((r (car org-time-stamp-rounding-minutes))
  3072. (time (decode-time)))
  3073. (apply 'encode-time
  3074. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3075. (nthcdr 2 time))))
  3076. (current-time)))
  3077. ;;;; Font-Lock stuff, including the activators
  3078. (defvar org-mouse-map (make-sparse-keymap))
  3079. (org-defkey org-mouse-map
  3080. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3081. (org-defkey org-mouse-map
  3082. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3083. (when org-mouse-1-follows-link
  3084. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3085. (when org-tab-follows-link
  3086. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3087. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3088. (when org-return-follows-link
  3089. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3090. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3091. (require 'font-lock)
  3092. (defconst org-non-link-chars "]\t\n\r<>")
  3093. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3094. "shell" "elisp"))
  3095. (defvar org-link-re-with-space nil
  3096. "Matches a link with spaces, optional angular brackets around it.")
  3097. (defvar org-link-re-with-space2 nil
  3098. "Matches a link with spaces, optional angular brackets around it.")
  3099. (defvar org-angle-link-re nil
  3100. "Matches link with angular brackets, spaces are allowed.")
  3101. (defvar org-plain-link-re nil
  3102. "Matches plain link, without spaces.")
  3103. (defvar org-bracket-link-regexp nil
  3104. "Matches a link in double brackets.")
  3105. (defvar org-bracket-link-analytic-regexp nil
  3106. "Regular expression used to analyze links.
  3107. Here is what the match groups contain after a match:
  3108. 1: http:
  3109. 2: http
  3110. 3: path
  3111. 4: [desc]
  3112. 5: desc")
  3113. (defvar org-any-link-re nil
  3114. "Regular expression matching any link.")
  3115. (defun org-make-link-regexps ()
  3116. "Update the link regular expressions.
  3117. This should be called after the variable `org-link-types' has changed."
  3118. (setq org-link-re-with-space
  3119. (concat
  3120. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3121. "\\([^" org-non-link-chars " ]"
  3122. "[^" org-non-link-chars "]*"
  3123. "[^" org-non-link-chars " ]\\)>?")
  3124. org-link-re-with-space2
  3125. (concat
  3126. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3127. "\\([^" org-non-link-chars " ]"
  3128. "[^]\t\n\r]*"
  3129. "[^" org-non-link-chars " ]\\)>?")
  3130. org-angle-link-re
  3131. (concat
  3132. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3133. "\\([^" org-non-link-chars " ]"
  3134. "[^" org-non-link-chars "]*"
  3135. "\\)>")
  3136. org-plain-link-re
  3137. (concat
  3138. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3139. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3140. org-bracket-link-regexp
  3141. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3142. org-bracket-link-analytic-regexp
  3143. (concat
  3144. "\\[\\["
  3145. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3146. "\\([^]]+\\)"
  3147. "\\]"
  3148. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3149. "\\]")
  3150. org-any-link-re
  3151. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3152. org-angle-link-re "\\)\\|\\("
  3153. org-plain-link-re "\\)")))
  3154. (org-make-link-regexps)
  3155. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3156. "Regular expression for fast time stamp matching.")
  3157. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3158. "Regular expression for fast time stamp matching.")
  3159. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3160. "Regular expression matching time strings for analysis.
  3161. This one does not require the space after the date, so it can be used
  3162. on a string that terminates immediately after the date.")
  3163. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3164. "Regular expression matching time strings for analysis.")
  3165. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3166. "Regular expression matching time stamps, with groups.")
  3167. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3168. "Regular expression matching time stamps (also [..]), with groups.")
  3169. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3170. "Regular expression matching a time stamp range.")
  3171. (defconst org-tr-regexp-both
  3172. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3173. "Regular expression matching a time stamp range.")
  3174. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3175. org-ts-regexp "\\)?")
  3176. "Regular expression matching a time stamp or time stamp range.")
  3177. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3178. org-ts-regexp-both "\\)?")
  3179. "Regular expression matching a time stamp or time stamp range.
  3180. The time stamps may be either active or inactive.")
  3181. (defvar org-emph-face nil)
  3182. (defun org-do-emphasis-faces (limit)
  3183. "Run through the buffer and add overlays to links."
  3184. (let (rtn)
  3185. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3186. (if (not (= (char-after (match-beginning 3))
  3187. (char-after (match-beginning 4))))
  3188. (progn
  3189. (setq rtn t)
  3190. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3191. 'face
  3192. (nth 1 (assoc (match-string 3)
  3193. org-emphasis-alist)))
  3194. (add-text-properties (match-beginning 2) (match-end 2)
  3195. '(font-lock-multiline t))
  3196. (when org-hide-emphasis-markers
  3197. (add-text-properties (match-end 4) (match-beginning 5)
  3198. '(invisible org-link))
  3199. (add-text-properties (match-beginning 3) (match-end 3)
  3200. '(invisible org-link)))))
  3201. (backward-char 1))
  3202. rtn))
  3203. (defun org-emphasize (&optional char)
  3204. "Insert or change an emphasis, i.e. a font like bold or italic.
  3205. If there is an active region, change that region to a new emphasis.
  3206. If there is no region, just insert the marker characters and position
  3207. the cursor between them.
  3208. CHAR should be either the marker character, or the first character of the
  3209. HTML tag associated with that emphasis. If CHAR is a space, the means
  3210. to remove the emphasis of the selected region.
  3211. If char is not given (for example in an interactive call) it
  3212. will be prompted for."
  3213. (interactive)
  3214. (let ((eal org-emphasis-alist) e det
  3215. (erc org-emphasis-regexp-components)
  3216. (prompt "")
  3217. (string "") beg end move tag c s)
  3218. (if (org-region-active-p)
  3219. (setq beg (region-beginning) end (region-end)
  3220. string (buffer-substring beg end))
  3221. (setq move t))
  3222. (while (setq e (pop eal))
  3223. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3224. c (aref tag 0))
  3225. (push (cons c (string-to-char (car e))) det)
  3226. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3227. (substring tag 1)))))
  3228. (unless char
  3229. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3230. (setq char (read-char-exclusive)))
  3231. (setq char (or (cdr (assoc char det)) char))
  3232. (if (equal char ?\ )
  3233. (setq s "" move nil)
  3234. (unless (assoc (char-to-string char) org-emphasis-alist)
  3235. (error "No such emphasis marker: \"%c\"" char))
  3236. (setq s (char-to-string char)))
  3237. (while (and (> (length string) 1)
  3238. (equal (substring string 0 1) (substring string -1))
  3239. (assoc (substring string 0 1) org-emphasis-alist))
  3240. (setq string (substring string 1 -1)))
  3241. (setq string (concat s string s))
  3242. (if beg (delete-region beg end))
  3243. (unless (or (bolp)
  3244. (string-match (concat "[" (nth 0 erc) "\n]")
  3245. (char-to-string (char-before (point)))))
  3246. (insert " "))
  3247. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3248. (char-to-string (char-after (point))))
  3249. (insert " ") (backward-char 1))
  3250. (insert string)
  3251. (and move (backward-char 1))))
  3252. (defconst org-nonsticky-props
  3253. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3254. (defun org-activate-plain-links (limit)
  3255. "Run through the buffer and add overlays to links."
  3256. (catch 'exit
  3257. (let (f)
  3258. (while (re-search-forward org-plain-link-re limit t)
  3259. (setq f (get-text-property (match-beginning 0) 'face))
  3260. (if (or (eq f 'org-tag)
  3261. (and (listp f) (memq 'org-tag f)))
  3262. nil
  3263. (add-text-properties (match-beginning 0) (match-end 0)
  3264. (list 'mouse-face 'highlight
  3265. 'rear-nonsticky org-nonsticky-props
  3266. 'keymap org-mouse-map
  3267. ))
  3268. (throw 'exit t))))))
  3269. (defun org-activate-code (limit)
  3270. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3271. (unless (get-text-property (match-beginning 1) 'face)
  3272. (remove-text-properties (match-beginning 0) (match-end 0)
  3273. '(display t invisible t intangible t))
  3274. t)))
  3275. (defun org-activate-angle-links (limit)
  3276. "Run through the buffer and add overlays to links."
  3277. (if (re-search-forward org-angle-link-re limit t)
  3278. (progn
  3279. (add-text-properties (match-beginning 0) (match-end 0)
  3280. (list 'mouse-face 'highlight
  3281. 'rear-nonsticky org-nonsticky-props
  3282. 'keymap org-mouse-map
  3283. ))
  3284. t)))
  3285. (defun org-activate-bracket-links (limit)
  3286. "Run through the buffer and add overlays to bracketed links."
  3287. (if (re-search-forward org-bracket-link-regexp limit t)
  3288. (let* ((help (concat "LINK: "
  3289. (org-match-string-no-properties 1)))
  3290. ;; FIXME: above we should remove the escapes.
  3291. ;; but that requires another match, protecting match data,
  3292. ;; a lot of overhead for font-lock.
  3293. (ip (org-maybe-intangible
  3294. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3295. 'keymap org-mouse-map 'mouse-face 'highlight
  3296. 'font-lock-multiline t 'help-echo help)))
  3297. (vp (list 'rear-nonsticky org-nonsticky-props
  3298. 'keymap org-mouse-map 'mouse-face 'highlight
  3299. ' font-lock-multiline t 'help-echo help)))
  3300. ;; We need to remove the invisible property here. Table narrowing
  3301. ;; may have made some of this invisible.
  3302. (remove-text-properties (match-beginning 0) (match-end 0)
  3303. '(invisible nil))
  3304. (if (match-end 3)
  3305. (progn
  3306. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3307. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3308. (add-text-properties (match-end 3) (match-end 0) ip))
  3309. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3310. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3311. (add-text-properties (match-end 1) (match-end 0) ip))
  3312. t)))
  3313. (defun org-activate-dates (limit)
  3314. "Run through the buffer and add overlays to dates."
  3315. (if (re-search-forward org-tsr-regexp-both limit t)
  3316. (progn
  3317. (add-text-properties (match-beginning 0) (match-end 0)
  3318. (list 'mouse-face 'highlight
  3319. 'rear-nonsticky org-nonsticky-props
  3320. 'keymap org-mouse-map))
  3321. (when org-display-custom-times
  3322. (if (match-end 3)
  3323. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3324. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3325. t)))
  3326. (defvar org-target-link-regexp nil
  3327. "Regular expression matching radio targets in plain text.")
  3328. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3329. "Regular expression matching a link target.")
  3330. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3331. "Regular expression matching a radio target.")
  3332. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3333. "Regular expression matching any target.")
  3334. (defun org-activate-target-links (limit)
  3335. "Run through the buffer and add overlays to target matches."
  3336. (when org-target-link-regexp
  3337. (let ((case-fold-search t))
  3338. (if (re-search-forward org-target-link-regexp limit t)
  3339. (progn
  3340. (add-text-properties (match-beginning 0) (match-end 0)
  3341. (list 'mouse-face 'highlight
  3342. 'rear-nonsticky org-nonsticky-props
  3343. 'keymap org-mouse-map
  3344. 'help-echo "Radio target link"
  3345. 'org-linked-text t))
  3346. t)))))
  3347. (defun org-update-radio-target-regexp ()
  3348. "Find all radio targets in this file and update the regular expression."
  3349. (interactive)
  3350. (when (memq 'radio org-activate-links)
  3351. (setq org-target-link-regexp
  3352. (org-make-target-link-regexp (org-all-targets 'radio)))
  3353. (org-restart-font-lock)))
  3354. (defun org-hide-wide-columns (limit)
  3355. (let (s e)
  3356. (setq s (text-property-any (point) (or limit (point-max))
  3357. 'org-cwidth t))
  3358. (when s
  3359. (setq e (next-single-property-change s 'org-cwidth))
  3360. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3361. (goto-char e)
  3362. t)))
  3363. (defvar org-latex-and-specials-regexp nil
  3364. "Regular expression for highlighting export special stuff.")
  3365. (defvar org-match-substring-regexp)
  3366. (defvar org-match-substring-with-braces-regexp)
  3367. (defvar org-export-html-special-string-regexps)
  3368. (defun org-compute-latex-and-specials-regexp ()
  3369. "Compute regular expression for stuff treated specially by exporters."
  3370. (if (not org-highlight-latex-fragments-and-specials)
  3371. (org-set-local 'org-latex-and-specials-regexp nil)
  3372. (require 'org-exp)
  3373. (let*
  3374. ((matchers (plist-get org-format-latex-options :matchers))
  3375. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3376. org-latex-regexps)))
  3377. (options (org-combine-plists (org-default-export-plist)
  3378. (org-infile-export-plist)))
  3379. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3380. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3381. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3382. (org-export-html-expand (plist-get options :expand-quoted-html))
  3383. (org-export-with-special-strings (plist-get options :special-strings))
  3384. (re-sub
  3385. (cond
  3386. ((equal org-export-with-sub-superscripts '{})
  3387. (list org-match-substring-with-braces-regexp))
  3388. (org-export-with-sub-superscripts
  3389. (list org-match-substring-regexp))
  3390. (t nil)))
  3391. (re-latex
  3392. (if org-export-with-LaTeX-fragments
  3393. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3394. (re-macros
  3395. (if org-export-with-TeX-macros
  3396. (list (concat "\\\\"
  3397. (regexp-opt
  3398. (append (mapcar 'car org-html-entities)
  3399. (if (boundp 'org-latex-entities)
  3400. org-latex-entities nil))
  3401. 'words))) ; FIXME
  3402. ))
  3403. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3404. (re-special (if org-export-with-special-strings
  3405. (mapcar (lambda (x) (car x))
  3406. org-export-html-special-string-regexps)))
  3407. (re-rest
  3408. (delq nil
  3409. (list
  3410. (if org-export-html-expand "@<[^>\n]+>")
  3411. ))))
  3412. (org-set-local
  3413. 'org-latex-and-specials-regexp
  3414. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3415. re-rest) "\\|")))))
  3416. (defface org-latex-and-export-specials
  3417. (let ((font (cond ((assq :inherit custom-face-attributes)
  3418. '(:inherit underline))
  3419. (t '(:underline t)))))
  3420. `((((class grayscale) (background light))
  3421. (:foreground "DimGray" ,@font))
  3422. (((class grayscale) (background dark))
  3423. (:foreground "LightGray" ,@font))
  3424. (((class color) (background light))
  3425. (:foreground "SaddleBrown"))
  3426. (((class color) (background dark))
  3427. (:foreground "burlywood"))
  3428. (t (,@font))))
  3429. "Face used to highlight math latex and other special exporter stuff."
  3430. :group 'org-faces)
  3431. (defun org-do-latex-and-special-faces (limit)
  3432. "Run through the buffer and add overlays to links."
  3433. (when org-latex-and-specials-regexp
  3434. (let (rtn d)
  3435. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3436. limit t))
  3437. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3438. 'face))
  3439. '(org-code org-verbatim underline)))
  3440. (progn
  3441. (setq rtn t
  3442. d (cond ((member (char-after (1+ (match-beginning 0)))
  3443. '(?_ ?^)) 1)
  3444. (t 0)))
  3445. (font-lock-prepend-text-property
  3446. (+ d (match-beginning 0)) (match-end 0)
  3447. 'face 'org-latex-and-export-specials)
  3448. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3449. '(font-lock-multiline t)))))
  3450. rtn)))
  3451. (defun org-restart-font-lock ()
  3452. "Restart font-lock-mode, to force refontification."
  3453. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3454. (font-lock-mode -1)
  3455. (font-lock-mode 1)))
  3456. (defun org-all-targets (&optional radio)
  3457. "Return a list of all targets in this file.
  3458. With optional argument RADIO, only find radio targets."
  3459. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3460. rtn)
  3461. (save-excursion
  3462. (goto-char (point-min))
  3463. (while (re-search-forward re nil t)
  3464. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3465. rtn)))
  3466. (defun org-make-target-link-regexp (targets)
  3467. "Make regular expression matching all strings in TARGETS.
  3468. The regular expression finds the targets also if there is a line break
  3469. between words."
  3470. (and targets
  3471. (concat
  3472. "\\<\\("
  3473. (mapconcat
  3474. (lambda (x)
  3475. (while (string-match " +" x)
  3476. (setq x (replace-match "\\s-+" t t x)))
  3477. x)
  3478. targets
  3479. "\\|")
  3480. "\\)\\>")))
  3481. (defun org-activate-tags (limit)
  3482. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3483. (progn
  3484. (add-text-properties (match-beginning 1) (match-end 1)
  3485. (list 'mouse-face 'highlight
  3486. 'rear-nonsticky org-nonsticky-props
  3487. 'keymap org-mouse-map))
  3488. t)))
  3489. (defun org-outline-level ()
  3490. (save-excursion
  3491. (looking-at outline-regexp)
  3492. (if (match-beginning 1)
  3493. (+ (org-get-string-indentation (match-string 1)) 1000)
  3494. (1- (- (match-end 0) (match-beginning 0))))))
  3495. (defvar org-font-lock-keywords nil)
  3496. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  3497. "Regular expression matching a property line.")
  3498. (defun org-set-font-lock-defaults ()
  3499. (let* ((em org-fontify-emphasized-text)
  3500. (lk org-activate-links)
  3501. (org-font-lock-extra-keywords
  3502. (list
  3503. ;; Headlines
  3504. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3505. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3506. ;; Table lines
  3507. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3508. (1 'org-table t))
  3509. ;; Table internals
  3510. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3511. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3512. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3513. ;; Drawers
  3514. (list org-drawer-regexp '(0 'org-special-keyword t))
  3515. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3516. ;; Properties
  3517. (list org-property-re
  3518. '(1 'org-special-keyword t)
  3519. '(3 'org-property-value t))
  3520. (if org-format-transports-properties-p
  3521. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3522. ;; Links
  3523. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3524. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3525. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3526. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3527. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3528. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3529. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3530. '(org-hide-wide-columns (0 nil append))
  3531. ;; TODO lines
  3532. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3533. '(1 (org-get-todo-face 1) t))
  3534. ;; DONE
  3535. (if org-fontify-done-headline
  3536. (list (concat "^[*]+ +\\<\\("
  3537. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3538. "\\)\\(.*\\)")
  3539. '(2 'org-headline-done t))
  3540. nil)
  3541. ;; Priorities
  3542. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3543. ;; Special keywords
  3544. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3545. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3546. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3547. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3548. ;; Emphasis
  3549. (if em
  3550. (if (featurep 'xemacs)
  3551. '(org-do-emphasis-faces (0 nil append))
  3552. '(org-do-emphasis-faces)))
  3553. ;; Checkboxes
  3554. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3555. 2 'bold prepend)
  3556. (if org-provide-checkbox-statistics
  3557. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3558. (0 (org-get-checkbox-statistics-face) t)))
  3559. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3560. '(1 'org-archived prepend))
  3561. ;; Specials
  3562. '(org-do-latex-and-special-faces)
  3563. ;; Code
  3564. '(org-activate-code (1 'org-code t))
  3565. ;; COMMENT
  3566. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3567. "\\|" org-quote-string "\\)\\>")
  3568. '(1 'org-special-keyword t))
  3569. '("^#.*" (0 'font-lock-comment-face t))
  3570. )))
  3571. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3572. ;; Now set the full font-lock-keywords
  3573. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3574. (org-set-local 'font-lock-defaults
  3575. '(org-font-lock-keywords t nil nil backward-paragraph))
  3576. (kill-local-variable 'font-lock-keywords) nil))
  3577. (defvar org-m nil)
  3578. (defvar org-l nil)
  3579. (defvar org-f nil)
  3580. (defun org-get-level-face (n)
  3581. "Get the right face for match N in font-lock matching of healdines."
  3582. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3583. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3584. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3585. (cond
  3586. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3587. ((eq n 2) org-f)
  3588. (t (if org-level-color-stars-only nil org-f))))
  3589. (defun org-get-todo-face (kwd)
  3590. "Get the right face for a TODO keyword KWD.
  3591. If KWD is a number, get the corresponding match group."
  3592. (if (numberp kwd) (setq kwd (match-string kwd)))
  3593. (or (cdr (assoc kwd org-todo-keyword-faces))
  3594. (and (member kwd org-done-keywords) 'org-done)
  3595. 'org-todo))
  3596. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3597. "Remove fontification and activation overlays from links."
  3598. (font-lock-default-unfontify-region beg end)
  3599. (let* ((buffer-undo-list t)
  3600. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3601. (inhibit-modification-hooks t)
  3602. deactivate-mark buffer-file-name buffer-file-truename)
  3603. (remove-text-properties beg end
  3604. '(mouse-face t keymap t org-linked-text t
  3605. invisible t intangible t))))
  3606. ;;;; Visibility cycling, including org-goto and indirect buffer
  3607. ;;; Cycling
  3608. (defvar org-cycle-global-status nil)
  3609. (make-variable-buffer-local 'org-cycle-global-status)
  3610. (defvar org-cycle-subtree-status nil)
  3611. (make-variable-buffer-local 'org-cycle-subtree-status)
  3612. ;;;###autoload
  3613. (defun org-cycle (&optional arg)
  3614. "Visibility cycling for Org-mode.
  3615. - When this function is called with a prefix argument, rotate the entire
  3616. buffer through 3 states (global cycling)
  3617. 1. OVERVIEW: Show only top-level headlines.
  3618. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3619. 3. SHOW ALL: Show everything.
  3620. - When point is at the beginning of a headline, rotate the subtree started
  3621. by this line through 3 different states (local cycling)
  3622. 1. FOLDED: Only the main headline is shown.
  3623. 2. CHILDREN: The main headline and the direct children are shown.
  3624. From this state, you can move to one of the children
  3625. and zoom in further.
  3626. 3. SUBTREE: Show the entire subtree, including body text.
  3627. - When there is a numeric prefix, go up to a heading with level ARG, do
  3628. a `show-subtree' and return to the previous cursor position. If ARG
  3629. is negative, go up that many levels.
  3630. - When point is not at the beginning of a headline, execute
  3631. `indent-relative', like TAB normally does. See the option
  3632. `org-cycle-emulate-tab' for details.
  3633. - Special case: if point is at the beginning of the buffer and there is
  3634. no headline in line 1, this function will act as if called with prefix arg.
  3635. But only if also the variable `org-cycle-global-at-bob' is t."
  3636. (interactive "P")
  3637. (org-load-modules-maybe)
  3638. (let* ((outline-regexp
  3639. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3640. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3641. outline-regexp))
  3642. (bob-special (and org-cycle-global-at-bob (bobp)
  3643. (not (looking-at outline-regexp))))
  3644. (org-cycle-hook
  3645. (if bob-special
  3646. (delq 'org-optimize-window-after-visibility-change
  3647. (copy-sequence org-cycle-hook))
  3648. org-cycle-hook))
  3649. (pos (point)))
  3650. (if (or bob-special (equal arg '(4)))
  3651. ;; special case: use global cycling
  3652. (setq arg t))
  3653. (cond
  3654. ((org-at-table-p 'any)
  3655. ;; Enter the table or move to the next field in the table
  3656. (or (org-table-recognize-table.el)
  3657. (progn
  3658. (if arg (org-table-edit-field t)
  3659. (org-table-justify-field-maybe)
  3660. (call-interactively 'org-table-next-field)))))
  3661. ((eq arg t) ;; Global cycling
  3662. (cond
  3663. ((and (eq last-command this-command)
  3664. (eq org-cycle-global-status 'overview))
  3665. ;; We just created the overview - now do table of contents
  3666. ;; This can be slow in very large buffers, so indicate action
  3667. (message "CONTENTS...")
  3668. (org-content)
  3669. (message "CONTENTS...done")
  3670. (setq org-cycle-global-status 'contents)
  3671. (run-hook-with-args 'org-cycle-hook 'contents))
  3672. ((and (eq last-command this-command)
  3673. (eq org-cycle-global-status 'contents))
  3674. ;; We just showed the table of contents - now show everything
  3675. (show-all)
  3676. (message "SHOW ALL")
  3677. (setq org-cycle-global-status 'all)
  3678. (run-hook-with-args 'org-cycle-hook 'all))
  3679. (t
  3680. ;; Default action: go to overview
  3681. (org-overview)
  3682. (message "OVERVIEW")
  3683. (setq org-cycle-global-status 'overview)
  3684. (run-hook-with-args 'org-cycle-hook 'overview))))
  3685. ((and org-drawers org-drawer-regexp
  3686. (save-excursion
  3687. (beginning-of-line 1)
  3688. (looking-at org-drawer-regexp)))
  3689. ;; Toggle block visibility
  3690. (org-flag-drawer
  3691. (not (get-char-property (match-end 0) 'invisible))))
  3692. ((integerp arg)
  3693. ;; Show-subtree, ARG levels up from here.
  3694. (save-excursion
  3695. (org-back-to-heading)
  3696. (outline-up-heading (if (< arg 0) (- arg)
  3697. (- (funcall outline-level) arg)))
  3698. (org-show-subtree)))
  3699. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3700. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3701. ;; At a heading: rotate between three different views
  3702. (org-back-to-heading)
  3703. (let ((goal-column 0) eoh eol eos)
  3704. ;; First, some boundaries
  3705. (save-excursion
  3706. (org-back-to-heading)
  3707. (save-excursion
  3708. (beginning-of-line 2)
  3709. (while (and (not (eobp)) ;; this is like `next-line'
  3710. (get-char-property (1- (point)) 'invisible))
  3711. (beginning-of-line 2)) (setq eol (point)))
  3712. (outline-end-of-heading) (setq eoh (point))
  3713. (org-end-of-subtree t)
  3714. (unless (eobp)
  3715. (skip-chars-forward " \t\n")
  3716. (beginning-of-line 1) ; in case this is an item
  3717. )
  3718. (setq eos (1- (point))))
  3719. ;; Find out what to do next and set `this-command'
  3720. (cond
  3721. ((= eos eoh)
  3722. ;; Nothing is hidden behind this heading
  3723. (message "EMPTY ENTRY")
  3724. (setq org-cycle-subtree-status nil)
  3725. (save-excursion
  3726. (goto-char eos)
  3727. (outline-next-heading)
  3728. (if (org-invisible-p) (org-flag-heading nil))))
  3729. ((or (>= eol eos)
  3730. (not (string-match "\\S-" (buffer-substring eol eos))))
  3731. ;; Entire subtree is hidden in one line: open it
  3732. (org-show-entry)
  3733. (show-children)
  3734. (message "CHILDREN")
  3735. (save-excursion
  3736. (goto-char eos)
  3737. (outline-next-heading)
  3738. (if (org-invisible-p) (org-flag-heading nil)))
  3739. (setq org-cycle-subtree-status 'children)
  3740. (run-hook-with-args 'org-cycle-hook 'children))
  3741. ((and (eq last-command this-command)
  3742. (eq org-cycle-subtree-status 'children))
  3743. ;; We just showed the children, now show everything.
  3744. (org-show-subtree)
  3745. (message "SUBTREE")
  3746. (setq org-cycle-subtree-status 'subtree)
  3747. (run-hook-with-args 'org-cycle-hook 'subtree))
  3748. (t
  3749. ;; Default action: hide the subtree.
  3750. (hide-subtree)
  3751. (message "FOLDED")
  3752. (setq org-cycle-subtree-status 'folded)
  3753. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3754. ;; TAB emulation
  3755. (buffer-read-only (org-back-to-heading))
  3756. ((org-try-cdlatex-tab))
  3757. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3758. (or (not (bolp))
  3759. (not (looking-at outline-regexp))))
  3760. (call-interactively (global-key-binding "\t")))
  3761. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3762. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3763. (or (and (eq org-cycle-emulate-tab 'white)
  3764. (= (match-end 0) (point-at-eol)))
  3765. (and (eq org-cycle-emulate-tab 'whitestart)
  3766. (>= (match-end 0) pos))))
  3767. t
  3768. (eq org-cycle-emulate-tab t))
  3769. (call-interactively (global-key-binding "\t")))
  3770. (t (save-excursion
  3771. (org-back-to-heading)
  3772. (org-cycle))))))
  3773. ;;;###autoload
  3774. (defun org-global-cycle (&optional arg)
  3775. "Cycle the global visibility. For details see `org-cycle'."
  3776. (interactive "P")
  3777. (let ((org-cycle-include-plain-lists
  3778. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3779. (if (integerp arg)
  3780. (progn
  3781. (show-all)
  3782. (hide-sublevels arg)
  3783. (setq org-cycle-global-status 'contents))
  3784. (org-cycle '(4)))))
  3785. (defun org-overview ()
  3786. "Switch to overview mode, shoing only top-level headlines.
  3787. Really, this shows all headlines with level equal or greater than the level
  3788. of the first headline in the buffer. This is important, because if the
  3789. first headline is not level one, then (hide-sublevels 1) gives confusing
  3790. results."
  3791. (interactive)
  3792. (let ((level (save-excursion
  3793. (goto-char (point-min))
  3794. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3795. (progn
  3796. (goto-char (match-beginning 0))
  3797. (funcall outline-level))))))
  3798. (and level (hide-sublevels level))))
  3799. (defun org-content (&optional arg)
  3800. "Show all headlines in the buffer, like a table of contents.
  3801. With numerical argument N, show content up to level N."
  3802. (interactive "P")
  3803. (save-excursion
  3804. ;; Visit all headings and show their offspring
  3805. (and (integerp arg) (org-overview))
  3806. (goto-char (point-max))
  3807. (catch 'exit
  3808. (while (and (progn (condition-case nil
  3809. (outline-previous-visible-heading 1)
  3810. (error (goto-char (point-min))))
  3811. t)
  3812. (looking-at outline-regexp))
  3813. (if (integerp arg)
  3814. (show-children (1- arg))
  3815. (show-branches))
  3816. (if (bobp) (throw 'exit nil))))))
  3817. (defun org-optimize-window-after-visibility-change (state)
  3818. "Adjust the window after a change in outline visibility.
  3819. This function is the default value of the hook `org-cycle-hook'."
  3820. (when (get-buffer-window (current-buffer))
  3821. (cond
  3822. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3823. ; ((eq state 'overview) (org-beginning-of-line))
  3824. ((eq state 'content) nil)
  3825. ((eq state 'all) nil)
  3826. ((eq state 'folded) nil)
  3827. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3828. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3829. (defun org-compact-display-after-subtree-move ()
  3830. (let (beg end)
  3831. (save-excursion
  3832. (if (org-up-heading-safe)
  3833. (progn
  3834. (hide-subtree)
  3835. (show-entry)
  3836. (show-children)
  3837. (org-cycle-show-empty-lines 'children)
  3838. (org-cycle-hide-drawers 'children))
  3839. (org-overview)))))
  3840. (defun org-cycle-show-empty-lines (state)
  3841. "Show empty lines above all visible headlines.
  3842. The region to be covered depends on STATE when called through
  3843. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3844. entire buffer covered. Note that an empty line is only shown if there
  3845. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3846. (when (> org-cycle-separator-lines 0)
  3847. (save-excursion
  3848. (let* ((n org-cycle-separator-lines)
  3849. (re (cond
  3850. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3851. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3852. (t (let ((ns (number-to-string (- n 2))))
  3853. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3854. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3855. beg end)
  3856. (cond
  3857. ((memq state '(overview contents t))
  3858. (setq beg (point-min) end (point-max)))
  3859. ((memq state '(children folded))
  3860. (setq beg (point) end (progn (org-end-of-subtree t t)
  3861. (beginning-of-line 2)
  3862. (point)))))
  3863. (when beg
  3864. (goto-char beg)
  3865. (while (re-search-forward re end t)
  3866. (if (not (get-char-property (match-end 1) 'invisible))
  3867. (outline-flag-region
  3868. (match-beginning 1) (match-end 1) nil)))))))
  3869. ;; Never hide empty lines at the end of the file.
  3870. (save-excursion
  3871. (goto-char (point-max))
  3872. (outline-previous-heading)
  3873. (outline-end-of-heading)
  3874. (if (and (looking-at "[ \t\n]+")
  3875. (= (match-end 0) (point-max)))
  3876. (outline-flag-region (point) (match-end 0) nil))))
  3877. (defun org-subtree-end-visible-p ()
  3878. "Is the end of the current subtree visible?"
  3879. (pos-visible-in-window-p
  3880. (save-excursion (org-end-of-subtree t) (point))))
  3881. (defun org-first-headline-recenter (&optional N)
  3882. "Move cursor to the first headline and recenter the headline.
  3883. Optional argument N means, put the headline into the Nth line of the window."
  3884. (goto-char (point-min))
  3885. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3886. (beginning-of-line)
  3887. (recenter (prefix-numeric-value N))))
  3888. ;;; Org-goto
  3889. (defvar org-goto-window-configuration nil)
  3890. (defvar org-goto-marker nil)
  3891. (defvar org-goto-map
  3892. (let ((map (make-sparse-keymap)))
  3893. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3894. (while (setq cmd (pop cmds))
  3895. (substitute-key-definition cmd cmd map global-map)))
  3896. (suppress-keymap map)
  3897. (org-defkey map "\C-m" 'org-goto-ret)
  3898. (org-defkey map [(return)] 'org-goto-ret)
  3899. (org-defkey map [(left)] 'org-goto-left)
  3900. (org-defkey map [(right)] 'org-goto-right)
  3901. (org-defkey map [(control ?g)] 'org-goto-quit)
  3902. (org-defkey map "\C-i" 'org-cycle)
  3903. (org-defkey map [(tab)] 'org-cycle)
  3904. (org-defkey map [(down)] 'outline-next-visible-heading)
  3905. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3906. (if org-goto-auto-isearch
  3907. (if (fboundp 'define-key-after)
  3908. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3909. nil)
  3910. (org-defkey map "q" 'org-goto-quit)
  3911. (org-defkey map "n" 'outline-next-visible-heading)
  3912. (org-defkey map "p" 'outline-previous-visible-heading)
  3913. (org-defkey map "f" 'outline-forward-same-level)
  3914. (org-defkey map "b" 'outline-backward-same-level)
  3915. (org-defkey map "u" 'outline-up-heading))
  3916. (org-defkey map "/" 'org-occur)
  3917. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3918. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3919. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3920. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3921. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3922. map))
  3923. (defconst org-goto-help
  3924. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3925. RET=jump to location [Q]uit and return to previous location
  3926. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3927. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3928. (defun org-goto (&optional alternative-interface)
  3929. "Look up a different location in the current file, keeping current visibility.
  3930. When you want look-up or go to a different location in a document, the
  3931. fastest way is often to fold the entire buffer and then dive into the tree.
  3932. This method has the disadvantage, that the previous location will be folded,
  3933. which may not be what you want.
  3934. This command works around this by showing a copy of the current buffer
  3935. in an indirect buffer, in overview mode. You can dive into the tree in
  3936. that copy, use org-occur and incremental search to find a location.
  3937. When pressing RET or `Q', the command returns to the original buffer in
  3938. which the visibility is still unchanged. After RET is will also jump to
  3939. the location selected in the indirect buffer and expose the
  3940. the headline hierarchy above."
  3941. (interactive "P")
  3942. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3943. (org-refile-use-outline-path t)
  3944. (interface
  3945. (if (not alternative-interface)
  3946. org-goto-interface
  3947. (if (eq org-goto-interface 'outline)
  3948. 'outline-path-completion
  3949. 'outline)))
  3950. (org-goto-start-pos (point))
  3951. (selected-point
  3952. (if (eq interface 'outline)
  3953. (car (org-get-location (current-buffer) org-goto-help))
  3954. (nth 3 (org-refile-get-location "Goto: ")))))
  3955. (if selected-point
  3956. (progn
  3957. (org-mark-ring-push org-goto-start-pos)
  3958. (goto-char selected-point)
  3959. (if (or (org-invisible-p) (org-invisible-p2))
  3960. (org-show-context 'org-goto)))
  3961. (message "Quit"))))
  3962. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3963. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3964. (defvar org-goto-local-auto-isearch-map) ; defined below
  3965. (defun org-get-location (buf help)
  3966. "Let the user select a location in the Org-mode buffer BUF.
  3967. This function uses a recursive edit. It returns the selected position
  3968. or nil."
  3969. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3970. (isearch-hide-immediately nil)
  3971. (isearch-search-fun-function
  3972. (lambda () 'org-goto-local-search-forward-headings))
  3973. (org-goto-selected-point org-goto-exit-command))
  3974. (save-excursion
  3975. (save-window-excursion
  3976. (delete-other-windows)
  3977. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3978. (switch-to-buffer
  3979. (condition-case nil
  3980. (make-indirect-buffer (current-buffer) "*org-goto*")
  3981. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3982. (with-output-to-temp-buffer "*Help*"
  3983. (princ help))
  3984. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3985. (setq buffer-read-only nil)
  3986. (let ((org-startup-truncated t)
  3987. (org-startup-folded nil)
  3988. (org-startup-align-all-tables nil))
  3989. (org-mode)
  3990. (org-overview))
  3991. (setq buffer-read-only t)
  3992. (if (and (boundp 'org-goto-start-pos)
  3993. (integer-or-marker-p org-goto-start-pos))
  3994. (let ((org-show-hierarchy-above t)
  3995. (org-show-siblings t)
  3996. (org-show-following-heading t))
  3997. (goto-char org-goto-start-pos)
  3998. (and (org-invisible-p) (org-show-context)))
  3999. (goto-char (point-min)))
  4000. (org-beginning-of-line)
  4001. (message "Select location and press RET")
  4002. (use-local-map org-goto-map)
  4003. (recursive-edit)
  4004. ))
  4005. (kill-buffer "*org-goto*")
  4006. (cons org-goto-selected-point org-goto-exit-command)))
  4007. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4008. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4009. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4010. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4011. (defun org-goto-local-search-forward-headings (string bound noerror)
  4012. "Search and make sure that anu matches are in headlines."
  4013. (catch 'return
  4014. (while (search-forward string bound noerror)
  4015. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4016. (and (member :headline context)
  4017. (not (member :tags context))))
  4018. (throw 'return (point))))))
  4019. (defun org-goto-local-auto-isearch ()
  4020. "Start isearch."
  4021. (interactive)
  4022. (goto-char (point-min))
  4023. (let ((keys (this-command-keys)))
  4024. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4025. (isearch-mode t)
  4026. (isearch-process-search-char (string-to-char keys)))))
  4027. (defun org-goto-ret (&optional arg)
  4028. "Finish `org-goto' by going to the new location."
  4029. (interactive "P")
  4030. (setq org-goto-selected-point (point)
  4031. org-goto-exit-command 'return)
  4032. (throw 'exit nil))
  4033. (defun org-goto-left ()
  4034. "Finish `org-goto' by going to the new location."
  4035. (interactive)
  4036. (if (org-on-heading-p)
  4037. (progn
  4038. (beginning-of-line 1)
  4039. (setq org-goto-selected-point (point)
  4040. org-goto-exit-command 'left)
  4041. (throw 'exit nil))
  4042. (error "Not on a heading")))
  4043. (defun org-goto-right ()
  4044. "Finish `org-goto' by going to the new location."
  4045. (interactive)
  4046. (if (org-on-heading-p)
  4047. (progn
  4048. (setq org-goto-selected-point (point)
  4049. org-goto-exit-command 'right)
  4050. (throw 'exit nil))
  4051. (error "Not on a heading")))
  4052. (defun org-goto-quit ()
  4053. "Finish `org-goto' without cursor motion."
  4054. (interactive)
  4055. (setq org-goto-selected-point nil)
  4056. (setq org-goto-exit-command 'quit)
  4057. (throw 'exit nil))
  4058. ;;; Indirect buffer display of subtrees
  4059. (defvar org-indirect-dedicated-frame nil
  4060. "This is the frame being used for indirect tree display.")
  4061. (defvar org-last-indirect-buffer nil)
  4062. (defun org-tree-to-indirect-buffer (&optional arg)
  4063. "Create indirect buffer and narrow it to current subtree.
  4064. With numerical prefix ARG, go up to this level and then take that tree.
  4065. If ARG is negative, go up that many levels.
  4066. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4067. indirect buffer previously made with this command, to avoid proliferation of
  4068. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4069. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4070. is kept so that you can work with several indirect buffers at the same time.
  4071. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4072. requests that a new frame be made for the new buffer, so that the dedicated
  4073. frame is not changed."
  4074. (interactive "P")
  4075. (let ((cbuf (current-buffer))
  4076. (cwin (selected-window))
  4077. (pos (point))
  4078. beg end level heading ibuf)
  4079. (save-excursion
  4080. (org-back-to-heading t)
  4081. (when (numberp arg)
  4082. (setq level (org-outline-level))
  4083. (if (< arg 0) (setq arg (+ level arg)))
  4084. (while (> (setq level (org-outline-level)) arg)
  4085. (outline-up-heading 1 t)))
  4086. (setq beg (point)
  4087. heading (org-get-heading))
  4088. (org-end-of-subtree t) (setq end (point)))
  4089. (if (and (buffer-live-p org-last-indirect-buffer)
  4090. (not (eq org-indirect-buffer-display 'new-frame))
  4091. (not arg))
  4092. (kill-buffer org-last-indirect-buffer))
  4093. (setq ibuf (org-get-indirect-buffer cbuf)
  4094. org-last-indirect-buffer ibuf)
  4095. (cond
  4096. ((or (eq org-indirect-buffer-display 'new-frame)
  4097. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4098. (select-frame (make-frame))
  4099. (delete-other-windows)
  4100. (switch-to-buffer ibuf)
  4101. (org-set-frame-title heading))
  4102. ((eq org-indirect-buffer-display 'dedicated-frame)
  4103. (raise-frame
  4104. (select-frame (or (and org-indirect-dedicated-frame
  4105. (frame-live-p org-indirect-dedicated-frame)
  4106. org-indirect-dedicated-frame)
  4107. (setq org-indirect-dedicated-frame (make-frame)))))
  4108. (delete-other-windows)
  4109. (switch-to-buffer ibuf)
  4110. (org-set-frame-title (concat "Indirect: " heading)))
  4111. ((eq org-indirect-buffer-display 'current-window)
  4112. (switch-to-buffer ibuf))
  4113. ((eq org-indirect-buffer-display 'other-window)
  4114. (pop-to-buffer ibuf))
  4115. (t (error "Invalid value.")))
  4116. (if (featurep 'xemacs)
  4117. (save-excursion (org-mode) (turn-on-font-lock)))
  4118. (narrow-to-region beg end)
  4119. (show-all)
  4120. (goto-char pos)
  4121. (and (window-live-p cwin) (select-window cwin))))
  4122. (defun org-get-indirect-buffer (&optional buffer)
  4123. (setq buffer (or buffer (current-buffer)))
  4124. (let ((n 1) (base (buffer-name buffer)) bname)
  4125. (while (buffer-live-p
  4126. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4127. (setq n (1+ n)))
  4128. (condition-case nil
  4129. (make-indirect-buffer buffer bname 'clone)
  4130. (error (make-indirect-buffer buffer bname)))))
  4131. (defun org-set-frame-title (title)
  4132. "Set the title of the current frame to the string TITLE."
  4133. ;; FIXME: how to name a single frame in XEmacs???
  4134. (unless (featurep 'xemacs)
  4135. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4136. ;;;; Structure editing
  4137. ;;; Inserting headlines
  4138. (defun org-insert-heading (&optional force-heading)
  4139. "Insert a new heading or item with same depth at point.
  4140. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4141. If point is at the beginning of a headline, insert a sibling before the
  4142. current headline. If point is not at the beginning, do not split the line,
  4143. but create the new hedline after the current line."
  4144. (interactive "P")
  4145. (if (= (buffer-size) 0)
  4146. (insert "\n* ")
  4147. (when (or force-heading (not (org-insert-item)))
  4148. (let* ((head (save-excursion
  4149. (condition-case nil
  4150. (progn
  4151. (org-back-to-heading)
  4152. (match-string 0))
  4153. (error "*"))))
  4154. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4155. pos)
  4156. (cond
  4157. ((and (org-on-heading-p) (bolp)
  4158. (or (bobp)
  4159. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4160. ;; insert before the current line
  4161. (open-line (if blank 2 1)))
  4162. ((and (bolp)
  4163. (or (bobp)
  4164. (save-excursion
  4165. (backward-char 1) (not (org-invisible-p)))))
  4166. ;; insert right here
  4167. nil)
  4168. (t
  4169. ;; in the middle of the line
  4170. (org-show-entry)
  4171. (let ((split
  4172. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4173. tags pos)
  4174. (if (org-on-heading-p)
  4175. (progn
  4176. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4177. (setq tags (and (match-end 2) (match-string 2)))
  4178. (and (match-end 1)
  4179. (delete-region (match-beginning 1) (match-end 1)))
  4180. (setq pos (point-at-bol))
  4181. (or split (end-of-line 1))
  4182. (delete-horizontal-space)
  4183. (newline (if blank 2 1))
  4184. (when tags
  4185. (save-excursion
  4186. (goto-char pos)
  4187. (end-of-line 1)
  4188. (insert " " tags)
  4189. (org-set-tags nil 'align))))
  4190. (or split (end-of-line 1))
  4191. (newline (if blank 2 1))))))
  4192. (insert head) (just-one-space)
  4193. (setq pos (point))
  4194. (end-of-line 1)
  4195. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4196. (run-hooks 'org-insert-heading-hook)))))
  4197. (defun org-get-heading (&optional no-tags)
  4198. "Return the heading of the current entry, without the stars."
  4199. (save-excursion
  4200. (org-back-to-heading t)
  4201. (if (looking-at
  4202. (if no-tags
  4203. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4204. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4205. (match-string 1) "")))
  4206. (defun org-insert-heading-after-current ()
  4207. "Insert a new heading with same level as current, after current subtree."
  4208. (interactive)
  4209. (org-back-to-heading)
  4210. (org-insert-heading)
  4211. (org-move-subtree-down)
  4212. (end-of-line 1))
  4213. (defun org-insert-todo-heading (arg)
  4214. "Insert a new heading with the same level and TODO state as current heading.
  4215. If the heading has no TODO state, or if the state is DONE, use the first
  4216. state (TODO by default). Also with prefix arg, force first state."
  4217. (interactive "P")
  4218. (when (not (org-insert-item 'checkbox))
  4219. (org-insert-heading)
  4220. (save-excursion
  4221. (org-back-to-heading)
  4222. (outline-previous-heading)
  4223. (looking-at org-todo-line-regexp))
  4224. (if (or arg
  4225. (not (match-beginning 2))
  4226. (member (match-string 2) org-done-keywords))
  4227. (insert (car org-todo-keywords-1) " ")
  4228. (insert (match-string 2) " "))))
  4229. (defun org-insert-subheading (arg)
  4230. "Insert a new subheading and demote it.
  4231. Works for outline headings and for plain lists alike."
  4232. (interactive "P")
  4233. (org-insert-heading arg)
  4234. (cond
  4235. ((org-on-heading-p) (org-do-demote))
  4236. ((org-at-item-p) (org-indent-item 1))))
  4237. (defun org-insert-todo-subheading (arg)
  4238. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4239. Works for outline headings and for plain lists alike."
  4240. (interactive "P")
  4241. (org-insert-todo-heading arg)
  4242. (cond
  4243. ((org-on-heading-p) (org-do-demote))
  4244. ((org-at-item-p) (org-indent-item 1))))
  4245. ;;; Promotion and Demotion
  4246. (defun org-promote-subtree ()
  4247. "Promote the entire subtree.
  4248. See also `org-promote'."
  4249. (interactive)
  4250. (save-excursion
  4251. (org-map-tree 'org-promote))
  4252. (org-fix-position-after-promote))
  4253. (defun org-demote-subtree ()
  4254. "Demote the entire subtree. See `org-demote'.
  4255. See also `org-promote'."
  4256. (interactive)
  4257. (save-excursion
  4258. (org-map-tree 'org-demote))
  4259. (org-fix-position-after-promote))
  4260. (defun org-do-promote ()
  4261. "Promote the current heading higher up the tree.
  4262. If the region is active in `transient-mark-mode', promote all headings
  4263. in the region."
  4264. (interactive)
  4265. (save-excursion
  4266. (if (org-region-active-p)
  4267. (org-map-region 'org-promote (region-beginning) (region-end))
  4268. (org-promote)))
  4269. (org-fix-position-after-promote))
  4270. (defun org-do-demote ()
  4271. "Demote the current heading lower down the tree.
  4272. If the region is active in `transient-mark-mode', demote all headings
  4273. in the region."
  4274. (interactive)
  4275. (save-excursion
  4276. (if (org-region-active-p)
  4277. (org-map-region 'org-demote (region-beginning) (region-end))
  4278. (org-demote)))
  4279. (org-fix-position-after-promote))
  4280. (defun org-fix-position-after-promote ()
  4281. "Make sure that after pro/demotion cursor position is right."
  4282. (let ((pos (point)))
  4283. (when (save-excursion
  4284. (beginning-of-line 1)
  4285. (looking-at org-todo-line-regexp)
  4286. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4287. (cond ((eobp) (insert " "))
  4288. ((eolp) (insert " "))
  4289. ((equal (char-after) ?\ ) (forward-char 1))))))
  4290. (defun org-reduced-level (l)
  4291. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4292. (defun org-get-valid-level (level &optional change)
  4293. "Rectify a level change under the influence of `org-odd-levels-only'
  4294. LEVEL is a current level, CHANGE is by how much the level should be
  4295. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4296. even level numbers will become the next higher odd number."
  4297. (if org-odd-levels-only
  4298. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4299. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4300. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4301. (max 1 (+ level change))))
  4302. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4303. (define-obsolete-function-alias 'org-get-legal-level
  4304. 'org-get-valid-level)
  4305. (define-obsolete-function-alias 'org-get-legal-level
  4306. 'org-get-valid-level "23.1"))
  4307. (defun org-promote ()
  4308. "Promote the current heading higher up the tree.
  4309. If the region is active in `transient-mark-mode', promote all headings
  4310. in the region."
  4311. (org-back-to-heading t)
  4312. (let* ((level (save-match-data (funcall outline-level)))
  4313. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4314. (diff (abs (- level (length up-head) -1))))
  4315. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4316. (replace-match up-head nil t)
  4317. ;; Fixup tag positioning
  4318. (and org-auto-align-tags (org-set-tags nil t))
  4319. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4320. (defun org-demote ()
  4321. "Demote the current heading lower down the tree.
  4322. If the region is active in `transient-mark-mode', demote all headings
  4323. in the region."
  4324. (org-back-to-heading t)
  4325. (let* ((level (save-match-data (funcall outline-level)))
  4326. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4327. (diff (abs (- level (length down-head) -1))))
  4328. (replace-match down-head nil t)
  4329. ;; Fixup tag positioning
  4330. (and org-auto-align-tags (org-set-tags nil t))
  4331. (if org-adapt-indentation (org-fixup-indentation diff))))
  4332. (defun org-map-tree (fun)
  4333. "Call FUN for every heading underneath the current one."
  4334. (org-back-to-heading)
  4335. (let ((level (funcall outline-level)))
  4336. (save-excursion
  4337. (funcall fun)
  4338. (while (and (progn
  4339. (outline-next-heading)
  4340. (> (funcall outline-level) level))
  4341. (not (eobp)))
  4342. (funcall fun)))))
  4343. (defun org-map-region (fun beg end)
  4344. "Call FUN for every heading between BEG and END."
  4345. (let ((org-ignore-region t))
  4346. (save-excursion
  4347. (setq end (copy-marker end))
  4348. (goto-char beg)
  4349. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4350. (< (point) end))
  4351. (funcall fun))
  4352. (while (and (progn
  4353. (outline-next-heading)
  4354. (< (point) end))
  4355. (not (eobp)))
  4356. (funcall fun)))))
  4357. (defun org-fixup-indentation (diff)
  4358. "Change the indentation in the current entry by DIFF
  4359. However, if any line in the current entry has no indentation, or if it
  4360. would end up with no indentation after the change, nothing at all is done."
  4361. (save-excursion
  4362. (let ((end (save-excursion (outline-next-heading)
  4363. (point-marker)))
  4364. (prohibit (if (> diff 0)
  4365. "^\\S-"
  4366. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4367. col)
  4368. (unless (save-excursion (end-of-line 1)
  4369. (re-search-forward prohibit end t))
  4370. (while (and (< (point) end)
  4371. (re-search-forward "^[ \t]+" end t))
  4372. (goto-char (match-end 0))
  4373. (setq col (current-column))
  4374. (if (< diff 0) (replace-match ""))
  4375. (indent-to (+ diff col))))
  4376. (move-marker end nil))))
  4377. (defun org-convert-to-odd-levels ()
  4378. "Convert an org-mode file with all levels allowed to one with odd levels.
  4379. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4380. level 5 etc."
  4381. (interactive)
  4382. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4383. (let ((org-odd-levels-only nil) n)
  4384. (save-excursion
  4385. (goto-char (point-min))
  4386. (while (re-search-forward "^\\*\\*+ " nil t)
  4387. (setq n (- (length (match-string 0)) 2))
  4388. (while (>= (setq n (1- n)) 0)
  4389. (org-demote))
  4390. (end-of-line 1))))))
  4391. (defun org-convert-to-oddeven-levels ()
  4392. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4393. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4394. section with an even level, conversion would destroy the structure of the file. An error
  4395. is signaled in this case."
  4396. (interactive)
  4397. (goto-char (point-min))
  4398. ;; First check if there are no even levels
  4399. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4400. (org-show-context t)
  4401. (error "Not all levels are odd in this file. Conversion not possible."))
  4402. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4403. (let ((org-odd-levels-only nil) n)
  4404. (save-excursion
  4405. (goto-char (point-min))
  4406. (while (re-search-forward "^\\*\\*+ " nil t)
  4407. (setq n (/ (1- (length (match-string 0))) 2))
  4408. (while (>= (setq n (1- n)) 0)
  4409. (org-promote))
  4410. (end-of-line 1))))))
  4411. (defun org-tr-level (n)
  4412. "Make N odd if required."
  4413. (if org-odd-levels-only (1+ (/ n 2)) n))
  4414. ;;; Vertical tree motion, cutting and pasting of subtrees
  4415. (defun org-move-subtree-up (&optional arg)
  4416. "Move the current subtree up past ARG headlines of the same level."
  4417. (interactive "p")
  4418. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4419. (defun org-move-subtree-down (&optional arg)
  4420. "Move the current subtree down past ARG headlines of the same level."
  4421. (interactive "p")
  4422. (setq arg (prefix-numeric-value arg))
  4423. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4424. 'outline-get-last-sibling))
  4425. (ins-point (make-marker))
  4426. (cnt (abs arg))
  4427. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4428. ;; Select the tree
  4429. (org-back-to-heading)
  4430. (setq beg0 (point))
  4431. (save-excursion
  4432. (setq ne-beg (org-back-over-empty-lines))
  4433. (setq beg (point)))
  4434. (save-match-data
  4435. (save-excursion (outline-end-of-heading)
  4436. (setq folded (org-invisible-p)))
  4437. (outline-end-of-subtree))
  4438. (outline-next-heading)
  4439. (setq ne-end (org-back-over-empty-lines))
  4440. (setq end (point))
  4441. (goto-char beg0)
  4442. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4443. ;; include less whitespace
  4444. (save-excursion
  4445. (goto-char beg)
  4446. (forward-line (- ne-beg ne-end))
  4447. (setq beg (point))))
  4448. ;; Find insertion point, with error handling
  4449. (while (> cnt 0)
  4450. (or (and (funcall movfunc) (looking-at outline-regexp))
  4451. (progn (goto-char beg0)
  4452. (error "Cannot move past superior level or buffer limit")))
  4453. (setq cnt (1- cnt)))
  4454. (if (> arg 0)
  4455. ;; Moving forward - still need to move over subtree
  4456. (progn (org-end-of-subtree t t)
  4457. (save-excursion
  4458. (org-back-over-empty-lines)
  4459. (or (bolp) (newline)))))
  4460. (setq ne-ins (org-back-over-empty-lines))
  4461. (move-marker ins-point (point))
  4462. (setq txt (buffer-substring beg end))
  4463. (delete-region beg end)
  4464. (outline-flag-region (1- beg) beg nil)
  4465. (outline-flag-region (1- (point)) (point) nil)
  4466. (insert txt)
  4467. (or (bolp) (insert "\n"))
  4468. (setq ins-end (point))
  4469. (goto-char ins-point)
  4470. (org-skip-whitespace)
  4471. (when (and (< arg 0)
  4472. (org-first-sibling-p)
  4473. (> ne-ins ne-beg))
  4474. ;; Move whitespace back to beginning
  4475. (save-excursion
  4476. (goto-char ins-end)
  4477. (let ((kill-whole-line t))
  4478. (kill-line (- ne-ins ne-beg)) (point)))
  4479. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4480. (move-marker ins-point nil)
  4481. (org-compact-display-after-subtree-move)
  4482. (unless folded
  4483. (org-show-entry)
  4484. (show-children)
  4485. (org-cycle-hide-drawers 'children))))
  4486. (defvar org-subtree-clip ""
  4487. "Clipboard for cut and paste of subtrees.
  4488. This is actually only a copy of the kill, because we use the normal kill
  4489. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4490. (defvar org-subtree-clip-folded nil
  4491. "Was the last copied subtree folded?
  4492. This is used to fold the tree back after pasting.")
  4493. (defun org-cut-subtree (&optional n)
  4494. "Cut the current subtree into the clipboard.
  4495. With prefix arg N, cut this many sequential subtrees.
  4496. This is a short-hand for marking the subtree and then cutting it."
  4497. (interactive "p")
  4498. (org-copy-subtree n 'cut))
  4499. (defun org-copy-subtree (&optional n cut)
  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 copying it.
  4503. If CUT is non-nil, actually cut the subtree."
  4504. (interactive "p")
  4505. (let (beg end folded (beg0 (point)))
  4506. (if (interactive-p)
  4507. (org-back-to-heading nil) ; take what looks like a subtree
  4508. (org-back-to-heading t)) ; take what is really there
  4509. (org-back-over-empty-lines)
  4510. (setq beg (point))
  4511. (skip-chars-forward " \t\r\n")
  4512. (save-match-data
  4513. (save-excursion (outline-end-of-heading)
  4514. (setq folded (org-invisible-p)))
  4515. (condition-case nil
  4516. (outline-forward-same-level (1- n))
  4517. (error nil))
  4518. (org-end-of-subtree t t))
  4519. (org-back-over-empty-lines)
  4520. (setq end (point))
  4521. (goto-char beg0)
  4522. (when (> end beg)
  4523. (setq org-subtree-clip-folded folded)
  4524. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4525. (setq org-subtree-clip (current-kill 0))
  4526. (message "%s: Subtree(s) with %d characters"
  4527. (if cut "Cut" "Copied")
  4528. (length org-subtree-clip)))))
  4529. (defun org-paste-subtree (&optional level tree)
  4530. "Paste the clipboard as a subtree, with modification of headline level.
  4531. The entire subtree is promoted or demoted in order to match a new headline
  4532. level. By default, the new level is derived from the visible headings
  4533. before and after the insertion point, and taken to be the inferior headline
  4534. level of the two. So if the previous visible heading is level 3 and the
  4535. next is level 4 (or vice versa), level 4 will be used for insertion.
  4536. This makes sure that the subtree remains an independent subtree and does
  4537. not swallow low level entries.
  4538. You can also force a different level, either by using a numeric prefix
  4539. argument, or by inserting the heading marker by hand. For example, if the
  4540. cursor is after \"*****\", then the tree will be shifted to level 5.
  4541. If you want to insert the tree as is, just use \\[yank].
  4542. If optional TREE is given, use this text instead of the kill ring."
  4543. (interactive "P")
  4544. (unless (org-kill-is-subtree-p tree)
  4545. (error "%s"
  4546. (substitute-command-keys
  4547. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4548. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4549. (^re (concat "^\\(" outline-regexp "\\)"))
  4550. (re (concat "\\(" outline-regexp "\\)"))
  4551. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4552. (old-level (if (string-match ^re txt)
  4553. (- (match-end 0) (match-beginning 0) 1)
  4554. -1))
  4555. (force-level (cond (level (prefix-numeric-value level))
  4556. ((string-match
  4557. ^re_ (buffer-substring (point-at-bol) (point)))
  4558. (- (match-end 1) (match-beginning 1)))
  4559. (t nil)))
  4560. (previous-level (save-excursion
  4561. (condition-case nil
  4562. (progn
  4563. (outline-previous-visible-heading 1)
  4564. (if (looking-at re)
  4565. (- (match-end 0) (match-beginning 0) 1)
  4566. 1))
  4567. (error 1))))
  4568. (next-level (save-excursion
  4569. (condition-case nil
  4570. (progn
  4571. (or (looking-at outline-regexp)
  4572. (outline-next-visible-heading 1))
  4573. (if (looking-at re)
  4574. (- (match-end 0) (match-beginning 0) 1)
  4575. 1))
  4576. (error 1))))
  4577. (new-level (or force-level (max previous-level next-level)))
  4578. (shift (if (or (= old-level -1)
  4579. (= new-level -1)
  4580. (= old-level new-level))
  4581. 0
  4582. (- new-level old-level)))
  4583. (delta (if (> shift 0) -1 1))
  4584. (func (if (> shift 0) 'org-demote 'org-promote))
  4585. (org-odd-levels-only nil)
  4586. beg end)
  4587. ;; Remove the forced level indicator
  4588. (if force-level
  4589. (delete-region (point-at-bol) (point)))
  4590. ;; Paste
  4591. (beginning-of-line 1)
  4592. (org-back-over-empty-lines)
  4593. (setq beg (point))
  4594. (insert-before-markers txt)
  4595. (unless (string-match "\n\\'" txt) (insert "\n"))
  4596. (setq end (point))
  4597. (goto-char beg)
  4598. (skip-chars-forward " \t\n\r")
  4599. (setq beg (point))
  4600. ;; Shift if necessary
  4601. (unless (= shift 0)
  4602. (save-restriction
  4603. (narrow-to-region beg end)
  4604. (while (not (= shift 0))
  4605. (org-map-region func (point-min) (point-max))
  4606. (setq shift (+ delta shift)))
  4607. (goto-char (point-min))))
  4608. (when (interactive-p)
  4609. (message "Clipboard pasted as level %d subtree" new-level))
  4610. (if (and kill-ring
  4611. (eq org-subtree-clip (current-kill 0))
  4612. org-subtree-clip-folded)
  4613. ;; The tree was folded before it was killed/copied
  4614. (hide-subtree))))
  4615. (defun org-kill-is-subtree-p (&optional txt)
  4616. "Check if the current kill is an outline subtree, or a set of trees.
  4617. Returns nil if kill does not start with a headline, or if the first
  4618. headline level is not the largest headline level in the tree.
  4619. So this will actually accept several entries of equal levels as well,
  4620. which is OK for `org-paste-subtree'.
  4621. If optional TXT is given, check this string instead of the current kill."
  4622. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4623. (start-level (and kill
  4624. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4625. org-outline-regexp "\\)")
  4626. kill)
  4627. (- (match-end 2) (match-beginning 2) 1)))
  4628. (re (concat "^" org-outline-regexp))
  4629. (start (1+ (match-beginning 2))))
  4630. (if (not start-level)
  4631. (progn
  4632. nil) ;; does not even start with a heading
  4633. (catch 'exit
  4634. (while (setq start (string-match re kill (1+ start)))
  4635. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4636. (throw 'exit nil)))
  4637. t))))
  4638. (defun org-narrow-to-subtree ()
  4639. "Narrow buffer to the current subtree."
  4640. (interactive)
  4641. (save-excursion
  4642. (save-match-data
  4643. (narrow-to-region
  4644. (progn (org-back-to-heading) (point))
  4645. (progn (org-end-of-subtree t t) (point))))))
  4646. ;;; Outline Sorting
  4647. (defun org-sort (with-case)
  4648. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4649. Optional argument WITH-CASE means sort case-sensitively."
  4650. (interactive "P")
  4651. (if (org-at-table-p)
  4652. (org-call-with-arg 'org-table-sort-lines with-case)
  4653. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4654. (defun org-sort-remove-invisible (s)
  4655. (remove-text-properties 0 (length s) org-rm-props s)
  4656. (while (string-match org-bracket-link-regexp s)
  4657. (setq s (replace-match (if (match-end 2)
  4658. (match-string 3 s)
  4659. (match-string 1 s)) t t s)))
  4660. s)
  4661. (defvar org-priority-regexp) ; defined later in the file
  4662. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4663. "Sort entries on a certain level of an outline tree.
  4664. If there is an active region, the entries in the region are sorted.
  4665. Else, if the cursor is before the first entry, sort the top-level items.
  4666. Else, the children of the entry at point are sorted.
  4667. Sorting can be alphabetically, numerically, and by date/time as given by
  4668. the first time stamp in the entry. The command prompts for the sorting
  4669. type unless it has been given to the function through the SORTING-TYPE
  4670. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4671. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4672. called with point at the beginning of the record. It must return either
  4673. a string or a number that should serve as the sorting key for that record.
  4674. Comparing entries ignores case by default. However, with an optional argument
  4675. WITH-CASE, the sorting considers case as well."
  4676. (interactive "P")
  4677. (let ((case-func (if with-case 'identity 'downcase))
  4678. start beg end stars re re2
  4679. txt what tmp plain-list-p)
  4680. ;; Find beginning and end of region to sort
  4681. (cond
  4682. ((org-region-active-p)
  4683. ;; we will sort the region
  4684. (setq end (region-end)
  4685. what "region")
  4686. (goto-char (region-beginning))
  4687. (if (not (org-on-heading-p)) (outline-next-heading))
  4688. (setq start (point)))
  4689. ((org-at-item-p)
  4690. ;; we will sort this plain list
  4691. (org-beginning-of-item-list) (setq start (point))
  4692. (org-end-of-item-list) (setq end (point))
  4693. (goto-char start)
  4694. (setq plain-list-p t
  4695. what "plain list"))
  4696. ((or (org-on-heading-p)
  4697. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4698. ;; we will sort the children of the current headline
  4699. (org-back-to-heading)
  4700. (setq start (point)
  4701. end (progn (org-end-of-subtree t t)
  4702. (org-back-over-empty-lines)
  4703. (point))
  4704. what "children")
  4705. (goto-char start)
  4706. (show-subtree)
  4707. (outline-next-heading))
  4708. (t
  4709. ;; we will sort the top-level entries in this file
  4710. (goto-char (point-min))
  4711. (or (org-on-heading-p) (outline-next-heading))
  4712. (setq start (point) end (point-max) what "top-level")
  4713. (goto-char start)
  4714. (show-all)))
  4715. (setq beg (point))
  4716. (if (>= beg end) (error "Nothing to sort"))
  4717. (unless plain-list-p
  4718. (looking-at "\\(\\*+\\)")
  4719. (setq stars (match-string 1)
  4720. re (concat "^" (regexp-quote stars) " +")
  4721. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4722. txt (buffer-substring beg end))
  4723. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4724. (if (and (not (equal stars "*")) (string-match re2 txt))
  4725. (error "Region to sort contains a level above the first entry")))
  4726. (unless sorting-type
  4727. (message
  4728. (if plain-list-p
  4729. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4730. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
  4731. what)
  4732. (setq sorting-type (read-char-exclusive))
  4733. (and (= (downcase sorting-type) ?f)
  4734. (setq getkey-func
  4735. (completing-read "Sort using function: "
  4736. obarray 'fboundp t nil nil))
  4737. (setq getkey-func (intern getkey-func)))
  4738. (and (= (downcase sorting-type) ?r)
  4739. (setq property
  4740. (completing-read "Property: "
  4741. (mapcar 'list (org-buffer-property-keys t))
  4742. nil t))))
  4743. (message "Sorting entries...")
  4744. (save-restriction
  4745. (narrow-to-region start end)
  4746. (let ((dcst (downcase sorting-type))
  4747. (now (current-time)))
  4748. (sort-subr
  4749. (/= dcst sorting-type)
  4750. ;; This function moves to the beginning character of the "record" to
  4751. ;; be sorted.
  4752. (if plain-list-p
  4753. (lambda nil
  4754. (if (org-at-item-p) t (goto-char (point-max))))
  4755. (lambda nil
  4756. (if (re-search-forward re nil t)
  4757. (goto-char (match-beginning 0))
  4758. (goto-char (point-max)))))
  4759. ;; This function moves to the last character of the "record" being
  4760. ;; sorted.
  4761. (if plain-list-p
  4762. 'org-end-of-item
  4763. (lambda nil
  4764. (save-match-data
  4765. (condition-case nil
  4766. (outline-forward-same-level 1)
  4767. (error
  4768. (goto-char (point-max)))))))
  4769. ;; This function returns the value that gets sorted against.
  4770. (if plain-list-p
  4771. (lambda nil
  4772. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4773. (cond
  4774. ((= dcst ?n)
  4775. (string-to-number (buffer-substring (match-end 0)
  4776. (point-at-eol))))
  4777. ((= dcst ?a)
  4778. (buffer-substring (match-end 0) (point-at-eol)))
  4779. ((= dcst ?t)
  4780. (if (re-search-forward org-ts-regexp
  4781. (point-at-eol) t)
  4782. (org-time-string-to-time (match-string 0))
  4783. now))
  4784. ((= dcst ?f)
  4785. (if getkey-func
  4786. (progn
  4787. (setq tmp (funcall getkey-func))
  4788. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4789. tmp)
  4790. (error "Invalid key function `%s'" getkey-func)))
  4791. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4792. (lambda nil
  4793. (cond
  4794. ((= dcst ?n)
  4795. (if (looking-at outline-regexp)
  4796. (string-to-number (buffer-substring (match-end 0)
  4797. (point-at-eol)))
  4798. nil))
  4799. ((= dcst ?a)
  4800. (funcall case-func (buffer-substring (point-at-bol)
  4801. (point-at-eol))))
  4802. ((= dcst ?t)
  4803. (if (re-search-forward org-ts-regexp
  4804. (save-excursion
  4805. (forward-line 2)
  4806. (point)) t)
  4807. (org-time-string-to-time (match-string 0))
  4808. now))
  4809. ((= dcst ?p)
  4810. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4811. (string-to-char (match-string 2))
  4812. org-default-priority))
  4813. ((= dcst ?r)
  4814. (or (org-entry-get nil property) ""))
  4815. ((= dcst ?f)
  4816. (if getkey-func
  4817. (progn
  4818. (setq tmp (funcall getkey-func))
  4819. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4820. tmp)
  4821. (error "Invalid key function `%s'" getkey-func)))
  4822. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4823. nil
  4824. (cond
  4825. ((= dcst ?a) 'string<)
  4826. ((= dcst ?t) 'time-less-p)
  4827. (t nil)))))
  4828. (message "Sorting entries...done")))
  4829. (defun org-do-sort (table what &optional with-case sorting-type)
  4830. "Sort TABLE of WHAT according to SORTING-TYPE.
  4831. The user will be prompted for the SORTING-TYPE if the call to this
  4832. function does not specify it. WHAT is only for the prompt, to indicate
  4833. what is being sorted. The sorting key will be extracted from
  4834. the car of the elements of the table.
  4835. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4836. (unless sorting-type
  4837. (message
  4838. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4839. what)
  4840. (setq sorting-type (read-char-exclusive)))
  4841. (let ((dcst (downcase sorting-type))
  4842. extractfun comparefun)
  4843. ;; Define the appropriate functions
  4844. (cond
  4845. ((= dcst ?n)
  4846. (setq extractfun 'string-to-number
  4847. comparefun (if (= dcst sorting-type) '< '>)))
  4848. ((= dcst ?a)
  4849. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4850. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4851. comparefun (if (= dcst sorting-type)
  4852. 'string<
  4853. (lambda (a b) (and (not (string< a b))
  4854. (not (string= a b)))))))
  4855. ((= dcst ?t)
  4856. (setq extractfun
  4857. (lambda (x)
  4858. (if (string-match org-ts-regexp x)
  4859. (time-to-seconds
  4860. (org-time-string-to-time (match-string 0 x)))
  4861. 0))
  4862. comparefun (if (= dcst sorting-type) '< '>)))
  4863. (t (error "Invalid sorting type `%c'" sorting-type)))
  4864. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4865. table)
  4866. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4867. ;;;; Plain list items, including checkboxes
  4868. ;;; Plain list items
  4869. (defun org-at-item-p ()
  4870. "Is point in a line starting a hand-formatted item?"
  4871. (let ((llt org-plain-list-ordered-item-terminator))
  4872. (save-excursion
  4873. (goto-char (point-at-bol))
  4874. (looking-at
  4875. (cond
  4876. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4877. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4878. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4879. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4880. (defun org-in-item-p ()
  4881. "It the cursor inside a plain list item.
  4882. Does not have to be the first line."
  4883. (save-excursion
  4884. (condition-case nil
  4885. (progn
  4886. (org-beginning-of-item)
  4887. (org-at-item-p)
  4888. t)
  4889. (error nil))))
  4890. (defun org-insert-item (&optional checkbox)
  4891. "Insert a new item at the current level.
  4892. Return t when things worked, nil when we are not in an item."
  4893. (when (save-excursion
  4894. (condition-case nil
  4895. (progn
  4896. (org-beginning-of-item)
  4897. (org-at-item-p)
  4898. (if (org-invisible-p) (error "Invisible item"))
  4899. t)
  4900. (error nil)))
  4901. (let* ((bul (match-string 0))
  4902. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4903. (match-end 0)))
  4904. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4905. pos)
  4906. (cond
  4907. ((and (org-at-item-p) (<= (point) eow))
  4908. ;; before the bullet
  4909. (beginning-of-line 1)
  4910. (open-line (if blank 2 1)))
  4911. ((<= (point) eow)
  4912. (beginning-of-line 1))
  4913. (t
  4914. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4915. (end-of-line 1)
  4916. (delete-horizontal-space))
  4917. (newline (if blank 2 1))))
  4918. (insert bul (if checkbox "[ ]" ""))
  4919. (just-one-space)
  4920. (setq pos (point))
  4921. (end-of-line 1)
  4922. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4923. (org-maybe-renumber-ordered-list)
  4924. (and checkbox (org-update-checkbox-count-maybe))
  4925. t))
  4926. ;;; Checkboxes
  4927. (defun org-at-item-checkbox-p ()
  4928. "Is point at a line starting a plain-list item with a checklet?"
  4929. (and (org-at-item-p)
  4930. (save-excursion
  4931. (goto-char (match-end 0))
  4932. (skip-chars-forward " \t")
  4933. (looking-at "\\[[- X]\\]"))))
  4934. (defun org-toggle-checkbox (&optional arg)
  4935. "Toggle the checkbox in the current line."
  4936. (interactive "P")
  4937. (catch 'exit
  4938. (let (beg end status (firstnew 'unknown))
  4939. (cond
  4940. ((org-region-active-p)
  4941. (setq beg (region-beginning) end (region-end)))
  4942. ((org-on-heading-p)
  4943. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4944. ((org-at-item-checkbox-p)
  4945. (let ((pos (point)))
  4946. (replace-match
  4947. (cond (arg "[-]")
  4948. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4949. (t "[ ]"))
  4950. t t)
  4951. (goto-char pos))
  4952. (throw 'exit t))
  4953. (t (error "Not at a checkbox or heading, and no active region")))
  4954. (save-excursion
  4955. (goto-char beg)
  4956. (while (< (point) end)
  4957. (when (org-at-item-checkbox-p)
  4958. (setq status (equal (match-string 0) "[X]"))
  4959. (when (eq firstnew 'unknown)
  4960. (setq firstnew (not status)))
  4961. (replace-match
  4962. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4963. (beginning-of-line 2)))))
  4964. (org-update-checkbox-count-maybe))
  4965. (defun org-update-checkbox-count-maybe ()
  4966. "Update checkbox statistics unless turned off by user."
  4967. (when org-provide-checkbox-statistics
  4968. (org-update-checkbox-count)))
  4969. (defun org-update-checkbox-count (&optional all)
  4970. "Update the checkbox statistics in the current section.
  4971. This will find all statistic cookies like [57%] and [6/12] and update them
  4972. with the current numbers. With optional prefix argument ALL, do this for
  4973. the whole buffer."
  4974. (interactive "P")
  4975. (save-excursion
  4976. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4977. (beg (condition-case nil
  4978. (progn (outline-back-to-heading) (point))
  4979. (error (point-min))))
  4980. (end (move-marker (make-marker)
  4981. (progn (outline-next-heading) (point))))
  4982. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4983. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4984. (re-find (concat re "\\|" re-box))
  4985. beg-cookie end-cookie is-percent c-on c-off lim
  4986. eline curr-ind next-ind continue-from startsearch
  4987. (cstat 0)
  4988. )
  4989. (when all
  4990. (goto-char (point-min))
  4991. (outline-next-heading)
  4992. (setq beg (point) end (point-max)))
  4993. (goto-char end)
  4994. ;; find each statistic cookie
  4995. (while (re-search-backward re-find beg t)
  4996. (setq beg-cookie (match-beginning 1)
  4997. end-cookie (match-end 1)
  4998. cstat (+ cstat (if end-cookie 1 0))
  4999. startsearch (point-at-eol)
  5000. continue-from (point-at-bol)
  5001. is-percent (match-beginning 2)
  5002. lim (cond
  5003. ((org-on-heading-p) (outline-next-heading) (point))
  5004. ((org-at-item-p) (org-end-of-item) (point))
  5005. (t nil))
  5006. c-on 0
  5007. c-off 0)
  5008. (when lim
  5009. ;; find first checkbox for this cookie and gather
  5010. ;; statistics from all that are at this indentation level
  5011. (goto-char startsearch)
  5012. (if (re-search-forward re-box lim t)
  5013. (progn
  5014. (org-beginning-of-item)
  5015. (setq curr-ind (org-get-indentation))
  5016. (setq next-ind curr-ind)
  5017. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5018. (save-excursion (end-of-line) (setq eline (point)))
  5019. (if (re-search-forward re-box eline t)
  5020. (if (member (match-string 2) '("[ ]" "[-]"))
  5021. (setq c-off (1+ c-off))
  5022. (setq c-on (1+ c-on))
  5023. )
  5024. )
  5025. (org-end-of-item)
  5026. (setq next-ind (org-get-indentation))
  5027. )))
  5028. (goto-char continue-from)
  5029. ;; update cookie
  5030. (when end-cookie
  5031. (delete-region beg-cookie end-cookie)
  5032. (goto-char beg-cookie)
  5033. (insert
  5034. (if is-percent
  5035. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5036. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5037. ;; update items checkbox if it has one
  5038. (when (org-at-item-p)
  5039. (org-beginning-of-item)
  5040. (when (and (> (+ c-on c-off) 0)
  5041. (re-search-forward re-box (point-at-eol) t))
  5042. (setq beg-cookie (match-beginning 2)
  5043. end-cookie (match-end 2))
  5044. (delete-region beg-cookie end-cookie)
  5045. (goto-char beg-cookie)
  5046. (cond ((= c-off 0) (insert "[X]"))
  5047. ((= c-on 0) (insert "[ ]"))
  5048. (t (insert "[-]")))
  5049. )))
  5050. (goto-char continue-from))
  5051. (when (interactive-p)
  5052. (message "Checkbox satistics updated %s (%d places)"
  5053. (if all "in entire file" "in current outline entry") cstat)))))
  5054. (defun org-get-checkbox-statistics-face ()
  5055. "Select the face for checkbox statistics.
  5056. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5057. it will be `org-todo'."
  5058. (if (match-end 1)
  5059. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5060. (if (and (> (match-end 2) (match-beginning 2))
  5061. (equal (match-string 2) (match-string 3)))
  5062. 'org-done
  5063. 'org-todo)))
  5064. (defun org-get-indentation (&optional line)
  5065. "Get the indentation of the current line, interpreting tabs.
  5066. When LINE is given, assume it represents a line and compute its indentation."
  5067. (if line
  5068. (if (string-match "^ *" (org-remove-tabs line))
  5069. (match-end 0))
  5070. (save-excursion
  5071. (beginning-of-line 1)
  5072. (skip-chars-forward " \t")
  5073. (current-column))))
  5074. (defun org-remove-tabs (s &optional width)
  5075. "Replace tabulators in S with spaces.
  5076. Assumes that s is a single line, starting in column 0."
  5077. (setq width (or width tab-width))
  5078. (while (string-match "\t" s)
  5079. (setq s (replace-match
  5080. (make-string
  5081. (- (* width (/ (+ (match-beginning 0) width) width))
  5082. (match-beginning 0)) ?\ )
  5083. t t s)))
  5084. s)
  5085. (defun org-fix-indentation (line ind)
  5086. "Fix indentation in LINE.
  5087. IND is a cons cell with target and minimum indentation.
  5088. If the current indenation in LINE is smaller than the minimum,
  5089. leave it alone. If it is larger than ind, set it to the target."
  5090. (let* ((l (org-remove-tabs line))
  5091. (i (org-get-indentation l))
  5092. (i1 (car ind)) (i2 (cdr ind)))
  5093. (if (>= i i2) (setq l (substring line i2)))
  5094. (if (> i1 0)
  5095. (concat (make-string i1 ?\ ) l)
  5096. l)))
  5097. (defcustom org-empty-line-terminates-plain-lists nil
  5098. "Non-nil means, an empty line ends all plain list levels.
  5099. When nil, empty lines are part of the preceeding item."
  5100. :group 'org-plain-lists
  5101. :type 'boolean)
  5102. (defun org-beginning-of-item ()
  5103. "Go to the beginning of the current hand-formatted item.
  5104. If the cursor is not in an item, throw an error."
  5105. (interactive)
  5106. (let ((pos (point))
  5107. (limit (save-excursion
  5108. (condition-case nil
  5109. (progn
  5110. (org-back-to-heading)
  5111. (beginning-of-line 2) (point))
  5112. (error (point-min)))))
  5113. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5114. ind ind1)
  5115. (if (org-at-item-p)
  5116. (beginning-of-line 1)
  5117. (beginning-of-line 1)
  5118. (skip-chars-forward " \t")
  5119. (setq ind (current-column))
  5120. (if (catch 'exit
  5121. (while t
  5122. (beginning-of-line 0)
  5123. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5124. (if (looking-at "[ \t]*$")
  5125. (setq ind1 ind-empty)
  5126. (skip-chars-forward " \t")
  5127. (setq ind1 (current-column)))
  5128. (if (< ind1 ind)
  5129. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5130. nil
  5131. (goto-char pos)
  5132. (error "Not in an item")))))
  5133. (defun org-end-of-item ()
  5134. "Go to the end of the current hand-formatted item.
  5135. If the cursor is not in an item, throw an error."
  5136. (interactive)
  5137. (let* ((pos (point))
  5138. ind1
  5139. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5140. (limit (save-excursion (outline-next-heading) (point)))
  5141. (ind (save-excursion
  5142. (org-beginning-of-item)
  5143. (skip-chars-forward " \t")
  5144. (current-column)))
  5145. (end (catch 'exit
  5146. (while t
  5147. (beginning-of-line 2)
  5148. (if (eobp) (throw 'exit (point)))
  5149. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5150. (if (looking-at "[ \t]*$")
  5151. (setq ind1 ind-empty)
  5152. (skip-chars-forward " \t")
  5153. (setq ind1 (current-column)))
  5154. (if (<= ind1 ind)
  5155. (throw 'exit (point-at-bol)))))))
  5156. (if end
  5157. (goto-char end)
  5158. (goto-char pos)
  5159. (error "Not in an item"))))
  5160. (defun org-next-item ()
  5161. "Move to the beginning of the next item in the current plain list.
  5162. Error if not at a plain list, or if this is the last item in the list."
  5163. (interactive)
  5164. (let (ind ind1 (pos (point)))
  5165. (org-beginning-of-item)
  5166. (setq ind (org-get-indentation))
  5167. (org-end-of-item)
  5168. (setq ind1 (org-get-indentation))
  5169. (unless (and (org-at-item-p) (= ind ind1))
  5170. (goto-char pos)
  5171. (error "On last item"))))
  5172. (defun org-previous-item ()
  5173. "Move to the beginning of the previous item in the current plain list.
  5174. Error if not at a plain list, or if this is the first item in the list."
  5175. (interactive)
  5176. (let (beg ind ind1 (pos (point)))
  5177. (org-beginning-of-item)
  5178. (setq beg (point))
  5179. (setq ind (org-get-indentation))
  5180. (goto-char beg)
  5181. (catch 'exit
  5182. (while t
  5183. (beginning-of-line 0)
  5184. (if (looking-at "[ \t]*$")
  5185. nil
  5186. (if (<= (setq ind1 (org-get-indentation)) ind)
  5187. (throw 'exit t)))))
  5188. (condition-case nil
  5189. (if (or (not (org-at-item-p))
  5190. (< ind1 (1- ind)))
  5191. (error "")
  5192. (org-beginning-of-item))
  5193. (error (goto-char pos)
  5194. (error "On first item")))))
  5195. (defun org-first-list-item-p ()
  5196. "Is this heading the item in a plain list?"
  5197. (unless (org-at-item-p)
  5198. (error "Not at a plain list item"))
  5199. (org-beginning-of-item)
  5200. (= (point) (save-excursion (org-beginning-of-item-list))))
  5201. (defun org-move-item-down ()
  5202. "Move the plain list item at point down, i.e. swap with following item.
  5203. Subitems (items with larger indentation) are considered part of the item,
  5204. so this really moves item trees."
  5205. (interactive)
  5206. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5207. (org-beginning-of-item)
  5208. (setq beg0 (point))
  5209. (save-excursion
  5210. (setq ne-beg (org-back-over-empty-lines))
  5211. (setq beg (point)))
  5212. (goto-char beg0)
  5213. (setq ind (org-get-indentation))
  5214. (org-end-of-item)
  5215. (setq end0 (point))
  5216. (setq ind1 (org-get-indentation))
  5217. (setq ne-end (org-back-over-empty-lines))
  5218. (setq end (point))
  5219. (goto-char beg0)
  5220. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5221. ;; include less whitespace
  5222. (save-excursion
  5223. (goto-char beg)
  5224. (forward-line (- ne-beg ne-end))
  5225. (setq beg (point))))
  5226. (goto-char end0)
  5227. (if (and (org-at-item-p) (= ind ind1))
  5228. (progn
  5229. (org-end-of-item)
  5230. (org-back-over-empty-lines)
  5231. (setq txt (buffer-substring beg end))
  5232. (save-excursion
  5233. (delete-region beg end))
  5234. (setq pos (point))
  5235. (insert txt)
  5236. (goto-char pos) (org-skip-whitespace)
  5237. (org-maybe-renumber-ordered-list))
  5238. (goto-char pos)
  5239. (error "Cannot move this item further down"))))
  5240. (defun org-move-item-up (arg)
  5241. "Move the plain list item at point up, i.e. swap with previous item.
  5242. Subitems (items with larger indentation) are considered part of the item,
  5243. so this really moves item trees."
  5244. (interactive "p")
  5245. (let (beg beg0 end ind ind1 (pos (point)) txt
  5246. ne-beg ne-ins ins-end)
  5247. (org-beginning-of-item)
  5248. (setq beg0 (point))
  5249. (setq ind (org-get-indentation))
  5250. (save-excursion
  5251. (setq ne-beg (org-back-over-empty-lines))
  5252. (setq beg (point)))
  5253. (goto-char beg0)
  5254. (org-end-of-item)
  5255. (setq end (point))
  5256. (goto-char beg0)
  5257. (catch 'exit
  5258. (while t
  5259. (beginning-of-line 0)
  5260. (if (looking-at "[ \t]*$")
  5261. (if org-empty-line-terminates-plain-lists
  5262. (progn
  5263. (goto-char pos)
  5264. (error "Cannot move this item further up"))
  5265. nil)
  5266. (if (<= (setq ind1 (org-get-indentation)) ind)
  5267. (throw 'exit t)))))
  5268. (condition-case nil
  5269. (org-beginning-of-item)
  5270. (error (goto-char beg)
  5271. (error "Cannot move this item further up")))
  5272. (setq ind1 (org-get-indentation))
  5273. (if (and (org-at-item-p) (= ind ind1))
  5274. (progn
  5275. (setq ne-ins (org-back-over-empty-lines))
  5276. (setq txt (buffer-substring beg end))
  5277. (save-excursion
  5278. (delete-region beg end))
  5279. (setq pos (point))
  5280. (insert txt)
  5281. (setq ins-end (point))
  5282. (goto-char pos) (org-skip-whitespace)
  5283. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5284. ;; Move whitespace back to beginning
  5285. (save-excursion
  5286. (goto-char ins-end)
  5287. (let ((kill-whole-line t))
  5288. (kill-line (- ne-ins ne-beg)) (point)))
  5289. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5290. (org-maybe-renumber-ordered-list))
  5291. (goto-char pos)
  5292. (error "Cannot move this item further up"))))
  5293. (defun org-maybe-renumber-ordered-list ()
  5294. "Renumber the ordered list at point if setup allows it.
  5295. This tests the user option `org-auto-renumber-ordered-lists' before
  5296. doing the renumbering."
  5297. (interactive)
  5298. (when (and org-auto-renumber-ordered-lists
  5299. (org-at-item-p))
  5300. (if (match-beginning 3)
  5301. (org-renumber-ordered-list 1)
  5302. (org-fix-bullet-type))))
  5303. (defun org-maybe-renumber-ordered-list-safe ()
  5304. (condition-case nil
  5305. (save-excursion
  5306. (org-maybe-renumber-ordered-list))
  5307. (error nil)))
  5308. (defun org-cycle-list-bullet (&optional which)
  5309. "Cycle through the different itemize/enumerate bullets.
  5310. This cycle the entire list level through the sequence:
  5311. `-' -> `+' -> `*' -> `1.' -> `1)'
  5312. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5313. 0 meand `-', 1 means `+' etc."
  5314. (interactive "P")
  5315. (org-preserve-lc
  5316. (org-beginning-of-item-list)
  5317. (org-at-item-p)
  5318. (beginning-of-line 1)
  5319. (let ((current (match-string 0))
  5320. (prevp (eq which 'previous))
  5321. new)
  5322. (setq new (cond
  5323. ((and (numberp which)
  5324. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5325. ((string-match "-" current) (if prevp "1)" "+"))
  5326. ((string-match "\\+" current)
  5327. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5328. ((string-match "\\*" current) (if prevp "+" "1."))
  5329. ((string-match "\\." current) (if prevp "*" "1)"))
  5330. ((string-match ")" current) (if prevp "1." "-"))
  5331. (t (error "This should not happen"))))
  5332. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5333. (org-fix-bullet-type)
  5334. (org-maybe-renumber-ordered-list))))
  5335. (defun org-get-string-indentation (s)
  5336. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5337. (let ((n -1) (i 0) (w tab-width) c)
  5338. (catch 'exit
  5339. (while (< (setq n (1+ n)) (length s))
  5340. (setq c (aref s n))
  5341. (cond ((= c ?\ ) (setq i (1+ i)))
  5342. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5343. (t (throw 'exit t)))))
  5344. i))
  5345. (defun org-renumber-ordered-list (arg)
  5346. "Renumber an ordered plain list.
  5347. Cursor needs to be in the first line of an item, the line that starts
  5348. with something like \"1.\" or \"2)\"."
  5349. (interactive "p")
  5350. (unless (and (org-at-item-p)
  5351. (match-beginning 3))
  5352. (error "This is not an ordered list"))
  5353. (let ((line (org-current-line))
  5354. (col (current-column))
  5355. (ind (org-get-string-indentation
  5356. (buffer-substring (point-at-bol) (match-beginning 3))))
  5357. ;; (term (substring (match-string 3) -1))
  5358. ind1 (n (1- arg))
  5359. fmt)
  5360. ;; find where this list begins
  5361. (org-beginning-of-item-list)
  5362. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5363. (setq fmt (concat "%d" (match-string 1)))
  5364. (beginning-of-line 0)
  5365. ;; walk forward and replace these numbers
  5366. (catch 'exit
  5367. (while t
  5368. (catch 'next
  5369. (beginning-of-line 2)
  5370. (if (eobp) (throw 'exit nil))
  5371. (if (looking-at "[ \t]*$") (throw 'next nil))
  5372. (skip-chars-forward " \t") (setq ind1 (current-column))
  5373. (if (> ind1 ind) (throw 'next t))
  5374. (if (< ind1 ind) (throw 'exit t))
  5375. (if (not (org-at-item-p)) (throw 'exit nil))
  5376. (delete-region (match-beginning 2) (match-end 2))
  5377. (goto-char (match-beginning 2))
  5378. (insert (format fmt (setq n (1+ n)))))))
  5379. (goto-line line)
  5380. (move-to-column col)))
  5381. (defun org-fix-bullet-type ()
  5382. "Make sure all items in this list have the same bullet as the firsst item."
  5383. (interactive)
  5384. (unless (org-at-item-p) (error "This is not a list"))
  5385. (let ((line (org-current-line))
  5386. (col (current-column))
  5387. (ind (current-indentation))
  5388. ind1 bullet)
  5389. ;; find where this list begins
  5390. (org-beginning-of-item-list)
  5391. (beginning-of-line 1)
  5392. ;; find out what the bullet type is
  5393. (looking-at "[ \t]*\\(\\S-+\\)")
  5394. (setq bullet (match-string 1))
  5395. ;; walk forward and replace these numbers
  5396. (beginning-of-line 0)
  5397. (catch 'exit
  5398. (while t
  5399. (catch 'next
  5400. (beginning-of-line 2)
  5401. (if (eobp) (throw 'exit nil))
  5402. (if (looking-at "[ \t]*$") (throw 'next nil))
  5403. (skip-chars-forward " \t") (setq ind1 (current-column))
  5404. (if (> ind1 ind) (throw 'next t))
  5405. (if (< ind1 ind) (throw 'exit t))
  5406. (if (not (org-at-item-p)) (throw 'exit nil))
  5407. (skip-chars-forward " \t")
  5408. (looking-at "\\S-+")
  5409. (replace-match bullet))))
  5410. (goto-line line)
  5411. (move-to-column col)
  5412. (if (string-match "[0-9]" bullet)
  5413. (org-renumber-ordered-list 1))))
  5414. (defun org-beginning-of-item-list ()
  5415. "Go to the beginning of the current item list.
  5416. I.e. to the first item in this list."
  5417. (interactive)
  5418. (org-beginning-of-item)
  5419. (let ((pos (point-at-bol))
  5420. (ind (org-get-indentation))
  5421. ind1)
  5422. ;; find where this list begins
  5423. (catch 'exit
  5424. (while t
  5425. (catch 'next
  5426. (beginning-of-line 0)
  5427. (if (looking-at "[ \t]*$")
  5428. (throw (if (bobp) 'exit 'next) t))
  5429. (skip-chars-forward " \t") (setq ind1 (current-column))
  5430. (if (or (< ind1 ind)
  5431. (and (= ind1 ind)
  5432. (not (org-at-item-p)))
  5433. (bobp))
  5434. (throw 'exit t)
  5435. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5436. (goto-char pos)))
  5437. (defun org-end-of-item-list ()
  5438. "Go to the end of the current item list.
  5439. I.e. to the text after the last item."
  5440. (interactive)
  5441. (org-beginning-of-item)
  5442. (let ((pos (point-at-bol))
  5443. (ind (org-get-indentation))
  5444. ind1)
  5445. ;; find where this list begins
  5446. (catch 'exit
  5447. (while t
  5448. (catch 'next
  5449. (beginning-of-line 2)
  5450. (if (looking-at "[ \t]*$")
  5451. (throw (if (eobp) 'exit 'next) t))
  5452. (skip-chars-forward " \t") (setq ind1 (current-column))
  5453. (if (or (< ind1 ind)
  5454. (and (= ind1 ind)
  5455. (not (org-at-item-p)))
  5456. (eobp))
  5457. (progn
  5458. (setq pos (point-at-bol))
  5459. (throw 'exit t))))))
  5460. (goto-char pos)))
  5461. (defvar org-last-indent-begin-marker (make-marker))
  5462. (defvar org-last-indent-end-marker (make-marker))
  5463. (defun org-outdent-item (arg)
  5464. "Outdent a local list item."
  5465. (interactive "p")
  5466. (org-indent-item (- arg)))
  5467. (defun org-indent-item (arg)
  5468. "Indent a local list item."
  5469. (interactive "p")
  5470. (unless (org-at-item-p)
  5471. (error "Not on an item"))
  5472. (save-excursion
  5473. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5474. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5475. (setq beg org-last-indent-begin-marker
  5476. end org-last-indent-end-marker)
  5477. (org-beginning-of-item)
  5478. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5479. (org-end-of-item)
  5480. (setq end (move-marker org-last-indent-end-marker (point))))
  5481. (goto-char beg)
  5482. (setq tmp (org-item-indent-positions)
  5483. ind (car tmp)
  5484. ind-down (nth 2 tmp)
  5485. ind-up (nth 1 tmp)
  5486. delta (if (> arg 0)
  5487. (if ind-down (- ind-down ind) 2)
  5488. (if ind-up (- ind-up ind) -2)))
  5489. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5490. (while (< (point) end)
  5491. (beginning-of-line 1)
  5492. (skip-chars-forward " \t") (setq ind1 (current-column))
  5493. (delete-region (point-at-bol) (point))
  5494. (or (eolp) (indent-to-column (+ ind1 delta)))
  5495. (beginning-of-line 2))))
  5496. (org-fix-bullet-type)
  5497. (org-maybe-renumber-ordered-list-safe)
  5498. (save-excursion
  5499. (beginning-of-line 0)
  5500. (condition-case nil (org-beginning-of-item) (error nil))
  5501. (org-maybe-renumber-ordered-list-safe)))
  5502. (defun org-item-indent-positions ()
  5503. "Return indentation for plain list items.
  5504. This returns a list with three values: The current indentation, the
  5505. parent indentation and the indentation a child should habe.
  5506. Assumes cursor in item line."
  5507. (let* ((bolpos (point-at-bol))
  5508. (ind (org-get-indentation))
  5509. ind-down ind-up pos)
  5510. (save-excursion
  5511. (org-beginning-of-item-list)
  5512. (skip-chars-backward "\n\r \t")
  5513. (when (org-in-item-p)
  5514. (org-beginning-of-item)
  5515. (setq ind-up (org-get-indentation))))
  5516. (setq pos (point))
  5517. (save-excursion
  5518. (cond
  5519. ((and (condition-case nil (progn (org-previous-item) t)
  5520. (error nil))
  5521. (or (forward-char 1) t)
  5522. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5523. (setq ind-down (org-get-indentation)))
  5524. ((and (goto-char pos)
  5525. (org-at-item-p))
  5526. (goto-char (match-end 0))
  5527. (skip-chars-forward " \t")
  5528. (setq ind-down (current-column)))))
  5529. (list ind ind-up ind-down)))
  5530. ;;; The orgstruct minor mode
  5531. ;; Define a minor mode which can be used in other modes in order to
  5532. ;; integrate the org-mode structure editing commands.
  5533. ;; This is really a hack, because the org-mode structure commands use
  5534. ;; keys which normally belong to the major mode. Here is how it
  5535. ;; works: The minor mode defines all the keys necessary to operate the
  5536. ;; structure commands, but wraps the commands into a function which
  5537. ;; tests if the cursor is currently at a headline or a plain list
  5538. ;; item. If that is the case, the structure command is used,
  5539. ;; temporarily setting many Org-mode variables like regular
  5540. ;; expressions for filling etc. However, when any of those keys is
  5541. ;; used at a different location, function uses `key-binding' to look
  5542. ;; up if the key has an associated command in another currently active
  5543. ;; keymap (minor modes, major mode, global), and executes that
  5544. ;; command. There might be problems if any of the keys is otherwise
  5545. ;; used as a prefix key.
  5546. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5547. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5548. ;; addresses this by checking explicitly for both bindings.
  5549. (defvar orgstruct-mode-map (make-sparse-keymap)
  5550. "Keymap for the minor `orgstruct-mode'.")
  5551. (defvar org-local-vars nil
  5552. "List of local variables, for use by `orgstruct-mode'")
  5553. ;;;###autoload
  5554. (define-minor-mode orgstruct-mode
  5555. "Toggle the minor more `orgstruct-mode'.
  5556. This mode is for using Org-mode structure commands in other modes.
  5557. The following key behave as if Org-mode was active, if the cursor
  5558. is on a headline, or on a plain list item (both in the definition
  5559. of Org-mode).
  5560. M-up Move entry/item up
  5561. M-down Move entry/item down
  5562. M-left Promote
  5563. M-right Demote
  5564. M-S-up Move entry/item up
  5565. M-S-down Move entry/item down
  5566. M-S-left Promote subtree
  5567. M-S-right Demote subtree
  5568. M-q Fill paragraph and items like in Org-mode
  5569. C-c ^ Sort entries
  5570. C-c - Cycle list bullet
  5571. TAB Cycle item visibility
  5572. M-RET Insert new heading/item
  5573. S-M-RET Insert new TODO heading / Chekbox item
  5574. C-c C-c Set tags / toggle checkbox"
  5575. nil " OrgStruct" nil
  5576. (org-load-modules-maybe)
  5577. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5578. ;;;###autoload
  5579. (defun turn-on-orgstruct ()
  5580. "Unconditionally turn on `orgstruct-mode'."
  5581. (orgstruct-mode 1))
  5582. ;;;###autoload
  5583. (defun turn-on-orgstruct++ ()
  5584. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5585. In addition to setting orgstruct-mode, this also exports all indentation and
  5586. autofilling variables from org-mode into the buffer. Note that turning
  5587. off orgstruct-mode will *not* remove these additional settings."
  5588. (orgstruct-mode 1)
  5589. (let (var val)
  5590. (mapc
  5591. (lambda (x)
  5592. (when (string-match
  5593. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5594. (symbol-name (car x)))
  5595. (setq var (car x) val (nth 1 x))
  5596. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5597. org-local-vars)))
  5598. (defun orgstruct-error ()
  5599. "Error when there is no default binding for a structure key."
  5600. (interactive)
  5601. (error "This key has no function outside structure elements"))
  5602. (defun orgstruct-setup ()
  5603. "Setup orgstruct keymaps."
  5604. (let ((nfunc 0)
  5605. (bindings
  5606. (list
  5607. '([(meta up)] org-metaup)
  5608. '([(meta down)] org-metadown)
  5609. '([(meta left)] org-metaleft)
  5610. '([(meta right)] org-metaright)
  5611. '([(meta shift up)] org-shiftmetaup)
  5612. '([(meta shift down)] org-shiftmetadown)
  5613. '([(meta shift left)] org-shiftmetaleft)
  5614. '([(meta shift right)] org-shiftmetaright)
  5615. '([(shift up)] org-shiftup)
  5616. '([(shift down)] org-shiftdown)
  5617. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5618. '("\M-q" fill-paragraph)
  5619. '("\C-c^" org-sort)
  5620. '("\C-c-" org-cycle-list-bullet)))
  5621. elt key fun cmd)
  5622. (while (setq elt (pop bindings))
  5623. (setq nfunc (1+ nfunc))
  5624. (setq key (org-key (car elt))
  5625. fun (nth 1 elt)
  5626. cmd (orgstruct-make-binding fun nfunc key))
  5627. (org-defkey orgstruct-mode-map key cmd))
  5628. ;; Special treatment needed for TAB and RET
  5629. (org-defkey orgstruct-mode-map [(tab)]
  5630. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5631. (org-defkey orgstruct-mode-map "\C-i"
  5632. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5633. (org-defkey orgstruct-mode-map "\M-\C-m"
  5634. (orgstruct-make-binding 'org-insert-heading 105
  5635. "\M-\C-m" [(meta return)]))
  5636. (org-defkey orgstruct-mode-map [(meta return)]
  5637. (orgstruct-make-binding 'org-insert-heading 106
  5638. [(meta return)] "\M-\C-m"))
  5639. (org-defkey orgstruct-mode-map [(shift meta return)]
  5640. (orgstruct-make-binding 'org-insert-todo-heading 107
  5641. [(meta return)] "\M-\C-m"))
  5642. (unless org-local-vars
  5643. (setq org-local-vars (org-get-local-variables)))
  5644. t))
  5645. (defun orgstruct-make-binding (fun n &rest keys)
  5646. "Create a function for binding in the structure minor mode.
  5647. FUN is the command to call inside a table. N is used to create a unique
  5648. command name. KEYS are keys that should be checked in for a command
  5649. to execute outside of tables."
  5650. (eval
  5651. (list 'defun
  5652. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5653. '(arg)
  5654. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5655. "Outside of structure, run the binding of `"
  5656. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5657. "'.")
  5658. '(interactive "p")
  5659. (list 'if
  5660. '(org-context-p 'headline 'item)
  5661. (list 'org-run-like-in-org-mode (list 'quote fun))
  5662. (list 'let '(orgstruct-mode)
  5663. (list 'call-interactively
  5664. (append '(or)
  5665. (mapcar (lambda (k)
  5666. (list 'key-binding k))
  5667. keys)
  5668. '('orgstruct-error))))))))
  5669. (defun org-context-p (&rest contexts)
  5670. "Check if local context is and of CONTEXTS.
  5671. Possible values in the list of contexts are `table', `headline', and `item'."
  5672. (let ((pos (point)))
  5673. (goto-char (point-at-bol))
  5674. (prog1 (or (and (memq 'table contexts)
  5675. (looking-at "[ \t]*|"))
  5676. (and (memq 'headline contexts)
  5677. (looking-at "\\*+"))
  5678. (and (memq 'item contexts)
  5679. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5680. (goto-char pos))))
  5681. (defun org-get-local-variables ()
  5682. "Return a list of all local variables in an org-mode buffer."
  5683. (let (varlist)
  5684. (with-current-buffer (get-buffer-create "*Org tmp*")
  5685. (erase-buffer)
  5686. (org-mode)
  5687. (setq varlist (buffer-local-variables)))
  5688. (kill-buffer "*Org tmp*")
  5689. (delq nil
  5690. (mapcar
  5691. (lambda (x)
  5692. (setq x
  5693. (if (symbolp x)
  5694. (list x)
  5695. (list (car x) (list 'quote (cdr x)))))
  5696. (if (string-match
  5697. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5698. (symbol-name (car x)))
  5699. x nil))
  5700. varlist))))
  5701. ;;;###autoload
  5702. (defun org-run-like-in-org-mode (cmd)
  5703. (org-load-modules-maybe)
  5704. (unless org-local-vars
  5705. (setq org-local-vars (org-get-local-variables)))
  5706. (eval (list 'let org-local-vars
  5707. (list 'call-interactively (list 'quote cmd)))))
  5708. ;;;; Archiving
  5709. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  5710. (defun org-archive-subtree (&optional find-done)
  5711. "Move the current subtree to the archive.
  5712. The archive can be a certain top-level heading in the current file, or in
  5713. a different file. The tree will be moved to that location, the subtree
  5714. heading be marked DONE, and the current time will be added.
  5715. When called with prefix argument FIND-DONE, find whole trees without any
  5716. open TODO items and archive them (after getting confirmation from the user).
  5717. If the cursor is not at a headline when this comand is called, try all level
  5718. 1 trees. If the cursor is on a headline, only try the direct children of
  5719. this heading."
  5720. (interactive "P")
  5721. (if find-done
  5722. (org-archive-all-done)
  5723. ;; Save all relevant TODO keyword-relatex variables
  5724. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  5725. (tr-org-todo-keywords-1 org-todo-keywords-1)
  5726. (tr-org-todo-kwd-alist org-todo-kwd-alist)
  5727. (tr-org-done-keywords org-done-keywords)
  5728. (tr-org-todo-regexp org-todo-regexp)
  5729. (tr-org-todo-line-regexp org-todo-line-regexp)
  5730. (tr-org-odd-levels-only org-odd-levels-only)
  5731. (this-buffer (current-buffer))
  5732. (org-archive-location org-archive-location)
  5733. (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
  5734. ;; start of variables that will be used for saving context
  5735. ;; The compiler complains about them - keep them anyway!
  5736. (file (abbreviate-file-name (buffer-file-name)))
  5737. (olpath (mapconcat 'identity (org-get-outline-path) "/"))
  5738. (time (format-time-string
  5739. (substring (cdr org-time-stamp-formats) 1 -1)
  5740. (current-time)))
  5741. afile heading buffer level newfile-p
  5742. category todo priority
  5743. ;; start of variables that will be used for savind context
  5744. ltags itags prop)
  5745. ;; Try to find a local archive location
  5746. (save-excursion
  5747. (save-restriction
  5748. (widen)
  5749. (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
  5750. (if (and prop (string-match "\\S-" prop))
  5751. (setq org-archive-location prop)
  5752. (if (or (re-search-backward re nil t)
  5753. (re-search-forward re nil t))
  5754. (setq org-archive-location (match-string 1))))))
  5755. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  5756. (progn
  5757. (setq afile (format (match-string 1 org-archive-location)
  5758. (file-name-nondirectory buffer-file-name))
  5759. heading (match-string 2 org-archive-location)))
  5760. (error "Invalid `org-archive-location'"))
  5761. (if (> (length afile) 0)
  5762. (setq newfile-p (not (file-exists-p afile))
  5763. buffer (find-file-noselect afile))
  5764. (setq buffer (current-buffer)))
  5765. (unless buffer
  5766. (error "Cannot access file \"%s\"" afile))
  5767. (if (and (> (length heading) 0)
  5768. (string-match "^\\*+" heading))
  5769. (setq level (match-end 0))
  5770. (setq heading nil level 0))
  5771. (save-excursion
  5772. (org-back-to-heading t)
  5773. ;; Get context information that will be lost by moving the tree
  5774. (org-refresh-category-properties)
  5775. (setq category (org-get-category)
  5776. todo (and (looking-at org-todo-line-regexp)
  5777. (match-string 2))
  5778. priority (org-get-priority (if (match-end 3) (match-string 3) ""))
  5779. ltags (org-get-tags)
  5780. itags (org-delete-all ltags (org-get-tags-at)))
  5781. (setq ltags (mapconcat 'identity ltags " ")
  5782. itags (mapconcat 'identity itags " "))
  5783. ;; We first only copy, in case something goes wrong
  5784. ;; we need to protect this-command, to avoid kill-region sets it,
  5785. ;; which would lead to duplication of subtrees
  5786. (let (this-command) (org-copy-subtree))
  5787. (set-buffer buffer)
  5788. ;; Enforce org-mode for the archive buffer
  5789. (if (not (org-mode-p))
  5790. ;; Force the mode for future visits.
  5791. (let ((org-insert-mode-line-in-empty-file t)
  5792. (org-inhibit-startup t))
  5793. (call-interactively 'org-mode)))
  5794. (when newfile-p
  5795. (goto-char (point-max))
  5796. (insert (format "\nArchived entries from file %s\n\n"
  5797. (buffer-file-name this-buffer))))
  5798. ;; Force the TODO keywords of the original buffer
  5799. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  5800. (org-todo-keywords-1 tr-org-todo-keywords-1)
  5801. (org-todo-kwd-alist tr-org-todo-kwd-alist)
  5802. (org-done-keywords tr-org-done-keywords)
  5803. (org-todo-regexp tr-org-todo-regexp)
  5804. (org-todo-line-regexp tr-org-todo-line-regexp)
  5805. (org-odd-levels-only
  5806. (if (local-variable-p 'org-odd-levels-only (current-buffer))
  5807. org-odd-levels-only
  5808. tr-org-odd-levels-only)))
  5809. (goto-char (point-min))
  5810. (show-all)
  5811. (if heading
  5812. (progn
  5813. (if (re-search-forward
  5814. (concat "^" (regexp-quote heading)
  5815. (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
  5816. nil t)
  5817. (goto-char (match-end 0))
  5818. ;; Heading not found, just insert it at the end
  5819. (goto-char (point-max))
  5820. (or (bolp) (insert "\n"))
  5821. (insert "\n" heading "\n")
  5822. (end-of-line 0))
  5823. ;; Make the subtree visible
  5824. (show-subtree)
  5825. (org-end-of-subtree t)
  5826. (skip-chars-backward " \t\r\n")
  5827. (and (looking-at "[ \t\r\n]*")
  5828. (replace-match "\n\n")))
  5829. ;; No specific heading, just go to end of file.
  5830. (goto-char (point-max)) (insert "\n"))
  5831. ;; Paste
  5832. (org-paste-subtree (org-get-valid-level level 1))
  5833. ;; Mark the entry as done
  5834. (when (and org-archive-mark-done
  5835. (looking-at org-todo-line-regexp)
  5836. (or (not (match-end 2))
  5837. (not (member (match-string 2) org-done-keywords))))
  5838. (let (org-log-done org-todo-log-states)
  5839. (org-todo
  5840. (car (or (member org-archive-mark-done org-done-keywords)
  5841. org-done-keywords)))))
  5842. ;; Add the context info
  5843. (when org-archive-save-context-info
  5844. (let ((l org-archive-save-context-info) e n v)
  5845. (while (setq e (pop l))
  5846. (when (and (setq v (symbol-value e))
  5847. (stringp v) (string-match "\\S-" v))
  5848. (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
  5849. (org-entry-put (point) n v)))))
  5850. ;; Save and kill the buffer, if it is not the same buffer.
  5851. (if (not (eq this-buffer buffer))
  5852. (progn (save-buffer) (kill-buffer buffer)))))
  5853. ;; Here we are back in the original buffer. Everything seems to have
  5854. ;; worked. So now cut the tree and finish up.
  5855. (let (this-command) (org-cut-subtree))
  5856. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  5857. (message "Subtree archived %s"
  5858. (if (eq this-buffer buffer)
  5859. (concat "under heading: " heading)
  5860. (concat "in file: " (abbreviate-file-name afile)))))))
  5861. (defun org-get-category (&optional pos)
  5862. "Get the category applying to position POS."
  5863. (get-text-property (or pos (point)) 'org-category))
  5864. (defun org-refresh-category-properties ()
  5865. "Refresh category text properties in the buffer."
  5866. (let ((def-cat (cond
  5867. ((null org-category)
  5868. (if buffer-file-name
  5869. (file-name-sans-extension
  5870. (file-name-nondirectory buffer-file-name))
  5871. "???"))
  5872. ((symbolp org-category) (symbol-name org-category))
  5873. (t org-category)))
  5874. beg end cat pos optionp)
  5875. (org-unmodified
  5876. (save-excursion
  5877. (save-restriction
  5878. (widen)
  5879. (goto-char (point-min))
  5880. (put-text-property (point) (point-max) 'org-category def-cat)
  5881. (while (re-search-forward
  5882. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5883. (setq pos (match-end 0)
  5884. optionp (equal (char-after (match-beginning 0)) ?#)
  5885. cat (org-trim (match-string 2)))
  5886. (if optionp
  5887. (setq beg (point-at-bol) end (point-max))
  5888. (org-back-to-heading t)
  5889. (setq beg (point) end (org-end-of-subtree t t)))
  5890. (put-text-property beg end 'org-category cat)
  5891. (goto-char pos)))))))
  5892. (defun org-archive-all-done (&optional tag)
  5893. "Archive sublevels of the current tree without open TODO items.
  5894. If the cursor is not on a headline, try all level 1 trees. If
  5895. it is on a headline, try all direct children.
  5896. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  5897. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  5898. (rea (concat ".*:" org-archive-tag ":"))
  5899. (begm (make-marker))
  5900. (endm (make-marker))
  5901. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  5902. "Move subtree to archive (no open TODO items)? "))
  5903. beg end (cntarch 0))
  5904. (if (org-on-heading-p)
  5905. (progn
  5906. (setq re1 (concat "^" (regexp-quote
  5907. (make-string
  5908. (1+ (- (match-end 0) (match-beginning 0) 1))
  5909. ?*))
  5910. " "))
  5911. (move-marker begm (point))
  5912. (move-marker endm (org-end-of-subtree t)))
  5913. (setq re1 "^* ")
  5914. (move-marker begm (point-min))
  5915. (move-marker endm (point-max)))
  5916. (save-excursion
  5917. (goto-char begm)
  5918. (while (re-search-forward re1 endm t)
  5919. (setq beg (match-beginning 0)
  5920. end (save-excursion (org-end-of-subtree t) (point)))
  5921. (goto-char beg)
  5922. (if (re-search-forward re end t)
  5923. (goto-char end)
  5924. (goto-char beg)
  5925. (if (and (or (not tag) (not (looking-at rea)))
  5926. (y-or-n-p question))
  5927. (progn
  5928. (if tag
  5929. (org-toggle-tag org-archive-tag 'on)
  5930. (org-archive-subtree))
  5931. (setq cntarch (1+ cntarch)))
  5932. (goto-char end)))))
  5933. (message "%d trees archived" cntarch)))
  5934. (defun org-cycle-hide-drawers (state)
  5935. "Re-hide all drawers after a visibility state change."
  5936. (when (and (org-mode-p)
  5937. (not (memq state '(overview folded))))
  5938. (save-excursion
  5939. (let* ((globalp (memq state '(contents all)))
  5940. (beg (if globalp (point-min) (point)))
  5941. (end (if globalp (point-max) (org-end-of-subtree t))))
  5942. (goto-char beg)
  5943. (while (re-search-forward org-drawer-regexp end t)
  5944. (org-flag-drawer t))))))
  5945. (defun org-flag-drawer (flag)
  5946. (save-excursion
  5947. (beginning-of-line 1)
  5948. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5949. (let ((b (match-end 0))
  5950. (outline-regexp org-outline-regexp))
  5951. (if (re-search-forward
  5952. "^[ \t]*:END:"
  5953. (save-excursion (outline-next-heading) (point)) t)
  5954. (outline-flag-region b (point-at-eol) flag)
  5955. (error ":END: line missing"))))))
  5956. (defun org-cycle-hide-archived-subtrees (state)
  5957. "Re-hide all archived subtrees after a visibility state change."
  5958. (when (and (not org-cycle-open-archived-trees)
  5959. (not (memq state '(overview folded))))
  5960. (save-excursion
  5961. (let* ((globalp (memq state '(contents all)))
  5962. (beg (if globalp (point-min) (point)))
  5963. (end (if globalp (point-max) (org-end-of-subtree t))))
  5964. (org-hide-archived-subtrees beg end)
  5965. (goto-char beg)
  5966. (if (looking-at (concat ".*:" org-archive-tag ":"))
  5967. (message "%s" (substitute-command-keys
  5968. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  5969. (defun org-force-cycle-archived ()
  5970. "Cycle subtree even if it is archived."
  5971. (interactive)
  5972. (setq this-command 'org-cycle)
  5973. (let ((org-cycle-open-archived-trees t))
  5974. (call-interactively 'org-cycle)))
  5975. (defun org-hide-archived-subtrees (beg end)
  5976. "Re-hide all archived subtrees after a visibility state change."
  5977. (save-excursion
  5978. (let* ((re (concat ":" org-archive-tag ":")))
  5979. (goto-char beg)
  5980. (while (re-search-forward re end t)
  5981. (and (org-on-heading-p) (hide-subtree))
  5982. (org-end-of-subtree t)))))
  5983. (defun org-toggle-tag (tag &optional onoff)
  5984. "Toggle the tag TAG for the current line.
  5985. If ONOFF is `on' or `off', don't toggle but set to this state."
  5986. (unless (org-on-heading-p t) (error "Not on headling"))
  5987. (let (res current)
  5988. (save-excursion
  5989. (beginning-of-line)
  5990. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  5991. (point-at-eol) t)
  5992. (progn
  5993. (setq current (match-string 1))
  5994. (replace-match ""))
  5995. (setq current ""))
  5996. (setq current (nreverse (org-split-string current ":")))
  5997. (cond
  5998. ((eq onoff 'on)
  5999. (setq res t)
  6000. (or (member tag current) (push tag current)))
  6001. ((eq onoff 'off)
  6002. (or (not (member tag current)) (setq current (delete tag current))))
  6003. (t (if (member tag current)
  6004. (setq current (delete tag current))
  6005. (setq res t)
  6006. (push tag current))))
  6007. (end-of-line 1)
  6008. (if current
  6009. (progn
  6010. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  6011. (org-set-tags nil t))
  6012. (delete-horizontal-space))
  6013. (run-hooks 'org-after-tags-change-hook))
  6014. res))
  6015. (defun org-toggle-archive-tag (&optional arg)
  6016. "Toggle the archive tag for the current headline.
  6017. With prefix ARG, check all children of current headline and offer tagging
  6018. the children that do not contain any open TODO items."
  6019. (interactive "P")
  6020. (if arg
  6021. (org-archive-all-done 'tag)
  6022. (let (set)
  6023. (save-excursion
  6024. (org-back-to-heading t)
  6025. (setq set (org-toggle-tag org-archive-tag))
  6026. (when set (hide-subtree)))
  6027. (and set (beginning-of-line 1))
  6028. (message "Subtree %s" (if set "archived" "unarchived")))))
  6029. ;;;; Link Stuff
  6030. ;;; Link abbreviations
  6031. (defun org-link-expand-abbrev (link)
  6032. "Apply replacements as defined in `org-link-abbrev-alist."
  6033. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6034. (let* ((key (match-string 1 link))
  6035. (as (or (assoc key org-link-abbrev-alist-local)
  6036. (assoc key org-link-abbrev-alist)))
  6037. (tag (and (match-end 2) (match-string 3 link)))
  6038. rpl)
  6039. (if (not as)
  6040. link
  6041. (setq rpl (cdr as))
  6042. (cond
  6043. ((symbolp rpl) (funcall rpl tag))
  6044. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6045. (t (concat rpl tag)))))
  6046. link))
  6047. ;;; Storing and inserting links
  6048. (defvar org-insert-link-history nil
  6049. "Minibuffer history for links inserted with `org-insert-link'.")
  6050. (defvar org-stored-links nil
  6051. "Contains the links stored with `org-store-link'.")
  6052. (defvar org-store-link-plist nil
  6053. "Plist with info about the most recently link created with `org-store-link'.")
  6054. (defvar org-link-protocols nil
  6055. "Link protocols added to Org-mode using `org-add-link-type'.")
  6056. (defvar org-store-link-functions nil
  6057. "List of functions that are called to create and store a link.
  6058. Each function will be called in turn until one returns a non-nil
  6059. value. Each function should check if it is responsible for creating
  6060. this link (for example by looking at the major mode).
  6061. If not, it must exit and return nil.
  6062. If yes, it should return a non-nil value after a calling
  6063. `org-store-link-props' with a list of properties and values.
  6064. Special properties are:
  6065. :type The link prefix. like \"http\". This must be given.
  6066. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6067. This is obligatory as well.
  6068. :description Optional default description for the second pair
  6069. of brackets in an Org-mode link. The user can still change
  6070. this when inserting this link into an Org-mode buffer.
  6071. In addition to these, any additional properties can be specified
  6072. and then used in remember templates.")
  6073. (defun org-add-link-type (type &optional follow export)
  6074. "Add TYPE to the list of `org-link-types'.
  6075. Re-compute all regular expressions depending on `org-link-types'
  6076. FOLLOW and EXPORT are two functions.
  6077. FOLLOW should take the link path as the single argument and do whatever
  6078. is necessary to follow the link, for example find a file or display
  6079. a mail message.
  6080. EXPORT should format the link path for export to one of the export formats.
  6081. It should be a function accepting three arguments:
  6082. path the path of the link, the text after the prefix (like \"http:\")
  6083. desc the description of the link, if any, nil if there was no descripton
  6084. format the export format, a symbol like `html' or `latex'.
  6085. The function may use the FORMAT information to return different values
  6086. depending on the format. The return value will be put literally into
  6087. the exported file.
  6088. Org-mode has a built-in default for exporting links. If you are happy with
  6089. this default, there is no need to define an export function for the link
  6090. type. For a simple example of an export function, see `org-bbdb.el'."
  6091. (add-to-list 'org-link-types type t)
  6092. (org-make-link-regexps)
  6093. (if (assoc type org-link-protocols)
  6094. (setcdr (assoc type org-link-protocols) (list follow export))
  6095. (push (list type follow export) org-link-protocols)))
  6096. ;;;###autoload
  6097. (defun org-store-link (arg)
  6098. "\\<org-mode-map>Store an org-link to the current location.
  6099. This link is added to `org-stored-links' and can later be inserted
  6100. into an org-buffer with \\[org-insert-link].
  6101. For some link types, a prefix arg is interpreted:
  6102. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6103. For file links, arg negates `org-context-in-file-links'."
  6104. (interactive "P")
  6105. (org-load-modules-maybe)
  6106. (setq org-store-link-plist nil) ; reset
  6107. (let (link cpltxt desc description search txt)
  6108. (cond
  6109. ((run-hook-with-args-until-success 'org-store-link-functions)
  6110. (setq link (plist-get org-store-link-plist :link)
  6111. desc (or (plist-get org-store-link-plist :description) link)))
  6112. ((eq major-mode 'calendar-mode)
  6113. (let ((cd (calendar-cursor-to-date)))
  6114. (setq link
  6115. (format-time-string
  6116. (car org-time-stamp-formats)
  6117. (apply 'encode-time
  6118. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6119. nil nil nil))))
  6120. (org-store-link-props :type "calendar" :date cd)))
  6121. ((eq major-mode 'w3-mode)
  6122. (setq cpltxt (url-view-url t)
  6123. link (org-make-link cpltxt))
  6124. (org-store-link-props :type "w3" :url (url-view-url t)))
  6125. ((eq major-mode 'w3m-mode)
  6126. (setq cpltxt (or w3m-current-title w3m-current-url)
  6127. link (org-make-link w3m-current-url))
  6128. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6129. ((setq search (run-hook-with-args-until-success
  6130. 'org-create-file-search-functions))
  6131. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6132. "::" search))
  6133. (setq cpltxt (or description link)))
  6134. ((eq major-mode 'image-mode)
  6135. (setq cpltxt (concat "file:"
  6136. (abbreviate-file-name buffer-file-name))
  6137. link (org-make-link cpltxt))
  6138. (org-store-link-props :type "image" :file buffer-file-name))
  6139. ((eq major-mode 'dired-mode)
  6140. ;; link to the file in the current line
  6141. (setq cpltxt (concat "file:"
  6142. (abbreviate-file-name
  6143. (expand-file-name
  6144. (dired-get-filename nil t))))
  6145. link (org-make-link cpltxt)))
  6146. ((and buffer-file-name (org-mode-p))
  6147. ;; Just link to current headline
  6148. (setq cpltxt (concat "file:"
  6149. (abbreviate-file-name buffer-file-name)))
  6150. ;; Add a context search string
  6151. (when (org-xor org-context-in-file-links arg)
  6152. ;; Check if we are on a target
  6153. (if (org-in-regexp "<<\\(.*?\\)>>")
  6154. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  6155. (setq txt (cond
  6156. ((org-on-heading-p) nil)
  6157. ((org-region-active-p)
  6158. (buffer-substring (region-beginning) (region-end)))
  6159. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  6160. (when (or (null txt) (string-match "\\S-" txt))
  6161. (setq cpltxt
  6162. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6163. desc "NONE"))))
  6164. (if (string-match "::\\'" cpltxt)
  6165. (setq cpltxt (substring cpltxt 0 -2)))
  6166. (setq link (org-make-link cpltxt)))
  6167. ((buffer-file-name (buffer-base-buffer))
  6168. ;; Just link to this file here.
  6169. (setq cpltxt (concat "file:"
  6170. (abbreviate-file-name
  6171. (buffer-file-name (buffer-base-buffer)))))
  6172. ;; Add a context string
  6173. (when (org-xor org-context-in-file-links arg)
  6174. (setq txt (if (org-region-active-p)
  6175. (buffer-substring (region-beginning) (region-end))
  6176. (buffer-substring (point-at-bol) (point-at-eol))))
  6177. ;; Only use search option if there is some text.
  6178. (when (string-match "\\S-" txt)
  6179. (setq cpltxt
  6180. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6181. desc "NONE")))
  6182. (setq link (org-make-link cpltxt)))
  6183. ((interactive-p)
  6184. (error "Cannot link to a buffer which is not visiting a file"))
  6185. (t (setq link nil)))
  6186. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6187. (setq link (or link cpltxt)
  6188. desc (or desc cpltxt))
  6189. (if (equal desc "NONE") (setq desc nil))
  6190. (if (and (interactive-p) link)
  6191. (progn
  6192. (setq org-stored-links
  6193. (cons (list link desc) org-stored-links))
  6194. (message "Stored: %s" (or desc link)))
  6195. (and link (org-make-link-string link desc)))))
  6196. (defun org-store-link-props (&rest plist)
  6197. "Store link properties, extract names and addresses."
  6198. (let (x adr)
  6199. (when (setq x (plist-get plist :from))
  6200. (setq adr (mail-extract-address-components x))
  6201. (plist-put plist :fromname (car adr))
  6202. (plist-put plist :fromaddress (nth 1 adr)))
  6203. (when (setq x (plist-get plist :to))
  6204. (setq adr (mail-extract-address-components x))
  6205. (plist-put plist :toname (car adr))
  6206. (plist-put plist :toaddress (nth 1 adr))))
  6207. (let ((from (plist-get plist :from))
  6208. (to (plist-get plist :to)))
  6209. (when (and from to org-from-is-user-regexp)
  6210. (plist-put plist :fromto
  6211. (if (string-match org-from-is-user-regexp from)
  6212. (concat "to %t")
  6213. (concat "from %f")))))
  6214. (setq org-store-link-plist plist))
  6215. (defun org-add-link-props (&rest plist)
  6216. "Add these properties to the link property list."
  6217. (let (key value)
  6218. (while plist
  6219. (setq key (pop plist) value (pop plist))
  6220. (setq org-store-link-plist
  6221. (plist-put org-store-link-plist key value)))))
  6222. (defun org-email-link-description (&optional fmt)
  6223. "Return the description part of an email link.
  6224. This takes information from `org-store-link-plist' and formats it
  6225. according to FMT (default from `org-email-link-description-format')."
  6226. (setq fmt (or fmt org-email-link-description-format))
  6227. (let* ((p org-store-link-plist)
  6228. (to (plist-get p :toaddress))
  6229. (from (plist-get p :fromaddress))
  6230. (table
  6231. (list
  6232. (cons "%c" (plist-get p :fromto))
  6233. (cons "%F" (plist-get p :from))
  6234. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6235. (cons "%T" (plist-get p :to))
  6236. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6237. (cons "%s" (plist-get p :subject))
  6238. (cons "%m" (plist-get p :message-id)))))
  6239. (when (string-match "%c" fmt)
  6240. ;; Check if the user wrote this message
  6241. (if (and org-from-is-user-regexp from to
  6242. (save-match-data (string-match org-from-is-user-regexp from)))
  6243. (setq fmt (replace-match "to %t" t t fmt))
  6244. (setq fmt (replace-match "from %f" t t fmt))))
  6245. (org-replace-escapes fmt table)))
  6246. (defun org-make-org-heading-search-string (&optional string heading)
  6247. "Make search string for STRING or current headline."
  6248. (interactive)
  6249. (let ((s (or string (org-get-heading))))
  6250. (unless (and string (not heading))
  6251. ;; We are using a headline, clean up garbage in there.
  6252. (if (string-match org-todo-regexp s)
  6253. (setq s (replace-match "" t t s)))
  6254. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6255. (setq s (replace-match "" t t s)))
  6256. (setq s (org-trim s))
  6257. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6258. org-comment-string "\\)") s)
  6259. (setq s (replace-match "" t t s)))
  6260. (while (string-match org-ts-regexp s)
  6261. (setq s (replace-match "" t t s))))
  6262. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6263. (setq s (replace-match " " t t s)))
  6264. (or string (setq s (concat "*" s))) ; Add * for headlines
  6265. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6266. (defun org-make-link (&rest strings)
  6267. "Concatenate STRINGS."
  6268. (apply 'concat strings))
  6269. (defun org-make-link-string (link &optional description)
  6270. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6271. (unless (string-match "\\S-" link)
  6272. (error "Empty link"))
  6273. (when (stringp description)
  6274. ;; Remove brackets from the description, they are fatal.
  6275. (while (string-match "\\[" description)
  6276. (setq description (replace-match "{" t t description)))
  6277. (while (string-match "\\]" description)
  6278. (setq description (replace-match "}" t t description))))
  6279. (when (equal (org-link-escape link) description)
  6280. ;; No description needed, it is identical
  6281. (setq description nil))
  6282. (when (and (not description)
  6283. (not (equal link (org-link-escape link))))
  6284. (setq description link))
  6285. (concat "[[" (org-link-escape link) "]"
  6286. (if description (concat "[" description "]") "")
  6287. "]"))
  6288. (defconst org-link-escape-chars
  6289. '((?\ . "%20")
  6290. (?\[ . "%5B")
  6291. (?\] . "%5D")
  6292. (?\340 . "%E0") ; `a
  6293. (?\342 . "%E2") ; ^a
  6294. (?\347 . "%E7") ; ,c
  6295. (?\350 . "%E8") ; `e
  6296. (?\351 . "%E9") ; 'e
  6297. (?\352 . "%EA") ; ^e
  6298. (?\356 . "%EE") ; ^i
  6299. (?\364 . "%F4") ; ^o
  6300. (?\371 . "%F9") ; `u
  6301. (?\373 . "%FB") ; ^u
  6302. (?\; . "%3B")
  6303. (?? . "%3F")
  6304. (?= . "%3D")
  6305. (?+ . "%2B")
  6306. )
  6307. "Association list of escapes for some characters problematic in links.
  6308. This is the list that is used for internal purposes.")
  6309. (defconst org-link-escape-chars-browser
  6310. '((?\ . "%20")) ; 32 for the SPC char
  6311. "Association list of escapes for some characters problematic in links.
  6312. This is the list that is used before handing over to the browser.")
  6313. (defun org-link-escape (text &optional table)
  6314. "Escape charaters in TEXT that are problematic for links."
  6315. (setq table (or table org-link-escape-chars))
  6316. (when text
  6317. (let ((re (mapconcat (lambda (x) (regexp-quote
  6318. (char-to-string (car x))))
  6319. table "\\|")))
  6320. (while (string-match re text)
  6321. (setq text
  6322. (replace-match
  6323. (cdr (assoc (string-to-char (match-string 0 text))
  6324. table))
  6325. t t text)))
  6326. text)))
  6327. (defun org-link-unescape (text &optional table)
  6328. "Reverse the action of `org-link-escape'."
  6329. (setq table (or table org-link-escape-chars))
  6330. (when text
  6331. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6332. table "\\|")))
  6333. (while (string-match re text)
  6334. (setq text
  6335. (replace-match
  6336. (char-to-string (car (rassoc (match-string 0 text) table)))
  6337. t t text)))
  6338. text)))
  6339. (defun org-xor (a b)
  6340. "Exclusive or."
  6341. (if a (not b) b))
  6342. (defun org-get-header (header)
  6343. "Find a header field in the current buffer."
  6344. (save-excursion
  6345. (goto-char (point-min))
  6346. (let ((case-fold-search t) s)
  6347. (cond
  6348. ((eq header 'from)
  6349. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6350. (setq s (match-string 1)))
  6351. (while (string-match "\"" s)
  6352. (setq s (replace-match "" t t s)))
  6353. (if (string-match "[<(].*" s)
  6354. (setq s (replace-match "" t t s))))
  6355. ((eq header 'message-id)
  6356. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6357. (setq s (match-string 1))))
  6358. ((eq header 'subject)
  6359. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6360. (setq s (match-string 1)))))
  6361. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6362. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6363. s)))
  6364. (defun org-fixup-message-id-for-http (s)
  6365. "Replace special characters in a message id, so it can be used in an http query."
  6366. (while (string-match "<" s)
  6367. (setq s (replace-match "%3C" t t s)))
  6368. (while (string-match ">" s)
  6369. (setq s (replace-match "%3E" t t s)))
  6370. (while (string-match "@" s)
  6371. (setq s (replace-match "%40" t t s)))
  6372. s)
  6373. ;;;###autoload
  6374. (defun org-insert-link-global ()
  6375. "Insert a link like Org-mode does.
  6376. This command can be called in any mode to insert a link in Org-mode syntax."
  6377. (interactive)
  6378. (org-load-modules-maybe)
  6379. (org-run-like-in-org-mode 'org-insert-link))
  6380. (defun org-insert-link (&optional complete-file)
  6381. "Insert a link. At the prompt, enter the link.
  6382. Completion can be used to select a link previously stored with
  6383. `org-store-link'. When the empty string is entered (i.e. if you just
  6384. press RET at the prompt), the link defaults to the most recently
  6385. stored link. As SPC triggers completion in the minibuffer, you need to
  6386. use M-SPC or C-q SPC to force the insertion of a space character.
  6387. You will also be prompted for a description, and if one is given, it will
  6388. be displayed in the buffer instead of the link.
  6389. If there is already a link at point, this command will allow you to edit link
  6390. and description parts.
  6391. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  6392. selected using completion. The path to the file will be relative to
  6393. the current directory if the file is in the current directory or a
  6394. subdirectory. Otherwise, the link will be the absolute path as
  6395. completed in the minibuffer (i.e. normally ~/path/to/file).
  6396. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  6397. is in the current directory or below.
  6398. With three \\[universal-argument] prefixes, negate the meaning of
  6399. `org-keep-stored-link-after-insertion'."
  6400. (interactive "P")
  6401. (let* ((wcf (current-window-configuration))
  6402. (region (if (org-region-active-p)
  6403. (buffer-substring (region-beginning) (region-end))))
  6404. (remove (and region (list (region-beginning) (region-end))))
  6405. (desc region)
  6406. tmphist ; byte-compile incorrectly complains about this
  6407. link entry file)
  6408. (cond
  6409. ((org-in-regexp org-bracket-link-regexp 1)
  6410. ;; We do have a link at point, and we are going to edit it.
  6411. (setq remove (list (match-beginning 0) (match-end 0)))
  6412. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6413. (setq link (read-string "Link: "
  6414. (org-link-unescape
  6415. (org-match-string-no-properties 1)))))
  6416. ((or (org-in-regexp org-angle-link-re)
  6417. (org-in-regexp org-plain-link-re))
  6418. ;; Convert to bracket link
  6419. (setq remove (list (match-beginning 0) (match-end 0))
  6420. link (read-string "Link: "
  6421. (org-remove-angle-brackets (match-string 0)))))
  6422. ((equal complete-file '(4))
  6423. ;; Completing read for file names.
  6424. (setq file (read-file-name "File: "))
  6425. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6426. (pwd1 (file-name-as-directory (abbreviate-file-name
  6427. (expand-file-name ".")))))
  6428. (cond
  6429. ((equal complete-file '(16))
  6430. (setq link (org-make-link
  6431. "file:"
  6432. (abbreviate-file-name (expand-file-name file)))))
  6433. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6434. (setq link (org-make-link "file:" (match-string 1 file))))
  6435. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6436. (expand-file-name file))
  6437. (setq link (org-make-link
  6438. "file:" (match-string 1 (expand-file-name file)))))
  6439. (t (setq link (org-make-link "file:" file))))))
  6440. (t
  6441. ;; Read link, with completion for stored links.
  6442. (with-output-to-temp-buffer "*Org Links*"
  6443. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6444. (when org-stored-links
  6445. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6446. (princ (mapconcat
  6447. (lambda (x)
  6448. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6449. (reverse org-stored-links) "\n"))))
  6450. (let ((cw (selected-window)))
  6451. (select-window (get-buffer-window "*Org Links*"))
  6452. (shrink-window-if-larger-than-buffer)
  6453. (setq truncate-lines t)
  6454. (select-window cw))
  6455. ;; Fake a link history, containing the stored links.
  6456. (setq tmphist (append (mapcar 'car org-stored-links)
  6457. org-insert-link-history))
  6458. (unwind-protect
  6459. (setq link (org-completing-read
  6460. "Link: "
  6461. (append
  6462. (mapcar (lambda (x) (list (concat (car x) ":")))
  6463. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6464. (mapcar (lambda (x) (list (concat x ":")))
  6465. org-link-types))
  6466. nil nil nil
  6467. 'tmphist
  6468. (or (car (car org-stored-links)))))
  6469. (set-window-configuration wcf)
  6470. (kill-buffer "*Org Links*"))
  6471. (setq entry (assoc link org-stored-links))
  6472. (or entry (push link org-insert-link-history))
  6473. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6474. (not org-keep-stored-link-after-insertion))
  6475. (setq org-stored-links (delq (assoc link org-stored-links)
  6476. org-stored-links)))
  6477. (setq desc (or desc (nth 1 entry)))))
  6478. (if (string-match org-plain-link-re link)
  6479. ;; URL-like link, normalize the use of angular brackets.
  6480. (setq link (org-make-link (org-remove-angle-brackets link))))
  6481. ;; Check if we are linking to the current file with a search option
  6482. ;; If yes, simplify the link by using only the search option.
  6483. (when (and buffer-file-name
  6484. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6485. (let* ((path (match-string 1 link))
  6486. (case-fold-search nil)
  6487. (search (match-string 2 link)))
  6488. (save-match-data
  6489. (if (equal (file-truename buffer-file-name) (file-truename path))
  6490. ;; We are linking to this same file, with a search option
  6491. (setq link search)))))
  6492. ;; Check if we can/should use a relative path. If yes, simplify the link
  6493. (when (string-match "\\<file:\\(.*\\)" link)
  6494. (let* ((path (match-string 1 link))
  6495. (origpath path)
  6496. (case-fold-search nil))
  6497. (cond
  6498. ((eq org-link-file-path-type 'absolute)
  6499. (setq path (abbreviate-file-name (expand-file-name path))))
  6500. ((eq org-link-file-path-type 'noabbrev)
  6501. (setq path (expand-file-name path)))
  6502. ((eq org-link-file-path-type 'relative)
  6503. (setq path (file-relative-name path)))
  6504. (t
  6505. (save-match-data
  6506. (if (string-match (concat "^" (regexp-quote
  6507. (file-name-as-directory
  6508. (expand-file-name "."))))
  6509. (expand-file-name path))
  6510. ;; We are linking a file with relative path name.
  6511. (setq path (substring (expand-file-name path)
  6512. (match-end 0)))))))
  6513. (setq link (concat "file:" path))
  6514. (if (equal desc origpath)
  6515. (setq desc path))))
  6516. (setq desc (read-string "Description: " desc))
  6517. (unless (string-match "\\S-" desc) (setq desc nil))
  6518. (if remove (apply 'delete-region remove))
  6519. (insert (org-make-link-string link desc))))
  6520. (defun org-completing-read (&rest args)
  6521. (let ((minibuffer-local-completion-map
  6522. (copy-keymap minibuffer-local-completion-map)))
  6523. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6524. (apply 'completing-read args)))
  6525. ;;; Opening/following a link
  6526. (defvar org-link-search-failed nil)
  6527. (defun org-next-link ()
  6528. "Move forward to the next link.
  6529. If the link is in hidden text, expose it."
  6530. (interactive)
  6531. (when (and org-link-search-failed (eq this-command last-command))
  6532. (goto-char (point-min))
  6533. (message "Link search wrapped back to beginning of buffer"))
  6534. (setq org-link-search-failed nil)
  6535. (let* ((pos (point))
  6536. (ct (org-context))
  6537. (a (assoc :link ct)))
  6538. (if a (goto-char (nth 2 a)))
  6539. (if (re-search-forward org-any-link-re nil t)
  6540. (progn
  6541. (goto-char (match-beginning 0))
  6542. (if (org-invisible-p) (org-show-context)))
  6543. (goto-char pos)
  6544. (setq org-link-search-failed t)
  6545. (error "No further link found"))))
  6546. (defun org-previous-link ()
  6547. "Move backward to the previous link.
  6548. If the link is in hidden text, expose it."
  6549. (interactive)
  6550. (when (and org-link-search-failed (eq this-command last-command))
  6551. (goto-char (point-max))
  6552. (message "Link search wrapped back to end of buffer"))
  6553. (setq org-link-search-failed nil)
  6554. (let* ((pos (point))
  6555. (ct (org-context))
  6556. (a (assoc :link ct)))
  6557. (if a (goto-char (nth 1 a)))
  6558. (if (re-search-backward org-any-link-re nil t)
  6559. (progn
  6560. (goto-char (match-beginning 0))
  6561. (if (org-invisible-p) (org-show-context)))
  6562. (goto-char pos)
  6563. (setq org-link-search-failed t)
  6564. (error "No further link found"))))
  6565. (defun org-find-file-at-mouse (ev)
  6566. "Open file link or URL at mouse."
  6567. (interactive "e")
  6568. (mouse-set-point ev)
  6569. (org-open-at-point 'in-emacs))
  6570. (defun org-open-at-mouse (ev)
  6571. "Open file link or URL at mouse."
  6572. (interactive "e")
  6573. (mouse-set-point ev)
  6574. (org-open-at-point))
  6575. (defvar org-window-config-before-follow-link nil
  6576. "The window configuration before following a link.
  6577. This is saved in case the need arises to restore it.")
  6578. (defvar org-open-link-marker (make-marker)
  6579. "Marker pointing to the location where `org-open-at-point; was called.")
  6580. ;;;###autoload
  6581. (defun org-open-at-point-global ()
  6582. "Follow a link like Org-mode does.
  6583. This command can be called in any mode to follow a link that has
  6584. Org-mode syntax."
  6585. (interactive)
  6586. (org-run-like-in-org-mode 'org-open-at-point))
  6587. (defun org-open-at-point (&optional in-emacs)
  6588. "Open link at or after point.
  6589. If there is no link at point, this function will search forward up to
  6590. the end of the current subtree.
  6591. Normally, files will be opened by an appropriate application. If the
  6592. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6593. (interactive "P")
  6594. (org-load-modules-maybe)
  6595. (move-marker org-open-link-marker (point))
  6596. (setq org-window-config-before-follow-link (current-window-configuration))
  6597. (org-remove-occur-highlights nil nil t)
  6598. (if (org-at-timestamp-p t)
  6599. (org-follow-timestamp-link)
  6600. (let (type path link line search (pos (point)))
  6601. (catch 'match
  6602. (save-excursion
  6603. (skip-chars-forward "^]\n\r")
  6604. (when (org-in-regexp org-bracket-link-regexp)
  6605. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6606. (while (string-match " *\n *" link)
  6607. (setq link (replace-match " " t t link)))
  6608. (setq link (org-link-expand-abbrev link))
  6609. (if (string-match org-link-re-with-space2 link)
  6610. (setq type (match-string 1 link) path (match-string 2 link))
  6611. (setq type "thisfile" path link))
  6612. (throw 'match t)))
  6613. (when (get-text-property (point) 'org-linked-text)
  6614. (setq type "thisfile"
  6615. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6616. (1+ (point)) (point))
  6617. path (buffer-substring
  6618. (previous-single-property-change pos 'org-linked-text)
  6619. (next-single-property-change pos 'org-linked-text)))
  6620. (throw 'match t))
  6621. (save-excursion
  6622. (when (or (org-in-regexp org-angle-link-re)
  6623. (org-in-regexp org-plain-link-re))
  6624. (setq type (match-string 1) path (match-string 2))
  6625. (throw 'match t)))
  6626. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6627. (setq type "tree-match"
  6628. path (match-string 1))
  6629. (throw 'match t))
  6630. (save-excursion
  6631. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6632. (setq type "tags"
  6633. path (match-string 1))
  6634. (while (string-match ":" path)
  6635. (setq path (replace-match "+" t t path)))
  6636. (throw 'match t))))
  6637. (unless path
  6638. (error "No link found"))
  6639. ;; Remove any trailing spaces in path
  6640. (if (string-match " +\\'" path)
  6641. (setq path (replace-match "" t t path)))
  6642. (cond
  6643. ((assoc type org-link-protocols)
  6644. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6645. ((equal type "mailto")
  6646. (let ((cmd (car org-link-mailto-program))
  6647. (args (cdr org-link-mailto-program)) args1
  6648. (address path) (subject "") a)
  6649. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6650. (setq address (match-string 1 path)
  6651. subject (org-link-escape (match-string 2 path))))
  6652. (while args
  6653. (cond
  6654. ((not (stringp (car args))) (push (pop args) args1))
  6655. (t (setq a (pop args))
  6656. (if (string-match "%a" a)
  6657. (setq a (replace-match address t t a)))
  6658. (if (string-match "%s" a)
  6659. (setq a (replace-match subject t t a)))
  6660. (push a args1))))
  6661. (apply cmd (nreverse args1))))
  6662. ((member type '("http" "https" "ftp" "news"))
  6663. (browse-url (concat type ":" (org-link-escape
  6664. path org-link-escape-chars-browser))))
  6665. ((member type '("message"))
  6666. (browse-url (concat type ":" path)))
  6667. ((string= type "tags")
  6668. (org-tags-view in-emacs path))
  6669. ((string= type "thisfile")
  6670. (if in-emacs
  6671. (switch-to-buffer-other-window
  6672. (org-get-buffer-for-internal-link (current-buffer)))
  6673. (org-mark-ring-push))
  6674. (let ((cmd `(org-link-search
  6675. ,path
  6676. ,(cond ((equal in-emacs '(4)) 'occur)
  6677. ((equal in-emacs '(16)) 'org-occur)
  6678. (t nil))
  6679. ,pos)))
  6680. (condition-case nil (eval cmd)
  6681. (error (progn (widen) (eval cmd))))))
  6682. ((string= type "tree-match")
  6683. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6684. ((string= type "file")
  6685. (if (string-match "::\\([0-9]+\\)\\'" path)
  6686. (setq line (string-to-number (match-string 1 path))
  6687. path (substring path 0 (match-beginning 0)))
  6688. (if (string-match "::\\(.+\\)\\'" path)
  6689. (setq search (match-string 1 path)
  6690. path (substring path 0 (match-beginning 0)))))
  6691. (if (string-match "[*?{]" (file-name-nondirectory path))
  6692. (dired path)
  6693. (org-open-file path in-emacs line search)))
  6694. ((string= type "news")
  6695. (require 'org-gnus)
  6696. (org-gnus-follow-link path))
  6697. ((string= type "shell")
  6698. (let ((cmd path))
  6699. (if (or (not org-confirm-shell-link-function)
  6700. (funcall org-confirm-shell-link-function
  6701. (format "Execute \"%s\" in shell? "
  6702. (org-add-props cmd nil
  6703. 'face 'org-warning))))
  6704. (progn
  6705. (message "Executing %s" cmd)
  6706. (shell-command cmd))
  6707. (error "Abort"))))
  6708. ((string= type "elisp")
  6709. (let ((cmd path))
  6710. (if (or (not org-confirm-elisp-link-function)
  6711. (funcall org-confirm-elisp-link-function
  6712. (format "Execute \"%s\" as elisp? "
  6713. (org-add-props cmd nil
  6714. 'face 'org-warning))))
  6715. (message "%s => %s" cmd (eval (read cmd)))
  6716. (error "Abort"))))
  6717. (t
  6718. (browse-url-at-point)))))
  6719. (move-marker org-open-link-marker nil)
  6720. (run-hook-with-args 'org-follow-link-hook))
  6721. ;;; File search
  6722. (defvar org-create-file-search-functions nil
  6723. "List of functions to construct the right search string for a file link.
  6724. These functions are called in turn with point at the location to
  6725. which the link should point.
  6726. A function in the hook should first test if it would like to
  6727. handle this file type, for example by checking the major-mode or
  6728. the file extension. If it decides not to handle this file, it
  6729. should just return nil to give other functions a chance. If it
  6730. does handle the file, it must return the search string to be used
  6731. when following the link. The search string will be part of the
  6732. file link, given after a double colon, and `org-open-at-point'
  6733. will automatically search for it. If special measures must be
  6734. taken to make the search successful, another function should be
  6735. added to the companion hook `org-execute-file-search-functions',
  6736. which see.
  6737. A function in this hook may also use `setq' to set the variable
  6738. `description' to provide a suggestion for the descriptive text to
  6739. be used for this link when it gets inserted into an Org-mode
  6740. buffer with \\[org-insert-link].")
  6741. (defvar org-execute-file-search-functions nil
  6742. "List of functions to execute a file search triggered by a link.
  6743. Functions added to this hook must accept a single argument, the
  6744. search string that was part of the file link, the part after the
  6745. double colon. The function must first check if it would like to
  6746. handle this search, for example by checking the major-mode or the
  6747. file extension. If it decides not to handle this search, it
  6748. should just return nil to give other functions a chance. If it
  6749. does handle the search, it must return a non-nil value to keep
  6750. other functions from trying.
  6751. Each function can access the current prefix argument through the
  6752. variable `current-prefix-argument'. Note that a single prefix is
  6753. used to force opening a link in Emacs, so it may be good to only
  6754. use a numeric or double prefix to guide the search function.
  6755. In case this is needed, a function in this hook can also restore
  6756. the window configuration before `org-open-at-point' was called using:
  6757. (set-window-configuration org-window-config-before-follow-link)")
  6758. (defun org-link-search (s &optional type avoid-pos)
  6759. "Search for a link search option.
  6760. If S is surrounded by forward slashes, it is interpreted as a
  6761. regular expression. In org-mode files, this will create an `org-occur'
  6762. sparse tree. In ordinary files, `occur' will be used to list matches.
  6763. If the current buffer is in `dired-mode', grep will be used to search
  6764. in all files. If AVOID-POS is given, ignore matches near that position."
  6765. (let ((case-fold-search t)
  6766. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6767. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6768. (append '(("") (" ") ("\t") ("\n"))
  6769. org-emphasis-alist)
  6770. "\\|") "\\)"))
  6771. (pos (point))
  6772. (pre "") (post "")
  6773. words re0 re1 re2 re3 re4 re5 re2a reall)
  6774. (cond
  6775. ;; First check if there are any special
  6776. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6777. ;; Now try the builtin stuff
  6778. ((save-excursion
  6779. (goto-char (point-min))
  6780. (and
  6781. (re-search-forward
  6782. (concat "<<" (regexp-quote s0) ">>") nil t)
  6783. (setq pos (match-beginning 0))))
  6784. ;; There is an exact target for this
  6785. (goto-char pos))
  6786. ((string-match "^/\\(.*\\)/$" s)
  6787. ;; A regular expression
  6788. (cond
  6789. ((org-mode-p)
  6790. (org-occur (match-string 1 s)))
  6791. ;;((eq major-mode 'dired-mode)
  6792. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6793. (t (org-do-occur (match-string 1 s)))))
  6794. (t
  6795. ;; A normal search strings
  6796. (when (equal (string-to-char s) ?*)
  6797. ;; Anchor on headlines, post may include tags.
  6798. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6799. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6800. s (substring s 1)))
  6801. (remove-text-properties
  6802. 0 (length s)
  6803. '(face nil mouse-face nil keymap nil fontified nil) s)
  6804. ;; Make a series of regular expressions to find a match
  6805. (setq words (org-split-string s "[ \n\r\t]+")
  6806. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6807. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6808. "\\)" markers)
  6809. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6810. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6811. re1 (concat pre re2 post)
  6812. re3 (concat pre re4 post)
  6813. re5 (concat pre ".*" re4)
  6814. re2 (concat pre re2)
  6815. re2a (concat pre re2a)
  6816. re4 (concat pre re4)
  6817. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6818. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6819. re5 "\\)"
  6820. ))
  6821. (cond
  6822. ((eq type 'org-occur) (org-occur reall))
  6823. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6824. (t (goto-char (point-min))
  6825. (if (or (org-search-not-self 1 re0 nil t)
  6826. (org-search-not-self 1 re1 nil t)
  6827. (org-search-not-self 1 re2 nil t)
  6828. (org-search-not-self 1 re2a nil t)
  6829. (org-search-not-self 1 re3 nil t)
  6830. (org-search-not-self 1 re4 nil t)
  6831. (org-search-not-self 1 re5 nil t)
  6832. )
  6833. (goto-char (match-beginning 1))
  6834. (goto-char pos)
  6835. (error "No match")))))
  6836. (t
  6837. ;; Normal string-search
  6838. (goto-char (point-min))
  6839. (if (search-forward s nil t)
  6840. (goto-char (match-beginning 0))
  6841. (error "No match"))))
  6842. (and (org-mode-p) (org-show-context 'link-search))))
  6843. (defun org-search-not-self (group &rest args)
  6844. "Execute `re-search-forward', but only accept matches that do not
  6845. enclose the position of `org-open-link-marker'."
  6846. (let ((m org-open-link-marker))
  6847. (catch 'exit
  6848. (while (apply 're-search-forward args)
  6849. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6850. (goto-char (match-end group))
  6851. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6852. (> (match-beginning 0) (marker-position m))
  6853. (< (match-end 0) (marker-position m)))
  6854. (save-match-data
  6855. (or (not (org-in-regexp
  6856. org-bracket-link-analytic-regexp 1))
  6857. (not (match-end 4)) ; no description
  6858. (and (<= (match-beginning 4) (point))
  6859. (>= (match-end 4) (point))))))
  6860. (throw 'exit (point))))))))
  6861. (defun org-get-buffer-for-internal-link (buffer)
  6862. "Return a buffer to be used for displaying the link target of internal links."
  6863. (cond
  6864. ((not org-display-internal-link-with-indirect-buffer)
  6865. buffer)
  6866. ((string-match "(Clone)$" (buffer-name buffer))
  6867. (message "Buffer is already a clone, not making another one")
  6868. ;; we also do not modify visibility in this case
  6869. buffer)
  6870. (t ; make a new indirect buffer for displaying the link
  6871. (let* ((bn (buffer-name buffer))
  6872. (ibn (concat bn "(Clone)"))
  6873. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6874. (with-current-buffer ib (org-overview))
  6875. ib))))
  6876. (defun org-do-occur (regexp &optional cleanup)
  6877. "Call the Emacs command `occur'.
  6878. If CLEANUP is non-nil, remove the printout of the regular expression
  6879. in the *Occur* buffer. This is useful if the regex is long and not useful
  6880. to read."
  6881. (occur regexp)
  6882. (when cleanup
  6883. (let ((cwin (selected-window)) win beg end)
  6884. (when (setq win (get-buffer-window "*Occur*"))
  6885. (select-window win))
  6886. (goto-char (point-min))
  6887. (when (re-search-forward "match[a-z]+" nil t)
  6888. (setq beg (match-end 0))
  6889. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6890. (setq end (1- (match-beginning 0)))))
  6891. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6892. (goto-char (point-min))
  6893. (select-window cwin))))
  6894. ;;; The mark ring for links jumps
  6895. (defvar org-mark-ring nil
  6896. "Mark ring for positions before jumps in Org-mode.")
  6897. (defvar org-mark-ring-last-goto nil
  6898. "Last position in the mark ring used to go back.")
  6899. ;; Fill and close the ring
  6900. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6901. (loop for i from 1 to org-mark-ring-length do
  6902. (push (make-marker) org-mark-ring))
  6903. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6904. org-mark-ring)
  6905. (defun org-mark-ring-push (&optional pos buffer)
  6906. "Put the current position or POS into the mark ring and rotate it."
  6907. (interactive)
  6908. (setq pos (or pos (point)))
  6909. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6910. (move-marker (car org-mark-ring)
  6911. (or pos (point))
  6912. (or buffer (current-buffer)))
  6913. (message "%s"
  6914. (substitute-command-keys
  6915. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6916. (defun org-mark-ring-goto (&optional n)
  6917. "Jump to the previous position in the mark ring.
  6918. With prefix arg N, jump back that many stored positions. When
  6919. called several times in succession, walk through the entire ring.
  6920. Org-mode commands jumping to a different position in the current file,
  6921. or to another Org-mode file, automatically push the old position
  6922. onto the ring."
  6923. (interactive "p")
  6924. (let (p m)
  6925. (if (eq last-command this-command)
  6926. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6927. (setq p org-mark-ring))
  6928. (setq org-mark-ring-last-goto p)
  6929. (setq m (car p))
  6930. (switch-to-buffer (marker-buffer m))
  6931. (goto-char m)
  6932. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6933. (defun org-remove-angle-brackets (s)
  6934. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6935. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6936. s)
  6937. (defun org-add-angle-brackets (s)
  6938. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6939. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6940. s)
  6941. ;;; Following specific links
  6942. (defun org-follow-timestamp-link ()
  6943. (cond
  6944. ((org-at-date-range-p t)
  6945. (let ((org-agenda-start-on-weekday)
  6946. (t1 (match-string 1))
  6947. (t2 (match-string 2)))
  6948. (setq t1 (time-to-days (org-time-string-to-time t1))
  6949. t2 (time-to-days (org-time-string-to-time t2)))
  6950. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6951. ((org-at-timestamp-p t)
  6952. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6953. (substring (match-string 1) 0 10)))
  6954. 1))
  6955. (t (error "This should not happen"))))
  6956. ;;; Following file links
  6957. (defun org-open-file (path &optional in-emacs line search)
  6958. "Open the file at PATH.
  6959. First, this expands any special file name abbreviations. Then the
  6960. configuration variable `org-file-apps' is checked if it contains an
  6961. entry for this file type, and if yes, the corresponding command is launched.
  6962. If no application is found, Emacs simply visits the file.
  6963. With optional argument IN-EMACS, Emacs will visit the file.
  6964. Optional LINE specifies a line to go to, optional SEARCH a string to
  6965. search for. If LINE or SEARCH is given, the file will always be
  6966. opened in Emacs.
  6967. If the file does not exist, an error is thrown."
  6968. (setq in-emacs (or in-emacs line search))
  6969. (let* ((file (if (equal path "")
  6970. buffer-file-name
  6971. (substitute-in-file-name (expand-file-name path))))
  6972. (apps (append org-file-apps (org-default-apps)))
  6973. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6974. (dirp (if remp nil (file-directory-p file)))
  6975. (dfile (downcase file))
  6976. (old-buffer (current-buffer))
  6977. (old-pos (point))
  6978. (old-mode major-mode)
  6979. ext cmd)
  6980. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6981. (setq ext (match-string 1 dfile))
  6982. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6983. (setq ext (match-string 1 dfile))))
  6984. (if in-emacs
  6985. (setq cmd 'emacs)
  6986. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6987. (and dirp (cdr (assoc 'directory apps)))
  6988. (cdr (assoc ext apps))
  6989. (cdr (assoc t apps)))))
  6990. (when (eq cmd 'mailcap)
  6991. (require 'mailcap)
  6992. (mailcap-parse-mailcaps)
  6993. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6994. (command (mailcap-mime-info mime-type)))
  6995. (if (stringp command)
  6996. (setq cmd command)
  6997. (setq cmd 'emacs))))
  6998. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6999. (not (file-exists-p file))
  7000. (not org-open-non-existing-files))
  7001. (error "No such file: %s" file))
  7002. (cond
  7003. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7004. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7005. (while (string-match "['\"]%s['\"]" cmd)
  7006. (setq cmd (replace-match "%s" t t cmd)))
  7007. (while (string-match "%s" cmd)
  7008. (setq cmd (replace-match
  7009. (save-match-data (shell-quote-argument file))
  7010. t t cmd)))
  7011. (save-window-excursion
  7012. (start-process-shell-command cmd nil cmd)))
  7013. ((or (stringp cmd)
  7014. (eq cmd 'emacs))
  7015. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7016. (widen)
  7017. (if line (goto-line line)
  7018. (if search (org-link-search search))))
  7019. ((consp cmd)
  7020. (eval cmd))
  7021. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7022. (and (org-mode-p) (eq old-mode 'org-mode)
  7023. (or (not (equal old-buffer (current-buffer)))
  7024. (not (equal old-pos (point))))
  7025. (org-mark-ring-push old-pos old-buffer))))
  7026. (defun org-default-apps ()
  7027. "Return the default applications for this operating system."
  7028. (cond
  7029. ((eq system-type 'darwin)
  7030. org-file-apps-defaults-macosx)
  7031. ((eq system-type 'windows-nt)
  7032. org-file-apps-defaults-windowsnt)
  7033. (t org-file-apps-defaults-gnu)))
  7034. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7035. (defun org-file-remote-p (file)
  7036. "Test whether FILE specifies a location on a remote system.
  7037. Return non-nil if the location is indeed remote.
  7038. For example, the filename \"/user@host:/foo\" specifies a location
  7039. on the system \"/user@host:\"."
  7040. (cond ((fboundp 'file-remote-p)
  7041. (file-remote-p file))
  7042. ((fboundp 'tramp-handle-file-remote-p)
  7043. (tramp-handle-file-remote-p file))
  7044. ((and (boundp 'ange-ftp-name-format)
  7045. (string-match (car ange-ftp-name-format) file))
  7046. t)
  7047. (t nil)))
  7048. ;;;; Refiling
  7049. (defun org-get-org-file ()
  7050. "Read a filename, with default directory `org-directory'."
  7051. (let ((default (or org-default-notes-file remember-data-file)))
  7052. (read-file-name (format "File name [%s]: " default)
  7053. (file-name-as-directory org-directory)
  7054. default)))
  7055. (defun org-notes-order-reversed-p ()
  7056. "Check if the current file should receive notes in reversed order."
  7057. (cond
  7058. ((not org-reverse-note-order) nil)
  7059. ((eq t org-reverse-note-order) t)
  7060. ((not (listp org-reverse-note-order)) nil)
  7061. (t (catch 'exit
  7062. (let ((all org-reverse-note-order)
  7063. entry)
  7064. (while (setq entry (pop all))
  7065. (if (string-match (car entry) buffer-file-name)
  7066. (throw 'exit (cdr entry))))
  7067. nil)))))
  7068. (defvar org-refile-target-table nil
  7069. "The list of refile targets, created by `org-refile'.")
  7070. (defvar org-agenda-new-buffers nil
  7071. "Buffers created to visit agenda files.")
  7072. (defun org-get-refile-targets (&optional default-buffer)
  7073. "Produce a table with refile targets."
  7074. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7075. targets txt re files f desc descre)
  7076. (with-current-buffer (or default-buffer (current-buffer))
  7077. (while (setq entry (pop entries))
  7078. (setq files (car entry) desc (cdr entry))
  7079. (cond
  7080. ((null files) (setq files (list (current-buffer))))
  7081. ((eq files 'org-agenda-files)
  7082. (setq files (org-agenda-files 'unrestricted)))
  7083. ((and (symbolp files) (fboundp files))
  7084. (setq files (funcall files)))
  7085. ((and (symbolp files) (boundp files))
  7086. (setq files (symbol-value files))))
  7087. (if (stringp files) (setq files (list files)))
  7088. (cond
  7089. ((eq (car desc) :tag)
  7090. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7091. ((eq (car desc) :todo)
  7092. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7093. ((eq (car desc) :regexp)
  7094. (setq descre (cdr desc)))
  7095. ((eq (car desc) :level)
  7096. (setq descre (concat "^\\*\\{" (number-to-string
  7097. (if org-odd-levels-only
  7098. (1- (* 2 (cdr desc)))
  7099. (cdr desc)))
  7100. "\\}[ \t]")))
  7101. ((eq (car desc) :maxlevel)
  7102. (setq descre (concat "^\\*\\{1," (number-to-string
  7103. (if org-odd-levels-only
  7104. (1- (* 2 (cdr desc)))
  7105. (cdr desc)))
  7106. "\\}[ \t]")))
  7107. (t (error "Bad refiling target description %s" desc)))
  7108. (while (setq f (pop files))
  7109. (save-excursion
  7110. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7111. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7112. (save-excursion
  7113. (save-restriction
  7114. (widen)
  7115. (goto-char (point-min))
  7116. (while (re-search-forward descre nil t)
  7117. (goto-char (point-at-bol))
  7118. (when (looking-at org-complex-heading-regexp)
  7119. (setq txt (match-string 4)
  7120. re (concat "^" (regexp-quote
  7121. (buffer-substring (match-beginning 1)
  7122. (match-end 4)))))
  7123. (if (match-end 5) (setq re (concat re "[ \t]+"
  7124. (regexp-quote
  7125. (match-string 5)))))
  7126. (setq re (concat re "[ \t]*$"))
  7127. (when org-refile-use-outline-path
  7128. (setq txt (mapconcat 'identity
  7129. (append
  7130. (if (eq org-refile-use-outline-path 'file)
  7131. (list (file-name-nondirectory
  7132. (buffer-file-name (buffer-base-buffer))))
  7133. (if (eq org-refile-use-outline-path 'full-file-path)
  7134. (list (buffer-file-name (buffer-base-buffer)))))
  7135. (org-get-outline-path)
  7136. (list txt))
  7137. "/")))
  7138. (push (list txt f re (point)) targets))
  7139. (goto-char (point-at-eol))))))))
  7140. (nreverse targets))))
  7141. (defun org-get-outline-path ()
  7142. "Return the outline path to the current entry, as a list."
  7143. (let (rtn)
  7144. (save-excursion
  7145. (while (org-up-heading-safe)
  7146. (when (looking-at org-complex-heading-regexp)
  7147. (push (org-match-string-no-properties 4) rtn)))
  7148. rtn)))
  7149. (defvar org-refile-history nil
  7150. "History for refiling operations.")
  7151. (defun org-refile (&optional goto default-buffer)
  7152. "Move the entry at point to another heading.
  7153. The list of target headings is compiled using the information in
  7154. `org-refile-targets', which see. This list is created before each use
  7155. and will therefore always be up-to-date.
  7156. At the target location, the entry is filed as a subitem of the target heading.
  7157. Depending on `org-reverse-note-order', the new subitem will either be the
  7158. first of the last subitem.
  7159. With prefix arg GOTO, the command will only visit the target location,
  7160. not actually move anything.
  7161. With a double prefix `C-c C-c', go to the location where the last refiling
  7162. operation has put the subtree."
  7163. (interactive "P")
  7164. (let* ((cbuf (current-buffer))
  7165. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7166. pos it nbuf file re level reversed)
  7167. (if (equal goto '(16))
  7168. (org-refile-goto-last-stored)
  7169. (when (setq it (org-refile-get-location
  7170. (if goto "Goto: " "Refile to: ") default-buffer))
  7171. (setq file (nth 1 it)
  7172. re (nth 2 it)
  7173. pos (nth 3 it))
  7174. (setq nbuf (or (find-buffer-visiting file)
  7175. (find-file-noselect file)))
  7176. (if goto
  7177. (progn
  7178. (switch-to-buffer nbuf)
  7179. (goto-char pos)
  7180. (org-show-context 'org-goto))
  7181. (org-copy-special)
  7182. (save-excursion
  7183. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7184. (find-file-noselect file))))
  7185. (setq reversed (org-notes-order-reversed-p))
  7186. (save-excursion
  7187. (save-restriction
  7188. (widen)
  7189. (goto-char pos)
  7190. (looking-at outline-regexp)
  7191. (setq level (org-get-valid-level (funcall outline-level) 1))
  7192. (goto-char
  7193. (if reversed
  7194. (outline-next-heading)
  7195. (or (save-excursion (outline-get-next-sibling))
  7196. (org-end-of-subtree t t)
  7197. (point-max))))
  7198. (bookmark-set "org-refile-last-stored")
  7199. (org-paste-subtree level))))
  7200. (org-cut-special)
  7201. (message "Entry refiled to \"%s\"" (car it)))))))
  7202. (defun org-refile-goto-last-stored ()
  7203. "Go to the location where the last refile was stored."
  7204. (interactive)
  7205. (bookmark-jump "org-refile-last-stored")
  7206. (message "This is the location of the last refile"))
  7207. (defun org-refile-get-location (&optional prompt default-buffer)
  7208. "Prompt the user for a refile location, using PROMPT."
  7209. (let ((org-refile-targets org-refile-targets)
  7210. (org-refile-use-outline-path org-refile-use-outline-path))
  7211. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7212. (unless org-refile-target-table
  7213. (error "No refile targets"))
  7214. (let* ((cbuf (current-buffer))
  7215. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7216. (fname (and filename (file-truename filename)))
  7217. (tbl (mapcar
  7218. (lambda (x)
  7219. (if (not (equal fname (file-truename (nth 1 x))))
  7220. (cons (concat (car x) " (" (file-name-nondirectory
  7221. (nth 1 x)) ")")
  7222. (cdr x))
  7223. x))
  7224. org-refile-target-table))
  7225. (completion-ignore-case t))
  7226. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  7227. tbl)))
  7228. ;;;; Dynamic blocks
  7229. (defun org-find-dblock (name)
  7230. "Find the first dynamic block with name NAME in the buffer.
  7231. If not found, stay at current position and return nil."
  7232. (let (pos)
  7233. (save-excursion
  7234. (goto-char (point-min))
  7235. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7236. nil t)
  7237. (match-beginning 0))))
  7238. (if pos (goto-char pos))
  7239. pos))
  7240. (defconst org-dblock-start-re
  7241. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7242. "Matches the startline of a dynamic block, with parameters.")
  7243. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7244. "Matches the end of a dyhamic block.")
  7245. (defun org-create-dblock (plist)
  7246. "Create a dynamic block section, with parameters taken from PLIST.
  7247. PLIST must containe a :name entry which is used as name of the block."
  7248. (unless (bolp) (newline))
  7249. (let ((name (plist-get plist :name)))
  7250. (insert "#+BEGIN: " name)
  7251. (while plist
  7252. (if (eq (car plist) :name)
  7253. (setq plist (cddr plist))
  7254. (insert " " (prin1-to-string (pop plist)))))
  7255. (insert "\n\n#+END:\n")
  7256. (beginning-of-line -2)))
  7257. (defun org-prepare-dblock ()
  7258. "Prepare dynamic block for refresh.
  7259. This empties the block, puts the cursor at the insert position and returns
  7260. the property list including an extra property :name with the block name."
  7261. (unless (looking-at org-dblock-start-re)
  7262. (error "Not at a dynamic block"))
  7263. (let* ((begdel (1+ (match-end 0)))
  7264. (name (org-no-properties (match-string 1)))
  7265. (params (append (list :name name)
  7266. (read (concat "(" (match-string 3) ")")))))
  7267. (unless (re-search-forward org-dblock-end-re nil t)
  7268. (error "Dynamic block not terminated"))
  7269. (setq params
  7270. (append params
  7271. (list :content (buffer-substring
  7272. begdel (match-beginning 0)))))
  7273. (delete-region begdel (match-beginning 0))
  7274. (goto-char begdel)
  7275. (open-line 1)
  7276. params))
  7277. (defun org-map-dblocks (&optional command)
  7278. "Apply COMMAND to all dynamic blocks in the current buffer.
  7279. If COMMAND is not given, use `org-update-dblock'."
  7280. (let ((cmd (or command 'org-update-dblock))
  7281. pos)
  7282. (save-excursion
  7283. (goto-char (point-min))
  7284. (while (re-search-forward org-dblock-start-re nil t)
  7285. (goto-char (setq pos (match-beginning 0)))
  7286. (condition-case nil
  7287. (funcall cmd)
  7288. (error (message "Error during update of dynamic block")))
  7289. (goto-char pos)
  7290. (unless (re-search-forward org-dblock-end-re nil t)
  7291. (error "Dynamic block not terminated"))))))
  7292. (defun org-dblock-update (&optional arg)
  7293. "User command for updating dynamic blocks.
  7294. Update the dynamic block at point. With prefix ARG, update all dynamic
  7295. blocks in the buffer."
  7296. (interactive "P")
  7297. (if arg
  7298. (org-update-all-dblocks)
  7299. (or (looking-at org-dblock-start-re)
  7300. (org-beginning-of-dblock))
  7301. (org-update-dblock)))
  7302. (defun org-update-dblock ()
  7303. "Update the dynamic block at point
  7304. This means to empty the block, parse for parameters and then call
  7305. the correct writing function."
  7306. (save-window-excursion
  7307. (let* ((pos (point))
  7308. (line (org-current-line))
  7309. (params (org-prepare-dblock))
  7310. (name (plist-get params :name))
  7311. (cmd (intern (concat "org-dblock-write:" name))))
  7312. (message "Updating dynamic block `%s' at line %d..." name line)
  7313. (funcall cmd params)
  7314. (message "Updating dynamic block `%s' at line %d...done" name line)
  7315. (goto-char pos))))
  7316. (defun org-beginning-of-dblock ()
  7317. "Find the beginning of the dynamic block at point.
  7318. Error if there is no scuh block at point."
  7319. (let ((pos (point))
  7320. beg)
  7321. (end-of-line 1)
  7322. (if (and (re-search-backward org-dblock-start-re nil t)
  7323. (setq beg (match-beginning 0))
  7324. (re-search-forward org-dblock-end-re nil t)
  7325. (> (match-end 0) pos))
  7326. (goto-char beg)
  7327. (goto-char pos)
  7328. (error "Not in a dynamic block"))))
  7329. (defun org-update-all-dblocks ()
  7330. "Update all dynamic blocks in the buffer.
  7331. This function can be used in a hook."
  7332. (when (org-mode-p)
  7333. (org-map-dblocks 'org-update-dblock)))
  7334. ;;;; Completion
  7335. (defconst org-additional-option-like-keywords
  7336. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7337. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7338. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7339. (defun org-complete (&optional arg)
  7340. "Perform completion on word at point.
  7341. At the beginning of a headline, this completes TODO keywords as given in
  7342. `org-todo-keywords'.
  7343. If the current word is preceded by a backslash, completes the TeX symbols
  7344. that are supported for HTML support.
  7345. If the current word is preceded by \"#+\", completes special words for
  7346. setting file options.
  7347. In the line after \"#+STARTUP:, complete valid keywords.\"
  7348. At all other locations, this simply calls the value of
  7349. `org-completion-fallback-command'."
  7350. (interactive "P")
  7351. (org-without-partial-completion
  7352. (catch 'exit
  7353. (let* ((end (point))
  7354. (beg1 (save-excursion
  7355. (skip-chars-backward (org-re "[:alnum:]_@"))
  7356. (point)))
  7357. (beg (save-excursion
  7358. (skip-chars-backward "a-zA-Z0-9_:$")
  7359. (point)))
  7360. (confirm (lambda (x) (stringp (car x))))
  7361. (searchhead (equal (char-before beg) ?*))
  7362. (tag (and (equal (char-before beg1) ?:)
  7363. (equal (char-after (point-at-bol)) ?*)))
  7364. (prop (and (equal (char-before beg1) ?:)
  7365. (not (equal (char-after (point-at-bol)) ?*))))
  7366. (texp (equal (char-before beg) ?\\))
  7367. (link (equal (char-before beg) ?\[))
  7368. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7369. beg)
  7370. "#+"))
  7371. (startup (string-match "^#\\+STARTUP:.*"
  7372. (buffer-substring (point-at-bol) (point))))
  7373. (completion-ignore-case opt)
  7374. (type nil)
  7375. (tbl nil)
  7376. (table (cond
  7377. (opt
  7378. (setq type :opt)
  7379. (require 'org-exp)
  7380. (append
  7381. (mapcar
  7382. (lambda (x)
  7383. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7384. (cons (match-string 2 x) (match-string 1 x)))
  7385. (org-split-string (org-get-current-options) "\n"))
  7386. (mapcar 'list org-additional-option-like-keywords)))
  7387. (startup
  7388. (setq type :startup)
  7389. org-startup-options)
  7390. (link (append org-link-abbrev-alist-local
  7391. org-link-abbrev-alist))
  7392. (texp
  7393. (setq type :tex)
  7394. org-html-entities)
  7395. ((string-match "\\`\\*+[ \t]+\\'"
  7396. (buffer-substring (point-at-bol) beg))
  7397. (setq type :todo)
  7398. (mapcar 'list org-todo-keywords-1))
  7399. (searchhead
  7400. (setq type :searchhead)
  7401. (save-excursion
  7402. (goto-char (point-min))
  7403. (while (re-search-forward org-todo-line-regexp nil t)
  7404. (push (list
  7405. (org-make-org-heading-search-string
  7406. (match-string 3) t))
  7407. tbl)))
  7408. tbl)
  7409. (tag (setq type :tag beg beg1)
  7410. (or org-tag-alist (org-get-buffer-tags)))
  7411. (prop (setq type :prop beg beg1)
  7412. (mapcar 'list (org-buffer-property-keys nil t t)))
  7413. (t (progn
  7414. (call-interactively org-completion-fallback-command)
  7415. (throw 'exit nil)))))
  7416. (pattern (buffer-substring-no-properties beg end))
  7417. (completion (try-completion pattern table confirm)))
  7418. (cond ((eq completion t)
  7419. (if (not (assoc (upcase pattern) table))
  7420. (message "Already complete")
  7421. (if (and (equal type :opt)
  7422. (not (member (car (assoc (upcase pattern) table))
  7423. org-additional-option-like-keywords)))
  7424. (insert (substring (cdr (assoc (upcase pattern) table))
  7425. (length pattern)))
  7426. (if (memq type '(:tag :prop)) (insert ":")))))
  7427. ((null completion)
  7428. (message "Can't find completion for \"%s\"" pattern)
  7429. (ding))
  7430. ((not (string= pattern completion))
  7431. (delete-region beg end)
  7432. (if (string-match " +$" completion)
  7433. (setq completion (replace-match "" t t completion)))
  7434. (insert completion)
  7435. (if (get-buffer-window "*Completions*")
  7436. (delete-window (get-buffer-window "*Completions*")))
  7437. (if (assoc completion table)
  7438. (if (eq type :todo) (insert " ")
  7439. (if (memq type '(:tag :prop)) (insert ":"))))
  7440. (if (and (equal type :opt) (assoc completion table))
  7441. (message "%s" (substitute-command-keys
  7442. "Press \\[org-complete] again to insert example settings"))))
  7443. (t
  7444. (message "Making completion list...")
  7445. (let ((list (sort (all-completions pattern table confirm)
  7446. 'string<)))
  7447. (with-output-to-temp-buffer "*Completions*"
  7448. (condition-case nil
  7449. ;; Protection needed for XEmacs and emacs 21
  7450. (display-completion-list list pattern)
  7451. (error (display-completion-list list)))))
  7452. (message "Making completion list...%s" "done")))))))
  7453. ;;;; TODO, DEADLINE, Comments
  7454. (defun org-toggle-comment ()
  7455. "Change the COMMENT state of an entry."
  7456. (interactive)
  7457. (save-excursion
  7458. (org-back-to-heading)
  7459. (let (case-fold-search)
  7460. (if (looking-at (concat outline-regexp
  7461. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7462. (replace-match "" t t nil 1)
  7463. (if (looking-at outline-regexp)
  7464. (progn
  7465. (goto-char (match-end 0))
  7466. (insert org-comment-string " ")))))))
  7467. (defvar org-last-todo-state-is-todo nil
  7468. "This is non-nil when the last TODO state change led to a TODO state.
  7469. If the last change removed the TODO tag or switched to DONE, then
  7470. this is nil.")
  7471. (defvar org-setting-tags nil) ; dynamically skiped
  7472. (defun org-parse-local-options (string var)
  7473. "Parse STRING for startup setting relevant for variable VAR."
  7474. (let ((rtn (symbol-value var))
  7475. e opts)
  7476. (save-match-data
  7477. (if (or (not string) (not (string-match "\\S-" string)))
  7478. rtn
  7479. (setq opts (delq nil (mapcar (lambda (x)
  7480. (setq e (assoc x org-startup-options))
  7481. (if (eq (nth 1 e) var) e nil))
  7482. (org-split-string string "[ \t]+"))))
  7483. (if (not opts)
  7484. rtn
  7485. (setq rtn nil)
  7486. (while (setq e (pop opts))
  7487. (if (not (nth 3 e))
  7488. (setq rtn (nth 2 e))
  7489. (if (not (listp rtn)) (setq rtn nil))
  7490. (push (nth 2 e) rtn)))
  7491. rtn)))))
  7492. (defvar org-blocker-hook nil
  7493. "Hook for functions that are allowed to block a state change.
  7494. Each function gets as its single argument a property list, see
  7495. `org-trigger-hook' for more information about this list.
  7496. If any of the functions in this hook returns nil, the state change
  7497. is blocked.")
  7498. (defvar org-trigger-hook nil
  7499. "Hook for functions that are triggered by a state change.
  7500. Each function gets as its single argument a property list with at least
  7501. the following elements:
  7502. (:type type-of-change :position pos-at-entry-start
  7503. :from old-state :to new-state)
  7504. Depending on the type, more properties may be present.
  7505. This mechanism is currently implemented for:
  7506. TODO state changes
  7507. ------------------
  7508. :type todo-state-change
  7509. :from previous state (keyword as a string), or nil
  7510. :to new state (keyword as a string), or nil")
  7511. (defun org-todo (&optional arg)
  7512. "Change the TODO state of an item.
  7513. The state of an item is given by a keyword at the start of the heading,
  7514. like
  7515. *** TODO Write paper
  7516. *** DONE Call mom
  7517. The different keywords are specified in the variable `org-todo-keywords'.
  7518. By default the available states are \"TODO\" and \"DONE\".
  7519. So for this example: when the item starts with TODO, it is changed to DONE.
  7520. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7521. DONE are present, add TODO at the beginning of the heading.
  7522. With C-u prefix arg, use completion to determine the new state.
  7523. With numeric prefix arg, switch to that state.
  7524. For calling through lisp, arg is also interpreted in the following way:
  7525. 'none -> empty state
  7526. \"\"(empty string) -> switch to empty state
  7527. 'done -> switch to DONE
  7528. 'nextset -> switch to the next set of keywords
  7529. 'previousset -> switch to the previous set of keywords
  7530. \"WAITING\" -> switch to the specified keyword, but only if it
  7531. really is a member of `org-todo-keywords'."
  7532. (interactive "P")
  7533. (save-excursion
  7534. (catch 'exit
  7535. (org-back-to-heading)
  7536. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7537. (or (looking-at (concat " +" org-todo-regexp " *"))
  7538. (looking-at " *"))
  7539. (let* ((match-data (match-data))
  7540. (startpos (point-at-bol))
  7541. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7542. (org-log-done org-log-done)
  7543. (org-log-repeat org-log-repeat)
  7544. (org-todo-log-states org-todo-log-states)
  7545. (this (match-string 1))
  7546. (hl-pos (match-beginning 0))
  7547. (head (org-get-todo-sequence-head this))
  7548. (ass (assoc head org-todo-kwd-alist))
  7549. (interpret (nth 1 ass))
  7550. (done-word (nth 3 ass))
  7551. (final-done-word (nth 4 ass))
  7552. (last-state (or this ""))
  7553. (completion-ignore-case t)
  7554. (member (member this org-todo-keywords-1))
  7555. (tail (cdr member))
  7556. (state (cond
  7557. ((and org-todo-key-trigger
  7558. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7559. (and (not arg) org-use-fast-todo-selection
  7560. (not (eq org-use-fast-todo-selection 'prefix)))))
  7561. ;; Use fast selection
  7562. (org-fast-todo-selection))
  7563. ((and (equal arg '(4))
  7564. (or (not org-use-fast-todo-selection)
  7565. (not org-todo-key-trigger)))
  7566. ;; Read a state with completion
  7567. (completing-read "State: " (mapcar (lambda(x) (list x))
  7568. org-todo-keywords-1)
  7569. nil t))
  7570. ((eq arg 'right)
  7571. (if this
  7572. (if tail (car tail) nil)
  7573. (car org-todo-keywords-1)))
  7574. ((eq arg 'left)
  7575. (if (equal member org-todo-keywords-1)
  7576. nil
  7577. (if this
  7578. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7579. org-todo-keywords-1)
  7580. (org-last org-todo-keywords-1))))
  7581. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7582. (setq arg nil))) ; hack to fall back to cycling
  7583. (arg
  7584. ;; user or caller requests a specific state
  7585. (cond
  7586. ((equal arg "") nil)
  7587. ((eq arg 'none) nil)
  7588. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7589. ((eq arg 'nextset)
  7590. (or (car (cdr (member head org-todo-heads)))
  7591. (car org-todo-heads)))
  7592. ((eq arg 'previousset)
  7593. (let ((org-todo-heads (reverse org-todo-heads)))
  7594. (or (car (cdr (member head org-todo-heads)))
  7595. (car org-todo-heads))))
  7596. ((car (member arg org-todo-keywords-1)))
  7597. ((nth (1- (prefix-numeric-value arg))
  7598. org-todo-keywords-1))))
  7599. ((null member) (or head (car org-todo-keywords-1)))
  7600. ((equal this final-done-word) nil) ;; -> make empty
  7601. ((null tail) nil) ;; -> first entry
  7602. ((eq interpret 'sequence)
  7603. (car tail))
  7604. ((memq interpret '(type priority))
  7605. (if (eq this-command last-command)
  7606. (car tail)
  7607. (if (> (length tail) 0)
  7608. (or done-word (car org-done-keywords))
  7609. nil)))
  7610. (t nil)))
  7611. (next (if state (concat " " state " ") " "))
  7612. (change-plist (list :type 'todo-state-change :from this :to state
  7613. :position startpos))
  7614. dolog now-done-p)
  7615. (when org-blocker-hook
  7616. (unless (save-excursion
  7617. (save-match-data
  7618. (run-hook-with-args-until-failure
  7619. 'org-blocker-hook change-plist)))
  7620. (if (interactive-p)
  7621. (error "TODO state change from %s to %s blocked" this state)
  7622. ;; fail silently
  7623. (message "TODO state change from %s to %s blocked" this state)
  7624. (throw 'exit nil))))
  7625. (store-match-data match-data)
  7626. (replace-match next t t)
  7627. (unless (pos-visible-in-window-p hl-pos)
  7628. (message "TODO state changed to %s" (org-trim next)))
  7629. (unless head
  7630. (setq head (org-get-todo-sequence-head state)
  7631. ass (assoc head org-todo-kwd-alist)
  7632. interpret (nth 1 ass)
  7633. done-word (nth 3 ass)
  7634. final-done-word (nth 4 ass)))
  7635. (when (memq arg '(nextset previousset))
  7636. (message "Keyword-Set %d/%d: %s"
  7637. (- (length org-todo-sets) -1
  7638. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7639. (length org-todo-sets)
  7640. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7641. (setq org-last-todo-state-is-todo
  7642. (not (member state org-done-keywords)))
  7643. (setq now-done-p (and (member state org-done-keywords)
  7644. (not (member this org-done-keywords))))
  7645. (and logging (org-local-logging logging))
  7646. (when (and (or org-todo-log-states org-log-done)
  7647. (not (memq arg '(nextset previousset))))
  7648. ;; we need to look at recording a time and note
  7649. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7650. (nth 2 (assoc this org-todo-log-states))))
  7651. (when (and state
  7652. (member state org-not-done-keywords)
  7653. (not (member this org-not-done-keywords)))
  7654. ;; This is now a todo state and was not one before
  7655. ;; If there was a CLOSED time stamp, get rid of it.
  7656. (org-add-planning-info nil nil 'closed))
  7657. (when (and now-done-p org-log-done)
  7658. ;; It is now done, and it was not done before
  7659. (org-add-planning-info 'closed (org-current-time))
  7660. (if (and (not dolog) (eq 'note org-log-done))
  7661. (org-add-log-setup 'done state 'findpos 'note)))
  7662. (when (and state dolog)
  7663. ;; This is a non-nil state, and we need to log it
  7664. (org-add-log-setup 'state state 'findpos dolog)))
  7665. ;; Fixup tag positioning
  7666. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7667. (run-hooks 'org-after-todo-state-change-hook)
  7668. (if (and arg (not (member state org-done-keywords)))
  7669. (setq head (org-get-todo-sequence-head state)))
  7670. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7671. ;; Do we need to trigger a repeat?
  7672. (when now-done-p (org-auto-repeat-maybe state))
  7673. ;; Fixup cursor location if close to the keyword
  7674. (if (and (outline-on-heading-p)
  7675. (not (bolp))
  7676. (save-excursion (beginning-of-line 1)
  7677. (looking-at org-todo-line-regexp))
  7678. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7679. (progn
  7680. (goto-char (or (match-end 2) (match-end 1)))
  7681. (just-one-space)))
  7682. (when org-trigger-hook
  7683. (save-excursion
  7684. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7685. (defun org-local-logging (value)
  7686. "Get logging settings from a property VALUE."
  7687. (let* (words w a)
  7688. ;; directly set the variables, they are already local.
  7689. (setq org-log-done nil
  7690. org-log-repeat nil
  7691. org-todo-log-states nil)
  7692. (setq words (org-split-string value))
  7693. (while (setq w (pop words))
  7694. (cond
  7695. ((setq a (assoc w org-startup-options))
  7696. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7697. (set (nth 1 a) (nth 2 a))))
  7698. ((setq a (org-extract-log-state-settings w))
  7699. (and (member (car a) org-todo-keywords-1)
  7700. (push a org-todo-log-states)))))))
  7701. (defun org-get-todo-sequence-head (kwd)
  7702. "Return the head of the TODO sequence to which KWD belongs.
  7703. If KWD is not set, check if there is a text property remembering the
  7704. right sequence."
  7705. (let (p)
  7706. (cond
  7707. ((not kwd)
  7708. (or (get-text-property (point-at-bol) 'org-todo-head)
  7709. (progn
  7710. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7711. nil (point-at-eol)))
  7712. (get-text-property p 'org-todo-head))))
  7713. ((not (member kwd org-todo-keywords-1))
  7714. (car org-todo-keywords-1))
  7715. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7716. (defun org-fast-todo-selection ()
  7717. "Fast TODO keyword selection with single keys.
  7718. Returns the new TODO keyword, or nil if no state change should occur."
  7719. (let* ((fulltable org-todo-key-alist)
  7720. (done-keywords org-done-keywords) ;; needed for the faces.
  7721. (maxlen (apply 'max (mapcar
  7722. (lambda (x)
  7723. (if (stringp (car x)) (string-width (car x)) 0))
  7724. fulltable)))
  7725. (expert nil)
  7726. (fwidth (+ maxlen 3 1 3))
  7727. (ncol (/ (- (window-width) 4) fwidth))
  7728. tg cnt e c tbl
  7729. groups ingroup)
  7730. (save-window-excursion
  7731. (if expert
  7732. (set-buffer (get-buffer-create " *Org todo*"))
  7733. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7734. (erase-buffer)
  7735. (org-set-local 'org-done-keywords done-keywords)
  7736. (setq tbl fulltable cnt 0)
  7737. (while (setq e (pop tbl))
  7738. (cond
  7739. ((equal e '(:startgroup))
  7740. (push '() groups) (setq ingroup t)
  7741. (when (not (= cnt 0))
  7742. (setq cnt 0)
  7743. (insert "\n"))
  7744. (insert "{ "))
  7745. ((equal e '(:endgroup))
  7746. (setq ingroup nil cnt 0)
  7747. (insert "}\n"))
  7748. (t
  7749. (setq tg (car e) c (cdr e))
  7750. (if ingroup (push tg (car groups)))
  7751. (setq tg (org-add-props tg nil 'face
  7752. (org-get-todo-face tg)))
  7753. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7754. (insert "[" c "] " tg (make-string
  7755. (- fwidth 4 (length tg)) ?\ ))
  7756. (when (= (setq cnt (1+ cnt)) ncol)
  7757. (insert "\n")
  7758. (if ingroup (insert " "))
  7759. (setq cnt 0)))))
  7760. (insert "\n")
  7761. (goto-char (point-min))
  7762. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7763. (fit-window-to-buffer))
  7764. (message "[a-z..]:Set [SPC]:clear")
  7765. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7766. (cond
  7767. ((or (= c ?\C-g)
  7768. (and (= c ?q) (not (rassoc c fulltable))))
  7769. (setq quit-flag t))
  7770. ((= c ?\ ) nil)
  7771. ((setq e (rassoc c fulltable) tg (car e))
  7772. tg)
  7773. (t (setq quit-flag t))))))
  7774. (defun org-entry-is-todo-p ()
  7775. (member (org-get-todo-state) org-not-done-keywords))
  7776. (defun org-entry-is-done-p ()
  7777. (member (org-get-todo-state) org-done-keywords))
  7778. (defun org-get-todo-state ()
  7779. (save-excursion
  7780. (org-back-to-heading t)
  7781. (and (looking-at org-todo-line-regexp)
  7782. (match-end 2)
  7783. (match-string 2))))
  7784. (defun org-at-date-range-p (&optional inactive-ok)
  7785. "Is the cursor inside a date range?"
  7786. (interactive)
  7787. (save-excursion
  7788. (catch 'exit
  7789. (let ((pos (point)))
  7790. (skip-chars-backward "^[<\r\n")
  7791. (skip-chars-backward "<[")
  7792. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7793. (>= (match-end 0) pos)
  7794. (throw 'exit t))
  7795. (skip-chars-backward "^<[\r\n")
  7796. (skip-chars-backward "<[")
  7797. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7798. (>= (match-end 0) pos)
  7799. (throw 'exit t)))
  7800. nil)))
  7801. (defun org-get-repeat ()
  7802. "Check if tere is a deadline/schedule with repeater in this entry."
  7803. (save-match-data
  7804. (save-excursion
  7805. (org-back-to-heading t)
  7806. (if (re-search-forward
  7807. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7808. (match-string 1)))))
  7809. (defvar org-last-changed-timestamp)
  7810. (defvar org-log-post-message)
  7811. (defvar org-log-note-purpose)
  7812. (defvar org-log-note-how)
  7813. (defun org-auto-repeat-maybe (done-word)
  7814. "Check if the current headline contains a repeated deadline/schedule.
  7815. If yes, set TODO state back to what it was and change the base date
  7816. of repeating deadline/scheduled time stamps to new date.
  7817. This function is run automatically after each state change to a DONE state."
  7818. ;; last-state is dynamically scoped into this function
  7819. (let* ((repeat (org-get-repeat))
  7820. (aa (assoc last-state org-todo-kwd-alist))
  7821. (interpret (nth 1 aa))
  7822. (head (nth 2 aa))
  7823. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7824. (msg "Entry repeats: ")
  7825. (org-log-done nil)
  7826. (org-todo-log-states nil)
  7827. (nshiftmax 10) (nshift 0)
  7828. re type n what ts mb0 time)
  7829. (when repeat
  7830. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7831. (org-todo (if (eq interpret 'type) last-state head))
  7832. (when org-log-repeat
  7833. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7834. (memq 'org-add-log-note post-command-hook))
  7835. ;; OK, we are already setup for some record
  7836. (if (eq org-log-repeat 'note)
  7837. ;; make sure we take a note, not only a time stamp
  7838. (setq org-log-note-how 'note))
  7839. ;; Set up for taking a record
  7840. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7841. 'findpos org-log-repeat)))
  7842. (org-back-to-heading t)
  7843. (org-add-planning-info nil nil 'closed)
  7844. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7845. org-deadline-time-regexp "\\)\\|\\("
  7846. org-ts-regexp "\\)"))
  7847. (while (re-search-forward
  7848. re (save-excursion (outline-next-heading) (point)) t)
  7849. (setq type (if (match-end 1) org-scheduled-string
  7850. (if (match-end 3) org-deadline-string "Plain:"))
  7851. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7852. mb0 (match-beginning 0))
  7853. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7854. (setq n (string-to-number (match-string 2 ts))
  7855. what (match-string 3 ts))
  7856. (if (equal what "w") (setq n (* n 7) what "d"))
  7857. ;; Preparation, see if we need to modify the start date for the change
  7858. (when (match-end 1)
  7859. (setq time (save-match-data (org-time-string-to-time ts)))
  7860. (cond
  7861. ((equal (match-string 1 ts) ".")
  7862. ;; Shift starting date to today
  7863. (org-timestamp-change
  7864. (- (time-to-days (current-time)) (time-to-days time))
  7865. 'day))
  7866. ((equal (match-string 1 ts) "+")
  7867. (while (or (= nshift 0)
  7868. (<= (time-to-days time) (time-to-days (current-time))))
  7869. (when (= (incf nshift) nshiftmax)
  7870. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7871. (error "Abort")))
  7872. (org-timestamp-change n (cdr (assoc what whata)))
  7873. (org-at-timestamp-p t)
  7874. (setq ts (match-string 1))
  7875. (setq time (save-match-data (org-time-string-to-time ts))))
  7876. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7877. ;; rematch, so that we have everything in place for the real shift
  7878. (org-at-timestamp-p t)
  7879. (setq ts (match-string 1))
  7880. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7881. (org-timestamp-change n (cdr (assoc what whata)))
  7882. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7883. (setq org-log-post-message msg)
  7884. (message "%s" msg))))
  7885. (defun org-show-todo-tree (arg)
  7886. "Make a compact tree which shows all headlines marked with TODO.
  7887. The tree will show the lines where the regexp matches, and all higher
  7888. headlines above the match.
  7889. With a \\[universal-argument] prefix, also show the DONE entries.
  7890. With a numeric prefix N, construct a sparse tree for the Nth element
  7891. of `org-todo-keywords-1'."
  7892. (interactive "P")
  7893. (let ((case-fold-search nil)
  7894. (kwd-re
  7895. (cond ((null arg) org-not-done-regexp)
  7896. ((equal arg '(4))
  7897. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7898. (mapcar 'list org-todo-keywords-1))))
  7899. (concat "\\("
  7900. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7901. "\\)\\>")))
  7902. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7903. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7904. org-todo-keywords-1)))
  7905. (t (error "Invalid prefix argument: %s" arg)))))
  7906. (message "%d TODO entries found"
  7907. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7908. (defun org-deadline (&optional remove)
  7909. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7910. With argument REMOVE, remove any deadline from the item."
  7911. (interactive "P")
  7912. (if remove
  7913. (progn
  7914. (org-remove-timestamp-with-keyword org-deadline-string)
  7915. (message "Item no longer has a deadline."))
  7916. (org-add-planning-info 'deadline nil 'closed)))
  7917. (defun org-schedule (&optional remove)
  7918. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7919. With argument REMOVE, remove any scheduling date from the item."
  7920. (interactive "P")
  7921. (if remove
  7922. (progn
  7923. (org-remove-timestamp-with-keyword org-scheduled-string)
  7924. (message "Item is no longer scheduled."))
  7925. (org-add-planning-info 'scheduled nil 'closed)))
  7926. (defun org-remove-timestamp-with-keyword (keyword)
  7927. "Remove all time stamps with KEYWORD in the current entry."
  7928. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7929. beg)
  7930. (save-excursion
  7931. (org-back-to-heading t)
  7932. (setq beg (point))
  7933. (org-end-of-subtree t t)
  7934. (while (re-search-backward re beg t)
  7935. (replace-match "")
  7936. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7937. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7938. (defun org-add-planning-info (what &optional time &rest remove)
  7939. "Insert new timestamp with keyword in the line directly after the headline.
  7940. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7941. If non is given, the user is prompted for a date.
  7942. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7943. be removed."
  7944. (interactive)
  7945. (let (org-time-was-given org-end-time-was-given ts
  7946. end default-time default-input)
  7947. (when (and (not time) (memq what '(scheduled deadline)))
  7948. ;; Try to get a default date/time from existing timestamp
  7949. (save-excursion
  7950. (org-back-to-heading t)
  7951. (setq end (save-excursion (outline-next-heading) (point)))
  7952. (when (re-search-forward (if (eq what 'scheduled)
  7953. org-scheduled-time-regexp
  7954. org-deadline-time-regexp)
  7955. end t)
  7956. (setq ts (match-string 1)
  7957. default-time
  7958. (apply 'encode-time (org-parse-time-string ts))
  7959. default-input (and ts (org-get-compact-tod ts))))))
  7960. (when what
  7961. ;; If necessary, get the time from the user
  7962. (setq time (or time (org-read-date nil 'to-time nil nil
  7963. default-time default-input))))
  7964. (when (and org-insert-labeled-timestamps-at-point
  7965. (member what '(scheduled deadline)))
  7966. (insert
  7967. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7968. (org-insert-time-stamp time org-time-was-given
  7969. nil nil nil (list org-end-time-was-given))
  7970. (setq what nil))
  7971. (save-excursion
  7972. (save-restriction
  7973. (let (col list elt ts buffer-invisibility-spec)
  7974. (org-back-to-heading t)
  7975. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7976. (goto-char (match-end 1))
  7977. (setq col (current-column))
  7978. (goto-char (match-end 0))
  7979. (if (eobp) (insert "\n") (forward-char 1))
  7980. (if (and (not (looking-at outline-regexp))
  7981. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7982. "[^\r\n]*"))
  7983. (not (equal (match-string 1) org-clock-string)))
  7984. (narrow-to-region (match-beginning 0) (match-end 0))
  7985. (insert-before-markers "\n")
  7986. (backward-char 1)
  7987. (narrow-to-region (point) (point))
  7988. (indent-to-column col))
  7989. ;; Check if we have to remove something.
  7990. (setq list (cons what remove))
  7991. (while list
  7992. (setq elt (pop list))
  7993. (goto-char (point-min))
  7994. (when (or (and (eq elt 'scheduled)
  7995. (re-search-forward org-scheduled-time-regexp nil t))
  7996. (and (eq elt 'deadline)
  7997. (re-search-forward org-deadline-time-regexp nil t))
  7998. (and (eq elt 'closed)
  7999. (re-search-forward org-closed-time-regexp nil t)))
  8000. (replace-match "")
  8001. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8002. (if (looking-at " +") (replace-match ""))))
  8003. (goto-char (point-max))
  8004. (when what
  8005. (insert
  8006. (if (not (equal (char-before) ?\ )) " " "")
  8007. (cond ((eq what 'scheduled) org-scheduled-string)
  8008. ((eq what 'deadline) org-deadline-string)
  8009. ((eq what 'closed) org-closed-string))
  8010. " ")
  8011. (setq ts (org-insert-time-stamp
  8012. time
  8013. (or org-time-was-given
  8014. (and (eq what 'closed) org-log-done-with-time))
  8015. (eq what 'closed)
  8016. nil nil (list org-end-time-was-given)))
  8017. (end-of-line 1))
  8018. (goto-char (point-min))
  8019. (widen)
  8020. (if (looking-at "[ \t]+\r?\n")
  8021. (replace-match ""))
  8022. ts)))))
  8023. (defvar org-log-note-marker (make-marker))
  8024. (defvar org-log-note-purpose nil)
  8025. (defvar org-log-note-state nil)
  8026. (defvar org-log-note-how nil)
  8027. (defvar org-log-note-window-configuration nil)
  8028. (defvar org-log-note-return-to (make-marker))
  8029. (defvar org-log-post-message nil
  8030. "Message to be displayed after a log note has been stored.
  8031. The auto-repeater uses this.")
  8032. (defun org-add-note ()
  8033. "Add a note to the current entry.
  8034. This is done in the same way as adding a state change note."
  8035. (interactive)
  8036. (org-add-log-setup 'note nil t nil))
  8037. (defun org-add-log-setup (&optional purpose state findpos how)
  8038. "Set up the post command hook to take a note.
  8039. If this is about to TODO state change, the new state is expected in STATE.
  8040. When FINDPOS is non-nil, find the correct position for the note in
  8041. the current entry. If not, assume that it can be inserted at point."
  8042. (save-excursion
  8043. (when findpos
  8044. (org-back-to-heading t)
  8045. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8046. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8047. "[^\r\n]*\\)?"))
  8048. (goto-char (match-end 0))
  8049. (unless org-log-states-order-reversed
  8050. (and (= (char-after) ?\n) (forward-char 1))
  8051. (org-skip-over-state-notes)
  8052. (skip-chars-backward " \t\n\r")))
  8053. (move-marker org-log-note-marker (point))
  8054. (setq org-log-note-purpose purpose
  8055. org-log-note-state state
  8056. org-log-note-how how)
  8057. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8058. (defun org-skip-over-state-notes ()
  8059. "Skip past the list of State notes in an entry."
  8060. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8061. (while (looking-at "[ \t]*- State")
  8062. (condition-case nil
  8063. (org-next-item)
  8064. (error (org-end-of-item)))))
  8065. (defun org-add-log-note (&optional purpose)
  8066. "Pop up a window for taking a note, and add this note later at point."
  8067. (remove-hook 'post-command-hook 'org-add-log-note)
  8068. (setq org-log-note-window-configuration (current-window-configuration))
  8069. (delete-other-windows)
  8070. (move-marker org-log-note-return-to (point))
  8071. (switch-to-buffer (marker-buffer org-log-note-marker))
  8072. (goto-char org-log-note-marker)
  8073. (org-switch-to-buffer-other-window "*Org Note*")
  8074. (erase-buffer)
  8075. (if (memq org-log-note-how '(time state))
  8076. (org-store-log-note)
  8077. (let ((org-inhibit-startup t)) (org-mode))
  8078. (insert (format "# Insert note for %s.
  8079. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8080. (cond
  8081. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8082. ((eq org-log-note-purpose 'done) "closed todo item")
  8083. ((eq org-log-note-purpose 'state)
  8084. (format "state change to \"%s\"" org-log-note-state))
  8085. ((eq org-log-note-purpose 'note)
  8086. "this entry")
  8087. (t (error "This should not happen")))))
  8088. (org-set-local 'org-finish-function 'org-store-log-note)))
  8089. (defvar org-note-abort nil) ; dynamically scoped
  8090. (defun org-store-log-note ()
  8091. "Finish taking a log note, and insert it to where it belongs."
  8092. (let ((txt (buffer-string))
  8093. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8094. lines ind)
  8095. (kill-buffer (current-buffer))
  8096. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8097. (setq txt (replace-match "" t t txt)))
  8098. (if (string-match "\\s-+\\'" txt)
  8099. (setq txt (replace-match "" t t txt)))
  8100. (setq lines (org-split-string txt "\n"))
  8101. (when (and note (string-match "\\S-" note))
  8102. (setq note
  8103. (org-replace-escapes
  8104. note
  8105. (list (cons "%u" (user-login-name))
  8106. (cons "%U" user-full-name)
  8107. (cons "%t" (format-time-string
  8108. (org-time-stamp-format 'long 'inactive)
  8109. (current-time)))
  8110. (cons "%s" (if org-log-note-state
  8111. (concat "\"" org-log-note-state "\"")
  8112. "")))))
  8113. (if lines (setq note (concat note " \\\\")))
  8114. (push note lines))
  8115. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8116. (when lines
  8117. (save-excursion
  8118. (set-buffer (marker-buffer org-log-note-marker))
  8119. (save-excursion
  8120. (goto-char org-log-note-marker)
  8121. (move-marker org-log-note-marker nil)
  8122. (end-of-line 1)
  8123. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8124. (indent-relative nil)
  8125. (insert "- " (pop lines))
  8126. (org-indent-line-function)
  8127. (beginning-of-line 1)
  8128. (looking-at "[ \t]*")
  8129. (setq ind (concat (match-string 0) " "))
  8130. (end-of-line 1)
  8131. (while lines (insert "\n" ind (pop lines)))))))
  8132. (set-window-configuration org-log-note-window-configuration)
  8133. (with-current-buffer (marker-buffer org-log-note-return-to)
  8134. (goto-char org-log-note-return-to))
  8135. (move-marker org-log-note-return-to nil)
  8136. (and org-log-post-message (message "%s" org-log-post-message)))
  8137. (defun org-sparse-tree (&optional arg)
  8138. "Create a sparse tree, prompt for the details.
  8139. This command can create sparse trees. You first need to select the type
  8140. of match used to create the tree:
  8141. t Show entries with a specific TODO keyword.
  8142. T Show entries selected by a tags match.
  8143. p Enter a property name and its value (both with completion on existing
  8144. names/values) and show entries with that property.
  8145. r Show entries matching a regular expression
  8146. d Show deadlines due within `org-deadline-warning-days'."
  8147. (interactive "P")
  8148. (let (ans kwd value)
  8149. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8150. (setq ans (read-char-exclusive))
  8151. (cond
  8152. ((equal ans ?d)
  8153. (call-interactively 'org-check-deadlines))
  8154. ((equal ans ?b)
  8155. (call-interactively 'org-check-before-date))
  8156. ((equal ans ?t)
  8157. (org-show-todo-tree '(4)))
  8158. ((equal ans ?T)
  8159. (call-interactively 'org-tags-sparse-tree))
  8160. ((member ans '(?p ?P))
  8161. (setq kwd (completing-read "Property: "
  8162. (mapcar 'list (org-buffer-property-keys))))
  8163. (setq value (completing-read "Value: "
  8164. (mapcar 'list (org-property-values kwd))))
  8165. (unless (string-match "\\`{.*}\\'" value)
  8166. (setq value (concat "\"" value "\"")))
  8167. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8168. ((member ans '(?r ?R ?/))
  8169. (call-interactively 'org-occur))
  8170. (t (error "No such sparse tree command \"%c\"" ans)))))
  8171. (defvar org-occur-highlights nil
  8172. "List of overlays used for occur matches.")
  8173. (make-variable-buffer-local 'org-occur-highlights)
  8174. (defvar org-occur-parameters nil
  8175. "Parameters of the active org-occur calls.
  8176. This is a list, each call to org-occur pushes as cons cell,
  8177. containing the regular expression and the callback, onto the list.
  8178. The list can contain several entries if `org-occur' has been called
  8179. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8180. will only contain one set of parameters. When the highlights are
  8181. removed (for example with `C-c C-c', or with the next edit (depending
  8182. on `org-remove-highlights-with-change'), this variable is emptied
  8183. as well.")
  8184. (make-variable-buffer-local 'org-occur-parameters)
  8185. (defun org-occur (regexp &optional keep-previous callback)
  8186. "Make a compact tree which shows all matches of REGEXP.
  8187. The tree will show the lines where the regexp matches, and all higher
  8188. headlines above the match. It will also show the heading after the match,
  8189. to make sure editing the matching entry is easy.
  8190. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8191. call to `org-occur' will be kept, to allow stacking of calls to this
  8192. command.
  8193. If CALLBACK is non-nil, it is a function which is called to confirm
  8194. that the match should indeed be shown."
  8195. (interactive "sRegexp: \nP")
  8196. (unless keep-previous
  8197. (org-remove-occur-highlights nil nil t))
  8198. (push (cons regexp callback) org-occur-parameters)
  8199. (let ((cnt 0))
  8200. (save-excursion
  8201. (goto-char (point-min))
  8202. (if (or (not keep-previous) ; do not want to keep
  8203. (not org-occur-highlights)) ; no previous matches
  8204. ;; hide everything
  8205. (org-overview))
  8206. (while (re-search-forward regexp nil t)
  8207. (when (or (not callback)
  8208. (save-match-data (funcall callback)))
  8209. (setq cnt (1+ cnt))
  8210. (when org-highlight-sparse-tree-matches
  8211. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8212. (org-show-context 'occur-tree))))
  8213. (when org-remove-highlights-with-change
  8214. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8215. nil 'local))
  8216. (unless org-sparse-tree-open-archived-trees
  8217. (org-hide-archived-subtrees (point-min) (point-max)))
  8218. (run-hooks 'org-occur-hook)
  8219. (if (interactive-p)
  8220. (message "%d match(es) for regexp %s" cnt regexp))
  8221. cnt))
  8222. (defun org-show-context (&optional key)
  8223. "Make sure point and context and visible.
  8224. How much context is shown depends upon the variables
  8225. `org-show-hierarchy-above', `org-show-following-heading'. and
  8226. `org-show-siblings'."
  8227. (let ((heading-p (org-on-heading-p t))
  8228. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8229. (following-p (org-get-alist-option org-show-following-heading key))
  8230. (entry-p (org-get-alist-option org-show-entry-below key))
  8231. (siblings-p (org-get-alist-option org-show-siblings key)))
  8232. (catch 'exit
  8233. ;; Show heading or entry text
  8234. (if (and heading-p (not entry-p))
  8235. (org-flag-heading nil) ; only show the heading
  8236. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8237. (org-show-hidden-entry))) ; show entire entry
  8238. (when following-p
  8239. ;; Show next sibling, or heading below text
  8240. (save-excursion
  8241. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8242. (org-flag-heading nil))))
  8243. (when siblings-p (org-show-siblings))
  8244. (when hierarchy-p
  8245. ;; show all higher headings, possibly with siblings
  8246. (save-excursion
  8247. (while (and (condition-case nil
  8248. (progn (org-up-heading-all 1) t)
  8249. (error nil))
  8250. (not (bobp)))
  8251. (org-flag-heading nil)
  8252. (when siblings-p (org-show-siblings))))))))
  8253. (defun org-reveal (&optional siblings)
  8254. "Show current entry, hierarchy above it, and the following headline.
  8255. This can be used to show a consistent set of context around locations
  8256. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8257. not t for the search context.
  8258. With optional argument SIBLINGS, on each level of the hierarchy all
  8259. siblings are shown. This repairs the tree structure to what it would
  8260. look like when opened with hierarchical calls to `org-cycle'."
  8261. (interactive "P")
  8262. (let ((org-show-hierarchy-above t)
  8263. (org-show-following-heading t)
  8264. (org-show-siblings (if siblings t org-show-siblings)))
  8265. (org-show-context nil)))
  8266. (defun org-highlight-new-match (beg end)
  8267. "Highlight from BEG to END and mark the highlight is an occur headline."
  8268. (let ((ov (org-make-overlay beg end)))
  8269. (org-overlay-put ov 'face 'secondary-selection)
  8270. (push ov org-occur-highlights)))
  8271. (defun org-remove-occur-highlights (&optional beg end noremove)
  8272. "Remove the occur highlights from the buffer.
  8273. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8274. from the `before-change-functions' in the current buffer."
  8275. (interactive)
  8276. (unless org-inhibit-highlight-removal
  8277. (mapc 'org-delete-overlay org-occur-highlights)
  8278. (setq org-occur-highlights nil)
  8279. (setq org-occur-parameters nil)
  8280. (unless noremove
  8281. (remove-hook 'before-change-functions
  8282. 'org-remove-occur-highlights 'local))))
  8283. ;;;; Priorities
  8284. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8285. "Regular expression matching the priority indicator.")
  8286. (defvar org-remove-priority-next-time nil)
  8287. (defun org-priority-up ()
  8288. "Increase the priority of the current item."
  8289. (interactive)
  8290. (org-priority 'up))
  8291. (defun org-priority-down ()
  8292. "Decrease the priority of the current item."
  8293. (interactive)
  8294. (org-priority 'down))
  8295. (defun org-priority (&optional action)
  8296. "Change the priority of an item by ARG.
  8297. ACTION can be `set', `up', `down', or a character."
  8298. (interactive)
  8299. (setq action (or action 'set))
  8300. (let (current new news have remove)
  8301. (save-excursion
  8302. (org-back-to-heading)
  8303. (if (looking-at org-priority-regexp)
  8304. (setq current (string-to-char (match-string 2))
  8305. have t)
  8306. (setq current org-default-priority))
  8307. (cond
  8308. ((or (eq action 'set) (integerp action))
  8309. (if (integerp action)
  8310. (setq new action)
  8311. (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
  8312. (setq new (read-char-exclusive)))
  8313. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8314. (= (upcase org-lowest-priority) org-lowest-priority))
  8315. (setq new (upcase new)))
  8316. (cond ((equal new ?\ ) (setq remove t))
  8317. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8318. (error "Priority must be between `%c' and `%c'"
  8319. org-highest-priority org-lowest-priority))))
  8320. ((eq action 'up)
  8321. (if (and (not have) (eq last-command this-command))
  8322. (setq new org-lowest-priority)
  8323. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8324. org-default-priority (1- current)))))
  8325. ((eq action 'down)
  8326. (if (and (not have) (eq last-command this-command))
  8327. (setq new org-highest-priority)
  8328. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8329. org-default-priority (1+ current)))))
  8330. (t (error "Invalid action")))
  8331. (if (or (< (upcase new) org-highest-priority)
  8332. (> (upcase new) org-lowest-priority))
  8333. (setq remove t))
  8334. (setq news (format "%c" new))
  8335. (if have
  8336. (if remove
  8337. (replace-match "" t t nil 1)
  8338. (replace-match news t t nil 2))
  8339. (if remove
  8340. (error "No priority cookie found in line")
  8341. (looking-at org-todo-line-regexp)
  8342. (if (match-end 2)
  8343. (progn
  8344. (goto-char (match-end 2))
  8345. (insert " [#" news "]"))
  8346. (goto-char (match-beginning 3))
  8347. (insert "[#" news "] ")))))
  8348. (org-preserve-lc (org-set-tags nil 'align))
  8349. (if remove
  8350. (message "Priority removed")
  8351. (message "Priority of current item set to %s" news))))
  8352. (defun org-get-priority (s)
  8353. "Find priority cookie and return priority."
  8354. (save-match-data
  8355. (if (not (string-match org-priority-regexp s))
  8356. (* 1000 (- org-lowest-priority org-default-priority))
  8357. (* 1000 (- org-lowest-priority
  8358. (string-to-char (match-string 2 s)))))))
  8359. ;;;; Tags
  8360. (defun org-scan-tags (action matcher &optional todo-only)
  8361. "Scan headline tags with inheritance and produce output ACTION.
  8362. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8363. evaluated, testing if a given set of tags qualifies a headline for
  8364. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8365. are included in the output."
  8366. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8367. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8368. (org-re
  8369. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8370. (props (list 'face nil
  8371. 'done-face 'org-done
  8372. 'undone-face nil
  8373. 'mouse-face 'highlight
  8374. 'org-not-done-regexp org-not-done-regexp
  8375. 'org-todo-regexp org-todo-regexp
  8376. 'keymap org-agenda-keymap
  8377. 'help-echo
  8378. (format "mouse-2 or RET jump to org file %s"
  8379. (abbreviate-file-name
  8380. (or (buffer-file-name (buffer-base-buffer))
  8381. (buffer-name (buffer-base-buffer)))))))
  8382. (case-fold-search nil)
  8383. lspos
  8384. tags tags-list tags-alist (llast 0) rtn level category i txt
  8385. todo marker entry priority)
  8386. (save-excursion
  8387. (goto-char (point-min))
  8388. (when (eq action 'sparse-tree)
  8389. (org-overview)
  8390. (org-remove-occur-highlights))
  8391. (while (re-search-forward re nil t)
  8392. (catch :skip
  8393. (setq todo (if (match-end 1) (match-string 2))
  8394. tags (if (match-end 4) (match-string 4)))
  8395. (goto-char (setq lspos (1+ (match-beginning 0))))
  8396. (setq level (org-reduced-level (funcall outline-level))
  8397. category (org-get-category))
  8398. (setq i llast llast level)
  8399. ;; remove tag lists from same and sublevels
  8400. (while (>= i level)
  8401. (when (setq entry (assoc i tags-alist))
  8402. (setq tags-alist (delete entry tags-alist)))
  8403. (setq i (1- i)))
  8404. ;; add the next tags
  8405. (when tags
  8406. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8407. tags-alist
  8408. (cons (cons level tags) tags-alist)))
  8409. ;; compile tags for current headline
  8410. (setq tags-list
  8411. (if org-use-tag-inheritance
  8412. (apply 'append (mapcar 'cdr tags-alist))
  8413. tags))
  8414. (when (and tags org-use-tag-inheritance
  8415. (not (eq t org-use-tag-inheritance)))
  8416. ;; selective inheritance, remove uninherited ones
  8417. (setcdr (car tags-alist)
  8418. (org-remove-uniherited-tags (cdar tags-alist))))
  8419. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8420. (eval matcher)
  8421. (or (not org-agenda-skip-archived-trees)
  8422. (not (member org-archive-tag tags-list))))
  8423. (and (eq action 'agenda) (org-agenda-skip))
  8424. ;; list this headline
  8425. (if (eq action 'sparse-tree)
  8426. (progn
  8427. (and org-highlight-sparse-tree-matches
  8428. (org-get-heading) (match-end 0)
  8429. (org-highlight-new-match
  8430. (match-beginning 0) (match-beginning 1)))
  8431. (org-show-context 'tags-tree))
  8432. (setq txt (org-format-agenda-item
  8433. ""
  8434. (concat
  8435. (if org-tags-match-list-sublevels
  8436. (make-string (1- level) ?.) "")
  8437. (org-get-heading))
  8438. category tags-list)
  8439. priority (org-get-priority txt))
  8440. (goto-char lspos)
  8441. (setq marker (org-agenda-new-marker))
  8442. (org-add-props txt props
  8443. 'org-marker marker 'org-hd-marker marker 'org-category category
  8444. 'priority priority 'type "tagsmatch")
  8445. (push txt rtn))
  8446. ;; if we are to skip sublevels, jump to end of subtree
  8447. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8448. (when (and (eq action 'sparse-tree)
  8449. (not org-sparse-tree-open-archived-trees))
  8450. (org-hide-archived-subtrees (point-min) (point-max)))
  8451. (nreverse rtn)))
  8452. (defun org-remove-uniherited-tags (tags)
  8453. "Remove all tags that are not inherited from the list TAGS."
  8454. (cond
  8455. ((eq org-use-tag-inheritance t) tags)
  8456. ((not org-use-tag-inheritance) nil)
  8457. ((stringp org-use-tag-inheritance)
  8458. (delq nil (mapcar
  8459. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8460. tags)))
  8461. ((listp org-use-tag-inheritance)
  8462. (org-delete-all org-use-tag-inheritance tags))))
  8463. (defvar todo-only) ;; dynamically scoped
  8464. (defun org-tags-sparse-tree (&optional todo-only match)
  8465. "Create a sparse tree according to tags string MATCH.
  8466. MATCH can contain positive and negative selection of tags, like
  8467. \"+WORK+URGENT-WITHBOSS\".
  8468. If optional argument TODO_ONLY is non-nil, only select lines that are
  8469. also TODO lines."
  8470. (interactive "P")
  8471. (org-prepare-agenda-buffers (list (current-buffer)))
  8472. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8473. (defvar org-cached-props nil)
  8474. (defun org-cached-entry-get (pom property)
  8475. (if (or (eq t org-use-property-inheritance)
  8476. (and (stringp org-use-property-inheritance)
  8477. (string-match org-use-property-inheritance property))
  8478. (and (listp org-use-property-inheritance)
  8479. (member property org-use-property-inheritance)))
  8480. ;; Caching is not possible, check it directly
  8481. (org-entry-get pom property 'inherit)
  8482. ;; Get all properties, so that we can do complicated checks easily
  8483. (cdr (assoc property (or org-cached-props
  8484. (setq org-cached-props
  8485. (org-entry-properties pom)))))))
  8486. (defun org-global-tags-completion-table (&optional files)
  8487. "Return the list of all tags in all agenda buffer/files."
  8488. (save-excursion
  8489. (org-uniquify
  8490. (delq nil
  8491. (apply 'append
  8492. (mapcar
  8493. (lambda (file)
  8494. (set-buffer (find-file-noselect file))
  8495. (append (org-get-buffer-tags)
  8496. (mapcar (lambda (x) (if (stringp (car-safe x))
  8497. (list (car-safe x)) nil))
  8498. org-tag-alist)))
  8499. (if (and files (car files))
  8500. files
  8501. (org-agenda-files))))))))
  8502. (defun org-make-tags-matcher (match)
  8503. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8504. ;; todo-only is scoped dynamically into this function, and the function
  8505. ;; may change it it the matcher asksk for it.
  8506. (unless match
  8507. ;; Get a new match request, with completion
  8508. (let ((org-last-tags-completion-table
  8509. (org-global-tags-completion-table)))
  8510. (setq match (completing-read
  8511. "Match: " 'org-tags-completion-function nil nil nil
  8512. 'org-tags-history))))
  8513. ;; Parse the string and create a lisp form
  8514. (let ((match0 match)
  8515. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  8516. minus tag mm
  8517. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8518. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  8519. (if (string-match "/+" match)
  8520. ;; match contains also a todo-matching request
  8521. (progn
  8522. (setq tagsmatch (substring match 0 (match-beginning 0))
  8523. todomatch (substring match (match-end 0)))
  8524. (if (string-match "^!" todomatch)
  8525. (setq todo-only t todomatch (substring todomatch 1)))
  8526. (if (string-match "^\\s-*$" todomatch)
  8527. (setq todomatch nil)))
  8528. ;; only matching tags
  8529. (setq tagsmatch match todomatch nil))
  8530. ;; Make the tags matcher
  8531. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8532. (setq tagsmatcher t)
  8533. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8534. (while (setq term (pop orterms))
  8535. (while (and (equal (substring term -1) "\\") orterms)
  8536. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8537. (while (string-match re term)
  8538. (setq minus (and (match-end 1)
  8539. (equal (match-string 1 term) "-"))
  8540. tag (match-string 2 term)
  8541. re-p (equal (string-to-char tag) ?{)
  8542. level-p (match-end 3)
  8543. prop-p (match-end 4)
  8544. mm (cond
  8545. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8546. (level-p `(= level ,(string-to-number
  8547. (match-string 3 term))))
  8548. (prop-p
  8549. (setq pn (match-string 4 term)
  8550. pv (match-string 5 term)
  8551. cat-p (equal pn "CATEGORY")
  8552. re-p (equal (string-to-char pv) ?{)
  8553. pv (substring pv 1 -1))
  8554. (if (equal pn "CATEGORY")
  8555. (setq gv '(get-text-property (point) 'org-category))
  8556. (setq gv `(org-cached-entry-get nil ,pn)))
  8557. (if re-p
  8558. `(string-match ,pv (or ,gv ""))
  8559. `(equal ,pv (or ,gv ""))))
  8560. (t `(member ,(downcase tag) tags-list)))
  8561. mm (if minus (list 'not mm) mm)
  8562. term (substring term (match-end 0)))
  8563. (push mm tagsmatcher))
  8564. (push (if (> (length tagsmatcher) 1)
  8565. (cons 'and tagsmatcher)
  8566. (car tagsmatcher))
  8567. orlist)
  8568. (setq tagsmatcher nil))
  8569. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8570. (setq tagsmatcher
  8571. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8572. ;; Make the todo matcher
  8573. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8574. (setq todomatcher t)
  8575. (setq orterms (org-split-string todomatch "|") orlist nil)
  8576. (while (setq term (pop orterms))
  8577. (while (string-match re term)
  8578. (setq minus (and (match-end 1)
  8579. (equal (match-string 1 term) "-"))
  8580. kwd (match-string 2 term)
  8581. re-p (equal (string-to-char kwd) ?{)
  8582. term (substring term (match-end 0))
  8583. mm (if re-p
  8584. `(string-match ,(substring kwd 1 -1) todo)
  8585. (list 'equal 'todo kwd))
  8586. mm (if minus (list 'not mm) mm))
  8587. (push mm todomatcher))
  8588. (push (if (> (length todomatcher) 1)
  8589. (cons 'and todomatcher)
  8590. (car todomatcher))
  8591. orlist)
  8592. (setq todomatcher nil))
  8593. (setq todomatcher (if (> (length orlist) 1)
  8594. (cons 'or orlist) (car orlist))))
  8595. ;; Return the string and lisp forms of the matcher
  8596. (setq matcher (if todomatcher
  8597. (list 'and tagsmatcher todomatcher)
  8598. tagsmatcher))
  8599. (cons match0 matcher)))
  8600. (defun org-match-any-p (re list)
  8601. "Does re match any element of list?"
  8602. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8603. (delq nil list))
  8604. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8605. (defvar org-tags-overlay (org-make-overlay 1 1))
  8606. (org-detach-overlay org-tags-overlay)
  8607. (defun org-get-tags-at (&optional pos)
  8608. "Get a list of all headline tags applicable at POS.
  8609. POS defaults to point. If tags are inherited, the list contains
  8610. the targets in the same sequence as the headlines appear, i.e.
  8611. sthe tags of the current headline come last."
  8612. (interactive)
  8613. (let (tags ltags lastpos parent)
  8614. (save-excursion
  8615. (save-restriction
  8616. (widen)
  8617. (goto-char (or pos (point)))
  8618. (save-match-data
  8619. (condition-case nil
  8620. (progn
  8621. (org-back-to-heading t)
  8622. (while (not (equal lastpos (point)))
  8623. (setq lastpos (point))
  8624. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8625. (setq ltags (org-split-string
  8626. (org-match-string-no-properties 1) ":"))
  8627. (setq tags (append (org-remove-uniherited-tags ltags)
  8628. tags)))
  8629. (or org-use-tag-inheritance (error ""))
  8630. (org-up-heading-all 1)
  8631. (setq parent t)))
  8632. (error nil))))
  8633. tags)))
  8634. (defun org-align-tags-here (to-col)
  8635. ;; Assumes that this is a headline
  8636. (let ((pos (point)) (col (current-column)) tags)
  8637. (beginning-of-line 1)
  8638. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8639. (< pos (match-beginning 2)))
  8640. (progn
  8641. (setq tags (match-string 2))
  8642. (goto-char (match-beginning 1))
  8643. (insert " ")
  8644. (delete-region (point) (1+ (match-end 0)))
  8645. (backward-char 1)
  8646. (move-to-column
  8647. (max (1+ (current-column))
  8648. (1+ col)
  8649. (if (> to-col 0)
  8650. to-col
  8651. (- (abs to-col) (length tags))))
  8652. t)
  8653. (insert tags)
  8654. (move-to-column (min (current-column) col) t))
  8655. (goto-char pos))))
  8656. (defun org-set-tags (&optional arg just-align)
  8657. "Set the tags for the current headline.
  8658. With prefix ARG, realign all tags in headings in the current buffer."
  8659. (interactive "P")
  8660. (let* ((re (concat "^" outline-regexp))
  8661. (current (org-get-tags-string))
  8662. (col (current-column))
  8663. (org-setting-tags t)
  8664. table current-tags inherited-tags ; computed below when needed
  8665. tags p0 c0 c1 rpl)
  8666. (if arg
  8667. (save-excursion
  8668. (goto-char (point-min))
  8669. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8670. (while (re-search-forward re nil t)
  8671. (org-set-tags nil t)
  8672. (end-of-line 1)))
  8673. (message "All tags realigned to column %d" org-tags-column))
  8674. (if just-align
  8675. (setq tags current)
  8676. ;; Get a new set of tags from the user
  8677. (save-excursion
  8678. (setq table (or org-tag-alist (org-get-buffer-tags))
  8679. org-last-tags-completion-table table
  8680. current-tags (org-split-string current ":")
  8681. inherited-tags (nreverse
  8682. (nthcdr (length current-tags)
  8683. (nreverse (org-get-tags-at))))
  8684. tags
  8685. (if (or (eq t org-use-fast-tag-selection)
  8686. (and org-use-fast-tag-selection
  8687. (delq nil (mapcar 'cdr table))))
  8688. (org-fast-tag-selection
  8689. current-tags inherited-tags table
  8690. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8691. (let ((org-add-colon-after-tag-completion t))
  8692. (org-trim
  8693. (org-without-partial-completion
  8694. (completing-read "Tags: " 'org-tags-completion-function
  8695. nil nil current 'org-tags-history)))))))
  8696. (while (string-match "[-+&]+" tags)
  8697. ;; No boolean logic, just a list
  8698. (setq tags (replace-match ":" t t tags))))
  8699. (if (string-match "\\`[\t ]*\\'" tags)
  8700. (setq tags "")
  8701. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8702. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8703. ;; Insert new tags at the correct column
  8704. (beginning-of-line 1)
  8705. (cond
  8706. ((and (equal current "") (equal tags "")))
  8707. ((re-search-forward
  8708. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8709. (point-at-eol) t)
  8710. (if (equal tags "")
  8711. (setq rpl "")
  8712. (goto-char (match-beginning 0))
  8713. (setq c0 (current-column) p0 (point)
  8714. c1 (max (1+ c0) (if (> org-tags-column 0)
  8715. org-tags-column
  8716. (- (- org-tags-column) (length tags))))
  8717. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8718. (replace-match rpl t t)
  8719. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8720. tags)
  8721. (t (error "Tags alignment failed")))
  8722. (move-to-column col)
  8723. (unless just-align
  8724. (run-hooks 'org-after-tags-change-hook)))))
  8725. (defun org-change-tag-in-region (beg end tag off)
  8726. "Add or remove TAG for each entry in the region.
  8727. This works in the agenda, and also in an org-mode buffer."
  8728. (interactive
  8729. (list (region-beginning) (region-end)
  8730. (let ((org-last-tags-completion-table
  8731. (if (org-mode-p)
  8732. (org-get-buffer-tags)
  8733. (org-global-tags-completion-table))))
  8734. (completing-read
  8735. "Tag: " 'org-tags-completion-function nil nil nil
  8736. 'org-tags-history))
  8737. (progn
  8738. (message "[s]et or [r]emove? ")
  8739. (equal (read-char-exclusive) ?r))))
  8740. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8741. (let ((agendap (equal major-mode 'org-agenda-mode))
  8742. l1 l2 m buf pos newhead (cnt 0))
  8743. (goto-char end)
  8744. (setq l2 (1- (org-current-line)))
  8745. (goto-char beg)
  8746. (setq l1 (org-current-line))
  8747. (loop for l from l1 to l2 do
  8748. (goto-line l)
  8749. (setq m (get-text-property (point) 'org-hd-marker))
  8750. (when (or (and (org-mode-p) (org-on-heading-p))
  8751. (and agendap m))
  8752. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8753. pos (if agendap m (point)))
  8754. (with-current-buffer buf
  8755. (save-excursion
  8756. (save-restriction
  8757. (goto-char pos)
  8758. (setq cnt (1+ cnt))
  8759. (org-toggle-tag tag (if off 'off 'on))
  8760. (setq newhead (org-get-heading)))))
  8761. (and agendap (org-agenda-change-all-lines newhead m))))
  8762. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8763. (defun org-tags-completion-function (string predicate &optional flag)
  8764. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8765. (confirm (lambda (x) (stringp (car x)))))
  8766. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8767. (setq s1 (match-string 1 string)
  8768. s2 (match-string 2 string))
  8769. (setq s1 "" s2 string))
  8770. (cond
  8771. ((eq flag nil)
  8772. ;; try completion
  8773. (setq rtn (try-completion s2 ctable confirm))
  8774. (if (stringp rtn)
  8775. (setq rtn
  8776. (concat s1 s2 (substring rtn (length s2))
  8777. (if (and org-add-colon-after-tag-completion
  8778. (assoc rtn ctable))
  8779. ":" ""))))
  8780. rtn)
  8781. ((eq flag t)
  8782. ;; all-completions
  8783. (all-completions s2 ctable confirm)
  8784. )
  8785. ((eq flag 'lambda)
  8786. ;; exact match?
  8787. (assoc s2 ctable)))
  8788. ))
  8789. (defun org-fast-tag-insert (kwd tags face &optional end)
  8790. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8791. (insert (format "%-12s" (concat kwd ":"))
  8792. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8793. (or end "")))
  8794. (defun org-fast-tag-show-exit (flag)
  8795. (save-excursion
  8796. (goto-line 3)
  8797. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8798. (replace-match ""))
  8799. (when flag
  8800. (end-of-line 1)
  8801. (move-to-column (- (window-width) 19) t)
  8802. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8803. (defun org-set-current-tags-overlay (current prefix)
  8804. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8805. (if (featurep 'xemacs)
  8806. (org-overlay-display org-tags-overlay (concat prefix s)
  8807. 'secondary-selection)
  8808. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8809. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8810. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8811. "Fast tag selection with single keys.
  8812. CURRENT is the current list of tags in the headline, INHERITED is the
  8813. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8814. possibly with grouping information. TODO-TABLE is a similar table with
  8815. TODO keywords, should these have keys assigned to them.
  8816. If the keys are nil, a-z are automatically assigned.
  8817. Returns the new tags string, or nil to not change the current settings."
  8818. (let* ((fulltable (append table todo-table))
  8819. (maxlen (apply 'max (mapcar
  8820. (lambda (x)
  8821. (if (stringp (car x)) (string-width (car x)) 0))
  8822. fulltable)))
  8823. (buf (current-buffer))
  8824. (expert (eq org-fast-tag-selection-single-key 'expert))
  8825. (buffer-tags nil)
  8826. (fwidth (+ maxlen 3 1 3))
  8827. (ncol (/ (- (window-width) 4) fwidth))
  8828. (i-face 'org-done)
  8829. (c-face 'org-todo)
  8830. tg cnt e c char c1 c2 ntable tbl rtn
  8831. ov-start ov-end ov-prefix
  8832. (exit-after-next org-fast-tag-selection-single-key)
  8833. (done-keywords org-done-keywords)
  8834. groups ingroup)
  8835. (save-excursion
  8836. (beginning-of-line 1)
  8837. (if (looking-at
  8838. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8839. (setq ov-start (match-beginning 1)
  8840. ov-end (match-end 1)
  8841. ov-prefix "")
  8842. (setq ov-start (1- (point-at-eol))
  8843. ov-end (1+ ov-start))
  8844. (skip-chars-forward "^\n\r")
  8845. (setq ov-prefix
  8846. (concat
  8847. (buffer-substring (1- (point)) (point))
  8848. (if (> (current-column) org-tags-column)
  8849. " "
  8850. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8851. (org-move-overlay org-tags-overlay ov-start ov-end)
  8852. (save-window-excursion
  8853. (if expert
  8854. (set-buffer (get-buffer-create " *Org tags*"))
  8855. (delete-other-windows)
  8856. (split-window-vertically)
  8857. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8858. (erase-buffer)
  8859. (org-set-local 'org-done-keywords done-keywords)
  8860. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8861. (org-fast-tag-insert "Current" current c-face "\n\n")
  8862. (org-fast-tag-show-exit exit-after-next)
  8863. (org-set-current-tags-overlay current ov-prefix)
  8864. (setq tbl fulltable char ?a cnt 0)
  8865. (while (setq e (pop tbl))
  8866. (cond
  8867. ((equal e '(:startgroup))
  8868. (push '() groups) (setq ingroup t)
  8869. (when (not (= cnt 0))
  8870. (setq cnt 0)
  8871. (insert "\n"))
  8872. (insert "{ "))
  8873. ((equal e '(:endgroup))
  8874. (setq ingroup nil cnt 0)
  8875. (insert "}\n"))
  8876. (t
  8877. (setq tg (car e) c2 nil)
  8878. (if (cdr e)
  8879. (setq c (cdr e))
  8880. ;; automatically assign a character.
  8881. (setq c1 (string-to-char
  8882. (downcase (substring
  8883. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8884. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8885. (while (or (rassoc char ntable) (rassoc char table))
  8886. (setq char (1+ char)))
  8887. (setq c2 c1))
  8888. (setq c (or c2 char)))
  8889. (if ingroup (push tg (car groups)))
  8890. (setq tg (org-add-props tg nil 'face
  8891. (cond
  8892. ((not (assoc tg table))
  8893. (org-get-todo-face tg))
  8894. ((member tg current) c-face)
  8895. ((member tg inherited) i-face)
  8896. (t nil))))
  8897. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8898. (insert "[" c "] " tg (make-string
  8899. (- fwidth 4 (length tg)) ?\ ))
  8900. (push (cons tg c) ntable)
  8901. (when (= (setq cnt (1+ cnt)) ncol)
  8902. (insert "\n")
  8903. (if ingroup (insert " "))
  8904. (setq cnt 0)))))
  8905. (setq ntable (nreverse ntable))
  8906. (insert "\n")
  8907. (goto-char (point-min))
  8908. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8909. (fit-window-to-buffer))
  8910. (setq rtn
  8911. (catch 'exit
  8912. (while t
  8913. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8914. (if groups " [!] no groups" " [!]groups")
  8915. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8916. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8917. (cond
  8918. ((= c ?\r) (throw 'exit t))
  8919. ((= c ?!)
  8920. (setq groups (not groups))
  8921. (goto-char (point-min))
  8922. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8923. ((= c ?\C-c)
  8924. (if (not expert)
  8925. (org-fast-tag-show-exit
  8926. (setq exit-after-next (not exit-after-next)))
  8927. (setq expert nil)
  8928. (delete-other-windows)
  8929. (split-window-vertically)
  8930. (org-switch-to-buffer-other-window " *Org tags*")
  8931. (and (fboundp 'fit-window-to-buffer)
  8932. (fit-window-to-buffer))))
  8933. ((or (= c ?\C-g)
  8934. (and (= c ?q) (not (rassoc c ntable))))
  8935. (org-detach-overlay org-tags-overlay)
  8936. (setq quit-flag t))
  8937. ((= c ?\ )
  8938. (setq current nil)
  8939. (if exit-after-next (setq exit-after-next 'now)))
  8940. ((= c ?\t)
  8941. (condition-case nil
  8942. (setq tg (completing-read
  8943. "Tag: "
  8944. (or buffer-tags
  8945. (with-current-buffer buf
  8946. (org-get-buffer-tags)))))
  8947. (quit (setq tg "")))
  8948. (when (string-match "\\S-" tg)
  8949. (add-to-list 'buffer-tags (list tg))
  8950. (if (member tg current)
  8951. (setq current (delete tg current))
  8952. (push tg current)))
  8953. (if exit-after-next (setq exit-after-next 'now)))
  8954. ((setq e (rassoc c todo-table) tg (car e))
  8955. (with-current-buffer buf
  8956. (save-excursion (org-todo tg)))
  8957. (if exit-after-next (setq exit-after-next 'now)))
  8958. ((setq e (rassoc c ntable) tg (car e))
  8959. (if (member tg current)
  8960. (setq current (delete tg current))
  8961. (loop for g in groups do
  8962. (if (member tg g)
  8963. (mapc (lambda (x)
  8964. (setq current (delete x current)))
  8965. g)))
  8966. (push tg current))
  8967. (if exit-after-next (setq exit-after-next 'now))))
  8968. ;; Create a sorted list
  8969. (setq current
  8970. (sort current
  8971. (lambda (a b)
  8972. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8973. (if (eq exit-after-next 'now) (throw 'exit t))
  8974. (goto-char (point-min))
  8975. (beginning-of-line 2)
  8976. (delete-region (point) (point-at-eol))
  8977. (org-fast-tag-insert "Current" current c-face)
  8978. (org-set-current-tags-overlay current ov-prefix)
  8979. (while (re-search-forward
  8980. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8981. (setq tg (match-string 1))
  8982. (add-text-properties
  8983. (match-beginning 1) (match-end 1)
  8984. (list 'face
  8985. (cond
  8986. ((member tg current) c-face)
  8987. ((member tg inherited) i-face)
  8988. (t (get-text-property (match-beginning 1) 'face))))))
  8989. (goto-char (point-min)))))
  8990. (org-detach-overlay org-tags-overlay)
  8991. (if rtn
  8992. (mapconcat 'identity current ":")
  8993. nil))))
  8994. (defun org-get-tags-string ()
  8995. "Get the TAGS string in the current headline."
  8996. (unless (org-on-heading-p t)
  8997. (error "Not on a heading"))
  8998. (save-excursion
  8999. (beginning-of-line 1)
  9000. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9001. (org-match-string-no-properties 1)
  9002. "")))
  9003. (defun org-get-tags ()
  9004. "Get the list of tags specified in the current headline."
  9005. (org-split-string (org-get-tags-string) ":"))
  9006. (defun org-get-buffer-tags ()
  9007. "Get a table of all tags used in the buffer, for completion."
  9008. (let (tags)
  9009. (save-excursion
  9010. (goto-char (point-min))
  9011. (while (re-search-forward
  9012. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9013. (when (equal (char-after (point-at-bol 0)) ?*)
  9014. (mapc (lambda (x) (add-to-list 'tags x))
  9015. (org-split-string (org-match-string-no-properties 1) ":")))))
  9016. (mapcar 'list tags)))
  9017. ;;;; Properties
  9018. ;;; Setting and retrieving properties
  9019. (defconst org-special-properties
  9020. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9021. "TIMESTAMP" "TIMESTAMP_IA")
  9022. "The special properties valid in Org-mode.
  9023. These are properties that are not defined in the property drawer,
  9024. but in some other way.")
  9025. (defconst org-default-properties
  9026. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9027. "LOCATION" "LOGGING" "COLUMNS")
  9028. "Some properties that are used by Org-mode for various purposes.
  9029. Being in this list makes sure that they are offered for completion.")
  9030. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9031. "Regular expression matching the first line of a property drawer.")
  9032. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9033. "Regular expression matching the first line of a property drawer.")
  9034. (defun org-property-action ()
  9035. "Do an action on properties."
  9036. (interactive)
  9037. (let (c)
  9038. (org-at-property-p)
  9039. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9040. (setq c (read-char-exclusive))
  9041. (cond
  9042. ((equal c ?s)
  9043. (call-interactively 'org-set-property))
  9044. ((equal c ?d)
  9045. (call-interactively 'org-delete-property))
  9046. ((equal c ?D)
  9047. (call-interactively 'org-delete-property-globally))
  9048. ((equal c ?c)
  9049. (call-interactively 'org-compute-property-at-point))
  9050. (t (error "No such property action %c" c)))))
  9051. (defun org-at-property-p ()
  9052. "Is the cursor in a property line?"
  9053. ;; FIXME: Does not check if we are actually in the drawer.
  9054. ;; FIXME: also returns true on any drawers.....
  9055. ;; This is used by C-c C-c for property action.
  9056. (save-excursion
  9057. (beginning-of-line 1)
  9058. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9059. (defun org-get-property-block (&optional beg end force)
  9060. "Return the (beg . end) range of the body of the property drawer.
  9061. BEG and END can be beginning and end of subtree, if not given
  9062. they will be found.
  9063. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9064. (catch 'exit
  9065. (save-excursion
  9066. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9067. (end (or end (progn (outline-next-heading) (point)))))
  9068. (goto-char beg)
  9069. (if (re-search-forward org-property-start-re end t)
  9070. (setq beg (1+ (match-end 0)))
  9071. (if force
  9072. (save-excursion
  9073. (org-insert-property-drawer)
  9074. (setq end (progn (outline-next-heading) (point))))
  9075. (throw 'exit nil))
  9076. (goto-char beg)
  9077. (if (re-search-forward org-property-start-re end t)
  9078. (setq beg (1+ (match-end 0)))))
  9079. (if (re-search-forward org-property-end-re end t)
  9080. (setq end (match-beginning 0))
  9081. (or force (throw 'exit nil))
  9082. (goto-char beg)
  9083. (setq end beg)
  9084. (org-indent-line-function)
  9085. (insert ":END:\n"))
  9086. (cons beg end)))))
  9087. (defun org-entry-properties (&optional pom which)
  9088. "Get all properties of the entry at point-or-marker POM.
  9089. This includes the TODO keyword, the tags, time strings for deadline,
  9090. scheduled, and clocking, and any additional properties defined in the
  9091. entry. The return value is an alist, keys may occur multiple times
  9092. if the property key was used several times.
  9093. POM may also be nil, in which case the current entry is used.
  9094. If WHICH is nil or `all', get all properties. If WHICH is
  9095. `special' or `standard', only get that subclass."
  9096. (setq which (or which 'all))
  9097. (org-with-point-at pom
  9098. (let ((clockstr (substring org-clock-string 0 -1))
  9099. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9100. beg end range props sum-props key value string clocksum)
  9101. (save-excursion
  9102. (when (condition-case nil (org-back-to-heading t) (error nil))
  9103. (setq beg (point))
  9104. (setq sum-props (get-text-property (point) 'org-summaries))
  9105. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9106. (outline-next-heading)
  9107. (setq end (point))
  9108. (when (memq which '(all special))
  9109. ;; Get the special properties, like TODO and tags
  9110. (goto-char beg)
  9111. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9112. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9113. (when (looking-at org-priority-regexp)
  9114. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9115. (when (and (setq value (org-get-tags-string))
  9116. (string-match "\\S-" value))
  9117. (push (cons "TAGS" value) props))
  9118. (when (setq value (org-get-tags-at))
  9119. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9120. props))
  9121. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9122. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9123. string (if (equal key clockstr)
  9124. (org-no-properties
  9125. (org-trim
  9126. (buffer-substring
  9127. (match-beginning 3) (goto-char (point-at-eol)))))
  9128. (substring (org-match-string-no-properties 3) 1 -1)))
  9129. (unless key
  9130. (if (= (char-after (match-beginning 3)) ?\[)
  9131. (setq key "TIMESTAMP_IA")
  9132. (setq key "TIMESTAMP")))
  9133. (when (or (equal key clockstr) (not (assoc key props)))
  9134. (push (cons key string) props)))
  9135. )
  9136. (when (memq which '(all standard))
  9137. ;; Get the standard properties, like :PORP: ...
  9138. (setq range (org-get-property-block beg end))
  9139. (when range
  9140. (goto-char (car range))
  9141. (while (re-search-forward
  9142. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9143. (cdr range) t)
  9144. (setq key (org-match-string-no-properties 1)
  9145. value (org-trim (or (org-match-string-no-properties 2) "")))
  9146. (unless (member key excluded)
  9147. (push (cons key (or value "")) props)))))
  9148. (if clocksum
  9149. (push (cons "CLOCKSUM"
  9150. (org-column-number-to-string (/ (float clocksum) 60.)
  9151. 'add_times))
  9152. props))
  9153. (append sum-props (nreverse props)))))))
  9154. (defun org-entry-get (pom property &optional inherit)
  9155. "Get value of PROPERTY for entry at point-or-marker POM.
  9156. If INHERIT is non-nil and the entry does not have the property,
  9157. then also check higher levels of the hierarchy.
  9158. If INHERIT is the symbol `selective', use inheritance only if the setting
  9159. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9160. If the property is present but empty, the return value is the empty string.
  9161. If the property is not present at all, nil is returned."
  9162. (org-with-point-at pom
  9163. (if (and inherit (if (eq inherit 'selective)
  9164. (org-property-inherit-p property)
  9165. t))
  9166. (org-entry-get-with-inheritance property)
  9167. (if (member property org-special-properties)
  9168. ;; We need a special property. Use brute force, get all properties.
  9169. (cdr (assoc property (org-entry-properties nil 'special)))
  9170. (let ((range (org-get-property-block)))
  9171. (if (and range
  9172. (goto-char (car range))
  9173. (re-search-forward
  9174. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9175. (cdr range) t))
  9176. ;; Found the property, return it.
  9177. (if (match-end 1)
  9178. (org-match-string-no-properties 1)
  9179. "")))))))
  9180. (defun org-property-or-variable-value (var &optional inherit)
  9181. "Check if there is a property fixing the value of VAR.
  9182. If yes, return this value. If not, return the current value of the variable."
  9183. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9184. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9185. (read prop)
  9186. (symbol-value var))))
  9187. (defun org-entry-delete (pom property)
  9188. "Delete the property PROPERTY from entry at point-or-marker POM."
  9189. (org-with-point-at pom
  9190. (if (member property org-special-properties)
  9191. nil ; cannot delete these properties.
  9192. (let ((range (org-get-property-block)))
  9193. (if (and range
  9194. (goto-char (car range))
  9195. (re-search-forward
  9196. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9197. (cdr range) t))
  9198. (progn
  9199. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9200. t)
  9201. nil)))))
  9202. ;; Multi-values properties are properties that contain multiple values
  9203. ;; These values are assumed to be single words, separated by whitespace.
  9204. (defun org-entry-add-to-multivalued-property (pom property value)
  9205. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9206. (let* ((old (org-entry-get pom property))
  9207. (values (and old (org-split-string old "[ \t]"))))
  9208. (unless (member value values)
  9209. (setq values (cons value values))
  9210. (org-entry-put pom property
  9211. (mapconcat 'identity values " ")))))
  9212. (defun org-entry-remove-from-multivalued-property (pom property value)
  9213. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9214. (let* ((old (org-entry-get pom property))
  9215. (values (and old (org-split-string old "[ \t]"))))
  9216. (when (member value values)
  9217. (setq values (delete value values))
  9218. (org-entry-put pom property
  9219. (mapconcat 'identity values " ")))))
  9220. (defun org-entry-member-in-multivalued-property (pom property value)
  9221. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9222. (let* ((old (org-entry-get pom property))
  9223. (values (and old (org-split-string old "[ \t]"))))
  9224. (member value values)))
  9225. (defvar org-entry-property-inherited-from (make-marker))
  9226. (defun org-entry-get-with-inheritance (property)
  9227. "Get entry property, and search higher levels if not present."
  9228. (let (tmp)
  9229. (save-excursion
  9230. (save-restriction
  9231. (widen)
  9232. (catch 'ex
  9233. (while t
  9234. (when (setq tmp (org-entry-get nil property))
  9235. (org-back-to-heading t)
  9236. (move-marker org-entry-property-inherited-from (point))
  9237. (throw 'ex tmp))
  9238. (or (org-up-heading-safe) (throw 'ex nil)))))
  9239. (or tmp (cdr (assoc property org-local-properties))
  9240. (cdr (assoc property org-global-properties))))))
  9241. (defun org-entry-put (pom property value)
  9242. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9243. (org-with-point-at pom
  9244. (org-back-to-heading t)
  9245. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9246. range)
  9247. (cond
  9248. ((equal property "TODO")
  9249. (when (and (stringp value) (string-match "\\S-" value)
  9250. (not (member value org-todo-keywords-1)))
  9251. (error "\"%s\" is not a valid TODO state" value))
  9252. (if (or (not value)
  9253. (not (string-match "\\S-" value)))
  9254. (setq value 'none))
  9255. (org-todo value)
  9256. (org-set-tags nil 'align))
  9257. ((equal property "PRIORITY")
  9258. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9259. (string-to-char value) ?\ ))
  9260. (org-set-tags nil 'align))
  9261. ((equal property "SCHEDULED")
  9262. (if (re-search-forward org-scheduled-time-regexp end t)
  9263. (cond
  9264. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9265. ((eq value 'later) (org-timestamp-change 1 'day))
  9266. (t (call-interactively 'org-schedule)))
  9267. (call-interactively 'org-schedule)))
  9268. ((equal property "DEADLINE")
  9269. (if (re-search-forward org-deadline-time-regexp end t)
  9270. (cond
  9271. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9272. ((eq value 'later) (org-timestamp-change 1 'day))
  9273. (t (call-interactively 'org-deadline)))
  9274. (call-interactively 'org-deadline)))
  9275. ((member property org-special-properties)
  9276. (error "The %s property can not yet be set with `org-entry-put'"
  9277. property))
  9278. (t ; a non-special property
  9279. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9280. (setq range (org-get-property-block beg end 'force))
  9281. (goto-char (car range))
  9282. (if (re-search-forward
  9283. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9284. (progn
  9285. (delete-region (match-beginning 1) (match-end 1))
  9286. (goto-char (match-beginning 1)))
  9287. (goto-char (cdr range))
  9288. (insert "\n")
  9289. (backward-char 1)
  9290. (org-indent-line-function)
  9291. (insert ":" property ":"))
  9292. (and value (insert " " value))
  9293. (org-indent-line-function)))))))
  9294. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9295. "Get all property keys in the current buffer.
  9296. With INCLUDE-SPECIALS, also list the special properties that relect things
  9297. like tags and TODO state.
  9298. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9299. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9300. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9301. formats in the current buffer."
  9302. (let (rtn range cfmt cols s p)
  9303. (save-excursion
  9304. (save-restriction
  9305. (widen)
  9306. (goto-char (point-min))
  9307. (while (re-search-forward org-property-start-re nil t)
  9308. (setq range (org-get-property-block))
  9309. (goto-char (car range))
  9310. (while (re-search-forward
  9311. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9312. (cdr range) t)
  9313. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9314. (outline-next-heading))))
  9315. (when include-specials
  9316. (setq rtn (append org-special-properties rtn)))
  9317. (when include-defaults
  9318. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9319. (when include-columns
  9320. (save-excursion
  9321. (save-restriction
  9322. (widen)
  9323. (goto-char (point-min))
  9324. (while (re-search-forward
  9325. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9326. nil t)
  9327. (setq cfmt (match-string 2) s 0)
  9328. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9329. cfmt s)
  9330. (setq s (match-end 0)
  9331. p (match-string 1 cfmt))
  9332. (unless (or (equal p "ITEM")
  9333. (member p org-special-properties))
  9334. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9335. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9336. (defun org-property-values (key)
  9337. "Return a list of all values of property KEY."
  9338. (save-excursion
  9339. (save-restriction
  9340. (widen)
  9341. (goto-char (point-min))
  9342. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9343. values)
  9344. (while (re-search-forward re nil t)
  9345. (add-to-list 'values (org-trim (match-string 1))))
  9346. (delete "" values)))))
  9347. (defun org-insert-property-drawer ()
  9348. "Insert a property drawer into the current entry."
  9349. (interactive)
  9350. (org-back-to-heading t)
  9351. (looking-at outline-regexp)
  9352. (let ((indent (- (match-end 0)(match-beginning 0)))
  9353. (beg (point))
  9354. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9355. end hiddenp)
  9356. (outline-next-heading)
  9357. (setq end (point))
  9358. (goto-char beg)
  9359. (while (re-search-forward re end t))
  9360. (setq hiddenp (org-invisible-p))
  9361. (end-of-line 1)
  9362. (and (equal (char-after) ?\n) (forward-char 1))
  9363. (org-skip-over-state-notes)
  9364. (skip-chars-backward " \t\n\r")
  9365. (if (eq (char-before) ?*) (forward-char 1))
  9366. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9367. (beginning-of-line 0)
  9368. (indent-to-column indent)
  9369. (beginning-of-line 2)
  9370. (indent-to-column indent)
  9371. (beginning-of-line 0)
  9372. (if hiddenp
  9373. (save-excursion
  9374. (org-back-to-heading t)
  9375. (hide-entry))
  9376. (org-flag-drawer t))))
  9377. (defun org-set-property (property value)
  9378. "In the current entry, set PROPERTY to VALUE.
  9379. When called interactively, this will prompt for a property name, offering
  9380. completion on existing and default properties. And then it will prompt
  9381. for a value, offering competion either on allowed values (via an inherited
  9382. xxx_ALL property) or on existing values in other instances of this property
  9383. in the current file."
  9384. (interactive
  9385. (let* ((prop (completing-read
  9386. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  9387. (cur (org-entry-get nil prop))
  9388. (allowed (org-property-get-allowed-values nil prop 'table))
  9389. (existing (mapcar 'list (org-property-values prop)))
  9390. (val (if allowed
  9391. (completing-read "Value: " allowed nil 'req-match)
  9392. (completing-read
  9393. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9394. (concat "[" cur "]") "")
  9395. ": ")
  9396. existing nil nil "" nil cur))))
  9397. (list prop (if (equal val "") cur val))))
  9398. (unless (equal (org-entry-get nil property) value)
  9399. (org-entry-put nil property value)))
  9400. (defun org-delete-property (property)
  9401. "In the current entry, delete PROPERTY."
  9402. (interactive
  9403. (let* ((prop (completing-read
  9404. "Property: " (org-entry-properties nil 'standard))))
  9405. (list prop)))
  9406. (message "Property %s %s" property
  9407. (if (org-entry-delete nil property)
  9408. "deleted"
  9409. "was not present in the entry")))
  9410. (defun org-delete-property-globally (property)
  9411. "Remove PROPERTY globally, from all entries."
  9412. (interactive
  9413. (let* ((prop (completing-read
  9414. "Globally remove property: "
  9415. (mapcar 'list (org-buffer-property-keys)))))
  9416. (list prop)))
  9417. (save-excursion
  9418. (save-restriction
  9419. (widen)
  9420. (goto-char (point-min))
  9421. (let ((cnt 0))
  9422. (while (re-search-forward
  9423. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9424. nil t)
  9425. (setq cnt (1+ cnt))
  9426. (replace-match ""))
  9427. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9428. (defvar org-columns-current-fmt-compiled) ; defined below
  9429. (defun org-compute-property-at-point ()
  9430. "Compute the property at point.
  9431. This looks for an enclosing column format, extracts the operator and
  9432. then applies it to the proerty in the column format's scope."
  9433. (interactive)
  9434. (unless (org-at-property-p)
  9435. (error "Not at a property"))
  9436. (let ((prop (org-match-string-no-properties 2)))
  9437. (org-columns-get-format-and-top-level)
  9438. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9439. (error "No operator defined for property %s" prop))
  9440. (org-columns-compute prop)))
  9441. (defun org-property-get-allowed-values (pom property &optional table)
  9442. "Get allowed values for the property PROPERTY.
  9443. When TABLE is non-nil, return an alist that can directly be used for
  9444. completion."
  9445. (let (vals)
  9446. (cond
  9447. ((equal property "TODO")
  9448. (setq vals (org-with-point-at pom
  9449. (append org-todo-keywords-1 '("")))))
  9450. ((equal property "PRIORITY")
  9451. (let ((n org-lowest-priority))
  9452. (while (>= n org-highest-priority)
  9453. (push (char-to-string n) vals)
  9454. (setq n (1- n)))))
  9455. ((member property org-special-properties))
  9456. (t
  9457. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9458. (when (and vals (string-match "\\S-" vals))
  9459. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9460. (setq vals (mapcar (lambda (x)
  9461. (cond ((stringp x) x)
  9462. ((numberp x) (number-to-string x))
  9463. ((symbolp x) (symbol-name x))
  9464. (t "???")))
  9465. vals)))))
  9466. (if table (mapcar 'list vals) vals)))
  9467. (defun org-property-previous-allowed-value (&optional previous)
  9468. "Switch to the next allowed value for this property."
  9469. (interactive)
  9470. (org-property-next-allowed-value t))
  9471. (defun org-property-next-allowed-value (&optional previous)
  9472. "Switch to the next allowed value for this property."
  9473. (interactive)
  9474. (unless (org-at-property-p)
  9475. (error "Not at a property"))
  9476. (let* ((key (match-string 2))
  9477. (value (match-string 3))
  9478. (allowed (or (org-property-get-allowed-values (point) key)
  9479. (and (member value '("[ ]" "[-]" "[X]"))
  9480. '("[ ]" "[X]"))))
  9481. nval)
  9482. (unless allowed
  9483. (error "Allowed values for this property have not been defined"))
  9484. (if previous (setq allowed (reverse allowed)))
  9485. (if (member value allowed)
  9486. (setq nval (car (cdr (member value allowed)))))
  9487. (setq nval (or nval (car allowed)))
  9488. (if (equal nval value)
  9489. (error "Only one allowed value for this property"))
  9490. (org-at-property-p)
  9491. (replace-match (concat " :" key ": " nval) t t)
  9492. (org-indent-line-function)
  9493. (beginning-of-line 1)
  9494. (skip-chars-forward " \t")))
  9495. (defun org-find-entry-with-id (ident)
  9496. "Locate the entry that contains the ID property with exact value IDENT.
  9497. IDENT can be a string, a symbol or a number, this function will search for
  9498. the string representation of it.
  9499. Return the position where this entry starts, or nil if there is no such entry."
  9500. (let ((id (cond
  9501. ((stringp ident) ident)
  9502. ((symbol-name ident) (symbol-name ident))
  9503. ((numberp ident) (number-to-string ident))
  9504. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9505. (case-fold-search nil))
  9506. (save-excursion
  9507. (save-restriction
  9508. (widen)
  9509. (goto-char (point-min))
  9510. (when (re-search-forward
  9511. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9512. nil t)
  9513. (org-back-to-heading)
  9514. (point))))))
  9515. ;;; Column View
  9516. (defvar org-columns-overlays nil
  9517. "Holds the list of current column overlays.")
  9518. (defvar org-columns-current-fmt nil
  9519. "Local variable, holds the currently active column format.")
  9520. (defvar org-columns-current-fmt-compiled nil
  9521. "Local variable, holds the currently active column format.
  9522. This is the compiled version of the format.")
  9523. (defvar org-columns-current-widths nil
  9524. "Loval variable, holds the currently widths of fields.")
  9525. (defvar org-columns-current-maxwidths nil
  9526. "Loval variable, holds the currently active maximum column widths.")
  9527. (defvar org-columns-begin-marker (make-marker)
  9528. "Points to the position where last a column creation command was called.")
  9529. (defvar org-columns-top-level-marker (make-marker)
  9530. "Points to the position where current columns region starts.")
  9531. (defvar org-columns-map (make-sparse-keymap)
  9532. "The keymap valid in column display.")
  9533. (defun org-columns-content ()
  9534. "Switch to contents view while in columns view."
  9535. (interactive)
  9536. (org-overview)
  9537. (org-content))
  9538. (org-defkey org-columns-map "c" 'org-columns-content)
  9539. (org-defkey org-columns-map "o" 'org-overview)
  9540. (org-defkey org-columns-map "e" 'org-columns-edit-value)
  9541. (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
  9542. (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
  9543. (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
  9544. (org-defkey org-columns-map "v" 'org-columns-show-value)
  9545. (org-defkey org-columns-map "q" 'org-columns-quit)
  9546. (org-defkey org-columns-map "r" 'org-columns-redo)
  9547. (org-defkey org-columns-map "g" 'org-columns-redo)
  9548. (org-defkey org-columns-map [left] 'backward-char)
  9549. (org-defkey org-columns-map "\M-b" 'backward-char)
  9550. (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
  9551. (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
  9552. (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
  9553. (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
  9554. (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
  9555. (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
  9556. (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
  9557. (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
  9558. (org-defkey org-columns-map "<" 'org-columns-narrow)
  9559. (org-defkey org-columns-map ">" 'org-columns-widen)
  9560. (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
  9561. (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
  9562. (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
  9563. (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
  9564. (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
  9565. '("Column"
  9566. ["Edit property" org-columns-edit-value t]
  9567. ["Next allowed value" org-columns-next-allowed-value t]
  9568. ["Previous allowed value" org-columns-previous-allowed-value t]
  9569. ["Show full value" org-columns-show-value t]
  9570. ["Edit allowed values" org-columns-edit-allowed t]
  9571. "--"
  9572. ["Edit column attributes" org-columns-edit-attributes t]
  9573. ["Increase column width" org-columns-widen t]
  9574. ["Decrease column width" org-columns-narrow t]
  9575. "--"
  9576. ["Move column right" org-columns-move-right t]
  9577. ["Move column left" org-columns-move-left t]
  9578. ["Add column" org-columns-new t]
  9579. ["Delete column" org-columns-delete t]
  9580. "--"
  9581. ["CONTENTS" org-columns-content t]
  9582. ["OVERVIEW" org-overview t]
  9583. ["Refresh columns display" org-columns-redo t]
  9584. "--"
  9585. ["Open link" org-columns-open-link t]
  9586. "--"
  9587. ["Quit" org-columns-quit t]))
  9588. (defun org-columns-new-overlay (beg end &optional string face)
  9589. "Create a new column overlay and add it to the list."
  9590. (let ((ov (org-make-overlay beg end)))
  9591. (org-overlay-put ov 'face (or face 'secondary-selection))
  9592. (org-overlay-display ov string face)
  9593. (push ov org-columns-overlays)
  9594. ov))
  9595. (defun org-columns-display-here (&optional props)
  9596. "Overlay the current line with column display."
  9597. (interactive)
  9598. (let* ((fmt org-columns-current-fmt-compiled)
  9599. (beg (point-at-bol))
  9600. (level-face (save-excursion
  9601. (beginning-of-line 1)
  9602. (and (looking-at "\\(\\**\\)\\(\\* \\)")
  9603. (org-get-level-face 2))))
  9604. (color (list :foreground
  9605. (face-attribute (or level-face 'default) :foreground)))
  9606. props pom property ass width f string ov column val modval)
  9607. ;; Check if the entry is in another buffer.
  9608. (unless props
  9609. (if (eq major-mode 'org-agenda-mode)
  9610. (setq pom (or (get-text-property (point) 'org-hd-marker)
  9611. (get-text-property (point) 'org-marker))
  9612. props (if pom (org-entry-properties pom) nil))
  9613. (setq props (org-entry-properties nil))))
  9614. ;; Walk the format
  9615. (while (setq column (pop fmt))
  9616. (setq property (car column)
  9617. ass (if (equal property "ITEM")
  9618. (cons "ITEM"
  9619. (save-match-data
  9620. (org-no-properties
  9621. (org-remove-tabs
  9622. (buffer-substring-no-properties
  9623. (point-at-bol) (point-at-eol))))))
  9624. (assoc property props))
  9625. width (or (cdr (assoc property org-columns-current-maxwidths))
  9626. (nth 2 column)
  9627. (length property))
  9628. f (format "%%-%d.%ds | " width width)
  9629. val (or (cdr ass) "")
  9630. modval (if (equal property "ITEM")
  9631. (org-columns-cleanup-item val org-columns-current-fmt-compiled))
  9632. string (format f (or modval val)))
  9633. ;; Create the overlay
  9634. (org-unmodified
  9635. (setq ov (org-columns-new-overlay
  9636. beg (setq beg (1+ beg)) string
  9637. (list color 'org-column)))
  9638. (org-overlay-put ov 'keymap org-columns-map)
  9639. (org-overlay-put ov 'org-columns-key property)
  9640. (org-overlay-put ov 'org-columns-value (cdr ass))
  9641. (org-overlay-put ov 'org-columns-value-modified modval)
  9642. (org-overlay-put ov 'org-columns-pom pom)
  9643. (org-overlay-put ov 'org-columns-format f))
  9644. (if (or (not (char-after beg))
  9645. (equal (char-after beg) ?\n))
  9646. (let ((inhibit-read-only t))
  9647. (save-excursion
  9648. (goto-char beg)
  9649. (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
  9650. ;; Make the rest of the line disappear.
  9651. (org-unmodified
  9652. (setq ov (org-columns-new-overlay beg (point-at-eol)))
  9653. (org-overlay-put ov 'invisible t)
  9654. (org-overlay-put ov 'keymap org-columns-map)
  9655. (org-overlay-put ov 'intangible t)
  9656. (push ov org-columns-overlays)
  9657. (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
  9658. (org-overlay-put ov 'keymap org-columns-map)
  9659. (push ov org-columns-overlays)
  9660. (let ((inhibit-read-only t))
  9661. (put-text-property (max (point-min) (1- (point-at-bol)))
  9662. (min (point-max) (1+ (point-at-eol)))
  9663. 'read-only "Type `e' to edit property")))))
  9664. (defvar org-columns-full-header-line-format nil
  9665. "Fthe full header line format, will be shifted by horizontal scrolling." )
  9666. (defvar org-previous-header-line-format nil
  9667. "The header line format before column view was turned on.")
  9668. (defvar org-columns-inhibit-recalculation nil
  9669. "Inhibit recomputing of columns on column view startup.")
  9670. (defvar header-line-format)
  9671. (defvar org-columns-previous-hscroll 0)
  9672. (defun org-columns-display-here-title ()
  9673. "Overlay the newline before the current line with the table title."
  9674. (interactive)
  9675. (let ((fmt org-columns-current-fmt-compiled)
  9676. string (title "")
  9677. property width f column str widths)
  9678. (while (setq column (pop fmt))
  9679. (setq property (car column)
  9680. str (or (nth 1 column) property)
  9681. width (or (cdr (assoc property org-columns-current-maxwidths))
  9682. (nth 2 column)
  9683. (length str))
  9684. widths (push width widths)
  9685. f (format "%%-%d.%ds | " width width)
  9686. string (format f str)
  9687. title (concat title string)))
  9688. (setq title (concat
  9689. (org-add-props " " nil 'display '(space :align-to 0))
  9690. (org-add-props title nil 'face '(:weight bold :underline t :inherit default))))
  9691. (org-set-local 'org-previous-header-line-format header-line-format)
  9692. (org-set-local 'org-columns-current-widths (nreverse widths))
  9693. (setq org-columns-full-header-line-format title)
  9694. (setq org-columns-previous-hscroll -1)
  9695. ; (org-columns-hscoll-title)
  9696. (org-add-hook 'post-command-hook 'org-columns-hscoll-title nil 'local)))
  9697. (defun org-columns-hscoll-title ()
  9698. "Set the header-line-format so that it scrolls along with the table."
  9699. (sit-for .0001) ; need to force a redisplay to update window-hscroll
  9700. (when (not (= (window-hscroll) org-columns-previous-hscroll))
  9701. (setq header-line-format
  9702. (concat (substring org-columns-full-header-line-format 0 1)
  9703. (substring org-columns-full-header-line-format
  9704. (1+ (window-hscroll))))
  9705. org-columns-previous-hscroll (window-hscroll))
  9706. (force-mode-line-update)))
  9707. (defun org-columns-remove-overlays ()
  9708. "Remove all currently active column overlays."
  9709. (interactive)
  9710. (when (marker-buffer org-columns-begin-marker)
  9711. (with-current-buffer (marker-buffer org-columns-begin-marker)
  9712. (when (local-variable-p 'org-previous-header-line-format)
  9713. (setq header-line-format org-previous-header-line-format)
  9714. (kill-local-variable 'org-previous-header-line-format)
  9715. (remove-hook 'post-command-hook 'org-columns-hscoll-title 'local))
  9716. (move-marker org-columns-begin-marker nil)
  9717. (move-marker org-columns-top-level-marker nil)
  9718. (org-unmodified
  9719. (mapc 'org-delete-overlay org-columns-overlays)
  9720. (setq org-columns-overlays nil)
  9721. (let ((inhibit-read-only t))
  9722. (remove-text-properties (point-min) (point-max) '(read-only t)))))))
  9723. (defun org-columns-cleanup-item (item fmt)
  9724. "Remove from ITEM what is a column in the format FMT."
  9725. (if (not org-complex-heading-regexp)
  9726. item
  9727. (when (string-match org-complex-heading-regexp item)
  9728. (concat
  9729. (org-add-props (concat (match-string 1 item) " ") nil
  9730. 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
  9731. (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
  9732. (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
  9733. " " (match-string 4 item)
  9734. (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
  9735. (defun org-columns-show-value ()
  9736. "Show the full value of the property."
  9737. (interactive)
  9738. (let ((value (get-char-property (point) 'org-columns-value)))
  9739. (message "Value is: %s" (or value ""))))
  9740. (defun org-columns-quit ()
  9741. "Remove the column overlays and in this way exit column editing."
  9742. (interactive)
  9743. (org-unmodified
  9744. (org-columns-remove-overlays)
  9745. (let ((inhibit-read-only t))
  9746. (remove-text-properties (point-min) (point-max) '(read-only t))))
  9747. (when (eq major-mode 'org-agenda-mode)
  9748. (message
  9749. "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
  9750. (defun org-columns-check-computed ()
  9751. "Check if this column value is computed.
  9752. If yes, throw an error indicating that changing it does not make sense."
  9753. (let ((val (get-char-property (point) 'org-columns-value)))
  9754. (when (and (stringp val)
  9755. (get-char-property 0 'org-computed val))
  9756. (error "This value is computed from the entry's children"))))
  9757. (defun org-columns-todo (&optional arg)
  9758. "Change the TODO state during column view."
  9759. (interactive "P")
  9760. (org-columns-edit-value "TODO"))
  9761. (defun org-columns-set-tags-or-toggle (&optional arg)
  9762. "Toggle checkbox at point, or set tags for current headline."
  9763. (interactive "P")
  9764. (if (string-match "\\`\\[[ xX-]\\]\\'"
  9765. (get-char-property (point) 'org-columns-value))
  9766. (org-columns-next-allowed-value)
  9767. (org-columns-edit-value "TAGS")))
  9768. (defun org-columns-edit-value (&optional key)
  9769. "Edit the value of the property at point in column view.
  9770. Where possible, use the standard interface for changing this line."
  9771. (interactive)
  9772. (org-columns-check-computed)
  9773. (let* ((external-key key)
  9774. (col (current-column))
  9775. (key (or key (get-char-property (point) 'org-columns-key)))
  9776. (value (get-char-property (point) 'org-columns-value))
  9777. (bol (point-at-bol)) (eol (point-at-eol))
  9778. (pom (or (get-text-property bol 'org-hd-marker)
  9779. (point))) ; keep despite of compiler waring
  9780. (line-overlays
  9781. (delq nil (mapcar (lambda (x)
  9782. (and (eq (overlay-buffer x) (current-buffer))
  9783. (>= (overlay-start x) bol)
  9784. (<= (overlay-start x) eol)
  9785. x))
  9786. org-columns-overlays)))
  9787. nval eval allowed)
  9788. (cond
  9789. ((equal key "CLOCKSUM")
  9790. (error "This special column cannot be edited"))
  9791. ((equal key "ITEM")
  9792. (setq eval '(org-with-point-at pom
  9793. (org-edit-headline))))
  9794. ((equal key "TODO")
  9795. (setq eval '(org-with-point-at pom
  9796. (let ((current-prefix-arg
  9797. (if external-key current-prefix-arg '(4))))
  9798. (call-interactively 'org-todo)))))
  9799. ((equal key "PRIORITY")
  9800. (setq eval '(org-with-point-at pom
  9801. (call-interactively 'org-priority))))
  9802. ((equal key "TAGS")
  9803. (setq eval '(org-with-point-at pom
  9804. (let ((org-fast-tag-selection-single-key
  9805. (if (eq org-fast-tag-selection-single-key 'expert)
  9806. t org-fast-tag-selection-single-key)))
  9807. (call-interactively 'org-set-tags)))))
  9808. ((equal key "DEADLINE")
  9809. (setq eval '(org-with-point-at pom
  9810. (call-interactively 'org-deadline))))
  9811. ((equal key "SCHEDULED")
  9812. (setq eval '(org-with-point-at pom
  9813. (call-interactively 'org-schedule))))
  9814. (t
  9815. (setq allowed (org-property-get-allowed-values pom key 'table))
  9816. (if allowed
  9817. (setq nval (completing-read "Value: " allowed nil t))
  9818. (setq nval (read-string "Edit: " value)))
  9819. (setq nval (org-trim nval))
  9820. (when (not (equal nval value))
  9821. (setq eval '(org-entry-put pom key nval)))))
  9822. (when eval
  9823. (let ((inhibit-read-only t))
  9824. (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
  9825. (unwind-protect
  9826. (progn
  9827. (setq org-columns-overlays
  9828. (org-delete-all line-overlays org-columns-overlays))
  9829. (mapc 'org-delete-overlay line-overlays)
  9830. (org-columns-eval eval))
  9831. (org-columns-display-here))))
  9832. (move-to-column col)
  9833. (if (and (org-mode-p)
  9834. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9835. (org-columns-update key))))
  9836. (defun org-edit-headline () ; FIXME: this is not columns specific. Make interactive????? Use from agenda????
  9837. "Edit the current headline, the part without TODO keyword, TAGS."
  9838. (org-back-to-heading)
  9839. (when (looking-at org-todo-line-regexp)
  9840. (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
  9841. (txt (match-string 3))
  9842. (post "")
  9843. txt2)
  9844. (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
  9845. (setq post (match-string 0 txt)
  9846. txt (substring txt 0 (match-beginning 0))))
  9847. (setq txt2 (read-string "Edit: " txt))
  9848. (when (not (equal txt txt2))
  9849. (beginning-of-line 1)
  9850. (insert pre txt2 post)
  9851. (delete-region (point) (point-at-eol))
  9852. (org-set-tags nil t)))))
  9853. (defun org-columns-edit-allowed ()
  9854. "Edit the list of allowed values for the current property."
  9855. (interactive)
  9856. (let* ((key (get-char-property (point) 'org-columns-key))
  9857. (key1 (concat key "_ALL"))
  9858. (allowed (org-entry-get (point) key1 t))
  9859. nval)
  9860. ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
  9861. (setq nval (read-string "Allowed: " allowed))
  9862. (org-entry-put
  9863. (cond ((marker-position org-entry-property-inherited-from)
  9864. org-entry-property-inherited-from)
  9865. ((marker-position org-columns-top-level-marker)
  9866. org-columns-top-level-marker))
  9867. key1 nval)))
  9868. (defun org-columns-eval (form)
  9869. (let (hidep)
  9870. (save-excursion
  9871. (beginning-of-line 1)
  9872. ;; `next-line' is needed here, because it skips invisible line.
  9873. (condition-case nil (org-no-warnings (next-line 1)) (error nil))
  9874. (setq hidep (org-on-heading-p 1)))
  9875. (eval form)
  9876. (and hidep (hide-entry))))
  9877. (defun org-columns-previous-allowed-value ()
  9878. "Switch to the previous allowed value for this column."
  9879. (interactive)
  9880. (org-columns-next-allowed-value t))
  9881. (defun org-columns-next-allowed-value (&optional previous)
  9882. "Switch to the next allowed value for this column."
  9883. (interactive)
  9884. (org-columns-check-computed)
  9885. (let* ((col (current-column))
  9886. (key (get-char-property (point) 'org-columns-key))
  9887. (value (get-char-property (point) 'org-columns-value))
  9888. (bol (point-at-bol)) (eol (point-at-eol))
  9889. (pom (or (get-text-property bol 'org-hd-marker)
  9890. (point))) ; keep despite of compiler waring
  9891. (line-overlays
  9892. (delq nil (mapcar (lambda (x)
  9893. (and (eq (overlay-buffer x) (current-buffer))
  9894. (>= (overlay-start x) bol)
  9895. (<= (overlay-start x) eol)
  9896. x))
  9897. org-columns-overlays)))
  9898. (allowed (or (org-property-get-allowed-values pom key)
  9899. (and (memq
  9900. (nth 4 (assoc key org-columns-current-fmt-compiled))
  9901. '(checkbox checkbox-n-of-m checkbox-percent))
  9902. '("[ ]" "[X]"))))
  9903. nval)
  9904. (when (equal key "ITEM")
  9905. (error "Cannot edit item headline from here"))
  9906. (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
  9907. (error "Allowed values for this property have not been defined"))
  9908. (if (member key '("SCHEDULED" "DEADLINE"))
  9909. (setq nval (if previous 'earlier 'later))
  9910. (if previous (setq allowed (reverse allowed)))
  9911. (if (member value allowed)
  9912. (setq nval (car (cdr (member value allowed)))))
  9913. (setq nval (or nval (car allowed)))
  9914. (if (equal nval value)
  9915. (error "Only one allowed value for this property")))
  9916. (let ((inhibit-read-only t))
  9917. (remove-text-properties (1- bol) eol '(read-only t))
  9918. (unwind-protect
  9919. (progn
  9920. (setq org-columns-overlays
  9921. (org-delete-all line-overlays org-columns-overlays))
  9922. (mapc 'org-delete-overlay line-overlays)
  9923. (org-columns-eval '(org-entry-put pom key nval)))
  9924. (org-columns-display-here)))
  9925. (move-to-column col)
  9926. (if (and (org-mode-p)
  9927. (nth 3 (assoc key org-columns-current-fmt-compiled)))
  9928. (org-columns-update key))))
  9929. (defun org-verify-version (task)
  9930. (cond
  9931. ((eq task 'columns)
  9932. (if (or (featurep 'xemacs)
  9933. (< emacs-major-version 22))
  9934. (error "Emacs 22 is required for the columns feature")))))
  9935. (defun org-columns-open-link (&optional arg)
  9936. (interactive "P")
  9937. (let ((value (get-char-property (point) 'org-columns-value)))
  9938. (org-open-link-from-string value arg)))
  9939. (defun org-open-link-from-string (s &optional arg)
  9940. "Open a link in the string S, as if it was in Org-mode."
  9941. (interactive)
  9942. (with-temp-buffer
  9943. (let ((org-inhibit-startup t))
  9944. (org-mode)
  9945. (insert s)
  9946. (goto-char (point-min))
  9947. (org-open-at-point arg))))
  9948. (defun org-columns-get-format-and-top-level ()
  9949. (let (fmt)
  9950. (when (condition-case nil (org-back-to-heading) (error nil))
  9951. (move-marker org-entry-property-inherited-from nil)
  9952. (setq fmt (org-entry-get nil "COLUMNS" t)))
  9953. (setq fmt (or fmt org-columns-default-format))
  9954. (org-set-local 'org-columns-current-fmt fmt)
  9955. (org-columns-compile-format fmt)
  9956. (if (marker-position org-entry-property-inherited-from)
  9957. (move-marker org-columns-top-level-marker
  9958. org-entry-property-inherited-from)
  9959. (move-marker org-columns-top-level-marker (point)))
  9960. fmt))
  9961. (defun org-columns ()
  9962. "Turn on column view on an org-mode file."
  9963. (interactive)
  9964. (org-verify-version 'columns)
  9965. (org-columns-remove-overlays)
  9966. (move-marker org-columns-begin-marker (point))
  9967. (let (beg end fmt cache maxwidths)
  9968. (setq fmt (org-columns-get-format-and-top-level))
  9969. (save-excursion
  9970. (goto-char org-columns-top-level-marker)
  9971. (setq beg (point))
  9972. (unless org-columns-inhibit-recalculation
  9973. (org-columns-compute-all))
  9974. (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
  9975. (point-max)))
  9976. ;; Get and cache the properties
  9977. (goto-char beg)
  9978. (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
  9979. (save-excursion
  9980. (save-restriction
  9981. (narrow-to-region beg end)
  9982. (org-clock-sum))))
  9983. (while (re-search-forward (concat "^" outline-regexp) end t)
  9984. (push (cons (org-current-line) (org-entry-properties)) cache))
  9985. (when cache
  9986. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  9987. (org-set-local 'org-columns-current-maxwidths maxwidths)
  9988. (org-columns-display-here-title)
  9989. (mapc (lambda (x)
  9990. (goto-line (car x))
  9991. (org-columns-display-here (cdr x)))
  9992. cache)))))
  9993. (defun org-columns-new (&optional prop title width op fmt &rest rest)
  9994. "Insert a new column, to the left of the current column."
  9995. (interactive)
  9996. (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
  9997. cell)
  9998. (setq prop (completing-read
  9999. "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
  10000. nil nil prop))
  10001. (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
  10002. (setq width (read-string "Column width: " (if width (number-to-string width))))
  10003. (if (string-match "\\S-" width)
  10004. (setq width (string-to-number width))
  10005. (setq width nil))
  10006. (setq fmt (completing-read "Summary [none]: "
  10007. '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
  10008. nil t))
  10009. (if (string-match "\\S-" fmt)
  10010. (setq fmt (intern fmt))
  10011. (setq fmt nil))
  10012. (if (eq fmt 'none) (setq fmt nil))
  10013. (if editp
  10014. (progn
  10015. (setcar editp prop)
  10016. (setcdr editp (list title width nil fmt)))
  10017. (setq cell (nthcdr (1- (current-column))
  10018. org-columns-current-fmt-compiled))
  10019. (setcdr cell (cons (list prop title width nil fmt)
  10020. (cdr cell))))
  10021. (org-columns-store-format)
  10022. (org-columns-redo)))
  10023. (defun org-columns-delete ()
  10024. "Delete the column at point from columns view."
  10025. (interactive)
  10026. (let* ((n (current-column))
  10027. (title (nth 1 (nth n org-columns-current-fmt-compiled))))
  10028. (when (y-or-n-p
  10029. (format "Are you sure you want to remove column \"%s\"? " title))
  10030. (setq org-columns-current-fmt-compiled
  10031. (delq (nth n org-columns-current-fmt-compiled)
  10032. org-columns-current-fmt-compiled))
  10033. (org-columns-store-format)
  10034. (org-columns-redo)
  10035. (if (>= (current-column) (length org-columns-current-fmt-compiled))
  10036. (backward-char 1)))))
  10037. (defun org-columns-edit-attributes ()
  10038. "Edit the attributes of the current column."
  10039. (interactive)
  10040. (let* ((n (current-column))
  10041. (info (nth n org-columns-current-fmt-compiled)))
  10042. (apply 'org-columns-new info)))
  10043. (defun org-columns-widen (arg)
  10044. "Make the column wider by ARG characters."
  10045. (interactive "p")
  10046. (let* ((n (current-column))
  10047. (entry (nth n org-columns-current-fmt-compiled))
  10048. (width (or (nth 2 entry)
  10049. (cdr (assoc (car entry) org-columns-current-maxwidths)))))
  10050. (setq width (max 1 (+ width arg)))
  10051. (setcar (nthcdr 2 entry) width)
  10052. (org-columns-store-format)
  10053. (org-columns-redo)))
  10054. (defun org-columns-narrow (arg)
  10055. "Make the column nrrower by ARG characters."
  10056. (interactive "p")
  10057. (org-columns-widen (- arg)))
  10058. (defun org-columns-move-right ()
  10059. "Swap this column with the one to the right."
  10060. (interactive)
  10061. (let* ((n (current-column))
  10062. (cell (nthcdr n org-columns-current-fmt-compiled))
  10063. e)
  10064. (when (>= n (1- (length org-columns-current-fmt-compiled)))
  10065. (error "Cannot shift this column further to the right"))
  10066. (setq e (car cell))
  10067. (setcar cell (car (cdr cell)))
  10068. (setcdr cell (cons e (cdr (cdr cell))))
  10069. (org-columns-store-format)
  10070. (org-columns-redo)
  10071. (forward-char 1)))
  10072. (defun org-columns-move-left ()
  10073. "Swap this column with the one to the left."
  10074. (interactive)
  10075. (let* ((n (current-column)))
  10076. (when (= n 0)
  10077. (error "Cannot shift this column further to the left"))
  10078. (backward-char 1)
  10079. (org-columns-move-right)
  10080. (backward-char 1)))
  10081. (defun org-columns-store-format ()
  10082. "Store the text version of the current columns format in appropriate place.
  10083. This is either in the COLUMNS property of the node starting the current column
  10084. display, or in the #+COLUMNS line of the current buffer."
  10085. (let (fmt (cnt 0))
  10086. (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
  10087. (org-set-local 'org-columns-current-fmt fmt)
  10088. (if (marker-position org-columns-top-level-marker)
  10089. (save-excursion
  10090. (goto-char org-columns-top-level-marker)
  10091. (if (and (org-at-heading-p)
  10092. (org-entry-get nil "COLUMNS"))
  10093. (org-entry-put nil "COLUMNS" fmt)
  10094. (goto-char (point-min))
  10095. ;; Overwrite all #+COLUMNS lines....
  10096. (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
  10097. (setq cnt (1+ cnt))
  10098. (replace-match (concat "#+COLUMNS: " fmt) t t))
  10099. (unless (> cnt 0)
  10100. (goto-char (point-min))
  10101. (or (org-on-heading-p t) (outline-next-heading))
  10102. (let ((inhibit-read-only t))
  10103. (insert-before-markers "#+COLUMNS: " fmt "\n")))
  10104. (org-set-local 'org-columns-default-format fmt))))))
  10105. (defvar org-overriding-columns-format nil
  10106. "When set, overrides any other definition.")
  10107. (defvar org-agenda-view-columns-initially nil
  10108. "When set, switch to columns view immediately after creating the agenda.")
  10109. (defun org-agenda-columns ()
  10110. "Turn on column view in the agenda."
  10111. (interactive)
  10112. (org-verify-version 'columns)
  10113. (org-columns-remove-overlays)
  10114. (move-marker org-columns-begin-marker (point))
  10115. (let (fmt cache maxwidths m)
  10116. (cond
  10117. ((and (local-variable-p 'org-overriding-columns-format)
  10118. org-overriding-columns-format)
  10119. (setq fmt org-overriding-columns-format))
  10120. ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
  10121. (setq fmt (org-entry-get m "COLUMNS" t)))
  10122. ((and (boundp 'org-columns-current-fmt)
  10123. (local-variable-p 'org-columns-current-fmt)
  10124. org-columns-current-fmt)
  10125. (setq fmt org-columns-current-fmt))
  10126. ((setq m (next-single-property-change (point-min) 'org-hd-marker))
  10127. (setq m (get-text-property m 'org-hd-marker))
  10128. (setq fmt (org-entry-get m "COLUMNS" t))))
  10129. (setq fmt (or fmt org-columns-default-format))
  10130. (org-set-local 'org-columns-current-fmt fmt)
  10131. (org-columns-compile-format fmt)
  10132. (save-excursion
  10133. ;; Get and cache the properties
  10134. (goto-char (point-min))
  10135. (while (not (eobp))
  10136. (when (setq m (or (get-text-property (point) 'org-hd-marker)
  10137. (get-text-property (point) 'org-marker)))
  10138. (push (cons (org-current-line) (org-entry-properties m)) cache))
  10139. (beginning-of-line 2))
  10140. (when cache
  10141. (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
  10142. (org-set-local 'org-columns-current-maxwidths maxwidths)
  10143. (org-columns-display-here-title)
  10144. (mapc (lambda (x)
  10145. (goto-line (car x))
  10146. (org-columns-display-here (cdr x)))
  10147. cache)))))
  10148. (defun org-columns-get-autowidth-alist (s cache)
  10149. "Derive the maximum column widths from the format and the cache."
  10150. (let ((start 0) rtn)
  10151. (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
  10152. (push (cons (match-string 1 s) 1) rtn)
  10153. (setq start (match-end 0)))
  10154. (mapc (lambda (x)
  10155. (setcdr x (apply 'max
  10156. (mapcar
  10157. (lambda (y)
  10158. (length (or (cdr (assoc (car x) (cdr y))) " ")))
  10159. cache))))
  10160. rtn)
  10161. rtn))
  10162. (defun org-columns-compute-all ()
  10163. "Compute all columns that have operators defined."
  10164. (org-unmodified
  10165. (remove-text-properties (point-min) (point-max) '(org-summaries t)))
  10166. (let ((columns org-columns-current-fmt-compiled) col)
  10167. (while (setq col (pop columns))
  10168. (when (nth 3 col)
  10169. (save-excursion
  10170. (org-columns-compute (car col)))))))
  10171. (defun org-columns-update (property)
  10172. "Recompute PROPERTY, and update the columns display for it."
  10173. (org-columns-compute property)
  10174. (let (fmt val pos)
  10175. (save-excursion
  10176. (mapc (lambda (ov)
  10177. (when (equal (org-overlay-get ov 'org-columns-key) property)
  10178. (setq pos (org-overlay-start ov))
  10179. (goto-char pos)
  10180. (when (setq val (cdr (assoc property
  10181. (get-text-property
  10182. (point-at-bol) 'org-summaries))))
  10183. (setq fmt (org-overlay-get ov 'org-columns-format))
  10184. (org-overlay-put ov 'org-columns-value val)
  10185. (org-overlay-put ov 'display (format fmt val)))))
  10186. org-columns-overlays))))
  10187. (defun org-columns-compute (property)
  10188. "Sum the values of property PROPERTY hierarchically, for the entire buffer."
  10189. (interactive)
  10190. (let* ((re (concat "^" outline-regexp))
  10191. (lmax 30) ; Does anyone use deeper levels???
  10192. (lsum (make-vector lmax 0))
  10193. (lflag (make-vector lmax nil))
  10194. (level 0)
  10195. (ass (assoc property org-columns-current-fmt-compiled))
  10196. (format (nth 4 ass))
  10197. (printf (nth 5 ass))
  10198. (beg org-columns-top-level-marker)
  10199. last-level val valflag flag end sumpos sum-alist sum str str1 useval)
  10200. (save-excursion
  10201. ;; Find the region to compute
  10202. (goto-char beg)
  10203. (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
  10204. (goto-char end)
  10205. ;; Walk the tree from the back and do the computations
  10206. (while (re-search-backward re beg t)
  10207. (setq sumpos (match-beginning 0)
  10208. last-level level
  10209. level (org-outline-level)
  10210. val (org-entry-get nil property)
  10211. valflag (and val (string-match "\\S-" val)))
  10212. (cond
  10213. ((< level last-level)
  10214. ;; put the sum of lower levels here as a property
  10215. (setq sum (aref lsum last-level) ; current sum
  10216. flag (aref lflag last-level) ; any valid entries from children?
  10217. str (org-column-number-to-string sum format printf)
  10218. str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
  10219. useval (if flag str1 (if valflag val ""))
  10220. sum-alist (get-text-property sumpos 'org-summaries))
  10221. (if (assoc property sum-alist)
  10222. (setcdr (assoc property sum-alist) useval)
  10223. (push (cons property useval) sum-alist)
  10224. (org-unmodified
  10225. (add-text-properties sumpos (1+ sumpos)
  10226. (list 'org-summaries sum-alist))))
  10227. (when val
  10228. (org-entry-put nil property (if flag str val)))
  10229. ;; add current to current level accumulator
  10230. (when (or flag valflag)
  10231. (aset lsum level (+ (aref lsum level)
  10232. (if flag sum (org-column-string-to-number
  10233. (if flag str val) format))))
  10234. (aset lflag level t))
  10235. ;; clear accumulators for deeper levels
  10236. (loop for l from (1+ level) to (1- lmax) do
  10237. (aset lsum l 0)
  10238. (aset lflag l nil)))
  10239. ((>= level last-level)
  10240. ;; add what we have here to the accumulator for this level
  10241. (aset lsum level (+ (aref lsum level)
  10242. (org-column-string-to-number (or val "0") format)))
  10243. (and valflag (aset lflag level t)))
  10244. (t (error "This should not happen")))))))
  10245. (defun org-columns-redo ()
  10246. "Construct the column display again."
  10247. (interactive)
  10248. (message "Recomputing columns...")
  10249. (save-excursion
  10250. (if (marker-position org-columns-begin-marker)
  10251. (goto-char org-columns-begin-marker))
  10252. (org-columns-remove-overlays)
  10253. (if (org-mode-p)
  10254. (call-interactively 'org-columns)
  10255. (call-interactively 'org-agenda-columns)))
  10256. (message "Recomputing columns...done"))
  10257. (defun org-columns-not-in-agenda ()
  10258. (if (eq major-mode 'org-agenda-mode)
  10259. (error "This command is only allowed in Org-mode buffers")))
  10260. (defun org-string-to-number (s)
  10261. "Convert string to number, and interpret hh:mm:ss."
  10262. (if (not (string-match ":" s))
  10263. (string-to-number s)
  10264. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10265. (while l
  10266. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10267. sum)))
  10268. (defun org-column-number-to-string (n fmt &optional printf)
  10269. "Convert a computed column number to a string value, according to FMT."
  10270. (cond
  10271. ((eq fmt 'add_times)
  10272. (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
  10273. (format "%d:%02d" h m)))
  10274. ((eq fmt 'checkbox)
  10275. (cond ((= n (floor n)) "[X]")
  10276. ((> n 1.) "[-]")
  10277. (t "[ ]")))
  10278. ((memq fmt '(checkbox-n-of-m checkbox-percent))
  10279. (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
  10280. (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
  10281. (printf (format printf n))
  10282. ((eq fmt 'currency)
  10283. (format "%.2f" n))
  10284. (t (number-to-string n))))
  10285. (defun org-nofm-to-completion (n m &optional percent)
  10286. (if (not percent)
  10287. (format "[%d/%d]" n m)
  10288. (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
  10289. (defun org-column-string-to-number (s fmt)
  10290. "Convert a column value to a number that can be used for column computing."
  10291. (cond
  10292. ((string-match ":" s)
  10293. (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
  10294. (while l
  10295. (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
  10296. sum))
  10297. ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
  10298. (if (equal s "[X]") 1. 0.000001))
  10299. (t (string-to-number s))))
  10300. (defun org-columns-uncompile-format (cfmt)
  10301. "Turn the compiled columns format back into a string representation."
  10302. (let ((rtn "") e s prop title op width fmt printf)
  10303. (while (setq e (pop cfmt))
  10304. (setq prop (car e)
  10305. title (nth 1 e)
  10306. width (nth 2 e)
  10307. op (nth 3 e)
  10308. fmt (nth 4 e)
  10309. printf (nth 5 e))
  10310. (cond
  10311. ((eq fmt 'add_times) (setq op ":"))
  10312. ((eq fmt 'checkbox) (setq op "X"))
  10313. ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
  10314. ((eq fmt 'checkbox-percent) (setq op "X%"))
  10315. ((eq fmt 'add_numbers) (setq op "+"))
  10316. ((eq fmt 'currency) (setq op "$")))
  10317. (if (and op printf) (setq op (concat op ";" printf)))
  10318. (if (equal title prop) (setq title nil))
  10319. (setq s (concat "%" (if width (number-to-string width))
  10320. prop
  10321. (if title (concat "(" title ")"))
  10322. (if op (concat "{" op "}"))))
  10323. (setq rtn (concat rtn " " s)))
  10324. (org-trim rtn)))
  10325. (defun org-columns-compile-format (fmt)
  10326. "Turn a column format string into an alist of specifications.
  10327. The alist has one entry for each column in the format. The elements of
  10328. that list are:
  10329. property the property
  10330. title the title field for the columns
  10331. width the column width in characters, can be nil for automatic
  10332. operator the operator if any
  10333. format the output format for computed results, derived from operator
  10334. printf a printf format for computed values"
  10335. (let ((start 0) width prop title op f printf)
  10336. (setq org-columns-current-fmt-compiled nil)
  10337. (while (string-match
  10338. (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
  10339. fmt start)
  10340. (setq start (match-end 0)
  10341. width (match-string 1 fmt)
  10342. prop (match-string 2 fmt)
  10343. title (or (match-string 3 fmt) prop)
  10344. op (match-string 4 fmt)
  10345. f nil
  10346. printf nil)
  10347. (if width (setq width (string-to-number width)))
  10348. (when (and op (string-match ";" op))
  10349. (setq printf (substring op (match-end 0))
  10350. op (substring op 0 (match-beginning 0))))
  10351. (cond
  10352. ((equal op "+") (setq f 'add_numbers))
  10353. ((equal op "$") (setq f 'currency))
  10354. ((equal op ":") (setq f 'add_times))
  10355. ((equal op "X") (setq f 'checkbox))
  10356. ((equal op "X/") (setq f 'checkbox-n-of-m))
  10357. ((equal op "X%") (setq f 'checkbox-percent))
  10358. )
  10359. (push (list prop title width op f printf) org-columns-current-fmt-compiled))
  10360. (setq org-columns-current-fmt-compiled
  10361. (nreverse org-columns-current-fmt-compiled))))
  10362. ;;; Dynamic block for Column view
  10363. (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
  10364. "Get the column view of the current buffer or subtree.
  10365. The first optional argument MAXLEVEL sets the level limit. A
  10366. second optional argument SKIP-EMPTY-ROWS tells whether to skip
  10367. empty rows, an empty row being one where all the column view
  10368. specifiers except ITEM are empty. This function returns a list
  10369. containing the title row and all other rows. Each row is a list
  10370. of fields."
  10371. (save-excursion
  10372. (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
  10373. (n (length title)) row tbl)
  10374. (goto-char (point-min))
  10375. (while (and (re-search-forward "^\\(\\*+\\) " nil t)
  10376. (or (null maxlevel)
  10377. (>= maxlevel
  10378. (if org-odd-levels-only
  10379. (/ (1+ (length (match-string 1))) 2)
  10380. (length (match-string 1))))))
  10381. (when (get-char-property (match-beginning 0) 'org-columns-key)
  10382. (setq row nil)
  10383. (loop for i from 0 to (1- n) do
  10384. (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
  10385. (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
  10386. "")
  10387. row))
  10388. (setq row (nreverse row))
  10389. (unless (and skip-empty-rows
  10390. (eq 1 (length (delete "" (delete-dups row)))))
  10391. (push row tbl))))
  10392. (append (list title 'hline) (nreverse tbl)))))
  10393. (defun org-dblock-write:columnview (params)
  10394. "Write the column view table.
  10395. PARAMS is a property list of parameters:
  10396. :width enforce same column widths with <N> specifiers.
  10397. :id the :ID: property of the entry where the columns view
  10398. should be built, as a string. When `local', call locally.
  10399. When `global' call column view with the cursor at the beginning
  10400. of the buffer (usually this means that the whole buffer switches
  10401. to column view).
  10402. :hlines When t, insert a hline before each item. When a number, insert
  10403. a hline before each level <= that number.
  10404. :vlines When t, make each column a colgroup to enforce vertical lines.
  10405. :maxlevel When set to a number, don't capture headlines below this level.
  10406. :skip-empty-rows
  10407. When t, skip rows where all specifiers other than ITEM are empty."
  10408. (let ((pos (move-marker (make-marker) (point)))
  10409. (hlines (plist-get params :hlines))
  10410. (vlines (plist-get params :vlines))
  10411. (maxlevel (plist-get params :maxlevel))
  10412. (skip-empty-rows (plist-get params :skip-empty-rows))
  10413. tbl id idpos nfields tmp)
  10414. (save-excursion
  10415. (save-restriction
  10416. (when (setq id (plist-get params :id))
  10417. (cond ((not id) nil)
  10418. ((eq id 'global) (goto-char (point-min)))
  10419. ((eq id 'local) nil)
  10420. ((setq idpos (org-find-entry-with-id id))
  10421. (goto-char idpos))
  10422. (t (error "Cannot find entry with :ID: %s" id))))
  10423. (org-columns)
  10424. (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
  10425. (setq nfields (length (car tbl)))
  10426. (org-columns-quit)))
  10427. (goto-char pos)
  10428. (move-marker pos nil)
  10429. (when tbl
  10430. (when (plist-get params :hlines)
  10431. (setq tmp nil)
  10432. (while tbl
  10433. (if (eq (car tbl) 'hline)
  10434. (push (pop tbl) tmp)
  10435. (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
  10436. (if (and (not (eq (car tmp) 'hline))
  10437. (or (eq hlines t)
  10438. (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
  10439. (push 'hline tmp)))
  10440. (push (pop tbl) tmp)))
  10441. (setq tbl (nreverse tmp)))
  10442. (when vlines
  10443. (setq tbl (mapcar (lambda (x)
  10444. (if (eq 'hline x) x (cons "" x)))
  10445. tbl))
  10446. (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
  10447. (setq pos (point))
  10448. (insert (org-listtable-to-string tbl))
  10449. (when (plist-get params :width)
  10450. (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
  10451. org-columns-current-widths "|")))
  10452. (goto-char pos)
  10453. (org-table-align))))
  10454. (defun org-listtable-to-string (tbl)
  10455. "Convert a listtable TBL to a string that contains the Org-mode table.
  10456. The table still need to be alligned. The resulting string has no leading
  10457. and tailing newline characters."
  10458. (mapconcat
  10459. (lambda (x)
  10460. (cond
  10461. ((listp x)
  10462. (concat "|" (mapconcat 'identity x "|") "|"))
  10463. ((eq x 'hline) "|-|")
  10464. (t (error "Garbage in listtable: %s" x))))
  10465. tbl "\n"))
  10466. (defun org-insert-columns-dblock ()
  10467. "Create a dynamic block capturing a column view table."
  10468. (interactive)
  10469. (let ((defaults '(:name "columnview" :hlines 1))
  10470. (id (completing-read
  10471. "Capture columns (local, global, entry with :ID: property) [local]: "
  10472. (append '(("global") ("local"))
  10473. (mapcar 'list (org-property-values "ID"))))))
  10474. (if (equal id "") (setq id 'local))
  10475. (if (equal id "global") (setq id 'global))
  10476. (setq defaults (append defaults (list :id id)))
  10477. (org-create-dblock defaults)
  10478. (org-update-dblock)))
  10479. ;;;; Timestamps
  10480. (defvar org-last-changed-timestamp nil)
  10481. (defvar org-time-was-given) ; dynamically scoped parameter
  10482. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10483. (defvar org-ts-what) ; dynamically scoped parameter
  10484. (defun org-time-stamp (arg)
  10485. "Prompt for a date/time and insert a time stamp.
  10486. If the user specifies a time like HH:MM, or if this command is called
  10487. with a prefix argument, the time stamp will contain date and time.
  10488. Otherwise, only the date will be included. All parts of a date not
  10489. specified by the user will be filled in from the current date/time.
  10490. So if you press just return without typing anything, the time stamp
  10491. will represent the current date/time. If there is already a timestamp
  10492. at the cursor, it will be modified."
  10493. (interactive "P")
  10494. (let* ((ts nil)
  10495. (default-time
  10496. ;; Default time is either today, or, when entering a range,
  10497. ;; the range start.
  10498. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10499. (save-excursion
  10500. (re-search-backward
  10501. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10502. (- (point) 20) t)))
  10503. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10504. (current-time)))
  10505. (default-input (and ts (org-get-compact-tod ts)))
  10506. org-time-was-given org-end-time-was-given time)
  10507. (cond
  10508. ((and (org-at-timestamp-p)
  10509. (eq last-command 'org-time-stamp)
  10510. (eq this-command 'org-time-stamp))
  10511. (insert "--")
  10512. (setq time (let ((this-command this-command))
  10513. (org-read-date arg 'totime nil nil default-time default-input)))
  10514. (org-insert-time-stamp time (or org-time-was-given arg)))
  10515. ((org-at-timestamp-p)
  10516. (setq time (let ((this-command this-command))
  10517. (org-read-date arg 'totime nil nil default-time default-input)))
  10518. (when (org-at-timestamp-p) ; just to get the match data
  10519. (replace-match "")
  10520. (setq org-last-changed-timestamp
  10521. (org-insert-time-stamp
  10522. time (or org-time-was-given arg)
  10523. nil nil nil (list org-end-time-was-given))))
  10524. (message "Timestamp updated"))
  10525. (t
  10526. (setq time (let ((this-command this-command))
  10527. (org-read-date arg 'totime nil nil default-time default-input)))
  10528. (org-insert-time-stamp time (or org-time-was-given arg)
  10529. nil nil nil (list org-end-time-was-given))))))
  10530. ;; FIXME: can we use this for something else, like computing time differences?
  10531. (defun org-get-compact-tod (s)
  10532. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10533. (let* ((t1 (match-string 1 s))
  10534. (h1 (string-to-number (match-string 2 s)))
  10535. (m1 (string-to-number (match-string 3 s)))
  10536. (t2 (and (match-end 4) (match-string 5 s)))
  10537. (h2 (and t2 (string-to-number (match-string 6 s))))
  10538. (m2 (and t2 (string-to-number (match-string 7 s))))
  10539. dh dm)
  10540. (if (not t2)
  10541. t1
  10542. (setq dh (- h2 h1) dm (- m2 m1))
  10543. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10544. (concat t1 "+" (number-to-string dh)
  10545. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10546. (defun org-time-stamp-inactive (&optional arg)
  10547. "Insert an inactive time stamp.
  10548. An inactive time stamp is enclosed in square brackets instead of angle
  10549. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10550. does not link to the calendar and cannot be changed with the S-cursor keys.
  10551. So these are more for recording a certain time/date."
  10552. (interactive "P")
  10553. (let (org-time-was-given org-end-time-was-given time)
  10554. (setq time (org-read-date arg 'totime))
  10555. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  10556. nil nil (list org-end-time-was-given))))
  10557. (defvar org-date-ovl (org-make-overlay 1 1))
  10558. (org-overlay-put org-date-ovl 'face 'org-warning)
  10559. (org-detach-overlay org-date-ovl)
  10560. (defvar org-ans1) ; dynamically scoped parameter
  10561. (defvar org-ans2) ; dynamically scoped parameter
  10562. (defvar org-plain-time-of-day-regexp) ; defined below
  10563. (defvar org-read-date-overlay nil)
  10564. (defvar org-dcst nil) ; dynamically scoped
  10565. (defun org-read-date (&optional with-time to-time from-string prompt
  10566. default-time default-input)
  10567. "Read a date, possibly a time, and make things smooth for the user.
  10568. The prompt will suggest to enter an ISO date, but you can also enter anything
  10569. which will at least partially be understood by `parse-time-string'.
  10570. Unrecognized parts of the date will default to the current day, month, year,
  10571. hour and minute. If this command is called to replace a timestamp at point,
  10572. of to enter the second timestamp of a range, the default time is taken from the
  10573. existing stamp. For example,
  10574. 3-2-5 --> 2003-02-05
  10575. feb 15 --> currentyear-02-15
  10576. sep 12 9 --> 2009-09-12
  10577. 12:45 --> today 12:45
  10578. 22 sept 0:34 --> currentyear-09-22 0:34
  10579. 12 --> currentyear-currentmonth-12
  10580. Fri --> nearest Friday (today or later)
  10581. etc.
  10582. Furthermore you can specify a relative date by giving, as the *first* thing
  10583. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10584. change in days weeks, months, years.
  10585. With a single plus or minus, the date is relative to today. With a double
  10586. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10587. +4d --> four days from today
  10588. +4 --> same as above
  10589. +2w --> two weeks from today
  10590. ++5 --> five days from default date
  10591. The function understands only English month and weekday abbreviations,
  10592. but this can be configured with the variables `parse-time-months' and
  10593. `parse-time-weekdays'.
  10594. While prompting, a calendar is popped up - you can also select the
  10595. date with the mouse (button 1). The calendar shows a period of three
  10596. months. To scroll it to other months, use the keys `>' and `<'.
  10597. If you don't like the calendar, turn it off with
  10598. \(setq org-read-date-popup-calendar nil)
  10599. With optional argument TO-TIME, the date will immediately be converted
  10600. to an internal time.
  10601. With an optional argument WITH-TIME, the prompt will suggest to also
  10602. insert a time. Note that when WITH-TIME is not set, you can still
  10603. enter a time, and this function will inform the calling routine about
  10604. this change. The calling routine may then choose to change the format
  10605. used to insert the time stamp into the buffer to include the time.
  10606. With optional argument FROM-STRING, read from this string instead from
  10607. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10608. the time/date that is used for everything that is not specified by the
  10609. user."
  10610. (require 'parse-time)
  10611. (let* ((org-time-stamp-rounding-minutes
  10612. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10613. (org-dcst org-display-custom-times)
  10614. (ct (org-current-time))
  10615. (def (or default-time ct))
  10616. (defdecode (decode-time def))
  10617. (dummy (progn
  10618. (when (< (nth 2 defdecode) org-extend-today-until)
  10619. (setcar (nthcdr 2 defdecode) -1)
  10620. (setcar (nthcdr 1 defdecode) 59)
  10621. (setq def (apply 'encode-time defdecode)
  10622. defdecode (decode-time def)))))
  10623. (calendar-move-hook nil)
  10624. (view-diary-entries-initially nil)
  10625. (view-calendar-holidays-initially nil)
  10626. (timestr (format-time-string
  10627. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10628. (prompt (concat (if prompt (concat prompt " ") "")
  10629. (format "Date+time [%s]: " timestr)))
  10630. ans (org-ans0 "") org-ans1 org-ans2 final)
  10631. (cond
  10632. (from-string (setq ans from-string))
  10633. (org-read-date-popup-calendar
  10634. (save-excursion
  10635. (save-window-excursion
  10636. (calendar)
  10637. (calendar-forward-day (- (time-to-days def)
  10638. (calendar-absolute-from-gregorian
  10639. (calendar-current-date))))
  10640. (org-eval-in-calendar nil t)
  10641. (let* ((old-map (current-local-map))
  10642. (map (copy-keymap calendar-mode-map))
  10643. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10644. (org-defkey map (kbd "RET") 'org-calendar-select)
  10645. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10646. 'org-calendar-select-mouse)
  10647. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10648. 'org-calendar-select-mouse)
  10649. (org-defkey minibuffer-local-map [(meta shift left)]
  10650. (lambda () (interactive)
  10651. (org-eval-in-calendar '(calendar-backward-month 1))))
  10652. (org-defkey minibuffer-local-map [(meta shift right)]
  10653. (lambda () (interactive)
  10654. (org-eval-in-calendar '(calendar-forward-month 1))))
  10655. (org-defkey minibuffer-local-map [(meta shift up)]
  10656. (lambda () (interactive)
  10657. (org-eval-in-calendar '(calendar-backward-year 1))))
  10658. (org-defkey minibuffer-local-map [(meta shift down)]
  10659. (lambda () (interactive)
  10660. (org-eval-in-calendar '(calendar-forward-year 1))))
  10661. (org-defkey minibuffer-local-map [(shift up)]
  10662. (lambda () (interactive)
  10663. (org-eval-in-calendar '(calendar-backward-week 1))))
  10664. (org-defkey minibuffer-local-map [(shift down)]
  10665. (lambda () (interactive)
  10666. (org-eval-in-calendar '(calendar-forward-week 1))))
  10667. (org-defkey minibuffer-local-map [(shift left)]
  10668. (lambda () (interactive)
  10669. (org-eval-in-calendar '(calendar-backward-day 1))))
  10670. (org-defkey minibuffer-local-map [(shift right)]
  10671. (lambda () (interactive)
  10672. (org-eval-in-calendar '(calendar-forward-day 1))))
  10673. (org-defkey minibuffer-local-map ">"
  10674. (lambda () (interactive)
  10675. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10676. (org-defkey minibuffer-local-map "<"
  10677. (lambda () (interactive)
  10678. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10679. (unwind-protect
  10680. (progn
  10681. (use-local-map map)
  10682. (add-hook 'post-command-hook 'org-read-date-display)
  10683. (setq org-ans0 (read-string prompt default-input nil nil))
  10684. ;; org-ans0: from prompt
  10685. ;; org-ans1: from mouse click
  10686. ;; org-ans2: from calendar motion
  10687. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10688. (remove-hook 'post-command-hook 'org-read-date-display)
  10689. (use-local-map old-map)
  10690. (when org-read-date-overlay
  10691. (org-delete-overlay org-read-date-overlay)
  10692. (setq org-read-date-overlay nil)))))))
  10693. (t ; Naked prompt only
  10694. (unwind-protect
  10695. (setq ans (read-string prompt default-input nil timestr))
  10696. (when org-read-date-overlay
  10697. (org-delete-overlay org-read-date-overlay)
  10698. (setq org-read-date-overlay nil)))))
  10699. (setq final (org-read-date-analyze ans def defdecode))
  10700. (if to-time
  10701. (apply 'encode-time final)
  10702. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10703. (format "%04d-%02d-%02d %02d:%02d"
  10704. (nth 5 final) (nth 4 final) (nth 3 final)
  10705. (nth 2 final) (nth 1 final))
  10706. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10707. (defvar def)
  10708. (defvar defdecode)
  10709. (defvar with-time)
  10710. (defun org-read-date-display ()
  10711. "Display the currrent date prompt interpretation in the minibuffer."
  10712. (when org-read-date-display-live
  10713. (when org-read-date-overlay
  10714. (org-delete-overlay org-read-date-overlay))
  10715. (let ((p (point)))
  10716. (end-of-line 1)
  10717. (while (not (equal (buffer-substring
  10718. (max (point-min) (- (point) 4)) (point))
  10719. " "))
  10720. (insert " "))
  10721. (goto-char p))
  10722. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10723. " " (or org-ans1 org-ans2)))
  10724. (org-end-time-was-given nil)
  10725. (f (org-read-date-analyze ans def defdecode))
  10726. (fmts (if org-dcst
  10727. org-time-stamp-custom-formats
  10728. org-time-stamp-formats))
  10729. (fmt (if (or with-time
  10730. (and (boundp 'org-time-was-given) org-time-was-given))
  10731. (cdr fmts)
  10732. (car fmts)))
  10733. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10734. (when (and org-end-time-was-given
  10735. (string-match org-plain-time-of-day-regexp txt))
  10736. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10737. org-end-time-was-given
  10738. (substring txt (match-end 0)))))
  10739. (setq org-read-date-overlay
  10740. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  10741. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10742. (defun org-read-date-analyze (ans def defdecode)
  10743. "Analyze the combined answer of the date prompt."
  10744. ;; FIXME: cleanup and comment
  10745. (let (delta deltan deltaw deltadef year month day
  10746. hour minute second wday pm h2 m2 tl wday1
  10747. iso-year iso-weekday iso-week iso-year iso-date)
  10748. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10749. (setq ans (replace-match "" t t ans)
  10750. deltan (car delta)
  10751. deltaw (nth 1 delta)
  10752. deltadef (nth 2 delta)))
  10753. ;; Check if there is an iso week date in there
  10754. ;; If yes, sore the info and ostpone interpreting it until the rest
  10755. ;; of the parsing is done
  10756. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10757. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10758. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10759. iso-week (string-to-number (match-string 2 ans)))
  10760. (setq ans (replace-match "" t t ans)))
  10761. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10762. (when (string-match
  10763. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10764. (setq year (if (match-end 2)
  10765. (string-to-number (match-string 2 ans))
  10766. (string-to-number (format-time-string "%Y")))
  10767. month (string-to-number (match-string 3 ans))
  10768. day (string-to-number (match-string 4 ans)))
  10769. (if (< year 100) (setq year (+ 2000 year)))
  10770. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10771. t nil ans)))
  10772. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10773. ;; If there is a time with am/pm, and *no* time without it, we convert
  10774. ;; so that matching will be successful.
  10775. (loop for i from 1 to 2 do ; twice, for end time as well
  10776. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10777. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10778. (setq hour (string-to-number (match-string 1 ans))
  10779. minute (if (match-end 3)
  10780. (string-to-number (match-string 3 ans))
  10781. 0)
  10782. pm (equal ?p
  10783. (string-to-char (downcase (match-string 4 ans)))))
  10784. (if (and (= hour 12) (not pm))
  10785. (setq hour 0)
  10786. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10787. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10788. t t ans))))
  10789. ;; Check if a time range is given as a duration
  10790. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10791. (setq hour (string-to-number (match-string 1 ans))
  10792. h2 (+ hour (string-to-number (match-string 3 ans)))
  10793. minute (string-to-number (match-string 2 ans))
  10794. m2 (+ minute (if (match-end 5) (string-to-number
  10795. (match-string 5 ans))0)))
  10796. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10797. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10798. t t ans)))
  10799. ;; Check if there is a time range
  10800. (when (boundp 'org-end-time-was-given)
  10801. (setq org-time-was-given nil)
  10802. (when (and (string-match org-plain-time-of-day-regexp ans)
  10803. (match-end 8))
  10804. (setq org-end-time-was-given (match-string 8 ans))
  10805. (setq ans (concat (substring ans 0 (match-beginning 7))
  10806. (substring ans (match-end 7))))))
  10807. (setq tl (parse-time-string ans)
  10808. day (or (nth 3 tl) (nth 3 defdecode))
  10809. month (or (nth 4 tl)
  10810. (if (and org-read-date-prefer-future
  10811. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10812. (1+ (nth 4 defdecode))
  10813. (nth 4 defdecode)))
  10814. year (or (nth 5 tl)
  10815. (if (and org-read-date-prefer-future
  10816. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10817. (1+ (nth 5 defdecode))
  10818. (nth 5 defdecode)))
  10819. hour (or (nth 2 tl) (nth 2 defdecode))
  10820. minute (or (nth 1 tl) (nth 1 defdecode))
  10821. second (or (nth 0 tl) 0)
  10822. wday (nth 6 tl))
  10823. ;; Special date definitions below
  10824. (cond
  10825. (iso-week
  10826. ;; There was an iso week
  10827. (setq year (or iso-year year)
  10828. day (or iso-weekday wday 1)
  10829. wday nil ; to make sure that the trigger below does not match
  10830. iso-date (calendar-gregorian-from-absolute
  10831. (calendar-absolute-from-iso
  10832. (list iso-week day year))))
  10833. ; FIXME: Should we also push ISO weeks into the future?
  10834. ; (when (and org-read-date-prefer-future
  10835. ; (not iso-year)
  10836. ; (< (calendar-absolute-from-gregorian iso-date)
  10837. ; (time-to-days (current-time))))
  10838. ; (setq year (1+ year)
  10839. ; iso-date (calendar-gregorian-from-absolute
  10840. ; (calendar-absolute-from-iso
  10841. ; (list iso-week day year)))))
  10842. (setq month (car iso-date)
  10843. year (nth 2 iso-date)
  10844. day (nth 1 iso-date)))
  10845. (deltan
  10846. (unless deltadef
  10847. (let ((now (decode-time (current-time))))
  10848. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10849. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10850. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10851. ((equal deltaw "m") (setq month (+ month deltan)))
  10852. ((equal deltaw "y") (setq year (+ year deltan)))))
  10853. ((and wday (not (nth 3 tl)))
  10854. ;; Weekday was given, but no day, so pick that day in the week
  10855. ;; on or after the derived date.
  10856. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10857. (unless (equal wday wday1)
  10858. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10859. (if (and (boundp 'org-time-was-given)
  10860. (nth 2 tl))
  10861. (setq org-time-was-given t))
  10862. (if (< year 100) (setq year (+ 2000 year)))
  10863. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10864. (list second minute hour day month year)))
  10865. (defvar parse-time-weekdays)
  10866. (defun org-read-date-get-relative (s today default)
  10867. "Check string S for special relative date string.
  10868. TODAY and DEFAULT are internal times, for today and for a default.
  10869. Return shift list (N what def-flag)
  10870. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10871. N is the number of WHATs to shift.
  10872. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10873. the DEFAULT date rather than TODAY."
  10874. (when (string-match
  10875. (concat
  10876. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  10877. "\\([0-9]+\\)?"
  10878. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10879. "\\([ \t]\\|$\\)") s)
  10880. (let* ((dir (if (match-end 1)
  10881. (string-to-char (substring (match-string 1 s) -1))
  10882. ?+))
  10883. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10884. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10885. (what (if (match-end 3) (match-string 3 s) "d"))
  10886. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10887. (date (if rel default today))
  10888. (wday (nth 6 (decode-time date)))
  10889. delta)
  10890. (if wday1
  10891. (progn
  10892. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10893. (if (= dir ?-) (setq delta (- delta 7)))
  10894. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10895. (list delta "d" rel))
  10896. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10897. (defun org-eval-in-calendar (form &optional keepdate)
  10898. "Eval FORM in the calendar window and return to current window.
  10899. Also, store the cursor date in variable org-ans2."
  10900. (let ((sw (selected-window)))
  10901. (select-window (get-buffer-window "*Calendar*"))
  10902. (eval form)
  10903. (when (and (not keepdate) (calendar-cursor-to-date))
  10904. (let* ((date (calendar-cursor-to-date))
  10905. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10906. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10907. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10908. (select-window sw)))
  10909. ; ;; Update the prompt to show new default date
  10910. ; (save-excursion
  10911. ; (goto-char (point-min))
  10912. ; (when (and org-ans2
  10913. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10914. ; (get-text-property (match-end 0) 'field))
  10915. ; (let ((inhibit-read-only t))
  10916. ; (replace-match (concat "[" org-ans2 "]") t t)
  10917. ; (add-text-properties (point-min) (1+ (match-end 0))
  10918. ; (text-properties-at (1+ (point-min)))))))))
  10919. (defun org-calendar-select ()
  10920. "Return to `org-read-date' with the date currently selected.
  10921. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10922. (interactive)
  10923. (when (calendar-cursor-to-date)
  10924. (let* ((date (calendar-cursor-to-date))
  10925. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10926. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10927. (if (active-minibuffer-window) (exit-minibuffer))))
  10928. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10929. "Insert a date stamp for the date given by the internal TIME.
  10930. WITH-HM means, use the stamp format that includes the time of the day.
  10931. INACTIVE means use square brackets instead of angular ones, so that the
  10932. stamp will not contribute to the agenda.
  10933. PRE and POST are optional strings to be inserted before and after the
  10934. stamp.
  10935. The command returns the inserted time stamp."
  10936. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10937. stamp)
  10938. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10939. (insert-before-markers (or pre ""))
  10940. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10941. (when (listp extra)
  10942. (setq extra (car extra))
  10943. (if (and (stringp extra)
  10944. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10945. (setq extra (format "-%02d:%02d"
  10946. (string-to-number (match-string 1 extra))
  10947. (string-to-number (match-string 2 extra))))
  10948. (setq extra nil)))
  10949. (when extra
  10950. (backward-char 1)
  10951. (insert-before-markers extra)
  10952. (forward-char 1))
  10953. (insert-before-markers (or post ""))
  10954. stamp))
  10955. (defun org-toggle-time-stamp-overlays ()
  10956. "Toggle the use of custom time stamp formats."
  10957. (interactive)
  10958. (setq org-display-custom-times (not org-display-custom-times))
  10959. (unless org-display-custom-times
  10960. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10961. (while (setq p (next-single-property-change p 'display))
  10962. (if (and (get-text-property p 'display)
  10963. (eq (get-text-property p 'face) 'org-date))
  10964. (remove-text-properties
  10965. p (setq p (next-single-property-change p 'display))
  10966. '(display t))))
  10967. (set-buffer-modified-p bmp)))
  10968. (if (featurep 'xemacs)
  10969. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10970. (org-restart-font-lock)
  10971. (setq org-table-may-need-update t)
  10972. (if org-display-custom-times
  10973. (message "Time stamps are overlayed with custom format")
  10974. (message "Time stamp overlays removed")))
  10975. (defun org-display-custom-time (beg end)
  10976. "Overlay modified time stamp format over timestamp between BED and END."
  10977. (let* ((ts (buffer-substring beg end))
  10978. t1 w1 with-hm tf time str w2 (off 0))
  10979. (save-match-data
  10980. (setq t1 (org-parse-time-string ts t))
  10981. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10982. (setq off (- (match-end 0) (match-beginning 0)))))
  10983. (setq end (- end off))
  10984. (setq w1 (- end beg)
  10985. with-hm (and (nth 1 t1) (nth 2 t1))
  10986. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10987. time (org-fix-decoded-time t1)
  10988. str (org-add-props
  10989. (format-time-string
  10990. (substring tf 1 -1) (apply 'encode-time time))
  10991. nil 'mouse-face 'highlight)
  10992. w2 (length str))
  10993. (if (not (= w2 w1))
  10994. (add-text-properties (1+ beg) (+ 2 beg)
  10995. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10996. (if (featurep 'xemacs)
  10997. (progn
  10998. (put-text-property beg end 'invisible t)
  10999. (put-text-property beg end 'end-glyph (make-glyph str)))
  11000. (put-text-property beg end 'display str))))
  11001. (defun org-translate-time (string)
  11002. "Translate all timestamps in STRING to custom format.
  11003. But do this only if the variable `org-display-custom-times' is set."
  11004. (when org-display-custom-times
  11005. (save-match-data
  11006. (let* ((start 0)
  11007. (re org-ts-regexp-both)
  11008. t1 with-hm inactive tf time str beg end)
  11009. (while (setq start (string-match re string start))
  11010. (setq beg (match-beginning 0)
  11011. end (match-end 0)
  11012. t1 (save-match-data
  11013. (org-parse-time-string (substring string beg end) t))
  11014. with-hm (and (nth 1 t1) (nth 2 t1))
  11015. inactive (equal (substring string beg (1+ beg)) "[")
  11016. tf (funcall (if with-hm 'cdr 'car)
  11017. org-time-stamp-custom-formats)
  11018. time (org-fix-decoded-time t1)
  11019. str (format-time-string
  11020. (concat
  11021. (if inactive "[" "<") (substring tf 1 -1)
  11022. (if inactive "]" ">"))
  11023. (apply 'encode-time time))
  11024. string (replace-match str t t string)
  11025. start (+ start (length str)))))))
  11026. string)
  11027. (defun org-fix-decoded-time (time)
  11028. "Set 0 instead of nil for the first 6 elements of time.
  11029. Don't touch the rest."
  11030. (let ((n 0))
  11031. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11032. (defun org-days-to-time (timestamp-string)
  11033. "Difference between TIMESTAMP-STRING and now in days."
  11034. (- (time-to-days (org-time-string-to-time timestamp-string))
  11035. (time-to-days (current-time))))
  11036. (defun org-deadline-close (timestamp-string &optional ndays)
  11037. "Is the time in TIMESTAMP-STRING close to the current date?"
  11038. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11039. (and (< (org-days-to-time timestamp-string) ndays)
  11040. (not (org-entry-is-done-p))))
  11041. (defun org-get-wdays (ts)
  11042. "Get the deadline lead time appropriate for timestring TS."
  11043. (cond
  11044. ((<= org-deadline-warning-days 0)
  11045. ;; 0 or negative, enforce this value no matter what
  11046. (- org-deadline-warning-days))
  11047. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  11048. ;; lead time is specified.
  11049. (floor (* (string-to-number (match-string 1 ts))
  11050. (cdr (assoc (match-string 2 ts)
  11051. '(("d" . 1) ("w" . 7)
  11052. ("m" . 30.4) ("y" . 365.25)))))))
  11053. ;; go for the default.
  11054. (t org-deadline-warning-days)))
  11055. (defun org-calendar-select-mouse (ev)
  11056. "Return to `org-read-date' with the date currently selected.
  11057. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11058. (interactive "e")
  11059. (mouse-set-point ev)
  11060. (when (calendar-cursor-to-date)
  11061. (let* ((date (calendar-cursor-to-date))
  11062. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11063. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11064. (if (active-minibuffer-window) (exit-minibuffer))))
  11065. (defun org-check-deadlines (ndays)
  11066. "Check if there are any deadlines due or past due.
  11067. A deadline is considered due if it happens within `org-deadline-warning-days'
  11068. days from today's date. If the deadline appears in an entry marked DONE,
  11069. it is not shown. The prefix arg NDAYS can be used to test that many
  11070. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11071. (interactive "P")
  11072. (let* ((org-warn-days
  11073. (cond
  11074. ((equal ndays '(4)) 100000)
  11075. (ndays (prefix-numeric-value ndays))
  11076. (t (abs org-deadline-warning-days))))
  11077. (case-fold-search nil)
  11078. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11079. (callback
  11080. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11081. (message "%d deadlines past-due or due within %d days"
  11082. (org-occur regexp nil callback)
  11083. org-warn-days)))
  11084. (defun org-check-before-date (date)
  11085. "Check if there are deadlines or scheduled entries before DATE."
  11086. (interactive (list (org-read-date)))
  11087. (let ((case-fold-search nil)
  11088. (regexp (concat "\\<\\(" org-deadline-string
  11089. "\\|" org-scheduled-string
  11090. "\\) *<\\([^>]+\\)>"))
  11091. (callback
  11092. (lambda () (time-less-p
  11093. (org-time-string-to-time (match-string 2))
  11094. (org-time-string-to-time date)))))
  11095. (message "%d entries before %s"
  11096. (org-occur regexp nil callback) date)))
  11097. (defun org-evaluate-time-range (&optional to-buffer)
  11098. "Evaluate a time range by computing the difference between start and end.
  11099. Normally the result is just printed in the echo area, but with prefix arg
  11100. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11101. If the time range is actually in a table, the result is inserted into the
  11102. next column.
  11103. For time difference computation, a year is assumed to be exactly 365
  11104. days in order to avoid rounding problems."
  11105. (interactive "P")
  11106. (or
  11107. (org-clock-update-time-maybe)
  11108. (save-excursion
  11109. (unless (org-at-date-range-p t)
  11110. (goto-char (point-at-bol))
  11111. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11112. (if (not (org-at-date-range-p t))
  11113. (error "Not at a time-stamp range, and none found in current line")))
  11114. (let* ((ts1 (match-string 1))
  11115. (ts2 (match-string 2))
  11116. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11117. (match-end (match-end 0))
  11118. (time1 (org-time-string-to-time ts1))
  11119. (time2 (org-time-string-to-time ts2))
  11120. (t1 (time-to-seconds time1))
  11121. (t2 (time-to-seconds time2))
  11122. (diff (abs (- t2 t1)))
  11123. (negative (< (- t2 t1) 0))
  11124. ;; (ys (floor (* 365 24 60 60)))
  11125. (ds (* 24 60 60))
  11126. (hs (* 60 60))
  11127. (fy "%dy %dd %02d:%02d")
  11128. (fy1 "%dy %dd")
  11129. (fd "%dd %02d:%02d")
  11130. (fd1 "%dd")
  11131. (fh "%02d:%02d")
  11132. y d h m align)
  11133. (if havetime
  11134. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11135. y 0
  11136. d (floor (/ diff ds)) diff (mod diff ds)
  11137. h (floor (/ diff hs)) diff (mod diff hs)
  11138. m (floor (/ diff 60)))
  11139. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11140. y 0
  11141. d (floor (+ (/ diff ds) 0.5))
  11142. h 0 m 0))
  11143. (if (not to-buffer)
  11144. (message "%s" (org-make-tdiff-string y d h m))
  11145. (if (org-at-table-p)
  11146. (progn
  11147. (goto-char match-end)
  11148. (setq align t)
  11149. (and (looking-at " *|") (goto-char (match-end 0))))
  11150. (goto-char match-end))
  11151. (if (looking-at
  11152. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11153. (replace-match ""))
  11154. (if negative (insert " -"))
  11155. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11156. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11157. (insert " " (format fh h m))))
  11158. (if align (org-table-align))
  11159. (message "Time difference inserted")))))
  11160. (defun org-make-tdiff-string (y d h m)
  11161. (let ((fmt "")
  11162. (l nil))
  11163. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11164. l (push y l)))
  11165. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11166. l (push d l)))
  11167. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11168. l (push h l)))
  11169. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11170. l (push m l)))
  11171. (apply 'format fmt (nreverse l))))
  11172. (defun org-time-string-to-time (s)
  11173. (apply 'encode-time (org-parse-time-string s)))
  11174. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11175. "Convert a time stamp to an absolute day number.
  11176. If there is a specifyer for a cyclic time stamp, get the closest date to
  11177. DAYNR.
  11178. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  11179. (cond
  11180. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11181. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11182. daynr
  11183. (+ daynr 1000)))
  11184. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11185. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11186. (time-to-days (current-time))) (match-string 0 s)
  11187. prefer show-all))
  11188. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11189. (defun org-days-to-iso-week (days)
  11190. "Return the iso week number."
  11191. (require 'cal-iso)
  11192. (car (calendar-iso-from-absolute days)))
  11193. (defun org-small-year-to-year (year)
  11194. "Convert 2-digit years into 4-digit years.
  11195. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11196. The year 2000 cannot be abbreviated. Any year lager than 99
  11197. is retrned unchanged."
  11198. (if (< year 38)
  11199. (setq year (+ 2000 year))
  11200. (if (< year 100)
  11201. (setq year (+ 1900 year))))
  11202. year)
  11203. (defun org-time-from-absolute (d)
  11204. "Return the time corresponding to date D.
  11205. D may be an absolute day number, or a calendar-type list (month day year)."
  11206. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11207. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11208. (defun org-calendar-holiday ()
  11209. "List of holidays, for Diary display in Org-mode."
  11210. (require 'holidays)
  11211. (let ((hl (funcall
  11212. (if (fboundp 'calendar-check-holidays)
  11213. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11214. (if hl (mapconcat 'identity hl "; "))))
  11215. (defun org-diary-sexp-entry (sexp entry date)
  11216. "Process a SEXP diary ENTRY for DATE."
  11217. (require 'diary-lib)
  11218. (let ((result (if calendar-debug-sexp
  11219. (let ((stack-trace-on-error t))
  11220. (eval (car (read-from-string sexp))))
  11221. (condition-case nil
  11222. (eval (car (read-from-string sexp)))
  11223. (error
  11224. (beep)
  11225. (message "Bad sexp at line %d in %s: %s"
  11226. (org-current-line)
  11227. (buffer-file-name) sexp)
  11228. (sleep-for 2))))))
  11229. (cond ((stringp result) result)
  11230. ((and (consp result)
  11231. (stringp (cdr result))) (cdr result))
  11232. (result entry)
  11233. (t nil))))
  11234. (defun org-diary-to-ical-string (frombuf)
  11235. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11236. This uses the icalendar.el library."
  11237. (let* ((tmpdir (if (featurep 'xemacs)
  11238. (temp-directory)
  11239. temporary-file-directory))
  11240. (tmpfile (make-temp-name
  11241. (expand-file-name "orgics" tmpdir)))
  11242. buf rtn b e)
  11243. (save-excursion
  11244. (set-buffer frombuf)
  11245. (icalendar-export-region (point-min) (point-max) tmpfile)
  11246. (setq buf (find-buffer-visiting tmpfile))
  11247. (set-buffer buf)
  11248. (goto-char (point-min))
  11249. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11250. (setq b (match-beginning 0)))
  11251. (goto-char (point-max))
  11252. (if (re-search-backward "^END:VEVENT" nil t)
  11253. (setq e (match-end 0)))
  11254. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11255. (kill-buffer buf)
  11256. (kill-buffer frombuf)
  11257. (delete-file tmpfile)
  11258. rtn))
  11259. (defun org-closest-date (start current change prefer show-all)
  11260. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11261. When PREFER is `past' return a date that is either CURRENT or past.
  11262. When PREFER is `future', return a date that is either CURRENT or future.
  11263. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  11264. ;; Make the proper lists from the dates
  11265. (catch 'exit
  11266. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11267. dn dw sday cday n1 n2
  11268. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11269. (setq start (org-date-to-gregorian start)
  11270. current (org-date-to-gregorian
  11271. (if show-all
  11272. current
  11273. (time-to-days (current-time))))
  11274. sday (calendar-absolute-from-gregorian start)
  11275. cday (calendar-absolute-from-gregorian current))
  11276. (if (<= cday sday) (throw 'exit sday))
  11277. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11278. (setq dn (string-to-number (match-string 1 change))
  11279. dw (cdr (assoc (match-string 2 change) a1)))
  11280. (error "Invalid change specifyer: %s" change))
  11281. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11282. (cond
  11283. ((eq dw 'day)
  11284. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11285. n2 (+ n1 dn)))
  11286. ((eq dw 'year)
  11287. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11288. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11289. (setq date1 (list m d y1)
  11290. n1 (calendar-absolute-from-gregorian date1)
  11291. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11292. n2 (calendar-absolute-from-gregorian date2)))
  11293. ((eq dw 'month)
  11294. ;; approx number of month between the tow dates
  11295. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11296. ;; How often does dn fit in there?
  11297. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11298. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11299. m (+ m nm)
  11300. ny (floor (/ m 12))
  11301. y (+ y ny)
  11302. m (- m (* ny 12)))
  11303. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11304. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11305. (setq m2 (+ m dn) y2 y)
  11306. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11307. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11308. (while (< n2 cday)
  11309. (setq n1 n2 m m2 y y2)
  11310. (setq m2 (+ m dn) y2 y)
  11311. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11312. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11313. (if show-all
  11314. (cond
  11315. ((eq prefer 'past) n1)
  11316. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11317. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11318. (cond
  11319. ((eq prefer 'past) n1)
  11320. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11321. (t (if (= cday n1) n1 n2)))))))
  11322. (defun org-date-to-gregorian (date)
  11323. "Turn any specification of DATE into a gregorian date for the calendar."
  11324. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11325. ((and (listp date) (= (length date) 3)) date)
  11326. ((stringp date)
  11327. (setq date (org-parse-time-string date))
  11328. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11329. ((listp date)
  11330. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11331. (defun org-parse-time-string (s &optional nodefault)
  11332. "Parse the standard Org-mode time string.
  11333. This should be a lot faster than the normal `parse-time-string'.
  11334. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11335. hour and minute fields will be nil if not given."
  11336. (if (string-match org-ts-regexp0 s)
  11337. (list 0
  11338. (if (or (match-beginning 8) (not nodefault))
  11339. (string-to-number (or (match-string 8 s) "0")))
  11340. (if (or (match-beginning 7) (not nodefault))
  11341. (string-to-number (or (match-string 7 s) "0")))
  11342. (string-to-number (match-string 4 s))
  11343. (string-to-number (match-string 3 s))
  11344. (string-to-number (match-string 2 s))
  11345. nil nil nil)
  11346. (make-list 9 0)))
  11347. (defun org-timestamp-up (&optional arg)
  11348. "Increase the date item at the cursor by one.
  11349. If the cursor is on the year, change the year. If it is on the month or
  11350. the day, change that.
  11351. With prefix ARG, change by that many units."
  11352. (interactive "p")
  11353. (org-timestamp-change (prefix-numeric-value arg)))
  11354. (defun org-timestamp-down (&optional arg)
  11355. "Decrease the date item at the cursor by one.
  11356. If the cursor is on the year, change the year. If it is on the month or
  11357. the day, change that.
  11358. With prefix ARG, change by that many units."
  11359. (interactive "p")
  11360. (org-timestamp-change (- (prefix-numeric-value arg))))
  11361. (defun org-timestamp-up-day (&optional arg)
  11362. "Increase the date in the time stamp by one day.
  11363. With prefix ARG, change that many days."
  11364. (interactive "p")
  11365. (if (and (not (org-at-timestamp-p t))
  11366. (org-on-heading-p))
  11367. (org-todo 'up)
  11368. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11369. (defun org-timestamp-down-day (&optional arg)
  11370. "Decrease the date in the time stamp by one day.
  11371. With prefix ARG, change that many days."
  11372. (interactive "p")
  11373. (if (and (not (org-at-timestamp-p t))
  11374. (org-on-heading-p))
  11375. (org-todo 'down)
  11376. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11377. (defun org-at-timestamp-p (&optional inactive-ok)
  11378. "Determine if the cursor is in or at a timestamp."
  11379. (interactive)
  11380. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11381. (pos (point))
  11382. (ans (or (looking-at tsr)
  11383. (save-excursion
  11384. (skip-chars-backward "^[<\n\r\t")
  11385. (if (> (point) (point-min)) (backward-char 1))
  11386. (and (looking-at tsr)
  11387. (> (- (match-end 0) pos) -1))))))
  11388. (and ans
  11389. (boundp 'org-ts-what)
  11390. (setq org-ts-what
  11391. (cond
  11392. ((= pos (match-beginning 0)) 'bracket)
  11393. ((= pos (1- (match-end 0))) 'bracket)
  11394. ((org-pos-in-match-range pos 2) 'year)
  11395. ((org-pos-in-match-range pos 3) 'month)
  11396. ((org-pos-in-match-range pos 7) 'hour)
  11397. ((org-pos-in-match-range pos 8) 'minute)
  11398. ((or (org-pos-in-match-range pos 4)
  11399. (org-pos-in-match-range pos 5)) 'day)
  11400. ((and (> pos (or (match-end 8) (match-end 5)))
  11401. (< pos (match-end 0)))
  11402. (- pos (or (match-end 8) (match-end 5))))
  11403. (t 'day))))
  11404. ans))
  11405. (defun org-toggle-timestamp-type ()
  11406. "Toggle the type (<active> or [inactive]) of a time stamp."
  11407. (interactive)
  11408. (when (org-at-timestamp-p t)
  11409. (save-excursion
  11410. (goto-char (match-beginning 0))
  11411. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  11412. (goto-char (1- (match-end 0)))
  11413. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  11414. (message "Timestamp is now %sactive"
  11415. (if (equal (char-before) ?>) "in" ""))))
  11416. (defun org-timestamp-change (n &optional what)
  11417. "Change the date in the time stamp at point.
  11418. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11419. `year', `minute', `second'. If WHAT is not given, the cursor position
  11420. in the timestamp determines what will be changed."
  11421. (let ((pos (point))
  11422. with-hm inactive
  11423. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11424. org-ts-what
  11425. extra rem
  11426. ts time time0)
  11427. (if (not (org-at-timestamp-p t))
  11428. (error "Not at a timestamp"))
  11429. (if (and (not what) (eq org-ts-what 'bracket))
  11430. (org-toggle-timestamp-type)
  11431. (if (and (not what) (not (eq org-ts-what 'day))
  11432. org-display-custom-times
  11433. (get-text-property (point) 'display)
  11434. (not (get-text-property (1- (point)) 'display)))
  11435. (setq org-ts-what 'day))
  11436. (setq org-ts-what (or what org-ts-what)
  11437. inactive (= (char-after (match-beginning 0)) ?\[)
  11438. ts (match-string 0))
  11439. (replace-match "")
  11440. (if (string-match
  11441. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11442. ts)
  11443. (setq extra (match-string 1 ts)))
  11444. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11445. (setq with-hm t))
  11446. (setq time0 (org-parse-time-string ts))
  11447. (when (and (eq org-ts-what 'minute)
  11448. (eq current-prefix-arg nil))
  11449. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11450. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11451. (setcar (cdr time0) (+ (nth 1 time0)
  11452. (if (> n 0) (- rem) (- dm rem))))))
  11453. (setq time
  11454. (encode-time (or (car time0) 0)
  11455. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11456. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11457. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11458. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11459. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11460. (nthcdr 6 time0)))
  11461. (when (integerp org-ts-what)
  11462. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11463. (if (eq what 'calendar)
  11464. (let ((cal-date (org-get-date-from-calendar)))
  11465. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11466. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11467. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11468. (setcar time0 (or (car time0) 0))
  11469. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11470. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11471. (setq time (apply 'encode-time time0))))
  11472. (setq org-last-changed-timestamp
  11473. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11474. (org-clock-update-time-maybe)
  11475. (goto-char pos)
  11476. ;; Try to recenter the calendar window, if any
  11477. (if (and org-calendar-follow-timestamp-change
  11478. (get-buffer-window "*Calendar*" t)
  11479. (memq org-ts-what '(day month year)))
  11480. (org-recenter-calendar (time-to-days time))))))
  11481. (defun org-modify-ts-extra (s pos n dm)
  11482. "Change the different parts of the lead-time and repeat fields in timestamp."
  11483. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11484. ng h m new rem)
  11485. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11486. (cond
  11487. ((or (org-pos-in-match-range pos 2)
  11488. (org-pos-in-match-range pos 3))
  11489. (setq m (string-to-number (match-string 3 s))
  11490. h (string-to-number (match-string 2 s)))
  11491. (if (org-pos-in-match-range pos 2)
  11492. (setq h (+ h n))
  11493. (setq n (* dm (org-no-warnings (signum n))))
  11494. (when (not (= 0 (setq rem (% m dm))))
  11495. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11496. (setq m (+ m n)))
  11497. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11498. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11499. (setq h (min 24 (max 0 h)))
  11500. (setq ng 1 new (format "-%02d:%02d" h m)))
  11501. ((org-pos-in-match-range pos 6)
  11502. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11503. ((org-pos-in-match-range pos 5)
  11504. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11505. ((org-pos-in-match-range pos 9)
  11506. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11507. ((org-pos-in-match-range pos 8)
  11508. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11509. (when ng
  11510. (setq s (concat
  11511. (substring s 0 (match-beginning ng))
  11512. new
  11513. (substring s (match-end ng))))))
  11514. s))
  11515. (defun org-recenter-calendar (date)
  11516. "If the calendar is visible, recenter it to DATE."
  11517. (let* ((win (selected-window))
  11518. (cwin (get-buffer-window "*Calendar*" t))
  11519. (calendar-move-hook nil))
  11520. (when cwin
  11521. (select-window cwin)
  11522. (calendar-goto-date (if (listp date) date
  11523. (calendar-gregorian-from-absolute date)))
  11524. (select-window win))))
  11525. (defun org-goto-calendar (&optional arg)
  11526. "Go to the Emacs calendar at the current date.
  11527. If there is a time stamp in the current line, go to that date.
  11528. A prefix ARG can be used to force the current date."
  11529. (interactive "P")
  11530. (let ((tsr org-ts-regexp) diff
  11531. (calendar-move-hook nil)
  11532. (view-calendar-holidays-initially nil)
  11533. (view-diary-entries-initially nil))
  11534. (if (or (org-at-timestamp-p)
  11535. (save-excursion
  11536. (beginning-of-line 1)
  11537. (looking-at (concat ".*" tsr))))
  11538. (let ((d1 (time-to-days (current-time)))
  11539. (d2 (time-to-days
  11540. (org-time-string-to-time (match-string 1)))))
  11541. (setq diff (- d2 d1))))
  11542. (calendar)
  11543. (calendar-goto-today)
  11544. (if (and diff (not arg)) (calendar-forward-day diff))))
  11545. (defun org-get-date-from-calendar ()
  11546. "Return a list (month day year) of date at point in calendar."
  11547. (with-current-buffer "*Calendar*"
  11548. (save-match-data
  11549. (calendar-cursor-to-date))))
  11550. (defun org-date-from-calendar ()
  11551. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11552. If there is already a time stamp at the cursor position, update it."
  11553. (interactive)
  11554. (if (org-at-timestamp-p t)
  11555. (org-timestamp-change 0 'calendar)
  11556. (let ((cal-date (org-get-date-from-calendar)))
  11557. (org-insert-time-stamp
  11558. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11559. (defun org-minutes-to-hours (m)
  11560. "Compute H:MM from a number of minutes."
  11561. (let ((h (/ m 60)))
  11562. (setq m (- m (* 60 h)))
  11563. (format "%d:%02d" h m)))
  11564. ;;;; Agenda files
  11565. (defun org-agenda-files (&optional unrestricted)
  11566. "Get the list of agenda files.
  11567. Optional UNRESTRICTED means return the full list even if a restriction
  11568. is currently in place."
  11569. (let ((files
  11570. (cond
  11571. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11572. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11573. ((listp org-agenda-files) org-agenda-files)
  11574. (t (error "Invalid value of `org-agenda-files'")))))
  11575. (setq files (apply 'append
  11576. (mapcar (lambda (f)
  11577. (if (file-directory-p f)
  11578. (directory-files f t
  11579. org-agenda-file-regexp)
  11580. (list f)))
  11581. files)))
  11582. (if org-agenda-skip-unavailable-files
  11583. (delq nil
  11584. (mapcar (function
  11585. (lambda (file)
  11586. (and (file-readable-p file) file)))
  11587. files))
  11588. files))) ; `org-check-agenda-file' will remove them from the list
  11589. (defun org-edit-agenda-file-list ()
  11590. "Edit the list of agenda files.
  11591. Depending on setup, this either uses customize to edit the variable
  11592. `org-agenda-files', or it visits the file that is holding the list. In the
  11593. latter case, the buffer is set up in a way that saving it automatically kills
  11594. the buffer and restores the previous window configuration."
  11595. (interactive)
  11596. (if (stringp org-agenda-files)
  11597. (let ((cw (current-window-configuration)))
  11598. (find-file org-agenda-files)
  11599. (org-set-local 'org-window-configuration cw)
  11600. (org-add-hook 'after-save-hook
  11601. (lambda ()
  11602. (set-window-configuration
  11603. (prog1 org-window-configuration
  11604. (kill-buffer (current-buffer))))
  11605. (org-install-agenda-files-menu)
  11606. (message "New agenda file list installed"))
  11607. nil 'local)
  11608. (message "%s" (substitute-command-keys
  11609. "Edit list and finish with \\[save-buffer]")))
  11610. (customize-variable 'org-agenda-files)))
  11611. (defun org-store-new-agenda-file-list (list)
  11612. "Set new value for the agenda file list and save it correcly."
  11613. (if (stringp org-agenda-files)
  11614. (let ((f org-agenda-files) b)
  11615. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11616. (with-temp-file f
  11617. (insert (mapconcat 'identity list "\n") "\n")))
  11618. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11619. (setq org-agenda-files list)
  11620. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11621. (defun org-read-agenda-file-list ()
  11622. "Read the list of agenda files from a file."
  11623. (when (stringp org-agenda-files)
  11624. (with-temp-buffer
  11625. (insert-file-contents org-agenda-files)
  11626. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11627. ;;;###autoload
  11628. (defun org-cycle-agenda-files ()
  11629. "Cycle through the files in `org-agenda-files'.
  11630. If the current buffer visits an agenda file, find the next one in the list.
  11631. If the current buffer does not, find the first agenda file."
  11632. (interactive)
  11633. (let* ((fs (org-agenda-files t))
  11634. (files (append fs (list (car fs))))
  11635. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11636. file)
  11637. (unless files (error "No agenda files"))
  11638. (catch 'exit
  11639. (while (setq file (pop files))
  11640. (if (equal (file-truename file) tcf)
  11641. (when (car files)
  11642. (find-file (car files))
  11643. (throw 'exit t))))
  11644. (find-file (car fs)))
  11645. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11646. (defun org-agenda-file-to-front (&optional to-end)
  11647. "Move/add the current file to the top of the agenda file list.
  11648. If the file is not present in the list, it is added to the front. If it is
  11649. present, it is moved there. With optional argument TO-END, add/move to the
  11650. end of the list."
  11651. (interactive "P")
  11652. (let ((org-agenda-skip-unavailable-files nil)
  11653. (file-alist (mapcar (lambda (x)
  11654. (cons (file-truename x) x))
  11655. (org-agenda-files t)))
  11656. (ctf (file-truename buffer-file-name))
  11657. x had)
  11658. (setq x (assoc ctf file-alist) had x)
  11659. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11660. (if to-end
  11661. (setq file-alist (append (delq x file-alist) (list x)))
  11662. (setq file-alist (cons x (delq x file-alist))))
  11663. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11664. (org-install-agenda-files-menu)
  11665. (message "File %s to %s of agenda file list"
  11666. (if had "moved" "added") (if to-end "end" "front"))))
  11667. (defun org-remove-file (&optional file)
  11668. "Remove current file from the list of files in variable `org-agenda-files'.
  11669. These are the files which are being checked for agenda entries.
  11670. Optional argument FILE means, use this file instead of the current."
  11671. (interactive)
  11672. (let* ((org-agenda-skip-unavailable-files nil)
  11673. (file (or file buffer-file-name))
  11674. (true-file (file-truename file))
  11675. (afile (abbreviate-file-name file))
  11676. (files (delq nil (mapcar
  11677. (lambda (x)
  11678. (if (equal true-file
  11679. (file-truename x))
  11680. nil x))
  11681. (org-agenda-files t)))))
  11682. (if (not (= (length files) (length (org-agenda-files t))))
  11683. (progn
  11684. (org-store-new-agenda-file-list files)
  11685. (org-install-agenda-files-menu)
  11686. (message "Removed file: %s" afile))
  11687. (message "File was not in list: %s (not removed)" afile))))
  11688. (defun org-file-menu-entry (file)
  11689. (vector file (list 'find-file file) t))
  11690. (defun org-check-agenda-file (file)
  11691. "Make sure FILE exists. If not, ask user what to do."
  11692. (when (not (file-exists-p file))
  11693. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11694. (abbreviate-file-name file))
  11695. (let ((r (downcase (read-char-exclusive))))
  11696. (cond
  11697. ((equal r ?r)
  11698. (org-remove-file file)
  11699. (throw 'nextfile t))
  11700. (t (error "Abort"))))))
  11701. (defun org-get-agenda-file-buffer (file)
  11702. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11703. it to the list of buffers which might be released later."
  11704. (let ((buf (org-find-base-buffer-visiting file)))
  11705. (if buf
  11706. buf ; just return it
  11707. ;; Make a new buffer and remember it
  11708. (setq buf (find-file-noselect file))
  11709. (if buf (push buf org-agenda-new-buffers))
  11710. buf)))
  11711. (defun org-release-buffers (blist)
  11712. "Release all buffers in list, asking the user for confirmation when needed.
  11713. When a buffer is unmodified, it is just killed. When modified, it is saved
  11714. \(if the user agrees) and then killed."
  11715. (let (buf file)
  11716. (while (setq buf (pop blist))
  11717. (setq file (buffer-file-name buf))
  11718. (when (and (buffer-modified-p buf)
  11719. file
  11720. (y-or-n-p (format "Save file %s? " file)))
  11721. (with-current-buffer buf (save-buffer)))
  11722. (kill-buffer buf))))
  11723. (defun org-prepare-agenda-buffers (files)
  11724. "Create buffers for all agenda files, protect archived trees and comments."
  11725. (interactive)
  11726. (let ((pa '(:org-archived t))
  11727. (pc '(:org-comment t))
  11728. (pall '(:org-archived t :org-comment t))
  11729. (inhibit-read-only t)
  11730. (rea (concat ":" org-archive-tag ":"))
  11731. bmp file re)
  11732. (save-excursion
  11733. (save-restriction
  11734. (while (setq file (pop files))
  11735. (if (bufferp file)
  11736. (set-buffer file)
  11737. (org-check-agenda-file file)
  11738. (set-buffer (org-get-agenda-file-buffer file)))
  11739. (widen)
  11740. (setq bmp (buffer-modified-p))
  11741. (org-refresh-category-properties)
  11742. (setq org-todo-keywords-for-agenda
  11743. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11744. (setq org-done-keywords-for-agenda
  11745. (append org-done-keywords-for-agenda org-done-keywords))
  11746. (save-excursion
  11747. (remove-text-properties (point-min) (point-max) pall)
  11748. (when org-agenda-skip-archived-trees
  11749. (goto-char (point-min))
  11750. (while (re-search-forward rea nil t)
  11751. (if (org-on-heading-p t)
  11752. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11753. (goto-char (point-min))
  11754. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11755. (while (re-search-forward re nil t)
  11756. (add-text-properties
  11757. (match-beginning 0) (org-end-of-subtree t) pc)))
  11758. (set-buffer-modified-p bmp))))))
  11759. ;;;; Embedded LaTeX
  11760. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11761. "Keymap for the minor `org-cdlatex-mode'.")
  11762. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11763. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11764. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11765. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11766. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11767. (defvar org-cdlatex-texmathp-advice-is-done nil
  11768. "Flag remembering if we have applied the advice to texmathp already.")
  11769. (define-minor-mode org-cdlatex-mode
  11770. "Toggle the minor `org-cdlatex-mode'.
  11771. This mode supports entering LaTeX environment and math in LaTeX fragments
  11772. in Org-mode.
  11773. \\{org-cdlatex-mode-map}"
  11774. nil " OCDL" nil
  11775. (when org-cdlatex-mode (require 'cdlatex))
  11776. (unless org-cdlatex-texmathp-advice-is-done
  11777. (setq org-cdlatex-texmathp-advice-is-done t)
  11778. (defadvice texmathp (around org-math-always-on activate)
  11779. "Always return t in org-mode buffers.
  11780. This is because we want to insert math symbols without dollars even outside
  11781. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11782. en embedded LaTeX fragement, let texmathp do its job.
  11783. \\[org-cdlatex-mode-map]"
  11784. (interactive)
  11785. (let (p)
  11786. (cond
  11787. ((not (org-mode-p)) ad-do-it)
  11788. ((eq this-command 'cdlatex-math-symbol)
  11789. (setq ad-return-value t
  11790. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11791. (t
  11792. (let ((p (org-inside-LaTeX-fragment-p)))
  11793. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11794. (setq ad-return-value t
  11795. texmathp-why '("Org-mode embedded math" . 0))
  11796. (if p ad-do-it)))))))))
  11797. (defun turn-on-org-cdlatex ()
  11798. "Unconditionally turn on `org-cdlatex-mode'."
  11799. (org-cdlatex-mode 1))
  11800. (defun org-inside-LaTeX-fragment-p ()
  11801. "Test if point is inside a LaTeX fragment.
  11802. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11803. sequence appearing also before point.
  11804. Even though the matchers for math are configurable, this function assumes
  11805. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11806. delimiters are skipped when they have been removed by customization.
  11807. The return value is nil, or a cons cell with the delimiter and
  11808. and the position of this delimiter.
  11809. This function does a reasonably good job, but can locally be fooled by
  11810. for example currency specifications. For example it will assume being in
  11811. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11812. fragments that are properly closed, but during editing, we have to live
  11813. with the uncertainty caused by missing closing delimiters. This function
  11814. looks only before point, not after."
  11815. (catch 'exit
  11816. (let ((pos (point))
  11817. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11818. (lim (progn
  11819. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11820. (point)))
  11821. dd-on str (start 0) m re)
  11822. (goto-char pos)
  11823. (when dodollar
  11824. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11825. re (nth 1 (assoc "$" org-latex-regexps)))
  11826. (while (string-match re str start)
  11827. (cond
  11828. ((= (match-end 0) (length str))
  11829. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11830. ((= (match-end 0) (- (length str) 5))
  11831. (throw 'exit nil))
  11832. (t (setq start (match-end 0))))))
  11833. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11834. (goto-char pos)
  11835. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11836. (and (match-beginning 2) (throw 'exit nil))
  11837. ;; count $$
  11838. (while (re-search-backward "\\$\\$" lim t)
  11839. (setq dd-on (not dd-on)))
  11840. (goto-char pos)
  11841. (if dd-on (cons "$$" m))))))
  11842. (defun org-try-cdlatex-tab ()
  11843. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11844. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11845. - inside a LaTeX fragment, or
  11846. - after the first word in a line, where an abbreviation expansion could
  11847. insert a LaTeX environment."
  11848. (when org-cdlatex-mode
  11849. (cond
  11850. ((save-excursion
  11851. (skip-chars-backward "a-zA-Z0-9*")
  11852. (skip-chars-backward " \t")
  11853. (bolp))
  11854. (cdlatex-tab) t)
  11855. ((org-inside-LaTeX-fragment-p)
  11856. (cdlatex-tab) t)
  11857. (t nil))))
  11858. (defun org-cdlatex-underscore-caret (&optional arg)
  11859. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11860. Revert to the normal definition outside of these fragments."
  11861. (interactive "P")
  11862. (if (org-inside-LaTeX-fragment-p)
  11863. (call-interactively 'cdlatex-sub-superscript)
  11864. (let (org-cdlatex-mode)
  11865. (call-interactively (key-binding (vector last-input-event))))))
  11866. (defun org-cdlatex-math-modify (&optional arg)
  11867. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11868. Revert to the normal definition outside of these fragments."
  11869. (interactive "P")
  11870. (if (org-inside-LaTeX-fragment-p)
  11871. (call-interactively 'cdlatex-math-modify)
  11872. (let (org-cdlatex-mode)
  11873. (call-interactively (key-binding (vector last-input-event))))))
  11874. (defvar org-latex-fragment-image-overlays nil
  11875. "List of overlays carrying the images of latex fragments.")
  11876. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11877. (defun org-remove-latex-fragment-image-overlays ()
  11878. "Remove all overlays with LaTeX fragment images in current buffer."
  11879. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11880. (setq org-latex-fragment-image-overlays nil))
  11881. (defun org-preview-latex-fragment (&optional subtree)
  11882. "Preview the LaTeX fragment at point, or all locally or globally.
  11883. If the cursor is in a LaTeX fragment, create the image and overlay
  11884. it over the source code. If there is no fragment at point, display
  11885. all fragments in the current text, from one headline to the next. With
  11886. prefix SUBTREE, display all fragments in the current subtree. With a
  11887. double prefix `C-u C-u', or when the cursor is before the first headline,
  11888. display all fragments in the buffer.
  11889. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11890. (interactive "P")
  11891. (org-remove-latex-fragment-image-overlays)
  11892. (save-excursion
  11893. (save-restriction
  11894. (let (beg end at msg)
  11895. (cond
  11896. ((or (equal subtree '(16))
  11897. (not (save-excursion
  11898. (re-search-backward (concat "^" outline-regexp) nil t))))
  11899. (setq beg (point-min) end (point-max)
  11900. msg "Creating images for buffer...%s"))
  11901. ((equal subtree '(4))
  11902. (org-back-to-heading)
  11903. (setq beg (point) end (org-end-of-subtree t)
  11904. msg "Creating images for subtree...%s"))
  11905. (t
  11906. (if (setq at (org-inside-LaTeX-fragment-p))
  11907. (goto-char (max (point-min) (- (cdr at) 2)))
  11908. (org-back-to-heading))
  11909. (setq beg (point) end (progn (outline-next-heading) (point))
  11910. msg (if at "Creating image...%s"
  11911. "Creating images for entry...%s"))))
  11912. (message msg "")
  11913. (narrow-to-region beg end)
  11914. (goto-char beg)
  11915. (org-format-latex
  11916. (concat "ltxpng/" (file-name-sans-extension
  11917. (file-name-nondirectory
  11918. buffer-file-name)))
  11919. default-directory 'overlays msg at 'forbuffer)
  11920. (message msg "done. Use `C-c C-c' to remove images.")))))
  11921. (defvar org-latex-regexps
  11922. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11923. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11924. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11925. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11926. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11927. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11928. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11929. "Regular expressions for matching embedded LaTeX.")
  11930. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11931. "Replace LaTeX fragments with links to an image, and produce images."
  11932. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11933. (let* ((prefixnodir (file-name-nondirectory prefix))
  11934. (absprefix (expand-file-name prefix dir))
  11935. (todir (file-name-directory absprefix))
  11936. (opt org-format-latex-options)
  11937. (matchers (plist-get opt :matchers))
  11938. (re-list org-latex-regexps)
  11939. (cnt 0) txt link beg end re e checkdir
  11940. m n block linkfile movefile ov)
  11941. ;; Check if there are old images files with this prefix, and remove them
  11942. (when (file-directory-p todir)
  11943. (mapc 'delete-file
  11944. (directory-files
  11945. todir 'full
  11946. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11947. ;; Check the different regular expressions
  11948. (while (setq e (pop re-list))
  11949. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11950. block (if (nth 3 e) "\n\n" ""))
  11951. (when (member m matchers)
  11952. (goto-char (point-min))
  11953. (while (re-search-forward re nil t)
  11954. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11955. (setq txt (match-string n)
  11956. beg (match-beginning n) end (match-end n)
  11957. cnt (1+ cnt)
  11958. linkfile (format "%s_%04d.png" prefix cnt)
  11959. movefile (format "%s_%04d.png" absprefix cnt)
  11960. link (concat block "[[file:" linkfile "]]" block))
  11961. (if msg (message msg cnt))
  11962. (goto-char beg)
  11963. (unless checkdir ; make sure the directory exists
  11964. (setq checkdir t)
  11965. (or (file-directory-p todir) (make-directory todir)))
  11966. (org-create-formula-image
  11967. txt movefile opt forbuffer)
  11968. (if overlays
  11969. (progn
  11970. (setq ov (org-make-overlay beg end))
  11971. (if (featurep 'xemacs)
  11972. (progn
  11973. (org-overlay-put ov 'invisible t)
  11974. (org-overlay-put
  11975. ov 'end-glyph
  11976. (make-glyph (vector 'png :file movefile))))
  11977. (org-overlay-put
  11978. ov 'display
  11979. (list 'image :type 'png :file movefile :ascent 'center)))
  11980. (push ov org-latex-fragment-image-overlays)
  11981. (goto-char end))
  11982. (delete-region beg end)
  11983. (insert link))))))))
  11984. ;; This function borrows from Ganesh Swami's latex2png.el
  11985. (defun org-create-formula-image (string tofile options buffer)
  11986. (let* ((tmpdir (if (featurep 'xemacs)
  11987. (temp-directory)
  11988. temporary-file-directory))
  11989. (texfilebase (make-temp-name
  11990. (expand-file-name "orgtex" tmpdir)))
  11991. (texfile (concat texfilebase ".tex"))
  11992. (dvifile (concat texfilebase ".dvi"))
  11993. (pngfile (concat texfilebase ".png"))
  11994. (fnh (face-attribute 'default :height nil))
  11995. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11996. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11997. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11998. "Black"))
  11999. (bg (or (plist-get options (if buffer :background :html-background))
  12000. "Transparent")))
  12001. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12002. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12003. (with-temp-file texfile
  12004. (insert org-format-latex-header
  12005. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12006. (let ((dir default-directory))
  12007. (condition-case nil
  12008. (progn
  12009. (cd tmpdir)
  12010. (call-process "latex" nil nil nil texfile))
  12011. (error nil))
  12012. (cd dir))
  12013. (if (not (file-exists-p dvifile))
  12014. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12015. (call-process "dvipng" nil nil nil
  12016. "-E" "-fg" fg "-bg" bg
  12017. "-D" dpi
  12018. ;;"-x" scale "-y" scale
  12019. "-T" "tight"
  12020. "-o" pngfile
  12021. dvifile)
  12022. (if (not (file-exists-p pngfile))
  12023. (progn (message "Failed to create png file from %s" texfile) nil)
  12024. ;; Use the requested file name and clean up
  12025. (copy-file pngfile tofile 'replace)
  12026. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12027. (delete-file (concat texfilebase e)))
  12028. pngfile))))
  12029. (defun org-dvipng-color (attr)
  12030. "Return an rgb color specification for dvipng."
  12031. (apply 'format "rgb %s %s %s"
  12032. (mapcar 'org-normalize-color
  12033. (color-values (face-attribute 'default attr nil)))))
  12034. (defun org-normalize-color (value)
  12035. "Return string to be used as color value for an RGB component."
  12036. (format "%g" (/ value 65535.0)))
  12037. ;;;; Key bindings
  12038. ;; Make `C-c C-x' a prefix key
  12039. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12040. ;; TAB key with modifiers
  12041. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12042. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12043. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12044. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12045. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12046. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12047. ;; The following line is necessary under Suse GNU/Linux
  12048. (unless (featurep 'xemacs)
  12049. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12050. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12051. (define-key org-mode-map [backtab] 'org-shifttab)
  12052. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12053. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12054. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12055. ;; Cursor keys with modifiers
  12056. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12057. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12058. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12059. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12060. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12061. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12062. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12063. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12064. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12065. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12066. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12067. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12068. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12069. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12070. ;;; Extra keys for tty access.
  12071. ;; We only set them when really needed because otherwise the
  12072. ;; menus don't show the simple keys
  12073. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12074. (not window-system))
  12075. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12076. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12077. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12078. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12079. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12080. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12081. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12082. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12083. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12084. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12085. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12086. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12087. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12088. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12089. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12090. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12091. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12092. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12093. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12094. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12095. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12096. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12097. ;; All the other keys
  12098. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12099. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12100. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  12101. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12102. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12103. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12104. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12105. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12106. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12107. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12108. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12109. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12110. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12111. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12112. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12113. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12114. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12115. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12116. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  12117. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12118. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12119. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12120. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12121. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12122. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12123. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12124. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12125. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12126. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12127. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12128. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12129. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12130. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12131. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12132. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12133. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12134. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12135. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12136. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12137. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12138. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12139. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12140. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12141. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12142. (org-defkey org-mode-map "\C-m" 'org-return)
  12143. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12144. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12145. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12146. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12147. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12148. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  12149. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12150. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12151. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12152. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12153. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12154. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12155. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12156. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12157. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12158. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12159. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12160. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12161. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12162. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12163. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12164. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12165. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12166. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12167. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12168. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12169. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12170. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12171. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12172. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12173. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12174. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  12175. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12176. (when (featurep 'xemacs)
  12177. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12178. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12179. (defun org-self-insert-command (N)
  12180. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12181. If the cursor is in a table looking at whitespace, the whitespace is
  12182. overwritten, and the table is not marked as requiring realignment."
  12183. (interactive "p")
  12184. (if (and (org-table-p)
  12185. (progn
  12186. ;; check if we blank the field, and if that triggers align
  12187. (and (featurep 'org-table) org-table-auto-blank-field
  12188. (member last-command
  12189. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12190. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12191. ;; got extra space, this field does not determine column width
  12192. (let (org-table-may-need-update) (org-table-blank-field))
  12193. ;; no extra space, this field may determine column width
  12194. (org-table-blank-field)))
  12195. t)
  12196. (eq N 1)
  12197. (looking-at "[^|\n]* |"))
  12198. (let (org-table-may-need-update)
  12199. (goto-char (1- (match-end 0)))
  12200. (delete-backward-char 1)
  12201. (goto-char (match-beginning 0))
  12202. (self-insert-command N))
  12203. (setq org-table-may-need-update t)
  12204. (self-insert-command N)
  12205. (org-fix-tags-on-the-fly)))
  12206. (defun org-fix-tags-on-the-fly ()
  12207. (when (and (equal (char-after (point-at-bol)) ?*)
  12208. (org-on-heading-p))
  12209. (org-align-tags-here org-tags-column)))
  12210. (defun org-delete-backward-char (N)
  12211. "Like `delete-backward-char', insert whitespace at field end in tables.
  12212. When deleting backwards, in tables this function will insert whitespace in
  12213. front of the next \"|\" separator, to keep the table aligned. The table will
  12214. still be marked for re-alignment if the field did fill the entire column,
  12215. because, in this case the deletion might narrow the column."
  12216. (interactive "p")
  12217. (if (and (org-table-p)
  12218. (eq N 1)
  12219. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12220. (looking-at ".*?|"))
  12221. (let ((pos (point))
  12222. (noalign (looking-at "[^|\n\r]* |"))
  12223. (c org-table-may-need-update))
  12224. (backward-delete-char N)
  12225. (skip-chars-forward "^|")
  12226. (insert " ")
  12227. (goto-char (1- pos))
  12228. ;; noalign: if there were two spaces at the end, this field
  12229. ;; does not determine the width of the column.
  12230. (if noalign (setq org-table-may-need-update c)))
  12231. (backward-delete-char N)
  12232. (org-fix-tags-on-the-fly)))
  12233. (defun org-delete-char (N)
  12234. "Like `delete-char', but insert whitespace at field end in tables.
  12235. When deleting characters, in tables this function will insert whitespace in
  12236. front of the next \"|\" separator, to keep the table aligned. The table will
  12237. still be marked for re-alignment if the field did fill the entire column,
  12238. because, in this case the deletion might narrow the column."
  12239. (interactive "p")
  12240. (if (and (org-table-p)
  12241. (not (bolp))
  12242. (not (= (char-after) ?|))
  12243. (eq N 1))
  12244. (if (looking-at ".*?|")
  12245. (let ((pos (point))
  12246. (noalign (looking-at "[^|\n\r]* |"))
  12247. (c org-table-may-need-update))
  12248. (replace-match (concat
  12249. (substring (match-string 0) 1 -1)
  12250. " |"))
  12251. (goto-char pos)
  12252. ;; noalign: if there were two spaces at the end, this field
  12253. ;; does not determine the width of the column.
  12254. (if noalign (setq org-table-may-need-update c)))
  12255. (delete-char N))
  12256. (delete-char N)
  12257. (org-fix-tags-on-the-fly)))
  12258. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12259. (put 'org-self-insert-command 'delete-selection t)
  12260. (put 'orgtbl-self-insert-command 'delete-selection t)
  12261. (put 'org-delete-char 'delete-selection 'supersede)
  12262. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12263. ;; Make `flyspell-mode' delay after some commands
  12264. (put 'org-self-insert-command 'flyspell-delayed t)
  12265. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12266. (put 'org-delete-char 'flyspell-delayed t)
  12267. (put 'org-delete-backward-char 'flyspell-delayed t)
  12268. ;; Make pabbrev-mode expand after org-mode commands
  12269. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12270. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  12271. ;; How to do this: Measure non-white length of current string
  12272. ;; If equal to column width, we should realign.
  12273. (defun org-remap (map &rest commands)
  12274. "In MAP, remap the functions given in COMMANDS.
  12275. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12276. (let (new old)
  12277. (while commands
  12278. (setq old (pop commands) new (pop commands))
  12279. (if (fboundp 'command-remapping)
  12280. (org-defkey map (vector 'remap old) new)
  12281. (substitute-key-definition old new map global-map)))))
  12282. (when (eq org-enable-table-editor 'optimized)
  12283. ;; If the user wants maximum table support, we need to hijack
  12284. ;; some standard editing functions
  12285. (org-remap org-mode-map
  12286. 'self-insert-command 'org-self-insert-command
  12287. 'delete-char 'org-delete-char
  12288. 'delete-backward-char 'org-delete-backward-char)
  12289. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12290. (defun org-shiftcursor-error ()
  12291. "Throw an error because Shift-Cursor command was applied in wrong context."
  12292. (error "This command is active in special context like tables, headlines or timestamps"))
  12293. (defun org-shifttab (&optional arg)
  12294. "Global visibility cycling or move to previous table field.
  12295. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12296. on context.
  12297. See the individual commands for more information."
  12298. (interactive "P")
  12299. (cond
  12300. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12301. (arg (message "Content view to level: ")
  12302. (org-content (prefix-numeric-value arg))
  12303. (setq org-cycle-global-status 'overview))
  12304. (t (call-interactively 'org-global-cycle))))
  12305. (defun org-shiftmetaleft ()
  12306. "Promote subtree or delete table column.
  12307. Calls `org-promote-subtree', `org-outdent-item',
  12308. or `org-table-delete-column', depending on context.
  12309. See the individual commands for more information."
  12310. (interactive)
  12311. (cond
  12312. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12313. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12314. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12315. (t (org-shiftcursor-error))))
  12316. (defun org-shiftmetaright ()
  12317. "Demote subtree or insert table column.
  12318. Calls `org-demote-subtree', `org-indent-item',
  12319. or `org-table-insert-column', depending on context.
  12320. See the individual commands for more information."
  12321. (interactive)
  12322. (cond
  12323. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12324. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12325. ((org-at-item-p) (call-interactively 'org-indent-item))
  12326. (t (org-shiftcursor-error))))
  12327. (defun org-shiftmetaup (&optional arg)
  12328. "Move subtree up or kill table row.
  12329. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12330. `org-move-item-up' depending on context. See the individual commands
  12331. for more information."
  12332. (interactive "P")
  12333. (cond
  12334. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12335. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12336. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12337. (t (org-shiftcursor-error))))
  12338. (defun org-shiftmetadown (&optional arg)
  12339. "Move subtree down or insert table row.
  12340. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12341. `org-move-item-down', depending on context. See the individual
  12342. commands for more information."
  12343. (interactive "P")
  12344. (cond
  12345. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12346. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12347. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12348. (t (org-shiftcursor-error))))
  12349. (defun org-metaleft (&optional arg)
  12350. "Promote heading or move table column to left.
  12351. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12352. With no specific context, calls the Emacs default `backward-word'.
  12353. See the individual commands for more information."
  12354. (interactive "P")
  12355. (cond
  12356. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12357. ((or (org-on-heading-p) (org-region-active-p))
  12358. (call-interactively 'org-do-promote))
  12359. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12360. (t (call-interactively 'backward-word))))
  12361. (defun org-metaright (&optional arg)
  12362. "Demote subtree or move table column to right.
  12363. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12364. With no specific context, calls the Emacs default `forward-word'.
  12365. See the individual commands for more information."
  12366. (interactive "P")
  12367. (cond
  12368. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12369. ((or (org-on-heading-p) (org-region-active-p))
  12370. (call-interactively 'org-do-demote))
  12371. ((org-at-item-p) (call-interactively 'org-indent-item))
  12372. (t (call-interactively 'forward-word))))
  12373. (defun org-metaup (&optional arg)
  12374. "Move subtree up or move table row up.
  12375. Calls `org-move-subtree-up' or `org-table-move-row' or
  12376. `org-move-item-up', depending on context. See the individual commands
  12377. for more information."
  12378. (interactive "P")
  12379. (cond
  12380. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12381. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12382. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12383. (t (transpose-lines 1) (beginning-of-line -1))))
  12384. (defun org-metadown (&optional arg)
  12385. "Move subtree down or move table row down.
  12386. Calls `org-move-subtree-down' or `org-table-move-row' or
  12387. `org-move-item-down', depending on context. See the individual
  12388. commands for more information."
  12389. (interactive "P")
  12390. (cond
  12391. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12392. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12393. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12394. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12395. (defun org-shiftup (&optional arg)
  12396. "Increase item in timestamp or increase priority of current headline.
  12397. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12398. depending on context. See the individual commands for more information."
  12399. (interactive "P")
  12400. (cond
  12401. ((org-at-timestamp-p t)
  12402. (call-interactively (if org-edit-timestamp-down-means-later
  12403. 'org-timestamp-down 'org-timestamp-up)))
  12404. ((org-on-heading-p) (call-interactively 'org-priority-up))
  12405. ((org-at-item-p) (call-interactively 'org-previous-item))
  12406. ((org-clocktable-try-shift 'up arg))
  12407. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  12408. (defun org-shiftdown (&optional arg)
  12409. "Decrease item in timestamp or decrease priority of current headline.
  12410. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12411. depending on context. See the individual commands for more information."
  12412. (interactive "P")
  12413. (cond
  12414. ((org-at-timestamp-p t)
  12415. (call-interactively (if org-edit-timestamp-down-means-later
  12416. 'org-timestamp-up 'org-timestamp-down)))
  12417. ((org-on-heading-p) (call-interactively 'org-priority-down))
  12418. ((org-clocktable-try-shift 'down arg))
  12419. (t (call-interactively 'org-next-item))))
  12420. (defun org-shiftright (&optional arg)
  12421. "Next TODO keyword or timestamp one day later, depending on context."
  12422. (interactive "P")
  12423. (cond
  12424. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12425. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  12426. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  12427. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  12428. ((org-clocktable-try-shift 'right arg))
  12429. (t (org-shiftcursor-error))))
  12430. (defun org-shiftleft (&optional arg)
  12431. "Previous TODO keyword or timestamp one day earlier, depending on context."
  12432. (interactive "P")
  12433. (cond
  12434. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12435. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  12436. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12437. ((org-at-property-p)
  12438. (call-interactively 'org-property-previous-allowed-value))
  12439. ((org-clocktable-try-shift 'left arg))
  12440. (t (org-shiftcursor-error))))
  12441. (defun org-shiftcontrolright ()
  12442. "Switch to next TODO set."
  12443. (interactive)
  12444. (cond
  12445. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  12446. (t (org-shiftcursor-error))))
  12447. (defun org-shiftcontrolleft ()
  12448. "Switch to previous TODO set."
  12449. (interactive)
  12450. (cond
  12451. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  12452. (t (org-shiftcursor-error))))
  12453. (defun org-ctrl-c-ret ()
  12454. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12455. (interactive)
  12456. (cond
  12457. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12458. (t (call-interactively 'org-insert-heading))))
  12459. (defun org-copy-special ()
  12460. "Copy region in table or copy current subtree.
  12461. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12462. See the individual commands for more information."
  12463. (interactive)
  12464. (call-interactively
  12465. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12466. (defun org-cut-special ()
  12467. "Cut region in table or cut current subtree.
  12468. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12469. See the individual commands for more information."
  12470. (interactive)
  12471. (call-interactively
  12472. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12473. (defun org-paste-special (arg)
  12474. "Paste rectangular region into table, or past subtree relative to level.
  12475. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12476. See the individual commands for more information."
  12477. (interactive "P")
  12478. (if (org-at-table-p)
  12479. (org-table-paste-rectangle)
  12480. (org-paste-subtree arg)))
  12481. (defun org-ctrl-c-ctrl-c (&optional arg)
  12482. "Set tags in headline, or update according to changed information at point.
  12483. This command does many different things, depending on context:
  12484. - If the cursor is in a headline, prompt for tags and insert them
  12485. into the current line, aligned to `org-tags-column'. When called
  12486. with prefix arg, realign all tags in the current buffer.
  12487. - If the cursor is in one of the special #+KEYWORD lines, this
  12488. triggers scanning the buffer for these lines and updating the
  12489. information.
  12490. - If the cursor is inside a table, realign the table. This command
  12491. works even if the automatic table editor has been turned off.
  12492. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12493. the entire table.
  12494. - If the cursor is a the beginning of a dynamic block, update it.
  12495. - If the cursor is inside a table created by the table.el package,
  12496. activate that table.
  12497. - If the current buffer is a remember buffer, close note and file it.
  12498. with a prefix argument, file it without further interaction to the default
  12499. location.
  12500. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12501. links in this buffer.
  12502. - If the cursor is on a numbered item in a plain list, renumber the
  12503. ordered list.
  12504. - If the cursor is on a checkbox, toggle it."
  12505. (interactive "P")
  12506. (let ((org-enable-table-editor t))
  12507. (cond
  12508. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12509. org-occur-highlights
  12510. org-latex-fragment-image-overlays)
  12511. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  12512. (org-remove-occur-highlights)
  12513. (org-remove-latex-fragment-image-overlays)
  12514. (message "Temporary highlights/overlays removed from current buffer"))
  12515. ((and (local-variable-p 'org-finish-function (current-buffer))
  12516. (fboundp org-finish-function))
  12517. (funcall org-finish-function))
  12518. ((org-at-property-p)
  12519. (call-interactively 'org-property-action))
  12520. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12521. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12522. ((org-at-table.el-p)
  12523. (require 'table)
  12524. (beginning-of-line 1)
  12525. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12526. (call-interactively 'table-recognize-table))
  12527. ((org-at-table-p)
  12528. (org-table-maybe-eval-formula)
  12529. (if arg
  12530. (call-interactively 'org-table-recalculate)
  12531. (org-table-maybe-recalculate-line))
  12532. (call-interactively 'org-table-align))
  12533. ((org-at-item-checkbox-p)
  12534. (call-interactively 'org-toggle-checkbox))
  12535. ((org-at-item-p)
  12536. (call-interactively 'org-maybe-renumber-ordered-list))
  12537. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12538. ;; Dynamic block
  12539. (beginning-of-line 1)
  12540. (org-update-dblock))
  12541. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12542. (cond
  12543. ((equal (match-string 1) "TBLFM")
  12544. ;; Recalculate the table before this line
  12545. (save-excursion
  12546. (beginning-of-line 1)
  12547. (skip-chars-backward " \r\n\t")
  12548. (if (org-at-table-p)
  12549. (org-call-with-arg 'org-table-recalculate t))))
  12550. (t
  12551. (call-interactively 'org-mode-restart))))
  12552. (t (error "C-c C-c can do nothing useful at this location.")))))
  12553. (defun org-mode-restart ()
  12554. "Restart Org-mode, to scan again for special lines.
  12555. Also updates the keyword regular expressions."
  12556. (interactive)
  12557. (let ((org-inhibit-startup t)) (org-mode))
  12558. (message "Org-mode restarted to refresh keyword and special line setup"))
  12559. (defun org-kill-note-or-show-branches ()
  12560. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12561. (interactive)
  12562. (if (not org-finish-function)
  12563. (call-interactively 'show-branches)
  12564. (let ((org-note-abort t))
  12565. (funcall org-finish-function))))
  12566. (defun org-return (&optional indent)
  12567. "Goto next table row or insert a newline.
  12568. Calls `org-table-next-row' or `newline', depending on context.
  12569. See the individual commands for more information."
  12570. (interactive)
  12571. (cond
  12572. ((bobp) (if indent (newline-and-indent) (newline)))
  12573. ((and (org-at-heading-p)
  12574. (looking-at
  12575. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12576. (org-show-entry)
  12577. (end-of-line 1)
  12578. (newline))
  12579. ((org-at-table-p)
  12580. (org-table-justify-field-maybe)
  12581. (call-interactively 'org-table-next-row))
  12582. (t (if indent (newline-and-indent) (newline)))))
  12583. (defun org-return-indent ()
  12584. "Goto next table row or insert a newline and indent.
  12585. Calls `org-table-next-row' or `newline-and-indent', depending on
  12586. context. See the individual commands for more information."
  12587. (interactive)
  12588. (org-return t))
  12589. (defun org-ctrl-c-star ()
  12590. "Compute table, or change heading status of lines.
  12591. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  12592. depending on context. This will also turn a plain list item or a normal
  12593. line into a subheading."
  12594. (interactive)
  12595. (cond
  12596. ((org-at-table-p)
  12597. (call-interactively 'org-table-recalculate))
  12598. ((org-region-active-p)
  12599. ;; Convert all lines in region to list items
  12600. (call-interactively 'org-toggle-region-headings))
  12601. ((org-on-heading-p)
  12602. (org-toggle-region-headings (point-at-bol)
  12603. (min (1+ (point-at-eol)) (point-max))))
  12604. ((org-at-item-p)
  12605. ;; Convert to heading
  12606. (let ((level (save-match-data
  12607. (save-excursion
  12608. (condition-case nil
  12609. (progn
  12610. (org-back-to-heading t)
  12611. (funcall outline-level))
  12612. (error 0))))))
  12613. (replace-match
  12614. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12615. (t (org-toggle-region-headings (point-at-bol)
  12616. (min (1+ (point-at-eol)) (point-max))))))
  12617. (defun org-ctrl-c-minus ()
  12618. "Insert separator line in table or modify bullet status of line.
  12619. Also turns a plain line or a region of lines into list items.
  12620. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12621. `org-cycle-list-bullet', depending on context."
  12622. (interactive)
  12623. (cond
  12624. ((org-at-table-p)
  12625. (call-interactively 'org-table-insert-hline))
  12626. ((org-on-heading-p)
  12627. ;; Convert to item
  12628. (save-excursion
  12629. (beginning-of-line 1)
  12630. (if (looking-at "\\*+ ")
  12631. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12632. ((org-region-active-p)
  12633. ;; Convert all lines in region to list items
  12634. (call-interactively 'org-toggle-region-items))
  12635. ((org-in-item-p)
  12636. (call-interactively 'org-cycle-list-bullet))
  12637. (t (org-toggle-region-items (point-at-bol)
  12638. (min (1+ (point-at-eol)) (point-max))))))
  12639. (defun org-toggle-region-items (beg end)
  12640. "Convert all lines in region to list items.
  12641. If the first line is already an item, convert all list items in the region
  12642. to normal lines."
  12643. (interactive "r")
  12644. (let (l2 l)
  12645. (save-excursion
  12646. (goto-char end)
  12647. (setq l2 (org-current-line))
  12648. (goto-char beg)
  12649. (beginning-of-line 1)
  12650. (setq l (1- (org-current-line)))
  12651. (if (org-at-item-p)
  12652. ;; We already have items, de-itemize
  12653. (while (< (setq l (1+ l)) l2)
  12654. (when (org-at-item-p)
  12655. (goto-char (match-beginning 2))
  12656. (delete-region (match-beginning 2) (match-end 2))
  12657. (and (looking-at "[ \t]+") (replace-match "")))
  12658. (beginning-of-line 2))
  12659. (while (< (setq l (1+ l)) l2)
  12660. (unless (org-at-item-p)
  12661. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12662. (replace-match "\\1- \\2")))
  12663. (beginning-of-line 2))))))
  12664. (defun org-toggle-region-headings (beg end)
  12665. "Convert all lines in region to list items.
  12666. If the first line is already an item, convert all list items in the region
  12667. to normal lines."
  12668. (interactive "r")
  12669. (let (l2 l)
  12670. (save-excursion
  12671. (goto-char end)
  12672. (setq l2 (org-current-line))
  12673. (goto-char beg)
  12674. (beginning-of-line 1)
  12675. (setq l (1- (org-current-line)))
  12676. (if (org-on-heading-p)
  12677. ;; We already have headlines, de-star them
  12678. (while (< (setq l (1+ l)) l2)
  12679. (when (org-on-heading-p t)
  12680. (and (looking-at outline-regexp) (replace-match "")))
  12681. (beginning-of-line 2))
  12682. (let* ((stars (save-excursion
  12683. (re-search-backward org-complex-heading-regexp nil t)
  12684. (or (match-string 1) "*")))
  12685. (add-stars (if org-odd-levels-only "**" "*"))
  12686. (rpl (concat stars add-stars " \\2")))
  12687. (while (< (setq l (1+ l)) l2)
  12688. (unless (org-on-heading-p)
  12689. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12690. (replace-match rpl)))
  12691. (beginning-of-line 2)))))))
  12692. (defun org-meta-return (&optional arg)
  12693. "Insert a new heading or wrap a region in a table.
  12694. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12695. See the individual commands for more information."
  12696. (interactive "P")
  12697. (cond
  12698. ((org-at-table-p)
  12699. (call-interactively 'org-table-wrap-region))
  12700. (t (call-interactively 'org-insert-heading))))
  12701. ;;; Menu entries
  12702. ;; Define the Org-mode menus
  12703. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12704. '("Tbl"
  12705. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  12706. ["Next Field" org-cycle (org-at-table-p)]
  12707. ["Previous Field" org-shifttab (org-at-table-p)]
  12708. ["Next Row" org-return (org-at-table-p)]
  12709. "--"
  12710. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12711. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12712. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12713. "--"
  12714. ("Column"
  12715. ["Move Column Left" org-metaleft (org-at-table-p)]
  12716. ["Move Column Right" org-metaright (org-at-table-p)]
  12717. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12718. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12719. ("Row"
  12720. ["Move Row Up" org-metaup (org-at-table-p)]
  12721. ["Move Row Down" org-metadown (org-at-table-p)]
  12722. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12723. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12724. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12725. "--"
  12726. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12727. ("Rectangle"
  12728. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12729. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12730. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12731. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12732. "--"
  12733. ("Calculate"
  12734. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12735. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12736. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  12737. "--"
  12738. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12739. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12740. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12741. "--"
  12742. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12743. "--"
  12744. ["Sum Column/Rectangle" org-table-sum
  12745. (or (org-at-table-p) (org-region-active-p))]
  12746. ["Which Column?" org-table-current-column (org-at-table-p)])
  12747. ["Debug Formulas"
  12748. org-table-toggle-formula-debugger
  12749. :style toggle :selected org-table-formula-debug]
  12750. ["Show Col/Row Numbers"
  12751. org-table-toggle-coordinate-overlays
  12752. :style toggle :selected org-table-overlay-coordinates]
  12753. "--"
  12754. ["Create" org-table-create (and (not (org-at-table-p))
  12755. org-enable-table-editor)]
  12756. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12757. ["Import from File" org-table-import (not (org-at-table-p))]
  12758. ["Export to File" org-table-export (org-at-table-p)]
  12759. "--"
  12760. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12761. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12762. '("Org"
  12763. ("Show/Hide"
  12764. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  12765. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  12766. ["Sparse Tree" org-occur t]
  12767. ["Reveal Context" org-reveal t]
  12768. ["Show All" show-all t]
  12769. "--"
  12770. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12771. "--"
  12772. ["New Heading" org-insert-heading t]
  12773. ("Navigate Headings"
  12774. ["Up" outline-up-heading t]
  12775. ["Next" outline-next-visible-heading t]
  12776. ["Previous" outline-previous-visible-heading t]
  12777. ["Next Same Level" outline-forward-same-level t]
  12778. ["Previous Same Level" outline-backward-same-level t]
  12779. "--"
  12780. ["Jump" org-goto t])
  12781. ("Edit Structure"
  12782. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12783. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12784. "--"
  12785. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12786. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12787. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12788. "--"
  12789. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12790. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12791. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12792. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12793. "--"
  12794. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12795. "--"
  12796. ["Convert to odd levels" org-convert-to-odd-levels t]
  12797. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12798. ("Editing"
  12799. ["Emphasis..." org-emphasize t])
  12800. ("Archive"
  12801. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12802. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12803. ; :active t :keys "C-u C-c C-x C-a"]
  12804. ["Sparse trees open ARCHIVE trees"
  12805. (setq org-sparse-tree-open-archived-trees
  12806. (not org-sparse-tree-open-archived-trees))
  12807. :style toggle :selected org-sparse-tree-open-archived-trees]
  12808. ["Cycling opens ARCHIVE trees"
  12809. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12810. :style toggle :selected org-cycle-open-archived-trees]
  12811. ["Agenda includes ARCHIVE trees"
  12812. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  12813. :style toggle :selected (not org-agenda-skip-archived-trees)]
  12814. "--"
  12815. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12816. ; ["Check and Move Children" (org-archive-subtree '(4))
  12817. ; :active t :keys "C-u C-c C-x C-s"]
  12818. )
  12819. "--"
  12820. ("TODO Lists"
  12821. ["TODO/DONE/-" org-todo t]
  12822. ("Select keyword"
  12823. ["Next keyword" org-shiftright (org-on-heading-p)]
  12824. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12825. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12826. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12827. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12828. ["Show TODO Tree" org-show-todo-tree t]
  12829. ["Global TODO list" org-todo-list t]
  12830. "--"
  12831. ["Set Priority" org-priority t]
  12832. ["Priority Up" org-shiftup t]
  12833. ["Priority Down" org-shiftdown t])
  12834. ("TAGS and Properties"
  12835. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  12836. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12837. "--"
  12838. ["Set property" 'org-set-property t]
  12839. ["Column view of properties" org-columns t]
  12840. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12841. ("Dates and Scheduling"
  12842. ["Timestamp" org-time-stamp t]
  12843. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12844. ("Change Date"
  12845. ["1 Day Later" org-shiftright t]
  12846. ["1 Day Earlier" org-shiftleft t]
  12847. ["1 ... Later" org-shiftup t]
  12848. ["1 ... Earlier" org-shiftdown t])
  12849. ["Compute Time Range" org-evaluate-time-range t]
  12850. ["Schedule Item" org-schedule t]
  12851. ["Deadline" org-deadline t]
  12852. "--"
  12853. ["Custom time format" org-toggle-time-stamp-overlays
  12854. :style radio :selected org-display-custom-times]
  12855. "--"
  12856. ["Goto Calendar" org-goto-calendar t]
  12857. ["Date from Calendar" org-date-from-calendar t])
  12858. ("Logging work"
  12859. ["Clock in" org-clock-in t]
  12860. ["Clock out" org-clock-out t]
  12861. ["Clock cancel" org-clock-cancel t]
  12862. ["Goto running clock" org-clock-goto t]
  12863. ["Display times" org-clock-display t]
  12864. ["Create clock table" org-clock-report t]
  12865. "--"
  12866. ["Record DONE time"
  12867. (progn (setq org-log-done (not org-log-done))
  12868. (message "Switching to %s will %s record a timestamp"
  12869. (car org-done-keywords)
  12870. (if org-log-done "automatically" "not")))
  12871. :style toggle :selected org-log-done])
  12872. "--"
  12873. ["Agenda Command..." org-agenda t]
  12874. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12875. ("File List for Agenda")
  12876. ("Special views current file"
  12877. ["TODO Tree" org-show-todo-tree t]
  12878. ["Check Deadlines" org-check-deadlines t]
  12879. ["Timeline" org-timeline t]
  12880. ["Tags Tree" org-tags-sparse-tree t])
  12881. "--"
  12882. ("Hyperlinks"
  12883. ["Store Link (Global)" org-store-link t]
  12884. ["Insert Link" org-insert-link t]
  12885. ["Follow Link" org-open-at-point t]
  12886. "--"
  12887. ["Next link" org-next-link t]
  12888. ["Previous link" org-previous-link t]
  12889. "--"
  12890. ["Descriptive Links"
  12891. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12892. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  12893. ["Literal Links"
  12894. (progn
  12895. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12896. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
  12897. "--"
  12898. ["Export/Publish..." org-export t]
  12899. ("LaTeX"
  12900. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12901. :selected org-cdlatex-mode]
  12902. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12903. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12904. ["Modify math symbol" org-cdlatex-math-modify
  12905. (org-inside-LaTeX-fragment-p)]
  12906. ["Export LaTeX fragments as images"
  12907. (if (featurep 'org-exp)
  12908. (setq org-export-with-LaTeX-fragments
  12909. (not org-export-with-LaTeX-fragments))
  12910. (require 'org-exp))
  12911. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12912. org-export-with-LaTeX-fragments)])
  12913. "--"
  12914. ("Documentation"
  12915. ["Show Version" org-version t]
  12916. ["Info Documentation" org-info t])
  12917. ("Customize"
  12918. ["Browse Org Group" org-customize t]
  12919. "--"
  12920. ["Expand This Menu" org-create-customize-menu
  12921. (fboundp 'customize-menu-create)])
  12922. "--"
  12923. ["Refresh setup" org-mode-restart t]
  12924. ))
  12925. (defun org-info (&optional node)
  12926. "Read documentation for Org-mode in the info system.
  12927. With optional NODE, go directly to that node."
  12928. (interactive)
  12929. (info (format "(org)%s" (or node ""))))
  12930. (defun org-install-agenda-files-menu ()
  12931. (let ((bl (buffer-list)))
  12932. (save-excursion
  12933. (while bl
  12934. (set-buffer (pop bl))
  12935. (if (org-mode-p) (setq bl nil)))
  12936. (when (org-mode-p)
  12937. (easy-menu-change
  12938. '("Org") "File List for Agenda"
  12939. (append
  12940. (list
  12941. ["Edit File List" (org-edit-agenda-file-list) t]
  12942. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12943. ["Remove Current File from List" org-remove-file t]
  12944. ["Cycle through agenda files" org-cycle-agenda-files t]
  12945. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12946. "--")
  12947. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12948. ;;;; Documentation
  12949. (defun org-customize ()
  12950. "Call the customize function with org as argument."
  12951. (interactive)
  12952. (customize-browse 'org))
  12953. (defun org-create-customize-menu ()
  12954. "Create a full customization menu for Org-mode, insert it into the menu."
  12955. (interactive)
  12956. (if (fboundp 'customize-menu-create)
  12957. (progn
  12958. (easy-menu-change
  12959. '("Org") "Customize"
  12960. `(["Browse Org group" org-customize t]
  12961. "--"
  12962. ,(customize-menu-create 'org)
  12963. ["Set" Custom-set t]
  12964. ["Save" Custom-save t]
  12965. ["Reset to Current" Custom-reset-current t]
  12966. ["Reset to Saved" Custom-reset-saved t]
  12967. ["Reset to Standard Settings" Custom-reset-standard t]))
  12968. (message "\"Org\"-menu now contains full customization menu"))
  12969. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12970. ;;;; Miscellaneous stuff
  12971. ;;; Generally useful functions
  12972. (defun org-force-self-insert (N)
  12973. "Needed to enforce self-insert under remapping."
  12974. (interactive "p")
  12975. (self-insert-command N))
  12976. (defun org-string-width (s)
  12977. "Compute width of string, ignoring invisible characters.
  12978. This ignores character with invisibility property `org-link', and also
  12979. characters with property `org-cwidth', because these will become invisible
  12980. upon the next fontification round."
  12981. (let (b l)
  12982. (when (or (eq t buffer-invisibility-spec)
  12983. (assq 'org-link buffer-invisibility-spec))
  12984. (while (setq b (text-property-any 0 (length s)
  12985. 'invisible 'org-link s))
  12986. (setq s (concat (substring s 0 b)
  12987. (substring s (or (next-single-property-change
  12988. b 'invisible s) (length s)))))))
  12989. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12990. (setq s (concat (substring s 0 b)
  12991. (substring s (or (next-single-property-change
  12992. b 'org-cwidth s) (length s))))))
  12993. (setq l (string-width s) b -1)
  12994. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12995. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12996. l))
  12997. (defun org-trim (s)
  12998. "Remove whitespace at beginning and end of string."
  12999. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13000. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13001. s)
  13002. (defun org-wrap (string &optional width lines)
  13003. "Wrap string to either a number of lines, or a width in characters.
  13004. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13005. that costs. If there is a word longer than WIDTH, the text is actually
  13006. wrapped to the length of that word.
  13007. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13008. many lines, whatever width that takes.
  13009. The return value is a list of lines, without newlines at the end."
  13010. (let* ((words (org-split-string string "[ \t\n]+"))
  13011. (maxword (apply 'max (mapcar 'org-string-width words)))
  13012. w ll)
  13013. (cond (width
  13014. (org-do-wrap words (max maxword width)))
  13015. (lines
  13016. (setq w maxword)
  13017. (setq ll (org-do-wrap words maxword))
  13018. (if (<= (length ll) lines)
  13019. ll
  13020. (setq ll words)
  13021. (while (> (length ll) lines)
  13022. (setq w (1+ w))
  13023. (setq ll (org-do-wrap words w)))
  13024. ll))
  13025. (t (error "Cannot wrap this")))))
  13026. (defun org-do-wrap (words width)
  13027. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13028. (let (lines line)
  13029. (while words
  13030. (setq line (pop words))
  13031. (while (and words (< (+ (length line) (length (car words))) width))
  13032. (setq line (concat line " " (pop words))))
  13033. (setq lines (push line lines)))
  13034. (nreverse lines)))
  13035. (defun org-split-string (string &optional separators)
  13036. "Splits STRING into substrings at SEPARATORS.
  13037. No empty strings are returned if there are matches at the beginning
  13038. and end of string."
  13039. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13040. (start 0)
  13041. notfirst
  13042. (list nil))
  13043. (while (and (string-match rexp string
  13044. (if (and notfirst
  13045. (= start (match-beginning 0))
  13046. (< start (length string)))
  13047. (1+ start) start))
  13048. (< (match-beginning 0) (length string)))
  13049. (setq notfirst t)
  13050. (or (eq (match-beginning 0) 0)
  13051. (and (eq (match-beginning 0) (match-end 0))
  13052. (eq (match-beginning 0) start))
  13053. (setq list
  13054. (cons (substring string start (match-beginning 0))
  13055. list)))
  13056. (setq start (match-end 0)))
  13057. (or (eq start (length string))
  13058. (setq list
  13059. (cons (substring string start)
  13060. list)))
  13061. (nreverse list)))
  13062. (defun org-context ()
  13063. "Return a list of contexts of the current cursor position.
  13064. If several contexts apply, all are returned.
  13065. Each context entry is a list with a symbol naming the context, and
  13066. two positions indicating start and end of the context. Possible
  13067. contexts are:
  13068. :headline anywhere in a headline
  13069. :headline-stars on the leading stars in a headline
  13070. :todo-keyword on a TODO keyword (including DONE) in a headline
  13071. :tags on the TAGS in a headline
  13072. :priority on the priority cookie in a headline
  13073. :item on the first line of a plain list item
  13074. :item-bullet on the bullet/number of a plain list item
  13075. :checkbox on the checkbox in a plain list item
  13076. :table in an org-mode table
  13077. :table-special on a special filed in a table
  13078. :table-table in a table.el table
  13079. :link on a hyperlink
  13080. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13081. :target on a <<target>>
  13082. :radio-target on a <<<radio-target>>>
  13083. :latex-fragment on a LaTeX fragment
  13084. :latex-preview on a LaTeX fragment with overlayed preview image
  13085. This function expects the position to be visible because it uses font-lock
  13086. faces as a help to recognize the following contexts: :table-special, :link,
  13087. and :keyword."
  13088. (let* ((f (get-text-property (point) 'face))
  13089. (faces (if (listp f) f (list f)))
  13090. (p (point)) clist o)
  13091. ;; First the large context
  13092. (cond
  13093. ((org-on-heading-p t)
  13094. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13095. (when (progn
  13096. (beginning-of-line 1)
  13097. (looking-at org-todo-line-tags-regexp))
  13098. (push (org-point-in-group p 1 :headline-stars) clist)
  13099. (push (org-point-in-group p 2 :todo-keyword) clist)
  13100. (push (org-point-in-group p 4 :tags) clist))
  13101. (goto-char p)
  13102. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13103. (if (looking-at "\\[#[A-Z0-9]\\]")
  13104. (push (org-point-in-group p 0 :priority) clist)))
  13105. ((org-at-item-p)
  13106. (push (org-point-in-group p 2 :item-bullet) clist)
  13107. (push (list :item (point-at-bol)
  13108. (save-excursion (org-end-of-item) (point)))
  13109. clist)
  13110. (and (org-at-item-checkbox-p)
  13111. (push (org-point-in-group p 0 :checkbox) clist)))
  13112. ((org-at-table-p)
  13113. (push (list :table (org-table-begin) (org-table-end)) clist)
  13114. (if (memq 'org-formula faces)
  13115. (push (list :table-special
  13116. (previous-single-property-change p 'face)
  13117. (next-single-property-change p 'face)) clist)))
  13118. ((org-at-table-p 'any)
  13119. (push (list :table-table) clist)))
  13120. (goto-char p)
  13121. ;; Now the small context
  13122. (cond
  13123. ((org-at-timestamp-p)
  13124. (push (org-point-in-group p 0 :timestamp) clist))
  13125. ((memq 'org-link faces)
  13126. (push (list :link
  13127. (previous-single-property-change p 'face)
  13128. (next-single-property-change p 'face)) clist))
  13129. ((memq 'org-special-keyword faces)
  13130. (push (list :keyword
  13131. (previous-single-property-change p 'face)
  13132. (next-single-property-change p 'face)) clist))
  13133. ((org-on-target-p)
  13134. (push (org-point-in-group p 0 :target) clist)
  13135. (goto-char (1- (match-beginning 0)))
  13136. (if (looking-at org-radio-target-regexp)
  13137. (push (org-point-in-group p 0 :radio-target) clist))
  13138. (goto-char p))
  13139. ((setq o (car (delq nil
  13140. (mapcar
  13141. (lambda (x)
  13142. (if (memq x org-latex-fragment-image-overlays) x))
  13143. (org-overlays-at (point))))))
  13144. (push (list :latex-fragment
  13145. (org-overlay-start o) (org-overlay-end o)) clist)
  13146. (push (list :latex-preview
  13147. (org-overlay-start o) (org-overlay-end o)) clist))
  13148. ((org-inside-LaTeX-fragment-p)
  13149. ;; FIXME: positions wrong.
  13150. (push (list :latex-fragment (point) (point)) clist)))
  13151. (setq clist (nreverse (delq nil clist)))
  13152. clist))
  13153. ;; FIXME: Compare with at-regexp-p Do we need both?
  13154. (defun org-in-regexp (re &optional nlines visually)
  13155. "Check if point is inside a match of regexp.
  13156. Normally only the current line is checked, but you can include NLINES extra
  13157. lines both before and after point into the search.
  13158. If VISUALLY is set, require that the cursor is not after the match but
  13159. really on, so that the block visually is on the match."
  13160. (catch 'exit
  13161. (let ((pos (point))
  13162. (eol (point-at-eol (+ 1 (or nlines 0))))
  13163. (inc (if visually 1 0)))
  13164. (save-excursion
  13165. (beginning-of-line (- 1 (or nlines 0)))
  13166. (while (re-search-forward re eol t)
  13167. (if (and (<= (match-beginning 0) pos)
  13168. (>= (+ inc (match-end 0)) pos))
  13169. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13170. (defun org-at-regexp-p (regexp)
  13171. "Is point inside a match of REGEXP in the current line?"
  13172. (catch 'exit
  13173. (save-excursion
  13174. (let ((pos (point)) (end (point-at-eol)))
  13175. (beginning-of-line 1)
  13176. (while (re-search-forward regexp end t)
  13177. (if (and (<= (match-beginning 0) pos)
  13178. (>= (match-end 0) pos))
  13179. (throw 'exit t)))
  13180. nil))))
  13181. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13182. "Call `multi-occur' with buffers for all agenda files."
  13183. (interactive "sOrg-files matching: \np")
  13184. (let* ((files (org-agenda-files))
  13185. (tnames (mapcar 'file-truename files))
  13186. (extra org-agenda-text-search-extra-files)
  13187. f)
  13188. (while (setq f (pop extra))
  13189. (unless (member (file-truename f) tnames)
  13190. (add-to-list 'files f 'append)
  13191. (add-to-list 'tnames (file-truename f) 'append)))
  13192. (multi-occur
  13193. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13194. regexp)))
  13195. (if (boundp 'occur-mode-find-occurrence-hook)
  13196. ;; Emacs 23
  13197. (add-hook 'occur-mode-find-occurrence-hook
  13198. (lambda ()
  13199. (when (org-mode-p)
  13200. (org-reveal))))
  13201. ;; Emacs 22
  13202. (defadvice occur-mode-goto-occurrence
  13203. (after org-occur-reveal activate)
  13204. (and (org-mode-p) (org-reveal)))
  13205. (defadvice occur-mode-goto-occurrence-other-window
  13206. (after org-occur-reveal activate)
  13207. (and (org-mode-p) (org-reveal)))
  13208. (defadvice occur-mode-display-occurrence
  13209. (after org-occur-reveal activate)
  13210. (when (org-mode-p)
  13211. (let ((pos (occur-mode-find-occurrence)))
  13212. (with-current-buffer (marker-buffer pos)
  13213. (save-excursion
  13214. (goto-char pos)
  13215. (org-reveal)))))))
  13216. (defun org-uniquify (list)
  13217. "Remove duplicate elements from LIST."
  13218. (let (res)
  13219. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13220. res))
  13221. (defun org-delete-all (elts list)
  13222. "Remove all elements in ELTS from LIST."
  13223. (while elts
  13224. (setq list (delete (pop elts) list)))
  13225. list)
  13226. (defun org-back-over-empty-lines ()
  13227. "Move backwards over witespace, to the beginning of the first empty line.
  13228. Returns the number of empty lines passed."
  13229. (let ((pos (point)))
  13230. (skip-chars-backward " \t\n\r")
  13231. (beginning-of-line 2)
  13232. (goto-char (min (point) pos))
  13233. (count-lines (point) pos)))
  13234. (defun org-skip-whitespace ()
  13235. (skip-chars-forward " \t\n\r"))
  13236. (defun org-point-in-group (point group &optional context)
  13237. "Check if POINT is in match-group GROUP.
  13238. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13239. match. If the match group does ot exist or point is not inside it,
  13240. return nil."
  13241. (and (match-beginning group)
  13242. (>= point (match-beginning group))
  13243. (<= point (match-end group))
  13244. (if context
  13245. (list context (match-beginning group) (match-end group))
  13246. t)))
  13247. (defun org-switch-to-buffer-other-window (&rest args)
  13248. "Switch to buffer in a second window on the current frame.
  13249. In particular, do not allow pop-up frames."
  13250. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13251. special-display-function)
  13252. (apply 'switch-to-buffer-other-window args)))
  13253. (defun org-combine-plists (&rest plists)
  13254. "Create a single property list from all plists in PLISTS.
  13255. The process starts by copying the first list, and then setting properties
  13256. from the other lists. Settings in the last list are the most significant
  13257. ones and overrule settings in the other lists."
  13258. (let ((rtn (copy-sequence (pop plists)))
  13259. p v ls)
  13260. (while plists
  13261. (setq ls (pop plists))
  13262. (while ls
  13263. (setq p (pop ls) v (pop ls))
  13264. (setq rtn (plist-put rtn p v))))
  13265. rtn))
  13266. (defun org-move-line-down (arg)
  13267. "Move the current line down. With prefix argument, move it past ARG lines."
  13268. (interactive "p")
  13269. (let ((col (current-column))
  13270. beg end pos)
  13271. (beginning-of-line 1) (setq beg (point))
  13272. (beginning-of-line 2) (setq end (point))
  13273. (beginning-of-line (+ 1 arg))
  13274. (setq pos (move-marker (make-marker) (point)))
  13275. (insert (delete-and-extract-region beg end))
  13276. (goto-char pos)
  13277. (move-to-column col)))
  13278. (defun org-move-line-up (arg)
  13279. "Move the current line up. With prefix argument, move it past ARG lines."
  13280. (interactive "p")
  13281. (let ((col (current-column))
  13282. beg end pos)
  13283. (beginning-of-line 1) (setq beg (point))
  13284. (beginning-of-line 2) (setq end (point))
  13285. (beginning-of-line (- arg))
  13286. (setq pos (move-marker (make-marker) (point)))
  13287. (insert (delete-and-extract-region beg end))
  13288. (goto-char pos)
  13289. (move-to-column col)))
  13290. (defun org-replace-escapes (string table)
  13291. "Replace %-escapes in STRING with values in TABLE.
  13292. TABLE is an association list with keys like \"%a\" and string values.
  13293. The sequences in STRING may contain normal field width and padding information,
  13294. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13295. so values can contain further %-escapes if they are define later in TABLE."
  13296. (let ((case-fold-search nil)
  13297. e re rpl)
  13298. (while (setq e (pop table))
  13299. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13300. (while (string-match re string)
  13301. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13302. (cdr e)))
  13303. (setq string (replace-match rpl t t string))))
  13304. string))
  13305. (defun org-sublist (list start end)
  13306. "Return a section of LIST, from START to END.
  13307. Counting starts at 1."
  13308. (let (rtn (c start))
  13309. (setq list (nthcdr (1- start) list))
  13310. (while (and list (<= c end))
  13311. (push (pop list) rtn)
  13312. (setq c (1+ c)))
  13313. (nreverse rtn)))
  13314. (defun org-find-base-buffer-visiting (file)
  13315. "Like `find-buffer-visiting' but alway return the base buffer and
  13316. not an indirect buffer."
  13317. (let ((buf (find-buffer-visiting file)))
  13318. (if buf
  13319. (or (buffer-base-buffer buf) buf)
  13320. nil)))
  13321. (defun org-image-file-name-regexp ()
  13322. "Return regexp matching the file names of images."
  13323. (if (fboundp 'image-file-name-regexp)
  13324. (image-file-name-regexp)
  13325. (let ((image-file-name-extensions
  13326. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13327. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13328. (concat "\\."
  13329. (regexp-opt (nconc (mapcar 'upcase
  13330. image-file-name-extensions)
  13331. image-file-name-extensions)
  13332. t)
  13333. "\\'"))))
  13334. (defun org-file-image-p (file)
  13335. "Return non-nil if FILE is an image."
  13336. (save-match-data
  13337. (string-match (org-image-file-name-regexp) file)))
  13338. ;;; Paragraph filling stuff.
  13339. ;; We want this to be just right, so use the full arsenal.
  13340. (defun org-indent-line-function ()
  13341. "Indent line like previous, but further if previous was headline or item."
  13342. (interactive)
  13343. (let* ((pos (point))
  13344. (itemp (org-at-item-p))
  13345. column bpos bcol tpos tcol bullet btype bullet-type)
  13346. ;; Find the previous relevant line
  13347. (beginning-of-line 1)
  13348. (cond
  13349. ((looking-at "#") (setq column 0))
  13350. ((looking-at "\\*+ ") (setq column 0))
  13351. (t
  13352. (beginning-of-line 0)
  13353. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13354. (beginning-of-line 0))
  13355. (cond
  13356. ((looking-at "\\*+[ \t]+")
  13357. (goto-char (match-end 0))
  13358. (setq column (current-column)))
  13359. ((org-in-item-p)
  13360. (org-beginning-of-item)
  13361. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13362. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  13363. (setq bpos (match-beginning 1) tpos (match-end 0)
  13364. bcol (progn (goto-char bpos) (current-column))
  13365. tcol (progn (goto-char tpos) (current-column))
  13366. bullet (match-string 1)
  13367. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13368. (if (not itemp)
  13369. (setq column tcol)
  13370. (goto-char pos)
  13371. (beginning-of-line 1)
  13372. (if (looking-at "\\S-")
  13373. (progn
  13374. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13375. (setq bullet (match-string 1)
  13376. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13377. (setq column (if (equal btype bullet-type) bcol tcol)))
  13378. (setq column (org-get-indentation)))))
  13379. (t (setq column (org-get-indentation))))))
  13380. (goto-char pos)
  13381. (if (<= (current-column) (current-indentation))
  13382. (indent-line-to column)
  13383. (save-excursion (indent-line-to column)))
  13384. (setq column (current-column))
  13385. (beginning-of-line 1)
  13386. (if (looking-at
  13387. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13388. (replace-match (concat "\\1" (format org-property-format
  13389. (match-string 2) (match-string 3)))
  13390. t nil))
  13391. (move-to-column column)))
  13392. (defun org-set-autofill-regexps ()
  13393. (interactive)
  13394. ;; In the paragraph separator we include headlines, because filling
  13395. ;; text in a line directly attached to a headline would otherwise
  13396. ;; fill the headline as well.
  13397. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13398. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13399. ;; The paragraph starter includes hand-formatted lists.
  13400. (org-set-local 'paragraph-start
  13401. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13402. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13403. ;; But only if the user has not turned off tables or fixed-width regions
  13404. (org-set-local
  13405. 'auto-fill-inhibit-regexp
  13406. (concat "\\*+ \\|#\\+"
  13407. "\\|[ \t]*" org-keyword-time-regexp
  13408. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13409. (concat
  13410. "\\|[ \t]*["
  13411. (if org-enable-table-editor "|" "")
  13412. (if org-enable-fixed-width-editor ":" "")
  13413. "]"))))
  13414. ;; We use our own fill-paragraph function, to make sure that tables
  13415. ;; and fixed-width regions are not wrapped. That function will pass
  13416. ;; through to `fill-paragraph' when appropriate.
  13417. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13418. ; Adaptive filling: To get full control, first make sure that
  13419. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13420. (org-set-local 'adaptive-fill-regexp "\000")
  13421. (org-set-local 'adaptive-fill-function
  13422. 'org-adaptive-fill-function)
  13423. (org-set-local
  13424. 'align-mode-rules-list
  13425. '((org-in-buffer-settings
  13426. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13427. (modes . '(org-mode))))))
  13428. (defun org-fill-paragraph (&optional justify)
  13429. "Re-align a table, pass through to fill-paragraph if no table."
  13430. (let ((table-p (org-at-table-p))
  13431. (table.el-p (org-at-table.el-p)))
  13432. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13433. (save-excursion (goto-char (point-at-bol))
  13434. (looking-at outline-regexp)))
  13435. t) ; skip headlines
  13436. (table.el-p t) ; skip table.el tables
  13437. (table-p (org-table-align) t) ; align org-mode tables
  13438. (t nil)))) ; call paragraph-fill
  13439. ;; For reference, this is the default value of adaptive-fill-regexp
  13440. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13441. (defun org-adaptive-fill-function ()
  13442. "Return a fill prefix for org-mode files.
  13443. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13444. work correctly."
  13445. (cond ((looking-at "#[ \t]+")
  13446. (match-string 0))
  13447. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  13448. (save-excursion
  13449. (goto-char (match-end 0))
  13450. (make-string (current-column) ?\ )))
  13451. (t nil)))
  13452. ;;; Other stuff.
  13453. (defun org-toggle-fixed-width-section (arg)
  13454. "Toggle the fixed-width export.
  13455. If there is no active region, the QUOTE keyword at the current headline is
  13456. inserted or removed. When present, it causes the text between this headline
  13457. and the next to be exported as fixed-width text, and unmodified.
  13458. If there is an active region, this command adds or removes a colon as the
  13459. first character of this line. If the first character of a line is a colon,
  13460. this line is also exported in fixed-width font."
  13461. (interactive "P")
  13462. (let* ((cc 0)
  13463. (regionp (org-region-active-p))
  13464. (beg (if regionp (region-beginning) (point)))
  13465. (end (if regionp (region-end)))
  13466. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13467. (case-fold-search nil)
  13468. (re "[ \t]*\\(:\\)")
  13469. off)
  13470. (if regionp
  13471. (save-excursion
  13472. (goto-char beg)
  13473. (setq cc (current-column))
  13474. (beginning-of-line 1)
  13475. (setq off (looking-at re))
  13476. (while (> nlines 0)
  13477. (setq nlines (1- nlines))
  13478. (beginning-of-line 1)
  13479. (cond
  13480. (arg
  13481. (move-to-column cc t)
  13482. (insert ":\n")
  13483. (forward-line -1))
  13484. ((and off (looking-at re))
  13485. (replace-match "" t t nil 1))
  13486. ((not off) (move-to-column cc t) (insert ":")))
  13487. (forward-line 1)))
  13488. (save-excursion
  13489. (org-back-to-heading)
  13490. (if (looking-at (concat outline-regexp
  13491. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13492. (replace-match "" t t nil 1)
  13493. (if (looking-at outline-regexp)
  13494. (progn
  13495. (goto-char (match-end 0))
  13496. (insert org-quote-string " "))))))))
  13497. ;;;; Functions extending outline functionality
  13498. (defun org-beginning-of-line (&optional arg)
  13499. "Go to the beginning of the current line. If that is invisible, continue
  13500. to a visible line beginning. This makes the function of C-a more intuitive.
  13501. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13502. first attempt, and only move to after the tags when the cursor is already
  13503. beyond the end of the headline."
  13504. (interactive "P")
  13505. (let ((pos (point)))
  13506. (beginning-of-line 1)
  13507. (if (bobp)
  13508. nil
  13509. (backward-char 1)
  13510. (if (org-invisible-p)
  13511. (while (and (not (bobp)) (org-invisible-p))
  13512. (backward-char 1)
  13513. (beginning-of-line 1))
  13514. (forward-char 1)))
  13515. (when org-special-ctrl-a/e
  13516. (cond
  13517. ((and (looking-at org-todo-line-regexp)
  13518. (= (char-after (match-end 1)) ?\ ))
  13519. (goto-char
  13520. (if (eq org-special-ctrl-a/e t)
  13521. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  13522. ((= pos (point)) (match-beginning 3))
  13523. (t (point)))
  13524. (cond ((> pos (point)) (point))
  13525. ((not (eq last-command this-command)) (point))
  13526. (t (match-beginning 3))))))
  13527. ((org-at-item-p)
  13528. (goto-char
  13529. (if (eq org-special-ctrl-a/e t)
  13530. (cond ((> pos (match-end 4)) (match-end 4))
  13531. ((= pos (point)) (match-end 4))
  13532. (t (point)))
  13533. (cond ((> pos (point)) (point))
  13534. ((not (eq last-command this-command)) (point))
  13535. (t (match-end 4))))))))))
  13536. (defun org-end-of-line (&optional arg)
  13537. "Go to the end of the line.
  13538. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13539. first attempt, and only move to after the tags when the cursor is already
  13540. beyond the end of the headline."
  13541. (interactive "P")
  13542. (if (or (not org-special-ctrl-a/e)
  13543. (not (org-on-heading-p)))
  13544. (end-of-line arg)
  13545. (let ((pos (point)))
  13546. (beginning-of-line 1)
  13547. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13548. (if (eq org-special-ctrl-a/e t)
  13549. (if (or (< pos (match-beginning 1))
  13550. (= pos (match-end 0)))
  13551. (goto-char (match-beginning 1))
  13552. (goto-char (match-end 0)))
  13553. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13554. (goto-char (match-end 0))
  13555. (goto-char (match-beginning 1))))
  13556. (end-of-line arg)))))
  13557. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13558. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13559. (defun org-kill-line (&optional arg)
  13560. "Kill line, to tags or end of line."
  13561. (interactive "P")
  13562. (cond
  13563. ((or (not org-special-ctrl-k)
  13564. (bolp)
  13565. (not (org-on-heading-p)))
  13566. (call-interactively 'kill-line))
  13567. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13568. (kill-region (point) (match-beginning 1))
  13569. (org-set-tags nil t))
  13570. (t (kill-region (point) (point-at-eol)))))
  13571. (define-key org-mode-map "\C-k" 'org-kill-line)
  13572. (defun org-invisible-p ()
  13573. "Check if point is at a character currently not visible."
  13574. ;; Early versions of noutline don't have `outline-invisible-p'.
  13575. (if (fboundp 'outline-invisible-p)
  13576. (outline-invisible-p)
  13577. (get-char-property (point) 'invisible)))
  13578. (defun org-invisible-p2 ()
  13579. "Check if point is at a character currently not visible."
  13580. (save-excursion
  13581. (if (and (eolp) (not (bobp))) (backward-char 1))
  13582. ;; Early versions of noutline don't have `outline-invisible-p'.
  13583. (if (fboundp 'outline-invisible-p)
  13584. (outline-invisible-p)
  13585. (get-char-property (point) 'invisible))))
  13586. (defalias 'org-back-to-heading 'outline-back-to-heading)
  13587. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13588. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13589. (defun org-at-heading-or-item-p ()
  13590. (or (org-on-heading-p) (org-at-item-p)))
  13591. (defun org-on-target-p ()
  13592. (or (org-in-regexp org-radio-target-regexp)
  13593. (org-in-regexp org-target-regexp)))
  13594. (defun org-up-heading-all (arg)
  13595. "Move to the heading line of which the present line is a subheading.
  13596. This function considers both visible and invisible heading lines.
  13597. With argument, move up ARG levels."
  13598. (if (fboundp 'outline-up-heading-all)
  13599. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13600. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13601. (defun org-up-heading-safe ()
  13602. "Move to the heading line of which the present line is a subheading.
  13603. This version will not throw an error. It will return the level of the
  13604. headline found, or nil if no higher level is found."
  13605. (let ((pos (point)) start-level level
  13606. (re (concat "^" outline-regexp)))
  13607. (catch 'exit
  13608. (outline-back-to-heading t)
  13609. (setq start-level (funcall outline-level))
  13610. (if (equal start-level 1) (throw 'exit nil))
  13611. (while (re-search-backward re nil t)
  13612. (setq level (funcall outline-level))
  13613. (if (< level start-level) (throw 'exit level)))
  13614. nil)))
  13615. (defun org-first-sibling-p ()
  13616. "Is this heading the first child of its parents?"
  13617. (interactive)
  13618. (let ((re (concat "^" outline-regexp))
  13619. level l)
  13620. (unless (org-at-heading-p t)
  13621. (error "Not at a heading"))
  13622. (setq level (funcall outline-level))
  13623. (save-excursion
  13624. (if (not (re-search-backward re nil t))
  13625. t
  13626. (setq l (funcall outline-level))
  13627. (< l level)))))
  13628. (defun org-goto-sibling (&optional previous)
  13629. "Goto the next sibling, even if it is invisible.
  13630. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13631. when a sibling was found. When none is found, return nil and don't
  13632. move point."
  13633. (let ((fun (if previous 're-search-backward 're-search-forward))
  13634. (pos (point))
  13635. (re (concat "^" outline-regexp))
  13636. level l)
  13637. (when (condition-case nil (org-back-to-heading t) (error nil))
  13638. (setq level (funcall outline-level))
  13639. (catch 'exit
  13640. (or previous (forward-char 1))
  13641. (while (funcall fun re nil t)
  13642. (setq l (funcall outline-level))
  13643. (when (< l level) (goto-char pos) (throw 'exit nil))
  13644. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13645. (goto-char pos)
  13646. nil))))
  13647. (defun org-show-siblings ()
  13648. "Show all siblings of the current headline."
  13649. (save-excursion
  13650. (while (org-goto-sibling) (org-flag-heading nil)))
  13651. (save-excursion
  13652. (while (org-goto-sibling 'previous)
  13653. (org-flag-heading nil))))
  13654. (defun org-show-hidden-entry ()
  13655. "Show an entry where even the heading is hidden."
  13656. (save-excursion
  13657. (org-show-entry)))
  13658. (defun org-flag-heading (flag &optional entry)
  13659. "Flag the current heading. FLAG non-nil means make invisible.
  13660. When ENTRY is non-nil, show the entire entry."
  13661. (save-excursion
  13662. (org-back-to-heading t)
  13663. ;; Check if we should show the entire entry
  13664. (if entry
  13665. (progn
  13666. (org-show-entry)
  13667. (save-excursion
  13668. (and (outline-next-heading)
  13669. (org-flag-heading nil))))
  13670. (outline-flag-region (max (point-min) (1- (point)))
  13671. (save-excursion (outline-end-of-heading) (point))
  13672. flag))))
  13673. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13674. ;; This is an exact copy of the original function, but it uses
  13675. ;; `org-back-to-heading', to make it work also in invisible
  13676. ;; trees. And is uses an invisible-OK argument.
  13677. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13678. (org-back-to-heading invisible-OK)
  13679. (let ((first t)
  13680. (level (funcall outline-level)))
  13681. (while (and (not (eobp))
  13682. (or first (> (funcall outline-level) level)))
  13683. (setq first nil)
  13684. (outline-next-heading))
  13685. (unless to-heading
  13686. (if (memq (preceding-char) '(?\n ?\^M))
  13687. (progn
  13688. ;; Go to end of line before heading
  13689. (forward-char -1)
  13690. (if (memq (preceding-char) '(?\n ?\^M))
  13691. ;; leave blank line before heading
  13692. (forward-char -1))))))
  13693. (point))
  13694. (defun org-show-subtree ()
  13695. "Show everything after this heading at deeper levels."
  13696. (outline-flag-region
  13697. (point)
  13698. (save-excursion
  13699. (outline-end-of-subtree) (outline-next-heading) (point))
  13700. nil))
  13701. (defun org-show-entry ()
  13702. "Show the body directly following this heading.
  13703. Show the heading too, if it is currently invisible."
  13704. (interactive)
  13705. (save-excursion
  13706. (condition-case nil
  13707. (progn
  13708. (org-back-to-heading t)
  13709. (outline-flag-region
  13710. (max (point-min) (1- (point)))
  13711. (save-excursion
  13712. (re-search-forward
  13713. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13714. (or (match-beginning 1) (point-max)))
  13715. nil))
  13716. (error nil))))
  13717. (defun org-make-options-regexp (kwds)
  13718. "Make a regular expression for keyword lines."
  13719. (concat
  13720. "^"
  13721. "#?[ \t]*\\+\\("
  13722. (mapconcat 'regexp-quote kwds "\\|")
  13723. "\\):[ \t]*"
  13724. "\\(.+\\)"))
  13725. ;; Make isearch reveal the necessary context
  13726. (defun org-isearch-end ()
  13727. "Reveal context after isearch exits."
  13728. (when isearch-success ; only if search was successful
  13729. (if (featurep 'xemacs)
  13730. ;; Under XEmacs, the hook is run in the correct place,
  13731. ;; we directly show the context.
  13732. (org-show-context 'isearch)
  13733. ;; In Emacs the hook runs *before* restoring the overlays.
  13734. ;; So we have to use a one-time post-command-hook to do this.
  13735. ;; (Emacs 22 has a special variable, see function `org-mode')
  13736. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13737. isearch-mode-end-hook-quit)
  13738. ;; Only when the isearch was not quitted.
  13739. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13740. 'append 'local)))))
  13741. (defun org-isearch-post-command ()
  13742. "Remove self from hook, and show context."
  13743. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13744. (org-show-context 'isearch))
  13745. ;;;; Integration with and fixes for other packages
  13746. ;;; Imenu support
  13747. (defvar org-imenu-markers nil
  13748. "All markers currently used by Imenu.")
  13749. (make-variable-buffer-local 'org-imenu-markers)
  13750. (defun org-imenu-new-marker (&optional pos)
  13751. "Return a new marker for use by Imenu, and remember the marker."
  13752. (let ((m (make-marker)))
  13753. (move-marker m (or pos (point)))
  13754. (push m org-imenu-markers)
  13755. m))
  13756. (defun org-imenu-get-tree ()
  13757. "Produce the index for Imenu."
  13758. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13759. (setq org-imenu-markers nil)
  13760. (let* ((n org-imenu-depth)
  13761. (re (concat "^" outline-regexp))
  13762. (subs (make-vector (1+ n) nil))
  13763. (last-level 0)
  13764. m tree level head)
  13765. (save-excursion
  13766. (save-restriction
  13767. (widen)
  13768. (goto-char (point-max))
  13769. (while (re-search-backward re nil t)
  13770. (setq level (org-reduced-level (funcall outline-level)))
  13771. (when (<= level n)
  13772. (looking-at org-complex-heading-regexp)
  13773. (setq head (org-match-string-no-properties 4)
  13774. m (org-imenu-new-marker))
  13775. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13776. (if (>= level last-level)
  13777. (push (cons head m) (aref subs level))
  13778. (push (cons head (aref subs (1+ level))) (aref subs level))
  13779. (loop for i from (1+ level) to n do (aset subs i nil)))
  13780. (setq last-level level)))))
  13781. (aref subs 1)))
  13782. (eval-after-load "imenu"
  13783. '(progn
  13784. (add-hook 'imenu-after-jump-hook
  13785. (lambda () (org-show-context 'org-goto)))))
  13786. ;; Speedbar support
  13787. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13788. "Overlay marking the agenda restriction line in speedbar.")
  13789. (org-overlay-put org-speedbar-restriction-lock-overlay
  13790. 'face 'org-agenda-restriction-lock)
  13791. (org-overlay-put org-speedbar-restriction-lock-overlay
  13792. 'help-echo "Agendas are currently limited to this item.")
  13793. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13794. (defun org-speedbar-set-agenda-restriction ()
  13795. "Restrict future agenda commands to the location at point in speedbar.
  13796. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13797. (interactive)
  13798. (require 'org-agenda)
  13799. (let (p m tp np dir txt w)
  13800. (cond
  13801. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13802. 'org-imenu t))
  13803. (setq m (get-text-property p 'org-imenu-marker))
  13804. (save-excursion
  13805. (save-restriction
  13806. (set-buffer (marker-buffer m))
  13807. (goto-char m)
  13808. (org-agenda-set-restriction-lock 'subtree))))
  13809. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13810. 'speedbar-function 'speedbar-find-file))
  13811. (setq tp (previous-single-property-change
  13812. (1+ p) 'speedbar-function)
  13813. np (next-single-property-change
  13814. tp 'speedbar-function)
  13815. dir (speedbar-line-directory)
  13816. txt (buffer-substring-no-properties (or tp (point-min))
  13817. (or np (point-max))))
  13818. (save-excursion
  13819. (save-restriction
  13820. (set-buffer (find-file-noselect
  13821. (let ((default-directory dir))
  13822. (expand-file-name txt))))
  13823. (unless (org-mode-p)
  13824. (error "Cannot restrict to non-Org-mode file"))
  13825. (org-agenda-set-restriction-lock 'file))))
  13826. (t (error "Don't know how to restrict Org-mode's agenda")))
  13827. (org-move-overlay org-speedbar-restriction-lock-overlay
  13828. (point-at-bol) (point-at-eol))
  13829. (setq current-prefix-arg nil)
  13830. (org-agenda-maybe-redo)))
  13831. (eval-after-load "speedbar"
  13832. '(progn
  13833. (speedbar-add-supported-extension ".org")
  13834. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13835. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13836. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13837. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13838. (add-hook 'speedbar-visiting-tag-hook
  13839. (lambda () (org-show-context 'org-goto)))))
  13840. ;;; Fixes and Hacks for problems with other packages
  13841. ;; Make flyspell not check words in links, to not mess up our keymap
  13842. (defun org-mode-flyspell-verify ()
  13843. "Don't let flyspell put overlays at active buttons."
  13844. (not (get-text-property (point) 'keymap)))
  13845. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13846. (eval-after-load "bookmark"
  13847. '(if (boundp 'bookmark-after-jump-hook)
  13848. ;; We can use the hook
  13849. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13850. ;; Hook not available, use advice
  13851. (defadvice bookmark-jump (after org-make-visible activate)
  13852. "Make the position visible."
  13853. (org-bookmark-jump-unhide))))
  13854. (defun org-bookmark-jump-unhide ()
  13855. "Unhide the current position, to show the bookmark location."
  13856. (and (org-mode-p)
  13857. (or (org-invisible-p)
  13858. (save-excursion (goto-char (max (point-min) (1- (point))))
  13859. (org-invisible-p)))
  13860. (org-show-context 'bookmark-jump)))
  13861. ;; Make session.el ignore our circular variable
  13862. (eval-after-load "session"
  13863. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13864. ;;;; Experimental code
  13865. (defun org-closed-in-range ()
  13866. "Sparse tree of items closed in a certain time range.
  13867. Still experimental, may disappear in the future."
  13868. (interactive)
  13869. ;; Get the time interval from the user.
  13870. (let* ((time1 (time-to-seconds
  13871. (org-read-date nil 'to-time nil "Starting date: ")))
  13872. (time2 (time-to-seconds
  13873. (org-read-date nil 'to-time nil "End date:")))
  13874. ;; callback function
  13875. (callback (lambda ()
  13876. (let ((time
  13877. (time-to-seconds
  13878. (apply 'encode-time
  13879. (org-parse-time-string
  13880. (match-string 1))))))
  13881. ;; check if time in interval
  13882. (and (>= time time1) (<= time time2))))))
  13883. ;; make tree, check each match with the callback
  13884. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13885. ;;;; Finish up
  13886. (provide 'org)
  13887. (run-hooks 'org-load-hook)
  13888. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13889. ;;; org.el ends here